| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "skia/ext/skia_utils_mac.h" | 5 #include "skia/ext/skia_utils_mac.h" |
| 6 | 6 |
| 7 #import <AppKit/AppKit.h> | 7 #import <AppKit/AppKit.h> |
| 8 | 8 |
| 9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "third_party/skia/include/core/SkCanvas.h" | |
| 13 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 12 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
| 14 | 13 |
| 15 namespace { | 14 namespace { |
| 16 | 15 |
| 17 class SkiaUtilsMacTest : public testing::Test { | 16 class SkiaUtilsMacTest : public testing::Test { |
| 18 public: | 17 public: |
| 19 // Creates a red or blue bitmap. | 18 // Creates a red or blue bitmap. |
| 20 SkBitmap CreateSkBitmap(int width, int height, bool isred, bool tfbit); | 19 SkBitmap CreateSkBitmap(int width, int height, bool isred, bool tfbit); |
| 21 | 20 |
| 22 // Creates a red image. | 21 // Creates a red image. |
| 23 NSImage* CreateNSImage(int width, int height); | 22 NSImage* CreateNSImage(int width, int height); |
| 24 | 23 |
| 25 // Checks that the given bitmap rep is actually red or blue. | 24 // Checks that the given bitmap rep is actually red or blue. |
| 26 void TestImageRep(NSBitmapImageRep* imageRep, bool isred); | 25 void TestImageRep(NSBitmapImageRep* imageRep, bool isred); |
| 27 | 26 |
| 28 // Checks that the given bitmap is red. | 27 // Checks that the given bitmap is red. |
| 29 void TestSkBitmap(const SkBitmap& bitmap); | 28 void TestSkBitmap(const SkBitmap& bitmap); |
| 30 | 29 |
| 31 enum BitLockerTest { | |
| 32 TestIdentity = 0, | |
| 33 TestTranslate = 1, | |
| 34 TestClip = 2, | |
| 35 TestXClip = TestTranslate | TestClip, | |
| 36 }; | |
| 37 void RunBitLockerTest(BitLockerTest test); | |
| 38 | |
| 39 // If not red, is blue. | 30 // If not red, is blue. |
| 40 // If not tfbit (twenty-four-bit), is 444. | 31 // If not tfbit (twenty-four-bit), is 444. |
| 41 void ShapeHelper(int width, int height, bool isred, bool tfbit); | 32 void ShapeHelper(int width, int height, bool isred, bool tfbit); |
| 42 }; | 33 }; |
| 43 | 34 |
| 44 SkBitmap SkiaUtilsMacTest::CreateSkBitmap(int width, int height, | 35 SkBitmap SkiaUtilsMacTest::CreateSkBitmap(int width, int height, |
| 45 bool isred, bool tfbit) { | 36 bool isred, bool tfbit) { |
| 46 SkColorType ct = tfbit ? kN32_SkColorType : kARGB_4444_SkColorType; | 37 SkColorType ct = tfbit ? kN32_SkColorType : kARGB_4444_SkColorType; |
| 47 SkImageInfo info = SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType); | 38 SkImageInfo info = SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType); |
| 48 | 39 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 int x = bitmap.width() > 17 ? 17 : 0; | 114 int x = bitmap.width() > 17 ? 17 : 0; |
| 124 int y = bitmap.height() > 17 ? 17 : 0; | 115 int y = bitmap.height() > 17 ? 17 : 0; |
| 125 SkColor color = bitmap.getColor(x, y); | 116 SkColor color = bitmap.getColor(x, y); |
| 126 | 117 |
| 127 EXPECT_EQ(255u, SkColorGetR(color)); | 118 EXPECT_EQ(255u, SkColorGetR(color)); |
| 128 EXPECT_EQ(0u, SkColorGetB(color)); | 119 EXPECT_EQ(0u, SkColorGetB(color)); |
| 129 EXPECT_EQ(0u, SkColorGetG(color)); | 120 EXPECT_EQ(0u, SkColorGetG(color)); |
| 130 EXPECT_EQ(255u, SkColorGetA(color)); | 121 EXPECT_EQ(255u, SkColorGetA(color)); |
| 131 } | 122 } |
| 132 | 123 |
| 133 void SkiaUtilsMacTest::RunBitLockerTest(BitLockerTest test) { | |
| 134 const unsigned width = 2; | |
| 135 const unsigned height = 2; | |
| 136 const unsigned storageSize = width * height; | |
| 137 const unsigned original[] = {0xFF333333, 0xFF666666, 0xFF999999, 0xFFCCCCCC}; | |
| 138 EXPECT_EQ(storageSize, sizeof(original) / sizeof(original[0])); | |
| 139 unsigned bits[storageSize]; | |
| 140 memcpy(bits, original, sizeof(original)); | |
| 141 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); | |
| 142 SkBitmap bitmap; | |
| 143 bitmap.installPixels(info, bits, info.minRowBytes()); | |
| 144 | |
| 145 SkCanvas canvas(bitmap); | |
| 146 if (test & TestTranslate) | |
| 147 canvas.translate(width / 2, 0); | |
| 148 if (test & TestClip) { | |
| 149 SkRect clipRect = {0, height / 2, width, height}; | |
| 150 canvas.clipRect(clipRect); | |
| 151 } | |
| 152 { | |
| 153 SkIRect clip = SkIRect::MakeSize(canvas.getBaseLayerSize()). | |
| 154 makeOffset((test & TestTranslate) ? - (static_cast<int>(width)) / 2 : 0,
0); | |
| 155 skia::SkiaBitLocker bitLocker(&canvas, clip); | |
| 156 CGContextRef cgContext = bitLocker.cgContext(); | |
| 157 CGColorRef testColor = CGColorGetConstantColor(kCGColorWhite); | |
| 158 CGContextSetFillColorWithColor(cgContext, testColor); | |
| 159 CGRect cgRect = {{0, 0}, {width, height}}; | |
| 160 CGContextFillRect(cgContext, cgRect); | |
| 161 } | |
| 162 const unsigned results[][storageSize] = { | |
| 163 {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}, // identity | |
| 164 {0xFF333333, 0xFFFFFFFF, 0xFF999999, 0xFFFFFFFF}, // translate | |
| 165 {0xFF333333, 0xFF666666, 0xFFFFFFFF, 0xFFFFFFFF}, // clip | |
| 166 {0xFF333333, 0xFF666666, 0xFF999999, 0xFFFFFFFF} // translate | clip | |
| 167 }; | |
| 168 for (unsigned index = 0; index < storageSize; index++) | |
| 169 EXPECT_EQ(results[test][index], bits[index]); | |
| 170 } | |
| 171 | |
| 172 void SkiaUtilsMacTest::ShapeHelper(int width, int height, | 124 void SkiaUtilsMacTest::ShapeHelper(int width, int height, |
| 173 bool isred, bool tfbit) { | 125 bool isred, bool tfbit) { |
| 174 SkBitmap thing(CreateSkBitmap(width, height, isred, tfbit)); | 126 SkBitmap thing(CreateSkBitmap(width, height, isred, tfbit)); |
| 175 | 127 |
| 176 // Confirm size | 128 // Confirm size |
| 177 NSImage* image = skia::SkBitmapToNSImage(thing); | 129 NSImage* image = skia::SkBitmapToNSImage(thing); |
| 178 EXPECT_DOUBLE_EQ([image size].width, (double)width); | 130 EXPECT_DOUBLE_EQ([image size].width, (double)width); |
| 179 EXPECT_DOUBLE_EQ([image size].height, (double)height); | 131 EXPECT_DOUBLE_EQ([image size].height, (double)height); |
| 180 | 132 |
| 181 EXPECT_TRUE([[image representations] count] == 1); | 133 EXPECT_TRUE([[image representations] count] == 1); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 NSImage* image = CreateNSImage(width, height); | 169 NSImage* image = CreateNSImage(width, height); |
| 218 EXPECT_EQ(1u, [[image representations] count]); | 170 EXPECT_EQ(1u, [[image representations] count]); |
| 219 NSBitmapImageRep* imageRep = base::mac::ObjCCastStrict<NSBitmapImageRep>( | 171 NSBitmapImageRep* imageRep = base::mac::ObjCCastStrict<NSBitmapImageRep>( |
| 220 [[image representations] lastObject]); | 172 [[image representations] lastObject]); |
| 221 NSColorSpace* colorSpace = [NSColorSpace genericRGBColorSpace]; | 173 NSColorSpace* colorSpace = [NSColorSpace genericRGBColorSpace]; |
| 222 SkBitmap bitmap(skia::NSImageRepToSkBitmapWithColorSpace( | 174 SkBitmap bitmap(skia::NSImageRepToSkBitmapWithColorSpace( |
| 223 imageRep, [image size], false, [colorSpace CGColorSpace])); | 175 imageRep, [image size], false, [colorSpace CGColorSpace])); |
| 224 TestSkBitmap(bitmap); | 176 TestSkBitmap(bitmap); |
| 225 } | 177 } |
| 226 | 178 |
| 227 TEST_F(SkiaUtilsMacTest, BitLocker_Identity) { | |
| 228 RunBitLockerTest(SkiaUtilsMacTest::TestIdentity); | |
| 229 } | |
| 230 | |
| 231 TEST_F(SkiaUtilsMacTest, BitLocker_Translate) { | |
| 232 RunBitLockerTest(SkiaUtilsMacTest::TestTranslate); | |
| 233 } | |
| 234 | |
| 235 TEST_F(SkiaUtilsMacTest, BitLocker_Clip) { | |
| 236 RunBitLockerTest(SkiaUtilsMacTest::TestClip); | |
| 237 } | |
| 238 | |
| 239 TEST_F(SkiaUtilsMacTest, BitLocker_XClip) { | |
| 240 RunBitLockerTest(SkiaUtilsMacTest::TestXClip); | |
| 241 } | |
| 242 | |
| 243 } // namespace | 179 } // namespace |
| 244 | 180 |
| OLD | NEW |