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" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 91 |
92 void NineImagePainter::Paint(Canvas* canvas, | 92 void NineImagePainter::Paint(Canvas* canvas, |
93 const Rect& bounds, | 93 const Rect& bounds, |
94 const uint8 alpha) { | 94 const uint8 alpha) { |
95 if (IsEmpty()) | 95 if (IsEmpty()) |
96 return; | 96 return; |
97 | 97 |
98 ScopedCanvas scoped_canvas(canvas); | 98 ScopedCanvas scoped_canvas(canvas); |
99 canvas->Translate(bounds.OffsetFromOrigin()); | 99 canvas->Translate(bounds.OffsetFromOrigin()); |
100 | 100 |
| 101 SkPaint paint; |
| 102 paint.setAlpha(alpha); |
| 103 |
101 // Get the current transform from the canvas and apply it to the logical | 104 // Get the current transform from the canvas and apply it to the logical |
102 // bounds passed in. This will give us the pixel bounds which can be used | 105 // bounds passed in. This will give us the pixel bounds which can be used |
103 // to draw the images at the correct locations. | 106 // to draw the images at the correct locations. |
104 // We should not scale the bounds by the canvas->image_scale() as that can be | 107 // We should not scale the bounds by the canvas->image_scale() as that can be |
105 // different from the real scale in the canvas transform. | 108 // different from the real scale in the canvas transform. |
106 SkRect bounds_in_pixels; | 109 SkMatrix matrix = canvas->sk_canvas()->getTotalMatrix(); |
107 if (!canvas->sk_canvas()->getTotalMatrix().mapRect(&bounds_in_pixels, | 110 SkRect scaled_rect; |
108 RectToSkRect(bounds))) | 111 matrix.mapRect(&scaled_rect, RectToSkRect(bounds)); |
109 return; // Invalid transform. | |
110 | 112 |
111 // It's often the case that |bounds| corresponds to the bounds of a View, say | 113 int scaled_width = gfx::ToCeiledInt(SkScalarToFloat(scaled_rect.width())); |
112 // a button. We use enclosed rect, in pixels coordinates, to ensure the | 114 int scaled_height = gfx::ToCeiledInt(SkScalarToFloat(scaled_rect.height())); |
113 // complete image can be seen. Without this clipping on all sides can happen. | |
114 const gfx::Rect enclosed_rect_in_pixels( | |
115 ToEnclosedRect(SkRectToRectF(bounds_in_pixels))); | |
116 | |
117 SkMatrix matrix = canvas->sk_canvas()->getTotalMatrix(); | |
118 matrix.setTranslateX(SkIntToScalar(enclosed_rect_in_pixels.x())); | |
119 matrix.setTranslateY(SkIntToScalar(enclosed_rect_in_pixels.y())); | |
120 canvas->sk_canvas()->setMatrix(matrix); | |
121 | |
122 const int width_in_pixels = enclosed_rect_in_pixels.width(); | |
123 const int height_in_pixels = enclosed_rect_in_pixels.height(); | |
124 | 115 |
125 // In case the corners and edges don't all have the same width/height, we draw | 116 // In case the corners and edges don't all have the same width/height, we draw |
126 // the center first, and extend it out in all directions to the edges of the | 117 // the center first, and extend it out in all directions to the edges of the |
127 // images with the smallest widths/heights. This way there will be no | 118 // images with the smallest widths/heights. This way there will be no |
128 // unpainted areas, though some corners or edges might overlap the center. | 119 // unpainted areas, though some corners or edges might overlap the center. |
129 int i0w = ImageWidthInPixels(images_[0], canvas); | 120 int i0w = ImageWidthInPixels(images_[0], canvas); |
130 int i2w = ImageWidthInPixels(images_[2], canvas); | 121 int i2w = ImageWidthInPixels(images_[2], canvas); |
131 int i3w = ImageWidthInPixels(images_[3], canvas); | 122 int i3w = ImageWidthInPixels(images_[3], canvas); |
132 int i5w = ImageWidthInPixels(images_[5], canvas); | 123 int i5w = ImageWidthInPixels(images_[5], canvas); |
133 int i6w = ImageWidthInPixels(images_[6], canvas); | 124 int i6w = ImageWidthInPixels(images_[6], canvas); |
134 int i8w = ImageWidthInPixels(images_[8], canvas); | 125 int i8w = ImageWidthInPixels(images_[8], canvas); |
135 | 126 |
136 int i4x = std::min(std::min(i0w, i3w), i6w); | 127 int i4x = std::min(std::min(i0w, i3w), i6w); |
137 int i4w = width_in_pixels - i4x - std::min(std::min(i2w, i5w), i8w); | 128 int i4w = scaled_width - i4x - std::min(std::min(i2w, i5w), i8w); |
138 | 129 |
139 int i0h = ImageHeightInPixels(images_[0], canvas); | 130 int i0h = ImageHeightInPixels(images_[0], canvas); |
140 int i1h = ImageHeightInPixels(images_[1], canvas); | 131 int i1h = ImageHeightInPixels(images_[1], canvas); |
141 int i2h = ImageHeightInPixels(images_[2], canvas); | 132 int i2h = ImageHeightInPixels(images_[2], canvas); |
142 int i6h = ImageHeightInPixels(images_[6], canvas); | 133 int i6h = ImageHeightInPixels(images_[6], canvas); |
143 int i7h = ImageHeightInPixels(images_[7], canvas); | 134 int i7h = ImageHeightInPixels(images_[7], canvas); |
144 int i8h = ImageHeightInPixels(images_[8], canvas); | 135 int i8h = ImageHeightInPixels(images_[8], canvas); |
145 | 136 |
146 int i4y = std::min(std::min(i0h, i1h), i2h); | 137 int i4y = std::min(std::min(i0h, i1h), i2h); |
147 int i4h = height_in_pixels - i4y - std::min(std::min(i6h, i7h), i8h); | 138 int i4h = scaled_height - i4y - std::min(std::min(i6h, i7h), i8h); |
148 | |
149 SkPaint paint; | |
150 paint.setAlpha(alpha); | |
151 | |
152 if (!images_[4].isNull()) | 139 if (!images_[4].isNull()) |
153 Fill(canvas, images_[4], i4x, i4y, i4w, i4h, paint); | 140 Fill(canvas, images_[4], i4x, i4y, i4w, i4h, paint); |
154 canvas->DrawImageIntInPixel(images_[0], 0, 0, i0w, i0h, | 141 canvas->DrawImageIntInPixel(images_[0], 0, 0, i0w, i0h, |
155 0, 0, i0w, i0h, false, paint); | 142 0, 0, i0w, i0h, false, paint); |
156 Fill(canvas, images_[1], i0w, 0, width_in_pixels - i0w - i2w, i1h, paint); | 143 Fill(canvas, images_[1], i0w, 0, scaled_width - i0w - i2w, i1h, paint); |
157 canvas->DrawImageIntInPixel(images_[2], 0, 0, i2w, i2h, width_in_pixels - i2w, | 144 canvas->DrawImageIntInPixel(images_[2], 0, 0, i2w, i2h, scaled_width - i2w, |
158 0, i2w, i2h, false, paint); | 145 0, i2w, i2h, false, paint); |
159 Fill(canvas, images_[3], 0, i0h, i3w, height_in_pixels - i0h - i6h, paint); | 146 Fill(canvas, images_[3], 0, i0h, i3w, scaled_height - i0h - i6h, paint); |
160 Fill(canvas, images_[5], width_in_pixels - i5w, i2h, i5w, | 147 Fill(canvas, images_[5], scaled_width - i5w, i2h, i5w, |
161 height_in_pixels - i2h - i8h, paint); | 148 scaled_height - i2h - i8h, paint); |
162 canvas->DrawImageIntInPixel(images_[6], 0, 0, i6w, i6h, 0, | 149 canvas->DrawImageIntInPixel(images_[6], 0, 0, i6w, i6h, 0, |
163 height_in_pixels - i6h, i6w, i6h, false, paint); | 150 scaled_height - i6h, i6w, i6h, false, paint); |
164 Fill(canvas, images_[7], i6w, height_in_pixels - i7h, | 151 Fill(canvas, images_[7], i6w, scaled_height - i7h, scaled_width - i6w - i8w, |
165 width_in_pixels - i6w - i8w, i7h, paint); | 152 i7h, paint); |
166 canvas->DrawImageIntInPixel(images_[8], 0, 0, i8w, i8h, width_in_pixels - i8w, | 153 canvas->DrawImageIntInPixel(images_[8], 0, 0, i8w, i8h, scaled_width - i8w, |
167 height_in_pixels - i8h, i8w, i8h, false, paint); | 154 scaled_height - i8h, i8w, i8h, false, paint); |
168 } | 155 } |
169 | 156 |
170 } // namespace gfx | 157 } // namespace gfx |
OLD | NEW |