Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: ui/gfx/nine_image_painter_unittest.cc

Issue 2682123003: Remove uses of skia::ReadPixels(PaintCanvas) (Closed)
Patch Set: more readpixels Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/color_utils_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "base/base64.h" 7 #include "base/base64.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/canvas.h" 9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/codec/png_codec.h" 10 #include "ui/gfx/codec/png_codec.h"
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 78
79 bool is_opaque = true; 79 bool is_opaque = true;
80 gfx::Canvas canvas(gfx::Size(100, 100), image_scale, is_opaque); 80 gfx::Canvas canvas(gfx::Size(100, 100), image_scale, is_opaque);
81 81
82 gfx::Vector2d offset(20, 10); 82 gfx::Vector2d offset(20, 10);
83 canvas.Translate(offset); 83 canvas.Translate(offset);
84 84
85 gfx::Rect bounds(0, 0, 50, 50); 85 gfx::Rect bounds(0, 0, 50, 50);
86 painter.Paint(&canvas, bounds); 86 painter.Paint(&canvas, bounds);
87 87
88 SkBitmap result; 88 SkBitmap result = canvas.ToBitmap();
89 const SkISize size = canvas.sk_canvas()->getBaseLayerSize();
90 result.allocN32Pixels(size.width(), size.height());
91 canvas.sk_canvas()->readPixels(&result, 0, 0);
92 89
93 gfx::Vector2d paint_offset = 90 gfx::Vector2d paint_offset =
94 gfx::ToFlooredVector2d(gfx::ScaleVector2d(offset, image_scale)); 91 gfx::ToFlooredVector2d(gfx::ScaleVector2d(offset, image_scale));
95 gfx::Rect green_rect = gfx::Rect(10, 10, 80, 80) + paint_offset; 92 gfx::Rect green_rect = gfx::Rect(10, 10, 80, 80) + paint_offset;
96 gfx::Rect outer_rect = gfx::Rect(100, 100) + paint_offset; 93 gfx::Rect outer_rect = gfx::Rect(100, 100) + paint_offset;
97 ExpectRedWithGreenRect(result, outer_rect, green_rect); 94 ExpectRedWithGreenRect(result, outer_rect, green_rect);
98 } 95 }
99 96
100 TEST(NineImagePainterTest, PaintStaysInBounds) { 97 TEST(NineImagePainterTest, PaintStaysInBounds) {
101 // In this test the bounds rect is 1x1 but each image is 2x2. 98 // In this test the bounds rect is 1x1 but each image is 2x2.
(...skipping 10 matching lines...) Expand all
112 gfx::NineImagePainter painter(image, insets); 109 gfx::NineImagePainter painter(image, insets);
113 110
114 int image_scale = 1; 111 int image_scale = 1;
115 bool is_opaque = true; 112 bool is_opaque = true;
116 gfx::Canvas canvas(gfx::Size(3, 3), image_scale, is_opaque); 113 gfx::Canvas canvas(gfx::Size(3, 3), image_scale, is_opaque);
117 canvas.DrawColor(SK_ColorBLACK); 114 canvas.DrawColor(SK_ColorBLACK);
118 115
119 gfx::Rect bounds(1, 1, 1, 1); 116 gfx::Rect bounds(1, 1, 1, 1);
120 painter.Paint(&canvas, bounds); 117 painter.Paint(&canvas, bounds);
121 118
122 SkBitmap result; 119 SkBitmap result = canvas.ToBitmap();
123 const SkISize size = canvas.sk_canvas()->getBaseLayerSize();
124 result.allocN32Pixels(size.width(), size.height());
125 canvas.sk_canvas()->readPixels(&result, 0, 0);
126 120
127 EXPECT_EQ(SK_ColorGREEN, result.getColor(1, 1)); 121 EXPECT_EQ(SK_ColorGREEN, result.getColor(1, 1));
128 122
129 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 0)); 123 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 0));
130 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 1)); 124 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 1));
131 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 2)); 125 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 2));
132 EXPECT_EQ(SK_ColorBLACK, result.getColor(1, 0)); 126 EXPECT_EQ(SK_ColorBLACK, result.getColor(1, 0));
133 EXPECT_EQ(SK_ColorBLACK, result.getColor(1, 2)); 127 EXPECT_EQ(SK_ColorBLACK, result.getColor(1, 2));
134 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 0)); 128 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 0));
135 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 1)); 129 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 1));
136 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 2)); 130 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 2));
137 } 131 }
138 132
139 TEST(NineImagePainterTest, PaintWithBoundOffset) { 133 TEST(NineImagePainterTest, PaintWithBoundOffset) {
140 SkBitmap src; 134 SkBitmap src;
141 src.allocN32Pixels(10, 10); 135 src.allocN32Pixels(10, 10);
142 src.eraseColor(SK_ColorRED); 136 src.eraseColor(SK_ColorRED);
143 src.eraseArea(SkIRect::MakeXYWH(1, 1, 8, 8), SK_ColorGREEN); 137 src.eraseArea(SkIRect::MakeXYWH(1, 1, 8, 8), SK_ColorGREEN);
144 138
145 gfx::ImageSkia image(gfx::ImageSkiaRep(src, 0.0f)); 139 gfx::ImageSkia image(gfx::ImageSkiaRep(src, 0.0f));
146 gfx::Insets insets(1, 1, 1, 1); 140 gfx::Insets insets(1, 1, 1, 1);
147 gfx::NineImagePainter painter(image, insets); 141 gfx::NineImagePainter painter(image, insets);
148 142
149 bool is_opaque = true; 143 bool is_opaque = true;
150 gfx::Canvas canvas(gfx::Size(10, 10), 1, is_opaque); 144 gfx::Canvas canvas(gfx::Size(10, 10), 1, is_opaque);
151 145
152 gfx::Rect bounds(1, 1, 10, 10); 146 gfx::Rect bounds(1, 1, 10, 10);
153 painter.Paint(&canvas, bounds); 147 painter.Paint(&canvas, bounds);
154 148
155 SkBitmap result; 149 SkBitmap result = canvas.ToBitmap();
156 const SkISize size = canvas.sk_canvas()->getBaseLayerSize();
157 result.allocN32Pixels(size.width(), size.height());
158 canvas.sk_canvas()->readPixels(&result, 0, 0);
159 150
160 SkIRect green_rect = SkIRect::MakeLTRB(2, 2, 10, 10); 151 SkIRect green_rect = SkIRect::MakeLTRB(2, 2, 10, 10);
161 for (int y = 1; y < 10; y++) { 152 for (int y = 1; y < 10; y++) {
162 for (int x = 1; x < 10; x++) { 153 for (int x = 1; x < 10; x++) {
163 if (green_rect.contains(x, y)) { 154 if (green_rect.contains(x, y)) {
164 ASSERT_EQ(SK_ColorGREEN, result.getColor(x, y)); 155 ASSERT_EQ(SK_ColorGREEN, result.getColor(x, y));
165 } else { 156 } else {
166 ASSERT_EQ(SK_ColorRED, result.getColor(x, y)); 157 ASSERT_EQ(SK_ColorRED, result.getColor(x, y));
167 } 158 }
168 } 159 }
(...skipping 15 matching lines...) Expand all
184 bool is_opaque = true; 175 bool is_opaque = true;
185 gfx::Canvas canvas(gfx::Size(400, 400), image_scale, is_opaque); 176 gfx::Canvas canvas(gfx::Size(400, 400), image_scale, is_opaque);
186 177
187 gfx::Vector2d offset(20, 10); 178 gfx::Vector2d offset(20, 10);
188 canvas.Translate(offset); 179 canvas.Translate(offset);
189 canvas.Scale(2, 1); 180 canvas.Scale(2, 1);
190 181
191 gfx::Rect bounds(0, 0, 50, 50); 182 gfx::Rect bounds(0, 0, 50, 50);
192 painter.Paint(&canvas, bounds); 183 painter.Paint(&canvas, bounds);
193 184
194 SkBitmap result; 185 SkBitmap result = canvas.ToBitmap();
195 const SkISize size = canvas.sk_canvas()->getBaseLayerSize();
196 result.allocN32Pixels(size.width(), size.height());
197 canvas.sk_canvas()->readPixels(&result, 0, 0);
198 186
199 gfx::Vector2d paint_offset = 187 gfx::Vector2d paint_offset =
200 gfx::ToFlooredVector2d(gfx::ScaleVector2d(offset, image_scale)); 188 gfx::ToFlooredVector2d(gfx::ScaleVector2d(offset, image_scale));
201 gfx::Rect green_rect = gfx::Rect(20, 10, 160, 80) + paint_offset; 189 gfx::Rect green_rect = gfx::Rect(20, 10, 160, 80) + paint_offset;
202 gfx::Rect outer_rect = gfx::Rect(200, 100) + paint_offset; 190 gfx::Rect outer_rect = gfx::Rect(200, 100) + paint_offset;
203 ExpectRedWithGreenRect(result, outer_rect, green_rect); 191 ExpectRedWithGreenRect(result, outer_rect, green_rect);
204 } 192 }
205 193
206 TEST(NineImagePainterTest, PaintWithNegativeScale) { 194 TEST(NineImagePainterTest, PaintWithNegativeScale) {
207 SkBitmap src; 195 SkBitmap src;
208 src.allocN32Pixels(100, 100); 196 src.allocN32Pixels(100, 100);
209 src.eraseColor(SK_ColorRED); 197 src.eraseColor(SK_ColorRED);
210 src.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN); 198 src.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN);
211 199
212 float image_scale = 2.f; 200 float image_scale = 2.f;
213 201
214 gfx::ImageSkia image(gfx::ImageSkiaRep(src, image_scale)); 202 gfx::ImageSkia image(gfx::ImageSkiaRep(src, image_scale));
215 gfx::Insets insets(10, 10, 10, 10); 203 gfx::Insets insets(10, 10, 10, 10);
216 gfx::NineImagePainter painter(image, insets); 204 gfx::NineImagePainter painter(image, insets);
217 205
218 bool is_opaque = true; 206 bool is_opaque = true;
219 gfx::Canvas canvas(gfx::Size(400, 400), image_scale, is_opaque); 207 gfx::Canvas canvas(gfx::Size(400, 400), image_scale, is_opaque);
220 canvas.Translate(gfx::Vector2d(70, 60)); 208 canvas.Translate(gfx::Vector2d(70, 60));
221 canvas.Scale(-1, -1); 209 canvas.Scale(-1, -1);
222 210
223 gfx::Rect bounds(0, 0, 50, 50); 211 gfx::Rect bounds(0, 0, 50, 50);
224 painter.Paint(&canvas, bounds); 212 painter.Paint(&canvas, bounds);
225 213
226 SkBitmap result; 214 SkBitmap result = canvas.ToBitmap();
227 const SkISize size = canvas.sk_canvas()->getBaseLayerSize();
228 result.allocN32Pixels(size.width(), size.height());
229 canvas.sk_canvas()->readPixels(&result, 0, 0);
230 215
231 // The painting space is 50x50 and the scale of -1,-1 means an offset of 50,50 216 // The painting space is 50x50 and the scale of -1,-1 means an offset of 50,50
232 // would put the output in the top left corner. Since the offset is 70,60 it 217 // would put the output in the top left corner. Since the offset is 70,60 it
233 // moves by 20,10. Since the output is 2x DPI it will become offset by 40,20. 218 // moves by 20,10. Since the output is 2x DPI it will become offset by 40,20.
234 gfx::Vector2d paint_offset(40, 20); 219 gfx::Vector2d paint_offset(40, 20);
235 gfx::Rect green_rect = gfx::Rect(10, 10, 80, 80) + paint_offset; 220 gfx::Rect green_rect = gfx::Rect(10, 10, 80, 80) + paint_offset;
236 gfx::Rect outer_rect = gfx::Rect(100, 100) + paint_offset; 221 gfx::Rect outer_rect = gfx::Rect(100, 100) + paint_offset;
237 ExpectRedWithGreenRect(result, outer_rect, green_rect); 222 ExpectRedWithGreenRect(result, outer_rect, green_rect);
238 } 223 }
239 224
240 } // namespace gfx 225 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/color_utils_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698