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 "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "skia/ext/platform_device.h" | 14 #include "skia/ext/platform_device.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 #include "third_party/skia/include/core/SkCanvas.h" |
17 #include "third_party/skia/include/core/SkColor.h" | 18 #include "third_party/skia/include/core/SkColor.h" |
18 #include "third_party/skia/include/core/SkColorPriv.h" | 19 #include "third_party/skia/include/core/SkColorPriv.h" |
19 #include "third_party/skia/include/core/SkPixelRef.h" | 20 #include "third_party/skia/include/core/SkPixelRef.h" |
20 | 21 |
21 #if defined(OS_MACOSX) | 22 #if defined(OS_MACOSX) |
22 #import <ApplicationServices/ApplicationServices.h> | 23 #import <ApplicationServices/ApplicationServices.h> |
23 #endif | 24 #endif |
24 | 25 |
25 #if !defined(OS_WIN) | 26 #if !defined(OS_WIN) |
26 #include <unistd.h> | 27 #include <unistd.h> |
27 #endif | 28 #endif |
28 | 29 |
29 namespace skia { | 30 namespace skia { |
30 | 31 |
31 namespace { | 32 namespace { |
32 | 33 |
33 bool IsOfColor(const SkBitmap& bitmap, int x, int y, uint32_t color) { | 34 bool IsOfColor(const SkBitmap& bitmap, int x, int y, uint32_t color) { |
34 // For masking out the alpha values. | 35 // For masking out the alpha values. |
35 static uint32_t alpha_mask = | 36 static uint32_t alpha_mask = |
36 static_cast<uint32_t>(SK_A32_MASK) << SK_A32_SHIFT; | 37 static_cast<uint32_t>(SK_A32_MASK) << SK_A32_SHIFT; |
37 return (*bitmap.getAddr32(x, y) | alpha_mask) == (color | alpha_mask); | 38 return (*bitmap.getAddr32(x, y) | alpha_mask) == (color | alpha_mask); |
38 } | 39 } |
39 | 40 |
40 // Return true if the canvas is filled to canvas_color, and contains a single | 41 // Return true if the canvas is filled to canvas_color, and contains a single |
41 // rectangle filled to rect_color. This function ignores the alpha channel, | 42 // rectangle filled to rect_color. This function ignores the alpha channel, |
42 // since Windows will sometimes clear the alpha channel when drawing, and we | 43 // since Windows will sometimes clear the alpha channel when drawing, and we |
43 // will fix that up later in cases it's necessary. | 44 // will fix that up later in cases it's necessary. |
44 bool VerifyRect(const PlatformCanvas& canvas, | 45 bool VerifyRect(const SkCanvas& canvas, |
45 uint32_t canvas_color, uint32_t rect_color, | 46 uint32_t canvas_color, uint32_t rect_color, |
46 int x, int y, int w, int h) { | 47 int x, int y, int w, int h) { |
47 const SkBitmap bitmap = skia::ReadPixels(const_cast<PlatformCanvas*>(&canvas))
; | 48 const SkBitmap bitmap = skia::ReadPixels(const_cast<SkCanvas*>(&canvas)); |
48 SkAutoLockPixels lock(bitmap); | 49 SkAutoLockPixels lock(bitmap); |
49 | 50 |
50 for (int cur_y = 0; cur_y < bitmap.height(); cur_y++) { | 51 for (int cur_y = 0; cur_y < bitmap.height(); cur_y++) { |
51 for (int cur_x = 0; cur_x < bitmap.width(); cur_x++) { | 52 for (int cur_x = 0; cur_x < bitmap.width(); cur_x++) { |
52 if (cur_x >= x && cur_x < x + w && | 53 if (cur_x >= x && cur_x < x + w && |
53 cur_y >= y && cur_y < y + h) { | 54 cur_y >= y && cur_y < y + h) { |
54 // Inside the square should be rect_color | 55 // Inside the square should be rect_color |
55 if (!IsOfColor(bitmap, cur_x, cur_y, rect_color)) | 56 if (!IsOfColor(bitmap, cur_x, cur_y, rect_color)) |
56 return false; | 57 return false; |
57 } else { | 58 } else { |
58 // Outside the square should be canvas_color | 59 // Outside the square should be canvas_color |
59 if (!IsOfColor(bitmap, cur_x, cur_y, canvas_color)) | 60 if (!IsOfColor(bitmap, cur_x, cur_y, canvas_color)) |
60 return false; | 61 return false; |
61 } | 62 } |
62 } | 63 } |
63 } | 64 } |
64 return true; | 65 return true; |
65 } | 66 } |
66 | 67 |
67 #if !defined(USE_AURA) && !defined(OS_MACOSX) | 68 #if !defined(USE_AURA) && !defined(OS_MACOSX) |
68 // Return true if canvas has something that passes for a rounded-corner | 69 // Return true if canvas has something that passes for a rounded-corner |
69 // rectangle. Basically, we're just checking to make sure that the pixels in the | 70 // rectangle. Basically, we're just checking to make sure that the pixels in the |
70 // middle are of rect_color and pixels in the corners are of canvas_color. | 71 // middle are of rect_color and pixels in the corners are of canvas_color. |
71 bool VerifyRoundedRect(const PlatformCanvas& canvas, | 72 bool VerifyRoundedRect(const SkCanvas& canvas, |
72 uint32_t canvas_color, | 73 uint32_t canvas_color, |
73 uint32_t rect_color, | 74 uint32_t rect_color, |
74 int x, | 75 int x, |
75 int y, | 76 int y, |
76 int w, | 77 int w, |
77 int h) { | 78 int h) { |
78 SkBaseDevice* device = skia::GetTopDevice(canvas); | 79 SkBaseDevice* device = skia::GetTopDevice(canvas); |
79 const SkBitmap& bitmap = device->accessBitmap(false); | 80 const SkBitmap& bitmap = device->accessBitmap(false); |
80 SkAutoLockPixels lock(bitmap); | 81 SkAutoLockPixels lock(bitmap); |
81 | 82 |
82 // Check corner points first. They should be of canvas_color. | 83 // Check corner points first. They should be of canvas_color. |
83 if (!IsOfColor(bitmap, x, y, canvas_color)) return false; | 84 if (!IsOfColor(bitmap, x, y, canvas_color)) return false; |
84 if (!IsOfColor(bitmap, x + w, y, canvas_color)) return false; | 85 if (!IsOfColor(bitmap, x + w, y, canvas_color)) return false; |
85 if (!IsOfColor(bitmap, x, y + h, canvas_color)) return false; | 86 if (!IsOfColor(bitmap, x, y + h, canvas_color)) return false; |
86 if (!IsOfColor(bitmap, x + w, y, canvas_color)) return false; | 87 if (!IsOfColor(bitmap, x + w, y, canvas_color)) return false; |
87 | 88 |
88 // Check middle points. They should be of rect_color. | 89 // Check middle points. They should be of rect_color. |
89 if (!IsOfColor(bitmap, (x + w / 2), y, rect_color)) return false; | 90 if (!IsOfColor(bitmap, (x + w / 2), y, rect_color)) return false; |
90 if (!IsOfColor(bitmap, x, (y + h / 2), rect_color)) return false; | 91 if (!IsOfColor(bitmap, x, (y + h / 2), rect_color)) return false; |
91 if (!IsOfColor(bitmap, x + w, (y + h / 2), rect_color)) return false; | 92 if (!IsOfColor(bitmap, x + w, (y + h / 2), rect_color)) return false; |
92 if (!IsOfColor(bitmap, (x + w / 2), y + h, rect_color)) return false; | 93 if (!IsOfColor(bitmap, (x + w / 2), y + h, rect_color)) return false; |
93 | 94 |
94 return true; | 95 return true; |
95 } | 96 } |
96 #endif | 97 #endif |
97 | 98 |
98 // Checks whether there is a white canvas with a black square at the given | 99 // Checks whether there is a white canvas with a black square at the given |
99 // location in pixels (not in the canvas coordinate system). | 100 // location in pixels (not in the canvas coordinate system). |
100 bool VerifyBlackRect(const PlatformCanvas& canvas, int x, int y, int w, int h) { | 101 bool VerifyBlackRect(const SkCanvas& canvas, int x, int y, int w, int h) { |
101 return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h); | 102 return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h); |
102 } | 103 } |
103 | 104 |
104 #if !defined(USE_AURA) // http://crbug.com/154358 | 105 #if !defined(USE_AURA) // http://crbug.com/154358 |
105 // Check that every pixel in the canvas is a single color. | 106 // Check that every pixel in the canvas is a single color. |
106 bool VerifyCanvasColor(const PlatformCanvas& canvas, uint32_t canvas_color) { | 107 bool VerifyCanvasColor(const SkCanvas& canvas, uint32_t canvas_color) { |
107 return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0); | 108 return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0); |
108 } | 109 } |
109 #endif // !defined(USE_AURA) | 110 #endif // !defined(USE_AURA) |
110 | 111 |
111 #if defined(OS_WIN) | 112 #if defined(OS_WIN) |
112 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) { | 113 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { |
113 skia::ScopedPlatformPaint scoped_platform_paint(&canvas); | 114 skia::ScopedPlatformPaint scoped_platform_paint(&canvas); |
114 HDC dc = scoped_platform_paint.GetPlatformSurface(); | 115 HDC dc = scoped_platform_paint.GetPlatformSurface(); |
115 | 116 |
116 RECT inner_rc; | 117 RECT inner_rc; |
117 inner_rc.left = x; | 118 inner_rc.left = x; |
118 inner_rc.top = y; | 119 inner_rc.top = y; |
119 inner_rc.right = x + w; | 120 inner_rc.right = x + w; |
120 inner_rc.bottom = y + h; | 121 inner_rc.bottom = y + h; |
121 FillRect(dc, &inner_rc, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)))
; | 122 FillRect(dc, &inner_rc, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)))
; |
122 } | 123 } |
123 #elif defined(OS_MACOSX) | 124 #elif defined(OS_MACOSX) |
124 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) { | 125 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { |
125 skia::ScopedPlatformPaint scoped_platform_paint(&canvas); | 126 skia::ScopedPlatformPaint scoped_platform_paint(&canvas); |
126 CGContextRef context = scoped_platform_paint.GetPlatformSurface(); | 127 CGContextRef context = scoped_platform_paint.GetPlatformSurface(); |
127 | 128 |
128 CGRect inner_rc = CGRectMake(x, y, w, h); | 129 CGRect inner_rc = CGRectMake(x, y, w, h); |
129 // RGBA opaque black | 130 // RGBA opaque black |
130 CGColorRef black = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0); | 131 CGColorRef black = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0); |
131 CGContextSetFillColorWithColor(context, black); | 132 CGContextSetFillColorWithColor(context, black); |
132 CGColorRelease(black); | 133 CGColorRelease(black); |
133 CGContextFillRect(context, inner_rc); | 134 CGContextFillRect(context, inner_rc); |
134 } | 135 } |
135 #else | 136 #else |
136 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) { | 137 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { |
137 NOTIMPLEMENTED(); | 138 NOTIMPLEMENTED(); |
138 } | 139 } |
139 #endif | 140 #endif |
140 | 141 |
141 // Clips the contents of the canvas to the given rectangle. This will be | 142 // Clips the contents of the canvas to the given rectangle. This will be |
142 // intersected with any existing clip. | 143 // intersected with any existing clip. |
143 void AddClip(PlatformCanvas& canvas, int x, int y, int w, int h) { | 144 void AddClip(SkCanvas& canvas, int x, int y, int w, int h) { |
144 SkRect rect; | 145 SkRect rect; |
145 rect.set(SkIntToScalar(x), SkIntToScalar(y), | 146 rect.set(SkIntToScalar(x), SkIntToScalar(y), |
146 SkIntToScalar(x + w), SkIntToScalar(y + h)); | 147 SkIntToScalar(x + w), SkIntToScalar(y + h)); |
147 canvas.clipRect(rect); | 148 canvas.clipRect(rect); |
148 } | 149 } |
149 | 150 |
150 class LayerSaver { | 151 class LayerSaver { |
151 public: | 152 public: |
152 LayerSaver(PlatformCanvas& canvas, int x, int y, int w, int h) | 153 LayerSaver(SkCanvas& canvas, int x, int y, int w, int h) |
153 : canvas_(canvas), | 154 : canvas_(canvas), |
154 x_(x), | 155 x_(x), |
155 y_(y), | 156 y_(y), |
156 w_(w), | 157 w_(w), |
157 h_(h) { | 158 h_(h) { |
158 SkRect bounds; | 159 SkRect bounds; |
159 bounds.set(SkIntToScalar(x_), SkIntToScalar(y_), | 160 bounds.set(SkIntToScalar(x_), SkIntToScalar(y_), |
160 SkIntToScalar(right()), SkIntToScalar(bottom())); | 161 SkIntToScalar(right()), SkIntToScalar(bottom())); |
161 canvas_.saveLayer(&bounds, NULL); | 162 canvas_.saveLayer(&bounds, NULL); |
162 canvas.clear(SkColorSetARGB(0, 0, 0, 0)); | 163 canvas.clear(SkColorSetARGB(0, 0, 0, 0)); |
163 } | 164 } |
164 | 165 |
165 ~LayerSaver() { | 166 ~LayerSaver() { |
166 canvas_.restore(); | 167 canvas_.restore(); |
167 } | 168 } |
168 | 169 |
169 int x() const { return x_; } | 170 int x() const { return x_; } |
170 int y() const { return y_; } | 171 int y() const { return y_; } |
171 int w() const { return w_; } | 172 int w() const { return w_; } |
172 int h() const { return h_; } | 173 int h() const { return h_; } |
173 | 174 |
174 // Returns the EXCLUSIVE far bounds of the layer. | 175 // Returns the EXCLUSIVE far bounds of the layer. |
175 int right() const { return x_ + w_; } | 176 int right() const { return x_ + w_; } |
176 int bottom() const { return y_ + h_; } | 177 int bottom() const { return y_ + h_; } |
177 | 178 |
178 private: | 179 private: |
179 PlatformCanvas& canvas_; | 180 SkCanvas& canvas_; |
180 int x_, y_, w_, h_; | 181 int x_, y_, w_, h_; |
181 }; | 182 }; |
182 | 183 |
183 // Size used for making layers in many of the below tests. | 184 // Size used for making layers in many of the below tests. |
184 const int kLayerX = 2; | 185 const int kLayerX = 2; |
185 const int kLayerY = 3; | 186 const int kLayerY = 3; |
186 const int kLayerW = 9; | 187 const int kLayerW = 9; |
187 const int kLayerH = 7; | 188 const int kLayerH = 7; |
188 | 189 |
189 // Size used by some tests to draw a rectangle inside the layer. | 190 // Size used by some tests to draw a rectangle inside the layer. |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 } | 393 } |
393 canvas->restore(); | 394 canvas->restore(); |
394 EXPECT_TRUE(VerifyRoundedRect(*canvas, SK_ColorWHITE, SK_ColorBLACK, | 395 EXPECT_TRUE(VerifyRoundedRect(*canvas, SK_ColorWHITE, SK_ColorBLACK, |
395 kInnerX + 1, kInnerY + 1, kInnerW, kInnerH)); | 396 kInnerX + 1, kInnerY + 1, kInnerW, kInnerH)); |
396 #endif | 397 #endif |
397 } | 398 } |
398 | 399 |
399 #endif // #if !defined(USE_AURA) | 400 #endif // #if !defined(USE_AURA) |
400 | 401 |
401 } // namespace skia | 402 } // namespace skia |
OLD | NEW |