Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
| 12 #include "third_party/skia/include/effects/SkGradientShader.h" | 12 #include "third_party/skia/include/effects/SkGradientShader.h" |
| 13 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gfx/font.h" | 14 #include "ui/gfx/font.h" |
| 15 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
| 16 #include "ui/gfx/skia_util.h" | 16 #include "ui/gfx/skia_util.h" |
| 17 #include "ui/gfx/transform.h" | 17 #include "ui/gfx/transform.h" |
| 18 | 18 |
| 19 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
| 20 #include "ui/gfx/canvas_skia_paint.h" | 20 #include "ui/gfx/canvas_skia_paint.h" |
| 21 #endif | 21 #endif |
| 22 | 22 |
| 23 namespace gfx { | 23 namespace gfx { |
| 24 | 24 |
| 25 Canvas::Canvas(const gfx::Size& size, bool is_opaque) | |
| 26 : owned_canvas_(new skia::PlatformCanvas(size.width(), size.height(), | |
| 27 is_opaque)), | |
| 28 canvas_(owned_canvas_.get()) { | |
| 29 #if defined(OS_WIN) || defined(OS_MACOSX) | |
| 30 // skia::PlatformCanvas instances are initialized to 0 by Cairo on Linux, but | |
| 31 // uninitialized on Win and Mac. | |
| 32 if (!is_opaque) | |
| 33 owned_canvas_->clear(SkColorSetARGB(0, 0, 0, 0)); | |
| 34 #endif | |
| 35 | |
| 36 ApplyScaleFactor(ui::SCALE_FACTOR_100P, false); | |
| 37 } | |
| 38 | |
| 39 Canvas::Canvas(const gfx::Size& size, | 25 Canvas::Canvas(const gfx::Size& size, |
| 40 ui::ScaleFactor scale_factor, | 26 ui::ScaleFactor scale_factor, |
| 41 bool is_opaque) { | 27 bool is_opaque) |
| 28 : scale_factor_(scale_factor) { | |
| 42 gfx::Size pixel_size = size.Scale(ui::GetScaleFactorScale(scale_factor)); | 29 gfx::Size pixel_size = size.Scale(ui::GetScaleFactorScale(scale_factor)); |
|
sky
2012/07/16 14:26:51
Member initialize canvas_ to NULL.
pkotwicz
2012/07/17 17:19:52
canvas_ is initialized in line 30 as part of the c
sky
2012/07/17 21:58:32
Yes.
| |
| 43 owned_canvas_.reset(new skia::PlatformCanvas(pixel_size.width(), | 30 owned_canvas_.reset(new skia::PlatformCanvas(pixel_size.width(), |
| 44 pixel_size.height(), | 31 pixel_size.height(), |
| 45 is_opaque)); | 32 is_opaque)); |
| 46 canvas_ = owned_canvas_.get(); | 33 canvas_ = owned_canvas_.get(); |
| 47 #if defined(OS_WIN) || defined(OS_MACOSX) | 34 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 48 // skia::PlatformCanvas instances are initialized to 0 by Cairo on Linux, but | 35 // skia::PlatformCanvas instances are initialized to 0 by Cairo on Linux, but |
| 49 // uninitialized on Win and Mac. | 36 // uninitialized on Win and Mac. |
| 50 if (!is_opaque) | 37 if (!is_opaque) |
| 51 owned_canvas_->clear(SkColorSetARGB(0, 0, 0, 0)); | 38 owned_canvas_->clear(SkColorSetARGB(0, 0, 0, 0)); |
| 52 #endif | 39 #endif |
| 53 | 40 |
| 54 ApplyScaleFactor(scale_factor, true); | 41 scale_factor_ = scale_factor; |
|
sky
2012/07/16 14:26:51
Doesn't line 28 set the scale_factor_?
pkotwicz
2012/07/17 17:19:52
Done.
| |
| 42 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor)); | |
| 43 canvas_->scale(scale, scale); | |
| 55 } | 44 } |
| 56 | 45 |
| 57 Canvas::Canvas(const gfx::ImageSkiaRep& image_rep, bool is_opaque) | 46 Canvas::Canvas(const gfx::ImageSkiaRep& image_rep, bool is_opaque) |
| 58 : owned_canvas_(new skia::PlatformCanvas(image_rep.pixel_width(), | 47 : scale_factor_(image_rep.scale_factor()), |
| 48 owned_canvas_(new skia::PlatformCanvas(image_rep.pixel_width(), | |
| 59 image_rep.pixel_height(), | 49 image_rep.pixel_height(), |
| 60 is_opaque)), | 50 is_opaque)), |
| 61 canvas_(owned_canvas_.get()) { | 51 canvas_(owned_canvas_.get()) { |
| 62 ApplyScaleFactor(image_rep.scale_factor(), true); | 52 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor_)); |
| 53 canvas_->scale(scale, scale); | |
| 63 DrawImageInt(gfx::ImageSkia(image_rep), 0, 0); | 54 DrawImageInt(gfx::ImageSkia(image_rep), 0, 0); |
| 64 } | 55 } |
| 65 | 56 |
| 66 Canvas::Canvas() | 57 Canvas::Canvas() |
| 67 : owned_canvas_(new skia::PlatformCanvas()), | 58 : scale_factor_(ui::SCALE_FACTOR_100P), |
| 59 owned_canvas_(new skia::PlatformCanvas()), | |
| 68 canvas_(owned_canvas_.get()) { | 60 canvas_(owned_canvas_.get()) { |
| 69 ApplyScaleFactor(ui::SCALE_FACTOR_100P, false); | |
| 70 } | 61 } |
| 71 | 62 |
| 72 Canvas::Canvas(SkCanvas* canvas, | 63 Canvas::Canvas(SkCanvas* canvas, ui::ScaleFactor scale_factor) |
|
sky
2012/07/16 14:26:51
Move to match position in header.
pkotwicz
2012/07/17 17:19:52
Done.
| |
| 73 ui::ScaleFactor scale_factor, | 64 : scale_factor_(scale_factor), |
| 74 bool scale_canvas) | 65 owned_canvas_(), |
| 75 : owned_canvas_(), | |
| 76 canvas_(canvas) { | 66 canvas_(canvas) { |
| 77 DCHECK(canvas); | 67 DCHECK(canvas); |
| 78 ApplyScaleFactor(scale_factor, scale_canvas); | |
| 79 } | 68 } |
| 80 | 69 |
| 81 Canvas::~Canvas() { | 70 Canvas::~Canvas() { |
| 82 if (scale_factor_scales_canvas_) { | 71 } |
| 83 SkScalar scale = 1.0f / ui::GetScaleFactorScale(scale_factor_); | 72 |
| 84 canvas_->scale(scale, scale); | 73 // static |
| 85 } | 74 Canvas* Canvas::CreateCanvasWithoutScaling(SkCanvas* canvas, |
| 75 ui::ScaleFactor scale_factor) { | |
|
sky
2012/07/16 14:26:51
nit: spacing
pkotwicz
2012/07/17 17:19:52
Done.
| |
| 76 return new Canvas(canvas, scale_factor); | |
| 86 } | 77 } |
| 87 | 78 |
| 88 void Canvas::RecreateBackingCanvas(const gfx::Size& size, | 79 void Canvas::RecreateBackingCanvas(const gfx::Size& size, |
| 89 ui::ScaleFactor scale_factor, | 80 ui::ScaleFactor scale_factor, |
| 90 bool is_opaque) { | 81 bool is_opaque) { |
| 82 scale_factor_ = scale_factor; | |
| 91 gfx::Size pixel_size = size.Scale(ui::GetScaleFactorScale(scale_factor)); | 83 gfx::Size pixel_size = size.Scale(ui::GetScaleFactorScale(scale_factor)); |
| 92 owned_canvas_.reset(new skia::PlatformCanvas(pixel_size.width(), | 84 owned_canvas_.reset(new skia::PlatformCanvas(pixel_size.width(), |
| 93 pixel_size.height(), | 85 pixel_size.height(), |
| 94 is_opaque)); | 86 is_opaque)); |
| 95 canvas_ = owned_canvas_.get(); | 87 canvas_ = owned_canvas_.get(); |
| 96 | 88 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor_)); |
| 97 ApplyScaleFactor(scale_factor, true); | 89 canvas_->scale(scale, scale); |
| 98 } | 90 } |
| 99 | 91 |
| 100 // static | 92 // static |
| 101 int Canvas::GetStringWidth(const string16& text, const gfx::Font& font) { | 93 int Canvas::GetStringWidth(const string16& text, const gfx::Font& font) { |
| 102 int width = 0, height = 0; | 94 int width = 0, height = 0; |
| 103 Canvas::SizeStringInt(text, font, &width, &height, NO_ELLIPSIS); | 95 Canvas::SizeStringInt(text, font, &width, &height, NO_ELLIPSIS); |
| 104 return width; | 96 return width; |
| 105 } | 97 } |
| 106 | 98 |
| 107 // static | 99 // static |
| 108 int Canvas::DefaultCanvasTextAlignment() { | 100 int Canvas::DefaultCanvasTextAlignment() { |
| 109 return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT; | 101 return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT; |
| 110 } | 102 } |
| 111 | 103 |
| 112 SkBitmap Canvas::ExtractBitmap() const { | 104 gfx::ImageSkiaRep Canvas::ExtractImageRep() const { |
| 113 const SkBitmap& device_bitmap = canvas_->getDevice()->accessBitmap(false); | 105 const SkBitmap& device_bitmap = canvas_->getDevice()->accessBitmap(false); |
| 114 | 106 |
| 115 // Make a bitmap to return, and a canvas to draw into it. We don't just want | 107 // Make a bitmap to return, and a canvas to draw into it. We don't just want |
| 116 // to call extractSubset or the copy constructor, since we want an actual copy | 108 // to call extractSubset or the copy constructor, since we want an actual copy |
| 117 // of the bitmap. | 109 // of the bitmap. |
| 118 SkBitmap result; | 110 SkBitmap result; |
| 119 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); | 111 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); |
| 120 return result; | |
| 121 } | |
| 122 | 112 |
| 123 gfx::ImageSkiaRep Canvas::ExtractImageSkiaRep() const { | 113 return gfx::ImageSkiaRep(result, scale_factor_); |
| 124 return gfx::ImageSkiaRep(ExtractBitmap(), scale_factor_); | |
| 125 } | 114 } |
| 126 | 115 |
| 127 void Canvas::DrawDashedRect(const gfx::Rect& rect, SkColor color) { | 116 void Canvas::DrawDashedRect(const gfx::Rect& rect, SkColor color) { |
| 128 // Create a 2D bitmap containing alternating on/off pixels - we do this | 117 // Create a 2D bitmap containing alternating on/off pixels - we do this |
| 129 // so that you never get two pixels of the same color around the edges | 118 // so that you never get two pixels of the same color around the edges |
| 130 // of the focus rect (this may mean that opposing edges of the rect may | 119 // of the focus rect (this may mean that opposing edges of the rect may |
| 131 // have a dot pattern out of phase to each other). | 120 // have a dot pattern out of phase to each other). |
| 132 static SkColor last_color; | 121 static SkColor last_color; |
| 133 static SkBitmap* dots = NULL; | 122 static SkBitmap* dots = NULL; |
| 134 if (!dots || last_color != color) { | 123 if (!dots || last_color != color) { |
| (...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 506 return canvas_->getClipBounds(&clip) && | 495 return canvas_->getClipBounds(&clip) && |
| 507 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), | 496 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), |
| 508 SkIntToScalar(y + h)); | 497 SkIntToScalar(y + h)); |
| 509 } | 498 } |
| 510 | 499 |
| 511 bool Canvas::IntersectsClipRect(const gfx::Rect& rect) { | 500 bool Canvas::IntersectsClipRect(const gfx::Rect& rect) { |
| 512 return IntersectsClipRectInt(rect.x(), rect.y(), | 501 return IntersectsClipRectInt(rect.x(), rect.y(), |
| 513 rect.width(), rect.height()); | 502 rect.width(), rect.height()); |
| 514 } | 503 } |
| 515 | 504 |
| 516 void Canvas::ApplyScaleFactor(ui::ScaleFactor scale_factor, | |
| 517 bool scale_canvas) { | |
| 518 scale_factor_scales_canvas_ = scale_canvas; | |
| 519 scale_factor_ = scale_factor; | |
| 520 if (scale_canvas) { | |
| 521 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor)); | |
| 522 canvas_->scale(scale, scale); | |
| 523 } | |
| 524 } | |
| 525 | |
| 526 const gfx::ImageSkiaRep& Canvas::GetImageRepToPaint( | 505 const gfx::ImageSkiaRep& Canvas::GetImageRepToPaint( |
| 527 const gfx::ImageSkia& image) const { | 506 const gfx::ImageSkia& image) const { |
| 528 return GetImageRepToPaint(image, 1.0f, 1.0f); | 507 return GetImageRepToPaint(image, 1.0f, 1.0f); |
| 529 } | 508 } |
| 530 | 509 |
| 531 const gfx::ImageSkiaRep& Canvas::GetImageRepToPaint( | 510 const gfx::ImageSkiaRep& Canvas::GetImageRepToPaint( |
| 532 const gfx::ImageSkia& image, | 511 const gfx::ImageSkia& image, |
| 533 float user_additional_scale_x, | 512 float user_additional_scale_x, |
| 534 float user_additional_scale_y) const { | 513 float user_additional_scale_y) const { |
| 535 const gfx::ImageSkiaRep& image_rep = image.GetRepresentation(scale_factor_); | 514 const gfx::ImageSkiaRep& image_rep = image.GetRepresentation(scale_factor_); |
| 536 | 515 |
| 537 if (!image_rep.is_null()) { | 516 if (!image_rep.is_null()) { |
| 538 SkMatrix m = canvas_->getTotalMatrix(); | 517 SkMatrix m = canvas_->getTotalMatrix(); |
| 539 float scale_x = SkScalarToFloat(SkScalarAbs(m.getScaleX())) * | 518 float scale_x = SkScalarToFloat(SkScalarAbs(m.getScaleX())) * |
| 540 user_additional_scale_x; | 519 user_additional_scale_x; |
| 541 float scale_y = SkScalarToFloat(SkScalarAbs(m.getScaleY())) * | 520 float scale_y = SkScalarToFloat(SkScalarAbs(m.getScaleY())) * |
| 542 user_additional_scale_y; | 521 user_additional_scale_y; |
| 543 | 522 |
| 544 float bitmap_scale = image_rep.GetScale(); | 523 float bitmap_scale = image_rep.GetScale(); |
| 545 if (scale_x < bitmap_scale || scale_y < bitmap_scale) | 524 if (scale_x < bitmap_scale || scale_y < bitmap_scale) |
| 546 const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap(); | 525 const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap(); |
| 547 } | 526 } |
| 548 | 527 |
| 549 return image_rep; | 528 return image_rep; |
| 550 } | 529 } |
| 551 | 530 |
| 552 } // namespace gfx | 531 } // namespace gfx |
| OLD | NEW |