|
1 /* |
|
2 * Copyright (C) 2010 Google Inc. All rights reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions are |
|
6 * met: |
|
7 * |
|
8 * * Redistributions of source code must retain the above copyright |
|
9 * notice, this list of conditions and the following disclaimer. |
|
10 * * Redistributions in binary form must reproduce the above |
|
11 * copyright notice, this list of conditions and the following disclaimer |
|
12 * in the documentation and/or other materials provided with the |
|
13 * distribution. |
|
14 * * Neither the name of Google Inc. nor the names of its |
|
15 * contributors may be used to endorse or promote products derived from |
|
16 * this software without specific prior written permission. |
|
17 * |
|
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 */ |
|
30 |
|
31 #include "config.h" |
|
32 #include "TransparencyWin.h" |
|
33 |
|
34 #include "AffineTransform.h" |
|
35 #include "ImageBuffer.h" |
|
36 |
|
37 #include <gtest/gtest.h> |
|
38 #include <windows.h> |
|
39 |
|
40 namespace WebCore { |
|
41 |
|
42 static FloatRect RECTToFloatRect(const RECT* rect) |
|
43 { |
|
44 return FloatRect(static_cast<float>(rect->left), |
|
45 static_cast<float>(rect->top), |
|
46 static_cast<float>(rect->right - rect->left), |
|
47 static_cast<float>(rect->bottom - rect->top)); |
|
48 } |
|
49 |
|
50 static void drawNativeRect(GraphicsContext* context, |
|
51 int x, int y, int w, int h) |
|
52 { |
|
53 skia::PlatformCanvas* canvas = context->platformContext()->canvas(); |
|
54 HDC dc = canvas->beginPlatformPaint(); |
|
55 |
|
56 RECT innerRc; |
|
57 innerRc.left = x; |
|
58 innerRc.top = y; |
|
59 innerRc.right = x + w; |
|
60 innerRc.bottom = y + h; |
|
61 FillRect(dc, &innerRc, |
|
62 reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH))); |
|
63 |
|
64 canvas->endPlatformPaint(); |
|
65 } |
|
66 |
|
67 static Color getPixelAt(GraphicsContext* context, int x, int y) |
|
68 { |
|
69 const SkBitmap& bitmap = context->platformContext()->canvas()-> |
|
70 getTopPlatformDevice().accessBitmap(false); |
|
71 return Color(*reinterpret_cast<const RGBA32*>(bitmap.getAddr32(x, y))); |
|
72 } |
|
73 |
|
74 // Resets the top layer's alpha channel to 0 for each pixel. This simulates |
|
75 // Windows messing it up. |
|
76 static void clearTopLayerAlphaChannel(GraphicsContext* context) |
|
77 { |
|
78 SkBitmap& bitmap = const_cast<SkBitmap&>(context->platformContext()-> |
|
79 canvas()->getTopPlatformDevice().accessBitmap(false)); |
|
80 for (int y = 0; y < bitmap.height(); y++) { |
|
81 uint32_t* row = bitmap.getAddr32(0, y); |
|
82 for (int x = 0; x < bitmap.width(); x++) |
|
83 row[x] &= 0x00FFFFFF; |
|
84 } |
|
85 } |
|
86 |
|
87 // Clears the alpha channel on the specified pixel. |
|
88 static void clearTopLayerAlphaPixel(GraphicsContext* context, int x, int y) |
|
89 { |
|
90 SkBitmap& bitmap = const_cast<SkBitmap&>(context->platformContext()-> |
|
91 canvas()->getTopPlatformDevice().accessBitmap(false)); |
|
92 *bitmap.getAddr32(x, y) &= 0x00FFFFFF; |
|
93 } |
|
94 |
|
95 static std::ostream& operator<<(std::ostream& out, const Color& c) |
|
96 { |
|
97 std::ios_base::fmtflags oldFlags = out.flags(std::ios_base::hex | |
|
98 std::ios_base::showbase); |
|
99 out << c.rgb(); |
|
100 out.flags(oldFlags); |
|
101 return out; |
|
102 } |
|
103 |
|
104 TEST(TransparencyWin, NoLayer) |
|
105 { |
|
106 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(17, 16), DeviceRGB)); |
|
107 |
|
108 // KeepTransform |
|
109 { |
|
110 TransparencyWin helper; |
|
111 helper.init(src->context(), |
|
112 TransparencyWin::NoLayer, |
|
113 TransparencyWin::KeepTransform, |
|
114 IntRect(1, 1, 14, 12)); |
|
115 |
|
116 EXPECT_TRUE(src->context() == helper.context()); |
|
117 EXPECT_TRUE(IntSize(14, 12) == helper.m_layerSize); |
|
118 EXPECT_TRUE(IntRect(1, 1, 14, 12) == helper.drawRect()); |
|
119 } |
|
120 |
|
121 // Untransform is not allowed for NoLayer. |
|
122 |
|
123 // ScaleTransform |
|
124 src->context()->save(); |
|
125 src->context()->scale(FloatSize(2.0, 0.5)); |
|
126 { |
|
127 TransparencyWin helper; |
|
128 helper.init(src->context(), |
|
129 TransparencyWin::NoLayer, |
|
130 TransparencyWin::ScaleTransform, |
|
131 IntRect(2, 2, 6, 6)); |
|
132 helper.composite(); |
|
133 |
|
134 // The coordinate system should be based in the upper left of our box. |
|
135 // It should be post-transformed. |
|
136 EXPECT_TRUE(src->context() == helper.context()); |
|
137 EXPECT_TRUE(IntSize(12, 3) == helper.m_layerSize); |
|
138 EXPECT_TRUE(IntRect(4, 1, 12, 3) == helper.drawRect()); |
|
139 } |
|
140 src->context()->restore(); |
|
141 } |
|
142 |
|
143 TEST(TransparencyWin, WhiteLayer) |
|
144 { |
|
145 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16), DeviceRGB)); |
|
146 |
|
147 // KeepTransform |
|
148 { |
|
149 TransparencyWin helper; |
|
150 helper.init(src->context(), |
|
151 TransparencyWin::WhiteLayer, |
|
152 TransparencyWin::KeepTransform, |
|
153 IntRect(1, 1, 14, 12)); |
|
154 helper.composite(); |
|
155 |
|
156 EXPECT_TRUE(src->context() != helper.context()); |
|
157 EXPECT_TRUE(IntSize(14, 12) == helper.m_layerSize); |
|
158 EXPECT_TRUE(IntRect(1, 1, 14, 12) == helper.drawRect()); |
|
159 } |
|
160 |
|
161 // Untransform |
|
162 { |
|
163 TransparencyWin helper; |
|
164 helper.init(src->context(), |
|
165 TransparencyWin::WhiteLayer, |
|
166 TransparencyWin::Untransform, |
|
167 IntRect(1, 1, 14, 12)); |
|
168 helper.composite(); |
|
169 |
|
170 EXPECT_TRUE(src->context() != helper.context()); |
|
171 EXPECT_TRUE(IntSize(14, 12) == helper.m_layerSize); |
|
172 EXPECT_TRUE(IntRect(0, 0, 14, 12) == helper.drawRect()); |
|
173 } |
|
174 |
|
175 // ScaleTransform |
|
176 src->context()->save(); |
|
177 src->context()->scale(FloatSize(2.0, 0.5)); |
|
178 { |
|
179 TransparencyWin helper; |
|
180 helper.init(src->context(), |
|
181 TransparencyWin::WhiteLayer, |
|
182 TransparencyWin::ScaleTransform, |
|
183 IntRect(2, 2, 6, 6)); |
|
184 helper.composite(); |
|
185 |
|
186 // The coordinate system should be based in the upper left of our box. |
|
187 // It should be post-transformed. |
|
188 EXPECT_TRUE(src->context() != helper.context()); |
|
189 EXPECT_TRUE(IntSize(12, 3) == helper.m_layerSize); |
|
190 EXPECT_TRUE(IntRect(0, 0, 12, 3) == helper.drawRect()); |
|
191 } |
|
192 src->context()->restore(); |
|
193 } |
|
194 |
|
195 TEST(TransparencyWin, TextComposite) |
|
196 { |
|
197 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16), DeviceRGB)); |
|
198 |
|
199 // KeepTransform is the only valid transform mode for TextComposite. |
|
200 { |
|
201 TransparencyWin helper; |
|
202 helper.init(src->context(), |
|
203 TransparencyWin::TextComposite, |
|
204 TransparencyWin::KeepTransform, |
|
205 IntRect(1, 1, 14, 12)); |
|
206 helper.composite(); |
|
207 |
|
208 EXPECT_TRUE(src->context() != helper.context()); |
|
209 EXPECT_TRUE(IntSize(14, 12) == helper.m_layerSize); |
|
210 EXPECT_TRUE(IntRect(1, 1, 14, 12) == helper.drawRect()); |
|
211 } |
|
212 } |
|
213 |
|
214 TEST(TransparencyWin, OpaqueCompositeLayer) |
|
215 { |
|
216 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16), DeviceRGB)); |
|
217 |
|
218 // KeepTransform |
|
219 { |
|
220 TransparencyWin helper; |
|
221 helper.init(src->context(), |
|
222 TransparencyWin::OpaqueCompositeLayer, |
|
223 TransparencyWin::KeepTransform, |
|
224 IntRect(1, 1, 14, 12)); |
|
225 helper.composite(); |
|
226 |
|
227 EXPECT_TRUE(src->context() != helper.context()); |
|
228 EXPECT_TRUE(IntSize(14, 12) == helper.m_layerSize); |
|
229 EXPECT_TRUE(IntRect(1, 1, 14, 12) == helper.drawRect()); |
|
230 } |
|
231 |
|
232 // KeepTransform with scroll applied. |
|
233 src->context()->save(); |
|
234 src->context()->translate(0, -1); |
|
235 { |
|
236 TransparencyWin helper; |
|
237 helper.init(src->context(), |
|
238 TransparencyWin::OpaqueCompositeLayer, |
|
239 TransparencyWin::KeepTransform, |
|
240 IntRect(1, 1, 14, 14)); |
|
241 helper.composite(); |
|
242 |
|
243 EXPECT_TRUE(src->context() != helper.context()); |
|
244 EXPECT_TRUE(IntSize(14, 14) == helper.m_layerSize); |
|
245 EXPECT_TRUE(IntRect(1, 1, 14, 14) == helper.drawRect()); |
|
246 } |
|
247 src->context()->restore(); |
|
248 |
|
249 // Untransform |
|
250 { |
|
251 TransparencyWin helper; |
|
252 helper.init(src->context(), |
|
253 TransparencyWin::OpaqueCompositeLayer, |
|
254 TransparencyWin::Untransform, |
|
255 IntRect(1, 1, 14, 12)); |
|
256 helper.composite(); |
|
257 |
|
258 EXPECT_TRUE(src->context() != helper.context()); |
|
259 EXPECT_TRUE(IntSize(14, 12) == helper.m_layerSize); |
|
260 EXPECT_TRUE(IntRect(0, 0, 14, 12) == helper.drawRect()); |
|
261 } |
|
262 |
|
263 // ScaleTransform |
|
264 src->context()->save(); |
|
265 src->context()->scale(FloatSize(2.0, 0.5)); |
|
266 { |
|
267 TransparencyWin helper; |
|
268 helper.init(src->context(), |
|
269 TransparencyWin::OpaqueCompositeLayer, |
|
270 TransparencyWin::ScaleTransform, |
|
271 IntRect(2, 2, 6, 6)); |
|
272 helper.composite(); |
|
273 |
|
274 // The coordinate system should be based in the upper left of our box. |
|
275 // It should be post-transformed. |
|
276 EXPECT_TRUE(src->context() != helper.context()); |
|
277 EXPECT_TRUE(IntSize(12, 3) == helper.m_layerSize); |
|
278 EXPECT_TRUE(IntRect(0, 0, 12, 3) == helper.drawRect()); |
|
279 } |
|
280 src->context()->restore(); |
|
281 } |
|
282 |
|
283 TEST(TransparencyWin, WhiteLayerPixelTest) |
|
284 { |
|
285 // Make a total transparent buffer, and draw the white layer inset by 1 px. |
|
286 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16), DeviceRGB)); |
|
287 |
|
288 { |
|
289 TransparencyWin helper; |
|
290 helper.init(src->context(), |
|
291 TransparencyWin::WhiteLayer, |
|
292 TransparencyWin::KeepTransform, |
|
293 IntRect(1, 1, 14, 14)); |
|
294 |
|
295 // Coordinates should be in the original space, not the layer. |
|
296 drawNativeRect(helper.context(), 3, 3, 1, 1); |
|
297 clearTopLayerAlphaChannel(helper.context()); |
|
298 helper.composite(); |
|
299 } |
|
300 |
|
301 // The final image should be transparent around the edges for 1 px, white |
|
302 // in the middle, with (3,3) (what we drew above) being opaque black. |
|
303 EXPECT_EQ(Color(Color::transparent), getPixelAt(src->context(), 0, 0)); |
|
304 EXPECT_EQ(Color(Color::white), getPixelAt(src->context(), 2, 2)); |
|
305 EXPECT_EQ(Color(Color::black), getPixelAt(src->context(), 3, 3)); |
|
306 EXPECT_EQ(Color(Color::white), getPixelAt(src->context(), 4, 4)); |
|
307 } |
|
308 |
|
309 TEST(TransparencyWin, OpaqueCompositeLayerPixel) |
|
310 { |
|
311 Color red(0xFFFF0000), darkRed(0xFFBF0000); |
|
312 Color green(0xFF00FF00); |
|
313 |
|
314 // Make a red bottom layer, followed by a half green next layer @ 50%. |
|
315 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16), DeviceRGB)); |
|
316 |
|
317 FloatRect fullRect(0, 0, 16, 16); |
|
318 src->context()->fillRect(fullRect, red, DeviceColorSpace); |
|
319 src->context()->beginTransparencyLayer(0.5); |
|
320 FloatRect rightHalf(8, 0, 8, 16); |
|
321 src->context()->fillRect(rightHalf, green, DeviceColorSpace); |
|
322 |
|
323 // Make a transparency layer inset by one pixel, and fill it inset by |
|
324 // another pixel with 50% black. |
|
325 { |
|
326 TransparencyWin helper; |
|
327 helper.init(src->context(), |
|
328 TransparencyWin::OpaqueCompositeLayer, |
|
329 TransparencyWin::KeepTransform, |
|
330 IntRect(1, 1, 14, 14)); |
|
331 |
|
332 FloatRect inner(2, 2, 12, 12); |
|
333 helper.context()->fillRect(inner, Color(0x7f000000), DeviceColorSpace); |
|
334 // These coordinates are relative to the layer, whish is inset by 1x1 |
|
335 // pixels from the top left. So we're actually clearing (2, 2) and |
|
336 // (13,13), which are the extreme corners of the black area (and which |
|
337 // we check below). |
|
338 clearTopLayerAlphaPixel(helper.context(), 1, 1); |
|
339 clearTopLayerAlphaPixel(helper.context(), 12, 12); |
|
340 helper.composite(); |
|
341 } |
|
342 |
|
343 // Finish the compositing. |
|
344 src->context()->endTransparencyLayer(); |
|
345 |
|
346 // Check that we got the right values, it should be like the rectangle was |
|
347 // drawn with half opacity even though the alpha channel got messed up. |
|
348 EXPECT_EQ(red, getPixelAt(src->context(), 0, 0)); |
|
349 EXPECT_EQ(red, getPixelAt(src->context(), 1, 1)); |
|
350 EXPECT_EQ(darkRed, getPixelAt(src->context(), 2, 2)); |
|
351 |
|
352 // The dark result is: |
|
353 // (black @ 50% atop green) @ 50% atop red = 0xFF804000 |
|
354 // which is 0xFFA02000 (Skia computes 0xFFA11F00 due to rounding). |
|
355 Color darkGreenRed(0xFF803f00); |
|
356 EXPECT_EQ(darkGreenRed, getPixelAt(src->context(), 13, 13)); |
|
357 |
|
358 // 50% green on top of red = FF808000 (rounded to what Skia will produce). |
|
359 Color greenRed(0xFF807F00); |
|
360 EXPECT_EQ(greenRed, getPixelAt(src->context(), 14, 14)); |
|
361 EXPECT_EQ(greenRed, getPixelAt(src->context(), 15, 15)); |
|
362 } |
|
363 |
|
364 // Tests that translations are properly handled when using KeepTransform. |
|
365 TEST(TransparencyWin, TranslateOpaqueCompositeLayer) |
|
366 { |
|
367 // Fill with white. |
|
368 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16), DeviceRGB)); |
|
369 Color white(0xFFFFFFFF); |
|
370 FloatRect fullRect(0, 0, 16, 16); |
|
371 src->context()->fillRect(fullRect, white, DeviceColorSpace); |
|
372 |
|
373 // Scroll down by 8 (coordinate system goes up). |
|
374 src->context()->save(); |
|
375 src->context()->translate(0, -8); |
|
376 |
|
377 Color red(0xFFFF0000); |
|
378 Color green(0xFF00FF00); |
|
379 { |
|
380 // Make the transparency layer after translation will be @ (0, -8) with |
|
381 // size 16x16. |
|
382 TransparencyWin helper; |
|
383 helper.init(src->context(), |
|
384 TransparencyWin::OpaqueCompositeLayer, |
|
385 TransparencyWin::KeepTransform, |
|
386 IntRect(0, 0, 16, 16)); |
|
387 |
|
388 // Draw a red pixel at (15, 15). This should be the at (15, 7) after |
|
389 // the transform. |
|
390 FloatRect bottomRight(15, 15, 1, 1); |
|
391 helper.context()->fillRect(bottomRight, green, DeviceColorSpace); |
|
392 helper.composite(); |
|
393 } |
|
394 |
|
395 src->context()->restore(); |
|
396 |
|
397 // Check the pixel we wrote. |
|
398 EXPECT_EQ(green, getPixelAt(src->context(), 15, 7)); |
|
399 } |
|
400 |
|
401 // Same as OpaqueCompositeLayer, but the canvas has a rotation applied. This |
|
402 // tests that the propert transform is applied to the copied layer. |
|
403 TEST(TransparencyWin, RotateOpaqueCompositeLayer) |
|
404 { |
|
405 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16), DeviceRGB)); |
|
406 |
|
407 // The background is white. |
|
408 Color white(0xFFFFFFFF); |
|
409 FloatRect fullRect(0, 0, 16, 16); |
|
410 src->context()->fillRect(fullRect, white, DeviceColorSpace); |
|
411 |
|
412 // Rotate the image by 90 degrees. This matrix is the same as |
|
413 // cw90.rotate(90); but avoids rounding errors. Rounding errors can cause |
|
414 // Skia to think that !rectStaysRect() and it will fall through to path |
|
415 // drawing mode, which in turn gives us antialiasing. We want no |
|
416 // antialiasing or other rounding problems since we're testing exact pixel |
|
417 // values. |
|
418 src->context()->save(); |
|
419 AffineTransform cw90(0, 1, -1, 0, 0, 0); |
|
420 src->context()->concatCTM(cw90); |
|
421 |
|
422 // Make a transparency layer consisting of a horizontal line of 50% black. |
|
423 // Since the rotation is applied, this will actually be a vertical line |
|
424 // down the middle of the image. |
|
425 src->context()->beginTransparencyLayer(0.5); |
|
426 FloatRect blackRect(0, -9, 16, 2); |
|
427 Color black(0xFF000000); |
|
428 src->context()->fillRect(blackRect, black, DeviceColorSpace); |
|
429 |
|
430 // Now draw 50% red square. |
|
431 { |
|
432 // Create a transparency helper inset one pixel in the buffer. The |
|
433 // coordinates are before transforming into this space, and maps to |
|
434 // IntRect(1, 1, 14, 14). |
|
435 TransparencyWin helper; |
|
436 helper.init(src->context(), |
|
437 TransparencyWin::OpaqueCompositeLayer, |
|
438 TransparencyWin::Untransform, |
|
439 IntRect(1, -15, 14, 14)); |
|
440 |
|
441 // Fill with red. |
|
442 helper.context()->fillRect(helper.drawRect(), Color(0x7f7f0000), DeviceColorSpace); |
|
443 clearTopLayerAlphaChannel(helper.context()); |
|
444 helper.composite(); |
|
445 } |
|
446 |
|
447 // Finish the compositing. |
|
448 src->context()->endTransparencyLayer(); |
|
449 |
|
450 // Top corner should be the original background. |
|
451 EXPECT_EQ(white, getPixelAt(src->context(), 0, 0)); |
|
452 |
|
453 // Check the stripe down the middle, first at the top... |
|
454 Color gray(0xFF808080); |
|
455 EXPECT_EQ(white, getPixelAt(src->context(), 6, 0)); |
|
456 EXPECT_EQ(gray, getPixelAt(src->context(), 7, 0)); |
|
457 EXPECT_EQ(gray, getPixelAt(src->context(), 8, 0)); |
|
458 EXPECT_EQ(white, getPixelAt(src->context(), 9, 0)); |
|
459 |
|
460 // ...now at the bottom. |
|
461 EXPECT_EQ(white, getPixelAt(src->context(), 6, 15)); |
|
462 EXPECT_EQ(gray, getPixelAt(src->context(), 7, 15)); |
|
463 EXPECT_EQ(gray, getPixelAt(src->context(), 8, 15)); |
|
464 EXPECT_EQ(white, getPixelAt(src->context(), 9, 15)); |
|
465 |
|
466 // Our red square should be 25% red over the top of those two. |
|
467 Color redwhite(0xFFdfbfbf); |
|
468 Color redgray(0xFF9f8080); |
|
469 EXPECT_EQ(white, getPixelAt(src->context(), 0, 1)); |
|
470 EXPECT_EQ(redwhite, getPixelAt(src->context(), 1, 1)); |
|
471 EXPECT_EQ(redwhite, getPixelAt(src->context(), 6, 1)); |
|
472 EXPECT_EQ(redgray, getPixelAt(src->context(), 7, 1)); |
|
473 EXPECT_EQ(redgray, getPixelAt(src->context(), 8, 1)); |
|
474 EXPECT_EQ(redwhite, getPixelAt(src->context(), 9, 1)); |
|
475 EXPECT_EQ(redwhite, getPixelAt(src->context(), 14, 1)); |
|
476 EXPECT_EQ(white, getPixelAt(src->context(), 15, 1)); |
|
477 |
|
478 // Complete the 50% transparent layer. |
|
479 src->context()->restore(); |
|
480 } |
|
481 |
|
482 TEST(TransparencyWin, TranslateScaleOpaqueCompositeLayer) |
|
483 { |
|
484 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16), DeviceRGB)); |
|
485 |
|
486 // The background is white on top with red on bottom. |
|
487 Color white(0xFFFFFFFF); |
|
488 FloatRect topRect(0, 0, 16, 8); |
|
489 src->context()->fillRect(topRect, white, DeviceColorSpace); |
|
490 Color red(0xFFFF0000); |
|
491 FloatRect bottomRect(0, 8, 16, 8); |
|
492 src->context()->fillRect(bottomRect, red, DeviceColorSpace); |
|
493 |
|
494 src->context()->save(); |
|
495 |
|
496 // Translate left by one pixel. |
|
497 AffineTransform left; |
|
498 left.translate(-1, 0); |
|
499 |
|
500 // Scale by 2x. |
|
501 AffineTransform scale; |
|
502 scale.scale(2.0); |
|
503 src->context()->concatCTM(scale); |
|
504 |
|
505 // Then translate up by one pixel (which will actually be 2 due to scaling). |
|
506 AffineTransform up; |
|
507 up.translate(0, -1); |
|
508 src->context()->concatCTM(up); |
|
509 |
|
510 // Now draw 50% red square. |
|
511 { |
|
512 // Create a transparency helper inset one pixel in the buffer. The |
|
513 // coordinates are before transforming into this space, and maps to |
|
514 // IntRect(1, 1, 14, 14). |
|
515 TransparencyWin helper; |
|
516 helper.init(src->context(), |
|
517 TransparencyWin::OpaqueCompositeLayer, |
|
518 TransparencyWin::KeepTransform, |
|
519 IntRect(1, -15, 14, 14)); |
|
520 |
|
521 // Fill with red. |
|
522 helper.context()->fillRect(helper.drawRect(), Color(0x7f7f0000), DeviceColorSpace); |
|
523 clearTopLayerAlphaChannel(helper.context()); |
|
524 helper.composite(); |
|
525 } |
|
526 } |
|
527 |
|
528 // Tests scale mode with no additional copy. |
|
529 TEST(TransparencyWin, Scale) |
|
530 { |
|
531 // Create an opaque white buffer. |
|
532 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16), DeviceRGB)); |
|
533 FloatRect fullBuffer(0, 0, 16, 16); |
|
534 src->context()->fillRect(fullBuffer, Color::white, DeviceColorSpace); |
|
535 |
|
536 // Scale by 2x. |
|
537 src->context()->save(); |
|
538 AffineTransform scale; |
|
539 scale.scale(2.0); |
|
540 src->context()->concatCTM(scale); |
|
541 |
|
542 // Start drawing a rectangle from 1->4. This should get scaled to 2->8. |
|
543 { |
|
544 TransparencyWin helper; |
|
545 helper.init(src->context(), |
|
546 TransparencyWin::NoLayer, |
|
547 TransparencyWin::ScaleTransform, |
|
548 IntRect(1, 1, 3, 3)); |
|
549 |
|
550 // The context should now have the identity transform and the returned |
|
551 // rect should be scaled. |
|
552 EXPECT_TRUE(helper.context()->getCTM().isIdentity()); |
|
553 EXPECT_EQ(2, helper.drawRect().x()); |
|
554 EXPECT_EQ(2, helper.drawRect().y()); |
|
555 EXPECT_EQ(8, helper.drawRect().right()); |
|
556 EXPECT_EQ(8, helper.drawRect().bottom()); |
|
557 |
|
558 // Set the pixel at (2, 2) to be transparent. This should be fixed when |
|
559 // the helper goes out of scope. We don't want to call |
|
560 // clearTopLayerAlphaChannel because that will actually clear the whole |
|
561 // canvas (since we have no extra layer!). |
|
562 SkBitmap& bitmap = const_cast<SkBitmap&>(helper.context()-> |
|
563 platformContext()->canvas()->getTopPlatformDevice(). |
|
564 accessBitmap(false)); |
|
565 *bitmap.getAddr32(2, 2) &= 0x00FFFFFF; |
|
566 helper.composite(); |
|
567 } |
|
568 |
|
569 src->context()->restore(); |
|
570 |
|
571 // Check the pixel we previously made transparent, it should have gotten |
|
572 // fixed back up to white. |
|
573 |
|
574 // The current version doesn't fixup transparency when there is no layer. |
|
575 // This seems not to be necessary, so we don't bother, but if it becomes |
|
576 // necessary, this line should be uncommented. |
|
577 // EXPECT_EQ(Color(Color::white), getPixelAt(src->context(), 2, 2)); |
|
578 } |
|
579 |
|
580 // Tests scale mode with an additional copy for transparency. This will happen |
|
581 // if we have a scaled textbox, for example. WebKit will create a new |
|
582 // transparency layer, draw the text field, then draw the text into it, then |
|
583 // composite this down with an opacity. |
|
584 TEST(TransparencyWin, ScaleTransparency) |
|
585 { |
|
586 // Create an opaque white buffer. |
|
587 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16), DeviceRGB)); |
|
588 FloatRect fullBuffer(0, 0, 16, 16); |
|
589 src->context()->fillRect(fullBuffer, Color::white, DeviceColorSpace); |
|
590 |
|
591 // Make another layer (which duplicates how WebKit will make this). We fill |
|
592 // the top half with red, and have the layer be 50% opaque. |
|
593 src->context()->beginTransparencyLayer(0.5); |
|
594 FloatRect topHalf(0, 0, 16, 8); |
|
595 src->context()->fillRect(topHalf, Color(0xFFFF0000), DeviceColorSpace); |
|
596 |
|
597 // Scale by 2x. |
|
598 src->context()->save(); |
|
599 AffineTransform scale; |
|
600 scale.scale(2.0); |
|
601 src->context()->concatCTM(scale); |
|
602 |
|
603 // Make a layer inset two pixels (because of scaling, this is 2->14). And |
|
604 // will it with 50% black. |
|
605 { |
|
606 TransparencyWin helper; |
|
607 helper.init(src->context(), |
|
608 TransparencyWin::OpaqueCompositeLayer, |
|
609 TransparencyWin::ScaleTransform, |
|
610 IntRect(1, 1, 6, 6)); |
|
611 |
|
612 helper.context()->fillRect(helper.drawRect(), Color(0x7f000000), DeviceColorSpace); |
|
613 clearTopLayerAlphaChannel(helper.context()); |
|
614 helper.composite(); |
|
615 } |
|
616 |
|
617 // Finish the layer. |
|
618 src->context()->restore(); |
|
619 src->context()->endTransparencyLayer(); |
|
620 |
|
621 Color redBackground(0xFFFF8080); // 50% red composited on white. |
|
622 EXPECT_EQ(redBackground, getPixelAt(src->context(), 0, 0)); |
|
623 EXPECT_EQ(redBackground, getPixelAt(src->context(), 1, 1)); |
|
624 |
|
625 // Top half (minus two pixel border) should be 50% gray atop opaque |
|
626 // red = 0xFF804141. Then that's composited with 50% transparency on solid |
|
627 // white = 0xFFC0A1A1. |
|
628 Color darkRed(0xFFBF8080); |
|
629 EXPECT_EQ(darkRed, getPixelAt(src->context(), 2, 2)); |
|
630 EXPECT_EQ(darkRed, getPixelAt(src->context(), 7, 7)); |
|
631 |
|
632 // Bottom half (minus a two pixel border) should be a layer with 5% gray |
|
633 // with another 50% opacity composited atop white. |
|
634 Color darkWhite(0xFFBFBFBF); |
|
635 EXPECT_EQ(darkWhite, getPixelAt(src->context(), 8, 8)); |
|
636 EXPECT_EQ(darkWhite, getPixelAt(src->context(), 13, 13)); |
|
637 |
|
638 Color white(0xFFFFFFFF); // Background in the lower-right. |
|
639 EXPECT_EQ(white, getPixelAt(src->context(), 14, 14)); |
|
640 EXPECT_EQ(white, getPixelAt(src->context(), 15, 15)); |
|
641 } |
|
642 |
|
643 TEST(TransparencyWin, Text) |
|
644 { |
|
645 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16), DeviceRGB)); |
|
646 |
|
647 // Our text should end up 50% transparent blue-green. |
|
648 Color fullResult(0x80008080); |
|
649 |
|
650 { |
|
651 TransparencyWin helper; |
|
652 helper.init(src->context(), |
|
653 TransparencyWin::TextComposite, |
|
654 TransparencyWin::KeepTransform, |
|
655 IntRect(0, 0, 16, 16)); |
|
656 helper.setTextCompositeColor(fullResult); |
|
657 |
|
658 // Write several different squares to simulate ClearType. These should |
|
659 // all reduce to 2/3 coverage. |
|
660 FloatRect pixel(0, 0, 1, 1); |
|
661 helper.context()->fillRect(pixel, 0xFFFF0000, DeviceColorSpace); |
|
662 pixel.move(1.0f, 0.0f); |
|
663 helper.context()->fillRect(pixel, 0xFF00FF00, DeviceColorSpace); |
|
664 pixel.move(1.0f, 0.0f); |
|
665 helper.context()->fillRect(pixel, 0xFF0000FF, DeviceColorSpace); |
|
666 pixel.move(1.0f, 0.0f); |
|
667 helper.context()->fillRect(pixel, 0xFF008080, DeviceColorSpace); |
|
668 pixel.move(1.0f, 0.0f); |
|
669 helper.context()->fillRect(pixel, 0xFF800080, DeviceColorSpace); |
|
670 pixel.move(1.0f, 0.0f); |
|
671 helper.context()->fillRect(pixel, 0xFF808000, DeviceColorSpace); |
|
672 |
|
673 // Try one with 100% coverage (opaque black). |
|
674 pixel.move(1.0f, 0.0f); |
|
675 helper.context()->fillRect(pixel, 0xFF000000, DeviceColorSpace); |
|
676 |
|
677 // Now mess with the alpha channel. |
|
678 clearTopLayerAlphaChannel(helper.context()); |
|
679 helper.composite(); |
|
680 } |
|
681 |
|
682 Color oneThirdResult(0x55005555); // = fullResult * 2 / 3 |
|
683 EXPECT_EQ(oneThirdResult, getPixelAt(src->context(), 0, 0)); |
|
684 EXPECT_EQ(oneThirdResult, getPixelAt(src->context(), 1, 0)); |
|
685 EXPECT_EQ(oneThirdResult, getPixelAt(src->context(), 2, 0)); |
|
686 EXPECT_EQ(oneThirdResult, getPixelAt(src->context(), 3, 0)); |
|
687 EXPECT_EQ(oneThirdResult, getPixelAt(src->context(), 4, 0)); |
|
688 EXPECT_EQ(oneThirdResult, getPixelAt(src->context(), 5, 0)); |
|
689 EXPECT_EQ(fullResult, getPixelAt(src->context(), 6, 0)); |
|
690 EXPECT_EQ(Color::transparent, getPixelAt(src->context(), 7, 0)); |
|
691 } |
|
692 |
|
693 } // namespace WebCore |