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.
5 // TODO(awalker): clean up the const/non-const reference handling in this test
7 #include "build/build_config.h"
10 #import <ApplicationServices/ApplicationServices.h>
17 #include "base/memory/scoped_ptr.h"
18 #include "skia/ext/platform_canvas.h"
19 #include "skia/ext/platform_device.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
22 #include "third_party/skia/include/core/SkColor.h"
23 #include "third_party/skia/include/core/SkPixelRef.h"
29 // Return true if the canvas is filled to canvas_color, and contains a single
30 // rectangle filled to rect_color. This function ignores the alpha channel,
31 // since Windows will sometimes clear the alpha channel when drawing, and we
32 // will fix that up later in cases it's necessary.
33 bool VerifyRect(const PlatformCanvas
& canvas
,
34 uint32_t canvas_color
, uint32_t rect_color
,
35 int x
, int y
, int w
, int h
) {
36 SkBaseDevice
* device
= skia::GetTopDevice(canvas
);
37 const SkBitmap
& bitmap
= device
->accessBitmap(false);
38 SkAutoLockPixels
lock(bitmap
);
40 // For masking out the alpha values.
41 uint32_t alpha_mask
= 0xFF << SK_A32_SHIFT
;
43 for (int cur_y
= 0; cur_y
< bitmap
.height(); cur_y
++) {
44 for (int cur_x
= 0; cur_x
< bitmap
.width(); cur_x
++) {
45 if (cur_x
>= x
&& cur_x
< x
+ w
&&
46 cur_y
>= y
&& cur_y
< y
+ h
) {
47 // Inside the square should be rect_color
48 if ((*bitmap
.getAddr32(cur_x
, cur_y
) | alpha_mask
) !=
49 (rect_color
| alpha_mask
))
52 // Outside the square should be canvas_color
53 if ((*bitmap
.getAddr32(cur_x
, cur_y
) | alpha_mask
) !=
54 (canvas_color
| alpha_mask
))
62 #if !defined(OS_MACOSX)
63 bool IsOfColor(const SkBitmap
& bitmap
, int x
, int y
, uint32_t color
) {
64 // For masking out the alpha values.
65 static uint32_t alpha_mask
= 0xFF << SK_A32_SHIFT
;
66 return (*bitmap
.getAddr32(x
, y
) | alpha_mask
) == (color
| alpha_mask
);
69 // Return true if canvas has something that passes for a rounded-corner
70 // rectangle. Basically, we're just checking to make sure that the pixels in the
71 // middle are of rect_color and pixels in the corners are of canvas_color.
72 bool VerifyRoundedRect(const PlatformCanvas
& canvas
,
73 uint32_t canvas_color
, uint32_t rect_color
,
74 int x
, int y
, int w
, int h
) {
75 SkBaseDevice
* device
= skia::GetTopDevice(canvas
);
76 const SkBitmap
& bitmap
= device
->accessBitmap(false);
77 SkAutoLockPixels
lock(bitmap
);
79 // Check corner points first. They should be of canvas_color.
80 if (!IsOfColor(bitmap
, x
, y
, canvas_color
)) return false;
81 if (!IsOfColor(bitmap
, x
+ w
, y
, canvas_color
)) return false;
82 if (!IsOfColor(bitmap
, x
, y
+ h
, canvas_color
)) return false;
83 if (!IsOfColor(bitmap
, x
+ w
, y
, canvas_color
)) return false;
85 // Check middle points. They should be of rect_color.
86 if (!IsOfColor(bitmap
, (x
+ w
/ 2), y
, rect_color
)) return false;
87 if (!IsOfColor(bitmap
, x
, (y
+ h
/ 2), rect_color
)) return false;
88 if (!IsOfColor(bitmap
, x
+ w
, (y
+ h
/ 2), rect_color
)) return false;
89 if (!IsOfColor(bitmap
, (x
+ w
/ 2), y
+ h
, rect_color
)) return false;
95 // Checks whether there is a white canvas with a black square at the given
96 // location in pixels (not in the canvas coordinate system).
97 bool VerifyBlackRect(const PlatformCanvas
& canvas
, int x
, int y
, int w
, int h
) {
98 return VerifyRect(canvas
, SK_ColorWHITE
, SK_ColorBLACK
, x
, y
, w
, h
);
101 // Check that every pixel in the canvas is a single color.
102 bool VerifyCanvasColor(const PlatformCanvas
& canvas
, uint32_t canvas_color
) {
103 return VerifyRect(canvas
, canvas_color
, 0, 0, 0, 0, 0);
107 void DrawNativeRect(PlatformCanvas
& canvas
, int x
, int y
, int w
, int h
) {
108 skia::ScopedPlatformPaint
scoped_platform_paint(&canvas
);
109 HDC dc
= scoped_platform_paint
.GetPlatformSurface();
114 inner_rc
.right
= x
+ w
;
115 inner_rc
.bottom
= y
+ h
;
116 FillRect(dc
, &inner_rc
, reinterpret_cast<HBRUSH
>(GetStockObject(BLACK_BRUSH
)));
118 #elif defined(OS_MACOSX)
119 void DrawNativeRect(PlatformCanvas
& canvas
, int x
, int y
, int w
, int h
) {
120 skia::ScopedPlatformPaint
scoped_platform_paint(&canvas
);
121 CGContextRef context
= scoped_platform_paint
.GetPlatformSurface();
123 CGRect inner_rc
= CGRectMake(x
, y
, w
, h
);
125 CGColorRef black
= CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0);
126 CGContextSetFillColorWithColor(context
, black
);
127 CGColorRelease(black
);
128 CGContextFillRect(context
, inner_rc
);
131 void DrawNativeRect(PlatformCanvas
& canvas
, int x
, int y
, int w
, int h
) {
136 // Clips the contents of the canvas to the given rectangle. This will be
137 // intersected with any existing clip.
138 void AddClip(PlatformCanvas
& canvas
, int x
, int y
, int w
, int h
) {
140 rect
.set(SkIntToScalar(x
), SkIntToScalar(y
),
141 SkIntToScalar(x
+ w
), SkIntToScalar(y
+ h
));
142 canvas
.clipRect(rect
);
147 LayerSaver(PlatformCanvas
& canvas
, int x
, int y
, int w
, int h
)
154 bounds
.set(SkIntToScalar(x_
), SkIntToScalar(y_
),
155 SkIntToScalar(right()), SkIntToScalar(bottom()));
156 canvas_
.saveLayer(&bounds
, NULL
);
157 canvas
.clear(SkColorSetARGB(0, 0, 0, 0));
164 int x() const { return x_
; }
165 int y() const { return y_
; }
166 int w() const { return w_
; }
167 int h() const { return h_
; }
169 // Returns the EXCLUSIVE far bounds of the layer.
170 int right() const { return x_
+ w_
; }
171 int bottom() const { return y_
+ h_
; }
174 PlatformCanvas
& canvas_
;
178 // Size used for making layers in many of the below tests.
179 const int kLayerX
= 2;
180 const int kLayerY
= 3;
181 const int kLayerW
= 9;
182 const int kLayerH
= 7;
184 // Size used by some tests to draw a rectangle inside the layer.
185 const int kInnerX
= 4;
186 const int kInnerY
= 5;
187 const int kInnerW
= 2;
188 const int kInnerH
= 3;
192 // This just checks that our checking code is working properly, it just uses
193 // regular skia primitives.
194 TEST(PlatformCanvas
, SkLayer
) {
195 // Create the canvas initialized to opaque white.
196 RefPtr
<SkCanvas
> canvas
= AdoptRef(CreatePlatformCanvas(16, 16, true));
197 canvas
->drawColor(SK_ColorWHITE
);
199 // Make a layer and fill it completely to make sure that the bounds are
202 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
203 canvas
->drawColor(SK_ColorBLACK
);
205 EXPECT_TRUE(VerifyBlackRect(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
));
208 #if !defined(USE_AURA) // http://crbug.com/154358
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
));
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
));
223 // Test that intersecting disjoint clip rectangles sets an empty clip region
224 canvas
->drawColor(SK_ColorWHITE
);
225 EXPECT_TRUE(VerifyCanvasColor(*canvas
, SK_ColorWHITE
));
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);
232 EXPECT_TRUE(VerifyCanvasColor(*canvas
, SK_ColorWHITE
));
235 #endif // !defined(USE_AURA)
237 // Test the layers get filled properly by native rendering.
238 TEST(PlatformCanvas
, FillLayer
) {
239 // Create the canvas initialized to opaque white.
240 RefPtr
<SkCanvas
> canvas
= AdoptRef(CreatePlatformCanvas(16, 16, true));
242 // Make a layer and fill it completely to make sure that the bounds are
244 canvas
->drawColor(SK_ColorWHITE
);
246 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
247 DrawNativeRect(*canvas
, 0, 0, 100, 100);
249 MakeOpaque(canvas
.get(), 0, 0, 100, 100);
252 EXPECT_TRUE(VerifyBlackRect(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
));
254 // Make a layer and fill it partially to make sure the translation is correct.
255 canvas
->drawColor(SK_ColorWHITE
);
257 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
258 DrawNativeRect(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
);
260 MakeOpaque(canvas
.get(), kInnerX
, kInnerY
, kInnerW
, kInnerH
);
263 EXPECT_TRUE(VerifyBlackRect(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
));
265 // Add a clip on the layer and fill to make sure clip is correct.
266 canvas
->drawColor(SK_ColorWHITE
);
268 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
270 AddClip(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
);
271 DrawNativeRect(*canvas
, 0, 0, 100, 100);
273 MakeOpaque(canvas
.get(), kInnerX
, kInnerY
, kInnerW
, kInnerH
);
277 EXPECT_TRUE(VerifyBlackRect(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
));
279 // Add a clip and then make the layer to make sure the clip is correct.
280 canvas
->drawColor(SK_ColorWHITE
);
282 AddClip(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
);
284 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
285 DrawNativeRect(*canvas
, 0, 0, 100, 100);
287 MakeOpaque(canvas
.get(), 0, 0, 100, 100);
291 EXPECT_TRUE(VerifyBlackRect(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
));
294 #if !defined(USE_AURA) // http://crbug.com/154358
296 // Test that translation + make layer works properly.
297 TEST(PlatformCanvas
, TranslateLayer
) {
298 // Create the canvas initialized to opaque white.
299 RefPtr
<SkCanvas
> canvas
= AdoptRef(CreatePlatformCanvas(16, 16, true));
301 // Make a layer and fill it completely to make sure that the bounds are
303 canvas
->drawColor(SK_ColorWHITE
);
305 canvas
->translate(1, 1);
307 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
308 DrawNativeRect(*canvas
, 0, 0, 100, 100);
310 MakeOpaque(canvas
.get(), 0, 0, 100, 100);
314 EXPECT_TRUE(VerifyBlackRect(*canvas
, kLayerX
+ 1, kLayerY
+ 1,
317 // Translate then make the layer.
318 canvas
->drawColor(SK_ColorWHITE
);
320 canvas
->translate(1, 1);
322 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
323 DrawNativeRect(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
);
325 MakeOpaque(canvas
.get(), kInnerX
, kInnerY
, kInnerW
, kInnerH
);
329 EXPECT_TRUE(VerifyBlackRect(*canvas
, kInnerX
+ 1, kInnerY
+ 1,
332 // Make the layer then translate.
333 canvas
->drawColor(SK_ColorWHITE
);
336 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
337 canvas
->translate(1, 1);
338 DrawNativeRect(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
);
340 MakeOpaque(canvas
.get(), kInnerX
, kInnerY
, kInnerW
, kInnerH
);
344 EXPECT_TRUE(VerifyBlackRect(*canvas
, kInnerX
+ 1, kInnerY
+ 1,
347 // Translate both before and after, and have a clip.
348 canvas
->drawColor(SK_ColorWHITE
);
350 canvas
->translate(1, 1);
352 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
353 canvas
->drawColor(SK_ColorWHITE
);
354 canvas
->translate(1, 1);
355 AddClip(*canvas
, kInnerX
+ 1, kInnerY
+ 1, kInnerW
- 1, kInnerH
- 1);
356 DrawNativeRect(*canvas
, 0, 0, 100, 100);
358 MakeOpaque(canvas
.get(), kLayerX
, kLayerY
, kLayerW
, kLayerH
);
362 EXPECT_TRUE(VerifyBlackRect(*canvas
, kInnerX
+ 3, kInnerY
+ 3,
363 kInnerW
- 1, kInnerH
- 1));
365 // TODO(dglazkov): Figure out why this fails on Mac (antialiased clipping?),
366 // modify test and remove this guard.
367 #if !defined(OS_MACOSX)
368 // Translate both before and after, and have a path clip.
369 canvas
->drawColor(SK_ColorWHITE
);
371 canvas
->translate(1, 1);
373 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
374 canvas
->drawColor(SK_ColorWHITE
);
375 canvas
->translate(1, 1);
379 rect
.iset(kInnerX
- 1, kInnerY
- 1,
380 kInnerX
+ kInnerW
, kInnerY
+ kInnerH
);
381 const SkScalar kRadius
= 2.0;
382 path
.addRoundRect(rect
, kRadius
, kRadius
);
383 canvas
->clipPath(path
);
385 DrawNativeRect(*canvas
, 0, 0, 100, 100);
387 MakeOpaque(canvas
.get(), kLayerX
, kLayerY
, kLayerW
, kLayerH
);
391 EXPECT_TRUE(VerifyRoundedRect(*canvas
, SK_ColorWHITE
, SK_ColorBLACK
,
392 kInnerX
+ 1, kInnerY
+ 1, kInnerW
, kInnerH
));
396 #endif // #if !defined(USE_AURA)
398 TEST(PlatformBitmapTest
, PlatformBitmap
) {
399 const int kWidth
= 400;
400 const int kHeight
= 300;
401 scoped_ptr
<PlatformBitmap
> platform_bitmap(new PlatformBitmap
);
403 EXPECT_TRUE(0 == platform_bitmap
->GetSurface());
404 EXPECT_TRUE(platform_bitmap
->GetBitmap().empty());
405 EXPECT_TRUE(platform_bitmap
->GetBitmap().isNull());
407 EXPECT_TRUE(platform_bitmap
->Allocate(kWidth
, kHeight
, /*is_opaque=*/false));
409 EXPECT_TRUE(0 != platform_bitmap
->GetSurface());
410 EXPECT_FALSE(platform_bitmap
->GetBitmap().empty());
411 EXPECT_FALSE(platform_bitmap
->GetBitmap().isNull());
412 EXPECT_EQ(kWidth
, platform_bitmap
->GetBitmap().width());
413 EXPECT_EQ(kHeight
, platform_bitmap
->GetBitmap().height());
414 EXPECT_LE(static_cast<size_t>(platform_bitmap
->GetBitmap().width()*4),
415 platform_bitmap
->GetBitmap().rowBytes());
416 EXPECT_EQ(SkBitmap::kARGB_8888_Config
, // Same for all platforms.
417 platform_bitmap
->GetBitmap().config());
418 EXPECT_TRUE(platform_bitmap
->GetBitmap().lockPixelsAreWritable());
419 EXPECT_TRUE(platform_bitmap
->GetBitmap().pixelRef()->isLocked());
420 EXPECT_EQ(1, platform_bitmap
->GetBitmap().pixelRef()->getRefCnt());
422 *(platform_bitmap
->GetBitmap().getAddr32(10, 20)) = 0xDEED1020;
423 *(platform_bitmap
->GetBitmap().getAddr32(20, 30)) = 0xDEED2030;
425 SkBitmap sk_bitmap
= platform_bitmap
->GetBitmap();
426 sk_bitmap
.lockPixels();
428 EXPECT_EQ(2, platform_bitmap
->GetBitmap().pixelRef()->getRefCnt());
429 EXPECT_EQ(2, sk_bitmap
.pixelRef()->getRefCnt());
431 EXPECT_EQ(0xDEED1020, *sk_bitmap
.getAddr32(10, 20));
432 EXPECT_EQ(0xDEED2030, *sk_bitmap
.getAddr32(20, 30));
434 *(platform_bitmap
->GetBitmap().getAddr32(30, 40)) = 0xDEED3040;
436 // The SkBitmaps derived from a PlatformBitmap must be capable of outliving
437 // the PlatformBitmap.
438 platform_bitmap
.reset();
440 EXPECT_EQ(1, sk_bitmap
.pixelRef()->getRefCnt());
442 EXPECT_EQ(0xDEED1020, *sk_bitmap
.getAddr32(10, 20));
443 EXPECT_EQ(0xDEED2030, *sk_bitmap
.getAddr32(20, 30));
444 EXPECT_EQ(0xDEED3040, *sk_bitmap
.getAddr32(30, 40));
445 sk_bitmap
.unlockPixels();
447 EXPECT_EQ(NULL
, sk_bitmap
.getPixels());
449 sk_bitmap
.lockPixels();
450 EXPECT_EQ(0xDEED1020, *sk_bitmap
.getAddr32(10, 20));
451 EXPECT_EQ(0xDEED2030, *sk_bitmap
.getAddr32(20, 30));
452 EXPECT_EQ(0xDEED3040, *sk_bitmap
.getAddr32(30, 40));
453 sk_bitmap
.unlockPixels();