blob: a1d7cc4eef3924ed5852b4df73fe9be85a8dbb47 [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@google.com62f46592011-01-05 15:49:43 +000027 *info = kCGBitmapByteOrder32Big | 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.
reed@google.com62f46592011-01-05 15:49:43 +000032 *info = kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst;
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@google.com62f46592011-01-05 15:49:43 +000039 *info = kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast;
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.com32a42492009-07-10 03:33:52 +000057 SkBitmap* copy;
58 if (upscaleTo32) {
59 copy = new SkBitmap;
60 // here we make a ceep copy of the pixels, since CG won't take our
61 // 565 directly
62 bm.copyTo(copy, SkBitmap::kARGB_8888_Config);
63 } else {
64 copy = new SkBitmap(bm);
65 }
66 return copy;
reed@android.com758b1292008-12-18 17:54:12 +000067}
68
reed@android.coma545a552009-06-29 17:07:19 +000069#undef HAS_ARGB_SHIFTS
70
reed@android.com38669c12011-01-03 13:48:50 +000071CGImageRef SkCreateCGImageRefWithColorspace(const SkBitmap& bm,
72 CGColorSpaceRef colorSpace) {
73 size_t bitsPerComponent SK_INIT_TO_AVOID_WARNING;
74 CGBitmapInfo info SK_INIT_TO_AVOID_WARNING;
reed@android.com758b1292008-12-18 17:54:12 +000075
76 SkBitmap* bitmap = prepareForImageRef(bm, &bitsPerComponent, &info);
77 if (NULL == bitmap) {
78 return NULL;
79 }
80
81 const int w = bitmap->width();
82 const int h = bitmap->height();
83 const size_t s = bitmap->getSize();
84
reed@android.com758b1292008-12-18 17:54:12 +000085 // our provider "owns" the bitmap*, and will take care of deleting it
reed@android.com2b26cac2008-12-22 02:33:11 +000086 // we initially lock it, so we can access the pixels. The bitmap will be deleted in the release
87 // proc, which will in turn unlock the pixels
88 bitmap->lockPixels();
89 CGDataProviderRef dataRef = CGDataProviderCreateWithData(bitmap, bitmap->getPixels(), s,
90 SkBitmap_ReleaseInfo);
91
reed@android.com38669c12011-01-03 13:48:50 +000092 bool releaseColorSpace = false;
93 if (NULL == colorSpace) {
94 colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
95 releaseColorSpace = true;
96 }
97
reed@android.com758b1292008-12-18 17:54:12 +000098 CGImageRef ref = CGImageCreate(w, h, bitsPerComponent,
99 bitmap->bytesPerPixel() * 8,
reed@android.com38669c12011-01-03 13:48:50 +0000100 bitmap->rowBytes(), colorSpace, info, dataRef,
reed@android.com758b1292008-12-18 17:54:12 +0000101 NULL, false, kCGRenderingIntentDefault);
reed@android.com38669c12011-01-03 13:48:50 +0000102
103 if (releaseColorSpace) {
104 CGColorSpaceRelease(colorSpace);
105 }
reed@android.com758b1292008-12-18 17:54:12 +0000106 CGDataProviderRelease(dataRef);
107 return ref;
108}
109
reed@android.comf2b98d62010-12-20 18:26:13 +0000110void SkCGDrawBitmap(CGContextRef cg, const SkBitmap& bm, float x, float y) {
111 CGImageRef img = SkCreateCGImageRef(bm);
112
113 if (img) {
114 CGRect r = CGRectMake(0, 0, bm.width(), bm.height());
reed@google.com62f46592011-01-05 15:49:43 +0000115
reed@android.comf2b98d62010-12-20 18:26:13 +0000116 CGContextSaveGState(cg);
117 CGContextTranslateCTM(cg, x, r.size.height + y);
118 CGContextScaleCTM(cg, 1, -1);
reed@google.com62f46592011-01-05 15:49:43 +0000119
reed@android.comf2b98d62010-12-20 18:26:13 +0000120 CGContextDrawImage(cg, r, img);
reed@google.com62f46592011-01-05 15:49:43 +0000121
reed@android.comf2b98d62010-12-20 18:26:13 +0000122 CGContextRestoreGState(cg);
reed@google.com62f46592011-01-05 15:49:43 +0000123
reed@android.comf2b98d62010-12-20 18:26:13 +0000124 CGImageRelease(img);
125 }
126}
127
128
reed@android.com758b1292008-12-18 17:54:12 +0000129