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.
4
5// TODO(awalker): clean up the const/non-const reference handling in this test
6
7#include "build/build_config.h"
8
9#if defined(OS_MACOSX)
10#import <ApplicationServices/ApplicationServices.h>
11#endif
12
13#if !defined(OS_WIN)
14#include <unistd.h>
15#endif
16
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"
24
25namespace skia {
26
27namespace {
28
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.
33bool VerifyRect(const PlatformCanvas& canvas,
34                uint32_t canvas_color, uint32_t rect_color,
35                int x, int y, int w, int h) {
36  SkDevice* device = skia::GetTopDevice(canvas);
37  const SkBitmap& bitmap = device->accessBitmap(false);
38  SkAutoLockPixels lock(bitmap);
39
40  // For masking out the alpha values.
41  uint32_t alpha_mask = 0xFF << SK_A32_SHIFT;
42
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))
50          return false;
51      } else {
52        // Outside the square should be canvas_color
53        if ((*bitmap.getAddr32(cur_x, cur_y) | alpha_mask) !=
54            (canvas_color | alpha_mask))
55          return false;
56      }
57    }
58  }
59  return true;
60}
61
62#if !defined(OS_MACOSX)
63bool 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);
67}
68
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.
72bool VerifyRoundedRect(const PlatformCanvas& canvas,
73                       uint32_t canvas_color, uint32_t rect_color,
74                       int x, int y, int w, int h) {
75  SkDevice* device = skia::GetTopDevice(canvas);
76  const SkBitmap& bitmap = device->accessBitmap(false);
77  SkAutoLockPixels lock(bitmap);
78
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;
84
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;
90
91  return true;
92}
93#endif
94
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).
97bool VerifyBlackRect(const PlatformCanvas& canvas, int x, int y, int w, int h) {
98  return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h);
99}
100
101// Check that every pixel in the canvas is a single color.
102bool VerifyCanvasColor(const PlatformCanvas& canvas, uint32_t canvas_color) {
103  return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0);
104}
105
106#if defined(OS_WIN)
107void 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();
110
111  RECT inner_rc;
112  inner_rc.left = x;
113  inner_rc.top = y;
114  inner_rc.right = x + w;
115  inner_rc.bottom = y + h;
116  FillRect(dc, &inner_rc, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
117}
118#elif defined(OS_MACOSX)
119void 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();
122
123  CGRect inner_rc = CGRectMake(x, y, w, h);
124  // RGBA opaque black
125  CGColorRef black = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0);
126  CGContextSetFillColorWithColor(context, black);
127  CGColorRelease(black);
128  CGContextFillRect(context, inner_rc);
129}
130#else
131void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) {
132  notImplemented();
133}
134#endif
135
136// Clips the contents of the canvas to the given rectangle. This will be
137// intersected with any existing clip.
138void AddClip(PlatformCanvas& canvas, int x, int y, int w, int h) {
139  SkRect rect;
140  rect.set(SkIntToScalar(x), SkIntToScalar(y),
141           SkIntToScalar(x + w), SkIntToScalar(y + h));
142  canvas.clipRect(rect);
143}
144
145class LayerSaver {
146 public:
147  LayerSaver(PlatformCanvas& canvas, int x, int y, int w, int h)
148      : canvas_(canvas),
149        x_(x),
150        y_(y),
151        w_(w),
152        h_(h) {
153    SkRect bounds;
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));
158  }
159
160  ~LayerSaver() {
161    canvas_.restore();
162  }
163
164  int x() const { return x_; }
165  int y() const { return y_; }
166  int w() const { return w_; }
167  int h() const { return h_; }
168
169  // Returns the EXCLUSIVE far bounds of the layer.
170  int right() const { return x_ + w_; }
171  int bottom() const { return y_ + h_; }
172
173 private:
174  PlatformCanvas& canvas_;
175  int x_, y_, w_, h_;
176};
177
178// Size used for making layers in many of the below tests.
179const int kLayerX = 2;
180const int kLayerY = 3;
181const int kLayerW = 9;
182const int kLayerH = 7;
183
184// Size used by some tests to draw a rectangle inside the layer.
185const int kInnerX = 4;
186const int kInnerY = 5;
187const int kInnerW = 2;
188const int kInnerH = 3;
189
190// Radius used by some tests to draw a rounded-corner rectangle.
191const SkScalar kRadius = 2.0;
192
193}
194
195// This just checks that our checking code is working properly, it just uses
196// regular skia primitives.
197TEST(PlatformCanvas, SkLayer) {
198  // Create the canvas initialized to opaque white.
199  RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true));
200  canvas->drawColor(SK_ColorWHITE);
201
202  // Make a layer and fill it completely to make sure that the bounds are
203  // correct.
204  {
205    LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
206    canvas->drawColor(SK_ColorBLACK);
207  }
208  EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH));
209}
210
211#if !defined(USE_AURA)  // http://crbug.com/154358
212
213// Test native clipping.
214TEST(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));
219
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));
225
226  // Test that intersecting disjoint clip rectangles sets an empty clip region
227  canvas->drawColor(SK_ColorWHITE);
228  EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE));
229  {
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);
234  }
235  EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE));
236}
237
238#endif  // !defined(USE_AURA)
239
240// Test the layers get filled properly by native rendering.
241TEST(PlatformCanvas, FillLayer) {
242  // Create the canvas initialized to opaque white.
243  RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true));
244
245  // Make a layer and fill it completely to make sure that the bounds are
246  // correct.
247  canvas->drawColor(SK_ColorWHITE);
248  {
249    LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
250    DrawNativeRect(*canvas, 0, 0, 100, 100);
251#if defined(OS_WIN)
252    MakeOpaque(canvas.get(), 0, 0, 100, 100);
253#endif
254  }
255  EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH));
256
257  // Make a layer and fill it partially to make sure the translation is correct.
258  canvas->drawColor(SK_ColorWHITE);
259  {
260    LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
261    DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
262#if defined(OS_WIN)
263    MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
264#endif
265  }
266  EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH));
267
268  // Add a clip on the layer and fill to make sure clip is correct.
269  canvas->drawColor(SK_ColorWHITE);
270  {
271    LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
272    canvas->save();
273    AddClip(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
274    DrawNativeRect(*canvas, 0, 0, 100, 100);
275#if defined(OS_WIN)
276    MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
277#endif
278    canvas->restore();
279  }
280  EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH));
281
282  // Add a clip and then make the layer to make sure the clip is correct.
283  canvas->drawColor(SK_ColorWHITE);
284  canvas->save();
285  AddClip(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
286  {
287    LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
288    DrawNativeRect(*canvas, 0, 0, 100, 100);
289#if defined(OS_WIN)
290    MakeOpaque(canvas.get(), 0, 0, 100, 100);
291#endif
292  }
293  canvas->restore();
294  EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH));
295}
296
297#if !defined(USE_AURA)  // http://crbug.com/154358
298
299// Test that translation + make layer works properly.
300TEST(PlatformCanvas, TranslateLayer) {
301  // Create the canvas initialized to opaque white.
302  RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true));
303
304  // Make a layer and fill it completely to make sure that the bounds are
305  // correct.
306  canvas->drawColor(SK_ColorWHITE);
307  canvas->save();
308  canvas->translate(1, 1);
309  {
310    LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
311    DrawNativeRect(*canvas, 0, 0, 100, 100);
312#if defined(OS_WIN)
313    MakeOpaque(canvas.get(), 0, 0, 100, 100);
314#endif
315  }
316  canvas->restore();
317  EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX + 1, kLayerY + 1,
318                              kLayerW, kLayerH));
319
320  // Translate then make the layer.
321  canvas->drawColor(SK_ColorWHITE);
322  canvas->save();
323  canvas->translate(1, 1);
324  {
325    LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
326    DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
327#if defined(OS_WIN)
328    MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
329#endif
330  }
331  canvas->restore();
332  EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 1, kInnerY + 1,
333                              kInnerW, kInnerH));
334
335  // Make the layer then translate.
336  canvas->drawColor(SK_ColorWHITE);
337  canvas->save();
338  {
339    LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
340    canvas->translate(1, 1);
341    DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
342#if defined(OS_WIN)
343    MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
344#endif
345  }
346  canvas->restore();
347  EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 1, kInnerY + 1,
348                              kInnerW, kInnerH));
349
350  // Translate both before and after, and have a clip.
351  canvas->drawColor(SK_ColorWHITE);
352  canvas->save();
353  canvas->translate(1, 1);
354  {
355    LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
356    canvas->drawColor(SK_ColorWHITE);
357    canvas->translate(1, 1);
358    AddClip(*canvas, kInnerX + 1, kInnerY + 1, kInnerW - 1, kInnerH - 1);
359    DrawNativeRect(*canvas, 0, 0, 100, 100);
360#if defined(OS_WIN)
361    MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH);
362#endif
363  }
364  canvas->restore();
365  EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 3, kInnerY + 3,
366                              kInnerW - 1, kInnerH - 1));
367
368// TODO(dglazkov): Figure out why this fails on Mac (antialiased clipping?),
369// modify test and remove this guard.
370#if !defined(OS_MACOSX)
371  // Translate both before and after, and have a path clip.
372  canvas->drawColor(SK_ColorWHITE);
373  canvas->save();
374  canvas->translate(1, 1);
375  {
376    LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
377    canvas->drawColor(SK_ColorWHITE);
378    canvas->translate(1, 1);
379
380    SkPath path;
381    SkRect rect;
382    rect.iset(kInnerX - 1, kInnerY - 1,
383              kInnerX + kInnerW, kInnerY + kInnerH);
384    path.addRoundRect(rect, kRadius, kRadius);
385    canvas->clipPath(path);
386
387    DrawNativeRect(*canvas, 0, 0, 100, 100);
388#if defined(OS_WIN)
389    MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH);
390#endif
391  }
392  canvas->restore();
393  EXPECT_TRUE(VerifyRoundedRect(*canvas, SK_ColorWHITE, SK_ColorBLACK,
394                                kInnerX + 1, kInnerY + 1, kInnerW, kInnerH));
395#endif
396}
397
398#endif  // #if !defined(USE_AURA)
399
400TEST(PlatformBitmapTest, PlatformBitmap) {
401  const int kWidth = 400;
402  const int kHeight = 300;
403  scoped_ptr<PlatformBitmap> platform_bitmap(new PlatformBitmap);
404
405  EXPECT_TRUE(0 == platform_bitmap->GetSurface());
406  EXPECT_TRUE(platform_bitmap->GetBitmap().empty());
407  EXPECT_TRUE(platform_bitmap->GetBitmap().isNull());
408
409  EXPECT_TRUE(platform_bitmap->Allocate(kWidth, kHeight, /*is_opaque=*/false));
410
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(SkBitmap::kARGB_8888_Config,  // Same for all platforms.
419            platform_bitmap->GetBitmap().config());
420  EXPECT_TRUE(platform_bitmap->GetBitmap().lockPixelsAreWritable());
421  EXPECT_TRUE(platform_bitmap->GetBitmap().pixelRef()->isLocked());
422  EXPECT_EQ(1, platform_bitmap->GetBitmap().pixelRef()->getRefCnt());
423
424  *(platform_bitmap->GetBitmap().getAddr32(10, 20)) = 0xDEED1020;
425  *(platform_bitmap->GetBitmap().getAddr32(20, 30)) = 0xDEED2030;
426
427  SkBitmap sk_bitmap = platform_bitmap->GetBitmap();
428  sk_bitmap.lockPixels();
429
430  EXPECT_EQ(2, platform_bitmap->GetBitmap().pixelRef()->getRefCnt());
431  EXPECT_EQ(2, sk_bitmap.pixelRef()->getRefCnt());
432
433  EXPECT_EQ(0xDEED1020, *sk_bitmap.getAddr32(10, 20));
434  EXPECT_EQ(0xDEED2030, *sk_bitmap.getAddr32(20, 30));
435
436  *(platform_bitmap->GetBitmap().getAddr32(30, 40)) = 0xDEED3040;
437
438  // The SkBitmaps derived from a PlatformBitmap must be capable of outliving
439  // the PlatformBitmap.
440  platform_bitmap.reset();
441
442  EXPECT_EQ(1, sk_bitmap.pixelRef()->getRefCnt());
443
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();
448
449  EXPECT_EQ(NULL, sk_bitmap.getPixels());
450
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();
456}
457
458
459}  // namespace skia
460