Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(302)

Side by Side Diff: ui/gfx/canvas.cc

Issue 303543004: MacViews: views_examples_with_content_exe working! Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add files Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/events/cocoa/events_mac.mm ('k') | ui/gfx/gfx.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/canvas.h" 5 #include "ui/gfx/canvas.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <limits> 8 #include <limits>
9 9
10 #include "base/i18n/rtl.h" 10 #include "base/i18n/rtl.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "third_party/skia/include/core/SkBitmap.h" 12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "third_party/skia/include/effects/SkGradientShader.h" 13 #include "third_party/skia/include/effects/SkGradientShader.h"
14 #include "ui/gfx/font_list.h" 14 #include "ui/gfx/font_list.h"
15 #include "ui/gfx/rect.h" 15 #include "ui/gfx/rect.h"
16 #include "ui/gfx/size_conversions.h" 16 #include "ui/gfx/size_conversions.h"
17 #include "ui/gfx/skia_util.h" 17 #include "ui/gfx/skia_util.h"
18 #include "ui/gfx/transform.h" 18 #include "ui/gfx/transform.h"
19 19
20 #if defined(OS_WIN) 20 #if defined(OS_WIN)
21 #include "ui/gfx/canvas_skia_paint.h" 21 #include "ui/gfx/canvas_skia_paint.h"
22 #endif 22 #endif
23 23
24 namespace gfx { 24 namespace gfx {
25 25
26 Canvas::Canvas(const Size& size, float image_scale, bool is_opaque) 26 Canvas::Canvas(const Size& size, float image_scale, bool is_opaque)
27 : image_scale_(image_scale), 27 : image_scale_(image_scale),
28 canvas_(NULL) { 28 canvas_(NULL) {
29 DCHECK_NE(0.0, image_scale_);
29 Size pixel_size = ToCeiledSize(ScaleSize(size, image_scale)); 30 Size pixel_size = ToCeiledSize(ScaleSize(size, image_scale));
30 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(), 31 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(),
31 pixel_size.height(), 32 pixel_size.height(),
32 is_opaque)); 33 is_opaque));
33 canvas_ = owned_canvas_.get(); 34 canvas_ = owned_canvas_.get();
34 #if defined(OS_WIN) || defined(OS_MACOSX) 35 #if defined(OS_WIN) || defined(OS_MACOSX)
35 // skia::PlatformCanvas instances are initialized to 0 by Cairo on Linux, but 36 // skia::PlatformCanvas instances are initialized to 0 by Cairo on Linux, but
36 // uninitialized on Win and Mac. 37 // uninitialized on Win and Mac.
37 if (!is_opaque) 38 if (!is_opaque)
38 owned_canvas_->clear(SkColorSetARGB(0, 0, 0, 0)); 39 owned_canvas_->clear(SkColorSetARGB(0, 0, 0, 0));
39 #endif 40 #endif
40 41
41 SkScalar scale_scalar = SkFloatToScalar(image_scale); 42 SkScalar scale_scalar = SkFloatToScalar(image_scale);
42 canvas_->scale(scale_scalar, scale_scalar); 43 canvas_->scale(scale_scalar, scale_scalar);
43 } 44 }
44 45
45 Canvas::Canvas(const ImageSkiaRep& image_rep, bool is_opaque) 46 Canvas::Canvas(const ImageSkiaRep& image_rep, bool is_opaque)
46 : image_scale_(image_rep.scale()), 47 : image_scale_(image_rep.scale()),
47 owned_canvas_(skia::AdoptRef( 48 owned_canvas_(skia::AdoptRef(
48 skia::CreatePlatformCanvas(image_rep.pixel_width(), 49 skia::CreatePlatformCanvas(image_rep.pixel_width(),
49 image_rep.pixel_height(), 50 image_rep.pixel_height(),
50 is_opaque))), 51 is_opaque))),
51 canvas_(owned_canvas_.get()) { 52 canvas_(owned_canvas_.get()) {
53 DCHECK_NE(0.0, image_scale_);
52 SkScalar scale_scalar = SkFloatToScalar(image_scale_); 54 SkScalar scale_scalar = SkFloatToScalar(image_scale_);
53 canvas_->scale(scale_scalar, scale_scalar); 55 canvas_->scale(scale_scalar, scale_scalar);
54 DrawImageInt(ImageSkia(image_rep), 0, 0); 56 DrawImageInt(ImageSkia(image_rep), 0, 0);
55 } 57 }
56 58
57 Canvas::Canvas() 59 Canvas::Canvas()
58 : image_scale_(1.0), 60 : image_scale_(1.0),
59 owned_canvas_(skia::AdoptRef(skia::CreatePlatformCanvas(0, 0, false))), 61 owned_canvas_(skia::AdoptRef(skia::CreatePlatformCanvas(0, 0, false))),
60 canvas_(owned_canvas_.get()) { 62 canvas_(owned_canvas_.get()) {
61 } 63 }
62 64
63 Canvas::~Canvas() { 65 Canvas::~Canvas() {
64 } 66 }
65 67
66 // static 68 // static
67 Canvas* Canvas::CreateCanvasWithoutScaling(SkCanvas* canvas, 69 Canvas* Canvas::CreateCanvasWithoutScaling(SkCanvas* canvas,
68 float image_scale) { 70 float image_scale) {
69 return new Canvas(canvas, image_scale); 71 return new Canvas(canvas, image_scale);
70 } 72 }
71 73
72 void Canvas::RecreateBackingCanvas(const Size& size, 74 void Canvas::RecreateBackingCanvas(const Size& size,
73 float image_scale, 75 float image_scale,
74 bool is_opaque) { 76 bool is_opaque) {
75 image_scale_ = image_scale; 77 image_scale_ = image_scale;
78 DCHECK_NE(0.0, image_scale_);
76 Size pixel_size = ToFlooredSize(ScaleSize(size, image_scale)); 79 Size pixel_size = ToFlooredSize(ScaleSize(size, image_scale));
77 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(), 80 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(),
78 pixel_size.height(), 81 pixel_size.height(),
79 is_opaque)); 82 is_opaque));
80 canvas_ = owned_canvas_.get(); 83 canvas_ = owned_canvas_.get();
81 SkScalar scale_scalar = SkFloatToScalar(image_scale); 84 SkScalar scale_scalar = SkFloatToScalar(image_scale);
82 canvas_->scale(scale_scalar, scale_scalar); 85 canvas_->scale(scale_scalar, scale_scalar);
83 } 86 }
84 87
85 // static 88 // static
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y, uint8 a) { 331 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y, uint8 a) {
329 SkPaint paint; 332 SkPaint paint;
330 paint.setAlpha(a); 333 paint.setAlpha(a);
331 DrawImageInt(image, x, y, paint); 334 DrawImageInt(image, x, y, paint);
332 } 335 }
333 336
334 void Canvas::DrawImageInt(const ImageSkia& image, 337 void Canvas::DrawImageInt(const ImageSkia& image,
335 int x, 338 int x,
336 int y, 339 int y,
337 const SkPaint& paint) { 340 const SkPaint& paint) {
341 if (image_scale_ == 0)
342 DLOG(ERROR) << "0 image scale??";
343 image_scale_ = 1;
338 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); 344 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_);
339 if (image_rep.is_null()) 345 if (image_rep.is_null())
340 return; 346 return;
341 const SkBitmap& bitmap = image_rep.sk_bitmap(); 347 const SkBitmap& bitmap = image_rep.sk_bitmap();
342 float bitmap_scale = image_rep.scale(); 348 float bitmap_scale = image_rep.scale();
343 349
344 canvas_->save(); 350 canvas_->save();
345 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), 351 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale),
346 SkFloatToScalar(1.0f / bitmap_scale)); 352 SkFloatToScalar(1.0f / bitmap_scale));
347 canvas_->drawBitmap(bitmap, 353 canvas_->drawBitmap(bitmap,
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
628 SkPaint p(paint); 634 SkPaint p(paint);
629 p.setFilterLevel(filter ? SkPaint::kLow_FilterLevel 635 p.setFilterLevel(filter ? SkPaint::kLow_FilterLevel
630 : SkPaint::kNone_FilterLevel); 636 : SkPaint::kNone_FilterLevel);
631 p.setShader(shader.get()); 637 p.setShader(shader.get());
632 638
633 // The rect will be filled by the bitmap. 639 // The rect will be filled by the bitmap.
634 canvas_->drawRect(dest_rect, p); 640 canvas_->drawRect(dest_rect, p);
635 } 641 }
636 642
637 } // namespace gfx 643 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/events/cocoa/events_mac.mm ('k') | ui/gfx/gfx.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698