| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // TODO(awalker): clean up the const/non-const reference handling in this test | 5 // TODO(awalker): clean up the const/non-const reference handling in this test |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #if !defined(OS_WIN) | 9 #if !defined(OS_WIN) |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 // Outside the square should be canvas_color | 49 // Outside the square should be canvas_color |
| 50 if ((*bitmap.getAddr32(cur_x, cur_y) | alpha_mask) != | 50 if ((*bitmap.getAddr32(cur_x, cur_y) | alpha_mask) != |
| 51 (canvas_color | alpha_mask)) | 51 (canvas_color | alpha_mask)) |
| 52 return false; | 52 return false; |
| 53 } | 53 } |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 return true; | 56 return true; |
| 57 } | 57 } |
| 58 | 58 |
| 59 bool IsOfColor(const SkBitmap& bitmap, int x, int y, uint32_t color) { |
| 60 // For masking out the alpha values. |
| 61 static uint32_t alpha_mask = 0xFF << SK_A32_SHIFT; |
| 62 return (*bitmap.getAddr32(x, y) | alpha_mask) == (color | alpha_mask); |
| 63 } |
| 64 |
| 65 // Return true if canvas has something that passes for a rounded-corner |
| 66 // rectangle. Basically, we're just checking to make sure that the pixels in the |
| 67 // middle are of rect_color and pixels in the corners are of canvas_color. |
| 68 bool VerifyRoundedRect(const PlatformCanvas& canvas, |
| 69 uint32_t canvas_color, uint32_t rect_color, |
| 70 int x, int y, int w, int h) { |
| 71 PlatformDevice& device = canvas.getTopPlatformDevice(); |
| 72 const SkBitmap& bitmap = device.accessBitmap(false); |
| 73 SkAutoLockPixels lock(bitmap); |
| 74 |
| 75 // Check corner points first. They should be of canvas_color. |
| 76 if (!IsOfColor(bitmap, x, y, canvas_color)) return false; |
| 77 if (!IsOfColor(bitmap, x + w, y, canvas_color)) return false; |
| 78 if (!IsOfColor(bitmap, x, y + h, canvas_color)) return false; |
| 79 if (!IsOfColor(bitmap, x + w, y, canvas_color)) return false; |
| 80 |
| 81 // Check middle points. They should be of rect_color. |
| 82 if (!IsOfColor(bitmap, (x + w / 2), y, rect_color)) return false; |
| 83 if (!IsOfColor(bitmap, x, (y + h / 2), rect_color)) return false; |
| 84 if (!IsOfColor(bitmap, x + w, (y + h / 2), rect_color)) return false; |
| 85 if (!IsOfColor(bitmap, (x + w / 2), y + h, rect_color)) return false; |
| 86 |
| 87 return true; |
| 88 } |
| 89 |
| 59 // Checks whether there is a white canvas with a black square at the given | 90 // Checks whether there is a white canvas with a black square at the given |
| 60 // location in pixels (not in the canvas coordinate system). | 91 // location in pixels (not in the canvas coordinate system). |
| 61 bool VerifyBlackRect(const PlatformCanvas& canvas, int x, int y, int w, int h) { | 92 bool VerifyBlackRect(const PlatformCanvas& canvas, int x, int y, int w, int h) { |
| 62 return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h); | 93 return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h); |
| 63 } | 94 } |
| 64 | 95 |
| 65 // Check that every pixel in the canvas is a single color. | 96 // Check that every pixel in the canvas is a single color. |
| 66 bool VerifyCanvasColor(const PlatformCanvas& canvas, uint32_t canvas_color) { | 97 bool VerifyCanvasColor(const PlatformCanvas& canvas, uint32_t canvas_color) { |
| 67 return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0); | 98 return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0); |
| 68 } | 99 } |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 const int kLayerY = 3; | 176 const int kLayerY = 3; |
| 146 const int kLayerW = 9; | 177 const int kLayerW = 9; |
| 147 const int kLayerH = 7; | 178 const int kLayerH = 7; |
| 148 | 179 |
| 149 // Size used by some tests to draw a rectangle inside the layer. | 180 // Size used by some tests to draw a rectangle inside the layer. |
| 150 const int kInnerX = 4; | 181 const int kInnerX = 4; |
| 151 const int kInnerY = 5; | 182 const int kInnerY = 5; |
| 152 const int kInnerW = 2; | 183 const int kInnerW = 2; |
| 153 const int kInnerH = 3; | 184 const int kInnerH = 3; |
| 154 | 185 |
| 186 // Radius used by some tests to draw a rounded-corner rectangle. |
| 187 const SkScalar kRadius = 2.0; |
| 188 |
| 155 } | 189 } |
| 156 | 190 |
| 157 // This just checks that our checking code is working properly, it just uses | 191 // This just checks that our checking code is working properly, it just uses |
| 158 // regular skia primitives. | 192 // regular skia primitives. |
| 159 TEST(PlatformCanvas, SkLayer) { | 193 TEST(PlatformCanvas, SkLayer) { |
| 160 // Create the canvas initialized to opaque white. | 194 // Create the canvas initialized to opaque white. |
| 161 PlatformCanvas canvas(16, 16, true); | 195 PlatformCanvas canvas(16, 16, true); |
| 162 canvas.drawColor(SK_ColorWHITE); | 196 canvas.drawColor(SK_ColorWHITE); |
| 163 | 197 |
| 164 // Make a layer and fill it completely to make sure that the bounds are | 198 // Make a layer and fill it completely to make sure that the bounds are |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 canvas.restore(); | 340 canvas.restore(); |
| 307 EXPECT_TRUE(VerifyBlackRect(canvas, kInnerX + 1, kInnerY + 1, | 341 EXPECT_TRUE(VerifyBlackRect(canvas, kInnerX + 1, kInnerY + 1, |
| 308 kInnerW, kInnerH)); | 342 kInnerW, kInnerH)); |
| 309 | 343 |
| 310 // Translate both before and after, and have a clip. | 344 // Translate both before and after, and have a clip. |
| 311 canvas.drawColor(SK_ColorWHITE); | 345 canvas.drawColor(SK_ColorWHITE); |
| 312 canvas.save(); | 346 canvas.save(); |
| 313 canvas.translate(1, 1); | 347 canvas.translate(1, 1); |
| 314 { | 348 { |
| 315 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); | 349 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); |
| 350 canvas.drawColor(SK_ColorWHITE); |
| 316 canvas.translate(1, 1); | 351 canvas.translate(1, 1); |
| 317 AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH); | 352 AddClip(canvas, kInnerX + 1, kInnerY + 1, kInnerW - 1, kInnerH - 1); |
| 318 DrawNativeRect(canvas, 0, 0, 100, 100); | 353 DrawNativeRect(canvas, 0, 0, 100, 100); |
| 319 #if defined(OS_WIN) | 354 #if defined(OS_WIN) |
| 320 canvas.getTopPlatformDevice().makeOpaque(kInnerX, kInnerY, | 355 canvas.getTopPlatformDevice().makeOpaque(kLayerX, kLayerY, |
| 321 kInnerW, kInnerH); | 356 kLayerW, kLayerH); |
| 322 #endif | 357 #endif |
| 323 } | 358 } |
| 324 canvas.restore(); | 359 canvas.restore(); |
| 325 EXPECT_TRUE(VerifyBlackRect(canvas, kInnerX + 2, kInnerY + 2, | 360 EXPECT_TRUE(VerifyBlackRect(canvas, kInnerX + 3, kInnerY + 3, |
| 326 kInnerW, kInnerH)); | 361 kInnerW - 1, kInnerH - 1)); |
| 362 |
| 363 // Translate both before and after, and have a path clip. |
| 364 canvas.drawColor(SK_ColorWHITE); |
| 365 canvas.save(); |
| 366 canvas.translate(1, 1); |
| 367 { |
| 368 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); |
| 369 canvas.drawColor(SK_ColorWHITE); |
| 370 canvas.translate(1, 1); |
| 371 |
| 372 SkPath path; |
| 373 SkRect rect; |
| 374 rect.iset(kInnerX - 1, kInnerY - 1, |
| 375 kInnerX + kInnerW, kInnerY + kInnerH); |
| 376 path.addRoundRect(rect, kRadius, kRadius); |
| 377 canvas.clipPath(path); |
| 378 |
| 379 DrawNativeRect(canvas, 0, 0, 100, 100); |
| 380 #if defined(OS_WIN) |
| 381 canvas.getTopPlatformDevice().makeOpaque(kLayerX, kLayerY, |
| 382 kLayerW, kLayerH); |
| 383 #endif |
| 384 } |
| 385 canvas.restore(); |
| 386 EXPECT_TRUE(VerifyRoundedRect(canvas, SK_ColorWHITE, SK_ColorBLACK, |
| 387 kInnerX + 1, kInnerY + 1, kInnerW, kInnerH)); |
| 327 } | 388 } |
| 328 | 389 |
| 329 } // namespace skia | 390 } // namespace skia |
| OLD | NEW |