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