OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // TODO(awalker): clean up the const/non-const reference handling in this test | |
6 | |
7 #include "skia/ext/platform_canvas.h" | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "build/build_config.h" | |
12 #include "skia/ext/platform_device.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "third_party/skia/include/core/SkBitmap.h" | |
15 #include "third_party/skia/include/core/SkColor.h" | |
16 #include "third_party/skia/include/core/SkColorPriv.h" | |
17 #include "third_party/skia/include/core/SkPixelRef.h" | |
18 | |
19 #if defined(OS_MACOSX) | |
20 #import <ApplicationServices/ApplicationServices.h> | |
21 #endif | |
22 | |
23 #if !defined(OS_WIN) | |
24 #include <unistd.h> | |
25 #endif | |
26 | |
27 namespace skia { | |
28 | |
29 namespace { | |
30 | |
31 bool IsOfColor(const SkBitmap& bitmap, int x, int y, uint32_t color) { | |
32 // For masking out the alpha values. | |
33 static uint32_t alpha_mask = | |
34 static_cast<uint32_t>(SK_A32_MASK) << SK_A32_SHIFT; | |
35 return (*bitmap.getAddr32(x, y) | alpha_mask) == (color | alpha_mask); | |
36 } | |
37 | |
38 // Return true if the canvas is filled to canvas_color, and contains a single | |
39 // rectangle filled to rect_color. This function ignores the alpha channel, | |
40 // since Windows will sometimes clear the alpha channel when drawing, and we | |
41 // will fix that up later in cases it's necessary. | |
42 bool VerifyRect(const PlatformCanvas& canvas, | |
43 uint32_t canvas_color, uint32_t rect_color, | |
44 int x, int y, int w, int h) { | |
45 SkBaseDevice* device = skia::GetTopDevice(canvas); | |
46 const SkBitmap& bitmap = device->accessBitmap(false); | |
47 SkAutoLockPixels lock(bitmap); | |
48 | |
49 for (int cur_y = 0; cur_y < bitmap.height(); cur_y++) { | |
50 for (int cur_x = 0; cur_x < bitmap.width(); cur_x++) { | |
51 if (cur_x >= x && cur_x < x + w && | |
52 cur_y >= y && cur_y < y + h) { | |
53 // Inside the square should be rect_color | |
54 if (!IsOfColor(bitmap, cur_x, cur_y, rect_color)) | |
55 return false; | |
56 } else { | |
57 // Outside the square should be canvas_color | |
58 if (!IsOfColor(bitmap, cur_x, cur_y, canvas_color)) | |
59 return false; | |
60 } | |
61 } | |
62 } | |
63 return true; | |
64 } | |
65 | |
66 #if !defined(OS_MACOSX) | |
67 // Return true if canvas has something that passes for a rounded-corner | |
68 // rectangle. Basically, we're just checking to make sure that the pixels in the | |
69 // middle are of rect_color and pixels in the corners are of canvas_color. | |
70 bool VerifyRoundedRect(const PlatformCanvas& canvas, | |
71 uint32_t canvas_color, | |
72 uint32_t rect_color, | |
73 int x, | |
74 int y, | |
75 int w, | |
76 int h) { | |
77 SkBaseDevice* device = skia::GetTopDevice(canvas); | |
78 const SkBitmap& bitmap = device->accessBitmap(false); | |
79 SkAutoLockPixels lock(bitmap); | |
80 | |
81 // Check corner points first. They should be of canvas_color. | |
82 if (!IsOfColor(bitmap, x, y, canvas_color)) return false; | |
83 if (!IsOfColor(bitmap, x + w, y, canvas_color)) return false; | |
84 if (!IsOfColor(bitmap, x, y + h, canvas_color)) return false; | |
85 if (!IsOfColor(bitmap, x + w, y, canvas_color)) return false; | |
86 | |
87 // Check middle points. They should be of rect_color. | |
88 if (!IsOfColor(bitmap, (x + w / 2), y, rect_color)) return false; | |
89 if (!IsOfColor(bitmap, x, (y + h / 2), rect_color)) return false; | |
90 if (!IsOfColor(bitmap, x + w, (y + h / 2), rect_color)) return false; | |
91 if (!IsOfColor(bitmap, (x + w / 2), y + h, rect_color)) return false; | |
92 | |
93 return true; | |
94 } | |
95 #endif | |
96 | |
97 // Checks whether there is a white canvas with a black square at the given | |
98 // location in pixels (not in the canvas coordinate system). | |
99 bool VerifyBlackRect(const PlatformCanvas& canvas, int x, int y, int w, int h) { | |
100 return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h); | |
101 } | |
102 | |
103 // Check that every pixel in the canvas is a single color. | |
104 bool VerifyCanvasColor(const PlatformCanvas& canvas, uint32_t canvas_color) { | |
105 return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0); | |
106 } | |
107 | |
108 #if defined(OS_WIN) | |
109 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) { | |
110 skia::ScopedPlatformPaint scoped_platform_paint(&canvas); | |
111 HDC dc = scoped_platform_paint.GetPlatformSurface(); | |
112 | |
113 RECT inner_rc; | |
114 inner_rc.left = x; | |
115 inner_rc.top = y; | |
116 inner_rc.right = x + w; | |
117 inner_rc.bottom = y + h; | |
118 FillRect(dc, &inner_rc, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)))
; | |
119 } | |
120 #elif defined(OS_MACOSX) | |
121 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) { | |
122 skia::ScopedPlatformPaint scoped_platform_paint(&canvas); | |
123 CGContextRef context = scoped_platform_paint.GetPlatformSurface(); | |
124 | |
125 CGRect inner_rc = CGRectMake(x, y, w, h); | |
126 // RGBA opaque black | |
127 CGColorRef black = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0); | |
128 CGContextSetFillColorWithColor(context, black); | |
129 CGColorRelease(black); | |
130 CGContextFillRect(context, inner_rc); | |
131 } | |
132 #else | |
133 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) { | |
134 NOTIMPLEMENTED(); | |
135 } | |
136 #endif | |
137 | |
138 // Clips the contents of the canvas to the given rectangle. This will be | |
139 // intersected with any existing clip. | |
140 void AddClip(PlatformCanvas& canvas, int x, int y, int w, int h) { | |
141 SkRect rect; | |
142 rect.set(SkIntToScalar(x), SkIntToScalar(y), | |
143 SkIntToScalar(x + w), SkIntToScalar(y + h)); | |
144 canvas.clipRect(rect); | |
145 } | |
146 | |
147 class LayerSaver { | |
148 public: | |
149 LayerSaver(PlatformCanvas& canvas, int x, int y, int w, int h) | |
150 : canvas_(canvas), | |
151 x_(x), | |
152 y_(y), | |
153 w_(w), | |
154 h_(h) { | |
155 SkRect bounds; | |
156 bounds.set(SkIntToScalar(x_), SkIntToScalar(y_), | |
157 SkIntToScalar(right()), SkIntToScalar(bottom())); | |
158 canvas_.saveLayer(&bounds, NULL); | |
159 canvas.clear(SkColorSetARGB(0, 0, 0, 0)); | |
160 } | |
161 | |
162 ~LayerSaver() { | |
163 canvas_.restore(); | |
164 } | |
165 | |
166 int x() const { return x_; } | |
167 int y() const { return y_; } | |
168 int w() const { return w_; } | |
169 int h() const { return h_; } | |
170 | |
171 // Returns the EXCLUSIVE far bounds of the layer. | |
172 int right() const { return x_ + w_; } | |
173 int bottom() const { return y_ + h_; } | |
174 | |
175 private: | |
176 PlatformCanvas& canvas_; | |
177 int x_, y_, w_, h_; | |
178 }; | |
179 | |
180 // Size used for making layers in many of the below tests. | |
181 const int kLayerX = 2; | |
182 const int kLayerY = 3; | |
183 const int kLayerW = 9; | |
184 const int kLayerH = 7; | |
185 | |
186 // Size used by some tests to draw a rectangle inside the layer. | |
187 const int kInnerX = 4; | |
188 const int kInnerY = 5; | |
189 const int kInnerW = 2; | |
190 const int kInnerH = 3; | |
191 | |
192 } | |
193 | |
194 // This just checks that our checking code is working properly, it just uses | |
195 // regular skia primitives. | |
196 TEST(PlatformCanvas, SkLayer) { | |
197 // Create the canvas initialized to opaque white. | |
198 RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true)); | |
199 canvas->drawColor(SK_ColorWHITE); | |
200 | |
201 // Make a layer and fill it completely to make sure that the bounds are | |
202 // correct. | |
203 { | |
204 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); | |
205 canvas->drawColor(SK_ColorBLACK); | |
206 } | |
207 EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH)); | |
208 } | |
209 | |
210 // Test native clipping. | |
211 TEST(PlatformCanvas, ClipRegion) { | |
212 // Initialize a white canvas | |
213 RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true)); | |
214 canvas->drawColor(SK_ColorWHITE); | |
215 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE)); | |
216 | |
217 // Test that initially the canvas has no clip region, by filling it | |
218 // with a black rectangle. | |
219 // Note: Don't use LayerSaver, since internally it sets a clip region. | |
220 DrawNativeRect(*canvas, 0, 0, 16, 16); | |
221 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorBLACK)); | |
222 | |
223 // Test that intersecting disjoint clip rectangles sets an empty clip region | |
224 canvas->drawColor(SK_ColorWHITE); | |
225 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE)); | |
226 { | |
227 LayerSaver layer(*canvas, 0, 0, 16, 16); | |
228 AddClip(*canvas, 2, 3, 4, 5); | |
229 AddClip(*canvas, 4, 9, 10, 10); | |
230 DrawNativeRect(*canvas, 0, 0, 16, 16); | |
231 } | |
232 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE)); | |
233 } | |
234 | |
235 // Test the layers get filled properly by native rendering. | |
236 TEST(PlatformCanvas, FillLayer) { | |
237 // Create the canvas initialized to opaque white. | |
238 RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true)); | |
239 | |
240 // Make a layer and fill it completely to make sure that the bounds are | |
241 // correct. | |
242 canvas->drawColor(SK_ColorWHITE); | |
243 { | |
244 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); | |
245 DrawNativeRect(*canvas, 0, 0, 100, 100); | |
246 #if defined(OS_WIN) | |
247 MakeOpaque(canvas.get(), 0, 0, 100, 100); | |
248 #endif | |
249 } | |
250 EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH)); | |
251 | |
252 // Make a layer and fill it partially to make sure the translation is correct. | |
253 canvas->drawColor(SK_ColorWHITE); | |
254 { | |
255 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); | |
256 DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH); | |
257 #if defined(OS_WIN) | |
258 MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH); | |
259 #endif | |
260 } | |
261 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH)); | |
262 | |
263 // Add a clip on the layer and fill to make sure clip is correct. | |
264 canvas->drawColor(SK_ColorWHITE); | |
265 { | |
266 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); | |
267 canvas->save(); | |
268 AddClip(*canvas, kInnerX, kInnerY, kInnerW, kInnerH); | |
269 DrawNativeRect(*canvas, 0, 0, 100, 100); | |
270 #if defined(OS_WIN) | |
271 MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH); | |
272 #endif | |
273 canvas->restore(); | |
274 } | |
275 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH)); | |
276 | |
277 // Add a clip and then make the layer to make sure the clip is correct. | |
278 canvas->drawColor(SK_ColorWHITE); | |
279 canvas->save(); | |
280 AddClip(*canvas, kInnerX, kInnerY, kInnerW, kInnerH); | |
281 { | |
282 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); | |
283 DrawNativeRect(*canvas, 0, 0, 100, 100); | |
284 #if defined(OS_WIN) | |
285 MakeOpaque(canvas.get(), 0, 0, 100, 100); | |
286 #endif | |
287 } | |
288 canvas->restore(); | |
289 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH)); | |
290 } | |
291 | |
292 // Test that translation + make layer works properly. | |
293 TEST(PlatformCanvas, TranslateLayer) { | |
294 // Create the canvas initialized to opaque white. | |
295 RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true)); | |
296 | |
297 // Make a layer and fill it completely to make sure that the bounds are | |
298 // correct. | |
299 canvas->drawColor(SK_ColorWHITE); | |
300 canvas->save(); | |
301 canvas->translate(1, 1); | |
302 { | |
303 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); | |
304 DrawNativeRect(*canvas, 0, 0, 100, 100); | |
305 #if defined(OS_WIN) | |
306 MakeOpaque(canvas.get(), 0, 0, 100, 100); | |
307 #endif | |
308 } | |
309 canvas->restore(); | |
310 EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX + 1, kLayerY + 1, | |
311 kLayerW, kLayerH)); | |
312 | |
313 // Translate then make the layer. | |
314 canvas->drawColor(SK_ColorWHITE); | |
315 canvas->save(); | |
316 canvas->translate(1, 1); | |
317 { | |
318 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); | |
319 DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH); | |
320 #if defined(OS_WIN) | |
321 MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH); | |
322 #endif | |
323 } | |
324 canvas->restore(); | |
325 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 1, kInnerY + 1, | |
326 kInnerW, kInnerH)); | |
327 | |
328 // Make the layer then translate. | |
329 canvas->drawColor(SK_ColorWHITE); | |
330 canvas->save(); | |
331 { | |
332 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); | |
333 canvas->translate(1, 1); | |
334 DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH); | |
335 #if defined(OS_WIN) | |
336 MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH); | |
337 #endif | |
338 } | |
339 canvas->restore(); | |
340 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 1, kInnerY + 1, | |
341 kInnerW, kInnerH)); | |
342 | |
343 // Translate both before and after, and have a clip. | |
344 canvas->drawColor(SK_ColorWHITE); | |
345 canvas->save(); | |
346 canvas->translate(1, 1); | |
347 { | |
348 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); | |
349 canvas->drawColor(SK_ColorWHITE); | |
350 canvas->translate(1, 1); | |
351 AddClip(*canvas, kInnerX + 1, kInnerY + 1, kInnerW - 1, kInnerH - 1); | |
352 DrawNativeRect(*canvas, 0, 0, 100, 100); | |
353 #if defined(OS_WIN) | |
354 MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH); | |
355 #endif | |
356 } | |
357 canvas->restore(); | |
358 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 3, kInnerY + 3, | |
359 kInnerW - 1, kInnerH - 1)); | |
360 | |
361 // TODO(dglazkov): Figure out why this fails on Mac (antialiased clipping?), | |
362 // modify test and remove this guard. | |
363 #if !defined(OS_MACOSX) | |
364 // Translate both before and after, and have a path clip. | |
365 canvas->drawColor(SK_ColorWHITE); | |
366 canvas->save(); | |
367 canvas->translate(1, 1); | |
368 { | |
369 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); | |
370 canvas->drawColor(SK_ColorWHITE); | |
371 canvas->translate(1, 1); | |
372 | |
373 SkPath path; | |
374 SkRect rect; | |
375 rect.iset(kInnerX - 1, kInnerY - 1, | |
376 kInnerX + kInnerW, kInnerY + kInnerH); | |
377 const SkScalar kRadius = 2.0; | |
378 path.addRoundRect(rect, kRadius, kRadius); | |
379 canvas->clipPath(path); | |
380 | |
381 DrawNativeRect(*canvas, 0, 0, 100, 100); | |
382 #if defined(OS_WIN) | |
383 MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH); | |
384 #endif | |
385 } | |
386 canvas->restore(); | |
387 EXPECT_TRUE(VerifyRoundedRect(*canvas, SK_ColorWHITE, SK_ColorBLACK, | |
388 kInnerX + 1, kInnerY + 1, kInnerW, kInnerH)); | |
389 #endif | |
390 } | |
391 | |
392 TEST(PlatformBitmapTest, PlatformBitmap) { | |
393 const int kWidth = 400; | |
394 const int kHeight = 300; | |
395 scoped_ptr<PlatformBitmap> platform_bitmap(new PlatformBitmap); | |
396 | |
397 EXPECT_TRUE(0 == platform_bitmap->GetSurface()); | |
398 EXPECT_TRUE(platform_bitmap->GetBitmap().empty()); | |
399 EXPECT_TRUE(platform_bitmap->GetBitmap().isNull()); | |
400 | |
401 EXPECT_TRUE(platform_bitmap->Allocate(kWidth, kHeight, /*is_opaque=*/false)); | |
402 | |
403 EXPECT_TRUE(0 != platform_bitmap->GetSurface()); | |
404 EXPECT_FALSE(platform_bitmap->GetBitmap().empty()); | |
405 EXPECT_FALSE(platform_bitmap->GetBitmap().isNull()); | |
406 EXPECT_EQ(kWidth, platform_bitmap->GetBitmap().width()); | |
407 EXPECT_EQ(kHeight, platform_bitmap->GetBitmap().height()); | |
408 EXPECT_LE(static_cast<size_t>(platform_bitmap->GetBitmap().width()*4), | |
409 platform_bitmap->GetBitmap().rowBytes()); | |
410 EXPECT_EQ(kN32_SkColorType, // Same for all platforms. | |
411 platform_bitmap->GetBitmap().colorType()); | |
412 EXPECT_TRUE(platform_bitmap->GetBitmap().lockPixelsAreWritable()); | |
413 #if defined(SK_DEBUG) | |
414 EXPECT_TRUE(platform_bitmap->GetBitmap().pixelRef()->isLocked()); | |
415 #endif | |
416 EXPECT_TRUE(platform_bitmap->GetBitmap().pixelRef()->unique()); | |
417 | |
418 *(platform_bitmap->GetBitmap().getAddr32(10, 20)) = 0xDEED1020; | |
419 *(platform_bitmap->GetBitmap().getAddr32(20, 30)) = 0xDEED2030; | |
420 | |
421 SkBitmap sk_bitmap = platform_bitmap->GetBitmap(); | |
422 sk_bitmap.lockPixels(); | |
423 | |
424 EXPECT_FALSE(platform_bitmap->GetBitmap().pixelRef()->unique()); | |
425 EXPECT_FALSE(sk_bitmap.pixelRef()->unique()); | |
426 | |
427 EXPECT_EQ(0xDEED1020, *sk_bitmap.getAddr32(10, 20)); | |
428 EXPECT_EQ(0xDEED2030, *sk_bitmap.getAddr32(20, 30)); | |
429 | |
430 *(platform_bitmap->GetBitmap().getAddr32(30, 40)) = 0xDEED3040; | |
431 | |
432 // The SkBitmaps derived from a PlatformBitmap must be capable of outliving | |
433 // the PlatformBitmap. | |
434 platform_bitmap.reset(); | |
435 | |
436 EXPECT_TRUE(sk_bitmap.pixelRef()->unique()); | |
437 | |
438 EXPECT_EQ(0xDEED1020, *sk_bitmap.getAddr32(10, 20)); | |
439 EXPECT_EQ(0xDEED2030, *sk_bitmap.getAddr32(20, 30)); | |
440 EXPECT_EQ(0xDEED3040, *sk_bitmap.getAddr32(30, 40)); | |
441 sk_bitmap.unlockPixels(); | |
442 | |
443 EXPECT_EQ(NULL, sk_bitmap.getPixels()); | |
444 | |
445 sk_bitmap.lockPixels(); | |
446 EXPECT_EQ(0xDEED1020, *sk_bitmap.getAddr32(10, 20)); | |
447 EXPECT_EQ(0xDEED2030, *sk_bitmap.getAddr32(20, 30)); | |
448 EXPECT_EQ(0xDEED3040, *sk_bitmap.getAddr32(30, 40)); | |
449 sk_bitmap.unlockPixels(); | |
450 } | |
451 | |
452 | |
453 } // namespace skia | |
OLD | NEW |