Chromecast ATV: distinguish actual shutdown vs. lifecycle from sleep.
[chromium-blink-merge.git] / skia / ext / platform_canvas_unittest.cc
blob9ab5667127b36605e639cc1dbb4468a416db1c65
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>
21 #endif
23 #if !defined(OS_WIN)
24 #include <unistd.h>
25 #endif
27 namespace skia {
29 namespace {
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))
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;
63 return true;
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);
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;
93 return true;
95 #endif
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 // 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);
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();
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)));
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();
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);
132 #else
133 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) {
134 NOTIMPLEMENTED();
136 #endif
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);
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));
162 ~LayerSaver() {
163 canvas_.restore();
166 int x() const { return x_; }
167 int y() const { return y_; }
168 int w() const { return w_; }
169 int h() const { return h_; }
171 // Returns the EXCLUSIVE far bounds of the layer.
172 int right() const { return x_ + w_; }
173 int bottom() const { return y_ + h_; }
175 private:
176 PlatformCanvas& canvas_;
177 int x_, y_, w_, h_;
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;
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;
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);
201 // Make a layer and fill it completely to make sure that the bounds are
202 // correct.
204 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
205 canvas->drawColor(SK_ColorBLACK);
207 EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH));
210 #if !defined(USE_AURA) // http://crbug.com/154358
212 // Test native clipping.
213 TEST(PlatformCanvas, ClipRegion) {
214 // Initialize a white canvas
215 RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true));
216 canvas->drawColor(SK_ColorWHITE);
217 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE));
219 // Test that initially the canvas has no clip region, by filling it
220 // with a black rectangle.
221 // Note: Don't use LayerSaver, since internally it sets a clip region.
222 DrawNativeRect(*canvas, 0, 0, 16, 16);
223 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorBLACK));
225 // Test that intersecting disjoint clip rectangles sets an empty clip region
226 canvas->drawColor(SK_ColorWHITE);
227 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE));
229 LayerSaver layer(*canvas, 0, 0, 16, 16);
230 AddClip(*canvas, 2, 3, 4, 5);
231 AddClip(*canvas, 4, 9, 10, 10);
232 DrawNativeRect(*canvas, 0, 0, 16, 16);
234 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
245 // correct.
246 canvas->drawColor(SK_ColorWHITE);
248 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
249 DrawNativeRect(*canvas, 0, 0, 100, 100);
250 #if defined(OS_WIN)
251 MakeOpaque(canvas.get(), 0, 0, 100, 100);
252 #endif
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);
261 #if defined(OS_WIN)
262 MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
263 #endif
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);
271 canvas->save();
272 AddClip(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
273 DrawNativeRect(*canvas, 0, 0, 100, 100);
274 #if defined(OS_WIN)
275 MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
276 #endif
277 canvas->restore();
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);
283 canvas->save();
284 AddClip(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
286 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
287 DrawNativeRect(*canvas, 0, 0, 100, 100);
288 #if defined(OS_WIN)
289 MakeOpaque(canvas.get(), 0, 0, 100, 100);
290 #endif
292 canvas->restore();
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
304 // correct.
305 canvas->drawColor(SK_ColorWHITE);
306 canvas->save();
307 canvas->translate(1, 1);
309 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
310 DrawNativeRect(*canvas, 0, 0, 100, 100);
311 #if defined(OS_WIN)
312 MakeOpaque(canvas.get(), 0, 0, 100, 100);
313 #endif
315 canvas->restore();
316 EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX + 1, kLayerY + 1,
317 kLayerW, kLayerH));
319 // Translate then make the layer.
320 canvas->drawColor(SK_ColorWHITE);
321 canvas->save();
322 canvas->translate(1, 1);
324 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
325 DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
326 #if defined(OS_WIN)
327 MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
328 #endif
330 canvas->restore();
331 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 1, kInnerY + 1,
332 kInnerW, kInnerH));
334 // Make the layer then translate.
335 canvas->drawColor(SK_ColorWHITE);
336 canvas->save();
338 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
339 canvas->translate(1, 1);
340 DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
341 #if defined(OS_WIN)
342 MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
343 #endif
345 canvas->restore();
346 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 1, kInnerY + 1,
347 kInnerW, kInnerH));
349 // Translate both before and after, and have a clip.
350 canvas->drawColor(SK_ColorWHITE);
351 canvas->save();
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);
359 #if defined(OS_WIN)
360 MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH);
361 #endif
363 canvas->restore();
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);
372 canvas->save();
373 canvas->translate(1, 1);
375 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
376 canvas->drawColor(SK_ColorWHITE);
377 canvas->translate(1, 1);
379 SkPath path;
380 SkRect rect;
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);
388 #if defined(OS_WIN)
389 MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH);
390 #endif
392 canvas->restore();
393 EXPECT_TRUE(VerifyRoundedRect(*canvas, SK_ColorWHITE, SK_ColorBLACK,
394 kInnerX + 1, kInnerY + 1, kInnerW, kInnerH));
395 #endif
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 EXPECT_TRUE(platform_bitmap->GetBitmap().pixelRef()->isLocked());
422 EXPECT_TRUE(platform_bitmap->GetBitmap().pixelRef()->unique());
424 *(platform_bitmap->GetBitmap().getAddr32(10, 20)) = 0xDEED1020;
425 *(platform_bitmap->GetBitmap().getAddr32(20, 30)) = 0xDEED2030;
427 SkBitmap sk_bitmap = platform_bitmap->GetBitmap();
428 sk_bitmap.lockPixels();
430 EXPECT_FALSE(platform_bitmap->GetBitmap().pixelRef()->unique());
431 EXPECT_FALSE(sk_bitmap.pixelRef()->unique());
433 EXPECT_EQ(0xDEED1020, *sk_bitmap.getAddr32(10, 20));
434 EXPECT_EQ(0xDEED2030, *sk_bitmap.getAddr32(20, 30));
436 *(platform_bitmap->GetBitmap().getAddr32(30, 40)) = 0xDEED3040;
438 // The SkBitmaps derived from a PlatformBitmap must be capable of outliving
439 // the PlatformBitmap.
440 platform_bitmap.reset();
442 EXPECT_TRUE(sk_bitmap.pixelRef()->unique());
444 EXPECT_EQ(0xDEED1020, *sk_bitmap.getAddr32(10, 20));
445 EXPECT_EQ(0xDEED2030, *sk_bitmap.getAddr32(20, 30));
446 EXPECT_EQ(0xDEED3040, *sk_bitmap.getAddr32(30, 40));
447 sk_bitmap.unlockPixels();
449 EXPECT_EQ(NULL, sk_bitmap.getPixels());
451 sk_bitmap.lockPixels();
452 EXPECT_EQ(0xDEED1020, *sk_bitmap.getAddr32(10, 20));
453 EXPECT_EQ(0xDEED2030, *sk_bitmap.getAddr32(20, 30));
454 EXPECT_EQ(0xDEED3040, *sk_bitmap.getAddr32(30, 40));
455 sk_bitmap.unlockPixels();
459 } // namespace skia