| 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 <windows.h> | |
| 11 #else | |
| 12 #include <unistd.h> | 10 #include <unistd.h> |
| 13 #endif | 11 #endif |
| 14 | 12 |
| 15 #include "skia/ext/platform_canvas.h" | 13 #include "skia/ext/platform_canvas.h" |
| 16 #include "skia/ext/platform_device.h" | 14 #include "skia/ext/platform_device.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 16 |
| 19 #include "SkColor.h" | 17 #include "SkColor.h" |
| 20 | 18 |
| 19 // TODO(maruel): Removes once notImplemented() is not necessary anymore. |
| 20 #include "NotImplemented.h" |
| 21 |
| 21 namespace skia { | 22 namespace skia { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Return true if the canvas is filled to canvas_color, | 26 // Return true if the canvas is filled to canvas_color, |
| 26 // and contains a single rectangle filled to rect_color. | 27 // and contains a single rectangle filled to rect_color. |
| 27 bool VerifyRect(const PlatformCanvas& canvas, | 28 bool VerifyRect(const PlatformCanvas& canvas, |
| 28 uint32_t canvas_color, uint32_t rect_color, | 29 uint32_t canvas_color, uint32_t rect_color, |
| 29 int x, int y, int w, int h) { | 30 int x, int y, int w, int h) { |
| 30 PlatformDevice& device = canvas.getTopPlatformDevice(); | 31 PlatformDevice& device = canvas.getTopPlatformDevice(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 43 if (*bitmap.getAddr32(cur_x, cur_y) != canvas_color) | 44 if (*bitmap.getAddr32(cur_x, cur_y) != canvas_color) |
| 44 return false; | 45 return false; |
| 45 } | 46 } |
| 46 } | 47 } |
| 47 } | 48 } |
| 48 return true; | 49 return true; |
| 49 } | 50 } |
| 50 | 51 |
| 51 // Checks whether there is a white canvas with a black square at the given | 52 // Checks whether there is a white canvas with a black square at the given |
| 52 // location in pixels (not in the canvas coordinate system). | 53 // location in pixels (not in the canvas coordinate system). |
| 53 // TODO(ericroman): rename Square to Rect | 54 bool VerifyBlackRect(const PlatformCanvas& canvas, int x, int y, int w, int h) { |
| 54 bool VerifyBlackSquare(const PlatformCanvas& canvas, int x, int y, int w, int h)
{ | |
| 55 return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h); | 55 return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Check that every pixel in the canvas is a single color. | 58 // Check that every pixel in the canvas is a single color. |
| 59 bool VerifyCanvasColor(const PlatformCanvas& canvas, uint32_t canvas_color) { | 59 bool VerifyCanvasColor(const PlatformCanvas& canvas, uint32_t canvas_color) { |
| 60 return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0); | 60 return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0); |
| 61 } | 61 } |
| 62 | 62 |
| 63 #if defined(OS_WIN) | 63 #if defined(OS_WIN) |
| 64 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) { | 64 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 81 // RGBA opaque black | 81 // RGBA opaque black |
| 82 CGColorRef black = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0); | 82 CGColorRef black = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0); |
| 83 CGContextSetFillColorWithColor(context, black); | 83 CGContextSetFillColorWithColor(context, black); |
| 84 CGColorRelease(black); | 84 CGColorRelease(black); |
| 85 CGContextFillRect(context, inner_rc); | 85 CGContextFillRect(context, inner_rc); |
| 86 | 86 |
| 87 canvas.endPlatformPaint(); | 87 canvas.endPlatformPaint(); |
| 88 } | 88 } |
| 89 #else | 89 #else |
| 90 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) { | 90 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) { |
| 91 NOTIMPLEMENTED(); | 91 notImplemented(); |
| 92 } | 92 } |
| 93 #endif | 93 #endif |
| 94 | 94 |
| 95 // Clips the contents of the canvas to the given rectangle. This will be | 95 // Clips the contents of the canvas to the given rectangle. This will be |
| 96 // intersected with any existing clip. | 96 // intersected with any existing clip. |
| 97 void AddClip(PlatformCanvas& canvas, int x, int y, int w, int h) { | 97 void AddClip(PlatformCanvas& canvas, int x, int y, int w, int h) { |
| 98 SkRect rect; | 98 SkRect rect; |
| 99 rect.set(SkIntToScalar(x), SkIntToScalar(y), | 99 rect.set(SkIntToScalar(x), SkIntToScalar(y), |
| 100 SkIntToScalar(x + w), SkIntToScalar(y + h)); | 100 SkIntToScalar(x + w), SkIntToScalar(y + h)); |
| 101 canvas.clipRect(rect); | 101 canvas.clipRect(rect); |
| 102 } | 102 } |
| 103 | 103 |
| 104 class LayerSaver { | 104 class LayerSaver { |
| 105 public: | 105 public: |
| 106 LayerSaver(PlatformCanvas& canvas, int x, int y, int w, int h) | 106 LayerSaver(PlatformCanvas& canvas, int x, int y, int w, int h) |
| 107 : canvas_(canvas), | 107 : canvas_(canvas), |
| 108 x_(x), | 108 x_(x), |
| 109 y_(y), | 109 y_(y), |
| 110 w_(w), | 110 w_(w), |
| 111 h_(h) { | 111 h_(h) { |
| 112 SkRect bounds; | 112 SkRect bounds; |
| 113 bounds.set(SkIntToScalar(x_), SkIntToScalar(y_), | 113 bounds.set(SkIntToScalar(x_), SkIntToScalar(y_), |
| 114 SkIntToScalar(right()), SkIntToScalar(bottom())); | 114 SkIntToScalar(right()), SkIntToScalar(bottom())); |
| 115 canvas_.saveLayer(&bounds, NULL); | 115 canvas_.saveLayer(&bounds, NULL); |
| 116 } | 116 } |
| 117 | 117 |
| 118 ~LayerSaver() { | 118 ~LayerSaver() { |
| 119 #if defined(OS_WIN) |
| 119 canvas_.getTopPlatformDevice().fixupAlphaBeforeCompositing(); | 120 canvas_.getTopPlatformDevice().fixupAlphaBeforeCompositing(); |
| 121 #endif |
| 120 canvas_.restore(); | 122 canvas_.restore(); |
| 121 } | 123 } |
| 122 | 124 |
| 123 int x() const { return x_; } | 125 int x() const { return x_; } |
| 124 int y() const { return y_; } | 126 int y() const { return y_; } |
| 125 int w() const { return w_; } | 127 int w() const { return w_; } |
| 126 int h() const { return h_; } | 128 int h() const { return h_; } |
| 127 | 129 |
| 128 // Returns the EXCLUSIVE far bounds of the layer. | 130 // Returns the EXCLUSIVE far bounds of the layer. |
| 129 int right() const { return x_ + w_; } | 131 int right() const { return x_ + w_; } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 154 // Create the canvas initialized to opaque white. | 156 // Create the canvas initialized to opaque white. |
| 155 PlatformCanvas canvas(16, 16, true); | 157 PlatformCanvas canvas(16, 16, true); |
| 156 canvas.drawColor(SK_ColorWHITE); | 158 canvas.drawColor(SK_ColorWHITE); |
| 157 | 159 |
| 158 // Make a layer and fill it completely to make sure that the bounds are | 160 // Make a layer and fill it completely to make sure that the bounds are |
| 159 // correct. | 161 // correct. |
| 160 { | 162 { |
| 161 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); | 163 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); |
| 162 canvas.drawColor(SK_ColorBLACK); | 164 canvas.drawColor(SK_ColorBLACK); |
| 163 } | 165 } |
| 164 EXPECT_TRUE(VerifyBlackSquare(canvas, kLayerX, kLayerY, kLayerW, kLayerH)); | 166 EXPECT_TRUE(VerifyBlackRect(canvas, kLayerX, kLayerY, kLayerW, kLayerH)); |
| 165 } | 167 } |
| 166 | 168 |
| 167 // Test native clipping. | 169 // Test native clipping. |
| 168 TEST(PlatformCanvas, ClipRegion) { | 170 TEST(PlatformCanvas, ClipRegion) { |
| 169 // Initialize a white canvas | 171 // Initialize a white canvas |
| 170 PlatformCanvas canvas(16, 16, true); | 172 PlatformCanvas canvas(16, 16, true); |
| 171 canvas.drawColor(SK_ColorWHITE); | 173 canvas.drawColor(SK_ColorWHITE); |
| 172 EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE)); | 174 EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE)); |
| 173 | 175 |
| 174 // Test that initially the canvas has no clip region, by filling it | 176 // Test that initially the canvas has no clip region, by filling it |
| 175 // with a black rectangle. | 177 // with a black rectangle. |
| 176 // Note: Don't use LayerSaver, since internally it sets a clip region. | 178 // Note: Don't use LayerSaver, since internally it sets a clip region. |
| 177 DrawNativeRect(canvas, 0, 0, 16, 16); | 179 DrawNativeRect(canvas, 0, 0, 16, 16); |
| 180 #if defined(OS_WIN) |
| 178 canvas.getTopPlatformDevice().fixupAlphaBeforeCompositing(); | 181 canvas.getTopPlatformDevice().fixupAlphaBeforeCompositing(); |
| 182 #endif |
| 179 EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorBLACK)); | 183 EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorBLACK)); |
| 180 | 184 |
| 181 // Test that intersecting disjoint clip rectangles sets an empty clip region | 185 // Test that intersecting disjoint clip rectangles sets an empty clip region |
| 182 canvas.drawColor(SK_ColorWHITE); | 186 canvas.drawColor(SK_ColorWHITE); |
| 183 EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE)); | 187 EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE)); |
| 184 { | 188 { |
| 185 LayerSaver layer(canvas, 0, 0, 16, 16); | 189 LayerSaver layer(canvas, 0, 0, 16, 16); |
| 186 AddClip(canvas, 2, 3, 4, 5); | 190 AddClip(canvas, 2, 3, 4, 5); |
| 187 AddClip(canvas, 4, 9, 10, 10); | 191 AddClip(canvas, 4, 9, 10, 10); |
| 188 DrawNativeRect(canvas, 0, 0, 16, 16); | 192 DrawNativeRect(canvas, 0, 0, 16, 16); |
| 189 } | 193 } |
| 190 EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE)); | 194 EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE)); |
| 191 } | 195 } |
| 192 | 196 |
| 193 // Test the layers get filled properly by native rendering. | 197 // Test the layers get filled properly by native rendering. |
| 194 TEST(PlatformCanvas, FillLayer) { | 198 TEST(PlatformCanvas, FillLayer) { |
| 195 // Create the canvas initialized to opaque white. | 199 // Create the canvas initialized to opaque white. |
| 196 PlatformCanvas canvas(16, 16, true); | 200 PlatformCanvas canvas(16, 16, true); |
| 197 | 201 |
| 198 // Make a layer and fill it completely to make sure that the bounds are | 202 // Make a layer and fill it completely to make sure that the bounds are |
| 199 // correct. | 203 // correct. |
| 200 canvas.drawColor(SK_ColorWHITE); | 204 canvas.drawColor(SK_ColorWHITE); |
| 201 { | 205 { |
| 202 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); | 206 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); |
| 203 DrawNativeRect(canvas, 0, 0, 100, 100); | 207 DrawNativeRect(canvas, 0, 0, 100, 100); |
| 204 } | 208 } |
| 205 EXPECT_TRUE(VerifyBlackSquare(canvas, kLayerX, kLayerY, kLayerW, kLayerH)); | 209 EXPECT_TRUE(VerifyBlackRect(canvas, kLayerX, kLayerY, kLayerW, kLayerH)); |
| 206 | 210 |
| 207 // Make a layer and fill it partially to make sure the translation is correct. | 211 // Make a layer and fill it partially to make sure the translation is correct. |
| 208 canvas.drawColor(SK_ColorWHITE); | 212 canvas.drawColor(SK_ColorWHITE); |
| 209 { | 213 { |
| 210 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); | 214 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); |
| 211 DrawNativeRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH); | 215 DrawNativeRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH); |
| 212 } | 216 } |
| 213 EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX, kInnerY, kInnerW, kInnerH)); | 217 EXPECT_TRUE(VerifyBlackRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH)); |
| 214 | 218 |
| 215 // Add a clip on the layer and fill to make sure clip is correct. | 219 // Add a clip on the layer and fill to make sure clip is correct. |
| 216 canvas.drawColor(SK_ColorWHITE); | 220 canvas.drawColor(SK_ColorWHITE); |
| 217 { | 221 { |
| 218 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); | 222 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); |
| 219 canvas.save(); | 223 canvas.save(); |
| 220 AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH); | 224 AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH); |
| 221 DrawNativeRect(canvas, 0, 0, 100, 100); | 225 DrawNativeRect(canvas, 0, 0, 100, 100); |
| 222 canvas.restore(); | 226 canvas.restore(); |
| 223 } | 227 } |
| 224 EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX, kInnerY, kInnerW, kInnerH)); | 228 EXPECT_TRUE(VerifyBlackRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH)); |
| 225 | 229 |
| 226 // Add a clip and then make the layer to make sure the clip is correct. | 230 // Add a clip and then make the layer to make sure the clip is correct. |
| 227 canvas.drawColor(SK_ColorWHITE); | 231 canvas.drawColor(SK_ColorWHITE); |
| 228 canvas.save(); | 232 canvas.save(); |
| 229 AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH); | 233 AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH); |
| 230 { | 234 { |
| 231 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); | 235 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); |
| 232 DrawNativeRect(canvas, 0, 0, 100, 100); | 236 DrawNativeRect(canvas, 0, 0, 100, 100); |
| 233 } | 237 } |
| 234 canvas.restore(); | 238 canvas.restore(); |
| 235 EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX, kInnerY, kInnerW, kInnerH)); | 239 EXPECT_TRUE(VerifyBlackRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH)); |
| 236 } | 240 } |
| 237 | 241 |
| 238 // Test that translation + make layer works properly. | 242 // Test that translation + make layer works properly. |
| 239 TEST(PlatformCanvas, TranslateLayer) { | 243 TEST(PlatformCanvas, TranslateLayer) { |
| 240 // Create the canvas initialized to opaque white. | 244 // Create the canvas initialized to opaque white. |
| 241 PlatformCanvas canvas(16, 16, true); | 245 PlatformCanvas canvas(16, 16, true); |
| 242 | 246 |
| 243 // Make a layer and fill it completely to make sure that the bounds are | 247 // Make a layer and fill it completely to make sure that the bounds are |
| 244 // correct. | 248 // correct. |
| 245 canvas.drawColor(SK_ColorWHITE); | 249 canvas.drawColor(SK_ColorWHITE); |
| 246 canvas.save(); | 250 canvas.save(); |
| 247 canvas.translate(1, 1); | 251 canvas.translate(1, 1); |
| 248 { | 252 { |
| 249 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); | 253 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); |
| 250 DrawNativeRect(canvas, 0, 0, 100, 100); | 254 DrawNativeRect(canvas, 0, 0, 100, 100); |
| 251 } | 255 } |
| 252 canvas.restore(); | 256 canvas.restore(); |
| 253 EXPECT_TRUE(VerifyBlackSquare(canvas, kLayerX + 1, kLayerY + 1, | 257 EXPECT_TRUE(VerifyBlackRect(canvas, kLayerX + 1, kLayerY + 1, |
| 254 kLayerW, kLayerH)); | 258 kLayerW, kLayerH)); |
| 255 | 259 |
| 256 // Translate then make the layer. | 260 // Translate then make the layer. |
| 257 canvas.drawColor(SK_ColorWHITE); | 261 canvas.drawColor(SK_ColorWHITE); |
| 258 canvas.save(); | 262 canvas.save(); |
| 259 canvas.translate(1, 1); | 263 canvas.translate(1, 1); |
| 260 { | 264 { |
| 261 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); | 265 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); |
| 262 DrawNativeRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH); | 266 DrawNativeRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH); |
| 263 } | 267 } |
| 264 canvas.restore(); | 268 canvas.restore(); |
| 265 EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX + 1, kInnerY + 1, | 269 EXPECT_TRUE(VerifyBlackRect(canvas, kInnerX + 1, kInnerY + 1, |
| 266 kInnerW, kInnerH)); | 270 kInnerW, kInnerH)); |
| 267 | 271 |
| 268 // Make the layer then translate. | 272 // Make the layer then translate. |
| 269 canvas.drawColor(SK_ColorWHITE); | 273 canvas.drawColor(SK_ColorWHITE); |
| 270 canvas.save(); | 274 canvas.save(); |
| 271 { | 275 { |
| 272 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); | 276 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); |
| 273 canvas.translate(1, 1); | 277 canvas.translate(1, 1); |
| 274 DrawNativeRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH); | 278 DrawNativeRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH); |
| 275 } | 279 } |
| 276 canvas.restore(); | 280 canvas.restore(); |
| 277 EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX + 1, kInnerY + 1, | 281 EXPECT_TRUE(VerifyBlackRect(canvas, kInnerX + 1, kInnerY + 1, |
| 278 kInnerW, kInnerH)); | 282 kInnerW, kInnerH)); |
| 279 | 283 |
| 280 // Translate both before and after, and have a clip. | 284 // Translate both before and after, and have a clip. |
| 281 canvas.drawColor(SK_ColorWHITE); | 285 canvas.drawColor(SK_ColorWHITE); |
| 282 canvas.save(); | 286 canvas.save(); |
| 283 canvas.translate(1, 1); | 287 canvas.translate(1, 1); |
| 284 { | 288 { |
| 285 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); | 289 LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); |
| 286 canvas.translate(1, 1); | 290 canvas.translate(1, 1); |
| 287 AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH); | 291 AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH); |
| 288 DrawNativeRect(canvas, 0, 0, 100, 100); | 292 DrawNativeRect(canvas, 0, 0, 100, 100); |
| 289 } | 293 } |
| 290 canvas.restore(); | 294 canvas.restore(); |
| 291 EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX + 2, kInnerY + 2, | 295 EXPECT_TRUE(VerifyBlackRect(canvas, kInnerX + 2, kInnerY + 2, |
| 292 kInnerW, kInnerH)); | 296 kInnerW, kInnerH)); |
| 293 } | 297 } |
| 294 | 298 |
| 295 } // namespace skia | 299 } // namespace skia |
| 296 | |
| OLD | NEW |