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 <cmath> | 7 #include <cmath> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
13 #include "third_party/skia/include/effects/SkGradientShader.h" | 13 #include "third_party/skia/include/effects/SkGradientShader.h" |
14 #include "ui/gfx/font_list.h" | 14 #include "ui/gfx/font_list.h" |
15 #include "ui/gfx/geometry/rect_conversions.h" | 15 #include "ui/gfx/geometry/rect_conversions.h" |
| 16 #include "ui/gfx/geometry/safe_integer_conversions.h" |
16 #include "ui/gfx/rect.h" | 17 #include "ui/gfx/rect.h" |
| 18 #include "ui/gfx/scoped_canvas.h" |
17 #include "ui/gfx/size_conversions.h" | 19 #include "ui/gfx/size_conversions.h" |
18 #include "ui/gfx/skia_util.h" | 20 #include "ui/gfx/skia_util.h" |
19 #include "ui/gfx/transform.h" | 21 #include "ui/gfx/transform.h" |
20 | 22 |
21 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
22 #include "ui/gfx/canvas_skia_paint.h" | 24 #include "ui/gfx/canvas_skia_paint.h" |
23 #endif | 25 #endif |
24 | 26 |
25 namespace gfx { | 27 namespace gfx { |
26 | 28 |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 void Canvas::DrawImageInt(const ImageSkia& image, | 341 void Canvas::DrawImageInt(const ImageSkia& image, |
340 int x, | 342 int x, |
341 int y, | 343 int y, |
342 const SkPaint& paint) { | 344 const SkPaint& paint) { |
343 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); | 345 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
344 if (image_rep.is_null()) | 346 if (image_rep.is_null()) |
345 return; | 347 return; |
346 const SkBitmap& bitmap = image_rep.sk_bitmap(); | 348 const SkBitmap& bitmap = image_rep.sk_bitmap(); |
347 float bitmap_scale = image_rep.scale(); | 349 float bitmap_scale = image_rep.scale(); |
348 | 350 |
349 canvas_->save(); | 351 ScopedCanvas scoper(this); |
350 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), | 352 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), |
351 SkFloatToScalar(1.0f / bitmap_scale)); | 353 SkFloatToScalar(1.0f / bitmap_scale)); |
352 canvas_->drawBitmap(bitmap, | 354 canvas_->drawBitmap(bitmap, |
353 SkFloatToScalar(x * bitmap_scale), | 355 SkFloatToScalar(x * bitmap_scale), |
354 SkFloatToScalar(y * bitmap_scale), | 356 SkFloatToScalar(y * bitmap_scale), |
355 &paint); | 357 &paint); |
356 canvas_->restore(); | |
357 } | 358 } |
358 | 359 |
359 void Canvas::DrawImageInt(const ImageSkia& image, | 360 void Canvas::DrawImageInt(const ImageSkia& image, |
360 int src_x, | 361 int src_x, |
361 int src_y, | 362 int src_y, |
362 int src_w, | 363 int src_w, |
363 int src_h, | 364 int src_h, |
364 int dest_x, | 365 int dest_x, |
365 int dest_y, | 366 int dest_y, |
366 int dest_w, | 367 int dest_w, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 // for fractional scale factors like 1.25/1.5, etc. | 409 // for fractional scale factors like 1.25/1.5, etc. |
409 // 4. Save the current state of the canvas. | 410 // 4. Save the current state of the canvas. |
410 // 5. Set the modified matrix in the canvas. This ensures that no scaling | 411 // 5. Set the modified matrix in the canvas. This ensures that no scaling |
411 // will be done for draw operations on the canvas. | 412 // will be done for draw operations on the canvas. |
412 // 6. Draw the image. | 413 // 6. Draw the image. |
413 // 7. Restore the state of the canvas and the SkCanvas matrix stack. | 414 // 7. Restore the state of the canvas and the SkCanvas matrix stack. |
414 SkMatrix matrix = canvas_->getTotalMatrix(); | 415 SkMatrix matrix = canvas_->getTotalMatrix(); |
415 | 416 |
416 // Ensure that the direction of the x and y scales is preserved. This is | 417 // Ensure that the direction of the x and y scales is preserved. This is |
417 // important for RTL layouts. | 418 // important for RTL layouts. |
418 matrix.getScaleX() > 0 ? matrix.setScaleX(1.0f) : matrix.setScaleX(-1.0f); | 419 matrix.setScaleX(matrix.getScaleX() > 0 ? 1.0f : -1.0f); |
419 matrix.getScaleY() > 0 ? matrix.setScaleY(1.0f) : matrix.setScaleY(-1.0f); | 420 matrix.setScaleY(matrix.getScaleY() > 0 ? 1.0f : -1.0f); |
420 | 421 |
421 matrix.setTranslateX(SkScalarRoundToInt(matrix.getTranslateX())); | 422 // Floor so that we get consistent rounding. |
422 matrix.setTranslateY(SkScalarRoundToInt(matrix.getTranslateY())); | 423 matrix.setTranslateX(SkScalarFloorToScalar(matrix.getTranslateX())); |
| 424 matrix.setTranslateY(SkScalarFloorToScalar(matrix.getTranslateY())); |
423 | 425 |
424 Save(); | 426 ScopedCanvas scoper(this); |
425 | 427 |
426 canvas_->setMatrix(matrix); | 428 canvas_->setMatrix(matrix); |
427 | 429 |
428 DrawImageIntHelper(image, | 430 DrawImageIntHelper(image, |
429 src_x, | 431 src_x, |
430 src_y, | 432 src_y, |
431 src_w, | 433 src_w, |
432 src_h, | 434 src_h, |
433 dest_x, | 435 dest_x, |
434 dest_y, | 436 dest_y, |
435 dest_w, | 437 dest_w, |
436 dest_h, | 438 dest_h, |
437 filter, | 439 filter, |
438 paint, | 440 paint, |
439 image_scale_, | 441 image_scale_, |
440 true); | 442 true); |
441 | |
442 // Restore the state of the canvas. | |
443 Restore(); | |
444 } | 443 } |
445 | 444 |
446 void Canvas::DrawImageInPath(const ImageSkia& image, | 445 void Canvas::DrawImageInPath(const ImageSkia& image, |
447 int x, | 446 int x, |
448 int y, | 447 int y, |
449 const SkPath& path, | 448 const SkPath& path, |
450 const SkPaint& paint) { | 449 const SkPaint& paint) { |
451 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); | 450 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
452 if (image_rep.is_null()) | 451 if (image_rep.is_null()) |
453 return; | 452 return; |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 SkPaint p(paint); | 634 SkPaint p(paint); |
636 p.setFilterLevel(filter ? SkPaint::kLow_FilterLevel | 635 p.setFilterLevel(filter ? SkPaint::kLow_FilterLevel |
637 : SkPaint::kNone_FilterLevel); | 636 : SkPaint::kNone_FilterLevel); |
638 p.setShader(shader.get()); | 637 p.setShader(shader.get()); |
639 | 638 |
640 // The rect will be filled by the bitmap. | 639 // The rect will be filled by the bitmap. |
641 canvas_->drawRect(dest_rect, p); | 640 canvas_->drawRect(dest_rect, p); |
642 } | 641 } |
643 | 642 |
644 } // namespace gfx | 643 } // namespace gfx |
OLD | NEW |