diff options
author | caryclark@chromium.org <caryclark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-17 23:06:11 +0000 |
---|---|---|
committer | caryclark@chromium.org <caryclark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-17 23:06:11 +0000 |
commit | 02c48b0f977407888733ceb5f855a0195ec6f2ba (patch) | |
tree | f558b24c1c2b3a3514beebf45eb25494cc05486f /skia/ext/skia_utils_mac_unittest.mm | |
parent | 73a516a76cd236c32d67b8f558a4aabc601e3481 (diff) | |
download | chromium_src-02c48b0f977407888733ceb5f855a0195ec6f2ba.zip chromium_src-02c48b0f977407888733ceb5f855a0195ec6f2ba.tar.gz chromium_src-02c48b0f977407888733ceb5f855a0195ec6f2ba.tar.bz2 |
Add utility for converting SkCanvas to CGContext
Skia on Mac uses Skia to render WebKit, and CG
to render UI elements. The CG elements need a
transcribed graphics context that preserves the
canvas matrix, and the canvas clip.
The BitLockerSkia utility class sets up a CGContext
from the SkCanvas, locks the bitmap's bits, and
releases the lock when the class goes out of scope.
Each time the CGContext is retrieved, it is rebuilt.
This permits the caller to modify the same canvas
between calls.
Outside of the unit test, there are no callers
to this utility for now.
BUG=79463
TEST=SkiaUtilsMacTest.*
Review URL: http://codereview.chromium.org/7031006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85692 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'skia/ext/skia_utils_mac_unittest.mm')
-rw-r--r-- | skia/ext/skia_utils_mac_unittest.mm | 62 |
1 files changed, 61 insertions, 1 deletions
diff --git a/skia/ext/skia_utils_mac_unittest.mm b/skia/ext/skia_utils_mac_unittest.mm index 132c6ac..b2175cd 100644 --- a/skia/ext/skia_utils_mac_unittest.mm +++ b/skia/ext/skia_utils_mac_unittest.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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. @@ -21,6 +21,14 @@ class SkiaUtilsMacTest : public testing::Test { // Checks that the given bitmap is actually red or blue. void TestSkBitmap(const SkBitmap& bitmap, bool isred); + enum BitLockerTest { + TestIdentity = 0, + TestTranslate = 1, + TestClip = 2, + TestXClip = TestTranslate | TestClip + }; + void RunBitLockerTest(BitLockerTest test); + // If not red, is blue. // If not tfbit (twenty-four-bit), is 444. void ShapeHelper(int width, int height, bool isred, bool tfbit); @@ -101,6 +109,41 @@ void SkiaUtilsMacTest::TestSkBitmap(const SkBitmap& bitmap, bool isred) { EXPECT_GT(SkColorGetA(color), 245u); } +void SkiaUtilsMacTest::RunBitLockerTest(BitLockerTest test) { + const unsigned width = 2; + const unsigned height = 2; + const unsigned storageSize = width * height; + const unsigned original[] = {0xFF333333, 0xFF666666, 0xFF999999, 0xFFCCCCCC}; + EXPECT_EQ(storageSize, sizeof(original) / sizeof(original[0])); + unsigned bits[storageSize]; + memcpy(bits, original, sizeof(original)); + SkBitmap bitmap; + bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); + bitmap.setPixels(bits); + SkCanvas canvas; + canvas.setBitmapDevice(bitmap); + if (test & TestTranslate) + canvas.translate(width / 2, 0); + if (test & TestClip) { + SkRect clipRect = {0, height / 2, width, height}; + canvas.clipRect(clipRect); + } + gfx::SkiaBitLocker bitLocker(&canvas); + CGContextRef cgContext = bitLocker.cgContext(); + CGColorRef testColor = CGColorGetConstantColor(kCGColorWhite); + CGContextSetFillColorWithColor(cgContext, testColor); + CGRect cgRect = {{0, 0}, {width, height}}; + CGContextFillRect(cgContext, cgRect); + const unsigned results[][storageSize] = { + {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}, // identity + {0xFF333333, 0xFFFFFFFF, 0xFF999999, 0xFFFFFFFF}, // translate + {0xFF333333, 0xFF666666, 0xFFFFFFFF, 0xFFFFFFFF}, // clip + {0xFF333333, 0xFF666666, 0xFF999999, 0xFFFFFFFF} // translate | clip + }; + for (unsigned index = 0; index < storageSize; index++) + EXPECT_EQ(results[test][index], bits[index]); +} + void SkiaUtilsMacTest::ShapeHelper(int width, int height, bool isred, bool tfbit) { SkBitmap thing(CreateSkBitmap(width, height, isred, tfbit)); @@ -172,4 +215,21 @@ TEST_F(SkiaUtilsMacTest, NSImageRepToSkBitmap) { TestSkBitmap(bitmap, isred); } +TEST_F(SkiaUtilsMacTest, BitLocker_Identity) { + RunBitLockerTest(SkiaUtilsMacTest::TestIdentity); +} + +TEST_F(SkiaUtilsMacTest, BitLocker_Translate) { + RunBitLockerTest(SkiaUtilsMacTest::TestTranslate); +} + +TEST_F(SkiaUtilsMacTest, BitLocker_Clip) { + RunBitLockerTest(SkiaUtilsMacTest::TestClip); +} + +TEST_F(SkiaUtilsMacTest, BitLocker_XClip) { + RunBitLockerTest(SkiaUtilsMacTest::TestXClip); +} + } // namespace + |