Chromium Code Reviews| Index: ui/gfx/nine_image_painter.cc |
| diff --git a/ui/gfx/nine_image_painter.cc b/ui/gfx/nine_image_painter.cc |
| index 92a38f7723757f06e6d9ce6d63defc1fca42c759..4aa13878872bfe71adc7dcc47636905ce4c2723f 100644 |
| --- a/ui/gfx/nine_image_painter.cc |
| +++ b/ui/gfx/nine_image_painter.cc |
| @@ -8,6 +8,8 @@ |
| #include "third_party/skia/include/core/SkPaint.h" |
| #include "ui/gfx/canvas.h" |
| +#include "ui/gfx/geometry/rect_conversions.h" |
| +#include "ui/gfx/geometry/safe_integer_conversions.h" |
| #include "ui/gfx/image/image_skia_operations.h" |
| #include "ui/gfx/insets.h" |
| #include "ui/gfx/rect.h" |
| @@ -17,6 +19,16 @@ namespace gfx { |
| namespace { |
| +// The following functions width and height of the image in pixels for the |
| +// scale factor in the Canvas. |
| +int ImageWidthInPixels(const ImageSkia& i, Canvas* c) { |
| + return i.GetRepresentation(c->image_scale()).pixel_width(); |
| +} |
| + |
| +int ImageHeightInPixels(const ImageSkia& i, Canvas* c) { |
| + return i.GetRepresentation(c->image_scale()).pixel_height(); |
| +} |
| + |
| // Stretches the given image over the specified canvas area. |
| void Fill(Canvas* c, |
| const ImageSkia& i, |
| @@ -25,7 +37,8 @@ void Fill(Canvas* c, |
| int w, |
| int h, |
| const SkPaint& paint) { |
| - c->DrawImageInt(i, 0, 0, i.width(), i.height(), x, y, w, h, false, paint); |
| + c->DrawImageIntInPixel(i, 0, 0, ImageWidthInPixels(i, c), |
| + ImageHeightInPixels(i, c), x, y, w, h, false, paint); |
| } |
| } // namespace |
| @@ -43,9 +56,9 @@ NineImagePainter::NineImagePainter(const ImageSkia& image, |
| // Extract subsets of the original image to match the |images_| format. |
| const int x[] = |
| - { 0, insets.left(), image.width() - insets.right(), image.width() }; |
|
sky
2014/05/05 14:08:16
nit: keep the space here. It's generally what we d
ananta
2014/05/05 18:53:28
Done.
|
| + { 0, insets.left(), image.width() - insets.right(), image.width()}; |
| const int y[] = |
| - { 0, insets.top(), image.height() - insets.bottom(), image.height() }; |
| + { 0, insets.top(), image.height() - insets.bottom(), image.height()}; |
| for (size_t j = 0; j < 3; ++j) { |
| for (size_t i = 0; i < 3; ++i) { |
| @@ -85,38 +98,51 @@ void NineImagePainter::Paint(Canvas* canvas, |
| SkPaint paint; |
| paint.setAlpha(alpha); |
| + // Scale the bounds and convert to the smallest enclosing rectangle after |
| + // scaling. This is to ensure that we paint all pixels correctly. |
| + Rect scaled_enclosing_rect = |
| + ToEnclosingRect(RectF(ScalePoint(bounds.origin(), canvas->image_scale()), |
| + ScaleSize(bounds.size(), canvas->image_scale()))); |
| + |
| // In case the corners and edges don't all have the same width/height, we draw |
| // the center first, and extend it out in all directions to the edges of the |
| // images with the smallest widths/heights. This way there will be no |
| // unpainted areas, though some corners or edges might overlap the center. |
| - int w = bounds.width(); |
| - int i0w = images_[0].width(); |
| - int i2w = images_[2].width(); |
| - int i3w = images_[3].width(); |
| - int i5w = images_[5].width(); |
| - int i6w = images_[6].width(); |
| - int i8w = images_[8].width(); |
| + int w = scaled_enclosing_rect.width(); |
| + int i0w = ImageWidthInPixels(images_[0], canvas); |
| + int i2w = ImageWidthInPixels(images_[2], canvas); |
| + int i3w = ImageWidthInPixels(images_[3], canvas); |
| + int i5w = ImageWidthInPixels(images_[5], canvas); |
| + int i6w = ImageWidthInPixels(images_[6], canvas); |
| + int i8w = ImageWidthInPixels(images_[8], canvas); |
| + |
| int i4x = std::min(std::min(i0w, i3w), i6w); |
| int i4w = w - i4x - std::min(std::min(i2w, i5w), i8w); |
| - int h = bounds.height(); |
| - int i0h = images_[0].height(); |
| - int i1h = images_[1].height(); |
| - int i2h = images_[2].height(); |
| - int i6h = images_[6].height(); |
| - int i7h = images_[7].height(); |
| - int i8h = images_[8].height(); |
| + int h = scaled_enclosing_rect.height(); |
| + |
| + int i0h = ImageHeightInPixels(images_[0], canvas); |
| + int i1h = ImageHeightInPixels(images_[1], canvas); |
| + int i2h = ImageHeightInPixels(images_[2], canvas); |
| + int i6h = ImageHeightInPixels(images_[6], canvas); |
| + int i7h = ImageHeightInPixels(images_[7], canvas); |
| + int i8h = ImageHeightInPixels(images_[8], canvas); |
| + |
| int i4y = std::min(std::min(i0h, i1h), i2h); |
| int i4h = h - i4y - std::min(std::min(i6h, i7h), i8h); |
| if (!images_[4].isNull()) |
| Fill(canvas, images_[4], i4x, i4y, i4w, i4h, paint); |
| - canvas->DrawImageInt(images_[0], 0, 0, paint); |
| + canvas->DrawImageIntInPixel(images_[0], 0, 0, i0w, i0h, |
| + 0, 0, i0w, i0h, false, paint); |
| Fill(canvas, images_[1], i0w, 0, w - i0w - i2w, i1h, paint); |
| - canvas->DrawImageInt(images_[2], w - i2w, 0, paint); |
| + canvas->DrawImageIntInPixel(images_[2], 0, 0, i2w, i2h, w - i2w, 0, |
| + i2w, i2h, false, paint); |
| Fill(canvas, images_[3], 0, i0h, i3w, h - i0h - i6h, paint); |
| Fill(canvas, images_[5], w - i5w, i2h, i5w, h - i2h - i8h, paint); |
| - canvas->DrawImageInt(images_[6], 0, h - i6h, paint); |
| + canvas->DrawImageIntInPixel(images_[6], 0, 0, i6w, i6h, 0, h - i6h, |
| + i6w, i6h, false, paint); |
| Fill(canvas, images_[7], i6w, h - i7h, w - i6w - i8w, i7h, paint); |
| - canvas->DrawImageInt(images_[8], w - i8w, h - i8h, paint); |
| + canvas->DrawImageIntInPixel(images_[8], 0, 0, i8w, i8h, w - i8w, h - i8h, |
| + i8w, i8h, false, paint); |
| } |
| } // namespace gfx |