blob: be53f00040e42eade73abb9b2515a90ecd004375 [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"
reed@android.coma545a552009-06-29 17:07:19 +00003#include "SkColorPriv.h"
reed@android.com758b1292008-12-18 17:54:12 +00004
5extern CGImageRef SkCreateCGImageRef(const SkBitmap&);
6
reed@android.com2b26cac2008-12-22 02:33:11 +00007static void SkBitmap_ReleaseInfo(void* info, const void* pixelData, size_t size) {
reed@android.com758b1292008-12-18 17:54:12 +00008 SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(info);
9 delete bitmap;
10}
11
reed@android.coma545a552009-06-29 17:07:19 +000012#define HAS_ARGB_SHIFTS(a, r, g, b) \
13 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \
14 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b))
15
reed@android.com758b1292008-12-18 17:54:12 +000016static SkBitmap* prepareForImageRef(const SkBitmap& bm,
17 size_t* bitsPerComponent,
18 CGBitmapInfo* info) {
reed@android.coma545a552009-06-29 17:07:19 +000019#if 0
20 SkDebugf("---- %d %d %d %d\n", SK_A32_SHIFT, SK_R32_SHIFT,
21 SK_G32_SHIFT, SK_B32_SHIFT);
22#endif
23
reed@android.com758b1292008-12-18 17:54:12 +000024 switch (bm.config()) {
25 case SkBitmap::kARGB_8888_Config:
26 *bitsPerComponent = 8;
reed@android.com8ede4922009-06-22 20:04:33 +000027#if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 0, 8, 16) \
28 || defined(SK_CPU_BENDIAN) && HAS_ARGB_SHIFTS(0, 24, 16, 8)
reed@android.com758b1292008-12-18 17:54:12 +000029 *info = kCGBitmapByteOrder32Big |
30 kCGImageAlphaPremultipliedLast;
reed@android.com8ede4922009-06-22 20:04:33 +000031#elif defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0) \
32 || defined(SK_CPU_BENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0)
33 // Matches the CGBitmapInfo that Apple recommends for best
34 // performance, used by google chrome.
35 *info = kCGBitmapByteOrder32Host |
36 kCGImageAlphaPremultipliedFirst;
37#else
38// ...add more formats as required...
39#warning Cannot convert SkBitmap to CGImageRef with these shiftmasks. \
40 This will probably not work.
41 // Legacy behavior. Perhaps turn this into an error at some
42 // point.
43 *info = kCGBitmapByteOrder32Big |
44 kCGImageAlphaPremultipliedLast;
45#endif
reed@android.com758b1292008-12-18 17:54:12 +000046 break;
reed@android.com0680d6c2008-12-19 19:46:15 +000047 case SkBitmap::kRGB_565_Config:
48 // doesn't see quite right. Are they thinking 1555?
49 *bitsPerComponent = 5;
50 *info = kCGBitmapByteOrder16Little;
51 break;
52 case SkBitmap::kARGB_4444_Config:
53 *bitsPerComponent = 4;
54 *info = kCGBitmapByteOrder16Little | kCGImageAlphaPremultipliedLast;
55 break;
reed@android.com758b1292008-12-18 17:54:12 +000056 default:
57 return NULL;
58 }
59
60 return new SkBitmap(bm);
61}
62
reed@android.coma545a552009-06-29 17:07:19 +000063#undef HAS_ARGB_SHIFTS
64
reed@android.com758b1292008-12-18 17:54:12 +000065CGImageRef SkCreateCGImageRef(const SkBitmap& bm) {
66 size_t bitsPerComponent;
67 CGBitmapInfo info;
68
69 SkBitmap* bitmap = prepareForImageRef(bm, &bitsPerComponent, &info);
70 if (NULL == bitmap) {
71 return NULL;
72 }
73
74 const int w = bitmap->width();
75 const int h = bitmap->height();
76 const size_t s = bitmap->getSize();
77
reed@android.com758b1292008-12-18 17:54:12 +000078 // our provider "owns" the bitmap*, and will take care of deleting it
reed@android.com2b26cac2008-12-22 02:33:11 +000079 // we initially lock it, so we can access the pixels. The bitmap will be deleted in the release
80 // proc, which will in turn unlock the pixels
81 bitmap->lockPixels();
82 CGDataProviderRef dataRef = CGDataProviderCreateWithData(bitmap, bitmap->getPixels(), s,
83 SkBitmap_ReleaseInfo);
84
reed@android.com758b1292008-12-18 17:54:12 +000085 CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
86 CGImageRef ref = CGImageCreate(w, h, bitsPerComponent,
87 bitmap->bytesPerPixel() * 8,
88 bitmap->rowBytes(), space, info, dataRef,
89 NULL, false, kCGRenderingIntentDefault);
90 CGColorSpaceRelease(space);
91 CGDataProviderRelease(dataRef);
92 return ref;
93}
94
95