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 "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
11 #include "ui/gfx/geometry/rect_conversions.h" | |
12 #include "ui/gfx/geometry/safe_integer_conversions.h" | |
11 #include "ui/gfx/image/image_skia_operations.h" | 13 #include "ui/gfx/image/image_skia_operations.h" |
12 #include "ui/gfx/insets.h" | 14 #include "ui/gfx/insets.h" |
13 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
14 #include "ui/gfx/scoped_canvas.h" | 16 #include "ui/gfx/scoped_canvas.h" |
15 | 17 |
16 namespace gfx { | 18 namespace gfx { |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
22 // The following functions width and height of the image in pixels for the | |
23 // scale factor in the Canvas. | |
24 int ImageWidthInPixels(const ImageSkia& i, Canvas* c) { | |
25 return i.GetRepresentation(c->image_scale()).pixel_width(); | |
26 } | |
27 | |
28 int ImageHeightInPixels(const ImageSkia& i, Canvas* c) { | |
29 return i.GetRepresentation(c->image_scale()).pixel_height(); | |
30 } | |
31 | |
20 // Stretches the given image over the specified canvas area. | 32 // Stretches the given image over the specified canvas area. |
21 void Fill(Canvas* c, | 33 void Fill(Canvas* c, |
22 const ImageSkia& i, | 34 const ImageSkia& i, |
23 int x, | 35 int x, |
24 int y, | 36 int y, |
25 int w, | 37 int w, |
26 int h, | 38 int h, |
27 const SkPaint& paint) { | 39 const SkPaint& paint) { |
28 c->DrawImageInt(i, 0, 0, i.width(), i.height(), x, y, w, h, false, paint); | 40 c->DrawImageIntInPixel(i, 0, 0, ImageWidthInPixels(i, c), |
41 ImageHeightInPixels(i, c), x, y, w, h, false, paint); | |
29 } | 42 } |
30 | 43 |
31 } // namespace | 44 } // namespace |
32 | 45 |
33 NineImagePainter::NineImagePainter(const std::vector<ImageSkia>& images) { | 46 NineImagePainter::NineImagePainter(const std::vector<ImageSkia>& images) { |
34 DCHECK_EQ(arraysize(images_), images.size()); | 47 DCHECK_EQ(arraysize(images_), images.size()); |
35 for (size_t i = 0; i < arraysize(images_); ++i) | 48 for (size_t i = 0; i < arraysize(images_); ++i) |
36 images_[i] = images[i]; | 49 images_[i] = images[i]; |
37 } | 50 } |
38 | 51 |
39 NineImagePainter::NineImagePainter(const ImageSkia& image, | 52 NineImagePainter::NineImagePainter(const ImageSkia& image, |
40 const Insets& insets) { | 53 const Insets& insets) { |
41 DCHECK_GE(image.width(), insets.width()); | 54 DCHECK_GE(image.width(), insets.width()); |
42 DCHECK_GE(image.height(), insets.height()); | 55 DCHECK_GE(image.height(), insets.height()); |
43 | 56 |
44 // Extract subsets of the original image to match the |images_| format. | 57 // Extract subsets of the original image to match the |images_| format. |
45 const int x[] = | 58 const int x[] = |
46 { 0, insets.left(), image.width() - insets.right(), image.width() }; | 59 { 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.
| |
47 const int y[] = | 60 const int y[] = |
48 { 0, insets.top(), image.height() - insets.bottom(), image.height() }; | 61 { 0, insets.top(), image.height() - insets.bottom(), image.height()}; |
49 | 62 |
50 for (size_t j = 0; j < 3; ++j) { | 63 for (size_t j = 0; j < 3; ++j) { |
51 for (size_t i = 0; i < 3; ++i) { | 64 for (size_t i = 0; i < 3; ++i) { |
52 images_[i + j * 3] = ImageSkiaOperations::ExtractSubset(image, | 65 images_[i + j * 3] = ImageSkiaOperations::ExtractSubset(image, |
53 Rect(x[i], y[j], x[i + 1] - x[i], y[j + 1] - y[j])); | 66 Rect(x[i], y[j], x[i + 1] - x[i], y[j + 1] - y[j])); |
54 } | 67 } |
55 } | 68 } |
56 } | 69 } |
57 | 70 |
58 NineImagePainter::~NineImagePainter() { | 71 NineImagePainter::~NineImagePainter() { |
(...skipping 19 matching lines...) Expand all Loading... | |
78 const uint8 alpha) { | 91 const uint8 alpha) { |
79 if (IsEmpty()) | 92 if (IsEmpty()) |
80 return; | 93 return; |
81 | 94 |
82 ScopedCanvas scoped_canvas(canvas); | 95 ScopedCanvas scoped_canvas(canvas); |
83 canvas->Translate(bounds.OffsetFromOrigin()); | 96 canvas->Translate(bounds.OffsetFromOrigin()); |
84 | 97 |
85 SkPaint paint; | 98 SkPaint paint; |
86 paint.setAlpha(alpha); | 99 paint.setAlpha(alpha); |
87 | 100 |
101 // Scale the bounds and convert to the smallest enclosing rectangle after | |
102 // scaling. This is to ensure that we paint all pixels correctly. | |
103 Rect scaled_enclosing_rect = | |
104 ToEnclosingRect(RectF(ScalePoint(bounds.origin(), canvas->image_scale()), | |
105 ScaleSize(bounds.size(), canvas->image_scale()))); | |
106 | |
88 // In case the corners and edges don't all have the same width/height, we draw | 107 // In case the corners and edges don't all have the same width/height, we draw |
89 // the center first, and extend it out in all directions to the edges of the | 108 // the center first, and extend it out in all directions to the edges of the |
90 // images with the smallest widths/heights. This way there will be no | 109 // images with the smallest widths/heights. This way there will be no |
91 // unpainted areas, though some corners or edges might overlap the center. | 110 // unpainted areas, though some corners or edges might overlap the center. |
92 int w = bounds.width(); | 111 int w = scaled_enclosing_rect.width(); |
93 int i0w = images_[0].width(); | 112 int i0w = ImageWidthInPixels(images_[0], canvas); |
94 int i2w = images_[2].width(); | 113 int i2w = ImageWidthInPixels(images_[2], canvas); |
95 int i3w = images_[3].width(); | 114 int i3w = ImageWidthInPixels(images_[3], canvas); |
96 int i5w = images_[5].width(); | 115 int i5w = ImageWidthInPixels(images_[5], canvas); |
97 int i6w = images_[6].width(); | 116 int i6w = ImageWidthInPixels(images_[6], canvas); |
98 int i8w = images_[8].width(); | 117 int i8w = ImageWidthInPixels(images_[8], canvas); |
118 | |
99 int i4x = std::min(std::min(i0w, i3w), i6w); | 119 int i4x = std::min(std::min(i0w, i3w), i6w); |
100 int i4w = w - i4x - std::min(std::min(i2w, i5w), i8w); | 120 int i4w = w - i4x - std::min(std::min(i2w, i5w), i8w); |
101 int h = bounds.height(); | 121 int h = scaled_enclosing_rect.height(); |
102 int i0h = images_[0].height(); | 122 |
103 int i1h = images_[1].height(); | 123 int i0h = ImageHeightInPixels(images_[0], canvas); |
104 int i2h = images_[2].height(); | 124 int i1h = ImageHeightInPixels(images_[1], canvas); |
105 int i6h = images_[6].height(); | 125 int i2h = ImageHeightInPixels(images_[2], canvas); |
106 int i7h = images_[7].height(); | 126 int i6h = ImageHeightInPixels(images_[6], canvas); |
107 int i8h = images_[8].height(); | 127 int i7h = ImageHeightInPixels(images_[7], canvas); |
128 int i8h = ImageHeightInPixels(images_[8], canvas); | |
129 | |
108 int i4y = std::min(std::min(i0h, i1h), i2h); | 130 int i4y = std::min(std::min(i0h, i1h), i2h); |
109 int i4h = h - i4y - std::min(std::min(i6h, i7h), i8h); | 131 int i4h = h - i4y - std::min(std::min(i6h, i7h), i8h); |
110 if (!images_[4].isNull()) | 132 if (!images_[4].isNull()) |
111 Fill(canvas, images_[4], i4x, i4y, i4w, i4h, paint); | 133 Fill(canvas, images_[4], i4x, i4y, i4w, i4h, paint); |
112 canvas->DrawImageInt(images_[0], 0, 0, paint); | 134 canvas->DrawImageIntInPixel(images_[0], 0, 0, i0w, i0h, |
135 0, 0, i0w, i0h, false, paint); | |
113 Fill(canvas, images_[1], i0w, 0, w - i0w - i2w, i1h, paint); | 136 Fill(canvas, images_[1], i0w, 0, w - i0w - i2w, i1h, paint); |
114 canvas->DrawImageInt(images_[2], w - i2w, 0, paint); | 137 canvas->DrawImageIntInPixel(images_[2], 0, 0, i2w, i2h, w - i2w, 0, |
138 i2w, i2h, false, paint); | |
115 Fill(canvas, images_[3], 0, i0h, i3w, h - i0h - i6h, paint); | 139 Fill(canvas, images_[3], 0, i0h, i3w, h - i0h - i6h, paint); |
116 Fill(canvas, images_[5], w - i5w, i2h, i5w, h - i2h - i8h, paint); | 140 Fill(canvas, images_[5], w - i5w, i2h, i5w, h - i2h - i8h, paint); |
117 canvas->DrawImageInt(images_[6], 0, h - i6h, paint); | 141 canvas->DrawImageIntInPixel(images_[6], 0, 0, i6w, i6h, 0, h - i6h, |
142 i6w, i6h, false, paint); | |
118 Fill(canvas, images_[7], i6w, h - i7h, w - i6w - i8w, i7h, paint); | 143 Fill(canvas, images_[7], i6w, h - i7h, w - i6w - i8w, i7h, paint); |
119 canvas->DrawImageInt(images_[8], w - i8w, h - i8h, paint); | 144 canvas->DrawImageIntInPixel(images_[8], 0, 0, i8w, i8h, w - i8w, h - i8h, |
145 i8w, i8h, false, paint); | |
120 } | 146 } |
121 | 147 |
122 } // namespace gfx | 148 } // namespace gfx |
OLD | NEW |