Speculatively re-enabling flaky WebRTC tests.
[chromium-blink-merge.git] / skia / ext / platform_canvas_unittest.cc
blob22a20a1aa33c6ecf74e1433b510215038a19a258
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(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,
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 #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)
110 #if defined(OS_WIN)
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();
115 RECT inner_rc;
116 inner_rc.left = x;
117 inner_rc.top = y;
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);
128 // RGBA opaque black
129 CGColorRef black = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0);
130 CGContextSetFillColorWithColor(context, black);
131 CGColorRelease(black);
132 CGContextFillRect(context, inner_rc);
134 #else
135 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) {
136 NOTIMPLEMENTED();
138 #endif
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) {
143 SkRect rect;
144 rect.set(SkIntToScalar(x), SkIntToScalar(y),
145 SkIntToScalar(x + w), SkIntToScalar(y + h));
146 canvas.clipRect(rect);
149 class LayerSaver {
150 public:
151 LayerSaver(PlatformCanvas& canvas, int x, int y, int w, int h)
152 : canvas_(canvas),
153 x_(x),
154 y_(y),
155 w_(w),
156 h_(h) {
157 SkRect bounds;
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));
164 ~LayerSaver() {
165 canvas_.restore();
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_; }
177 private:
178 PlatformCanvas& canvas_;
179 int x_, y_, w_, h_;
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
204 // correct.
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
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 #if defined(SK_DEBUG)
422 EXPECT_TRUE(platform_bitmap->GetBitmap().pixelRef()->isLocked());
423 #endif
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();
461 } // namespace skia