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/SkColorPriv.h" 24 #include "third_party/skia/include/core/SkPixelRef.h" 25 26 namespace skia { 27 28 namespace { 29 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); 35 } 36 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); 47 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; 59 } 60 } 61 } 62 return true; 63 } 64 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); 75 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; 81 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; 87 88 return true; 89 } 90 #endif 91 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); 96 } 97 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); 101 } 102 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(); 107 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))); 114 } 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(); 119 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); 126 } 127 #else 128 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) { 129 notImplemented(); 130 } 131 #endif 132 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); 140 } 141 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)); 155 } 156 157 ~LayerSaver() { 158 canvas_.restore(); 159 } 160 161 int x() const { return x_; } 162 int y() const { return y_; } 163 int w() const { return w_; } 164 int h() const { return h_; } 165 166 // Returns the EXCLUSIVE far bounds of the layer. 167 int right() const { return x_ + w_; } 168 int bottom() const { return y_ + h_; } 169 170 private: 171 PlatformCanvas& canvas_; 172 int x_, y_, w_, h_; 173 }; 174 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; 180 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; 186 187 } 188 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); 195 196 // Make a layer and fill it completely to make sure that the bounds are 197 // correct. 198 { 199 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); 200 canvas->drawColor(SK_ColorBLACK); 201 } 202 EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH)); 203 } 204 205 #if !defined(USE_AURA) // http://crbug.com/154358 206 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)); 213 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)); 219 220 // Test that intersecting disjoint clip rectangles sets an empty clip region 221 canvas->drawColor(SK_ColorWHITE); 222 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE)); 223 { 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); 228 } 229 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE)); 230 } 231 232 #endif // !defined(USE_AURA) 233 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)); 238 239 // Make a layer and fill it completely to make sure that the bounds are 240 // correct. 241 canvas->drawColor(SK_ColorWHITE); 242 { 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 248 } 249 EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH)); 250 251 // Make a layer and fill it partially to make sure the translation is correct. 252 canvas->drawColor(SK_ColorWHITE); 253 { 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 259 } 260 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH)); 261 262 // Add a clip on the layer and fill to make sure clip is correct. 263 canvas->drawColor(SK_ColorWHITE); 264 { 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(); 273 } 274 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH)); 275 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); 280 { 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 286 } 287 canvas->restore(); 288 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH)); 289 } 290 291 #if !defined(USE_AURA) // http://crbug.com/154358 292 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)); 297 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); 303 { 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 309 } 310 canvas->restore(); 311 EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX + 1, kLayerY + 1, 312 kLayerW, kLayerH)); 313 314 // Translate then make the layer. 315 canvas->drawColor(SK_ColorWHITE); 316 canvas->save(); 317 canvas->translate(1, 1); 318 { 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 324 } 325 canvas->restore(); 326 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 1, kInnerY + 1, 327 kInnerW, kInnerH)); 328 329 // Make the layer then translate. 330 canvas->drawColor(SK_ColorWHITE); 331 canvas->save(); 332 { 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 339 } 340 canvas->restore(); 341 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 1, kInnerY + 1, 342 kInnerW, kInnerH)); 343 344 // Translate both before and after, and have a clip. 345 canvas->drawColor(SK_ColorWHITE); 346 canvas->save(); 347 canvas->translate(1, 1); 348 { 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 357 } 358 canvas->restore(); 359 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 3, kInnerY + 3, 360 kInnerW - 1, kInnerH - 1)); 361 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); 369 { 370 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); 371 canvas->drawColor(SK_ColorWHITE); 372 canvas->translate(1, 1); 373 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); 381 382 DrawNativeRect(*canvas, 0, 0, 100, 100); 383 #if defined(OS_WIN) 384 MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH); 385 #endif 386 } 387 canvas->restore(); 388 EXPECT_TRUE(VerifyRoundedRect(*canvas, SK_ColorWHITE, SK_ColorBLACK, 389 kInnerX + 1, kInnerY + 1, kInnerW, kInnerH)); 390 #endif 391 } 392 393 #endif // #if !defined(USE_AURA) 394 395 TEST(PlatformBitmapTest, PlatformBitmap) { 396 const int kWidth = 400; 397 const int kHeight = 300; 398 scoped_ptr<PlatformBitmap> platform_bitmap(new PlatformBitmap); 399 400 EXPECT_TRUE(0 == platform_bitmap->GetSurface()); 401 EXPECT_TRUE(platform_bitmap->GetBitmap().empty()); 402 EXPECT_TRUE(platform_bitmap->GetBitmap().isNull()); 403 404 EXPECT_TRUE(platform_bitmap->Allocate(kWidth, kHeight, /*is_opaque=*/false)); 405 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()); 418 419 *(platform_bitmap->GetBitmap().getAddr32(10, 20)) = 0xDEED1020; 420 *(platform_bitmap->GetBitmap().getAddr32(20, 30)) = 0xDEED2030; 421 422 SkBitmap sk_bitmap = platform_bitmap->GetBitmap(); 423 sk_bitmap.lockPixels(); 424 425 EXPECT_EQ(2, platform_bitmap->GetBitmap().pixelRef()->getRefCnt()); 426 EXPECT_EQ(2, sk_bitmap.pixelRef()->getRefCnt()); 427 428 EXPECT_EQ(0xDEED1020, *sk_bitmap.getAddr32(10, 20)); 429 EXPECT_EQ(0xDEED2030, *sk_bitmap.getAddr32(20, 30)); 430 431 *(platform_bitmap->GetBitmap().getAddr32(30, 40)) = 0xDEED3040; 432 433 // The SkBitmaps derived from a PlatformBitmap must be capable of outliving 434 // the PlatformBitmap. 435 platform_bitmap.reset(); 436 437 EXPECT_EQ(1, sk_bitmap.pixelRef()->getRefCnt()); 438 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(); 443 444 EXPECT_EQ(NULL, sk_bitmap.getPixels()); 445 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(); 451 } 452 453 454 } // namespace skia 455