| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/nine_image_painter.h" | 5 #include "ui/gfx/nine_image_painter.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "third_party/skia/include/core/SkPaint.h" | 9 #include "third_party/skia/include/core/SkPaint.h" |
| 10 #include "third_party/skia/include/core/SkRect.h" | 10 #include "third_party/skia/include/core/SkRect.h" |
| 11 #include "third_party/skia/include/core/SkScalar.h" | 11 #include "third_party/skia/include/core/SkScalar.h" |
| 12 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
| 13 #include "ui/gfx/geometry/insets.h" | 13 #include "ui/gfx/geometry/insets.h" |
| 14 #include "ui/gfx/geometry/rect.h" | 14 #include "ui/gfx/geometry/rect.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/geometry/safe_integer_conversions.h" |
| 17 #include "ui/gfx/image/image_skia_operations.h" | 17 #include "ui/gfx/image/image_skia_operations.h" |
| 18 #include "ui/gfx/scoped_canvas.h" | 18 #include "ui/gfx/scoped_canvas.h" |
| 19 #include "ui/gfx/skia_util.h" | 19 #include "ui/gfx/skia_util.h" |
| 20 | 20 |
| 21 namespace gfx { | 21 namespace gfx { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 // The following functions calculate width and height of the image in pixels | 25 int ImageRepWidthInPixels(const ImageSkiaRep& rep) { |
| 26 // for the scale factor. | 26 if (rep.is_null()) |
| 27 int ImageWidthInPixels(const ImageSkia& i, float scale) { | |
| 28 if (i.isNull()) | |
| 29 return 0; | 27 return 0; |
| 30 ImageSkiaRep image_rep = i.GetRepresentation(scale); | 28 return rep.pixel_width(); |
| 31 return image_rep.pixel_width() * scale / image_rep.scale(); | |
| 32 } | 29 } |
| 33 | 30 |
| 34 int ImageHeightInPixels(const ImageSkia& i, float scale) { | 31 int ImageRepHeightInPixels(const ImageSkiaRep& rep) { |
| 35 if (i.isNull()) | 32 if (rep.is_null()) |
| 36 return 0; | 33 return 0; |
| 37 ImageSkiaRep image_rep = i.GetRepresentation(scale); | 34 return rep.pixel_height(); |
| 38 return image_rep.pixel_height() * scale / image_rep.scale(); | |
| 39 } | 35 } |
| 40 | 36 |
| 41 // Stretches the given image over the specified canvas area. | |
| 42 void Fill(Canvas* c, | 37 void Fill(Canvas* c, |
| 43 const ImageSkia& i, | 38 const ImageSkiaRep& rep, |
| 44 int x, | 39 int x, |
| 45 int y, | 40 int y, |
| 46 int w, | 41 int w, |
| 47 int h, | 42 int h, |
| 48 const SkPaint& paint) { | 43 const SkPaint& paint) { |
| 49 if (i.isNull()) | 44 if (rep.is_null()) |
| 50 return; | 45 return; |
| 51 c->DrawImageIntInPixel(i, 0, 0, ImageWidthInPixels(i, c->image_scale()), | 46 c->DrawImageIntInPixel(rep, x, y, w, h, false, paint); |
| 52 ImageHeightInPixels(i, c->image_scale()), | |
| 53 x, y, w, h, false, paint); | |
| 54 } | 47 } |
| 55 | 48 |
| 56 } // namespace | 49 } // namespace |
| 57 | 50 |
| 58 NineImagePainter::NineImagePainter(const std::vector<ImageSkia>& images) { | 51 NineImagePainter::NineImagePainter(const std::vector<ImageSkia>& images) { |
| 59 DCHECK_EQ(arraysize(images_), images.size()); | 52 DCHECK_EQ(arraysize(images_), images.size()); |
| 60 for (size_t i = 0; i < arraysize(images_); ++i) | 53 for (size_t i = 0; i < arraysize(images_); ++i) |
| 61 images_[i] = images[i]; | 54 images_[i] = images[i]; |
| 62 } | 55 } |
| 63 | 56 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 88 // When no alpha value is specified, use default value of 100% opacity. | 81 // When no alpha value is specified, use default value of 100% opacity. |
| 89 Paint(canvas, bounds, std::numeric_limits<uint8>::max()); | 82 Paint(canvas, bounds, std::numeric_limits<uint8>::max()); |
| 90 } | 83 } |
| 91 | 84 |
| 92 void NineImagePainter::Paint(Canvas* canvas, | 85 void NineImagePainter::Paint(Canvas* canvas, |
| 93 const Rect& bounds, | 86 const Rect& bounds, |
| 94 const uint8 alpha) { | 87 const uint8 alpha) { |
| 95 if (IsEmpty()) | 88 if (IsEmpty()) |
| 96 return; | 89 return; |
| 97 | 90 |
| 98 ScopedCanvas scoped_canvas(canvas); | 91 // Painting at physical device pixels (undo device scale factor). |
| 99 canvas->Translate(bounds.OffsetFromOrigin()); | 92 float scale = canvas->SaveAndUnscale(); |
| 100 | 93 |
| 101 // Get the current transform from the canvas and apply it to the logical | 94 // Since the drawing from the following Fill() calls assumes the mapped origin |
| 102 // bounds passed in. This will give us the pixel bounds which can be used | 95 // is at (0,0), we need to translate the canvas to the mapped origin. |
| 103 // to draw the images at the correct locations. | 96 const int left_in_pixels = ToRoundedInt(bounds.x() * scale); |
| 104 // We should not scale the bounds by the canvas->image_scale() as that can be | 97 const int top_in_pixels = ToRoundedInt(bounds.y() * scale); |
| 105 // different from the real scale in the canvas transform. | 98 const int right_in_pixels = ToRoundedInt(bounds.right() * scale); |
| 106 SkRect bounds_in_pixels_f; | 99 const int bottom_in_pixels = ToRoundedInt(bounds.bottom() * scale); |
| 107 if (!canvas->sk_canvas()->getTotalMatrix().mapRect( | |
| 108 &bounds_in_pixels_f, RectToSkRect(gfx::Rect(bounds.size())))) | |
| 109 return; // Invalid transform. | |
| 110 | 100 |
| 111 SkIRect bounds_in_pixels; | 101 const int width_in_pixels = right_in_pixels - left_in_pixels; |
| 112 bounds_in_pixels_f.dround(&bounds_in_pixels); | 102 const int height_in_pixels = bottom_in_pixels - top_in_pixels; |
| 113 | 103 |
| 114 SkMatrix matrix = canvas->sk_canvas()->getTotalMatrix(); | 104 // Since the drawing from the following Fill() calls assumes the mapped origin |
| 115 matrix.setTranslateX(SkIntToScalar(bounds_in_pixels.x())); | 105 // is at (0,0), we need to translate the canvas to the mapped origin. |
| 116 matrix.setTranslateY(SkIntToScalar(bounds_in_pixels.y())); | 106 canvas->Translate(gfx::Vector2d(left_in_pixels, top_in_pixels)); |
| 117 canvas->sk_canvas()->setMatrix(matrix); | |
| 118 | 107 |
| 119 const int width_in_pixels = bounds_in_pixels.width(); | 108 ImageSkiaRep image_reps[9]; |
| 120 const int height_in_pixels = bounds_in_pixels.height(); | 109 static_assert(arraysize(image_reps) == arraysize(images_), ""); |
| 121 const float scale_x = matrix.getScaleX(); | 110 for (size_t i = 0; i < arraysize(image_reps); ++i) { |
| 122 const float scale_y = matrix.getScaleY(); | 111 image_reps[i] = images_[i].GetRepresentation(scale); |
| 112 DCHECK(image_reps[i].is_null() || image_reps[i].scale() == scale); |
| 113 } |
| 123 | 114 |
| 124 // In case the corners and edges don't all have the same width/height, we draw | 115 // In case the corners and edges don't all have the same width/height, we draw |
| 125 // the center first, and extend it out in all directions to the edges of the | 116 // the center first, and extend it out in all directions to the edges of the |
| 126 // images with the smallest widths/heights. This way there will be no | 117 // images with the smallest widths/heights. This way there will be no |
| 127 // unpainted areas, though some corners or edges might overlap the center. | 118 // unpainted areas, though some corners or edges might overlap the center. |
| 128 int i0w = ImageWidthInPixels(images_[0], scale_x); | 119 int i0w = ImageRepWidthInPixels(image_reps[0]); |
| 129 int i2w = ImageWidthInPixels(images_[2], scale_x); | 120 int i2w = ImageRepWidthInPixels(image_reps[2]); |
| 130 int i3w = ImageWidthInPixels(images_[3], scale_x); | 121 int i3w = ImageRepWidthInPixels(image_reps[3]); |
| 131 int i5w = ImageWidthInPixels(images_[5], scale_x); | 122 int i5w = ImageRepWidthInPixels(image_reps[5]); |
| 132 int i6w = ImageWidthInPixels(images_[6], scale_x); | 123 int i6w = ImageRepWidthInPixels(image_reps[6]); |
| 133 int i8w = ImageWidthInPixels(images_[8], scale_x); | 124 int i8w = ImageRepWidthInPixels(image_reps[8]); |
| 134 | 125 |
| 135 int i0h = ImageHeightInPixels(images_[0], scale_y); | 126 int i0h = ImageRepHeightInPixels(image_reps[0]); |
| 136 int i1h = ImageHeightInPixels(images_[1], scale_y); | 127 int i1h = ImageRepHeightInPixels(image_reps[1]); |
| 137 int i2h = ImageHeightInPixels(images_[2], scale_y); | 128 int i2h = ImageRepHeightInPixels(image_reps[2]); |
| 138 int i6h = ImageHeightInPixels(images_[6], scale_y); | 129 int i6h = ImageRepHeightInPixels(image_reps[6]); |
| 139 int i7h = ImageHeightInPixels(images_[7], scale_y); | 130 int i7h = ImageRepHeightInPixels(image_reps[7]); |
| 140 int i8h = ImageHeightInPixels(images_[8], scale_y); | 131 int i8h = ImageRepHeightInPixels(image_reps[8]); |
| 141 | 132 |
| 142 i0w = std::min(i0w, width_in_pixels); | 133 i0w = std::min(i0w, width_in_pixels); |
| 143 i2w = std::min(i2w, width_in_pixels - i0w); | 134 i2w = std::min(i2w, width_in_pixels - i0w); |
| 144 i3w = std::min(i3w, width_in_pixels); | 135 i3w = std::min(i3w, width_in_pixels); |
| 145 i5w = std::min(i5w, width_in_pixels - i3w); | 136 i5w = std::min(i5w, width_in_pixels - i3w); |
| 146 i6w = std::min(i6w, width_in_pixels); | 137 i6w = std::min(i6w, width_in_pixels); |
| 147 i8w = std::min(i8w, width_in_pixels - i6w); | 138 i8w = std::min(i8w, width_in_pixels - i6w); |
| 148 | 139 |
| 149 i0h = std::min(i0h, height_in_pixels); | 140 i0h = std::min(i0h, height_in_pixels); |
| 150 i1h = std::min(i1h, height_in_pixels); | 141 i1h = std::min(i1h, height_in_pixels); |
| 151 i2h = std::min(i2h, height_in_pixels); | 142 i2h = std::min(i2h, height_in_pixels); |
| 152 i6h = std::min(i6h, height_in_pixels - i0h); | 143 i6h = std::min(i6h, height_in_pixels - i0h); |
| 153 i7h = std::min(i7h, height_in_pixels - i1h); | 144 i7h = std::min(i7h, height_in_pixels - i1h); |
| 154 i8h = std::min(i8h, height_in_pixels - i2h); | 145 i8h = std::min(i8h, height_in_pixels - i2h); |
| 155 | 146 |
| 156 int i4x = std::min(std::min(i0w, i3w), i6w); | 147 int i4x = std::min(std::min(i0w, i3w), i6w); |
| 157 int i4y = std::min(std::min(i0h, i1h), i2h); | 148 int i4y = std::min(std::min(i0h, i1h), i2h); |
| 158 int i4w = | 149 int i4w = |
| 159 std::max(width_in_pixels - i4x - std::min(std::min(i2w, i5w), i8w), 0); | 150 std::max(width_in_pixels - i4x - std::min(std::min(i2w, i5w), i8w), 0); |
| 160 int i4h = | 151 int i4h = |
| 161 std::max(height_in_pixels - i4y - std::min(std::min(i6h, i7h), i8h), 0); | 152 std::max(height_in_pixels - i4y - std::min(std::min(i6h, i7h), i8h), 0); |
| 162 | 153 |
| 163 SkPaint paint; | 154 SkPaint paint; |
| 164 paint.setAlpha(alpha); | 155 paint.setAlpha(alpha); |
| 165 | 156 |
| 166 Fill(canvas, images_[4], i4x, i4y, i4w, i4h, paint); | 157 Fill(canvas, image_reps[4], i4x, i4y, i4w, i4h, paint); |
| 158 Fill(canvas, image_reps[0], 0, 0, i0w, i0h, paint); |
| 159 Fill(canvas, image_reps[1], i0w, 0, width_in_pixels - i0w - i2w, i1h, |
| 160 paint); |
| 161 Fill(canvas, image_reps[2], width_in_pixels - i2w, 0, i2w, i2h, paint); |
| 162 Fill(canvas, image_reps[3], 0, i0h, i3w, height_in_pixels - i0h - i6h, |
| 163 paint); |
| 164 Fill(canvas, image_reps[5], width_in_pixels - i5w, i2h, i5w, |
| 165 height_in_pixels - i2h - i8h, paint); |
| 166 Fill(canvas, image_reps[6], 0, height_in_pixels - i6h, i6w, i6h, paint); |
| 167 Fill(canvas, image_reps[7], i6w, height_in_pixels - i7h, |
| 168 width_in_pixels - i6w - i8w, i7h, paint); |
| 169 Fill(canvas, image_reps[8], width_in_pixels - i8w, height_in_pixels - i8h, |
| 170 i8w, i8h, paint); |
| 167 | 171 |
| 168 Fill(canvas, images_[0], 0, 0, i0w, i0h, paint); | 172 canvas->Restore(); |
| 169 | |
| 170 Fill(canvas, images_[1], i0w, 0, width_in_pixels - i0w - i2w, i1h, paint); | |
| 171 | |
| 172 Fill(canvas, images_[2], width_in_pixels - i2w, 0, i2w, i2h, paint); | |
| 173 | |
| 174 Fill(canvas, images_[3], 0, i0h, i3w, height_in_pixels - i0h - i6h, paint); | |
| 175 | |
| 176 Fill(canvas, images_[5], width_in_pixels - i5w, i2h, i5w, | |
| 177 height_in_pixels - i2h - i8h, paint); | |
| 178 | |
| 179 Fill(canvas, images_[6], 0, height_in_pixels - i6h, i6w, i6h, paint); | |
| 180 | |
| 181 Fill(canvas, images_[7], i6w, height_in_pixels - i7h, | |
| 182 width_in_pixels - i6w - i8w, i7h, paint); | |
| 183 | |
| 184 Fill(canvas, images_[8], width_in_pixels - i8w, height_in_pixels - i8h, i8w, | |
| 185 i8h, paint); | |
| 186 } | 173 } |
| 187 | 174 |
| 188 // static | 175 // static |
| 189 void NineImagePainter::GetSubsetRegions(const ImageSkia& image, | 176 void NineImagePainter::GetSubsetRegions(const ImageSkia& image, |
| 190 const Insets& insets, | 177 const Insets& insets, |
| 191 std::vector<Rect>* regions) { | 178 std::vector<Rect>* regions) { |
| 192 DCHECK_GE(image.width(), insets.width()); | 179 DCHECK_GE(image.width(), insets.width()); |
| 193 DCHECK_GE(image.height(), insets.height()); | 180 DCHECK_GE(image.height(), insets.height()); |
| 194 | 181 |
| 195 std::vector<Rect> result(9); | 182 std::vector<Rect> result(9); |
| 196 | 183 |
| 197 const int x[] = { | 184 const int x[] = { |
| 198 0, insets.left(), image.width() - insets.right(), image.width()}; | 185 0, insets.left(), image.width() - insets.right(), image.width()}; |
| 199 const int y[] = { | 186 const int y[] = { |
| 200 0, insets.top(), image.height() - insets.bottom(), image.height()}; | 187 0, insets.top(), image.height() - insets.bottom(), image.height()}; |
| 201 | 188 |
| 202 for (size_t j = 0; j < 3; ++j) { | 189 for (size_t j = 0; j < 3; ++j) { |
| 203 for (size_t i = 0; i < 3; ++i) { | 190 for (size_t i = 0; i < 3; ++i) { |
| 204 result[i + j * 3] = Rect(x[i], y[j], x[i + 1] - x[i], y[j + 1] - y[j]); | 191 result[i + j * 3] = Rect(x[i], y[j], x[i + 1] - x[i], y[j + 1] - y[j]); |
| 205 } | 192 } |
| 206 } | 193 } |
| 207 result.swap(*regions); | 194 result.swap(*regions); |
| 208 } | 195 } |
| 209 | 196 |
| 210 } // namespace gfx | 197 } // namespace gfx |
| OLD | NEW |