blob: 1514ba6e8ab0f3134a226701f07561e24df3dba2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
mtklein1ee76512015-11-02 10:20:27 -08007
8#include "SkTypes.h"
9#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
10
reed@android.com0d55f1e2008-12-18 19:26:11 +000011#include "SkCGUtils.h"
reed@android.com758b1292008-12-18 17:54:12 +000012#include "SkBitmap.h"
Cary Clarka4083c92017-09-15 11:59:23 -040013#include "SkColorData.h"
reed@android.com758b1292008-12-18 17:54:12 +000014
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +000015static CGBitmapInfo ComputeCGAlphaInfo_RGBA(SkAlphaType at) {
16 CGBitmapInfo info = kCGBitmapByteOrder32Big;
17 switch (at) {
reed44977482015-02-27 10:23:00 -080018 case kUnknown_SkAlphaType:
19 break;
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +000020 case kOpaque_SkAlphaType:
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +000021 info |= kCGImageAlphaNoneSkipLast;
22 break;
23 case kPremul_SkAlphaType:
24 info |= kCGImageAlphaPremultipliedLast;
25 break;
26 case kUnpremul_SkAlphaType:
27 info |= kCGImageAlphaLast;
28 break;
29 }
30 return info;
31}
32
33static CGBitmapInfo ComputeCGAlphaInfo_BGRA(SkAlphaType at) {
34 CGBitmapInfo info = kCGBitmapByteOrder32Little;
35 switch (at) {
reed44977482015-02-27 10:23:00 -080036 case kUnknown_SkAlphaType:
37 break;
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +000038 case kOpaque_SkAlphaType:
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +000039 info |= kCGImageAlphaNoneSkipFirst;
40 break;
41 case kPremul_SkAlphaType:
42 info |= kCGImageAlphaPremultipliedFirst;
43 break;
44 case kUnpremul_SkAlphaType:
45 info |= kCGImageAlphaFirst;
46 break;
47 }
48 return info;
49}
50
reed@android.com2b26cac2008-12-22 02:33:11 +000051static void SkBitmap_ReleaseInfo(void* info, const void* pixelData, size_t size) {
reed@android.com758b1292008-12-18 17:54:12 +000052 SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(info);
53 delete bitmap;
54}
55
rmistry@google.comd6176b02012-08-23 18:14:13 +000056static bool getBitmapInfo(const SkBitmap& bm,
reed@google.com9c16bc02011-06-28 21:52:34 +000057 size_t* bitsPerComponent,
58 CGBitmapInfo* info,
59 bool* upscaleTo32) {
60 if (upscaleTo32) {
61 *upscaleTo32 = false;
62 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000063
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +000064 switch (bm.colorType()) {
65 case kRGB_565_SkColorType:
66#if 0
67 // doesn't see quite right. Are they thinking 1555?
68 *bitsPerComponent = 5;
69 *info = kCGBitmapByteOrder16Little | kCGImageAlphaNone;
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +000070#else
reed@google.com9c16bc02011-06-28 21:52:34 +000071 if (upscaleTo32) {
72 *upscaleTo32 = true;
73 }
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +000074 // now treat like RGBA
reed@android.com758b1292008-12-18 17:54:12 +000075 *bitsPerComponent = 8;
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +000076 *info = ComputeCGAlphaInfo_RGBA(kOpaque_SkAlphaType);
reed@android.com8ede4922009-06-22 20:04:33 +000077#endif
reed@android.com758b1292008-12-18 17:54:12 +000078 break;
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +000079 case kRGBA_8888_SkColorType:
80 *bitsPerComponent = 8;
81 *info = ComputeCGAlphaInfo_RGBA(bm.alphaType());
82 break;
83 case kBGRA_8888_SkColorType:
84 *bitsPerComponent = 8;
85 *info = ComputeCGAlphaInfo_BGRA(bm.alphaType());
86 break;
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +000087 case kARGB_4444_SkColorType:
reed@android.com0680d6c2008-12-19 19:46:15 +000088 *bitsPerComponent = 4;
scroggo@google.com426648e2012-11-12 16:18:43 +000089 *info = kCGBitmapByteOrder16Little;
90 if (bm.isOpaque()) {
91 *info |= kCGImageAlphaNoneSkipLast;
92 } else {
93 *info |= kCGImageAlphaPremultipliedLast;
94 }
reed@android.com0680d6c2008-12-19 19:46:15 +000095 break;
reed@android.com758b1292008-12-18 17:54:12 +000096 default:
reed@google.com9c16bc02011-06-28 21:52:34 +000097 return false;
98 }
99 return true;
100}
101
102static SkBitmap* prepareForImageRef(const SkBitmap& bm,
103 size_t* bitsPerComponent,
104 CGBitmapInfo* info) {
105 bool upscaleTo32;
106 if (!getBitmapInfo(bm, bitsPerComponent, info, &upscaleTo32)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700107 return nullptr;
reed@android.com758b1292008-12-18 17:54:12 +0000108 }
109
reed@android.com32a42492009-07-10 03:33:52 +0000110 SkBitmap* copy;
111 if (upscaleTo32) {
112 copy = new SkBitmap;
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400113 // here we make a deep copy of the pixels, since CG won't take our
reed@android.com32a42492009-07-10 03:33:52 +0000114 // 565 directly
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400115 copy->allocPixels(bm.info().makeColorType(kN32_SkColorType));
116 bm.readPixels(copy->info(), copy->getPixels(), copy->rowBytes(), 0, 0);
reed@android.com32a42492009-07-10 03:33:52 +0000117 } else {
118 copy = new SkBitmap(bm);
119 }
120 return copy;
reed@android.com758b1292008-12-18 17:54:12 +0000121}
122
reed@android.com38669c12011-01-03 13:48:50 +0000123CGImageRef SkCreateCGImageRefWithColorspace(const SkBitmap& bm,
124 CGColorSpaceRef colorSpace) {
125 size_t bitsPerComponent SK_INIT_TO_AVOID_WARNING;
126 CGBitmapInfo info SK_INIT_TO_AVOID_WARNING;
reed@android.com758b1292008-12-18 17:54:12 +0000127
128 SkBitmap* bitmap = prepareForImageRef(bm, &bitsPerComponent, &info);
halcanary96fcdcc2015-08-27 07:41:13 -0700129 if (nullptr == bitmap) {
130 return nullptr;
reed@android.com758b1292008-12-18 17:54:12 +0000131 }
132
133 const int w = bitmap->width();
134 const int h = bitmap->height();
Mike Reedcd284c52017-09-29 15:22:56 -0400135 const size_t s = bitmap->computeByteSize();
reed@android.com758b1292008-12-18 17:54:12 +0000136
reed@android.com758b1292008-12-18 17:54:12 +0000137 // our provider "owns" the bitmap*, and will take care of deleting it
reed@android.com2b26cac2008-12-22 02:33:11 +0000138 CGDataProviderRef dataRef = CGDataProviderCreateWithData(bitmap, bitmap->getPixels(), s,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000139 SkBitmap_ReleaseInfo);
reed@android.com2b26cac2008-12-22 02:33:11 +0000140
reed@android.com38669c12011-01-03 13:48:50 +0000141 bool releaseColorSpace = false;
halcanary96fcdcc2015-08-27 07:41:13 -0700142 if (nullptr == colorSpace) {
reed@google.comc280d112011-01-05 16:07:35 +0000143 colorSpace = CGColorSpaceCreateDeviceRGB();
reed@android.com38669c12011-01-03 13:48:50 +0000144 releaseColorSpace = true;
145 }
146
reed@android.com758b1292008-12-18 17:54:12 +0000147 CGImageRef ref = CGImageCreate(w, h, bitsPerComponent,
148 bitmap->bytesPerPixel() * 8,
reed@android.com38669c12011-01-03 13:48:50 +0000149 bitmap->rowBytes(), colorSpace, info, dataRef,
halcanary96fcdcc2015-08-27 07:41:13 -0700150 nullptr, false, kCGRenderingIntentDefault);
reed@android.com38669c12011-01-03 13:48:50 +0000151
152 if (releaseColorSpace) {
153 CGColorSpaceRelease(colorSpace);
154 }
reed@android.com758b1292008-12-18 17:54:12 +0000155 CGDataProviderRelease(dataRef);
156 return ref;
157}
158
reed@android.comf2b98d62010-12-20 18:26:13 +0000159void SkCGDrawBitmap(CGContextRef cg, const SkBitmap& bm, float x, float y) {
160 CGImageRef img = SkCreateCGImageRef(bm);
161
162 if (img) {
163 CGRect r = CGRectMake(0, 0, bm.width(), bm.height());
reed@google.com62f46592011-01-05 15:49:43 +0000164
reed@android.comf2b98d62010-12-20 18:26:13 +0000165 CGContextSaveGState(cg);
166 CGContextTranslateCTM(cg, x, r.size.height + y);
167 CGContextScaleCTM(cg, 1, -1);
reed@google.com62f46592011-01-05 15:49:43 +0000168
reed@android.comf2b98d62010-12-20 18:26:13 +0000169 CGContextDrawImage(cg, r, img);
reed@google.com62f46592011-01-05 15:49:43 +0000170
reed@android.comf2b98d62010-12-20 18:26:13 +0000171 CGContextRestoreGState(cg);
reed@google.com62f46592011-01-05 15:49:43 +0000172
reed@android.comf2b98d62010-12-20 18:26:13 +0000173 CGImageRelease(img);
174 }
175}
176
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000177///////////////////////////////////////////////////////////////////////////////////////////////////
178
Mike Reed356f7c22017-01-10 11:58:39 -0500179CGContextRef SkCreateCGContext(const SkPixmap& pmap) {
180 CGBitmapInfo cg_bitmap_info = 0;
181 size_t bitsPerComponent = 0;
182 switch (pmap.colorType()) {
183 case kRGBA_8888_SkColorType:
184 bitsPerComponent = 8;
185 cg_bitmap_info = ComputeCGAlphaInfo_RGBA(pmap.alphaType());
186 break;
187 case kBGRA_8888_SkColorType:
188 bitsPerComponent = 8;
189 cg_bitmap_info = ComputeCGAlphaInfo_BGRA(pmap.alphaType());
190 break;
191 default:
192 return nullptr; // no other colortypes are supported (for now)
193 }
194
195 size_t rb = pmap.addr() ? pmap.rowBytes() : 0;
196 CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
197 CGContextRef cg = CGBitmapContextCreate(pmap.writable_addr(), pmap.width(), pmap.height(),
198 bitsPerComponent, rb, cs, cg_bitmap_info);
199 CFRelease(cs);
200 return cg;
201}
202
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000203SK_API bool SkCopyPixelsFromCGImage(const SkImageInfo& info, size_t rowBytes, void* pixels,
204 CGImageRef image) {
205 CGBitmapInfo cg_bitmap_info = 0;
206 size_t bitsPerComponent = 0;
207 switch (info.colorType()) {
208 case kRGBA_8888_SkColorType:
209 bitsPerComponent = 8;
210 cg_bitmap_info = ComputeCGAlphaInfo_RGBA(info.alphaType());
211 break;
212 case kBGRA_8888_SkColorType:
213 bitsPerComponent = 8;
214 cg_bitmap_info = ComputeCGAlphaInfo_BGRA(info.alphaType());
215 break;
216 default:
217 return false; // no other colortypes are supported (for now)
218 }
219
220 CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
221 CGContextRef cg = CGBitmapContextCreate(pixels, info.width(), info.height(), bitsPerComponent,
222 rowBytes, cs, cg_bitmap_info);
223 CFRelease(cs);
halcanary96fcdcc2015-08-27 07:41:13 -0700224 if (nullptr == cg) {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000225 return false;
226 }
227
228 // use this blend mode, to avoid having to erase the pixels first, and to avoid CG performing
229 // any blending (which could introduce errors and be slower).
230 CGContextSetBlendMode(cg, kCGBlendModeCopy);
231
232 CGContextDrawImage(cg, CGRectMake(0, 0, info.width(), info.height()), image);
233 CGContextRelease(cg);
234 return true;
235}
236
Mike Reed463c8482016-12-21 12:01:12 -0500237bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef image) {
238 const int width = SkToInt(CGImageGetWidth(image));
239 const int height = SkToInt(CGImageGetHeight(image));
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000240 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
241
242 SkBitmap tmp;
reed84825042014-09-02 12:50:45 -0700243 if (!tmp.tryAllocPixels(info)) {
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000244 return false;
245 }
246
247 if (!SkCopyPixelsFromCGImage(tmp.info(), tmp.rowBytes(), tmp.getPixels(), image)) {
248 return false;
249 }
250
251 CGImageAlphaInfo cgInfo = CGImageGetAlphaInfo(image);
252 switch (cgInfo) {
253 case kCGImageAlphaNone:
254 case kCGImageAlphaNoneSkipLast:
255 case kCGImageAlphaNoneSkipFirst:
256 SkASSERT(SkBitmap::ComputeIsOpaque(tmp));
257 tmp.setAlphaType(kOpaque_SkAlphaType);
258 break;
259 default:
260 // we don't know if we're opaque or not, so compute it.
261 if (SkBitmap::ComputeIsOpaque(tmp)) {
262 tmp.setAlphaType(kOpaque_SkAlphaType);
263 }
264 }
265
266 *dst = tmp;
267 return true;
268}
mtklein1ee76512015-11-02 10:20:27 -0800269
Mike Reed463c8482016-12-21 12:01:12 -0500270sk_sp<SkImage> SkMakeImageFromCGImage(CGImageRef src) {
271 SkBitmap bm;
272 if (!SkCreateBitmapFromCGImage(&bm, src)) {
273 return nullptr;
274 }
275
276 bm.setImmutable();
277 return SkImage::MakeFromBitmap(bm);
278}
279
mtklein1ee76512015-11-02 10:20:27 -0800280#endif//defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)