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