blob: dea443c13c3f19056464d41333b49fb1d96d39d5 [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
reed@android.com2b26cac2008-12-22 02:33:11 +00005static void SkBitmap_ReleaseInfo(void* info, const void* pixelData, size_t size) {
reed@android.com758b1292008-12-18 17:54:12 +00006 SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(info);
7 delete bitmap;
8}
9
reed@android.coma545a552009-06-29 17:07:19 +000010#define HAS_ARGB_SHIFTS(a, r, g, b) \
11 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \
12 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b))
13
reed@android.com758b1292008-12-18 17:54:12 +000014static SkBitmap* prepareForImageRef(const SkBitmap& bm,
15 size_t* bitsPerComponent,
16 CGBitmapInfo* info) {
reed@android.com32a42492009-07-10 03:33:52 +000017 bool upscaleTo32 = false;
reed@android.coma545a552009-06-29 17:07:19 +000018
reed@android.com758b1292008-12-18 17:54:12 +000019 switch (bm.config()) {
reed@android.com32a42492009-07-10 03:33:52 +000020 case SkBitmap::kRGB_565_Config:
21 upscaleTo32 = true;
22 // fall through
reed@android.com758b1292008-12-18 17:54:12 +000023 case SkBitmap::kARGB_8888_Config:
24 *bitsPerComponent = 8;
reed@android.com8ede4922009-06-22 20:04:33 +000025#if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 0, 8, 16) \
26 || defined(SK_CPU_BENDIAN) && HAS_ARGB_SHIFTS(0, 24, 16, 8)
reed@android.comf2b98d62010-12-20 18:26:13 +000027 *info = kCGBitmapByteOrder32Big;
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.
reed@android.comf2b98d62010-12-20 18:26:13 +000032 *info = kCGBitmapByteOrder32Little;
reed@android.com8ede4922009-06-22 20:04:33 +000033#else
34// ...add more formats as required...
35#warning Cannot convert SkBitmap to CGImageRef with these shiftmasks. \
36 This will probably not work.
37 // Legacy behavior. Perhaps turn this into an error at some
38 // point.
reed@android.comf2b98d62010-12-20 18:26:13 +000039 *info = kCGBitmapByteOrder32Big;
reed@android.com8ede4922009-06-22 20:04:33 +000040#endif
reed@android.com758b1292008-12-18 17:54:12 +000041 break;
reed@android.com32a42492009-07-10 03:33:52 +000042#if 0
reed@android.com0680d6c2008-12-19 19:46:15 +000043 case SkBitmap::kRGB_565_Config:
44 // doesn't see quite right. Are they thinking 1555?
45 *bitsPerComponent = 5;
46 *info = kCGBitmapByteOrder16Little;
47 break;
reed@android.com32a42492009-07-10 03:33:52 +000048#endif
reed@android.com0680d6c2008-12-19 19:46:15 +000049 case SkBitmap::kARGB_4444_Config:
50 *bitsPerComponent = 4;
51 *info = kCGBitmapByteOrder16Little | kCGImageAlphaPremultipliedLast;
52 break;
reed@android.com758b1292008-12-18 17:54:12 +000053 default:
54 return NULL;
55 }
56
reed@android.comf2b98d62010-12-20 18:26:13 +000057 if (!bm.isOpaque()) {
58 *info |= kCGImageAlphaPremultipliedLast;
59 }
60
reed@android.com32a42492009-07-10 03:33:52 +000061 SkBitmap* copy;
62 if (upscaleTo32) {
63 copy = new SkBitmap;
64 // here we make a ceep copy of the pixels, since CG won't take our
65 // 565 directly
66 bm.copyTo(copy, SkBitmap::kARGB_8888_Config);
67 } else {
68 copy = new SkBitmap(bm);
69 }
70 return copy;
reed@android.com758b1292008-12-18 17:54:12 +000071}
72
reed@android.coma545a552009-06-29 17:07:19 +000073#undef HAS_ARGB_SHIFTS
74
reed@android.com38669c12011-01-03 13:48:50 +000075CGImageRef SkCreateCGImageRefWithColorspace(const SkBitmap& bm,
76 CGColorSpaceRef colorSpace) {
77 size_t bitsPerComponent SK_INIT_TO_AVOID_WARNING;
78 CGBitmapInfo info SK_INIT_TO_AVOID_WARNING;
reed@android.com758b1292008-12-18 17:54:12 +000079
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.com38669c12011-01-03 13:48:50 +000096 bool releaseColorSpace = false;
97 if (NULL == colorSpace) {
98 colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
99 releaseColorSpace = true;
100 }
101
reed@android.com758b1292008-12-18 17:54:12 +0000102 CGImageRef ref = CGImageCreate(w, h, bitsPerComponent,
103 bitmap->bytesPerPixel() * 8,
reed@android.com38669c12011-01-03 13:48:50 +0000104 bitmap->rowBytes(), colorSpace, info, dataRef,
reed@android.com758b1292008-12-18 17:54:12 +0000105 NULL, false, kCGRenderingIntentDefault);
reed@android.com38669c12011-01-03 13:48:50 +0000106
107 if (releaseColorSpace) {
108 CGColorSpaceRelease(colorSpace);
109 }
reed@android.com758b1292008-12-18 17:54:12 +0000110 CGDataProviderRelease(dataRef);
111 return ref;
112}
113
reed@android.comf2b98d62010-12-20 18:26:13 +0000114void SkCGDrawBitmap(CGContextRef cg, const SkBitmap& bm, float x, float y) {
115 CGImageRef img = SkCreateCGImageRef(bm);
116
117 if (img) {
118 CGRect r = CGRectMake(0, 0, bm.width(), bm.height());
119
120 CGContextSaveGState(cg);
121 CGContextTranslateCTM(cg, x, r.size.height + y);
122 CGContextScaleCTM(cg, 1, -1);
123
124 CGContextDrawImage(cg, r, img);
125
126 CGContextRestoreGState(cg);
127
128 CGImageRelease(img);
129 }
130}
131
132
reed@android.com758b1292008-12-18 17:54:12 +0000133