diff options
author | amit@chromium.org <amit@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-22 20:53:41 +0000 |
---|---|---|
committer | amit@chromium.org <amit@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-22 20:53:41 +0000 |
commit | c6ed21c4291e11007cae2dc946256faac1b36188 (patch) | |
tree | 326d91d92dfd4358814b12c8091b806dc9393b12 /skia | |
parent | 930c66b72e00d1b1a0b6e12bf1b9ca81c6b58b33 (diff) | |
download | chromium_src-c6ed21c4291e11007cae2dc946256faac1b36188.zip chromium_src-c6ed21c4291e11007cae2dc946256faac1b36188.tar.gz chromium_src-c6ed21c4291e11007cae2dc946256faac1b36188.tar.bz2 |
Revert due to NSImageRepToSkBitmap failure on MAC
Revert 82688 - Use large icon resource pakThis is a part of r82185 that was reverted. The change was reverted because it caused a performance regression.In r82538 and r82584 I re-checked in code to split high-res icons into a seperate resource pak and load it.This change adds code to actually use use the high-res icons.BUG=NoneTEST=NoneReview URL: http://codereview.chromium.org/6897013
TBR=sail@chromium.org
Review URL: http://codereview.chromium.org/6896034
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@82704 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'skia')
-rw-r--r-- | skia/ext/skia_utils_mac.h | 10 | ||||
-rw-r--r-- | skia/ext/skia_utils_mac.mm | 59 | ||||
-rw-r--r-- | skia/ext/skia_utils_mac_unittest.mm | 142 |
3 files changed, 36 insertions, 175 deletions
diff --git a/skia/ext/skia_utils_mac.h b/skia/ext/skia_utils_mac.h index 04d4a1d..0551643 100644 --- a/skia/ext/skia_utils_mac.h +++ b/skia/ext/skia_utils_mac.h @@ -7,7 +7,6 @@ #pragma once #include <CoreGraphics/CGColor.h> -#include <vector> #include "third_party/skia/include/core/SkColor.h" @@ -24,10 +23,8 @@ typedef struct _NSSize NSSize; #ifdef __OBJC__ @class NSImage; -@class NSImageRep; #else class NSImage; -class NSImageRep; #endif namespace gfx { @@ -67,9 +64,6 @@ SkBitmap CGImageToSkBitmap(CGImageRef image); // Draws an NSImage with a given size into a SkBitmap. SkBitmap NSImageToSkBitmap(NSImage* image, NSSize size, bool is_opaque); -// Draws an NSImageRep with a given size into a SkBitmap. -SkBitmap NSImageRepToSkBitmap(NSImageRep* image, NSSize size, bool is_opaque); - // Given an SkBitmap and a color space, return an autoreleased NSImage. NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& icon, CGColorSpaceRef colorSpace); @@ -79,10 +73,6 @@ NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& icon, // TODO(thakis): Remove this -- http://crbug.com/69432 NSImage* SkBitmapToNSImage(const SkBitmap& icon); -// Given a vector of SkBitmaps, return an NSImage with each bitmap added -// as a representation. -NSImage* SkBitmapsToNSImage(const std::vector<const SkBitmap*>& bitmaps); - // Returns |[NSImage imageNamed:@"NSApplicationIcon"]| as SkBitmap. SkBitmap AppplicationIconAtSize(int size); diff --git a/skia/ext/skia_utils_mac.mm b/skia/ext/skia_utils_mac.mm index 2ecb3f2..c78121a 100644 --- a/skia/ext/skia_utils_mac.mm +++ b/skia/ext/skia_utils_mac.mm @@ -8,7 +8,6 @@ #include "base/logging.h" #include "base/mac/scoped_cftyperef.h" -#include "base/memory/scoped_nsobject.h" #include "base/memory/scoped_ptr.h" #include "skia/ext/bitmap_platform_device_mac.h" #include "third_party/skia/include/utils/mac/SkCGUtils.h" @@ -117,20 +116,15 @@ SkBitmap CGImageToSkBitmap(CGImageRef image) { } SkBitmap NSImageToSkBitmap(NSImage* image, NSSize size, bool is_opaque) { - return NSImageRepToSkBitmap([image bestRepresentationForDevice:nil], - size, is_opaque); -} - -SkBitmap NSImageRepToSkBitmap(NSImageRep* image, NSSize size, bool is_opaque) { SkBitmap bitmap; bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width, size.height); - if (!bitmap.allocPixels()) + if (bitmap.allocPixels() != true) return bitmap; // Return |bitmap| which should respond true to isNull(). bitmap.setIsOpaque(is_opaque); base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( - CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); + CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); void* data = bitmap.getPixels(); // Allocate a bitmap context with 4 components per pixel (BGRA). Apple @@ -151,18 +145,21 @@ SkBitmap NSImageRepToSkBitmap(NSImageRep* image, NSSize size, bool is_opaque) { #undef HAS_ARGB_SHIFTS // Something went really wrong. Best guess is that the bitmap data is invalid. - DCHECK(context); + DCHECK(context != NULL); // Save the current graphics context so that we can restore it later. [NSGraphicsContext saveGraphicsState]; // Dummy context that we will draw into. NSGraphicsContext* context_cocoa = - [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]; + [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]; [NSGraphicsContext setCurrentContext:context_cocoa]; // This will stretch any images to |size| if it does not fit or is non-square. - [image drawInRect:NSMakeRect(0, 0, size.width, size.height)]; + [image drawInRect:NSMakeRect(0, 0, size.width, size.height) + fromRect:NSZeroRect + operation:NSCompositeCopy + fraction:1.0]; // Done drawing, restore context. [NSGraphicsContext restoreGraphicsState]; @@ -176,12 +173,13 @@ NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& skiaBitmap, return nil; // First convert SkBitmap to CGImageRef. - base::mac::ScopedCFTypeRef<CGImageRef> cgimage( - SkCreateCGImageRefWithColorspace(skiaBitmap, colorSpace)); + CGImageRef cgimage = + SkCreateCGImageRefWithColorspace(skiaBitmap, colorSpace); // Now convert to NSImage. - scoped_nsobject<NSBitmapImageRep> bitmap( - [[NSBitmapImageRep alloc] initWithCGImage:cgimage]); + NSBitmapImageRep* bitmap = [[[NSBitmapImageRep alloc] + initWithCGImage:cgimage] autorelease]; + CFRelease(cgimage); NSImage* image = [[[NSImage alloc] init] autorelease]; [image addRepresentation:bitmap]; [image setSize:NSMakeSize(skiaBitmap.width(), skiaBitmap.height())]; @@ -194,37 +192,6 @@ NSImage* SkBitmapToNSImage(const SkBitmap& skiaBitmap) { return SkBitmapToNSImageWithColorSpace(skiaBitmap, colorSpace.get()); } -NSImage* SkBitmapsToNSImage(const std::vector<const SkBitmap*>& bitmaps) { - if (bitmaps.empty()) - return nil; - - base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( - CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); - NSImage* image = [[[NSImage alloc] init] autorelease]; - NSSize min_size = NSZeroSize; - - for (std::vector<const SkBitmap*>::const_iterator it = bitmaps.begin(); - it != bitmaps.end(); ++it) { - const SkBitmap& skiaBitmap = **it; - // First convert SkBitmap to CGImageRef. - base::mac::ScopedCFTypeRef<CGImageRef> cgimage( - SkCreateCGImageRefWithColorspace(skiaBitmap, color_space)); - - // Now convert to NSImage. - scoped_nsobject<NSBitmapImageRep> bitmap( - [[NSBitmapImageRep alloc] initWithCGImage:cgimage]); - [image addRepresentation:bitmap]; - - if (min_size.width == 0 || min_size.width > skiaBitmap.width()) { - min_size.width = skiaBitmap.width(); - min_size.height = skiaBitmap.height(); - } - } - - [image setSize:min_size]; - return image; -} - SkBitmap AppplicationIconAtSize(int size) { NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; return NSImageToSkBitmap(image, NSMakeSize(size, size), /* is_opaque=*/true); diff --git a/skia/ext/skia_utils_mac_unittest.mm b/skia/ext/skia_utils_mac_unittest.mm index 8f1377a..7401f11 100644 --- a/skia/ext/skia_utils_mac_unittest.mm +++ b/skia/ext/skia_utils_mac_unittest.mm @@ -9,60 +9,39 @@ namespace { class SkiaUtilsMacTest : public testing::Test { public: - // Creates a red or blue bitmap. - SkBitmap CreateSkBitmap(int width, int height, bool isred, bool tfbit); - - // Creates a red or blue image. - NSImage* CreateNSImage(int width, int height, bool isred); - - // Checks that the given bitmap rep is actually red or blue. - void TestImageRep(NSBitmapImageRep* imageRep, bool isred); - - // Checks that the given bitmap is actually red or blue. - void TestSkBitmap(const SkBitmap& bitmap, bool isred); - // If not red, is blue. // If not tfbit (twenty-four-bit), is 444. void ShapeHelper(int width, int height, bool isred, bool tfbit); }; -SkBitmap SkiaUtilsMacTest::CreateSkBitmap(int width, int height, - bool isred, bool tfbit) { - SkBitmap bitmap; +void SkiaUtilsMacTest::ShapeHelper(int width, int height, + bool isred, bool tfbit) { + SkBitmap thing; if (tfbit) - bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); + thing.setConfig(SkBitmap::kARGB_8888_Config, width, height); else - bitmap.setConfig(SkBitmap::kARGB_4444_Config, width, height); - bitmap.allocPixels(); + thing.setConfig(SkBitmap::kARGB_4444_Config, width, height); + thing.allocPixels(); if (isred) - bitmap.eraseRGB(0xff, 0, 0); + thing.eraseRGB(0xff, 0, 0); else - bitmap.eraseRGB(0, 0, 0xff); + thing.eraseRGB(0, 0, 0xff); - return bitmap; -} + // Confirm size + NSImage* image = gfx::SkBitmapToNSImage(thing); + EXPECT_DOUBLE_EQ([image size].width, (double)width); + EXPECT_DOUBLE_EQ([image size].height, (double)height); -NSImage* SkiaUtilsMacTest::CreateNSImage(int width, int height, bool isred) { - NSImage* image = [[[NSImage alloc] initWithSize:NSMakeSize(width, height)] - autorelease]; + // Get the color of a pixel and make sure it looks fine [image lockFocus]; - if (isred) - [[NSColor colorWithDeviceRed:1.0 green:0.0 blue:0.0 alpha:1.0] set]; - else - [[NSColor colorWithDeviceRed:0.0 green:0.0 blue:1.0 alpha:1.0] set]; - NSRectFill(NSMakeRect(0, 0, width, height)); - [image unlockFocus]; - return image; -} -void SkiaUtilsMacTest::TestImageRep(NSBitmapImageRep* imageRep, bool isred) { - // Get the color of a pixel and make sure it looks fine - int x = [imageRep size].width > 17 ? 17 : 0; - int y = [imageRep size].height > 17 ? 17 : 0; - NSColor* color = [imageRep colorAtX:x y:y]; + int x = width > 17 ? 17 : 0; + int y = height > 17 ? 17 : 0; + NSColor* color = NSReadPixel(NSMakePoint(x, y)); CGFloat red = 0, green = 0, blue = 0, alpha = 0; + [image unlockFocus]; // SkBitmapToNSImage returns a bitmap in the calibrated color space (sRGB), // while NSReadPixel returns a color in the device color space. Convert back @@ -73,45 +52,14 @@ void SkiaUtilsMacTest::TestImageRep(NSBitmapImageRep* imageRep, bool isred) { // Be tolerant of floating point rounding and lossy color space conversions. if (isred) { - EXPECT_NEAR(red, 1.0, 0.025); - EXPECT_NEAR(blue, 0.0, 0.025); - } else { - EXPECT_NEAR(red, 0.0, 0.025); - EXPECT_NEAR(blue, 1.0, 0.025); - } - EXPECT_NEAR(green, 0.0, 0.025); - EXPECT_NEAR(alpha, 1.0, 0.025); -} - -void SkiaUtilsMacTest::TestSkBitmap(const SkBitmap& bitmap, bool isred) { - int x = bitmap.width() > 17 ? 17 : 0; - int y = bitmap.height() > 17 ? 17 : 0; - SkColor color = bitmap.getColor(x, y); - - if (isred) { - EXPECT_EQ(255u, SkColorGetR(color)); - EXPECT_EQ(0u, SkColorGetB(color)); + EXPECT_GT(red, 0.95); + EXPECT_LT(blue, 0.05); } else { - EXPECT_EQ(0u, SkColorGetR(color)); - EXPECT_EQ(255u, SkColorGetB(color)); + EXPECT_LT(red, 0.05); + EXPECT_GT(blue, 0.95); } - EXPECT_EQ(0u, SkColorGetG(color)); - EXPECT_EQ(255u, SkColorGetA(color)); -} - -void SkiaUtilsMacTest::ShapeHelper(int width, int height, - bool isred, bool tfbit) { - SkBitmap thing(CreateSkBitmap(width, height, isred, tfbit)); - - // Confirm size - NSImage* image = gfx::SkBitmapToNSImage(thing); - EXPECT_DOUBLE_EQ([image size].width, (double)width); - EXPECT_DOUBLE_EQ([image size].height, (double)height); - - EXPECT_TRUE([[image representations] count] == 1); - EXPECT_TRUE([[[image representations] lastObject] - isKindOfClass:[NSBitmapImageRep class]]); - TestImageRep([[image representations] lastObject], isred); + EXPECT_LT(green, 0.05); + EXPECT_GT(alpha, 0.95); } TEST_F(SkiaUtilsMacTest, FAILS_BitmapToNSImage_RedSquare64x64) { @@ -126,48 +74,4 @@ TEST_F(SkiaUtilsMacTest, FAILS_BitmapToNSImage_BlueRectangle444) { ShapeHelper(200, 200, false, false); } -TEST_F(SkiaUtilsMacTest, MultipleBitmapsToNSImage) { - int redWidth = 10; - int redHeight = 15; - int blueWidth = 20; - int blueHeight = 30; - - SkBitmap redBitmap(CreateSkBitmap(redWidth, redHeight, true, true)); - SkBitmap blueBitmap(CreateSkBitmap(blueWidth, blueHeight, false, true)); - std::vector<const SkBitmap*> bitmaps; - bitmaps.push_back(&redBitmap); - bitmaps.push_back(&blueBitmap); - - NSImage* image = gfx::SkBitmapsToNSImage(bitmaps); - - // Image size should be the same as the smallest bitmap. - EXPECT_DOUBLE_EQ(redWidth, [image size].width); - EXPECT_DOUBLE_EQ(redHeight, [image size].height); - - EXPECT_EQ(2u, [[image representations] count]); - - for (NSBitmapImageRep* imageRep in [image representations]) { - bool isred = [imageRep size].width == redWidth; - if (isred) { - EXPECT_DOUBLE_EQ(redHeight, [imageRep size].height); - } else { - EXPECT_DOUBLE_EQ(blueWidth, [imageRep size].width); - EXPECT_DOUBLE_EQ(blueHeight, [imageRep size].height); - } - TestImageRep(imageRep, isred); - } -} - -TEST_F(SkiaUtilsMacTest, NSImageRepToSkBitmap) { - int width = 10; - int height = 15; - bool isred = true; - - NSImage* image = CreateNSImage(width, height, isred); - EXPECT_EQ(1u, [[image representations] count]); - NSBitmapImageRep* imageRep = [[image representations] lastObject]; - SkBitmap bitmap(gfx::NSImageRepToSkBitmap(imageRep, [image size], false)); - TestSkBitmap(bitmap, isred); -} - } // namespace |