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