[SyncFS] Initialize SyncWorker when sync is enabled.
[chromium-blink-merge.git] / skia / ext / platform_canvas_unittest.cc
blob3d8ef00fe630ca7973d37c56d631f5dc63e89c99
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"
9 #if defined(OS_MACOSX)
10 #import <ApplicationServices/ApplicationServices.h>
11 #endif
13 #if !defined(OS_WIN)
14 #include <unistd.h>
15 #endif
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/SkColorPriv.h"
24 #include "third_party/skia/include/core/SkPixelRef.h"
26 namespace skia {
28 namespace {
30 bool IsOfColor(const SkBitmap& bitmap, int x, int y, uint32_t color) {
31 // For masking out the alpha values.
32 static uint32_t alpha_mask =
33 static_cast<uint32_t>(SK_A32_MASK) << SK_A32_SHIFT;
34 return (*bitmap.getAddr32(x, y) | alpha_mask) == (color | alpha_mask);
37 // Return true if the canvas is filled to canvas_color, and contains a single
38 // rectangle filled to rect_color. This function ignores the alpha channel,
39 // since Windows will sometimes clear the alpha channel when drawing, and we
40 // will fix that up later in cases it's necessary.
41 bool VerifyRect(const PlatformCanvas& canvas,
42 uint32_t canvas_color, uint32_t rect_color,
43 int x, int y, int w, int h) {
44 SkBaseDevice* device = skia::GetTopDevice(canvas);
45 const SkBitmap& bitmap = device->accessBitmap(false);
46 SkAutoLockPixels lock(bitmap);
48 for (int cur_y = 0; cur_y < bitmap.height(); cur_y++) {
49 for (int cur_x = 0; cur_x < bitmap.width(); cur_x++) {
50 if (cur_x >= x && cur_x < x + w &&
51 cur_y >= y && cur_y < y + h) {
52 // Inside the square should be rect_color
53 if (!IsOfColor(bitmap, cur_x, cur_y, rect_color))
54 return false;
55 } else {
56 // Outside the square should be canvas_color
57 if (!IsOfColor(bitmap, cur_x, cur_y, canvas_color))
58 return false;
62 return true;
65 #if !defined(OS_MACOSX)
66 // Return true if canvas has something that passes for a rounded-corner
67 // rectangle. Basically, we're just checking to make sure that the pixels in the
68 // middle are of rect_color and pixels in the corners are of canvas_color.
69 bool VerifyRoundedRect(const PlatformCanvas& canvas,
70 uint32_t canvas_color, uint32_t rect_color,
71 int x, int y, int w, int h) {
72 SkBaseDevice* device = skia::GetTopDevice(canvas);
73 const SkBitmap& bitmap = device->accessBitmap(false);
74 SkAutoLockPixels lock(bitmap);
76 // Check corner points first. They should be of canvas_color.
77 if (!IsOfColor(bitmap, x, y, canvas_color)) return false;
78 if (!IsOfColor(bitmap, x + w, y, canvas_color)) return false;
79 if (!IsOfColor(bitmap, x, y + h, canvas_color)) return false;
80 if (!IsOfColor(bitmap, x + w, y, canvas_color)) return false;
82 // Check middle points. They should be of rect_color.
83 if (!IsOfColor(bitmap, (x + w / 2), y, rect_color)) return false;
84 if (!IsOfColor(bitmap, x, (y + h / 2), rect_color)) return false;
85 if (!IsOfColor(bitmap, x + w, (y + h / 2), rect_color)) return false;
86 if (!IsOfColor(bitmap, (x + w / 2), y + h, rect_color)) return false;
88 return true;
90 #endif
92 // Checks whether there is a white canvas with a black square at the given
93 // location in pixels (not in the canvas coordinate system).
94 bool VerifyBlackRect(const PlatformCanvas& canvas, int x, int y, int w, int h) {
95 return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h);
98 // Check that every pixel in the canvas is a single color.
99 bool VerifyCanvasColor(const PlatformCanvas& canvas, uint32_t canvas_color) {
100 return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0);
103 #if defined(OS_WIN)
104 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) {
105 skia::ScopedPlatformPaint scoped_platform_paint(&canvas);
106 HDC dc = scoped_platform_paint.GetPlatformSurface();
108 RECT inner_rc;
109 inner_rc.left = x;
110 inner_rc.top = y;
111 inner_rc.right = x + w;
112 inner_rc.bottom = y + h;
113 FillRect(dc, &inner_rc, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
115 #elif defined(OS_MACOSX)
116 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) {
117 skia::ScopedPlatformPaint scoped_platform_paint(&canvas);
118 CGContextRef context = scoped_platform_paint.GetPlatformSurface();
120 CGRect inner_rc = CGRectMake(x, y, w, h);
121 // RGBA opaque black
122 CGColorRef black = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0);
123 CGContextSetFillColorWithColor(context, black);
124 CGColorRelease(black);
125 CGContextFillRect(context, inner_rc);
127 #else
128 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) {
129 notImplemented();
131 #endif
133 // Clips the contents of the canvas to the given rectangle. This will be
134 // intersected with any existing clip.
135 void AddClip(PlatformCanvas& canvas, int x, int y, int w, int h) {
136 SkRect rect;
137 rect.set(SkIntToScalar(x), SkIntToScalar(y),
138 SkIntToScalar(x + w), SkIntToScalar(y + h));
139 canvas.clipRect(rect);
142 class LayerSaver {
143 public:
144 LayerSaver(PlatformCanvas& canvas, int x, int y, int w, int h)
145 : canvas_(canvas),
146 x_(x),
147 y_(y),
148 w_(w),
149 h_(h) {
150 SkRect bounds;
151 bounds.set(SkIntToScalar(x_), SkIntToScalar(y_),
152 SkIntToScalar(right()), SkIntToScalar(bottom()));
153 canvas_.saveLayer(&bounds, NULL);
154 canvas.clear(SkColorSetARGB(0, 0, 0, 0));
157 ~LayerSaver() {
158 canvas_.restore();
161 int x() const { return x_; }
162 int y() const { return y_; }
163 int w() const { return w_; }
164 int h() const { return h_; }
166 // Returns the EXCLUSIVE far bounds of the layer.
167 int right() const { return x_ + w_; }
168 int bottom() const { return y_ + h_; }
170 private:
171 PlatformCanvas& canvas_;
172 int x_, y_, w_, h_;
175 // Size used for making layers in many of the below tests.
176 const int kLayerX = 2;
177 const int kLayerY = 3;
178 const int kLayerW = 9;
179 const int kLayerH = 7;
181 // Size used by some tests to draw a rectangle inside the layer.
182 const int kInnerX = 4;
183 const int kInnerY = 5;
184 const int kInnerW = 2;
185 const int kInnerH = 3;
189 // This just checks that our checking code is working properly, it just uses
190 // regular skia primitives.
191 TEST(PlatformCanvas, SkLayer) {
192 // Create the canvas initialized to opaque white.
193 RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true));
194 canvas->drawColor(SK_ColorWHITE);
196 // Make a layer and fill it completely to make sure that the bounds are
197 // correct.
199 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
200 canvas->drawColor(SK_ColorBLACK);
202 EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH));
205 #if !defined(USE_AURA) // http://crbug.com/154358
207 // Test native clipping.
208 TEST(PlatformCanvas, ClipRegion) {
209 // Initialize a white canvas
210 RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true));
211 canvas->drawColor(SK_ColorWHITE);
212 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE));
214 // Test that initially the canvas has no clip region, by filling it
215 // with a black rectangle.
216 // Note: Don't use LayerSaver, since internally it sets a clip region.
217 DrawNativeRect(*canvas, 0, 0, 16, 16);
218 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorBLACK));
220 // Test that intersecting disjoint clip rectangles sets an empty clip region
221 canvas->drawColor(SK_ColorWHITE);
222 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE));
224 LayerSaver layer(*canvas, 0, 0, 16, 16);
225 AddClip(*canvas, 2, 3, 4, 5);
226 AddClip(*canvas, 4, 9, 10, 10);
227 DrawNativeRect(*canvas, 0, 0, 16, 16);
229 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE));
232 #endif // !defined(USE_AURA)
234 // Test the layers get filled properly by native rendering.
235 TEST(PlatformCanvas, FillLayer) {
236 // Create the canvas initialized to opaque white.
237 RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true));
239 // Make a layer and fill it completely to make sure that the bounds are
240 // correct.
241 canvas->drawColor(SK_ColorWHITE);
243 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
244 DrawNativeRect(*canvas, 0, 0, 100, 100);
245 #if defined(OS_WIN)
246 MakeOpaque(canvas.get(), 0, 0, 100, 100);
247 #endif
249 EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH));
251 // Make a layer and fill it partially to make sure the translation is correct.
252 canvas->drawColor(SK_ColorWHITE);
254 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
255 DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
256 #if defined(OS_WIN)
257 MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
258 #endif
260 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH));
262 // Add a clip on the layer and fill to make sure clip is correct.
263 canvas->drawColor(SK_ColorWHITE);
265 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
266 canvas->save();
267 AddClip(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
268 DrawNativeRect(*canvas, 0, 0, 100, 100);
269 #if defined(OS_WIN)
270 MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
271 #endif
272 canvas->restore();
274 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH));
276 // Add a clip and then make the layer to make sure the clip is correct.
277 canvas->drawColor(SK_ColorWHITE);
278 canvas->save();
279 AddClip(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
281 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
282 DrawNativeRect(*canvas, 0, 0, 100, 100);
283 #if defined(OS_WIN)
284 MakeOpaque(canvas.get(), 0, 0, 100, 100);
285 #endif
287 canvas->restore();
288 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH));
291 #if !defined(USE_AURA) // http://crbug.com/154358
293 // Test that translation + make layer works properly.
294 TEST(PlatformCanvas, TranslateLayer) {
295 // Create the canvas initialized to opaque white.
296 RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true));
298 // Make a layer and fill it completely to make sure that the bounds are
299 // correct.
300 canvas->drawColor(SK_ColorWHITE);
301 canvas->save();
302 canvas->translate(1, 1);
304 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
305 DrawNativeRect(*canvas, 0, 0, 100, 100);
306 #if defined(OS_WIN)
307 MakeOpaque(canvas.get(), 0, 0, 100, 100);
308 #endif
310 canvas->restore();
311 EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX + 1, kLayerY + 1,
312 kLayerW, kLayerH));
314 // Translate then make the layer.
315 canvas->drawColor(SK_ColorWHITE);
316 canvas->save();
317 canvas->translate(1, 1);
319 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
320 DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
321 #if defined(OS_WIN)
322 MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
323 #endif
325 canvas->restore();
326 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 1, kInnerY + 1,
327 kInnerW, kInnerH));
329 // Make the layer then translate.
330 canvas->drawColor(SK_ColorWHITE);
331 canvas->save();
333 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
334 canvas->translate(1, 1);
335 DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
336 #if defined(OS_WIN)
337 MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
338 #endif
340 canvas->restore();
341 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 1, kInnerY + 1,
342 kInnerW, kInnerH));
344 // Translate both before and after, and have a clip.
345 canvas->drawColor(SK_ColorWHITE);
346 canvas->save();
347 canvas->translate(1, 1);
349 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
350 canvas->drawColor(SK_ColorWHITE);
351 canvas->translate(1, 1);
352 AddClip(*canvas, kInnerX + 1, kInnerY + 1, kInnerW - 1, kInnerH - 1);
353 DrawNativeRect(*canvas, 0, 0, 100, 100);
354 #if defined(OS_WIN)
355 MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH);
356 #endif
358 canvas->restore();
359 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 3, kInnerY + 3,
360 kInnerW - 1, kInnerH - 1));
362 // TODO(dglazkov): Figure out why this fails on Mac (antialiased clipping?),
363 // modify test and remove this guard.
364 #if !defined(OS_MACOSX)
365 // Translate both before and after, and have a path clip.
366 canvas->drawColor(SK_ColorWHITE);
367 canvas->save();
368 canvas->translate(1, 1);
370 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
371 canvas->drawColor(SK_ColorWHITE);
372 canvas->translate(1, 1);
374 SkPath path;
375 SkRect rect;
376 rect.iset(kInnerX - 1, kInnerY - 1,
377 kInnerX + kInnerW, kInnerY + kInnerH);
378 const SkScalar kRadius = 2.0;
379 path.addRoundRect(rect, kRadius, kRadius);
380 canvas->clipPath(path);
382 DrawNativeRect(*canvas, 0, 0, 100, 100);
383 #if defined(OS_WIN)
384 MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH);
385 #endif
387 canvas->restore();
388 EXPECT_TRUE(VerifyRoundedRect(*canvas, SK_ColorWHITE, SK_ColorBLACK,
389 kInnerX + 1, kInnerY + 1, kInnerW, kInnerH));
390 #endif
393 #endif // #if !defined(USE_AURA)
395 TEST(PlatformBitmapTest, PlatformBitmap) {
396 const int kWidth = 400;
397 const int kHeight = 300;
398 scoped_ptr<PlatformBitmap> platform_bitmap(new PlatformBitmap);
400 EXPECT_TRUE(0 == platform_bitmap->GetSurface());
401 EXPECT_TRUE(platform_bitmap->GetBitmap().empty());
402 EXPECT_TRUE(platform_bitmap->GetBitmap().isNull());
404 EXPECT_TRUE(platform_bitmap->Allocate(kWidth, kHeight, /*is_opaque=*/false));
406 EXPECT_TRUE(0 != platform_bitmap->GetSurface());
407 EXPECT_FALSE(platform_bitmap->GetBitmap().empty());
408 EXPECT_FALSE(platform_bitmap->GetBitmap().isNull());
409 EXPECT_EQ(kWidth, platform_bitmap->GetBitmap().width());
410 EXPECT_EQ(kHeight, platform_bitmap->GetBitmap().height());
411 EXPECT_LE(static_cast<size_t>(platform_bitmap->GetBitmap().width()*4),
412 platform_bitmap->GetBitmap().rowBytes());
413 EXPECT_EQ(kN32_SkColorType, // Same for all platforms.
414 platform_bitmap->GetBitmap().colorType());
415 EXPECT_TRUE(platform_bitmap->GetBitmap().lockPixelsAreWritable());
416 EXPECT_TRUE(platform_bitmap->GetBitmap().pixelRef()->isLocked());
417 EXPECT_EQ(1, platform_bitmap->GetBitmap().pixelRef()->getRefCnt());
419 *(platform_bitmap->GetBitmap().getAddr32(10, 20)) = 0xDEED1020;
420 *(platform_bitmap->GetBitmap().getAddr32(20, 30)) = 0xDEED2030;
422 SkBitmap sk_bitmap = platform_bitmap->GetBitmap();
423 sk_bitmap.lockPixels();
425 EXPECT_EQ(2, platform_bitmap->GetBitmap().pixelRef()->getRefCnt());
426 EXPECT_EQ(2, sk_bitmap.pixelRef()->getRefCnt());
428 EXPECT_EQ(0xDEED1020, *sk_bitmap.getAddr32(10, 20));
429 EXPECT_EQ(0xDEED2030, *sk_bitmap.getAddr32(20, 30));
431 *(platform_bitmap->GetBitmap().getAddr32(30, 40)) = 0xDEED3040;
433 // The SkBitmaps derived from a PlatformBitmap must be capable of outliving
434 // the PlatformBitmap.
435 platform_bitmap.reset();
437 EXPECT_EQ(1, sk_bitmap.pixelRef()->getRefCnt());
439 EXPECT_EQ(0xDEED1020, *sk_bitmap.getAddr32(10, 20));
440 EXPECT_EQ(0xDEED2030, *sk_bitmap.getAddr32(20, 30));
441 EXPECT_EQ(0xDEED3040, *sk_bitmap.getAddr32(30, 40));
442 sk_bitmap.unlockPixels();
444 EXPECT_EQ(NULL, sk_bitmap.getPixels());
446 sk_bitmap.lockPixels();
447 EXPECT_EQ(0xDEED1020, *sk_bitmap.getAddr32(10, 20));
448 EXPECT_EQ(0xDEED2030, *sk_bitmap.getAddr32(20, 30));
449 EXPECT_EQ(0xDEED3040, *sk_bitmap.getAddr32(30, 40));
450 sk_bitmap.unlockPixels();
454 } // namespace skia