blob: dca0175188c60d3c926892570d000c0a62ebd7e3 [file] [log] [blame]
reed@android.com0d55f1e2008-12-18 19:26:11 +00001#include "SkCGUtils.h"
reed@android.com758b1292008-12-18 17:54:12 +00002#include "SkBitmap.h"
3
4extern CGImageRef SkCreateCGImageRef(const SkBitmap&);
5
reed@android.com2b26cac2008-12-22 02:33:11 +00006static void SkBitmap_ReleaseInfo(void* info, const void* pixelData, size_t size) {
reed@android.com758b1292008-12-18 17:54:12 +00007 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;
reed@android.com8ede4922009-06-22 20:04:33 +000017 // 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.com758b1292008-12-18 17:54:12 +000026 *info = kCGBitmapByteOrder32Big |
27 kCGImageAlphaPremultipliedLast;
reed@android.com8ede4922009-06-22 20:04:33 +000028#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.com758b1292008-12-18 17:54:12 +000044 break;
reed@android.com0680d6c2008-12-19 19:46:15 +000045 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.com758b1292008-12-18 17:54:12 +000054 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
reed@android.com758b1292008-12-18 17:54:12 +000074 // our provider "owns" the bitmap*, and will take care of deleting it
reed@android.com2b26cac2008-12-22 02:33:11 +000075 // 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.com758b1292008-12-18 17:54:12 +000081 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