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