reed@android.com | 0d55f1e | 2008-12-18 19:26:11 +0000 | [diff] [blame] | 1 | #include "SkCGUtils.h" |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 2 | #include "SkBitmap.h" |
| 3 | |
| 4 | extern CGImageRef SkCreateCGImageRef(const SkBitmap&); |
| 5 | |
reed@android.com | 2b26cac | 2008-12-22 02:33:11 +0000 | [diff] [blame] | 6 | static void SkBitmap_ReleaseInfo(void* info, const void* pixelData, size_t size) { |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 7 | SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(info); |
| 8 | delete bitmap; |
| 9 | } |
| 10 | |
| 11 | static 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; |
reed@android.com | 8ede492 | 2009-06-22 20:04:33 +0000 | [diff] [blame^] | 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 |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 26 | *info = kCGBitmapByteOrder32Big | |
| 27 | kCGImageAlphaPremultipliedLast; |
reed@android.com | 8ede492 | 2009-06-22 20:04:33 +0000 | [diff] [blame^] | 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 |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 44 | break; |
reed@android.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 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; |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 54 | default: |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | return new SkBitmap(bm); |
| 59 | } |
| 60 | |
| 61 | CGImageRef 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 | |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 74 | // our provider "owns" the bitmap*, and will take care of deleting it |
reed@android.com | 2b26cac | 2008-12-22 02:33:11 +0000 | [diff] [blame] | 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 | |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 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 | |