[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / ui / gfx / canvas.cc
blobe8d6a9166fb1136c5d94fca30912b80f137a01be
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"
7 #include <cmath>
8 #include <limits>
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"
20 #if defined(OS_WIN)
21 #include "ui/gfx/canvas_skia_paint.h"
22 #endif
24 namespace gfx {
26 Canvas::Canvas(const Size& size, float image_scale, bool is_opaque)
27 : image_scale_(image_scale),
28 canvas_(NULL) {
29 Size pixel_size = ToCeiledSize(ScaleSize(size, image_scale));
30 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(),
31 pixel_size.height(),
32 is_opaque));
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.
37 if (!is_opaque)
38 owned_canvas_->clear(SkColorSetARGB(0, 0, 0, 0));
39 #endif
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(),
50 is_opaque))),
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);
57 Canvas::Canvas()
58 : image_scale_(1.0),
59 owned_canvas_(skia::AdoptRef(skia::CreatePlatformCanvas(0, 0, false))),
60 canvas_(owned_canvas_.get()) {
63 Canvas::~Canvas() {
66 // static
67 Canvas* Canvas::CreateCanvasWithoutScaling(SkCanvas* canvas,
68 float image_scale) {
69 return new Canvas(canvas, image_scale);
72 void Canvas::RecreateBackingCanvas(const Size& size,
73 float image_scale,
74 bool is_opaque) {
75 image_scale_ = image_scale;
76 Size pixel_size = ToFlooredSize(ScaleSize(size, image_scale));
77 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(),
78 pixel_size.height(),
79 is_opaque));
80 canvas_ = owned_canvas_.get();
81 SkScalar scale_scalar = SkFloatToScalar(image_scale);
82 canvas_->scale(scale_scalar, scale_scalar);
85 // static
86 void Canvas::SizeStringInt(const base::string16& text,
87 const FontList& font_list,
88 int* width,
89 int* height,
90 int line_height,
91 int flags) {
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);
100 // static
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);
105 return width;
108 // static
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);
113 return width;
116 // static
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
124 // of the bitmap.
125 const SkISize size = canvas_->getDeviceSize();
126 SkBitmap result;
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 if (rect.IsEmpty())
135 return;
136 // Create a 2D bitmap containing alternating on/off pixels - we do this
137 // so that you never get two pixels of the same color around the edges
138 // of the focus rect (this may mean that opposing edges of the rect may
139 // have a dot pattern out of phase to each other).
140 static SkColor last_color;
141 static SkBitmap* dots = NULL;
142 if (!dots || last_color != color) {
143 int col_pixels = 32;
144 int row_pixels = 32;
146 delete dots;
147 last_color = color;
148 dots = new SkBitmap;
149 dots->allocN32Pixels(col_pixels, row_pixels);
150 dots->eraseARGB(0, 0, 0, 0);
152 uint32_t* dot = dots->getAddr32(0, 0);
153 for (int i = 0; i < row_pixels; i++) {
154 for (int u = 0; u < col_pixels; u++) {
155 if ((u % 2 + i % 2) % 2 != 0) {
156 dot[i * row_pixels + u] = color;
162 // Make a shader for the bitmap with an origin of the box we'll draw. This
163 // shader is refcounted and will have an initial refcount of 1.
164 skia::RefPtr<SkShader> shader = skia::AdoptRef(
165 SkShader::CreateBitmapShader(
166 *dots, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode));
167 // Assign the shader to the paint & release our reference. The paint will
168 // now own the shader and the shader will be destroyed when the paint goes
169 // out of scope.
170 SkPaint paint;
171 paint.setShader(shader.get());
173 DrawRect(Rect(rect.x(), rect.y(), rect.width(), 1), paint);
174 DrawRect(Rect(rect.x(), rect.y() + rect.height() - 1, rect.width(), 1),
175 paint);
176 DrawRect(Rect(rect.x(), rect.y(), 1, rect.height()), paint);
177 DrawRect(Rect(rect.x() + rect.width() - 1, rect.y(), 1, rect.height()),
178 paint);
181 void Canvas::Save() {
182 canvas_->save();
185 void Canvas::SaveLayerAlpha(uint8 alpha) {
186 canvas_->saveLayerAlpha(NULL, alpha);
189 void Canvas::SaveLayerAlpha(uint8 alpha, const Rect& layer_bounds) {
190 SkRect bounds(RectToSkRect(layer_bounds));
191 canvas_->saveLayerAlpha(&bounds, alpha);
194 void Canvas::Restore() {
195 canvas_->restore();
198 void Canvas::ClipRect(const Rect& rect) {
199 canvas_->clipRect(RectToSkRect(rect));
202 void Canvas::ClipPath(const SkPath& path, bool do_anti_alias) {
203 canvas_->clipPath(path, SkRegion::kIntersect_Op, do_anti_alias);
206 bool Canvas::IsClipEmpty() const {
207 return canvas_->isClipEmpty();
210 bool Canvas::GetClipBounds(Rect* bounds) {
211 SkRect out;
212 bool has_non_empty_clip = canvas_->getClipBounds(&out);
213 bounds->SetRect(out.left(), out.top(), out.width(), out.height());
214 return has_non_empty_clip;
217 void Canvas::Translate(const Vector2d& offset) {
218 canvas_->translate(SkIntToScalar(offset.x()), SkIntToScalar(offset.y()));
221 void Canvas::Scale(int x_scale, int y_scale) {
222 canvas_->scale(SkIntToScalar(x_scale), SkIntToScalar(y_scale));
225 void Canvas::DrawColor(SkColor color) {
226 DrawColor(color, SkXfermode::kSrcOver_Mode);
229 void Canvas::DrawColor(SkColor color, SkXfermode::Mode mode) {
230 canvas_->drawColor(color, mode);
233 void Canvas::FillRect(const Rect& rect, SkColor color) {
234 FillRect(rect, color, SkXfermode::kSrcOver_Mode);
237 void Canvas::FillRect(const Rect& rect,
238 SkColor color,
239 SkXfermode::Mode mode) {
240 SkPaint paint;
241 paint.setColor(color);
242 paint.setStyle(SkPaint::kFill_Style);
243 paint.setXfermodeMode(mode);
244 DrawRect(rect, paint);
247 void Canvas::DrawRect(const Rect& rect, SkColor color) {
248 DrawRect(rect, color, SkXfermode::kSrcOver_Mode);
251 void Canvas::DrawRect(const Rect& rect,
252 SkColor color,
253 SkXfermode::Mode mode) {
254 SkPaint paint;
255 paint.setColor(color);
256 paint.setStyle(SkPaint::kStroke_Style);
257 // Set a stroke width of 0, which will put us down the stroke rect path. If
258 // we set a stroke width of 1, for example, this will internally create a
259 // path and fill it, which causes problems near the edge of the canvas.
260 paint.setStrokeWidth(SkIntToScalar(0));
261 paint.setXfermodeMode(mode);
263 DrawRect(rect, paint);
266 void Canvas::DrawRect(const Rect& rect, const SkPaint& paint) {
267 canvas_->drawIRect(RectToSkIRect(rect), paint);
270 void Canvas::DrawPoint(const Point& p1, const SkPaint& paint) {
271 canvas_->drawPoint(SkIntToScalar(p1.x()), SkIntToScalar(p1.y()), paint);
274 void Canvas::DrawLine(const Point& p1, const Point& p2, SkColor color) {
275 SkPaint paint;
276 paint.setColor(color);
277 paint.setStrokeWidth(SkIntToScalar(1));
278 DrawLine(p1, p2, paint);
281 void Canvas::DrawLine(const Point& p1, const Point& p2, const SkPaint& paint) {
282 canvas_->drawLine(SkIntToScalar(p1.x()), SkIntToScalar(p1.y()),
283 SkIntToScalar(p2.x()), SkIntToScalar(p2.y()), paint);
286 void Canvas::DrawCircle(const Point& center_point,
287 int radius,
288 const SkPaint& paint) {
289 canvas_->drawCircle(SkIntToScalar(center_point.x()),
290 SkIntToScalar(center_point.y()), SkIntToScalar(radius), paint);
293 void Canvas::DrawRoundRect(const Rect& rect,
294 int radius,
295 const SkPaint& paint) {
296 canvas_->drawRoundRect(RectToSkRect(rect), SkIntToScalar(radius),
297 SkIntToScalar(radius), paint);
300 void Canvas::DrawPath(const SkPath& path, const SkPaint& paint) {
301 canvas_->drawPath(path, paint);
304 void Canvas::DrawFocusRect(const Rect& rect) {
305 DrawDashedRect(rect, SK_ColorGRAY);
308 void Canvas::DrawSolidFocusRect(const Rect& rect, SkColor color) {
309 SkPaint paint;
310 paint.setColor(color);
311 paint.setStrokeWidth(SkIntToScalar(1));
312 // Note: We cannot use DrawRect since it would create a path and fill it which
313 // would cause problems near the edge of the canvas.
314 int x1 = std::min(rect.x(), rect.right());
315 int x2 = std::max(rect.x(), rect.right());
316 int y1 = std::min(rect.y(), rect.bottom());
317 int y2 = std::max(rect.y(), rect.bottom());
318 DrawLine(Point(x1, y1), Point(x2, y1), paint);
319 DrawLine(Point(x1, y2), Point(x2, y2), paint);
320 DrawLine(Point(x1, y1), Point(x1, y2), paint);
321 DrawLine(Point(x2, y1), Point(x2, y2 + 1), paint);
324 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y) {
325 SkPaint paint;
326 DrawImageInt(image, x, y, paint);
329 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y, uint8 a) {
330 SkPaint paint;
331 paint.setAlpha(a);
332 DrawImageInt(image, x, y, paint);
335 void Canvas::DrawImageInt(const ImageSkia& image,
336 int x,
337 int y,
338 const SkPaint& paint) {
339 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_);
340 if (image_rep.is_null())
341 return;
342 const SkBitmap& bitmap = image_rep.sk_bitmap();
343 float bitmap_scale = image_rep.scale();
345 canvas_->save();
346 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale),
347 SkFloatToScalar(1.0f / bitmap_scale));
348 canvas_->drawBitmap(bitmap,
349 SkFloatToScalar(x * bitmap_scale),
350 SkFloatToScalar(y * bitmap_scale),
351 &paint);
352 canvas_->restore();
355 void Canvas::DrawImageInt(const ImageSkia& image,
356 int src_x,
357 int src_y,
358 int src_w,
359 int src_h,
360 int dest_x,
361 int dest_y,
362 int dest_w,
363 int dest_h,
364 bool filter) {
365 SkPaint p;
366 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y,
367 dest_w, dest_h, filter, p);
370 void Canvas::DrawImageInt(const ImageSkia& image,
371 int src_x,
372 int src_y,
373 int src_w,
374 int src_h,
375 int dest_x,
376 int dest_y,
377 int dest_w,
378 int dest_h,
379 bool filter,
380 const SkPaint& paint) {
381 DrawImageIntHelper(image, src_x, src_y, src_w, src_h, dest_x, dest_y, dest_w,
382 dest_h, filter, paint, image_scale_, false);
385 void Canvas::DrawImageIntInPixel(const ImageSkia& image,
386 int src_x,
387 int src_y,
388 int src_w,
389 int src_h,
390 int dest_x,
391 int dest_y,
392 int dest_w,
393 int dest_h,
394 bool filter,
395 const SkPaint& paint) {
396 // All values passed into this function are in pixels, i.e. no scaling needs
397 // be done.
398 // Logic as below:-
399 // 1. Get the matrix transform from the canvas.
400 // 2. Set the scale in the matrix to 1.0 while honoring the direction of the
401 // the scale (x/y). Example RTL layouts.
402 // 3. Round off the X and Y translation components in the matrix. This is to
403 // reduce floating point errors during rect transformation. This is needed
404 // for fractional scale factors like 1.25/1.5, etc.
405 // 4. Save the current state of the canvas.
406 // 5. Set the modified matrix in the canvas. This ensures that no scaling
407 // will be done for draw operations on the canvas.
408 // 6. Draw the image.
409 // 7. Restore the state of the canvas and the SkCanvas matrix stack.
410 SkMatrix matrix = canvas_->getTotalMatrix();
412 // Ensure that the direction of the x and y scales is preserved. This is
413 // important for RTL layouts.
414 matrix.getScaleX() > 0 ? matrix.setScaleX(1.0f) : matrix.setScaleX(-1.0f);
415 matrix.getScaleY() > 0 ? matrix.setScaleY(1.0f) : matrix.setScaleY(-1.0f);
417 matrix.setTranslateX(SkScalarRoundToInt(matrix.getTranslateX()));
418 matrix.setTranslateY(SkScalarRoundToInt(matrix.getTranslateY()));
420 Save();
422 canvas_->setMatrix(matrix);
424 DrawImageIntHelper(image,
425 src_x,
426 src_y,
427 src_w,
428 src_h,
429 dest_x,
430 dest_y,
431 dest_w,
432 dest_h,
433 filter,
434 paint,
435 image_scale_,
436 true);
438 // Restore the state of the canvas.
439 Restore();
442 void Canvas::DrawImageInPath(const ImageSkia& image,
443 int x,
444 int y,
445 const SkPath& path,
446 const SkPaint& paint) {
447 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_);
448 if (image_rep.is_null())
449 return;
451 SkMatrix matrix;
452 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
453 skia::RefPtr<SkShader> shader = CreateImageRepShader(
454 image_rep,
455 SkShader::kRepeat_TileMode,
456 matrix);
458 SkPaint p(paint);
459 p.setShader(shader.get());
460 canvas_->drawPath(path, p);
463 void Canvas::DrawStringRect(const base::string16& text,
464 const FontList& font_list,
465 SkColor color,
466 const Rect& display_rect) {
467 DrawStringRectWithFlags(text, font_list, color, display_rect,
468 DefaultCanvasTextAlignment());
471 void Canvas::DrawStringRectWithFlags(const base::string16& text,
472 const FontList& font_list,
473 SkColor color,
474 const Rect& display_rect,
475 int flags) {
476 DrawStringRectWithShadows(text, font_list, color, display_rect, 0, flags,
477 ShadowValues());
480 void Canvas::TileImageInt(const ImageSkia& image,
481 int x,
482 int y,
483 int w,
484 int h) {
485 TileImageInt(image, 0, 0, x, y, w, h);
488 void Canvas::TileImageInt(const ImageSkia& image,
489 int src_x,
490 int src_y,
491 int dest_x,
492 int dest_y,
493 int w,
494 int h) {
495 TileImageInt(image, src_x, src_y, 1.0f, 1.0f, dest_x, dest_y, w, h);
498 void Canvas::TileImageInt(const ImageSkia& image,
499 int src_x,
500 int src_y,
501 float tile_scale_x,
502 float tile_scale_y,
503 int dest_x,
504 int dest_y,
505 int w,
506 int h) {
507 if (!IntersectsClipRectInt(dest_x, dest_y, w, h))
508 return;
510 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_);
511 if (image_rep.is_null())
512 return;
514 SkMatrix shader_scale;
515 shader_scale.setScale(SkFloatToScalar(tile_scale_x),
516 SkFloatToScalar(tile_scale_y));
517 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y));
518 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y));
520 skia::RefPtr<SkShader> shader = CreateImageRepShader(
521 image_rep,
522 SkShader::kRepeat_TileMode,
523 shader_scale);
525 SkPaint paint;
526 paint.setShader(shader.get());
527 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
529 SkRect dest_rect = { SkIntToScalar(dest_x),
530 SkIntToScalar(dest_y),
531 SkIntToScalar(dest_x + w),
532 SkIntToScalar(dest_y + h) };
533 canvas_->drawRect(dest_rect, paint);
536 NativeDrawingContext Canvas::BeginPlatformPaint() {
537 return skia::BeginPlatformPaint(canvas_);
540 void Canvas::EndPlatformPaint() {
541 skia::EndPlatformPaint(canvas_);
544 void Canvas::Transform(const gfx::Transform& transform) {
545 canvas_->concat(transform.matrix());
548 Canvas::Canvas(SkCanvas* canvas, float image_scale)
549 : image_scale_(image_scale),
550 owned_canvas_(),
551 canvas_(canvas) {
552 DCHECK(canvas);
555 bool Canvas::IntersectsClipRectInt(int x, int y, int w, int h) {
556 SkRect clip;
557 return canvas_->getClipBounds(&clip) &&
558 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w),
559 SkIntToScalar(y + h));
562 bool Canvas::IntersectsClipRect(const Rect& rect) {
563 return IntersectsClipRectInt(rect.x(), rect.y(),
564 rect.width(), rect.height());
567 void Canvas::DrawImageIntHelper(const ImageSkia& image,
568 int src_x,
569 int src_y,
570 int src_w,
571 int src_h,
572 int dest_x,
573 int dest_y,
574 int dest_w,
575 int dest_h,
576 bool filter,
577 const SkPaint& paint,
578 float image_scale,
579 bool pixel) {
580 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() &&
581 src_y + src_h < std::numeric_limits<int16_t>::max());
582 if (src_w <= 0 || src_h <= 0) {
583 NOTREACHED() << "Attempting to draw bitmap from an empty rect!";
584 return;
587 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h))
588 return;
590 float user_scale_x = static_cast<float>(dest_w) / src_w;
591 float user_scale_y = static_cast<float>(dest_h) / src_h;
593 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale);
594 if (image_rep.is_null())
595 return;
597 SkRect dest_rect = { SkIntToScalar(dest_x),
598 SkIntToScalar(dest_y),
599 SkIntToScalar(dest_x + dest_w),
600 SkIntToScalar(dest_y + dest_h) };
602 if (src_w == dest_w && src_h == dest_h &&
603 user_scale_x == 1.0f && user_scale_y == 1.0f &&
604 image_rep.scale() == 1.0f && !pixel) {
605 // Workaround for apparent bug in Skia that causes image to occasionally
606 // shift.
607 SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h };
608 const SkBitmap& bitmap = image_rep.sk_bitmap();
609 canvas_->drawBitmapRect(bitmap, &src_rect, dest_rect, &paint);
610 return;
613 // Make a bitmap shader that contains the bitmap we want to draw. This is
614 // basically what SkCanvas.drawBitmap does internally, but it gives us
615 // more control over quality and will use the mipmap in the source image if
616 // it has one, whereas drawBitmap won't.
617 SkMatrix shader_scale;
618 shader_scale.setScale(SkFloatToScalar(user_scale_x),
619 SkFloatToScalar(user_scale_y));
620 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y));
621 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y));
623 skia::RefPtr<SkShader> shader = CreateImageRepShaderForScale(
624 image_rep,
625 SkShader::kRepeat_TileMode,
626 shader_scale,
627 pixel ? 1.0f : image_rep.scale());
629 // Set up our paint to use the shader & release our reference (now just owned
630 // by the paint).
631 SkPaint p(paint);
632 p.setFilterLevel(filter ? SkPaint::kLow_FilterLevel
633 : SkPaint::kNone_FilterLevel);
634 p.setShader(shader.get());
636 // The rect will be filled by the bitmap.
637 canvas_->drawRect(dest_rect, p);
640 } // namespace gfx