2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 #include "platform/graphics/GraphicsContext.h"
34 #include "platform/graphics/ImageBuffer.h"
35 #include "platform/graphics/win/TransparencyWin.h"
36 #include "platform/transforms/AffineTransform.h"
39 #include <gtest/gtest.h>
43 static FloatRect RECTToFloatRect(const RECT* rect)
45 return FloatRect(static_cast<float>(rect->left),
46 static_cast<float>(rect->top),
47 static_cast<float>(rect->right - rect->left),
48 static_cast<float>(rect->bottom - rect->top));
51 static Color getPixelAt(GraphicsContext* context, int x, int y)
53 const SkBitmap& bitmap = context->layerBitmap();
54 return Color(*reinterpret_cast<const RGBA32*>(bitmap.getAddr32(x, y)));
57 // Resets the top layer's alpha channel to 0 for each pixel. This simulates
58 // Windows messing it up.
59 static void clearTopLayerAlphaChannel(GraphicsContext* context)
61 SkBitmap& bitmap = const_cast<SkBitmap&>(context->layerBitmap());
62 for (int y = 0; y < bitmap.height(); y++) {
63 uint32_t* row = bitmap.getAddr32(0, y);
64 for (int x = 0; x < bitmap.width(); x++)
69 // Clears the alpha channel on the specified pixel.
70 static void clearTopLayerAlphaPixel(GraphicsContext* context, int x, int y)
72 SkBitmap& bitmap = const_cast<SkBitmap&>(context->layerBitmap());
73 *bitmap.getAddr32(x, y) &= 0x00FFFFFF;
76 static std::ostream& operator<<(std::ostream& out, const Color& c)
78 std::ios_base::fmtflags oldFlags = out.flags(std::ios_base::hex |
79 std::ios_base::showbase);
85 TEST(TransparencyWin, NoLayer)
87 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(17, 16)));
91 TransparencyWin helper;
92 helper.init(src->context(),
93 TransparencyWin::NoLayer,
94 TransparencyWin::KeepTransform,
95 IntRect(1, 1, 14, 12));
97 EXPECT_TRUE(src->context() == helper.context());
98 EXPECT_TRUE(IntSize(14, 12) == helper.m_layerSize);
99 EXPECT_TRUE(IntRect(1, 1, 14, 12) == helper.drawRect());
102 // Untransform is not allowed for NoLayer.
105 src->context()->save();
106 src->context()->scale(FloatSize(2.0, 0.5));
108 TransparencyWin helper;
109 helper.init(src->context(),
110 TransparencyWin::NoLayer,
111 TransparencyWin::ScaleTransform,
112 IntRect(2, 2, 6, 6));
115 // The coordinate system should be based in the upper left of our box.
116 // It should be post-transformed.
117 EXPECT_TRUE(src->context() == helper.context());
118 EXPECT_TRUE(IntSize(12, 3) == helper.m_layerSize);
119 EXPECT_TRUE(IntRect(4, 1, 12, 3) == helper.drawRect());
121 src->context()->restore();
124 TEST(TransparencyWin, WhiteLayer)
126 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
130 TransparencyWin helper;
131 helper.init(src->context(),
132 TransparencyWin::WhiteLayer,
133 TransparencyWin::KeepTransform,
134 IntRect(1, 1, 14, 12));
137 EXPECT_TRUE(src->context() != helper.context());
138 EXPECT_TRUE(IntSize(14, 12) == helper.m_layerSize);
139 EXPECT_TRUE(IntRect(1, 1, 14, 12) == helper.drawRect());
144 TransparencyWin helper;
145 helper.init(src->context(),
146 TransparencyWin::WhiteLayer,
147 TransparencyWin::Untransform,
148 IntRect(1, 1, 14, 12));
151 EXPECT_TRUE(src->context() != helper.context());
152 EXPECT_TRUE(IntSize(14, 12) == helper.m_layerSize);
153 EXPECT_TRUE(IntRect(0, 0, 14, 12) == helper.drawRect());
157 src->context()->save();
158 src->context()->scale(FloatSize(2.0, 0.5));
160 TransparencyWin helper;
161 helper.init(src->context(),
162 TransparencyWin::WhiteLayer,
163 TransparencyWin::ScaleTransform,
164 IntRect(2, 2, 6, 6));
167 // The coordinate system should be based in the upper left of our box.
168 // It should be post-transformed.
169 EXPECT_TRUE(src->context() != helper.context());
170 EXPECT_TRUE(IntSize(12, 3) == helper.m_layerSize);
171 EXPECT_TRUE(IntRect(0, 0, 12, 3) == helper.drawRect());
173 src->context()->restore();
176 TEST(TransparencyWin, TextComposite)
178 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
180 // KeepTransform is the only valid transform mode for TextComposite.
182 TransparencyWin helper;
183 helper.init(src->context(),
184 TransparencyWin::TextComposite,
185 TransparencyWin::KeepTransform,
186 IntRect(1, 1, 14, 12));
189 EXPECT_TRUE(src->context() != helper.context());
190 EXPECT_TRUE(IntSize(14, 12) == helper.m_layerSize);
191 EXPECT_TRUE(IntRect(1, 1, 14, 12) == helper.drawRect());
195 TEST(TransparencyWin, OpaqueCompositeLayer)
197 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
201 TransparencyWin helper;
202 helper.init(src->context(),
203 TransparencyWin::OpaqueCompositeLayer,
204 TransparencyWin::KeepTransform,
205 IntRect(1, 1, 14, 12));
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());
213 // KeepTransform with scroll applied.
214 src->context()->save();
215 src->context()->translate(0, -1);
217 TransparencyWin helper;
218 helper.init(src->context(),
219 TransparencyWin::OpaqueCompositeLayer,
220 TransparencyWin::KeepTransform,
221 IntRect(1, 1, 14, 14));
224 EXPECT_TRUE(src->context() != helper.context());
225 EXPECT_TRUE(IntSize(14, 14) == helper.m_layerSize);
226 EXPECT_TRUE(IntRect(1, 1, 14, 14) == helper.drawRect());
228 src->context()->restore();
232 TransparencyWin helper;
233 helper.init(src->context(),
234 TransparencyWin::OpaqueCompositeLayer,
235 TransparencyWin::Untransform,
236 IntRect(1, 1, 14, 12));
239 EXPECT_TRUE(src->context() != helper.context());
240 EXPECT_TRUE(IntSize(14, 12) == helper.m_layerSize);
241 EXPECT_TRUE(IntRect(0, 0, 14, 12) == helper.drawRect());
245 src->context()->save();
246 src->context()->scale(FloatSize(2.0, 0.5));
248 TransparencyWin helper;
249 helper.init(src->context(),
250 TransparencyWin::OpaqueCompositeLayer,
251 TransparencyWin::ScaleTransform,
252 IntRect(2, 2, 6, 6));
255 // The coordinate system should be based in the upper left of our box.
256 // It should be post-transformed.
257 EXPECT_TRUE(src->context() != helper.context());
258 EXPECT_TRUE(IntSize(12, 3) == helper.m_layerSize);
259 EXPECT_TRUE(IntRect(0, 0, 12, 3) == helper.drawRect());
261 src->context()->restore();
264 TEST(TransparencyWin, OpaqueCompositeLayerPixel)
266 Color red(0xFFFF0000), darkRed(0xFFBF0000);
267 Color green(0xFF00FF00);
269 // Make a red bottom layer, followed by a half green next layer @ 50%.
270 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
272 FloatRect fullRect(0, 0, 16, 16);
273 src->context()->fillRect(fullRect, red);
274 src->context()->beginTransparencyLayer(0.5);
275 FloatRect rightHalf(8, 0, 8, 16);
276 src->context()->fillRect(rightHalf, green);
278 // Make a transparency layer inset by one pixel, and fill it inset by
279 // another pixel with 50% black.
281 TransparencyWin helper;
282 helper.init(src->context(),
283 TransparencyWin::OpaqueCompositeLayer,
284 TransparencyWin::KeepTransform,
285 IntRect(1, 1, 14, 14));
287 FloatRect inner(2, 2, 12, 12);
288 helper.context()->fillRect(inner, Color(0x7f000000));
289 // These coordinates are relative to the layer, whish is inset by 1x1
290 // pixels from the top left. So we're actually clearing (2, 2) and
291 // (13,13), which are the extreme corners of the black area (and which
293 clearTopLayerAlphaPixel(helper.context(), 1, 1);
294 clearTopLayerAlphaPixel(helper.context(), 12, 12);
298 // Finish the compositing.
299 src->context()->endLayer();
301 // Check that we got the right values, it should be like the rectangle was
302 // drawn with half opacity even though the alpha channel got messed up.
303 EXPECT_EQ(red, getPixelAt(src->context(), 0, 0));
304 EXPECT_EQ(red, getPixelAt(src->context(), 1, 1));
305 EXPECT_EQ(darkRed, getPixelAt(src->context(), 2, 2));
307 // The dark result is:
308 // (black @ 50% atop green) @ 50% atop red = 0xFF804000
309 // which is 0xFFA02000 (Skia computes 0xFFA11F00 due to rounding).
310 Color darkGreenRed(0xFF803f00);
311 EXPECT_EQ(darkGreenRed, getPixelAt(src->context(), 13, 13));
313 // 50% green on top of red = FF808000 (rounded to what Skia will produce).
314 Color greenRed(0xFF807F00);
315 EXPECT_EQ(greenRed, getPixelAt(src->context(), 14, 14));
316 EXPECT_EQ(greenRed, getPixelAt(src->context(), 15, 15));
319 // Tests that translations are properly handled when using KeepTransform.
320 TEST(TransparencyWin, TranslateOpaqueCompositeLayer)
323 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
324 Color white(0xFFFFFFFF);
325 FloatRect fullRect(0, 0, 16, 16);
326 src->context()->fillRect(fullRect, white);
328 // Scroll down by 8 (coordinate system goes up).
329 src->context()->save();
330 src->context()->translate(0, -8);
332 Color red(0xFFFF0000);
333 Color green(0xFF00FF00);
335 // Make the transparency layer after translation will be @ (0, -8) with
337 TransparencyWin helper;
338 helper.init(src->context(),
339 TransparencyWin::OpaqueCompositeLayer,
340 TransparencyWin::KeepTransform,
341 IntRect(0, 0, 16, 16));
343 // Draw a red pixel at (15, 15). This should be the at (15, 7) after
345 FloatRect bottomRight(15, 15, 1, 1);
346 helper.context()->fillRect(bottomRight, green);
350 src->context()->restore();
352 // Check the pixel we wrote.
353 EXPECT_EQ(green, getPixelAt(src->context(), 15, 7));
356 static void testClippedLayerKeepTransform(TransparencyWin::LayerMode layerMode)
359 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
360 Color white(0xFFFFFFFF);
361 FloatRect fullRect(0, 0, 16, 16);
362 src->context()->fillRect(fullRect, white);
364 IntRect clipRect(IntPoint(11, 5), IntSize(1, 1));
365 src->context()->clip(clipRect);
367 // Scroll down by 6 (coordinate system goes up).
368 src->context()->save();
369 src->context()->translate(0, -6);
371 Color red(0xFFFF0000);
372 Color green(0xFF00FF00);
374 // The transparency layer after translation will be @ (0, -6) with
375 // a size that would be too large to handle unclipped.
376 TransparencyWin helper;
377 helper.init(src->context(),
379 TransparencyWin::KeepTransform,
380 IntRect(0, 0, INT_MAX, INT_MAX));
382 // Draw a green pixel at (11, 11). This should be within the clip rect
383 // and at (11, 5) after the transform.
384 FloatRect greenRect(11, 11, 1, 1);
385 helper.context()->fillRect(greenRect, green);
387 // Draw a red pixel at (9, 9). This should be outside the clip rect
389 FloatRect redRect(9, 9, 1, 1);
390 helper.context()->fillRect(redRect, red);
394 src->context()->restore();
396 // Verify green pixel got drawn in clip rect and red pixel got clipped.
397 EXPECT_EQ(green, getPixelAt(src->context(), 11, 5));
398 EXPECT_EQ(white, getPixelAt(src->context(), 9, 3));
401 TEST(TransparencyWin, ClippedKeepTransformNoLayer)
403 testClippedLayerKeepTransform(TransparencyWin::NoLayer);
406 TEST(TransparencyWin, ClippedKeepTransformOpaqueCompositeLayer)
408 testClippedLayerKeepTransform(TransparencyWin::OpaqueCompositeLayer);
411 TEST(TransparencyWin, ClippedKeepTransformWhiteLayer)
413 testClippedLayerKeepTransform(TransparencyWin::WhiteLayer);
416 // Same as OpaqueCompositeLayer, but the canvas has a rotation applied. This
417 // tests that the propert transform is applied to the copied layer.
418 TEST(TransparencyWin, RotateOpaqueCompositeLayer)
420 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
422 // The background is white.
423 Color white(0xFFFFFFFF);
424 FloatRect fullRect(0, 0, 16, 16);
425 src->context()->fillRect(fullRect, white);
427 // Rotate the image by 90 degrees. This matrix is the same as
428 // cw90.rotate(90); but avoids rounding errors. Rounding errors can cause
429 // Skia to think that !rectStaysRect() and it will fall through to path
430 // drawing mode, which in turn gives us antialiasing. We want no
431 // antialiasing or other rounding problems since we're testing exact pixel
433 src->context()->save();
434 AffineTransform cw90(0, 1, -1, 0, 0, 0);
435 src->context()->concatCTM(cw90);
437 // Make a transparency layer consisting of a horizontal line of 50% black.
438 // Since the rotation is applied, this will actually be a vertical line
439 // down the middle of the image.
440 src->context()->beginTransparencyLayer(0.5);
441 FloatRect blackRect(0, -9, 16, 2);
442 Color black(0xFF000000);
443 src->context()->fillRect(blackRect, black);
445 // Now draw 50% red square.
447 // Create a transparency helper inset one pixel in the buffer. The
448 // coordinates are before transforming into this space, and maps to
449 // IntRect(1, 1, 14, 14).
450 TransparencyWin helper;
451 helper.init(src->context(),
452 TransparencyWin::OpaqueCompositeLayer,
453 TransparencyWin::Untransform,
454 IntRect(1, -15, 14, 14));
457 helper.context()->fillRect(helper.drawRect(), Color(0x7f7f0000));
458 clearTopLayerAlphaChannel(helper.context());
462 // Finish the compositing.
463 src->context()->endLayer();
465 // Top corner should be the original background.
466 EXPECT_EQ(white, getPixelAt(src->context(), 0, 0));
468 // Check the stripe down the middle, first at the top...
469 Color gray(0xFF808080);
470 EXPECT_EQ(white, getPixelAt(src->context(), 6, 0));
471 EXPECT_EQ(gray, getPixelAt(src->context(), 7, 0));
472 EXPECT_EQ(gray, getPixelAt(src->context(), 8, 0));
473 EXPECT_EQ(white, getPixelAt(src->context(), 9, 0));
475 // ...now at the bottom.
476 EXPECT_EQ(white, getPixelAt(src->context(), 6, 15));
477 EXPECT_EQ(gray, getPixelAt(src->context(), 7, 15));
478 EXPECT_EQ(gray, getPixelAt(src->context(), 8, 15));
479 EXPECT_EQ(white, getPixelAt(src->context(), 9, 15));
481 // Our red square should be 25% red over the top of those two.
482 Color redwhite(0xFFdfbfbf);
483 Color redgray(0xFF9f8080);
484 EXPECT_EQ(white, getPixelAt(src->context(), 0, 1));
485 EXPECT_EQ(redwhite, getPixelAt(src->context(), 1, 1));
486 EXPECT_EQ(redwhite, getPixelAt(src->context(), 6, 1));
487 EXPECT_EQ(redgray, getPixelAt(src->context(), 7, 1));
488 EXPECT_EQ(redgray, getPixelAt(src->context(), 8, 1));
489 EXPECT_EQ(redwhite, getPixelAt(src->context(), 9, 1));
490 EXPECT_EQ(redwhite, getPixelAt(src->context(), 14, 1));
491 EXPECT_EQ(white, getPixelAt(src->context(), 15, 1));
493 // Complete the 50% transparent layer.
494 src->context()->restore();
497 TEST(TransparencyWin, DISABLED_TranslateScaleOpaqueCompositeLayer)
499 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
501 // The background is white on top with red on bottom.
502 Color white(0xFFFFFFFF);
503 FloatRect topRect(0, 0, 16, 8);
504 src->context()->fillRect(topRect, white);
505 Color red(0xFFFF0000);
506 FloatRect bottomRect(0, 8, 16, 8);
507 src->context()->fillRect(bottomRect, red);
509 src->context()->save();
511 // Translate left by one pixel.
512 AffineTransform left;
513 left.translate(-1, 0);
516 AffineTransform scale;
518 src->context()->concatCTM(scale);
520 // Then translate up by one pixel (which will actually be 2 due to scaling).
523 src->context()->concatCTM(up);
525 // Now draw 50% red square.
527 // Create a transparency helper inset one pixel in the buffer. The
528 // coordinates are before transforming into this space, and maps to
529 // IntRect(1, 1, 14, 14).
530 TransparencyWin helper;
531 helper.init(src->context(),
532 TransparencyWin::OpaqueCompositeLayer,
533 TransparencyWin::KeepTransform,
534 IntRect(1, -15, 14, 14));
537 helper.context()->fillRect(helper.drawRect(), Color(0x7f7f0000));
538 clearTopLayerAlphaChannel(helper.context());
543 // Tests scale mode with no additional copy.
544 TEST(TransparencyWin, Scale)
546 // Create an opaque white buffer.
547 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
548 FloatRect fullBuffer(0, 0, 16, 16);
549 src->context()->fillRect(fullBuffer, Color::white);
552 src->context()->save();
553 AffineTransform scale;
555 src->context()->concatCTM(scale);
557 // Start drawing a rectangle from 1->4. This should get scaled to 2->8.
559 TransparencyWin helper;
560 helper.init(src->context(),
561 TransparencyWin::NoLayer,
562 TransparencyWin::ScaleTransform,
563 IntRect(1, 1, 3, 3));
565 // The context should now have the identity transform and the returned
566 // rect should be scaled.
567 EXPECT_TRUE(helper.context()->getCTM().isIdentity());
568 EXPECT_EQ(2, helper.drawRect().x());
569 EXPECT_EQ(2, helper.drawRect().y());
570 EXPECT_EQ(8, helper.drawRect().maxX());
571 EXPECT_EQ(8, helper.drawRect().maxY());
573 // Set the pixel at (2, 2) to be transparent. This should be fixed when
574 // the helper goes out of scope. We don't want to call
575 // clearTopLayerAlphaChannel because that will actually clear the whole
576 // canvas (since we have no extra layer!).
577 SkBitmap& bitmap = const_cast<SkBitmap&>(helper.context()->layerBitmap());
578 *bitmap.getAddr32(2, 2) &= 0x00FFFFFF;
582 src->context()->restore();
584 // Check the pixel we previously made transparent, it should have gotten
585 // fixed back up to white.
587 // The current version doesn't fixup transparency when there is no layer.
588 // This seems not to be necessary, so we don't bother, but if it becomes
589 // necessary, this line should be uncommented.
590 // EXPECT_EQ(Color(Color::white), getPixelAt(src->context(), 2, 2));
593 // Tests scale mode with an additional copy for transparency. This will happen
594 // if we have a scaled textbox, for example. WebKit will create a new
595 // transparency layer, draw the text field, then draw the text into it, then
596 // composite this down with an opacity.
597 TEST(TransparencyWin, ScaleTransparency)
599 // Create an opaque white buffer.
600 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
601 FloatRect fullBuffer(0, 0, 16, 16);
602 src->context()->fillRect(fullBuffer, Color::white);
604 // Make another layer (which duplicates how WebKit will make this). We fill
605 // the top half with red, and have the layer be 50% opaque.
606 src->context()->beginTransparencyLayer(0.5);
607 FloatRect topHalf(0, 0, 16, 8);
608 src->context()->fillRect(topHalf, Color(0xFFFF0000));
611 src->context()->save();
612 AffineTransform scale;
614 src->context()->concatCTM(scale);
616 // Make a layer inset two pixels (because of scaling, this is 2->14). And
617 // will it with 50% black.
619 TransparencyWin helper;
620 helper.init(src->context(),
621 TransparencyWin::OpaqueCompositeLayer,
622 TransparencyWin::ScaleTransform,
623 IntRect(1, 1, 6, 6));
625 helper.context()->fillRect(helper.drawRect(), Color(0x7f000000));
626 clearTopLayerAlphaChannel(helper.context());
631 src->context()->restore();
632 src->context()->endLayer();
634 Color redBackground(0xFFFF8080); // 50% red composited on white.
635 EXPECT_EQ(redBackground, getPixelAt(src->context(), 0, 0));
636 EXPECT_EQ(redBackground, getPixelAt(src->context(), 1, 1));
638 // Top half (minus two pixel border) should be 50% gray atop opaque
639 // red = 0xFF804141. Then that's composited with 50% transparency on solid
640 // white = 0xFFC0A1A1.
641 Color darkRed(0xFFBF8080);
642 EXPECT_EQ(darkRed, getPixelAt(src->context(), 2, 2));
643 EXPECT_EQ(darkRed, getPixelAt(src->context(), 7, 7));
645 // Bottom half (minus a two pixel border) should be a layer with 5% gray
646 // with another 50% opacity composited atop white.
647 Color darkWhite(0xFFBFBFBF);
648 EXPECT_EQ(darkWhite, getPixelAt(src->context(), 8, 8));
649 EXPECT_EQ(darkWhite, getPixelAt(src->context(), 13, 13));
651 Color white(0xFFFFFFFF); // Background in the lower-right.
652 EXPECT_EQ(white, getPixelAt(src->context(), 14, 14));
653 EXPECT_EQ(white, getPixelAt(src->context(), 15, 15));
656 TEST(TransparencyWin, Text)
658 OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
660 // Our text should end up 50% transparent blue-green.
661 Color fullResult(0x80008080);
664 TransparencyWin helper;
665 helper.init(src->context(),
666 TransparencyWin::TextComposite,
667 TransparencyWin::KeepTransform,
668 IntRect(0, 0, 16, 16));
669 helper.setTextCompositeColor(fullResult);
671 // Write several different squares to simulate ClearType. These should
672 // all reduce to 2/3 coverage.
673 FloatRect pixel(0, 0, 1, 1);
674 helper.context()->fillRect(pixel, 0xFFFF0000);
675 pixel.move(1.0f, 0.0f);
676 helper.context()->fillRect(pixel, 0xFF00FF00);
677 pixel.move(1.0f, 0.0f);
678 helper.context()->fillRect(pixel, 0xFF0000FF);
679 pixel.move(1.0f, 0.0f);
680 helper.context()->fillRect(pixel, 0xFF008080);
681 pixel.move(1.0f, 0.0f);
682 helper.context()->fillRect(pixel, 0xFF800080);
683 pixel.move(1.0f, 0.0f);
684 helper.context()->fillRect(pixel, 0xFF808000);
686 // Try one with 100% coverage (opaque black).
687 pixel.move(1.0f, 0.0f);
688 helper.context()->fillRect(pixel, 0xFF000000);
690 // Now mess with the alpha channel.
691 clearTopLayerAlphaChannel(helper.context());
695 Color oneThirdResult(0x55005555); // = fullResult * 2 / 3
696 EXPECT_EQ(oneThirdResult, getPixelAt(src->context(), 0, 0));
697 EXPECT_EQ(oneThirdResult, getPixelAt(src->context(), 1, 0));
698 EXPECT_EQ(oneThirdResult, getPixelAt(src->context(), 2, 0));
699 EXPECT_EQ(oneThirdResult, getPixelAt(src->context(), 3, 0));
700 EXPECT_EQ(oneThirdResult, getPixelAt(src->context(), 4, 0));
701 EXPECT_EQ(oneThirdResult, getPixelAt(src->context(), 5, 0));
702 EXPECT_EQ(fullResult, getPixelAt(src->context(), 6, 0));
703 EXPECT_EQ(Color::transparent, getPixelAt(src->context(), 7, 0));
706 } // namespace WebCore