blob: 2169bc09cbf9177b30996e1c9d42e7c623617e06 [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.comf2b98d62010-12-20 18:26:13 +000029 *info = kCGBitmapByteOrder32Big;
reed@android.com8ede4922009-06-22 20:04:33 +000030#elif defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0) \
31 || defined(SK_CPU_BENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0)
32 // Matches the CGBitmapInfo that Apple recommends for best
33 // performance, used by google chrome.
reed@android.comf2b98d62010-12-20 18:26:13 +000034 *info = kCGBitmapByteOrder32Little;
reed@android.com8ede4922009-06-22 20:04:33 +000035#else
36// ...add more formats as required...
37#warning Cannot convert SkBitmap to CGImageRef with these shiftmasks. \
38 This will probably not work.
39 // Legacy behavior. Perhaps turn this into an error at some
40 // point.
reed@android.comf2b98d62010-12-20 18:26:13 +000041 *info = kCGBitmapByteOrder32Big;
reed@android.com8ede4922009-06-22 20:04:33 +000042#endif
reed@android.com758b1292008-12-18 17:54:12 +000043 break;
reed@android.com32a42492009-07-10 03:33:52 +000044#if 0
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;
reed@android.com32a42492009-07-10 03:33:52 +000050#endif
reed@android.com0680d6c2008-12-19 19:46:15 +000051 case SkBitmap::kARGB_4444_Config:
52 *bitsPerComponent = 4;
53 *info = kCGBitmapByteOrder16Little | kCGImageAlphaPremultipliedLast;
54 break;
reed@android.com758b1292008-12-18 17:54:12 +000055 default:
56 return NULL;
57 }
58
reed@android.comf2b98d62010-12-20 18:26:13 +000059 if (!bm.isOpaque()) {
60 *info |= kCGImageAlphaPremultipliedLast;
61 }
62
reed@android.com32a42492009-07-10 03:33:52 +000063 SkBitmap* copy;
64 if (upscaleTo32) {
65 copy = new SkBitmap;
66 // here we make a ceep copy of the pixels, since CG won't take our
67 // 565 directly
68 bm.copyTo(copy, SkBitmap::kARGB_8888_Config);
69 } else {
70 copy = new SkBitmap(bm);
71 }
72 return copy;
reed@android.com758b1292008-12-18 17:54:12 +000073}
74
reed@android.coma545a552009-06-29 17:07:19 +000075#undef HAS_ARGB_SHIFTS
76
reed@android.com758b1292008-12-18 17:54:12 +000077CGImageRef SkCreateCGImageRef(const SkBitmap& bm) {
78 size_t bitsPerComponent;
79 CGBitmapInfo info;
80
81 SkBitmap* bitmap = prepareForImageRef(bm, &bitsPerComponent, &info);
82 if (NULL == bitmap) {
83 return NULL;
84 }
85
86 const int w = bitmap->width();
87 const int h = bitmap->height();
88 const size_t s = bitmap->getSize();
89
reed@android.com758b1292008-12-18 17:54:12 +000090 // our provider "owns" the bitmap*, and will take care of deleting it
reed@android.com2b26cac2008-12-22 02:33:11 +000091 // we initially lock it, so we can access the pixels. The bitmap will be deleted in the release
92 // proc, which will in turn unlock the pixels
93 bitmap->lockPixels();
94 CGDataProviderRef dataRef = CGDataProviderCreateWithData(bitmap, bitmap->getPixels(), s,
95 SkBitmap_ReleaseInfo);
96
reed@android.comf2b98d62010-12-20 18:26:13 +000097 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
reed@android.com758b1292008-12-18 17:54:12 +000098 CGImageRef ref = CGImageCreate(w, h, bitsPerComponent,
99 bitmap->bytesPerPixel() * 8,
100 bitmap->rowBytes(), space, info, dataRef,
101 NULL, false, kCGRenderingIntentDefault);
102 CGColorSpaceRelease(space);
103 CGDataProviderRelease(dataRef);
104 return ref;
105}
106
reed@android.comf2b98d62010-12-20 18:26:13 +0000107void SkCGDrawBitmap(CGContextRef cg, const SkBitmap& bm, float x, float y) {
108 CGImageRef img = SkCreateCGImageRef(bm);
109
110 if (img) {
111 CGRect r = CGRectMake(0, 0, bm.width(), bm.height());
112
113 CGContextSaveGState(cg);
114 CGContextTranslateCTM(cg, x, r.size.height + y);
115 CGContextScaleCTM(cg, 1, -1);
116
117 CGContextDrawImage(cg, r, img);
118
119 CGContextRestoreGState(cg);
120
121 CGImageRelease(img);
122 }
123}
124
125
reed@android.com758b1292008-12-18 17:54:12 +0000126