SkCreateCGImageRef.cpp revision 8ede49268daa98c4b2bce1c379aeb592f96243ee
1#include "SkCGUtils.h" 2#include "SkBitmap.h" 3 4extern CGImageRef SkCreateCGImageRef(const SkBitmap&); 5 6static void SkBitmap_ReleaseInfo(void* info, const void* pixelData, size_t size) { 7 SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(info); 8 delete bitmap; 9} 10 11static SkBitmap* prepareForImageRef(const SkBitmap& bm, 12 size_t* bitsPerComponent, 13 CGBitmapInfo* info) { 14 switch (bm.config()) { 15 case SkBitmap::kARGB_8888_Config: 16 *bitsPerComponent = 8; 17 // try to match our rgba ordering in SkColorPriv, but take into 18 // account that the data layout could have been overridden in 19 // SkUserConfig. 20#define HAS_ARGB_SHIFTS(a, r, g, b) \ 21 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \ 22 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b)) 23#if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 0, 8, 16) \ 24 || defined(SK_CPU_BENDIAN) && HAS_ARGB_SHIFTS(0, 24, 16, 8) 25 // The default rgba ordering from SkColorPriv.h 26 *info = kCGBitmapByteOrder32Big | 27 kCGImageAlphaPremultipliedLast; 28#elif defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0) \ 29 || defined(SK_CPU_BENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0) 30 // Matches the CGBitmapInfo that Apple recommends for best 31 // performance, used by google chrome. 32 *info = kCGBitmapByteOrder32Host | 33 kCGImageAlphaPremultipliedFirst; 34#else 35// ...add more formats as required... 36#warning Cannot convert SkBitmap to CGImageRef with these shiftmasks. \ 37 This will probably not work. 38 // Legacy behavior. Perhaps turn this into an error at some 39 // point. 40 *info = kCGBitmapByteOrder32Big | 41 kCGImageAlphaPremultipliedLast; 42#endif 43#undef HAS_ARGB_SHIFTS 44 break; 45 case SkBitmap::kRGB_565_Config: 46 // doesn't see quite right. Are they thinking 1555? 47 *bitsPerComponent = 5; 48 *info = kCGBitmapByteOrder16Little; 49 break; 50 case SkBitmap::kARGB_4444_Config: 51 *bitsPerComponent = 4; 52 *info = kCGBitmapByteOrder16Little | kCGImageAlphaPremultipliedLast; 53 break; 54 default: 55 return NULL; 56 } 57 58 return new SkBitmap(bm); 59} 60 61CGImageRef SkCreateCGImageRef(const SkBitmap& bm) { 62 size_t bitsPerComponent; 63 CGBitmapInfo info; 64 65 SkBitmap* bitmap = prepareForImageRef(bm, &bitsPerComponent, &info); 66 if (NULL == bitmap) { 67 return NULL; 68 } 69 70 const int w = bitmap->width(); 71 const int h = bitmap->height(); 72 const size_t s = bitmap->getSize(); 73 74 // our provider "owns" the bitmap*, and will take care of deleting it 75 // we initially lock it, so we can access the pixels. The bitmap will be deleted in the release 76 // proc, which will in turn unlock the pixels 77 bitmap->lockPixels(); 78 CGDataProviderRef dataRef = CGDataProviderCreateWithData(bitmap, bitmap->getPixels(), s, 79 SkBitmap_ReleaseInfo); 80 81 CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); 82 CGImageRef ref = CGImageCreate(w, h, bitsPerComponent, 83 bitmap->bytesPerPixel() * 8, 84 bitmap->rowBytes(), space, info, dataRef, 85 NULL, false, kCGRenderingIntentDefault); 86 CGColorSpaceRelease(space); 87 CGDataProviderRelease(dataRef); 88 return ref; 89} 90 91 92