From ab52947d4390b1b59aae47f5f6c16b7d537030fd Mon Sep 17 00:00:00 2001 From: "awalker@google.com" Date: Tue, 9 Sep 2008 20:38:55 +0000 Subject: Refactor the platform canvas unit test a bit so that it can be run on multiple platforms. Also adjusts platfom_canvas.h and platform_device.h so that some upper layers can include them instead of the windows versions. Change was originally http://codereview.chromium.org/1616, which was created in the wrong source tree. Applying approval from that issue. Review URL: http://codereview.chromium.org/1853 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1926 0039d316-1c4b-4281-b951-d872f2087c98 --- base/base.xcodeproj/project.pbxproj | 10 ++ base/build/base_unittests.vcproj | 2 +- base/gfx/SConscript | 2 +- base/gfx/bitmap_platform_device.h | 13 +- base/gfx/bitmap_platform_device_mac.h | 3 +- base/gfx/platform_canvas.h | 18 ++- base/gfx/platform_canvas_mac.cc | 5 + base/gfx/platform_canvas_unittest.c | 269 +++++++++++++++++++++++++++++++ base/gfx/platform_canvas_unittest_win.cc | 269 ------------------------------- base/gfx/platform_device.h | 15 +- base/gfx/platform_device_mac.h | 6 + 11 files changed, 326 insertions(+), 286 deletions(-) create mode 100644 base/gfx/platform_canvas_unittest.c delete mode 100644 base/gfx/platform_canvas_unittest_win.cc diff --git a/base/base.xcodeproj/project.pbxproj b/base/base.xcodeproj/project.pbxproj index 495f02f..fe90c36 100644 --- a/base/base.xcodeproj/project.pbxproj +++ b/base/base.xcodeproj/project.pbxproj @@ -140,6 +140,8 @@ AB956DFF0E5DDB6900BBE9D8 /* bitmap_platform_device_mac.cc in Sources */ = {isa = PBXBuildFile; fileRef = 829E2F940DBFD74B00819EBF /* bitmap_platform_device_mac.cc */; }; AB956E030E5DDB7A00BBE9D8 /* image_operations.cc in Sources */ = {isa = PBXBuildFile; fileRef = E48A06370E3F6C1F00172919 /* image_operations.cc */; }; AB956E0A0E5DDC0900BBE9D8 /* image_operations_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = E48A063B0E3F6C3000172919 /* image_operations_unittest.cc */; }; + ABE1BA2A0E7574D1009041DA /* ApplicationServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = ABE1BA290E7574D1009041DA /* ApplicationServices.framework */; }; + ABE1BA610E75757C009041DA /* skia_utils_mac.cc in Sources */ = {isa = PBXBuildFile; fileRef = ABE1BA600E75757C009041DA /* skia_utils_mac.cc */; }; ABF4B98F0DC2BA6900A6E319 /* base_paths_mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = ABF4B98E0DC2BA6900A6E319 /* base_paths_mac.mm */; }; ABF4B99E0DC2BB6000A6E319 /* clipboard_mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = ABF4B99D0DC2BB6000A6E319 /* clipboard_mac.mm */; }; ABF4B9AF0DC2BC6200A6E319 /* json_reader.cc in Sources */ = {isa = PBXBuildFile; fileRef = 8254031B0D92D1F40006B936 /* json_reader.cc */; }; @@ -559,6 +561,9 @@ AB6BC1ED0E54E836006436D5 /* bitmap_platform_device.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = bitmap_platform_device.h; sourceTree = ""; }; AB6BC1F10E54E900006436D5 /* platform_canvas.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = platform_canvas.h; sourceTree = ""; }; AB6BC1F50E54E97B006436D5 /* platform_device.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = platform_device.h; sourceTree = ""; }; + ABE1BA0C0E756EC4009041DA /* platform_canvas_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = platform_canvas_unittest.cc; sourceTree = ""; }; + ABE1BA290E7574D1009041DA /* ApplicationServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ApplicationServices.framework; path = System/Library/Frameworks/ApplicationServices.framework; sourceTree = SDKROOT; }; + ABE1BA600E75757C009041DA /* skia_utils_mac.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = skia_utils_mac.cc; sourceTree = ""; }; ABF4B98E0DC2BA6900A6E319 /* base_paths_mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = base_paths_mac.mm; sourceTree = ""; }; ABF4B99D0DC2BB6000A6E319 /* clipboard_mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = clipboard_mac.mm; sourceTree = ""; }; ABF4B9B40DC2BC9F00A6E319 /* path_service.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = path_service.cc; sourceTree = ""; }; @@ -636,6 +641,7 @@ 7B85062A0E5B556900730B43 /* libpng.a in Frameworks */, 7B4DF5350E5B6A66004D7619 /* libskia.a in Frameworks */, 7B85062F0E5B559A00730B43 /* libzlib.a in Frameworks */, + ABE1BA2A0E7574D1009041DA /* ApplicationServices.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -982,6 +988,7 @@ AB6BC1F10E54E900006436D5 /* platform_canvas.h */, 829E2F990DBFD76300819EBF /* platform_canvas_mac.cc */, 829E2F980DBFD76300819EBF /* platform_canvas_mac.h */, + ABE1BA0C0E756EC4009041DA /* platform_canvas_unittest.cc */, AB6BC1F50E54E97B006436D5 /* platform_device.h */, 829E2F9D0DBFD76F00819EBF /* platform_device_mac.cc */, 829E2F9C0DBFD76F00819EBF /* platform_device_mac.h */, @@ -999,6 +1006,7 @@ 825403D70D92D31D0006B936 /* size.h */, 7B8505A50E5B3FBE00730B43 /* skia_utils.cc */, 7B8505A60E5B3FBE00730B43 /* skia_utils.h */, + ABE1BA600E75757C009041DA /* skia_utils_mac.cc */, 825403DC0D92D31D0006B936 /* vector_canvas.cc */, 825403DD0D92D31D0006B936 /* vector_canvas.h */, 7B8505A80E5B3FBE00730B43 /* vector_canvas_unittest.cc */, @@ -1011,6 +1019,7 @@ 829E2FA80DBFD7D500819EBF /* Frameworks */ = { isa = PBXGroup; children = ( + ABE1BA290E7574D1009041DA /* ApplicationServices.framework */, 7B78D40D0E54FE8000609465 /* Foundation.framework */, ); name = Frameworks; @@ -1299,6 +1308,7 @@ 825403F20D92D31D0006B936 /* point.cc in Sources */, 825403F50D92D31D0006B936 /* rect.cc in Sources */, 7B8505B30E5B432200730B43 /* size.cc in Sources */, + ABE1BA610E75757C009041DA /* skia_utils_mac.cc in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/base/build/base_unittests.vcproj b/base/build/base_unittests.vcproj index f3993da..c8db389 100644 --- a/base/build/base_unittests.vcproj +++ b/base/build/base_unittests.vcproj @@ -380,7 +380,7 @@ > + +#include "base/gfx/platform_canvas_win.h" +#include "base/gfx/platform_device_win.h" +#include "testing/gtest/include/gtest/gtest.h" + +#include "SkColor.h" + +namespace gfx { + +namespace { + +// Return true if the canvas is filled to canvas_color, +// and contains a single rectangle filled to rect_color. +bool VerifyRect(const PlatformCanvasWin& canvas, + uint32_t canvas_color, uint32_t rect_color, + int x, int y, int w, int h) { + PlatformDeviceWin& device = canvas.getTopPlatformDevice(); + const SkBitmap& bitmap = device.accessBitmap(false); + SkAutoLockPixels lock(bitmap); + + for (int cur_y = 0; cur_y < bitmap.height(); cur_y++) { + for (int cur_x = 0; cur_x < bitmap.width(); cur_x++) { + if (cur_x >= x && cur_x < x + w && + cur_y >= y && cur_y < y + h) { + // Inside the square should be rect_color + if (*bitmap.getAddr32(cur_x, cur_y) != rect_color) + return false; + } else { + // Outside the square should be canvas_color + if (*bitmap.getAddr32(cur_x, cur_y) != canvas_color) + return false; + } + } + } + return true; +} + +// Checks whether there is a white canvas with a black square at the given +// location in pixels (not in the canvas coordinate system). +// TODO(ericroman): rename Square to Rect +bool VerifyBlackSquare(const PlatformCanvasWin& canvas, int x, int y, int w, int h) { + return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h); +} + +// Check that every pixel in the canvas is a single color. +bool VerifyCanvasColor(const PlatformCanvasWin& canvas, uint32_t canvas_color) { + return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0); +} + +void DrawGDIRect(PlatformCanvasWin& canvas, int x, int y, int w, int h) { + HDC dc = canvas.beginPlatformPaint(); + + RECT inner_rc; + inner_rc.left = x; + inner_rc.top = y; + inner_rc.right = x + w; + inner_rc.bottom = y + h; + FillRect(dc, &inner_rc, reinterpret_cast(GetStockObject(BLACK_BRUSH))); + + canvas.endPlatformPaint(); +} + +// Clips the contents of the canvas to the given rectangle. This will be +// intersected with any existing clip. +void AddClip(PlatformCanvasWin& canvas, int x, int y, int w, int h) { + SkRect rect; + rect.set(SkIntToScalar(x), SkIntToScalar(y), + SkIntToScalar(x + w), SkIntToScalar(y + h)); + canvas.clipRect(rect); +} + +class LayerSaver { + public: + LayerSaver(PlatformCanvasWin& canvas, int x, int y, int w, int h) + : canvas_(canvas), + x_(x), + y_(y), + w_(w), + h_(h) { + SkRect bounds; + bounds.set(SkIntToScalar(x_), SkIntToScalar(y_), + SkIntToScalar(right()), SkIntToScalar(bottom())); + canvas_.saveLayer(&bounds, NULL); + } + + ~LayerSaver() { + canvas_.getTopPlatformDevice().fixupAlphaBeforeCompositing(); + canvas_.restore(); + } + + int x() const { return x_; } + int y() const { return y_; } + int w() const { return w_; } + int h() const { return h_; } + + // Returns the EXCLUSIVE far bounds of the layer. + int right() const { return x_ + w_; } + int bottom() const { return y_ + h_; } + + private: + PlatformCanvasWin& canvas_; + int x_, y_, w_, h_; +}; + +// Size used for making layers in many of the below tests. +const int kLayerX = 2; +const int kLayerY = 3; +const int kLayerW = 9; +const int kLayerH = 7; + +// Size used by some tests to draw a rectangle inside the layer. +const int kInnerX = 4; +const int kInnerY = 5; +const int kInnerW = 2; +const int kInnerH = 3; + +} + +// This just checks that our checking code is working properly, it just uses +// regular skia primitives. +TEST(PlatformCanvasWin, SkLayer) { + // Create the canvas initialized to opaque white. + PlatformCanvasWin canvas(16, 16, true); + canvas.drawColor(SK_ColorWHITE); + + // Make a layer and fill it completely to make sure that the bounds are + // correct. + { + LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); + canvas.drawColor(SK_ColorBLACK); + } + EXPECT_TRUE(VerifyBlackSquare(canvas, kLayerX, kLayerY, kLayerW, kLayerH)); +} + +// Test the GDI clipping. +TEST(PlatformCanvasWin, GDIClipRegion) { + // Initialize a white canvas + PlatformCanvasWin canvas(16, 16, true); + canvas.drawColor(SK_ColorWHITE); + EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE)); + + // Test that initially the canvas has no clip region, by filling it + // with a black rectangle. + // Note: Don't use LayerSaver, since internally it sets a clip region. + DrawGDIRect(canvas, 0, 0, 16, 16); + canvas.getTopPlatformDevice().fixupAlphaBeforeCompositing(); + EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorBLACK)); + + // Test that intersecting disjoint clip rectangles sets an empty clip region + canvas.drawColor(SK_ColorWHITE); + EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE)); + { + LayerSaver layer(canvas, 0, 0, 16, 16); + AddClip(canvas, 2, 3, 4, 5); + AddClip(canvas, 4, 9, 10, 10); + DrawGDIRect(canvas, 0, 0, 16, 16); + } + EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE)); +} + +// Test the layers get filled properly by GDI. +TEST(PlatformCanvasWin, GDILayer) { + // Create the canvas initialized to opaque white. + PlatformCanvasWin canvas(16, 16, true); + + // Make a layer and fill it completely to make sure that the bounds are + // correct. + canvas.drawColor(SK_ColorWHITE); + { + LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); + DrawGDIRect(canvas, 0, 0, 100, 100); + } + EXPECT_TRUE(VerifyBlackSquare(canvas, kLayerX, kLayerY, kLayerW, kLayerH)); + + // Make a layer and fill it partially to make sure the translation is correct. + canvas.drawColor(SK_ColorWHITE); + { + LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); + DrawGDIRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH); + } + EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX, kInnerY, kInnerW, kInnerH)); + + // Add a clip on the layer and fill to make sure clip is correct. + canvas.drawColor(SK_ColorWHITE); + { + LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); + canvas.save(); + AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH); + DrawGDIRect(canvas, 0, 0, 100, 100); + canvas.restore(); + } + EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX, kInnerY, kInnerW, kInnerH)); + + // Add a clip and then make the layer to make sure the clip is correct. + canvas.drawColor(SK_ColorWHITE); + canvas.save(); + AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH); + { + LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); + DrawGDIRect(canvas, 0, 0, 100, 100); + } + canvas.restore(); + EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX, kInnerY, kInnerW, kInnerH)); +} + +// Test that translation + make layer works properly. +TEST(PlatformCanvasWin, GDITranslateLayer) { + // Create the canvas initialized to opaque white. + PlatformCanvasWin canvas(16, 16, true); + + // Make a layer and fill it completely to make sure that the bounds are + // correct. + canvas.drawColor(SK_ColorWHITE); + canvas.save(); + canvas.translate(1, 1); + { + LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); + DrawGDIRect(canvas, 0, 0, 100, 100); + } + canvas.restore(); + EXPECT_TRUE(VerifyBlackSquare(canvas, kLayerX + 1, kLayerY + 1, + kLayerW, kLayerH)); + + // Translate then make the layer. + canvas.drawColor(SK_ColorWHITE); + canvas.save(); + canvas.translate(1, 1); + { + LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); + DrawGDIRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH); + } + canvas.restore(); + EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX + 1, kInnerY + 1, + kInnerW, kInnerH)); + + // Make the layer then translate. + canvas.drawColor(SK_ColorWHITE); + canvas.save(); + { + LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); + canvas.translate(1, 1); + DrawGDIRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH); + } + canvas.restore(); + EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX + 1, kInnerY + 1, + kInnerW, kInnerH)); + + // Translate both before and after, and have a clip. + canvas.drawColor(SK_ColorWHITE); + canvas.save(); + canvas.translate(1, 1); + { + LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); + canvas.translate(1, 1); + AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH); + DrawGDIRect(canvas, 0, 0, 100, 100); + } + canvas.restore(); + EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX + 2, kInnerY + 2, + kInnerW, kInnerH)); +} + +} // namespace + diff --git a/base/gfx/platform_canvas_unittest_win.cc b/base/gfx/platform_canvas_unittest_win.cc deleted file mode 100644 index a9cf2ae..0000000 --- a/base/gfx/platform_canvas_unittest_win.cc +++ /dev/null @@ -1,269 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include - -#include "base/gfx/platform_canvas_win.h" -#include "base/gfx/platform_device_win.h" -#include "testing/gtest/include/gtest/gtest.h" - -#include "SkColor.h" - -namespace gfx { - -namespace { - -// Return true if the canvas is filled to canvas_color, -// and contains a single rectangle filled to rect_color. -bool VerifyRect(const PlatformCanvasWin& canvas, - uint32_t canvas_color, uint32_t rect_color, - int x, int y, int w, int h) { - PlatformDeviceWin& device = canvas.getTopPlatformDevice(); - const SkBitmap& bitmap = device.accessBitmap(false); - SkAutoLockPixels lock(bitmap); - - for (int cur_y = 0; cur_y < bitmap.height(); cur_y++) { - for (int cur_x = 0; cur_x < bitmap.width(); cur_x++) { - if (cur_x >= x && cur_x < x + w && - cur_y >= y && cur_y < y + h) { - // Inside the square should be rect_color - if (*bitmap.getAddr32(cur_x, cur_y) != rect_color) - return false; - } else { - // Outside the square should be canvas_color - if (*bitmap.getAddr32(cur_x, cur_y) != canvas_color) - return false; - } - } - } - return true; -} - -// Checks whether there is a white canvas with a black square at the given -// location in pixels (not in the canvas coordinate system). -// TODO(ericroman): rename Square to Rect -bool VerifyBlackSquare(const PlatformCanvasWin& canvas, int x, int y, int w, int h) { - return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h); -} - -// Check that every pixel in the canvas is a single color. -bool VerifyCanvasColor(const PlatformCanvasWin& canvas, uint32_t canvas_color) { - return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0); -} - -void DrawGDIRect(PlatformCanvasWin& canvas, int x, int y, int w, int h) { - HDC dc = canvas.beginPlatformPaint(); - - RECT inner_rc; - inner_rc.left = x; - inner_rc.top = y; - inner_rc.right = x + w; - inner_rc.bottom = y + h; - FillRect(dc, &inner_rc, reinterpret_cast(GetStockObject(BLACK_BRUSH))); - - canvas.endPlatformPaint(); -} - -// Clips the contents of the canvas to the given rectangle. This will be -// intersected with any existing clip. -void AddClip(PlatformCanvasWin& canvas, int x, int y, int w, int h) { - SkRect rect; - rect.set(SkIntToScalar(x), SkIntToScalar(y), - SkIntToScalar(x + w), SkIntToScalar(y + h)); - canvas.clipRect(rect); -} - -class LayerSaver { - public: - LayerSaver(PlatformCanvasWin& canvas, int x, int y, int w, int h) - : canvas_(canvas), - x_(x), - y_(y), - w_(w), - h_(h) { - SkRect bounds; - bounds.set(SkIntToScalar(x_), SkIntToScalar(y_), - SkIntToScalar(right()), SkIntToScalar(bottom())); - canvas_.saveLayer(&bounds, NULL); - } - - ~LayerSaver() { - canvas_.getTopPlatformDevice().fixupAlphaBeforeCompositing(); - canvas_.restore(); - } - - int x() const { return x_; } - int y() const { return y_; } - int w() const { return w_; } - int h() const { return h_; } - - // Returns the EXCLUSIVE far bounds of the layer. - int right() const { return x_ + w_; } - int bottom() const { return y_ + h_; } - - private: - PlatformCanvasWin& canvas_; - int x_, y_, w_, h_; -}; - -// Size used for making layers in many of the below tests. -const int kLayerX = 2; -const int kLayerY = 3; -const int kLayerW = 9; -const int kLayerH = 7; - -// Size used by some tests to draw a rectangle inside the layer. -const int kInnerX = 4; -const int kInnerY = 5; -const int kInnerW = 2; -const int kInnerH = 3; - -} - -// This just checks that our checking code is working properly, it just uses -// regular skia primitives. -TEST(PlatformCanvasWin, SkLayer) { - // Create the canvas initialized to opaque white. - PlatformCanvasWin canvas(16, 16, true); - canvas.drawColor(SK_ColorWHITE); - - // Make a layer and fill it completely to make sure that the bounds are - // correct. - { - LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); - canvas.drawColor(SK_ColorBLACK); - } - EXPECT_TRUE(VerifyBlackSquare(canvas, kLayerX, kLayerY, kLayerW, kLayerH)); -} - -// Test the GDI clipping. -TEST(PlatformCanvasWin, GDIClipRegion) { - // Initialize a white canvas - PlatformCanvasWin canvas(16, 16, true); - canvas.drawColor(SK_ColorWHITE); - EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE)); - - // Test that initially the canvas has no clip region, by filling it - // with a black rectangle. - // Note: Don't use LayerSaver, since internally it sets a clip region. - DrawGDIRect(canvas, 0, 0, 16, 16); - canvas.getTopPlatformDevice().fixupAlphaBeforeCompositing(); - EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorBLACK)); - - // Test that intersecting disjoint clip rectangles sets an empty clip region - canvas.drawColor(SK_ColorWHITE); - EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE)); - { - LayerSaver layer(canvas, 0, 0, 16, 16); - AddClip(canvas, 2, 3, 4, 5); - AddClip(canvas, 4, 9, 10, 10); - DrawGDIRect(canvas, 0, 0, 16, 16); - } - EXPECT_TRUE(VerifyCanvasColor(canvas, SK_ColorWHITE)); -} - -// Test the layers get filled properly by GDI. -TEST(PlatformCanvasWin, GDILayer) { - // Create the canvas initialized to opaque white. - PlatformCanvasWin canvas(16, 16, true); - - // Make a layer and fill it completely to make sure that the bounds are - // correct. - canvas.drawColor(SK_ColorWHITE); - { - LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); - DrawGDIRect(canvas, 0, 0, 100, 100); - } - EXPECT_TRUE(VerifyBlackSquare(canvas, kLayerX, kLayerY, kLayerW, kLayerH)); - - // Make a layer and fill it partially to make sure the translation is correct. - canvas.drawColor(SK_ColorWHITE); - { - LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); - DrawGDIRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH); - } - EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX, kInnerY, kInnerW, kInnerH)); - - // Add a clip on the layer and fill to make sure clip is correct. - canvas.drawColor(SK_ColorWHITE); - { - LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); - canvas.save(); - AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH); - DrawGDIRect(canvas, 0, 0, 100, 100); - canvas.restore(); - } - EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX, kInnerY, kInnerW, kInnerH)); - - // Add a clip and then make the layer to make sure the clip is correct. - canvas.drawColor(SK_ColorWHITE); - canvas.save(); - AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH); - { - LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); - DrawGDIRect(canvas, 0, 0, 100, 100); - } - canvas.restore(); - EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX, kInnerY, kInnerW, kInnerH)); -} - -// Test that translation + make layer works properly. -TEST(PlatformCanvasWin, GDITranslateLayer) { - // Create the canvas initialized to opaque white. - PlatformCanvasWin canvas(16, 16, true); - - // Make a layer and fill it completely to make sure that the bounds are - // correct. - canvas.drawColor(SK_ColorWHITE); - canvas.save(); - canvas.translate(1, 1); - { - LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); - DrawGDIRect(canvas, 0, 0, 100, 100); - } - canvas.restore(); - EXPECT_TRUE(VerifyBlackSquare(canvas, kLayerX + 1, kLayerY + 1, - kLayerW, kLayerH)); - - // Translate then make the layer. - canvas.drawColor(SK_ColorWHITE); - canvas.save(); - canvas.translate(1, 1); - { - LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); - DrawGDIRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH); - } - canvas.restore(); - EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX + 1, kInnerY + 1, - kInnerW, kInnerH)); - - // Make the layer then translate. - canvas.drawColor(SK_ColorWHITE); - canvas.save(); - { - LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); - canvas.translate(1, 1); - DrawGDIRect(canvas, kInnerX, kInnerY, kInnerW, kInnerH); - } - canvas.restore(); - EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX + 1, kInnerY + 1, - kInnerW, kInnerH)); - - // Translate both before and after, and have a clip. - canvas.drawColor(SK_ColorWHITE); - canvas.save(); - canvas.translate(1, 1); - { - LayerSaver layer(canvas, kLayerX, kLayerY, kLayerW, kLayerH); - canvas.translate(1, 1); - AddClip(canvas, kInnerX, kInnerY, kInnerW, kInnerH); - DrawGDIRect(canvas, 0, 0, 100, 100); - } - canvas.restore(); - EXPECT_TRUE(VerifyBlackSquare(canvas, kInnerX + 2, kInnerY + 2, - kInnerW, kInnerH)); -} - -} // namespace - diff --git a/base/gfx/platform_device.h b/base/gfx/platform_device.h index 796eec8..f1c890c 100644 --- a/base/gfx/platform_device.h +++ b/base/gfx/platform_device.h @@ -6,17 +6,22 @@ // that can be used by upper-level classes that just need to pass a reference // around. -namespace gfx { +#if defined(OS_WIN) +#include "base/gfx/platform_device_win.h" +#elif defined(OS_MACOSX) +#include "base/gfx/platform_device_mac.h" +#elif defined(OS_LINUX) +#include "base/gfx/platform_device_linux.h" +#endif +namespace gfx { + #if defined(OS_WIN) -class PlatformDeviceWin; typedef PlatformDeviceWin PlatformDevice; #elif defined(OS_MACOSX) -class PlatformDeviceMac; typedef PlatformDeviceMac PlatformDevice; #elif defined(OS_LINUX) -class PlatformDeviceLinux; typedef PlatformDeviceLinux PlatformDevice; #endif - + } // namespace gfx diff --git a/base/gfx/platform_device_mac.h b/base/gfx/platform_device_mac.h index 8cd2251..69eac95 100755 --- a/base/gfx/platform_device_mac.h +++ b/base/gfx/platform_device_mac.h @@ -59,6 +59,12 @@ class PlatformDeviceMac : public SkDevice { // Returns if the preferred rendering engine is vectorial or bitmap based. virtual bool IsVectorial() = 0; + // On platforms where the native rendering API does not support rendering + // into bitmaps with a premultiplied alpha channel, this call is responsible + // for doing any fixup necessary. It is not used on the Mac, since + // CoreGraphics can handle premultiplied alpha just fine. + virtual void fixupAlphaBeforeCompositing() = 0; + // Initializes the default settings and colors in a device context. static void InitializeCGContext(CGContextRef context); -- cgit v1.1