Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / tests / TransparencyWinTest.cpp
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
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"
37
38 #include <windows.h>
39 #include <gtest/gtest.h>
40
41 namespace WebCore {
42
43 static FloatRect RECTToFloatRect(const RECT* rect)
44 {
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));
49 }
50
51 static Color getPixelAt(GraphicsContext* context, int x, int y)
52 {
53     const SkBitmap& bitmap = context->layerBitmap();
54     return Color(*reinterpret_cast<const RGBA32*>(bitmap.getAddr32(x, y)));
55 }
56
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)
60 {
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++)
65             row[x] &= 0x00FFFFFF;
66     }
67 }
68
69 // Clears the alpha channel on the specified pixel.
70 static void clearTopLayerAlphaPixel(GraphicsContext* context, int x, int y)
71 {
72     SkBitmap& bitmap = const_cast<SkBitmap&>(context->layerBitmap());
73     *bitmap.getAddr32(x, y) &= 0x00FFFFFF;
74 }
75
76 static std::ostream& operator<<(std::ostream& out, const Color& c)
77 {
78     std::ios_base::fmtflags oldFlags = out.flags(std::ios_base::hex |
79                                                  std::ios_base::showbase);
80     out << c.rgb();
81     out.flags(oldFlags);
82     return out;
83 }
84
85 TEST(TransparencyWin, NoLayer)
86 {
87     OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(17, 16)));
88
89     // KeepTransform
90     {
91         TransparencyWin helper;
92         helper.init(src->context(),
93                     TransparencyWin::NoLayer,
94                     TransparencyWin::KeepTransform,
95                     IntRect(1, 1, 14, 12));
96
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());
100     }
101
102     // Untransform is not allowed for NoLayer.
103
104     // ScaleTransform
105     src->context()->save();
106     src->context()->scale(FloatSize(2.0, 0.5));
107     {
108         TransparencyWin helper;
109         helper.init(src->context(),
110                     TransparencyWin::NoLayer,
111                     TransparencyWin::ScaleTransform,
112                     IntRect(2, 2, 6, 6));
113         helper.composite();
114
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());
120     }
121     src->context()->restore();
122 }
123
124 TEST(TransparencyWin, WhiteLayer)
125 {
126     OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
127
128     // KeepTransform
129     {
130         TransparencyWin helper;
131         helper.init(src->context(),
132                     TransparencyWin::WhiteLayer,
133                     TransparencyWin::KeepTransform,
134                     IntRect(1, 1, 14, 12));
135         helper.composite();
136
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());
140     }
141
142     // Untransform
143     {
144         TransparencyWin helper;
145         helper.init(src->context(),
146                     TransparencyWin::WhiteLayer,
147                     TransparencyWin::Untransform,
148                     IntRect(1, 1, 14, 12));
149         helper.composite();
150
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());
154     }
155
156     // ScaleTransform
157     src->context()->save();
158     src->context()->scale(FloatSize(2.0, 0.5));
159     {
160         TransparencyWin helper;
161         helper.init(src->context(),
162                     TransparencyWin::WhiteLayer,
163                     TransparencyWin::ScaleTransform,
164                     IntRect(2, 2, 6, 6));
165         helper.composite();
166
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());
172     }
173     src->context()->restore();
174 }
175
176 TEST(TransparencyWin, TextComposite)
177 {
178     OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
179
180     // KeepTransform is the only valid transform mode for TextComposite.
181     {
182         TransparencyWin helper;
183         helper.init(src->context(),
184                     TransparencyWin::TextComposite,
185                     TransparencyWin::KeepTransform,
186                     IntRect(1, 1, 14, 12));
187         helper.composite();
188
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());
192     }
193 }
194
195 TEST(TransparencyWin, OpaqueCompositeLayer)
196 {
197     OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
198
199     // KeepTransform
200     {
201         TransparencyWin helper;
202         helper.init(src->context(),
203                     TransparencyWin::OpaqueCompositeLayer,
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     // KeepTransform with scroll applied.
214     src->context()->save();
215     src->context()->translate(0, -1);
216     {
217         TransparencyWin helper;
218         helper.init(src->context(),
219                     TransparencyWin::OpaqueCompositeLayer,
220                     TransparencyWin::KeepTransform,
221                     IntRect(1, 1, 14, 14));
222         helper.composite();
223
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());
227     }
228     src->context()->restore();
229
230     // Untransform
231     {
232         TransparencyWin helper;
233         helper.init(src->context(),
234                     TransparencyWin::OpaqueCompositeLayer,
235                     TransparencyWin::Untransform,
236                     IntRect(1, 1, 14, 12));
237         helper.composite();
238
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());
242     }
243
244     // ScaleTransform
245     src->context()->save();
246     src->context()->scale(FloatSize(2.0, 0.5));
247     {
248         TransparencyWin helper;
249         helper.init(src->context(),
250                     TransparencyWin::OpaqueCompositeLayer,
251                     TransparencyWin::ScaleTransform,
252                     IntRect(2, 2, 6, 6));
253         helper.composite();
254
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());
260     }
261     src->context()->restore();
262 }
263
264 TEST(TransparencyWin, OpaqueCompositeLayerPixel)
265 {
266     Color red(0xFFFF0000), darkRed(0xFFBF0000);
267     Color green(0xFF00FF00);
268
269     // Make a red bottom layer, followed by a half green next layer @ 50%.
270     OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
271
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);
277
278     // Make a transparency layer inset by one pixel, and fill it inset by
279     // another pixel with 50% black.
280     {
281         TransparencyWin helper;
282         helper.init(src->context(),
283                     TransparencyWin::OpaqueCompositeLayer,
284                     TransparencyWin::KeepTransform,
285                     IntRect(1, 1, 14, 14));
286
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
292         // we check below).
293         clearTopLayerAlphaPixel(helper.context(), 1, 1);
294         clearTopLayerAlphaPixel(helper.context(), 12, 12);
295         helper.composite();
296     }
297
298     // Finish the compositing.
299     src->context()->endLayer();
300
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));
306
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));
312
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));
317 }
318
319 // Tests that translations are properly handled when using KeepTransform.
320 TEST(TransparencyWin, TranslateOpaqueCompositeLayer)
321 {
322     // Fill with white.
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);
327
328     // Scroll down by 8 (coordinate system goes up).
329     src->context()->save();
330     src->context()->translate(0, -8);
331
332     Color red(0xFFFF0000);
333     Color green(0xFF00FF00);
334     {
335         // Make the transparency layer after translation will be @ (0, -8) with
336         // size 16x16.
337         TransparencyWin helper;
338         helper.init(src->context(),
339                     TransparencyWin::OpaqueCompositeLayer,
340                     TransparencyWin::KeepTransform,
341                     IntRect(0, 0, 16, 16));
342
343         // Draw a red pixel at (15, 15). This should be the at (15, 7) after
344         // the transform.
345         FloatRect bottomRight(15, 15, 1, 1);
346         helper.context()->fillRect(bottomRight, green);
347         helper.composite();
348     }
349
350     src->context()->restore();
351
352     // Check the pixel we wrote.
353     EXPECT_EQ(green, getPixelAt(src->context(), 15, 7));
354 }
355
356 static void testClippedLayerKeepTransform(TransparencyWin::LayerMode layerMode)
357 {
358     // Fill with white.
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);
363
364     IntRect clipRect(IntPoint(11, 5), IntSize(1, 1));
365     src->context()->clip(clipRect);
366
367     // Scroll down by 6 (coordinate system goes up).
368     src->context()->save();
369     src->context()->translate(0, -6);
370
371     Color red(0xFFFF0000);
372     Color green(0xFF00FF00);
373     {
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(),
378                     layerMode,
379                     TransparencyWin::KeepTransform,
380                     IntRect(0, 0, INT_MAX, INT_MAX));
381
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);
386
387         // Draw a red pixel at (9, 9). This should be outside the clip rect
388         // and not drawn.
389         FloatRect redRect(9, 9, 1, 1);
390         helper.context()->fillRect(redRect, red);
391         helper.composite();
392     }
393
394     src->context()->restore();
395
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));
399 }
400
401 TEST(TransparencyWin, ClippedKeepTransformNoLayer)
402 {
403     testClippedLayerKeepTransform(TransparencyWin::NoLayer);
404 }
405
406 TEST(TransparencyWin, ClippedKeepTransformOpaqueCompositeLayer)
407 {
408     testClippedLayerKeepTransform(TransparencyWin::OpaqueCompositeLayer);
409 }
410
411 TEST(TransparencyWin, ClippedKeepTransformWhiteLayer)
412 {
413     testClippedLayerKeepTransform(TransparencyWin::WhiteLayer);
414 }
415
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)
419 {
420     OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
421
422     // The background is white.
423     Color white(0xFFFFFFFF);
424     FloatRect fullRect(0, 0, 16, 16);
425     src->context()->fillRect(fullRect, white);
426
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
432     // values.
433     src->context()->save();
434     AffineTransform cw90(0, 1, -1, 0, 0, 0);
435     src->context()->concatCTM(cw90);
436
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);
444
445     // Now draw 50% red square.
446     {
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));
455
456         // Fill with red.
457         helper.context()->fillRect(helper.drawRect(), Color(0x7f7f0000));
458         clearTopLayerAlphaChannel(helper.context());
459         helper.composite();
460     }
461
462     // Finish the compositing.
463     src->context()->endLayer();
464
465     // Top corner should be the original background.
466     EXPECT_EQ(white, getPixelAt(src->context(), 0, 0));
467
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));
474
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));
480
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));
492
493     // Complete the 50% transparent layer.
494     src->context()->restore();
495 }
496
497 TEST(TransparencyWin, DISABLED_TranslateScaleOpaqueCompositeLayer)
498 {
499     OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
500
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);
508
509     src->context()->save();
510
511     // Translate left by one pixel.
512     AffineTransform left;
513     left.translate(-1, 0);
514
515     // Scale by 2x.
516     AffineTransform scale;
517     scale.scale(2.0);
518     src->context()->concatCTM(scale);
519
520     // Then translate up by one pixel (which will actually be 2 due to scaling).
521     AffineTransform up;
522     up.translate(0, -1);
523     src->context()->concatCTM(up);
524
525     // Now draw 50% red square.
526     {
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));
535
536         // Fill with red.
537         helper.context()->fillRect(helper.drawRect(), Color(0x7f7f0000));
538         clearTopLayerAlphaChannel(helper.context());
539         helper.composite();
540     }
541 }
542
543 // Tests scale mode with no additional copy.
544 TEST(TransparencyWin, Scale)
545 {
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);
550
551     // Scale by 2x.
552     src->context()->save();
553     AffineTransform scale;
554     scale.scale(2.0);
555     src->context()->concatCTM(scale);
556
557     // Start drawing a rectangle from 1->4. This should get scaled to 2->8.
558     {
559         TransparencyWin helper;
560         helper.init(src->context(),
561                     TransparencyWin::NoLayer,
562                     TransparencyWin::ScaleTransform,
563                     IntRect(1, 1, 3, 3));
564
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());
572
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;
579         helper.composite();
580     }
581
582     src->context()->restore();
583
584     // Check the pixel we previously made transparent, it should have gotten
585     // fixed back up to white.
586
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));
591 }
592
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)
598 {
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);
603
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));
609
610     // Scale by 2x.
611     src->context()->save();
612     AffineTransform scale;
613     scale.scale(2.0);
614     src->context()->concatCTM(scale);
615
616     // Make a layer inset two pixels (because of scaling, this is 2->14). And
617     // will it with 50% black.
618     {
619         TransparencyWin helper;
620         helper.init(src->context(),
621                     TransparencyWin::OpaqueCompositeLayer,
622                     TransparencyWin::ScaleTransform,
623                     IntRect(1, 1, 6, 6));
624
625         helper.context()->fillRect(helper.drawRect(), Color(0x7f000000));
626         clearTopLayerAlphaChannel(helper.context());
627         helper.composite();
628     }
629
630     // Finish the layer.
631     src->context()->restore();
632     src->context()->endLayer();
633
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));
637
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));
644
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));
650
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));
654 }
655
656 TEST(TransparencyWin, Text)
657 {
658     OwnPtr<ImageBuffer> src(ImageBuffer::create(IntSize(16, 16)));
659
660     // Our text should end up 50% transparent blue-green.
661     Color fullResult(0x80008080);
662
663     {
664         TransparencyWin helper;
665         helper.init(src->context(),
666                     TransparencyWin::TextComposite,
667                     TransparencyWin::KeepTransform,
668                     IntRect(0, 0, 16, 16));
669         helper.setTextCompositeColor(fullResult);
670
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);
685
686         // Try one with 100% coverage (opaque black).
687         pixel.move(1.0f, 0.0f);
688         helper.context()->fillRect(pixel, 0xFF000000);
689
690         // Now mess with the alpha channel.
691         clearTopLayerAlphaChannel(helper.context());
692         helper.composite();
693     }
694
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));
704 }
705
706 } // namespace WebCore