blob: 5c96e21ff60d027d5ff22b0934d6fd094e6fcd1c [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.com32a42492009-07-10 03:33:52 +000019 bool upscaleTo32 = false;
reed@android.coma545a552009-06-29 17:07:19 +000020
reed@android.com758b1292008-12-18 17:54:12 +000021 switch (bm.config()) {
reed@android.com32a42492009-07-10 03:33:52 +000022 case SkBitmap::kRGB_565_Config:
23 upscaleTo32 = true;
24 // fall through
reed@android.com758b1292008-12-18 17:54:12 +000025 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.com32a42492009-07-10 03:33:52 +000047#if 0
reed@android.com0680d6c2008-12-19 19:46:15 +000048 case SkBitmap::kRGB_565_Config:
49 // doesn't see quite right. Are they thinking 1555?
50 *bitsPerComponent = 5;
51 *info = kCGBitmapByteOrder16Little;
52 break;
reed@android.com32a42492009-07-10 03:33:52 +000053#endif
reed@android.com0680d6c2008-12-19 19:46:15 +000054 case SkBitmap::kARGB_4444_Config:
55 *bitsPerComponent = 4;
56 *info = kCGBitmapByteOrder16Little | kCGImageAlphaPremultipliedLast;
57 break;
reed@android.com758b1292008-12-18 17:54:12 +000058 default:
59 return NULL;
60 }
61
reed@android.com32a42492009-07-10 03:33:52 +000062 SkBitmap* copy;
63 if (upscaleTo32) {
64 copy = new SkBitmap;
65 // here we make a ceep copy of the pixels, since CG won't take our
66 // 565 directly
67 bm.copyTo(copy, SkBitmap::kARGB_8888_Config);
68 } else {
69 copy = new SkBitmap(bm);
70 }
71 return copy;
reed@android.com758b1292008-12-18 17:54:12 +000072}
73
reed@android.coma545a552009-06-29 17:07:19 +000074#undef HAS_ARGB_SHIFTS
75
reed@android.com758b1292008-12-18 17:54:12 +000076CGImageRef SkCreateCGImageRef(const SkBitmap& bm) {
77 size_t bitsPerComponent;
78 CGBitmapInfo info;
79
80 SkBitmap* bitmap = prepareForImageRef(bm, &bitsPerComponent, &info);
81 if (NULL == bitmap) {
82 return NULL;
83 }
84
85 const int w = bitmap->width();
86 const int h = bitmap->height();
87 const size_t s = bitmap->getSize();
88
reed@android.com758b1292008-12-18 17:54:12 +000089 // our provider "owns" the bitmap*, and will take care of deleting it
reed@android.com2b26cac2008-12-22 02:33:11 +000090 // we initially lock it, so we can access the pixels. The bitmap will be deleted in the release
91 // proc, which will in turn unlock the pixels
92 bitmap->lockPixels();
93 CGDataProviderRef dataRef = CGDataProviderCreateWithData(bitmap, bitmap->getPixels(), s,
94 SkBitmap_ReleaseInfo);
95
reed@android.com758b1292008-12-18 17:54:12 +000096 CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
97 CGImageRef ref = CGImageCreate(w, h, bitsPerComponent,
98 bitmap->bytesPerPixel() * 8,
99 bitmap->rowBytes(), space, info, dataRef,
100 NULL, false, kCGRenderingIntentDefault);
101 CGColorSpaceRelease(space);
102 CGDataProviderRelease(dataRef);
103 return ref;
104}
105
106