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
3 // found in the LICENSE file.
5 #include "ui/gfx/canvas.h"
10 #include "base/i18n/rtl.h"
11 #include "base/logging.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "third_party/skia/include/effects/SkGradientShader.h"
14 #include "ui/gfx/font_list.h"
15 #include "ui/gfx/rect.h"
16 #include "ui/gfx/size_conversions.h"
17 #include "ui/gfx/skia_util.h"
18 #include "ui/gfx/transform.h"
21 #include "ui/gfx/canvas_skia_paint.h"
26 Canvas::Canvas(const Size& size, float image_scale, bool is_opaque)
27 : image_scale_(image_scale),
29 Size pixel_size = ToCeiledSize(ScaleSize(size, image_scale));
30 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(),
33 canvas_ = owned_canvas_.get();
34 #if defined(OS_WIN) || defined(OS_MACOSX)
35 // skia::PlatformCanvas instances are initialized to 0 by Cairo on Linux, but
36 // uninitialized on Win and Mac.
38 owned_canvas_->clear(SkColorSetARGB(0, 0, 0, 0));
41 SkScalar scale_scalar = SkFloatToScalar(image_scale);
42 canvas_->scale(scale_scalar, scale_scalar);
45 Canvas::Canvas(const ImageSkiaRep& image_rep, bool is_opaque)
46 : image_scale_(image_rep.scale()),
47 owned_canvas_(skia::AdoptRef(
48 skia::CreatePlatformCanvas(image_rep.pixel_width(),
49 image_rep.pixel_height(),
51 canvas_(owned_canvas_.get()) {
52 SkScalar scale_scalar = SkFloatToScalar(image_scale_);
53 canvas_->scale(scale_scalar, scale_scalar);
54 DrawImageInt(ImageSkia(image_rep), 0, 0);
59 owned_canvas_(skia::AdoptRef(skia::CreatePlatformCanvas(0, 0, false))),
60 canvas_(owned_canvas_.get()) {
67 Canvas* Canvas::CreateCanvasWithoutScaling(SkCanvas* canvas,
69 return new Canvas(canvas, image_scale);
72 void Canvas::RecreateBackingCanvas(const Size& size,
75 image_scale_ = image_scale;
76 Size pixel_size = ToFlooredSize(ScaleSize(size, image_scale));
77 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(),
80 canvas_ = owned_canvas_.get();
81 SkScalar scale_scalar = SkFloatToScalar(image_scale);
82 canvas_->scale(scale_scalar, scale_scalar);
86 void Canvas::SizeStringInt(const base::string16& text,
87 const FontList& font_list,
92 float fractional_width = *width;
93 float factional_height = *height;
94 SizeStringFloat(text, font_list, &fractional_width,
95 &factional_height, line_height, flags);
96 *width = std::ceil(fractional_width);
97 *height = std::ceil(factional_height);
101 int Canvas::GetStringWidth(const base::string16& text,
102 const FontList& font_list) {
103 int width = 0, height = 0;
104 SizeStringInt(text, font_list, &width, &height, 0, NO_ELLIPSIS);
109 float Canvas::GetStringWidthF(const base::string16& text,
110 const FontList& font_list) {
111 float width = 0, height = 0;
112 SizeStringFloat(text, font_list, &width, &height, 0, NO_ELLIPSIS);
117 int Canvas::GetStringWidth(const base::string16& text, const Font& font) {
118 int width = 0, height = 0;
119 SizeStringInt(text, FontList(font), &width, &height, 0, NO_ELLIPSIS);
124 int Canvas::DefaultCanvasTextAlignment() {
125 return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT;
128 ImageSkiaRep Canvas::ExtractImageRep() const {
129 const SkBitmap& device_bitmap = canvas_->getDevice()->accessBitmap(false);
131 // Make a bitmap to return, and a canvas to draw into it. We don't just want
132 // to call extractSubset or the copy constructor, since we want an actual copy
135 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config);
137 return ImageSkiaRep(result, image_scale_);
140 void Canvas::DrawDashedRect(const Rect& rect, SkColor color) {
141 // Create a 2D bitmap containing alternating on/off pixels - we do this
142 // so that you never get two pixels of the same color around the edges
143 // of the focus rect (this may mean that opposing edges of the rect may
144 // have a dot pattern out of phase to each other).
145 static SkColor last_color;
146 static SkBitmap* dots = NULL;
147 if (!dots || last_color != color) {
154 dots->setConfig(SkBitmap::kARGB_8888_Config, col_pixels, row_pixels);
156 dots->eraseARGB(0, 0, 0, 0);
158 uint32_t* dot = dots->getAddr32(0, 0);
159 for (int i = 0; i < row_pixels; i++) {
160 for (int u = 0; u < col_pixels; u++) {
161 if ((u % 2 + i % 2) % 2 != 0) {
162 dot[i * row_pixels + u] = color;
168 // Make a shader for the bitmap with an origin of the box we'll draw. This
169 // shader is refcounted and will have an initial refcount of 1.
170 skia::RefPtr<SkShader> shader = skia::AdoptRef(
171 SkShader::CreateBitmapShader(
172 *dots, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode));
173 // Assign the shader to the paint & release our reference. The paint will
174 // now own the shader and the shader will be destroyed when the paint goes
177 paint.setShader(shader.get());
179 DrawRect(Rect(rect.x(), rect.y(), rect.width(), 1), paint);
180 DrawRect(Rect(rect.x(), rect.y() + rect.height() - 1, rect.width(), 1),
182 DrawRect(Rect(rect.x(), rect.y(), 1, rect.height()), paint);
183 DrawRect(Rect(rect.x() + rect.width() - 1, rect.y(), 1, rect.height()),
187 void Canvas::Save() {
191 void Canvas::SaveLayerAlpha(uint8 alpha) {
192 canvas_->saveLayerAlpha(NULL, alpha);
196 void Canvas::SaveLayerAlpha(uint8 alpha, const Rect& layer_bounds) {
197 SkRect bounds(RectToSkRect(layer_bounds));
198 canvas_->saveLayerAlpha(&bounds, alpha);
201 void Canvas::Restore() {
205 bool Canvas::ClipRect(const Rect& rect) {
206 return canvas_->clipRect(RectToSkRect(rect));
209 bool Canvas::ClipPath(const SkPath& path) {
210 return canvas_->clipPath(path);
213 bool Canvas::GetClipBounds(Rect* bounds) {
215 bool has_non_empty_clip = canvas_->getClipBounds(&out);
216 bounds->SetRect(out.left(), out.top(), out.width(), out.height());
217 return has_non_empty_clip;
220 void Canvas::Translate(const Vector2d& offset) {
221 canvas_->translate(SkIntToScalar(offset.x()), SkIntToScalar(offset.y()));
224 void Canvas::Scale(int x_scale, int y_scale) {
225 canvas_->scale(SkIntToScalar(x_scale), SkIntToScalar(y_scale));
228 void Canvas::DrawColor(SkColor color) {
229 DrawColor(color, SkXfermode::kSrcOver_Mode);
232 void Canvas::DrawColor(SkColor color, SkXfermode::Mode mode) {
233 canvas_->drawColor(color, mode);
236 void Canvas::FillRect(const Rect& rect, SkColor color) {
237 FillRect(rect, color, SkXfermode::kSrcOver_Mode);
240 void Canvas::FillRect(const Rect& rect,
242 SkXfermode::Mode mode) {
244 paint.setColor(color);
245 paint.setStyle(SkPaint::kFill_Style);
246 paint.setXfermodeMode(mode);
247 DrawRect(rect, paint);
250 void Canvas::DrawRect(const Rect& rect, SkColor color) {
251 DrawRect(rect, color, SkXfermode::kSrcOver_Mode);
254 void Canvas::DrawRect(const Rect& rect,
256 SkXfermode::Mode mode) {
258 paint.setColor(color);
259 paint.setStyle(SkPaint::kStroke_Style);
260 // Set a stroke width of 0, which will put us down the stroke rect path. If
261 // we set a stroke width of 1, for example, this will internally create a
262 // path and fill it, which causes problems near the edge of the canvas.
263 paint.setStrokeWidth(SkIntToScalar(0));
264 paint.setXfermodeMode(mode);
266 DrawRect(rect, paint);
269 void Canvas::DrawRect(const Rect& rect, const SkPaint& paint) {
270 canvas_->drawIRect(RectToSkIRect(rect), paint);
273 void Canvas::DrawPoint(const Point& p1, const SkPaint& paint) {
274 canvas_->drawPoint(SkIntToScalar(p1.x()), SkIntToScalar(p1.y()), paint);
277 void Canvas::DrawLine(const Point& p1, const Point& p2, SkColor color) {
279 paint.setColor(color);
280 paint.setStrokeWidth(SkIntToScalar(1));
281 DrawLine(p1, p2, paint);
284 void Canvas::DrawLine(const Point& p1, const Point& p2, const SkPaint& paint) {
285 canvas_->drawLine(SkIntToScalar(p1.x()), SkIntToScalar(p1.y()),
286 SkIntToScalar(p2.x()), SkIntToScalar(p2.y()), paint);
289 void Canvas::DrawCircle(const Point& center_point,
291 const SkPaint& paint) {
292 canvas_->drawCircle(SkIntToScalar(center_point.x()),
293 SkIntToScalar(center_point.y()), SkIntToScalar(radius), paint);
296 void Canvas::DrawRoundRect(const Rect& rect,
298 const SkPaint& paint) {
299 canvas_->drawRoundRect(RectToSkRect(rect), SkIntToScalar(radius),
300 SkIntToScalar(radius), paint);
303 void Canvas::DrawPath(const SkPath& path, const SkPaint& paint) {
304 canvas_->drawPath(path, paint);
307 void Canvas::DrawFocusRect(const Rect& rect) {
308 DrawDashedRect(rect, SK_ColorGRAY);
311 void Canvas::DrawSolidFocusRect(const Rect& rect, SkColor color) {
313 paint.setColor(color);
314 paint.setStrokeWidth(SkIntToScalar(1));
315 // Note: We cannot use DrawRect since it would create a path and fill it which
316 // would cause problems near the edge of the canvas.
317 int x1 = std::min(rect.x(), rect.right());
318 int x2 = std::max(rect.x(), rect.right());
319 int y1 = std::min(rect.y(), rect.bottom());
320 int y2 = std::max(rect.y(), rect.bottom());
321 DrawLine(Point(x1, y1), Point(x2, y1), paint);
322 DrawLine(Point(x1, y2), Point(x2, y2), paint);
323 DrawLine(Point(x1, y1), Point(x1, y2), paint);
324 DrawLine(Point(x2, y1), Point(x2, y2 + 1), paint);
327 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y) {
329 DrawImageInt(image, x, y, paint);
332 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y, uint8 a) {
335 DrawImageInt(image, x, y, paint);
338 void Canvas::DrawImageInt(const ImageSkia& image,
341 const SkPaint& paint) {
342 const ImageSkiaRep& image_rep = GetImageRepToPaint(image);
343 if (image_rep.is_null())
345 const SkBitmap& bitmap = image_rep.sk_bitmap();
346 float bitmap_scale = image_rep.scale();
349 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale),
350 SkFloatToScalar(1.0f / bitmap_scale));
351 canvas_->drawBitmap(bitmap,
352 SkFloatToScalar(x * bitmap_scale),
353 SkFloatToScalar(y * bitmap_scale),
358 void Canvas::DrawImageInt(const ImageSkia& image,
369 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y,
370 dest_w, dest_h, filter, p);
373 void Canvas::DrawImageInt(const ImageSkia& image,
383 const SkPaint& paint) {
384 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() &&
385 src_y + src_h < std::numeric_limits<int16_t>::max());
386 if (src_w <= 0 || src_h <= 0) {
387 NOTREACHED() << "Attempting to draw bitmap from an empty rect!";
391 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h))
394 float user_scale_x = static_cast<float>(dest_w) / src_w;
395 float user_scale_y = static_cast<float>(dest_h) / src_h;
397 const ImageSkiaRep& image_rep = GetImageRepToPaint(image,
398 user_scale_x, user_scale_y);
399 if (image_rep.is_null())
402 SkRect dest_rect = { SkIntToScalar(dest_x),
403 SkIntToScalar(dest_y),
404 SkIntToScalar(dest_x + dest_w),
405 SkIntToScalar(dest_y + dest_h) };
407 if (src_w == dest_w && src_h == dest_h &&
408 user_scale_x == 1.0f && user_scale_y == 1.0f &&
409 image_rep.scale() == 1.0f) {
410 // Workaround for apparent bug in Skia that causes image to occasionally
412 SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h };
413 const SkBitmap& bitmap = image_rep.sk_bitmap();
414 canvas_->drawBitmapRect(bitmap, &src_rect, dest_rect, &paint);
418 // Make a bitmap shader that contains the bitmap we want to draw. This is
419 // basically what SkCanvas.drawBitmap does internally, but it gives us
420 // more control over quality and will use the mipmap in the source image if
421 // it has one, whereas drawBitmap won't.
422 SkMatrix shader_scale;
423 shader_scale.setScale(SkFloatToScalar(user_scale_x),
424 SkFloatToScalar(user_scale_y));
425 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y));
426 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y));
428 skia::RefPtr<SkShader> shader = CreateImageRepShader(
430 SkShader::kRepeat_TileMode,
433 // Set up our paint to use the shader & release our reference (now just owned
436 p.setFilterBitmap(filter);
437 p.setShader(shader.get());
439 // The rect will be filled by the bitmap.
440 canvas_->drawRect(dest_rect, p);
443 void Canvas::DrawImageInPath(const ImageSkia& image,
447 const SkPaint& paint) {
448 const ImageSkiaRep& image_rep = GetImageRepToPaint(image);
449 if (image_rep.is_null())
453 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
454 skia::RefPtr<SkShader> shader = CreateImageRepShader(
456 SkShader::kRepeat_TileMode,
460 p.setShader(shader.get());
461 canvas_->drawPath(path, p);
464 void Canvas::DrawStringRect(const base::string16& text,
465 const FontList& font_list,
467 const Rect& display_rect) {
468 DrawStringRectWithFlags(text, font_list, color, display_rect,
469 DefaultCanvasTextAlignment());
472 void Canvas::DrawStringRectWithFlags(const base::string16& text,
473 const FontList& font_list,
475 const Rect& display_rect,
477 DrawStringRectWithShadows(text, font_list, color, display_rect, 0, flags,
481 void Canvas::TileImageInt(const ImageSkia& image,
486 TileImageInt(image, 0, 0, x, y, w, h);
489 void Canvas::TileImageInt(const ImageSkia& image,
496 TileImageInt(image, src_x, src_y, 1.0f, 1.0f, dest_x, dest_y, w, h);
499 void Canvas::TileImageInt(const ImageSkia& image,
508 if (!IntersectsClipRectInt(dest_x, dest_y, w, h))
511 const ImageSkiaRep& image_rep = GetImageRepToPaint(
512 image, tile_scale_x, tile_scale_y);
513 if (image_rep.is_null())
516 SkMatrix shader_scale;
517 shader_scale.setScale(SkFloatToScalar(tile_scale_x),
518 SkFloatToScalar(tile_scale_y));
519 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y));
520 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y));
522 skia::RefPtr<SkShader> shader = CreateImageRepShader(
524 SkShader::kRepeat_TileMode,
528 paint.setShader(shader.get());
529 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
531 SkRect dest_rect = { SkIntToScalar(dest_x),
532 SkIntToScalar(dest_y),
533 SkIntToScalar(dest_x + w),
534 SkIntToScalar(dest_y + h) };
535 canvas_->drawRect(dest_rect, paint);
538 NativeDrawingContext Canvas::BeginPlatformPaint() {
539 return skia::BeginPlatformPaint(canvas_);
542 void Canvas::EndPlatformPaint() {
543 skia::EndPlatformPaint(canvas_);
546 void Canvas::Transform(const gfx::Transform& transform) {
547 canvas_->concat(transform.matrix());
550 Canvas::Canvas(SkCanvas* canvas, float image_scale)
551 : image_scale_(image_scale),
557 bool Canvas::IntersectsClipRectInt(int x, int y, int w, int h) {
559 return canvas_->getClipBounds(&clip) &&
560 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w),
561 SkIntToScalar(y + h));
564 bool Canvas::IntersectsClipRect(const Rect& rect) {
565 return IntersectsClipRectInt(rect.x(), rect.y(),
566 rect.width(), rect.height());
569 const ImageSkiaRep& Canvas::GetImageRepToPaint(const ImageSkia& image) const {
570 return GetImageRepToPaint(image, 1.0f, 1.0f);
573 const ImageSkiaRep& Canvas::GetImageRepToPaint(
574 const ImageSkia& image,
575 float user_additional_scale_x,
576 float user_additional_scale_y) const {
577 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_);
579 if (!image_rep.is_null()) {
580 SkMatrix m = canvas_->getTotalMatrix();
581 float scale_x = SkScalarToFloat(SkScalarAbs(m.getScaleX())) *
582 user_additional_scale_x;
583 float scale_y = SkScalarToFloat(SkScalarAbs(m.getScaleY())) *
584 user_additional_scale_y;
586 float bitmap_scale = image_rep.scale();
587 if (scale_x < bitmap_scale || scale_y < bitmap_scale)
588 const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap();