Chromium Code Reviews| Index: skia/ext/skia_utils_mac.mm |
| diff --git a/skia/ext/skia_utils_mac.mm b/skia/ext/skia_utils_mac.mm |
| index c78121abd8593b40f2d9305d899380f6fe8ffb56..bf3726d4b026207aea40fd57305d110f25d27411 100644 |
| --- a/skia/ext/skia_utils_mac.mm |
| +++ b/skia/ext/skia_utils_mac.mm |
| @@ -167,6 +167,55 @@ SkBitmap NSImageToSkBitmap(NSImage* image, NSSize size, bool is_opaque) { |
| return bitmap; |
| } |
| +SkBitmap NSImageRepToSkBitmap(NSImageRep* image, NSSize size, bool is_opaque) { |
| + SkBitmap bitmap; |
| + bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width, size.height); |
| + if (bitmap.allocPixels() != true) |
|
Robert Sesek
2011/04/14 18:42:44
if (!bitmap.allocPixels())
|
| + return bitmap; // Return |bitmap| which should respond true to isNull(). |
| + |
| + bitmap.setIsOpaque(is_opaque); |
| + |
| + base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( |
| + CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); |
|
Robert Sesek
2011/04/14 18:42:44
nit: indent 4
|
| + void* data = bitmap.getPixels(); |
| + |
| + // Allocate a bitmap context with 4 components per pixel (BGRA). Apple |
| + // recommends these flags for improved CG performance. |
| +#define HAS_ARGB_SHIFTS(a, r, g, b) \ |
| + (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \ |
| + && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b)) |
| +#if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0) |
| + base::mac::ScopedCFTypeRef<CGContextRef> context( |
| + CGBitmapContextCreate(data, size.width, size.height, 8, size.width*4, |
| + color_space, |
| + kCGImageAlphaPremultipliedFirst | |
| + kCGBitmapByteOrder32Host)); |
| +#else |
| +#error We require that Skia's and CoreGraphics's recommended \ |
| + image memory layout match. |
| +#endif |
| +#undef HAS_ARGB_SHIFTS |
| + |
| + // Something went really wrong. Best guess is that the bitmap data is invalid. |
| + DCHECK(context != NULL); |
|
Robert Sesek
2011/04/14 18:42:44
DHCECK(context)
|
| + |
| + // 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]; |
|
Robert Sesek
2011/04/14 18:42:44
nit: indent 4
|
| + [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)]; |
| + |
| + // Done drawing, restore context. |
| + [NSGraphicsContext restoreGraphicsState]; |
| + |
| + return bitmap; |
| +} |
| + |
| NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& skiaBitmap, |
| CGColorSpaceRef colorSpace) { |
| if (skiaBitmap.isNull()) |
| @@ -192,6 +241,38 @@ NSImage* SkBitmapToNSImage(const SkBitmap& skiaBitmap) { |
| return SkBitmapToNSImageWithColorSpace(skiaBitmap, colorSpace.get()); |
| } |
| +NSImage* SkBitmapsToNSImage(const std::vector<SkBitmap*>& bitmaps) { |
| + if (bitmaps.empty()) |
| + return nil; |
| + |
| + base::mac::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( |
| + CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); |
| + NSImage* image = [[[NSImage alloc] init] autorelease]; |
| + NSSize minSize = NSZeroSize; |
| + |
| + for (std::vector<SkBitmap*>::const_iterator it = bitmaps.begin(); |
| + it != bitmaps.end(); ++it) { |
| + const SkBitmap& skiaBitmap = **it; |
| + // First convert SkBitmap to CGImageRef. |
| + CGImageRef cgimage = |
| + SkCreateCGImageRefWithColorspace(skiaBitmap, colorSpace); |
|
Robert Sesek
2011/04/14 18:42:44
nit: indent 4
|
| + |
| + // Now convert to NSImage. |
| + NSBitmapImageRep* bitmap = [[[NSBitmapImageRep alloc] |
|
Robert Sesek
2011/04/14 18:42:44
I'd use scoped_nsobject<> here so you control the
|
| + initWithCGImage:cgimage] autorelease]; |
| + CFRelease(cgimage); |
| + [image addRepresentation:bitmap]; |
|
Robert Sesek
2011/04/14 18:42:44
nit: missing a space
|
| + |
| + if (minSize.width == 0 || minSize.width < skiaBitmap.width()) { |
| + minSize.width = skiaBitmap.width(); |
| + minSize.height = skiaBitmap.height(); |
| + } |
| + } |
| + |
| + [image setSize:minSize]; |
| + return image; |
| +} |
| + |
| SkBitmap AppplicationIconAtSize(int size) { |
| NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; |
| return NSImageToSkBitmap(image, NSMakeSize(size, size), /* is_opaque=*/true); |