diff options
Diffstat (limited to 'skia')
-rw-r--r-- | skia/ext/skia_utils_mac.h | 10 | ||||
-rw-r--r-- | skia/ext/skia_utils_mac.mm | 46 | ||||
-rw-r--r-- | skia/ext/skia_utils_mac_unittest.mm | 40 |
3 files changed, 66 insertions, 30 deletions
diff --git a/skia/ext/skia_utils_mac.h b/skia/ext/skia_utils_mac.h index f8ba28c..da290ea 100644 --- a/skia/ext/skia_utils_mac.h +++ b/skia/ext/skia_utils_mac.h @@ -24,12 +24,10 @@ typedef struct _NSSize NSSize; #endif #ifdef __OBJC__ -@class NSBitmapImageRep; @class NSImage; @class NSImageRep; @class NSColor; #else -class NSBitmapImageRep; class NSImage; class NSImageRep; class NSColor; @@ -83,10 +81,6 @@ SK_API SkBitmap NSImageToSkBitmap(NSImage* image, NSSize size, bool is_opaque); SK_API SkBitmap NSImageRepToSkBitmap( NSImageRep* image, NSSize size, bool is_opaque); -// Given an SkBitmap, return an autoreleased NSBitmapImageRep in the generic -// color space. -SK_API NSBitmapImageRep* SkBitmapToNSBitmapImageRep(const SkBitmap& image); - // Given an SkBitmap and a color space, return an autoreleased NSImage. SK_API NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& icon, CGColorSpaceRef colorSpace); @@ -96,6 +90,10 @@ SK_API NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& icon, // TODO(thakis): Remove this -- http://crbug.com/69432 SK_API NSImage* SkBitmapToNSImage(const SkBitmap& icon); +// Given a vector of SkBitmaps, return an NSImage with each bitmap added +// as a representation. +SK_API NSImage* SkBitmapsToNSImage(const std::vector<const SkBitmap*>& bitmaps); + // Returns |[NSImage imageNamed:@"NSApplicationIcon"]| as SkBitmap. SK_API SkBitmap AppplicationIconAtSize(int size); diff --git a/skia/ext/skia_utils_mac.mm b/skia/ext/skia_utils_mac.mm index 48070d1..f359089 100644 --- a/skia/ext/skia_utils_mac.mm +++ b/skia/ext/skia_utils_mac.mm @@ -248,20 +248,6 @@ SkBitmap NSImageRepToSkBitmap(NSImageRep* image, NSSize size, bool is_opaque) { return NSImageOrNSImageRepToSkBitmap(nil, image, size, is_opaque); } -NSBitmapImageRep* SkBitmapToNSBitmapImageRep(const SkBitmap& skiaBitmap) { - base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( - CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); - - // First convert SkBitmap to CGImageRef. - base::mac::ScopedCFTypeRef<CGImageRef> cgimage( - SkCreateCGImageRefWithColorspace(skiaBitmap, color_space)); - - // Now convert to NSBitmapImageRep. - scoped_nsobject<NSBitmapImageRep> bitmap( - [[NSBitmapImageRep alloc] initWithCGImage:cgimage]); - return [bitmap.release() autorelease]; -} - NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& skiaBitmap, CGColorSpaceRef colorSpace) { if (skiaBitmap.isNull()) @@ -286,11 +272,43 @@ 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)); + scoped_nsobject<NSImage> image([[NSImage alloc] init]); + 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.release() autorelease]; +} + SkBitmap AppplicationIconAtSize(int size) { NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; return NSImageToSkBitmap(image, NSMakeSize(size, size), /* is_opaque=*/true); } + SkiaBitLocker::SkiaBitLocker(SkCanvas* canvas) : canvas_(canvas), cgContext_(0) { diff --git a/skia/ext/skia_utils_mac_unittest.mm b/skia/ext/skia_utils_mac_unittest.mm index 1353eb6..24310b0 100644 --- a/skia/ext/skia_utils_mac_unittest.mm +++ b/skia/ext/skia_utils_mac_unittest.mm @@ -187,16 +187,36 @@ TEST_F(SkiaUtilsMacTest, FAILS_BitmapToNSImage_BlueRectangle444) { ShapeHelper(200, 200, false, false); } -TEST_F(SkiaUtilsMacTest, FAILS_BitmapToNSBitmapImageRep_BlueRectangle20x30) { - int width = 20; - int height = 30; - - SkBitmap bitmap(CreateSkBitmap(width, height, false, true)); - NSBitmapImageRep* imageRep = gfx::SkBitmapToNSBitmapImageRep(bitmap); - - EXPECT_DOUBLE_EQ(width, [imageRep size].width); - EXPECT_DOUBLE_EQ(height, [imageRep size].height); - TestImageRep(imageRep, false); +TEST_F(SkiaUtilsMacTest, FAILS_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) { |