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::DefaultCanvasTextAlignment() {
118 return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT
: TEXT_ALIGN_LEFT
;
121 ImageSkiaRep
Canvas::ExtractImageRep() const {
122 // Make a bitmap to return, and a canvas to draw into it. We don't just want
123 // to call extractSubset or the copy constructor, since we want an actual copy
125 const SkISize size
= canvas_
->getDeviceSize();
127 result
.allocN32Pixels(size
.width(), size
.height());
129 canvas_
->readPixels(&result
, 0, 0);
130 return ImageSkiaRep(result
, image_scale_
);
133 void Canvas::DrawDashedRect(const Rect
& rect
, SkColor color
) {
134 // Create a 2D bitmap containing alternating on/off pixels - we do this
135 // so that you never get two pixels of the same color around the edges
136 // of the focus rect (this may mean that opposing edges of the rect may
137 // have a dot pattern out of phase to each other).
138 static SkColor last_color
;
139 static SkBitmap
* dots
= NULL
;
140 if (!dots
|| last_color
!= color
) {
147 dots
->allocN32Pixels(col_pixels
, row_pixels
);
148 dots
->eraseARGB(0, 0, 0, 0);
150 uint32_t* dot
= dots
->getAddr32(0, 0);
151 for (int i
= 0; i
< row_pixels
; i
++) {
152 for (int u
= 0; u
< col_pixels
; u
++) {
153 if ((u
% 2 + i
% 2) % 2 != 0) {
154 dot
[i
* row_pixels
+ u
] = color
;
160 // Make a shader for the bitmap with an origin of the box we'll draw. This
161 // shader is refcounted and will have an initial refcount of 1.
162 skia::RefPtr
<SkShader
> shader
= skia::AdoptRef(
163 SkShader::CreateBitmapShader(
164 *dots
, SkShader::kRepeat_TileMode
, SkShader::kRepeat_TileMode
));
165 // Assign the shader to the paint & release our reference. The paint will
166 // now own the shader and the shader will be destroyed when the paint goes
169 paint
.setShader(shader
.get());
171 DrawRect(Rect(rect
.x(), rect
.y(), rect
.width(), 1), paint
);
172 DrawRect(Rect(rect
.x(), rect
.y() + rect
.height() - 1, rect
.width(), 1),
174 DrawRect(Rect(rect
.x(), rect
.y(), 1, rect
.height()), paint
);
175 DrawRect(Rect(rect
.x() + rect
.width() - 1, rect
.y(), 1, rect
.height()),
179 void Canvas::Save() {
183 void Canvas::SaveLayerAlpha(uint8 alpha
) {
184 canvas_
->saveLayerAlpha(NULL
, alpha
);
187 void Canvas::SaveLayerAlpha(uint8 alpha
, const Rect
& layer_bounds
) {
188 SkRect
bounds(RectToSkRect(layer_bounds
));
189 canvas_
->saveLayerAlpha(&bounds
, alpha
);
192 void Canvas::Restore() {
196 void Canvas::ClipRect(const Rect
& rect
) {
197 canvas_
->clipRect(RectToSkRect(rect
));
200 void Canvas::ClipPath(const SkPath
& path
, bool do_anti_alias
) {
201 canvas_
->clipPath(path
, SkRegion::kIntersect_Op
, do_anti_alias
);
204 bool Canvas::IsClipEmpty() const {
205 return canvas_
->isClipEmpty();
208 bool Canvas::GetClipBounds(Rect
* bounds
) {
210 bool has_non_empty_clip
= canvas_
->getClipBounds(&out
);
211 bounds
->SetRect(out
.left(), out
.top(), out
.width(), out
.height());
212 return has_non_empty_clip
;
215 void Canvas::Translate(const Vector2d
& offset
) {
216 canvas_
->translate(SkIntToScalar(offset
.x()), SkIntToScalar(offset
.y()));
219 void Canvas::Scale(int x_scale
, int y_scale
) {
220 canvas_
->scale(SkIntToScalar(x_scale
), SkIntToScalar(y_scale
));
223 void Canvas::DrawColor(SkColor color
) {
224 DrawColor(color
, SkXfermode::kSrcOver_Mode
);
227 void Canvas::DrawColor(SkColor color
, SkXfermode::Mode mode
) {
228 canvas_
->drawColor(color
, mode
);
231 void Canvas::FillRect(const Rect
& rect
, SkColor color
) {
232 FillRect(rect
, color
, SkXfermode::kSrcOver_Mode
);
235 void Canvas::FillRect(const Rect
& rect
,
237 SkXfermode::Mode mode
) {
239 paint
.setColor(color
);
240 paint
.setStyle(SkPaint::kFill_Style
);
241 paint
.setXfermodeMode(mode
);
242 DrawRect(rect
, paint
);
245 void Canvas::DrawRect(const Rect
& rect
, SkColor color
) {
246 DrawRect(rect
, color
, SkXfermode::kSrcOver_Mode
);
249 void Canvas::DrawRect(const Rect
& rect
,
251 SkXfermode::Mode mode
) {
253 paint
.setColor(color
);
254 paint
.setStyle(SkPaint::kStroke_Style
);
255 // Set a stroke width of 0, which will put us down the stroke rect path. If
256 // we set a stroke width of 1, for example, this will internally create a
257 // path and fill it, which causes problems near the edge of the canvas.
258 paint
.setStrokeWidth(SkIntToScalar(0));
259 paint
.setXfermodeMode(mode
);
261 DrawRect(rect
, paint
);
264 void Canvas::DrawRect(const Rect
& rect
, const SkPaint
& paint
) {
265 canvas_
->drawIRect(RectToSkIRect(rect
), paint
);
268 void Canvas::DrawPoint(const Point
& p1
, const SkPaint
& paint
) {
269 canvas_
->drawPoint(SkIntToScalar(p1
.x()), SkIntToScalar(p1
.y()), paint
);
272 void Canvas::DrawLine(const Point
& p1
, const Point
& p2
, SkColor color
) {
274 paint
.setColor(color
);
275 paint
.setStrokeWidth(SkIntToScalar(1));
276 DrawLine(p1
, p2
, paint
);
279 void Canvas::DrawLine(const Point
& p1
, const Point
& p2
, const SkPaint
& paint
) {
280 canvas_
->drawLine(SkIntToScalar(p1
.x()), SkIntToScalar(p1
.y()),
281 SkIntToScalar(p2
.x()), SkIntToScalar(p2
.y()), paint
);
284 void Canvas::DrawCircle(const Point
& center_point
,
286 const SkPaint
& paint
) {
287 canvas_
->drawCircle(SkIntToScalar(center_point
.x()),
288 SkIntToScalar(center_point
.y()), SkIntToScalar(radius
), paint
);
291 void Canvas::DrawRoundRect(const Rect
& rect
,
293 const SkPaint
& paint
) {
294 canvas_
->drawRoundRect(RectToSkRect(rect
), SkIntToScalar(radius
),
295 SkIntToScalar(radius
), paint
);
298 void Canvas::DrawPath(const SkPath
& path
, const SkPaint
& paint
) {
299 canvas_
->drawPath(path
, paint
);
302 void Canvas::DrawFocusRect(const Rect
& rect
) {
303 DrawDashedRect(rect
, SK_ColorGRAY
);
306 void Canvas::DrawSolidFocusRect(const Rect
& rect
, SkColor color
) {
308 paint
.setColor(color
);
309 paint
.setStrokeWidth(SkIntToScalar(1));
310 // Note: We cannot use DrawRect since it would create a path and fill it which
311 // would cause problems near the edge of the canvas.
312 int x1
= std::min(rect
.x(), rect
.right());
313 int x2
= std::max(rect
.x(), rect
.right());
314 int y1
= std::min(rect
.y(), rect
.bottom());
315 int y2
= std::max(rect
.y(), rect
.bottom());
316 DrawLine(Point(x1
, y1
), Point(x2
, y1
), paint
);
317 DrawLine(Point(x1
, y2
), Point(x2
, y2
), paint
);
318 DrawLine(Point(x1
, y1
), Point(x1
, y2
), paint
);
319 DrawLine(Point(x2
, y1
), Point(x2
, y2
+ 1), paint
);
322 void Canvas::DrawImageInt(const ImageSkia
& image
, int x
, int y
) {
324 DrawImageInt(image
, x
, y
, paint
);
327 void Canvas::DrawImageInt(const ImageSkia
& image
, int x
, int y
, uint8 a
) {
330 DrawImageInt(image
, x
, y
, paint
);
333 void Canvas::DrawImageInt(const ImageSkia
& image
,
336 const SkPaint
& paint
) {
337 const ImageSkiaRep
& image_rep
= image
.GetRepresentation(image_scale_
);
338 if (image_rep
.is_null())
340 const SkBitmap
& bitmap
= image_rep
.sk_bitmap();
341 float bitmap_scale
= image_rep
.scale();
344 canvas_
->scale(SkFloatToScalar(1.0f
/ bitmap_scale
),
345 SkFloatToScalar(1.0f
/ bitmap_scale
));
346 canvas_
->drawBitmap(bitmap
,
347 SkFloatToScalar(x
* bitmap_scale
),
348 SkFloatToScalar(y
* bitmap_scale
),
353 void Canvas::DrawImageInt(const ImageSkia
& image
,
364 DrawImageInt(image
, src_x
, src_y
, src_w
, src_h
, dest_x
, dest_y
,
365 dest_w
, dest_h
, filter
, p
);
368 void Canvas::DrawImageInt(const ImageSkia
& image
,
378 const SkPaint
& paint
) {
379 DrawImageIntHelper(image
, src_x
, src_y
, src_w
, src_h
, dest_x
, dest_y
, dest_w
,
380 dest_h
, filter
, paint
, image_scale_
, false);
383 void Canvas::DrawImageIntInPixel(const ImageSkia
& image
,
393 const SkPaint
& paint
) {
395 // 1. Translate the destination rectangle using the current translation
396 // values from the SkCanvas matrix stack.
397 // 2. Save the current state of the canvas.
398 // 3. Reset the scales and the translation values in the SkCanvas matrix
400 // 4. Set the scale in gfx::Canvas instance to 1.0, 1.0.
401 // 5. Draw the image.
402 // 6. Restore the state of the canvas and the SkCanvas matrix stack.
403 SkMatrix matrix
= canvas_
->getTotalMatrix();
405 SkRect destination_rect
;
406 destination_rect
.set(SkIntToScalar(dest_x
),
407 SkIntToScalar(dest_y
),
408 SkIntToScalar(dest_x
+ dest_w
),
409 SkIntToScalar(dest_y
+ dest_h
));
410 matrix
.setScaleX(1.0f
);
411 matrix
.setScaleY(1.0f
);
412 matrix
.mapRect(&destination_rect
, destination_rect
);
416 // The destination is now in pixel values. No need for further translation.
417 matrix
.setTranslate(0, 0);
418 canvas_
->setMatrix(matrix
);
420 DrawImageIntHelper(image
,
425 SkScalarRoundToInt(destination_rect
.x()),
426 SkScalarRoundToInt(destination_rect
.y()),
427 SkScalarRoundToInt(destination_rect
.width()),
428 SkScalarRoundToInt(destination_rect
.height()),
434 // Restore the state of the canvas.
438 void Canvas::DrawImageInPath(const ImageSkia
& image
,
442 const SkPaint
& paint
) {
443 const ImageSkiaRep
& image_rep
= image
.GetRepresentation(image_scale_
);
444 if (image_rep
.is_null())
448 matrix
.setTranslate(SkIntToScalar(x
), SkIntToScalar(y
));
449 skia::RefPtr
<SkShader
> shader
= CreateImageRepShader(
451 SkShader::kRepeat_TileMode
,
455 p
.setShader(shader
.get());
456 canvas_
->drawPath(path
, p
);
459 void Canvas::DrawStringRect(const base::string16
& text
,
460 const FontList
& font_list
,
462 const Rect
& display_rect
) {
463 DrawStringRectWithFlags(text
, font_list
, color
, display_rect
,
464 DefaultCanvasTextAlignment());
467 void Canvas::DrawStringRectWithFlags(const base::string16
& text
,
468 const FontList
& font_list
,
470 const Rect
& display_rect
,
472 DrawStringRectWithShadows(text
, font_list
, color
, display_rect
, 0, flags
,
476 void Canvas::TileImageInt(const ImageSkia
& image
,
481 TileImageInt(image
, 0, 0, x
, y
, w
, h
);
484 void Canvas::TileImageInt(const ImageSkia
& image
,
491 TileImageInt(image
, src_x
, src_y
, 1.0f
, 1.0f
, dest_x
, dest_y
, w
, h
);
494 void Canvas::TileImageInt(const ImageSkia
& image
,
503 if (!IntersectsClipRectInt(dest_x
, dest_y
, w
, h
))
506 const ImageSkiaRep
& image_rep
= image
.GetRepresentation(image_scale_
);
507 if (image_rep
.is_null())
510 SkMatrix shader_scale
;
511 shader_scale
.setScale(SkFloatToScalar(tile_scale_x
),
512 SkFloatToScalar(tile_scale_y
));
513 shader_scale
.preTranslate(SkIntToScalar(-src_x
), SkIntToScalar(-src_y
));
514 shader_scale
.postTranslate(SkIntToScalar(dest_x
), SkIntToScalar(dest_y
));
516 skia::RefPtr
<SkShader
> shader
= CreateImageRepShader(
518 SkShader::kRepeat_TileMode
,
522 paint
.setShader(shader
.get());
523 paint
.setXfermodeMode(SkXfermode::kSrcOver_Mode
);
525 SkRect dest_rect
= { SkIntToScalar(dest_x
),
526 SkIntToScalar(dest_y
),
527 SkIntToScalar(dest_x
+ w
),
528 SkIntToScalar(dest_y
+ h
) };
529 canvas_
->drawRect(dest_rect
, paint
);
532 NativeDrawingContext
Canvas::BeginPlatformPaint() {
533 return skia::BeginPlatformPaint(canvas_
);
536 void Canvas::EndPlatformPaint() {
537 skia::EndPlatformPaint(canvas_
);
540 void Canvas::Transform(const gfx::Transform
& transform
) {
541 canvas_
->concat(transform
.matrix());
544 Canvas::Canvas(SkCanvas
* canvas
, float image_scale
)
545 : image_scale_(image_scale
),
551 bool Canvas::IntersectsClipRectInt(int x
, int y
, int w
, int h
) {
553 return canvas_
->getClipBounds(&clip
) &&
554 clip
.intersect(SkIntToScalar(x
), SkIntToScalar(y
), SkIntToScalar(x
+ w
),
555 SkIntToScalar(y
+ h
));
558 bool Canvas::IntersectsClipRect(const Rect
& rect
) {
559 return IntersectsClipRectInt(rect
.x(), rect
.y(),
560 rect
.width(), rect
.height());
563 void Canvas::DrawImageIntHelper(const ImageSkia
& image
,
573 const SkPaint
& paint
,
576 DLOG_ASSERT(src_x
+ src_w
< std::numeric_limits
<int16_t>::max() &&
577 src_y
+ src_h
< std::numeric_limits
<int16_t>::max());
578 if (src_w
<= 0 || src_h
<= 0) {
579 NOTREACHED() << "Attempting to draw bitmap from an empty rect!";
583 if (!IntersectsClipRectInt(dest_x
, dest_y
, dest_w
, dest_h
))
586 float user_scale_x
= static_cast<float>(dest_w
) / src_w
;
587 float user_scale_y
= static_cast<float>(dest_h
) / src_h
;
589 const ImageSkiaRep
& image_rep
= image
.GetRepresentation(image_scale
);
590 if (image_rep
.is_null())
593 SkRect dest_rect
= { SkIntToScalar(dest_x
),
594 SkIntToScalar(dest_y
),
595 SkIntToScalar(dest_x
+ dest_w
),
596 SkIntToScalar(dest_y
+ dest_h
) };
598 if (src_w
== dest_w
&& src_h
== dest_h
&&
599 user_scale_x
== 1.0f
&& user_scale_y
== 1.0f
&&
600 image_rep
.scale() == 1.0f
&& !pixel
) {
601 // Workaround for apparent bug in Skia that causes image to occasionally
603 SkIRect src_rect
= { src_x
, src_y
, src_x
+ src_w
, src_y
+ src_h
};
604 const SkBitmap
& bitmap
= image_rep
.sk_bitmap();
605 canvas_
->drawBitmapRect(bitmap
, &src_rect
, dest_rect
, &paint
);
609 // Make a bitmap shader that contains the bitmap we want to draw. This is
610 // basically what SkCanvas.drawBitmap does internally, but it gives us
611 // more control over quality and will use the mipmap in the source image if
612 // it has one, whereas drawBitmap won't.
613 SkMatrix shader_scale
;
614 shader_scale
.setScale(SkFloatToScalar(user_scale_x
),
615 SkFloatToScalar(user_scale_y
));
616 shader_scale
.preTranslate(SkIntToScalar(-src_x
), SkIntToScalar(-src_y
));
617 shader_scale
.postTranslate(SkIntToScalar(dest_x
), SkIntToScalar(dest_y
));
619 skia::RefPtr
<SkShader
> shader
= CreateImageRepShaderForScale(
621 SkShader::kRepeat_TileMode
,
623 pixel
? 1.0f
: image_rep
.scale());
625 // Set up our paint to use the shader & release our reference (now just owned
628 p
.setFilterLevel(filter
? SkPaint::kLow_FilterLevel
629 : SkPaint::kNone_FilterLevel
);
630 p
.setShader(shader
.get());
632 // The rect will be filled by the bitmap.
633 canvas_
->drawRect(dest_rect
, p
);