OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "skia/ext/platform_canvas.h" | 7 #include "skia/ext/platform_canvas.h" |
8 | 8 |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "skia/ext/platform_device.h" | 13 #include "skia/ext/platform_device.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
16 #include "third_party/skia/include/core/SkCanvas.h" | 16 #include "third_party/skia/include/core/SkCanvas.h" |
17 #include "third_party/skia/include/core/SkColor.h" | 17 #include "third_party/skia/include/core/SkColor.h" |
18 #include "third_party/skia/include/core/SkColorPriv.h" | 18 #include "third_party/skia/include/core/SkColorPriv.h" |
19 #include "third_party/skia/include/core/SkPixelRef.h" | 19 #include "third_party/skia/include/core/SkPixelRef.h" |
20 | 20 |
21 #if defined(OS_MACOSX) | 21 #if defined(OS_MACOSX) |
22 #import <ApplicationServices/ApplicationServices.h> | 22 #import <ApplicationServices/ApplicationServices.h> |
23 #endif | 23 #endif |
24 | 24 |
25 #if !defined(OS_WIN) | 25 #if !defined(OS_WIN) |
26 #include <unistd.h> | 26 #include <unistd.h> |
27 #endif | 27 #endif |
28 | 28 |
| 29 #if defined(USE_CAIRO) |
| 30 #if defined(OS_OPENBSD) |
| 31 #include <cairo.h> |
| 32 #else |
| 33 #include <cairo/cairo.h> |
| 34 #endif // OS_OPENBSD |
| 35 #endif // USE_CAIRO |
| 36 |
29 namespace skia { | 37 namespace skia { |
30 | 38 |
31 namespace { | 39 namespace { |
32 | 40 |
33 #if defined(OS_WIN) | 41 #if defined(OS_WIN) |
34 void MakeOpaque(SkCanvas* canvas, int x, int y, int width, int height) { | 42 void MakeOpaque(SkCanvas* canvas, int x, int y, int width, int height) { |
35 if (width <= 0 || height <= 0) | 43 if (width <= 0 || height <= 0) |
36 return; | 44 return; |
37 | 45 |
38 SkRect rect; | 46 SkRect rect; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 return true; | 117 return true; |
110 } | 118 } |
111 #endif | 119 #endif |
112 | 120 |
113 // Checks whether there is a white canvas with a black square at the given | 121 // Checks whether there is a white canvas with a black square at the given |
114 // location in pixels (not in the canvas coordinate system). | 122 // location in pixels (not in the canvas coordinate system). |
115 bool VerifyBlackRect(const SkCanvas& canvas, int x, int y, int w, int h) { | 123 bool VerifyBlackRect(const SkCanvas& canvas, int x, int y, int w, int h) { |
116 return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h); | 124 return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h); |
117 } | 125 } |
118 | 126 |
119 #if !defined(USE_AURA) // http://crbug.com/154358 | |
120 // Check that every pixel in the canvas is a single color. | 127 // Check that every pixel in the canvas is a single color. |
121 bool VerifyCanvasColor(const SkCanvas& canvas, uint32_t canvas_color) { | 128 bool VerifyCanvasColor(const SkCanvas& canvas, uint32_t canvas_color) { |
122 return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0); | 129 return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0); |
123 } | 130 } |
124 #endif // !defined(USE_AURA) | |
125 | 131 |
126 #if defined(OS_WIN) | 132 #if defined(OS_WIN) |
127 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { | 133 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { |
128 skia::ScopedPlatformPaint scoped_platform_paint(&canvas); | 134 skia::ScopedPlatformPaint scoped_platform_paint(&canvas); |
129 HDC dc = scoped_platform_paint.GetPlatformSurface(); | 135 HDC dc = scoped_platform_paint.GetPlatformSurface(); |
130 | 136 |
131 RECT inner_rc; | 137 RECT inner_rc; |
132 inner_rc.left = x; | 138 inner_rc.left = x; |
133 inner_rc.top = y; | 139 inner_rc.top = y; |
134 inner_rc.right = x + w; | 140 inner_rc.right = x + w; |
135 inner_rc.bottom = y + h; | 141 inner_rc.bottom = y + h; |
136 FillRect(dc, &inner_rc, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)))
; | 142 FillRect(dc, &inner_rc, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)))
; |
137 } | 143 } |
138 #elif defined(OS_MACOSX) | 144 #elif defined(OS_MACOSX) |
139 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { | 145 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { |
140 skia::ScopedPlatformPaint scoped_platform_paint(&canvas); | 146 skia::ScopedPlatformPaint scoped_platform_paint(&canvas); |
141 CGContextRef context = scoped_platform_paint.GetPlatformSurface(); | 147 CGContextRef context = scoped_platform_paint.GetPlatformSurface(); |
142 | 148 |
143 CGRect inner_rc = CGRectMake(x, y, w, h); | 149 CGRect inner_rc = CGRectMake(x, y, w, h); |
144 // RGBA opaque black | 150 // RGBA opaque black |
145 CGColorRef black = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0); | 151 CGColorRef black = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0); |
146 CGContextSetFillColorWithColor(context, black); | 152 CGContextSetFillColorWithColor(context, black); |
147 CGColorRelease(black); | 153 CGColorRelease(black); |
148 CGContextFillRect(context, inner_rc); | 154 CGContextFillRect(context, inner_rc); |
149 } | 155 } |
| 156 #elif defined(USE_CAIRO) |
| 157 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { |
| 158 skia::ScopedPlatformPaint scoped_platform_paint(&canvas); |
| 159 cairo_t* context = scoped_platform_paint.GetPlatformSurface(); |
| 160 |
| 161 cairo_rectangle(context, x, y, w, h); |
| 162 cairo_set_source_rgb(context, 0.0, 0.0, 0.0); |
| 163 cairo_fill(context); |
| 164 } |
150 #else | 165 #else |
151 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { | 166 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { |
152 NOTIMPLEMENTED(); | 167 NOTIMPLEMENTED(); |
153 } | 168 } |
154 #endif | 169 #endif |
155 | 170 |
156 // Clips the contents of the canvas to the given rectangle. This will be | 171 // Clips the contents of the canvas to the given rectangle. This will be |
157 // intersected with any existing clip. | 172 // intersected with any existing clip. |
158 void AddClip(SkCanvas& canvas, int x, int y, int w, int h) { | 173 void AddClip(SkCanvas& canvas, int x, int y, int w, int h) { |
159 SkRect rect; | 174 SkRect rect; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 | 233 |
219 // Make a layer and fill it completely to make sure that the bounds are | 234 // Make a layer and fill it completely to make sure that the bounds are |
220 // correct. | 235 // correct. |
221 { | 236 { |
222 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); | 237 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); |
223 canvas->drawColor(SK_ColorBLACK); | 238 canvas->drawColor(SK_ColorBLACK); |
224 } | 239 } |
225 EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH)); | 240 EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH)); |
226 } | 241 } |
227 | 242 |
228 #if !defined(USE_AURA) // http://crbug.com/154358 | |
229 // Test native clipping. | 243 // Test native clipping. |
230 TEST(PlatformCanvas, ClipRegion) { | 244 TEST(PlatformCanvas, ClipRegion) { |
231 // Initialize a white canvas | 245 // Initialize a white canvas |
232 sk_sp<SkCanvas> canvas(CreatePlatformCanvas(16, 16, true)); | 246 sk_sp<SkCanvas> canvas(CreatePlatformCanvas(16, 16, true)); |
233 canvas->drawColor(SK_ColorWHITE); | 247 canvas->drawColor(SK_ColorWHITE); |
234 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE)); | 248 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE)); |
235 | 249 |
236 // Test that initially the canvas has no clip region, by filling it | 250 // Test that initially the canvas has no clip region, by filling it |
237 // with a black rectangle. | 251 // with a black rectangle. |
238 // Note: Don't use LayerSaver, since internally it sets a clip region. | 252 // Note: Don't use LayerSaver, since internally it sets a clip region. |
239 DrawNativeRect(*canvas, 0, 0, 16, 16); | 253 DrawNativeRect(*canvas, 0, 0, 16, 16); |
240 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorBLACK)); | 254 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorBLACK)); |
241 | 255 |
242 // Test that intersecting disjoint clip rectangles sets an empty clip region | 256 // Test that intersecting disjoint clip rectangles sets an empty clip region |
243 canvas->drawColor(SK_ColorWHITE); | 257 canvas->drawColor(SK_ColorWHITE); |
244 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE)); | 258 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE)); |
245 { | 259 { |
246 LayerSaver layer(*canvas, 0, 0, 16, 16); | 260 LayerSaver layer(*canvas, 0, 0, 16, 16); |
247 AddClip(*canvas, 2, 3, 4, 5); | 261 AddClip(*canvas, 2, 3, 4, 5); |
248 AddClip(*canvas, 4, 9, 10, 10); | 262 AddClip(*canvas, 4, 9, 10, 10); |
249 DrawNativeRect(*canvas, 0, 0, 16, 16); | 263 DrawNativeRect(*canvas, 0, 0, 16, 16); |
250 } | 264 } |
251 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE)); | 265 EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE)); |
252 } | 266 } |
253 #endif // !defined(USE_AURA) | |
254 | 267 |
255 // Test the layers get filled properly by native rendering. | 268 // Test the layers get filled properly by native rendering. |
256 TEST(PlatformCanvas, FillLayer) { | 269 TEST(PlatformCanvas, FillLayer) { |
257 // Create the canvas initialized to opaque white. | 270 // Create the canvas initialized to opaque white. |
258 sk_sp<SkCanvas> canvas(CreatePlatformCanvas(16, 16, true)); | 271 sk_sp<SkCanvas> canvas(CreatePlatformCanvas(16, 16, true)); |
259 | 272 |
260 // Make a layer and fill it completely to make sure that the bounds are | 273 // Make a layer and fill it completely to make sure that the bounds are |
261 // correct. | 274 // correct. |
262 canvas->drawColor(SK_ColorWHITE); | 275 canvas->drawColor(SK_ColorWHITE); |
263 { | 276 { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 } | 420 } |
408 canvas->restore(); | 421 canvas->restore(); |
409 EXPECT_TRUE(VerifyRoundedRect(*canvas, SK_ColorWHITE, SK_ColorBLACK, | 422 EXPECT_TRUE(VerifyRoundedRect(*canvas, SK_ColorWHITE, SK_ColorBLACK, |
410 kInnerX + 1, kInnerY + 1, kInnerW, kInnerH)); | 423 kInnerX + 1, kInnerY + 1, kInnerW, kInnerH)); |
411 #endif | 424 #endif |
412 } | 425 } |
413 | 426 |
414 #endif // #if !defined(USE_AURA) | 427 #endif // #if !defined(USE_AURA) |
415 | 428 |
416 } // namespace skia | 429 } // namespace skia |
OLD | NEW |