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 "skia/ext/platform_canvas.h"
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"
19 #if defined(OS_MACOSX)
20 #import <ApplicationServices/ApplicationServices.h>
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
);
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
);
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
))
57 // Outside the square should be canvas_color
58 if (!IsOfColor(bitmap
, cur_x
, cur_y
, canvas_color
))
66 #if !defined(USE_AURA) && !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
,
77 SkBaseDevice
* device
= skia::GetTopDevice(canvas
);
78 const SkBitmap
& bitmap
= device
->accessBitmap(false);
79 SkAutoLockPixels
lock(bitmap
);
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;
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;
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
);
103 #if !defined(USE_AURA) // http://crbug.com/154358
104 // Check that every pixel in the canvas is a single color.
105 bool VerifyCanvasColor(const PlatformCanvas
& canvas
, uint32_t canvas_color
) {
106 return VerifyRect(canvas
, canvas_color
, 0, 0, 0, 0, 0);
108 #endif // !defined(USE_AURA)
111 void DrawNativeRect(PlatformCanvas
& canvas
, int x
, int y
, int w
, int h
) {
112 skia::ScopedPlatformPaint
scoped_platform_paint(&canvas
);
113 HDC dc
= scoped_platform_paint
.GetPlatformSurface();
118 inner_rc
.right
= x
+ w
;
119 inner_rc
.bottom
= y
+ h
;
120 FillRect(dc
, &inner_rc
, reinterpret_cast<HBRUSH
>(GetStockObject(BLACK_BRUSH
)));
122 #elif defined(OS_MACOSX)
123 void DrawNativeRect(PlatformCanvas
& canvas
, int x
, int y
, int w
, int h
) {
124 skia::ScopedPlatformPaint
scoped_platform_paint(&canvas
);
125 CGContextRef context
= scoped_platform_paint
.GetPlatformSurface();
127 CGRect inner_rc
= CGRectMake(x
, y
, w
, h
);
129 CGColorRef black
= CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0);
130 CGContextSetFillColorWithColor(context
, black
);
131 CGColorRelease(black
);
132 CGContextFillRect(context
, inner_rc
);
135 void DrawNativeRect(PlatformCanvas
& canvas
, int x
, int y
, int w
, int h
) {
140 // Clips the contents of the canvas to the given rectangle. This will be
141 // intersected with any existing clip.
142 void AddClip(PlatformCanvas
& canvas
, int x
, int y
, int w
, int h
) {
144 rect
.set(SkIntToScalar(x
), SkIntToScalar(y
),
145 SkIntToScalar(x
+ w
), SkIntToScalar(y
+ h
));
146 canvas
.clipRect(rect
);
151 LayerSaver(PlatformCanvas
& canvas
, int x
, int y
, int w
, int h
)
158 bounds
.set(SkIntToScalar(x_
), SkIntToScalar(y_
),
159 SkIntToScalar(right()), SkIntToScalar(bottom()));
160 canvas_
.saveLayer(&bounds
, NULL
);
161 canvas
.clear(SkColorSetARGB(0, 0, 0, 0));
168 int x() const { return x_
; }
169 int y() const { return y_
; }
170 int w() const { return w_
; }
171 int h() const { return h_
; }
173 // Returns the EXCLUSIVE far bounds of the layer.
174 int right() const { return x_
+ w_
; }
175 int bottom() const { return y_
+ h_
; }
178 PlatformCanvas
& canvas_
;
182 // Size used for making layers in many of the below tests.
183 const int kLayerX
= 2;
184 const int kLayerY
= 3;
185 const int kLayerW
= 9;
186 const int kLayerH
= 7;
188 // Size used by some tests to draw a rectangle inside the layer.
189 const int kInnerX
= 4;
190 const int kInnerY
= 5;
191 const int kInnerW
= 2;
192 const int kInnerH
= 3;
196 // This just checks that our checking code is working properly, it just uses
197 // regular skia primitives.
198 TEST(PlatformCanvas
, SkLayer
) {
199 // Create the canvas initialized to opaque white.
200 RefPtr
<SkCanvas
> canvas
= AdoptRef(CreatePlatformCanvas(16, 16, true));
201 canvas
->drawColor(SK_ColorWHITE
);
203 // Make a layer and fill it completely to make sure that the bounds are
206 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
207 canvas
->drawColor(SK_ColorBLACK
);
209 EXPECT_TRUE(VerifyBlackRect(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
));
212 #if !defined(USE_AURA) // http://crbug.com/154358
213 // Test native clipping.
214 TEST(PlatformCanvas
, ClipRegion
) {
215 // Initialize a white canvas
216 RefPtr
<SkCanvas
> canvas
= AdoptRef(CreatePlatformCanvas(16, 16, true));
217 canvas
->drawColor(SK_ColorWHITE
);
218 EXPECT_TRUE(VerifyCanvasColor(*canvas
, SK_ColorWHITE
));
220 // Test that initially the canvas has no clip region, by filling it
221 // with a black rectangle.
222 // Note: Don't use LayerSaver, since internally it sets a clip region.
223 DrawNativeRect(*canvas
, 0, 0, 16, 16);
224 EXPECT_TRUE(VerifyCanvasColor(*canvas
, SK_ColorBLACK
));
226 // Test that intersecting disjoint clip rectangles sets an empty clip region
227 canvas
->drawColor(SK_ColorWHITE
);
228 EXPECT_TRUE(VerifyCanvasColor(*canvas
, SK_ColorWHITE
));
230 LayerSaver
layer(*canvas
, 0, 0, 16, 16);
231 AddClip(*canvas
, 2, 3, 4, 5);
232 AddClip(*canvas
, 4, 9, 10, 10);
233 DrawNativeRect(*canvas
, 0, 0, 16, 16);
235 EXPECT_TRUE(VerifyCanvasColor(*canvas
, SK_ColorWHITE
));
237 #endif // !defined(USE_AURA)
239 // Test the layers get filled properly by native rendering.
240 TEST(PlatformCanvas
, FillLayer
) {
241 // Create the canvas initialized to opaque white.
242 RefPtr
<SkCanvas
> canvas
= AdoptRef(CreatePlatformCanvas(16, 16, true));
244 // Make a layer and fill it completely to make sure that the bounds are
246 canvas
->drawColor(SK_ColorWHITE
);
248 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
249 DrawNativeRect(*canvas
, 0, 0, 100, 100);
251 MakeOpaque(canvas
.get(), 0, 0, 100, 100);
254 EXPECT_TRUE(VerifyBlackRect(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
));
256 // Make a layer and fill it partially to make sure the translation is correct.
257 canvas
->drawColor(SK_ColorWHITE
);
259 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
260 DrawNativeRect(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
);
262 MakeOpaque(canvas
.get(), kInnerX
, kInnerY
, kInnerW
, kInnerH
);
265 EXPECT_TRUE(VerifyBlackRect(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
));
267 // Add a clip on the layer and fill to make sure clip is correct.
268 canvas
->drawColor(SK_ColorWHITE
);
270 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
272 AddClip(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
);
273 DrawNativeRect(*canvas
, 0, 0, 100, 100);
275 MakeOpaque(canvas
.get(), kInnerX
, kInnerY
, kInnerW
, kInnerH
);
279 EXPECT_TRUE(VerifyBlackRect(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
));
281 // Add a clip and then make the layer to make sure the clip is correct.
282 canvas
->drawColor(SK_ColorWHITE
);
284 AddClip(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
);
286 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
287 DrawNativeRect(*canvas
, 0, 0, 100, 100);
289 MakeOpaque(canvas
.get(), 0, 0, 100, 100);
293 EXPECT_TRUE(VerifyBlackRect(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
));
296 #if !defined(USE_AURA) // http://crbug.com/154358
298 // Test that translation + make layer works properly.
299 TEST(PlatformCanvas
, TranslateLayer
) {
300 // Create the canvas initialized to opaque white.
301 RefPtr
<SkCanvas
> canvas
= AdoptRef(CreatePlatformCanvas(16, 16, true));
303 // Make a layer and fill it completely to make sure that the bounds are
305 canvas
->drawColor(SK_ColorWHITE
);
307 canvas
->translate(1, 1);
309 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
310 DrawNativeRect(*canvas
, 0, 0, 100, 100);
312 MakeOpaque(canvas
.get(), 0, 0, 100, 100);
316 EXPECT_TRUE(VerifyBlackRect(*canvas
, kLayerX
+ 1, kLayerY
+ 1,
319 // Translate then make the layer.
320 canvas
->drawColor(SK_ColorWHITE
);
322 canvas
->translate(1, 1);
324 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
325 DrawNativeRect(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
);
327 MakeOpaque(canvas
.get(), kInnerX
, kInnerY
, kInnerW
, kInnerH
);
331 EXPECT_TRUE(VerifyBlackRect(*canvas
, kInnerX
+ 1, kInnerY
+ 1,
334 // Make the layer then translate.
335 canvas
->drawColor(SK_ColorWHITE
);
338 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
339 canvas
->translate(1, 1);
340 DrawNativeRect(*canvas
, kInnerX
, kInnerY
, kInnerW
, kInnerH
);
342 MakeOpaque(canvas
.get(), kInnerX
, kInnerY
, kInnerW
, kInnerH
);
346 EXPECT_TRUE(VerifyBlackRect(*canvas
, kInnerX
+ 1, kInnerY
+ 1,
349 // Translate both before and after, and have a clip.
350 canvas
->drawColor(SK_ColorWHITE
);
352 canvas
->translate(1, 1);
354 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
355 canvas
->drawColor(SK_ColorWHITE
);
356 canvas
->translate(1, 1);
357 AddClip(*canvas
, kInnerX
+ 1, kInnerY
+ 1, kInnerW
- 1, kInnerH
- 1);
358 DrawNativeRect(*canvas
, 0, 0, 100, 100);
360 MakeOpaque(canvas
.get(), kLayerX
, kLayerY
, kLayerW
, kLayerH
);
364 EXPECT_TRUE(VerifyBlackRect(*canvas
, kInnerX
+ 3, kInnerY
+ 3,
365 kInnerW
- 1, kInnerH
- 1));
367 // TODO(dglazkov): Figure out why this fails on Mac (antialiased clipping?),
368 // modify test and remove this guard.
369 #if !defined(OS_MACOSX)
370 // Translate both before and after, and have a path clip.
371 canvas
->drawColor(SK_ColorWHITE
);
373 canvas
->translate(1, 1);
375 LayerSaver
layer(*canvas
, kLayerX
, kLayerY
, kLayerW
, kLayerH
);
376 canvas
->drawColor(SK_ColorWHITE
);
377 canvas
->translate(1, 1);
381 rect
.iset(kInnerX
- 1, kInnerY
- 1,
382 kInnerX
+ kInnerW
, kInnerY
+ kInnerH
);
383 const SkScalar kRadius
= 2.0;
384 path
.addRoundRect(rect
, kRadius
, kRadius
);
385 canvas
->clipPath(path
);
387 DrawNativeRect(*canvas
, 0, 0, 100, 100);
389 MakeOpaque(canvas
.get(), kLayerX
, kLayerY
, kLayerW
, kLayerH
);
393 EXPECT_TRUE(VerifyRoundedRect(*canvas
, SK_ColorWHITE
, SK_ColorBLACK
,
394 kInnerX
+ 1, kInnerY
+ 1, kInnerW
, kInnerH
));
398 #endif // #if !defined(USE_AURA)
400 TEST(PlatformBitmapTest
, PlatformBitmap
) {
401 const int kWidth
= 400;
402 const int kHeight
= 300;
403 scoped_ptr
<PlatformBitmap
> platform_bitmap(new PlatformBitmap
);
405 EXPECT_TRUE(0 == platform_bitmap
->GetSurface());
406 EXPECT_TRUE(platform_bitmap
->GetBitmap().empty());
407 EXPECT_TRUE(platform_bitmap
->GetBitmap().isNull());
409 EXPECT_TRUE(platform_bitmap
->Allocate(kWidth
, kHeight
, /*is_opaque=*/false));
411 EXPECT_TRUE(0 != platform_bitmap
->GetSurface());
412 EXPECT_FALSE(platform_bitmap
->GetBitmap().empty());
413 EXPECT_FALSE(platform_bitmap
->GetBitmap().isNull());
414 EXPECT_EQ(kWidth
, platform_bitmap
->GetBitmap().width());
415 EXPECT_EQ(kHeight
, platform_bitmap
->GetBitmap().height());
416 EXPECT_LE(static_cast<size_t>(platform_bitmap
->GetBitmap().width()*4),
417 platform_bitmap
->GetBitmap().rowBytes());
418 EXPECT_EQ(kN32_SkColorType
, // Same for all platforms.
419 platform_bitmap
->GetBitmap().colorType());
420 EXPECT_TRUE(platform_bitmap
->GetBitmap().lockPixelsAreWritable());
421 #if defined(SK_DEBUG)
422 EXPECT_TRUE(platform_bitmap
->GetBitmap().pixelRef()->isLocked());
424 EXPECT_TRUE(platform_bitmap
->GetBitmap().pixelRef()->unique());
426 *(platform_bitmap
->GetBitmap().getAddr32(10, 20)) = 0xDEED1020;
427 *(platform_bitmap
->GetBitmap().getAddr32(20, 30)) = 0xDEED2030;
429 SkBitmap sk_bitmap
= platform_bitmap
->GetBitmap();
430 sk_bitmap
.lockPixels();
432 EXPECT_FALSE(platform_bitmap
->GetBitmap().pixelRef()->unique());
433 EXPECT_FALSE(sk_bitmap
.pixelRef()->unique());
435 EXPECT_EQ(0xDEED1020, *sk_bitmap
.getAddr32(10, 20));
436 EXPECT_EQ(0xDEED2030, *sk_bitmap
.getAddr32(20, 30));
438 *(platform_bitmap
->GetBitmap().getAddr32(30, 40)) = 0xDEED3040;
440 // The SkBitmaps derived from a PlatformBitmap must be capable of outliving
441 // the PlatformBitmap.
442 platform_bitmap
.reset();
444 EXPECT_TRUE(sk_bitmap
.pixelRef()->unique());
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();
451 EXPECT_EQ(NULL
, sk_bitmap
.getPixels());
453 sk_bitmap
.lockPixels();
454 EXPECT_EQ(0xDEED1020, *sk_bitmap
.getAddr32(10, 20));
455 EXPECT_EQ(0xDEED2030, *sk_bitmap
.getAddr32(20, 30));
456 EXPECT_EQ(0xDEED3040, *sk_bitmap
.getAddr32(30, 40));
457 sk_bitmap
.unlockPixels();