[email protected] | fa8cfb0 | 2012-01-13 00:27:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 5 | #include "ui/gfx/canvas.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 7 | #include <cmath> |
[email protected] | e1981f43 | 2008-08-12 15:22:13 | [diff] [blame] | 8 | #include <limits> |
| 9 | |
[email protected] | 7cf1b6ce | 2010-03-20 06:37:01 | [diff] [blame] | 10 | #include "base/i18n/rtl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | 757a8eb4 | 2011-12-21 20:25:22 | [diff] [blame] | 12 | #include "third_party/skia/include/core/SkBitmap.h" |
pkasting | 954f76a | 2016-02-25 01:11:00 | [diff] [blame] | 13 | #include "third_party/skia/include/core/SkPath.h" |
tomhudson | 399950b | 2016-05-11 11:00:13 | [diff] [blame] | 14 | #include "third_party/skia/include/core/SkRefCnt.h" |
[email protected] | 0f4fe84 | 2010-07-23 22:23:59 | [diff] [blame] | 15 | #include "third_party/skia/include/effects/SkGradientShader.h" |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 16 | #include "ui/gfx/font_list.h" |
tfarina | 3b0452d | 2014-12-31 15:20:09 | [diff] [blame] | 17 | #include "ui/gfx/geometry/rect.h" |
sky | e7da604 | 2014-09-10 22:19:37 | [diff] [blame] | 18 | #include "ui/gfx/geometry/rect_conversions.h" |
mgiuca | 9a713fe1 | 2015-11-10 02:53:30 | [diff] [blame] | 19 | #include "ui/gfx/geometry/rect_f.h" |
sky | 005605e | 2014-10-02 20:35:12 | [diff] [blame] | 20 | #include "ui/gfx/geometry/safe_integer_conversions.h" |
tfarina | ebe974f0 | 2015-01-03 04:25:32 | [diff] [blame] | 21 | #include "ui/gfx/geometry/size_conversions.h" |
sky | 005605e | 2014-10-02 20:35:12 | [diff] [blame] | 22 | #include "ui/gfx/scoped_canvas.h" |
[email protected] | 79ea54a | 2011-10-26 01:47:36 | [diff] [blame] | 23 | #include "ui/gfx/skia_util.h" |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 24 | #include "ui/gfx/transform.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | |
[email protected] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 26 | namespace gfx { |
| 27 | |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame] | 28 | Canvas::Canvas(const Size& size, float image_scale, bool is_opaque) |
tomhudson | 748157f7 | 2016-02-04 18:49:56 | [diff] [blame] | 29 | : image_scale_(image_scale) { |
danakj | ddaec91 | 2015-09-25 19:38:40 | [diff] [blame] | 30 | Size pixel_size = ScaleToCeiledSize(size, image_scale); |
tomhudson | 399950b | 2016-05-11 11:00:13 | [diff] [blame] | 31 | canvas_ = sk_sp<SkCanvas>(skia::CreatePlatformCanvas(pixel_size.width(), |
| 32 | pixel_size.height(), |
| 33 | is_opaque)); |
moshayedi | 6a8df30 | 2015-10-21 20:02:57 | [diff] [blame] | 34 | #if !defined(USE_CAIRO) |
| 35 | // skia::PlatformCanvas instances are initialized to 0 by Cairo, but |
| 36 | // uninitialized on other platforms. |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 37 | if (!is_opaque) |
tomhudson | 748157f7 | 2016-02-04 18:49:56 | [diff] [blame] | 38 | canvas_->clear(SkColorSetARGB(0, 0, 0, 0)); |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 39 | #endif |
| 40 | |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame] | 41 | SkScalar scale_scalar = SkFloatToScalar(image_scale); |
| 42 | canvas_->scale(scale_scalar, scale_scalar); |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 43 | } |
| 44 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 45 | Canvas::Canvas() |
danakj | 4bfbb34 | 2015-10-29 23:38:25 | [diff] [blame] | 46 | : image_scale_(1.f), |
tomhudson | 399950b | 2016-05-11 11:00:13 | [diff] [blame] | 47 | canvas_(sk_sp<SkCanvas>(skia::CreatePlatformCanvas(0, 0, false))) {} |
[email protected] | be172ba8 | 2011-10-05 19:05:07 | [diff] [blame] | 48 | |
tomhudson | 399950b | 2016-05-11 11:00:13 | [diff] [blame] | 49 | Canvas::Canvas(sk_sp<SkCanvas> canvas, float image_scale) |
| 50 | : image_scale_(image_scale), canvas_(std::move(canvas)) { |
| 51 | DCHECK(canvas_); |
[email protected] | de13f35 | 2012-07-24 16:51:16 | [diff] [blame] | 52 | } |
| 53 | |
danakj | 8fa739b6 | 2015-05-12 02:10:06 | [diff] [blame] | 54 | Canvas::~Canvas() { |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 57 | void Canvas::RecreateBackingCanvas(const Size& size, |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame] | 58 | float image_scale, |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 59 | bool is_opaque) { |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame] | 60 | image_scale_ = image_scale; |
danakj | ddaec91 | 2015-09-25 19:38:40 | [diff] [blame] | 61 | Size pixel_size = ScaleToFlooredSize(size, image_scale); |
tomhudson | 399950b | 2016-05-11 11:00:13 | [diff] [blame] | 62 | canvas_ = sk_sp<SkCanvas>(skia::CreatePlatformCanvas(pixel_size.width(), |
| 63 | pixel_size.height(), |
| 64 | is_opaque)); |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame] | 65 | SkScalar scale_scalar = SkFloatToScalar(image_scale); |
| 66 | canvas_->scale(scale_scalar, scale_scalar); |
[email protected] | be172ba8 | 2011-10-05 19:05:07 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | f286f77 | 2010-07-21 18:28:20 | [diff] [blame] | 69 | // static |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 70 | void Canvas::SizeStringInt(const base::string16& text, |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 71 | const FontList& font_list, |
| 72 | int* width, |
| 73 | int* height, |
| 74 | int line_height, |
| 75 | int flags) { |
pkasting | bc91db5 | 2014-10-21 20:35:42 | [diff] [blame] | 76 | float fractional_width = static_cast<float>(*width); |
| 77 | float factional_height = static_cast<float>(*height); |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 78 | SizeStringFloat(text, font_list, &fractional_width, |
| 79 | &factional_height, line_height, flags); |
pkasting | bc91db5 | 2014-10-21 20:35:42 | [diff] [blame] | 80 | *width = ToCeiledInt(fractional_width); |
| 81 | *height = ToCeiledInt(factional_height); |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // static |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 85 | int Canvas::GetStringWidth(const base::string16& text, |
| 86 | const FontList& font_list) { |
[email protected] | 7c61375 | 2012-01-24 21:08:51 | [diff] [blame] | 87 | int width = 0, height = 0; |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 88 | SizeStringInt(text, font_list, &width, &height, 0, NO_ELLIPSIS); |
| 89 | return width; |
| 90 | } |
| 91 | |
| 92 | // static |
[email protected] | 8ad3c5a | 2013-10-10 09:57:19 | [diff] [blame] | 93 | float Canvas::GetStringWidthF(const base::string16& text, |
| 94 | const FontList& font_list) { |
| 95 | float width = 0, height = 0; |
| 96 | SizeStringFloat(text, font_list, &width, &height, 0, NO_ELLIPSIS); |
| 97 | return width; |
| 98 | } |
| 99 | |
| 100 | // static |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 101 | int Canvas::DefaultCanvasTextAlignment() { |
| 102 | return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT; |
[email protected] | f286f77 | 2010-07-21 18:28:20 | [diff] [blame] | 103 | } |
| 104 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 105 | ImageSkiaRep Canvas::ExtractImageRep() const { |
[email protected] | 74db48eb | 2010-06-25 16:33:23 | [diff] [blame] | 106 | // Make a bitmap to return, and a canvas to draw into it. We don't just want |
| 107 | // to call extractSubset or the copy constructor, since we want an actual copy |
| 108 | // of the bitmap. |
tomhudson | 15223e9 | 2015-11-23 21:52:54 | [diff] [blame] | 109 | const SkISize size = canvas_->getBaseLayerSize(); |
[email protected] | 74db48eb | 2010-06-25 16:33:23 | [diff] [blame] | 110 | SkBitmap result; |
[email protected] | 8dca6e0 | 2014-03-04 04:21:39 | [diff] [blame] | 111 | result.allocN32Pixels(size.width(), size.height()); |
[email protected] | 74db48eb | 2010-06-25 16:33:23 | [diff] [blame] | 112 | |
[email protected] | 8dca6e0 | 2014-03-04 04:21:39 | [diff] [blame] | 113 | canvas_->readPixels(&result, 0, 0); |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame] | 114 | return ImageSkiaRep(result, image_scale_); |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 115 | } |
| 116 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 117 | void Canvas::DrawDashedRect(const Rect& rect, SkColor color) { |
[email protected] | 399be84 | 2014-07-15 01:05:15 | [diff] [blame] | 118 | if (rect.IsEmpty()) |
| 119 | return; |
[email protected] | edc844a | 2012-03-09 04:33:14 | [diff] [blame] | 120 | // Create a 2D bitmap containing alternating on/off pixels - we do this |
| 121 | // so that you never get two pixels of the same color around the edges |
| 122 | // of the focus rect (this may mean that opposing edges of the rect may |
| 123 | // have a dot pattern out of phase to each other). |
| 124 | static SkColor last_color; |
| 125 | static SkBitmap* dots = NULL; |
| 126 | if (!dots || last_color != color) { |
| 127 | int col_pixels = 32; |
| 128 | int row_pixels = 32; |
| 129 | |
| 130 | delete dots; |
| 131 | last_color = color; |
| 132 | dots = new SkBitmap; |
[email protected] | 8dca6e0 | 2014-03-04 04:21:39 | [diff] [blame] | 133 | dots->allocN32Pixels(col_pixels, row_pixels); |
[email protected] | edc844a | 2012-03-09 04:33:14 | [diff] [blame] | 134 | dots->eraseARGB(0, 0, 0, 0); |
| 135 | |
| 136 | uint32_t* dot = dots->getAddr32(0, 0); |
| 137 | for (int i = 0; i < row_pixels; i++) { |
| 138 | for (int u = 0; u < col_pixels; u++) { |
| 139 | if ((u % 2 + i % 2) % 2 != 0) { |
| 140 | dot[i * row_pixels + u] = color; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
reed | 642d8f9 | 2016-03-11 12:59:03 | [diff] [blame] | 146 | // Make a shader for the bitmap with an origin of the box we'll draw. |
[email protected] | edc844a | 2012-03-09 04:33:14 | [diff] [blame] | 147 | SkPaint paint; |
reed | 642d8f9 | 2016-03-11 12:59:03 | [diff] [blame] | 148 | paint.setShader(SkShader::MakeBitmapShader(*dots, SkShader::kRepeat_TileMode, |
| 149 | SkShader::kRepeat_TileMode)); |
[email protected] | edc844a | 2012-03-09 04:33:14 | [diff] [blame] | 150 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 151 | DrawRect(Rect(rect.x(), rect.y(), rect.width(), 1), paint); |
| 152 | DrawRect(Rect(rect.x(), rect.y() + rect.height() - 1, rect.width(), 1), |
[email protected] | edc844a | 2012-03-09 04:33:14 | [diff] [blame] | 153 | paint); |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 154 | DrawRect(Rect(rect.x(), rect.y(), 1, rect.height()), paint); |
| 155 | DrawRect(Rect(rect.x() + rect.width() - 1, rect.y(), 1, rect.height()), |
[email protected] | edc844a | 2012-03-09 04:33:14 | [diff] [blame] | 156 | paint); |
| 157 | } |
| 158 | |
danakj | da76437 | 2015-10-30 19:25:16 | [diff] [blame] | 159 | float Canvas::UndoDeviceScaleFactor() { |
pkasting | 0ca0ebd4 | 2015-10-14 02:19:02 | [diff] [blame] | 160 | SkScalar scale_factor = 1.0f / image_scale_; |
| 161 | canvas_->scale(scale_factor, scale_factor); |
| 162 | return image_scale_; |
| 163 | } |
| 164 | |
danakj | da76437 | 2015-10-30 19:25:16 | [diff] [blame] | 165 | void Canvas::Save() { |
| 166 | canvas_->save(); |
| 167 | } |
| 168 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 169 | void Canvas::SaveLayerAlpha(uint8_t alpha) { |
[email protected] | be172ba8 | 2011-10-05 19:05:07 | [diff] [blame] | 170 | canvas_->saveLayerAlpha(NULL, alpha); |
[email protected] | bd02680 | 2010-06-29 22:08:44 | [diff] [blame] | 171 | } |
| 172 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 173 | void Canvas::SaveLayerAlpha(uint8_t alpha, const Rect& layer_bounds) { |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 174 | SkRect bounds(RectToSkRect(layer_bounds)); |
[email protected] | be172ba8 | 2011-10-05 19:05:07 | [diff] [blame] | 175 | canvas_->saveLayerAlpha(&bounds, alpha); |
[email protected] | bd02680 | 2010-06-29 22:08:44 | [diff] [blame] | 176 | } |
| 177 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 178 | void Canvas::Restore() { |
[email protected] | be172ba8 | 2011-10-05 19:05:07 | [diff] [blame] | 179 | canvas_->restore(); |
[email protected] | bd02680 | 2010-06-29 22:08:44 | [diff] [blame] | 180 | } |
| 181 | |
[email protected] | a9103171 | 2014-03-03 15:41:16 | [diff] [blame] | 182 | void Canvas::ClipRect(const Rect& rect) { |
| 183 | canvas_->clipRect(RectToSkRect(rect)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | d26b905c | 2014-05-26 16:43:44 | [diff] [blame] | 186 | void Canvas::ClipPath(const SkPath& path, bool do_anti_alias) { |
| 187 | canvas_->clipPath(path, SkRegion::kIntersect_Op, do_anti_alias); |
[email protected] | a9103171 | 2014-03-03 15:41:16 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | bool Canvas::IsClipEmpty() const { |
| 191 | return canvas_->isClipEmpty(); |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 194 | bool Canvas::GetClipBounds(Rect* bounds) { |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 195 | SkRect out; |
sky | e7da604 | 2014-09-10 22:19:37 | [diff] [blame] | 196 | if (canvas_->getClipBounds(&out)) { |
| 197 | *bounds = ToEnclosingRect(SkRectToRectF(out)); |
| 198 | return true; |
| 199 | } |
| 200 | *bounds = gfx::Rect(); |
| 201 | return false; |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 204 | void Canvas::Translate(const Vector2d& offset) { |
[email protected] | ceb36f7d | 2012-10-31 18:33:24 | [diff] [blame] | 205 | canvas_->translate(SkIntToScalar(offset.x()), SkIntToScalar(offset.y())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | } |
| 207 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 208 | void Canvas::Scale(int x_scale, int y_scale) { |
[email protected] | 8027711 | 2011-10-27 12:29:21 | [diff] [blame] | 209 | canvas_->scale(SkIntToScalar(x_scale), SkIntToScalar(y_scale)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | } |
| 211 | |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 212 | void Canvas::DrawColor(SkColor color) { |
| 213 | DrawColor(color, SkXfermode::kSrcOver_Mode); |
| 214 | } |
| 215 | |
| 216 | void Canvas::DrawColor(SkColor color, SkXfermode::Mode mode) { |
| 217 | canvas_->drawColor(color, mode); |
| 218 | } |
| 219 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 220 | void Canvas::FillRect(const Rect& rect, SkColor color) { |
[email protected] | b8bdc42 | 2012-02-01 21:44:40 | [diff] [blame] | 221 | FillRect(rect, color, SkXfermode::kSrcOver_Mode); |
[email protected] | 602627cc | 2010-11-24 19:14:57 | [diff] [blame] | 222 | } |
| 223 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 224 | void Canvas::FillRect(const Rect& rect, |
[email protected] | 1b81ff2 | 2012-03-24 22:04:42 | [diff] [blame] | 225 | SkColor color, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 226 | SkXfermode::Mode mode) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | SkPaint paint; |
| 228 | paint.setColor(color); |
| 229 | paint.setStyle(SkPaint::kFill_Style); |
[email protected] | 602627cc | 2010-11-24 19:14:57 | [diff] [blame] | 230 | paint.setXfermodeMode(mode); |
[email protected] | 3acc642 | 2011-12-17 16:31:31 | [diff] [blame] | 231 | DrawRect(rect, paint); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 234 | void Canvas::DrawRect(const Rect& rect, SkColor color) { |
[email protected] | 3acc642 | 2011-12-17 16:31:31 | [diff] [blame] | 235 | DrawRect(rect, color, SkXfermode::kSrcOver_Mode); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 236 | } |
| 237 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 238 | void Canvas::DrawRect(const Rect& rect, |
[email protected] | 1b81ff2 | 2012-03-24 22:04:42 | [diff] [blame] | 239 | SkColor color, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 240 | SkXfermode::Mode mode) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 241 | SkPaint paint; |
| 242 | paint.setColor(color); |
| 243 | paint.setStyle(SkPaint::kStroke_Style); |
[email protected] | a64691f2 | 2009-04-07 16:15:56 | [diff] [blame] | 244 | // Set a stroke width of 0, which will put us down the stroke rect path. If |
| 245 | // we set a stroke width of 1, for example, this will internally create a |
| 246 | // path and fill it, which causes problems near the edge of the canvas. |
| 247 | paint.setStrokeWidth(SkIntToScalar(0)); |
[email protected] | 8860e4f5 | 2009-06-25 01:01:52 | [diff] [blame] | 248 | paint.setXfermodeMode(mode); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 249 | |
[email protected] | 3acc642 | 2011-12-17 16:31:31 | [diff] [blame] | 250 | DrawRect(rect, paint); |
[email protected] | 55a0ffd8 | 2010-08-18 20:20:05 | [diff] [blame] | 251 | } |
| 252 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 253 | void Canvas::DrawRect(const Rect& rect, const SkPaint& paint) { |
[email protected] | 3acc642 | 2011-12-17 16:31:31 | [diff] [blame] | 254 | canvas_->drawIRect(RectToSkIRect(rect), paint); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 255 | } |
| 256 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 257 | void Canvas::DrawPoint(const Point& p1, const SkPaint& paint) { |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 258 | canvas_->drawPoint(SkIntToScalar(p1.x()), SkIntToScalar(p1.y()), paint); |
| 259 | } |
| 260 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 261 | void Canvas::DrawLine(const Point& p1, const Point& p2, SkColor color) { |
estade | 397c9b9 | 2016-05-26 15:28:58 | [diff] [blame] | 262 | DrawLine(PointF(p1), PointF(p2), color); |
| 263 | } |
| 264 | |
| 265 | void Canvas::DrawLine(const PointF& p1, const PointF& p2, SkColor color) { |
[email protected] | 74eb4cee | 2009-04-15 14:41:48 | [diff] [blame] | 266 | SkPaint paint; |
| 267 | paint.setColor(color); |
| 268 | paint.setStrokeWidth(SkIntToScalar(1)); |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 269 | DrawLine(p1, p2, paint); |
| 270 | } |
| 271 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 272 | void Canvas::DrawLine(const Point& p1, const Point& p2, const SkPaint& paint) { |
estade | 397c9b9 | 2016-05-26 15:28:58 | [diff] [blame] | 273 | DrawLine(PointF(p1), PointF(p2), paint); |
| 274 | } |
| 275 | |
| 276 | void Canvas::DrawLine(const PointF& p1, |
| 277 | const PointF& p2, |
| 278 | const SkPaint& paint) { |
| 279 | canvas_->drawLine(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()), |
| 280 | SkFloatToScalar(p2.x()), SkFloatToScalar(p2.y()), paint); |
[email protected] | 74eb4cee | 2009-04-15 14:41:48 | [diff] [blame] | 281 | } |
| 282 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 283 | void Canvas::DrawCircle(const Point& center_point, |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 284 | int radius, |
| 285 | const SkPaint& paint) { |
| 286 | canvas_->drawCircle(SkIntToScalar(center_point.x()), |
| 287 | SkIntToScalar(center_point.y()), SkIntToScalar(radius), paint); |
| 288 | } |
| 289 | |
estade | 104196ec | 2016-06-02 01:37:53 | [diff] [blame] | 290 | void Canvas::DrawCircle(const PointF& center_point, |
| 291 | float radius, |
| 292 | const SkPaint& paint) { |
| 293 | canvas_->drawCircle(SkFloatToScalar(center_point.x()), |
| 294 | SkFloatToScalar(center_point.y()), radius, paint); |
| 295 | } |
| 296 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 297 | void Canvas::DrawRoundRect(const Rect& rect, |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 298 | int radius, |
| 299 | const SkPaint& paint) { |
| 300 | canvas_->drawRoundRect(RectToSkRect(rect), SkIntToScalar(radius), |
| 301 | SkIntToScalar(radius), paint); |
| 302 | } |
| 303 | |
mgiuca | 9a713fe1 | 2015-11-10 02:53:30 | [diff] [blame] | 304 | void Canvas::DrawRoundRect(const RectF& rect, |
| 305 | float radius, |
| 306 | const SkPaint& paint) { |
| 307 | canvas_->drawRoundRect(RectFToSkRect(rect), SkFloatToScalar(radius), |
| 308 | SkFloatToScalar(radius), paint); |
| 309 | } |
| 310 | |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 311 | void Canvas::DrawPath(const SkPath& path, const SkPaint& paint) { |
| 312 | canvas_->drawPath(path, paint); |
| 313 | } |
| 314 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 315 | void Canvas::DrawFocusRect(const Rect& rect) { |
[email protected] | edc844a | 2012-03-09 04:33:14 | [diff] [blame] | 316 | DrawDashedRect(rect, SK_ColorGRAY); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 317 | } |
| 318 | |
[email protected] | 5db93a1 | 2013-12-02 21:07:31 | [diff] [blame] | 319 | void Canvas::DrawSolidFocusRect(const Rect& rect, SkColor color) { |
| 320 | SkPaint paint; |
| 321 | paint.setColor(color); |
| 322 | paint.setStrokeWidth(SkIntToScalar(1)); |
| 323 | // Note: We cannot use DrawRect since it would create a path and fill it which |
| 324 | // would cause problems near the edge of the canvas. |
| 325 | int x1 = std::min(rect.x(), rect.right()); |
| 326 | int x2 = std::max(rect.x(), rect.right()); |
| 327 | int y1 = std::min(rect.y(), rect.bottom()); |
| 328 | int y2 = std::max(rect.y(), rect.bottom()); |
| 329 | DrawLine(Point(x1, y1), Point(x2, y1), paint); |
| 330 | DrawLine(Point(x1, y2), Point(x2, y2), paint); |
| 331 | DrawLine(Point(x1, y1), Point(x1, y2), paint); |
| 332 | DrawLine(Point(x2, y1), Point(x2, y2 + 1), paint); |
| 333 | } |
| 334 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 335 | void Canvas::DrawImageInt(const ImageSkia& image, int x, int y) { |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 336 | SkPaint paint; |
[email protected] | 243cac7d | 2012-06-07 13:29:20 | [diff] [blame] | 337 | DrawImageInt(image, x, y, paint); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 338 | } |
| 339 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 340 | void Canvas::DrawImageInt(const ImageSkia& image, int x, int y, uint8_t a) { |
[email protected] | 2885cb3 | 2012-10-03 19:41:11 | [diff] [blame] | 341 | SkPaint paint; |
| 342 | paint.setAlpha(a); |
| 343 | DrawImageInt(image, x, y, paint); |
| 344 | } |
| 345 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 346 | void Canvas::DrawImageInt(const ImageSkia& image, |
| 347 | int x, |
| 348 | int y, |
[email protected] | 243cac7d | 2012-06-07 13:29:20 | [diff] [blame] | 349 | const SkPaint& paint) { |
[email protected] | 1358f27 | 2014-05-10 02:48:00 | [diff] [blame] | 350 | const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
[email protected] | 8232da6b | 2012-07-02 19:41:15 | [diff] [blame] | 351 | if (image_rep.is_null()) |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 352 | return; |
[email protected] | 8232da6b | 2012-07-02 19:41:15 | [diff] [blame] | 353 | const SkBitmap& bitmap = image_rep.sk_bitmap(); |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame] | 354 | float bitmap_scale = image_rep.scale(); |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 355 | |
sky | 005605e | 2014-10-02 20:35:12 | [diff] [blame] | 356 | ScopedCanvas scoper(this); |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 357 | canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), |
| 358 | SkFloatToScalar(1.0f / bitmap_scale)); |
| 359 | canvas_->drawBitmap(bitmap, |
| 360 | SkFloatToScalar(x * bitmap_scale), |
[email protected] | bfc6a9b | 2012-05-23 18:11:05 | [diff] [blame] | 361 | SkFloatToScalar(y * bitmap_scale), |
| 362 | &paint); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 363 | } |
| 364 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 365 | void Canvas::DrawImageInt(const ImageSkia& image, |
| 366 | int src_x, |
| 367 | int src_y, |
| 368 | int src_w, |
| 369 | int src_h, |
| 370 | int dest_x, |
| 371 | int dest_y, |
| 372 | int dest_w, |
| 373 | int dest_h, |
[email protected] | 243cac7d | 2012-06-07 13:29:20 | [diff] [blame] | 374 | bool filter) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 375 | SkPaint p; |
[email protected] | 243cac7d | 2012-06-07 13:29:20 | [diff] [blame] | 376 | DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y, |
| 377 | dest_w, dest_h, filter, p); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 378 | } |
| 379 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 380 | void Canvas::DrawImageInt(const ImageSkia& image, |
| 381 | int src_x, |
| 382 | int src_y, |
| 383 | int src_w, |
| 384 | int src_h, |
| 385 | int dest_x, |
| 386 | int dest_y, |
| 387 | int dest_w, |
| 388 | int dest_h, |
[email protected] | 243cac7d | 2012-06-07 13:29:20 | [diff] [blame] | 389 | bool filter, |
| 390 | const SkPaint& paint) { |
danakj | 4bfbb34 | 2015-10-29 23:38:25 | [diff] [blame] | 391 | const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
| 392 | if (image_rep.is_null()) |
| 393 | return; |
danakj | f0107588 | 2016-01-06 19:39:32 | [diff] [blame] | 394 | bool remove_image_scale = true; |
danakj | 4bfbb34 | 2015-10-29 23:38:25 | [diff] [blame] | 395 | DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y, |
danakj | f0107588 | 2016-01-06 19:39:32 | [diff] [blame] | 396 | dest_w, dest_h, filter, paint, remove_image_scale); |
[email protected] | 54bc246 | 2014-05-06 05:10:58 | [diff] [blame] | 397 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 398 | |
danakj | 4bfbb34 | 2015-10-29 23:38:25 | [diff] [blame] | 399 | void Canvas::DrawImageIntInPixel(const ImageSkiaRep& image_rep, |
[email protected] | 54bc246 | 2014-05-06 05:10:58 | [diff] [blame] | 400 | int dest_x, |
| 401 | int dest_y, |
| 402 | int dest_w, |
| 403 | int dest_h, |
| 404 | bool filter, |
| 405 | const SkPaint& paint) { |
danakj | 4bfbb34 | 2015-10-29 23:38:25 | [diff] [blame] | 406 | int src_x = 0; |
| 407 | int src_y = 0; |
| 408 | int src_w = image_rep.pixel_width(); |
| 409 | int src_h = image_rep.pixel_height(); |
danakj | f0107588 | 2016-01-06 19:39:32 | [diff] [blame] | 410 | // Don't remove image scale here, this function is used to draw the |
| 411 | // (already scaled) |image_rep| at a 1:1 scale with the canvas. |
| 412 | bool remove_image_scale = false; |
danakj | 4bfbb34 | 2015-10-29 23:38:25 | [diff] [blame] | 413 | DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y, |
danakj | f0107588 | 2016-01-06 19:39:32 | [diff] [blame] | 414 | dest_w, dest_h, filter, paint, remove_image_scale); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 415 | } |
| 416 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 417 | void Canvas::DrawImageInPath(const ImageSkia& image, |
[email protected] | 8232da6b | 2012-07-02 19:41:15 | [diff] [blame] | 418 | int x, |
| 419 | int y, |
| 420 | const SkPath& path, |
| 421 | const SkPaint& paint) { |
[email protected] | 1358f27 | 2014-05-10 02:48:00 | [diff] [blame] | 422 | const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
[email protected] | 8232da6b | 2012-07-02 19:41:15 | [diff] [blame] | 423 | if (image_rep.is_null()) |
| 424 | return; |
| 425 | |
| 426 | SkMatrix matrix; |
| 427 | matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
[email protected] | 8232da6b | 2012-07-02 19:41:15 | [diff] [blame] | 428 | SkPaint p(paint); |
tomhudson | 2b45cf9 | 2016-03-31 14:10:15 | [diff] [blame] | 429 | p.setShader(CreateImageRepShader(image_rep, |
| 430 | SkShader::kRepeat_TileMode, |
| 431 | matrix)); |
[email protected] | 8232da6b | 2012-07-02 19:41:15 | [diff] [blame] | 432 | canvas_->drawPath(path, p); |
| 433 | } |
| 434 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 435 | void Canvas::DrawStringRect(const base::string16& text, |
| 436 | const FontList& font_list, |
| 437 | SkColor color, |
| 438 | const Rect& display_rect) { |
| 439 | DrawStringRectWithFlags(text, font_list, color, display_rect, |
| 440 | DefaultCanvasTextAlignment()); |
| 441 | } |
| 442 | |
| 443 | void Canvas::DrawStringRectWithFlags(const base::string16& text, |
| 444 | const FontList& font_list, |
| 445 | SkColor color, |
| 446 | const Rect& display_rect, |
| 447 | int flags) { |
| 448 | DrawStringRectWithShadows(text, font_list, color, display_rect, 0, flags, |
| 449 | ShadowValues()); |
| 450 | } |
| 451 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 452 | void Canvas::TileImageInt(const ImageSkia& image, |
| 453 | int x, |
| 454 | int y, |
| 455 | int w, |
| 456 | int h) { |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 457 | TileImageInt(image, 0, 0, x, y, w, h); |
[email protected] | b42a775 | 2009-02-04 01:02:00 | [diff] [blame] | 458 | } |
| 459 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 460 | void Canvas::TileImageInt(const ImageSkia& image, |
| 461 | int src_x, |
| 462 | int src_y, |
| 463 | int dest_x, |
| 464 | int dest_y, |
| 465 | int w, |
| 466 | int h) { |
[email protected] | 2a3115e | 2012-05-25 23:27:04 | [diff] [blame] | 467 | TileImageInt(image, src_x, src_y, 1.0f, 1.0f, dest_x, dest_y, w, h); |
| 468 | } |
| 469 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 470 | void Canvas::TileImageInt(const ImageSkia& image, |
| 471 | int src_x, |
| 472 | int src_y, |
| 473 | float tile_scale_x, |
| 474 | float tile_scale_y, |
| 475 | int dest_x, |
| 476 | int dest_y, |
| 477 | int w, |
| 478 | int h) { |
pkasting | 954f76a | 2016-02-25 01:11:00 | [diff] [blame] | 479 | SkRect dest_rect = { SkIntToScalar(dest_x), |
| 480 | SkIntToScalar(dest_y), |
| 481 | SkIntToScalar(dest_x + w), |
| 482 | SkIntToScalar(dest_y + h) }; |
| 483 | if (!IntersectsClipRect(dest_rect)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 484 | return; |
| 485 | |
pkasting | 954f76a | 2016-02-25 01:11:00 | [diff] [blame] | 486 | SkPaint paint; |
| 487 | if (InitSkPaintForTiling(image, src_x, src_y, tile_scale_x, tile_scale_y, |
| 488 | dest_x, dest_y, &paint)) |
| 489 | canvas_->drawRect(dest_rect, paint); |
| 490 | } |
| 491 | |
| 492 | bool Canvas::InitSkPaintForTiling(const ImageSkia& image, |
| 493 | int src_x, |
| 494 | int src_y, |
| 495 | float tile_scale_x, |
| 496 | float tile_scale_y, |
| 497 | int dest_x, |
| 498 | int dest_y, |
| 499 | SkPaint* paint) { |
[email protected] | 1358f27 | 2014-05-10 02:48:00 | [diff] [blame] | 500 | const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
[email protected] | 8232da6b | 2012-07-02 19:41:15 | [diff] [blame] | 501 | if (image_rep.is_null()) |
pkasting | 954f76a | 2016-02-25 01:11:00 | [diff] [blame] | 502 | return false; |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 503 | |
[email protected] | 2a3115e | 2012-05-25 23:27:04 | [diff] [blame] | 504 | SkMatrix shader_scale; |
| 505 | shader_scale.setScale(SkFloatToScalar(tile_scale_x), |
| 506 | SkFloatToScalar(tile_scale_y)); |
[email protected] | 8232da6b | 2012-07-02 19:41:15 | [diff] [blame] | 507 | shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); |
| 508 | shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); |
[email protected] | 2a3115e | 2012-05-25 23:27:04 | [diff] [blame] | 509 | |
pkasting | 954f76a | 2016-02-25 01:11:00 | [diff] [blame] | 510 | paint->setShader(CreateImageRepShader(image_rep, SkShader::kRepeat_TileMode, |
tomhudson | a7bbd0df | 2016-03-31 22:57:58 | [diff] [blame] | 511 | shader_scale)); |
pkasting | 954f76a | 2016-02-25 01:11:00 | [diff] [blame] | 512 | paint->setXfermodeMode(SkXfermode::kSrcOver_Mode); |
| 513 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 514 | } |
| 515 | |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 516 | void Canvas::Transform(const gfx::Transform& transform) { |
[email protected] | be172ba8 | 2011-10-05 19:05:07 | [diff] [blame] | 517 | canvas_->concat(transform.matrix()); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 518 | } |
| 519 | |
pkasting | 954f76a | 2016-02-25 01:11:00 | [diff] [blame] | 520 | bool Canvas::IntersectsClipRect(const SkRect& rect) { |
[email protected] | f286f77 | 2010-07-21 18:28:20 | [diff] [blame] | 521 | SkRect clip; |
pkasting | 954f76a | 2016-02-25 01:11:00 | [diff] [blame] | 522 | return canvas_->getClipBounds(&clip) && clip.intersects(rect); |
[email protected] | 0834e1b1 | 2012-04-11 02:23:56 | [diff] [blame] | 523 | } |
| 524 | |
danakj | 4bfbb34 | 2015-10-29 23:38:25 | [diff] [blame] | 525 | void Canvas::DrawImageIntHelper(const ImageSkiaRep& image_rep, |
[email protected] | 54bc246 | 2014-05-06 05:10:58 | [diff] [blame] | 526 | int src_x, |
| 527 | int src_y, |
| 528 | int src_w, |
| 529 | int src_h, |
| 530 | int dest_x, |
| 531 | int dest_y, |
| 532 | int dest_w, |
| 533 | int dest_h, |
| 534 | bool filter, |
| 535 | const SkPaint& paint, |
danakj | f0107588 | 2016-01-06 19:39:32 | [diff] [blame] | 536 | bool remove_image_scale) { |
[email protected] | 54bc246 | 2014-05-06 05:10:58 | [diff] [blame] | 537 | DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && |
| 538 | src_y + src_h < std::numeric_limits<int16_t>::max()); |
| 539 | if (src_w <= 0 || src_h <= 0) { |
| 540 | NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; |
| 541 | return; |
| 542 | } |
| 543 | |
[email protected] | 54bc246 | 2014-05-06 05:10:58 | [diff] [blame] | 544 | SkRect dest_rect = { SkIntToScalar(dest_x), |
| 545 | SkIntToScalar(dest_y), |
| 546 | SkIntToScalar(dest_x + dest_w), |
| 547 | SkIntToScalar(dest_y + dest_h) }; |
pkasting | 954f76a | 2016-02-25 01:11:00 | [diff] [blame] | 548 | if (!IntersectsClipRect(dest_rect)) |
| 549 | return; |
| 550 | |
| 551 | float user_scale_x = static_cast<float>(dest_w) / src_w; |
| 552 | float user_scale_y = static_cast<float>(dest_h) / src_h; |
[email protected] | 54bc246 | 2014-05-06 05:10:58 | [diff] [blame] | 553 | |
[email protected] | 54bc246 | 2014-05-06 05:10:58 | [diff] [blame] | 554 | // Make a bitmap shader that contains the bitmap we want to draw. This is |
| 555 | // basically what SkCanvas.drawBitmap does internally, but it gives us |
| 556 | // more control over quality and will use the mipmap in the source image if |
| 557 | // it has one, whereas drawBitmap won't. |
| 558 | SkMatrix shader_scale; |
| 559 | shader_scale.setScale(SkFloatToScalar(user_scale_x), |
| 560 | SkFloatToScalar(user_scale_y)); |
| 561 | shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); |
| 562 | shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); |
| 563 | |
[email protected] | 54bc246 | 2014-05-06 05:10:58 | [diff] [blame] | 564 | SkPaint p(paint); |
reed | 55801f24 | 2015-03-17 13:16:24 | [diff] [blame] | 565 | p.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
tomhudson | 2b45cf9 | 2016-03-31 14:10:15 | [diff] [blame] | 566 | p.setShader(CreateImageRepShaderForScale( |
| 567 | image_rep, SkShader::kRepeat_TileMode, shader_scale, |
| 568 | remove_image_scale ? image_rep.scale() : 1.f)); |
[email protected] | 54bc246 | 2014-05-06 05:10:58 | [diff] [blame] | 569 | |
| 570 | // The rect will be filled by the bitmap. |
| 571 | canvas_->drawRect(dest_rect, p); |
| 572 | } |
| 573 | |
[email protected] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 574 | } // namespace gfx |