blob: 10bed27c7f393e2a0be824d8e73d441bb6f81ef2 [file] [log] [blame]
reedb184f7f2014-07-13 04:32:32 -07001/*
2 * Copyright 2014 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 */
7
8#include "SkBitmap.h"
9#include "SkCanvas.h"
Matt Sarett909d3792016-12-22 10:52:25 -050010#include "SkColorSpaceXform.h"
11#include "SkColorSpaceXformPriv.h"
bsalomon@google.comfb0d7412012-02-22 21:25:34 +000012#include "SkConfig8888.h"
reed@google.com7111d462014-03-25 16:20:24 +000013#include "SkColorPriv.h"
reedb184f7f2014-07-13 04:32:32 -070014#include "SkDither.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050015#include "SkImageInfoPriv.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000016#include "SkMathPriv.h"
Matt Sarett909d3792016-12-22 10:52:25 -050017#include "SkPM4fPriv.h"
Mike Reed6f0286f2016-11-28 17:26:27 -050018#include "SkRasterPipeline.h"
commit-bot@chromium.org61c49f32013-06-14 13:38:56 +000019#include "SkUnPreMultiply.h"
bsalomon@google.comfb0d7412012-02-22 21:25:34 +000020
Matt Sarettcf3525a2017-01-24 12:43:41 -050021// Fast Path 1: The memcpy() case.
22static inline bool can_memcpy(const SkImageInfo& dstInfo, const SkImageInfo& srcInfo) {
23 if (dstInfo.colorType() != srcInfo.colorType()) {
24 return false;
25 }
26
27 if (dstInfo.alphaType() != srcInfo.alphaType() &&
28 kOpaque_SkAlphaType != dstInfo.alphaType() &&
29 kOpaque_SkAlphaType != srcInfo.alphaType())
30 {
31 // We need to premultiply or unpremultiply.
32 return false;
33 }
34
35 return !dstInfo.colorSpace() || !srcInfo.colorSpace() ||
36 SkColorSpace::Equals(dstInfo.colorSpace(), srcInfo.colorSpace());
37}
38
Mike Reed6f0286f2016-11-28 17:26:27 -050039// For now disable 565 in the pipeline. Its (higher) quality is so different its too much to
40// rebase (for now)
41//
42//#define PIPELINE_HANDLES_565
43
44static bool is_srgb(const SkImageInfo& info) {
45 return info.colorSpace() && info.colorSpace()->gammaCloseToSRGB();
46}
47
48static bool copy_pipeline_pixels(const SkImageInfo& dstInfo, void* dstRow, size_t dstRB,
49 const SkImageInfo& srcInfo, const void* srcRow, size_t srcRB,
50 SkColorTable* ctable) {
51 SkASSERT(srcInfo.width() == dstInfo.width());
52 SkASSERT(srcInfo.height() == dstInfo.height());
53
54 bool src_srgb = is_srgb(srcInfo);
55 const bool dst_srgb = is_srgb(dstInfo);
56 if (!dstInfo.colorSpace()) {
57 src_srgb = false; // untagged dst means ignore tags on src
58 }
59
Mike Reed6f0286f2016-11-28 17:26:27 -050060 SkRasterPipeline pipeline;
61
62 switch (srcInfo.colorType()) {
63 case kRGBA_8888_SkColorType:
64 case kBGRA_8888_SkColorType:
Mike Klein729b5822016-11-28 18:23:23 -050065 pipeline.append(SkRasterPipeline::load_8888, &srcRow);
Mike Reed6f0286f2016-11-28 17:26:27 -050066 if (src_srgb) {
Mike Kleind37d5d92016-12-14 13:38:24 +000067 pipeline.append_from_srgb(srcInfo.alphaType());
Mike Reed6f0286f2016-11-28 17:26:27 -050068 }
Matt Sarett909d3792016-12-22 10:52:25 -050069 if (kBGRA_8888_SkColorType == srcInfo.colorType()) {
Mike Reed6f0286f2016-11-28 17:26:27 -050070 pipeline.append(SkRasterPipeline::swap_rb);
71 }
72 break;
73#ifdef PIPELINE_HANDLES_565
74 case kRGB_565_SkColorType:
Mike Klein729b5822016-11-28 18:23:23 -050075 pipeline.append(SkRasterPipeline::load_565, &srcRow);
Mike Reed6f0286f2016-11-28 17:26:27 -050076 break;
77#endif
78 case kRGBA_F16_SkColorType:
Mike Klein729b5822016-11-28 18:23:23 -050079 pipeline.append(SkRasterPipeline::load_f16, &srcRow);
Mike Reed6f0286f2016-11-28 17:26:27 -050080 break;
81 default:
82 return false; // src colortype unsupported
83 }
84
Matt Sarett909d3792016-12-22 10:52:25 -050085 float matrix[12];
86 if (!append_gamut_transform(&pipeline, matrix, srcInfo.colorSpace(), dstInfo.colorSpace())) {
87 return false;
88 }
89
Mike Reed6f0286f2016-11-28 17:26:27 -050090 SkAlphaType sat = srcInfo.alphaType();
91 SkAlphaType dat = dstInfo.alphaType();
92 if (sat == kPremul_SkAlphaType && dat == kUnpremul_SkAlphaType) {
93 pipeline.append(SkRasterPipeline::unpremul);
94 } else if (sat == kUnpremul_SkAlphaType && dat == kPremul_SkAlphaType) {
95 pipeline.append(SkRasterPipeline::premul);
96 }
97
98 switch (dstInfo.colorType()) {
99 case kRGBA_8888_SkColorType:
100 case kBGRA_8888_SkColorType:
Matt Sarett909d3792016-12-22 10:52:25 -0500101 if (kBGRA_8888_SkColorType == dstInfo.colorType()) {
102 pipeline.append(SkRasterPipeline::swap_rb);
103 }
Mike Reed6f0286f2016-11-28 17:26:27 -0500104 if (dst_srgb) {
105 pipeline.append(SkRasterPipeline::to_srgb);
106 }
107 pipeline.append(SkRasterPipeline::store_8888, &dstRow);
108 break;
109#ifdef PIPELINE_HANDLES_565
110 case kRGB_565_SkColorType:
111 pipeline.append(SkRasterPipeline::store_565, &dstRow);
112 break;
113#endif
114 case kRGBA_F16_SkColorType:
115 pipeline.append(SkRasterPipeline::store_f16, &dstRow);
116 break;
117 default:
118 return false; // dst colortype unsupported
119 }
120
121 auto p = pipeline.compile();
122
123 for (int y = 0; y < srcInfo.height(); ++y) {
Mike Klein319ba3d2017-01-20 15:11:54 -0500124 p(0,srcInfo.width());
Mike Reed6f0286f2016-11-28 17:26:27 -0500125 // The pipeline has pointers to srcRow and dstRow, so we just need to update them in the
126 // loop to move between rows of src/dst.
127 srcRow = (const char*)srcRow + srcRB;
128 dstRow = (char*)dstRow + dstRB;
129 }
130 return true;
131}
132
reed@google.com7111d462014-03-25 16:20:24 +0000133enum AlphaVerb {
134 kNothing_AlphaVerb,
135 kPremul_AlphaVerb,
136 kUnpremul_AlphaVerb,
137};
bsalomon@google.comfb0d7412012-02-22 21:25:34 +0000138
reed@google.com7111d462014-03-25 16:20:24 +0000139template <bool doSwapRB, AlphaVerb doAlpha> uint32_t convert32(uint32_t c) {
140 if (doSwapRB) {
141 c = SkSwizzle_RB(c);
commit-bot@chromium.org231f6b82014-03-25 13:38:44 +0000142 }
reed@google.com7111d462014-03-25 16:20:24 +0000143
144 // Lucky for us, in both RGBA and BGRA, the alpha component is always in the same place, so
145 // we can perform premul or unpremul the same way without knowing the swizzles for RGB.
146 switch (doAlpha) {
147 case kNothing_AlphaVerb:
148 // no change
149 break;
150 case kPremul_AlphaVerb:
151 c = SkPreMultiplyARGB(SkGetPackedA32(c), SkGetPackedR32(c),
152 SkGetPackedG32(c), SkGetPackedB32(c));
153 break;
154 case kUnpremul_AlphaVerb:
155 c = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(c);
156 break;
157 }
158 return c;
commit-bot@chromium.org231f6b82014-03-25 13:38:44 +0000159}
160
reed@google.com7111d462014-03-25 16:20:24 +0000161template <bool doSwapRB, AlphaVerb doAlpha>
162void convert32_row(uint32_t* dst, const uint32_t* src, int count) {
163 // This has to be correct if src == dst (but not partial overlap)
164 for (int i = 0; i < count; ++i) {
165 dst[i] = convert32<doSwapRB, doAlpha>(src[i]);
bsalomon@google.comfb0d7412012-02-22 21:25:34 +0000166 }
167}
168
reed@google.com7111d462014-03-25 16:20:24 +0000169static bool is_32bit_colortype(SkColorType ct) {
170 return kRGBA_8888_SkColorType == ct || kBGRA_8888_SkColorType == ct;
171}
172
173static AlphaVerb compute_AlphaVerb(SkAlphaType src, SkAlphaType dst) {
reed44977482015-02-27 10:23:00 -0800174 SkASSERT(kUnknown_SkAlphaType != src);
175 SkASSERT(kUnknown_SkAlphaType != dst);
reed@google.com7111d462014-03-25 16:20:24 +0000176
177 if (kOpaque_SkAlphaType == src || kOpaque_SkAlphaType == dst || src == dst) {
178 return kNothing_AlphaVerb;
179 }
180 if (kPremul_SkAlphaType == dst) {
181 SkASSERT(kUnpremul_SkAlphaType == src);
182 return kPremul_AlphaVerb;
183 } else {
184 SkASSERT(kPremul_SkAlphaType == src);
185 SkASSERT(kUnpremul_SkAlphaType == dst);
186 return kUnpremul_AlphaVerb;
bsalomon@google.comfb0d7412012-02-22 21:25:34 +0000187 }
188}
189
reed@google.com7111d462014-03-25 16:20:24 +0000190bool SkSrcPixelInfo::convertPixelsTo(SkDstPixelInfo* dst, int width, int height) const {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500191 SkASSERT(width > 0 && height > 0);
commit-bot@chromium.org231f6b82014-03-25 13:38:44 +0000192
reed@google.com7111d462014-03-25 16:20:24 +0000193 if (!is_32bit_colortype(fColorType) || !is_32bit_colortype(dst->fColorType)) {
194 return false;
commit-bot@chromium.org231f6b82014-03-25 13:38:44 +0000195 }
commit-bot@chromium.org231f6b82014-03-25 13:38:44 +0000196
reed@google.com7111d462014-03-25 16:20:24 +0000197 void (*proc)(uint32_t* dst, const uint32_t* src, int count);
198 AlphaVerb doAlpha = compute_AlphaVerb(fAlphaType, dst->fAlphaType);
199 bool doSwapRB = fColorType != dst->fColorType;
commit-bot@chromium.org231f6b82014-03-25 13:38:44 +0000200
reed@google.com7111d462014-03-25 16:20:24 +0000201 switch (doAlpha) {
202 case kNothing_AlphaVerb:
Matt Sarettcf3525a2017-01-24 12:43:41 -0500203 SkASSERT(doSwapRB);
204 proc = convert32_row<true, kNothing_AlphaVerb>;
commit-bot@chromium.org231f6b82014-03-25 13:38:44 +0000205 break;
reed@google.com7111d462014-03-25 16:20:24 +0000206 case kPremul_AlphaVerb:
207 if (doSwapRB) {
208 proc = convert32_row<true, kPremul_AlphaVerb>;
209 } else {
210 proc = convert32_row<false, kPremul_AlphaVerb>;
211 }
commit-bot@chromium.org231f6b82014-03-25 13:38:44 +0000212 break;
reed@google.com7111d462014-03-25 16:20:24 +0000213 case kUnpremul_AlphaVerb:
214 if (doSwapRB) {
215 proc = convert32_row<true, kUnpremul_AlphaVerb>;
216 } else {
217 proc = convert32_row<false, kUnpremul_AlphaVerb>;
218 }
commit-bot@chromium.org231f6b82014-03-25 13:38:44 +0000219 break;
220 }
commit-bot@chromium.org231f6b82014-03-25 13:38:44 +0000221
reed@google.com7111d462014-03-25 16:20:24 +0000222 uint32_t* dstP = static_cast<uint32_t*>(dst->fPixels);
223 const uint32_t* srcP = static_cast<const uint32_t*>(fPixels);
224 size_t srcInc = fRowBytes >> 2;
225 size_t dstInc = dst->fRowBytes >> 2;
226 for (int y = 0; y < height; ++y) {
227 proc(dstP, srcP, width);
228 dstP += dstInc;
229 srcP += srcInc;
commit-bot@chromium.org231f6b82014-03-25 13:38:44 +0000230 }
reed@google.com7111d462014-03-25 16:20:24 +0000231 return true;
bsalomon@google.comfb0d7412012-02-22 21:25:34 +0000232}
reedb184f7f2014-07-13 04:32:32 -0700233
reed0c9b1a82015-03-17 17:44:06 -0700234static void copy_g8_to_32(void* dst, size_t dstRB, const void* src, size_t srcRB, int w, int h) {
235 uint32_t* dst32 = (uint32_t*)dst;
236 const uint8_t* src8 = (const uint8_t*)src;
halcanary9d524f22016-03-29 09:03:52 -0700237
reed0c9b1a82015-03-17 17:44:06 -0700238 for (int y = 0; y < h; ++y) {
239 for (int x = 0; x < w; ++x) {
240 dst32[x] = SkPackARGB32(0xFF, src8[x], src8[x], src8[x]);
241 }
242 dst32 = (uint32_t*)((char*)dst32 + dstRB);
243 src8 += srcRB;
244 }
245}
246
lsalzmana2415ac2016-10-11 14:29:12 -0700247static bool extract_alpha(void* dst, size_t dstRB, const void* src, size_t srcRB,
248 const SkImageInfo& srcInfo, SkColorTable* ctable) {
249 uint8_t* SK_RESTRICT dst8 = (uint8_t*)dst;
250
251 const int w = srcInfo.width();
252 const int h = srcInfo.height();
253 if (srcInfo.isOpaque()) {
254 // src is opaque, so just fill alpha with 0xFF
255 for (int y = 0; y < h; ++y) {
256 memset(dst8, 0xFF, w);
257 dst8 += dstRB;
258 }
259 return true;
260 }
261 switch (srcInfo.colorType()) {
262 case kN32_SkColorType: {
263 const SkPMColor* SK_RESTRICT src32 = (const SkPMColor*)src;
264 for (int y = 0; y < h; ++y) {
265 for (int x = 0; x < w; ++x) {
266 dst8[x] = SkGetPackedA32(src32[x]);
267 }
268 dst8 += dstRB;
269 src32 = (const SkPMColor*)((const char*)src32 + srcRB);
270 }
271 break;
272 }
273 case kARGB_4444_SkColorType: {
274 const SkPMColor16* SK_RESTRICT src16 = (const SkPMColor16*)src;
275 for (int y = 0; y < h; ++y) {
276 for (int x = 0; x < w; ++x) {
277 dst8[x] = SkPacked4444ToA32(src16[x]);
278 }
279 dst8 += dstRB;
280 src16 = (const SkPMColor16*)((const char*)src16 + srcRB);
281 }
282 break;
283 }
284 case kIndex_8_SkColorType: {
285 if (nullptr == ctable) {
286 return false;
287 }
288 const SkPMColor* SK_RESTRICT table = ctable->readColors();
289 const uint8_t* SK_RESTRICT src8 = (const uint8_t*)src;
290 for (int y = 0; y < h; ++y) {
291 for (int x = 0; x < w; ++x) {
292 dst8[x] = SkGetPackedA32(table[src8[x]]);
293 }
294 dst8 += dstRB;
295 src8 += srcRB;
296 }
297 break;
298 }
299 default:
300 return false;
301 }
302 return true;
303}
304
Matt Sarett909d3792016-12-22 10:52:25 -0500305static inline bool optimized_color_xform(const SkImageInfo& dstInfo, const SkImageInfo& srcInfo) {
306 if (kUnpremul_SkAlphaType == dstInfo.alphaType() && kPremul_SkAlphaType == srcInfo.alphaType())
307 {
308 return false;
309 }
310
311 switch (dstInfo.colorType()) {
312 case kRGBA_8888_SkColorType:
313 case kBGRA_8888_SkColorType:
314 case kRGBA_F16_SkColorType:
315 break;
316 default:
317 return false;
318 }
319
320 switch (srcInfo.colorType()) {
321 case kRGBA_8888_SkColorType:
322 case kBGRA_8888_SkColorType:
323 break;
324 default:
325 return false;
326 }
327
328 return true;
329}
330
331static inline void apply_color_xform(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
332 const SkImageInfo& srcInfo, const void* srcPixels,
333 size_t srcRB) {
334 SkColorSpaceXform::ColorFormat dstFormat = select_xform_format(dstInfo.colorType());
335 SkColorSpaceXform::ColorFormat srcFormat = select_xform_format(srcInfo.colorType());
336 SkAlphaType xformAlpha;
337 switch (srcInfo.alphaType()) {
338 case kOpaque_SkAlphaType:
339 xformAlpha = kOpaque_SkAlphaType;
340 break;
341 case kPremul_SkAlphaType:
342 SkASSERT(kPremul_SkAlphaType == dstInfo.alphaType());
343
344 // This signal means: copy the src alpha to the dst, do not premultiply (in this
345 // case because the pixels are already premultiplied).
346 xformAlpha = kUnpremul_SkAlphaType;
347 break;
348 case kUnpremul_SkAlphaType:
349 SkASSERT(kPremul_SkAlphaType == dstInfo.alphaType() ||
350 kUnpremul_SkAlphaType == dstInfo.alphaType());
351
352 xformAlpha = dstInfo.alphaType();
353 break;
354 default:
355 SkASSERT(false);
356 xformAlpha = kUnpremul_SkAlphaType;
357 break;
358 }
359
360 std::unique_ptr<SkColorSpaceXform> xform = SkColorSpaceXform::New(srcInfo.colorSpace(),
361 dstInfo.colorSpace());
362 SkASSERT(xform);
363
364 for (int y = 0; y < dstInfo.height(); y++) {
365 SkAssertResult(xform->apply(dstFormat, dstPixels, srcFormat, srcPixels, dstInfo.width(),
366 xformAlpha));
367 dstPixels = SkTAddOffset<void>(dstPixels, dstRB);
368 srcPixels = SkTAddOffset<const void>(srcPixels, srcRB);
369 }
370}
371
reedb184f7f2014-07-13 04:32:32 -0700372bool SkPixelInfo::CopyPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
373 const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRB,
374 SkColorTable* ctable) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500375 SkASSERT(dstInfo.dimensions() == srcInfo.dimensions());
376 SkASSERT(SkImageInfoValidConversion(dstInfo, srcInfo));
Matt Sarett909d3792016-12-22 10:52:25 -0500377
reedb184f7f2014-07-13 04:32:32 -0700378 const int width = srcInfo.width();
379 const int height = srcInfo.height();
halcanary9d524f22016-03-29 09:03:52 -0700380
Matt Sarettcf3525a2017-01-24 12:43:41 -0500381 // Fast Path 1: The memcpy() case.
382 if (can_memcpy(dstInfo, srcInfo)) {
383 SkRectMemcpy(dstPixels, dstRB, srcPixels, srcRB, dstInfo.minRowBytes(), dstInfo.height());
reedd96e7e52016-02-17 07:15:29 -0800384 return true;
385 }
reedb184f7f2014-07-13 04:32:32 -0700386
Matt Sarett909d3792016-12-22 10:52:25 -0500387 const bool isColorAware = srcInfo.colorSpace() && dstInfo.colorSpace();
388
reedb184f7f2014-07-13 04:32:32 -0700389 // Handle fancy alpha swizzling if both are ARGB32
Matt Sarett909d3792016-12-22 10:52:25 -0500390 if (4 == srcInfo.bytesPerPixel() && 4 == dstInfo.bytesPerPixel() && !isColorAware) {
reedb184f7f2014-07-13 04:32:32 -0700391 SkDstPixelInfo dstPI;
392 dstPI.fColorType = dstInfo.colorType();
393 dstPI.fAlphaType = dstInfo.alphaType();
394 dstPI.fPixels = dstPixels;
395 dstPI.fRowBytes = dstRB;
mtklein775b8192014-12-02 09:11:25 -0800396
reedb184f7f2014-07-13 04:32:32 -0700397 SkSrcPixelInfo srcPI;
398 srcPI.fColorType = srcInfo.colorType();
399 srcPI.fAlphaType = srcInfo.alphaType();
400 srcPI.fPixels = srcPixels;
401 srcPI.fRowBytes = srcRB;
mtklein775b8192014-12-02 09:11:25 -0800402
reedb184f7f2014-07-13 04:32:32 -0700403 return srcPI.convertPixelsTo(&dstPI, width, height);
404 }
405
Matt Sarett909d3792016-12-22 10:52:25 -0500406 if (isColorAware && optimized_color_xform(dstInfo, srcInfo)) {
407 apply_color_xform(dstInfo, dstPixels, dstRB, srcInfo, srcPixels, srcRB);
408 return true;
409 }
410
reedb184f7f2014-07-13 04:32:32 -0700411 /*
412 * Begin section where we try to change colorTypes along the way. Not all combinations
413 * are supported.
414 */
415
reed0c9b1a82015-03-17 17:44:06 -0700416 if (kGray_8_SkColorType == srcInfo.colorType() && 4 == dstInfo.bytesPerPixel()) {
417 copy_g8_to_32(dstPixels, dstRB, srcPixels, srcRB, width, height);
418 return true;
419 }
reed0c9b1a82015-03-17 17:44:06 -0700420
lsalzmana2415ac2016-10-11 14:29:12 -0700421 if (kAlpha_8_SkColorType == dstInfo.colorType() &&
422 extract_alpha(dstPixels, dstRB, srcPixels, srcRB, srcInfo, ctable)) {
423 return true;
424 }
425
Mike Reed6f0286f2016-11-28 17:26:27 -0500426 // Try the pipeline
427 //
428 if (copy_pipeline_pixels(dstInfo, dstPixels, dstRB, srcInfo, srcPixels, srcRB, ctable)) {
429 return true;
430 }
431
reedb184f7f2014-07-13 04:32:32 -0700432 // Can no longer draw directly into 4444, but we can manually whack it for a few combinations
433 if (kARGB_4444_SkColorType == dstInfo.colorType() &&
434 (kN32_SkColorType == srcInfo.colorType() || kIndex_8_SkColorType == srcInfo.colorType())) {
435 if (srcInfo.alphaType() == kUnpremul_SkAlphaType) {
436 // Our method for converting to 4444 assumes premultiplied.
437 return false;
438 }
mtklein775b8192014-12-02 09:11:25 -0800439
halcanary96fcdcc2015-08-27 07:41:13 -0700440 const SkPMColor* table = nullptr;
reedb184f7f2014-07-13 04:32:32 -0700441 if (kIndex_8_SkColorType == srcInfo.colorType()) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500442 SkASSERT(ctable);
mtklein775b8192014-12-02 09:11:25 -0800443 table = ctable->readColors();
reedb184f7f2014-07-13 04:32:32 -0700444 }
445
446 for (int y = 0; y < height; ++y) {
447 DITHER_4444_SCAN(y);
448 SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*)dstPixels;
449 if (table) {
450 const uint8_t* SK_RESTRICT srcRow = (const uint8_t*)srcPixels;
451 for (int x = 0; x < width; ++x) {
452 dstRow[x] = SkDitherARGB32To4444(table[srcRow[x]], DITHER_VALUE(x));
453 }
454 } else {
455 const SkPMColor* SK_RESTRICT srcRow = (const SkPMColor*)srcPixels;
456 for (int x = 0; x < width; ++x) {
457 dstRow[x] = SkDitherARGB32To4444(srcRow[x], DITHER_VALUE(x));
458 }
459 }
460 dstPixels = (char*)dstPixels + dstRB;
461 srcPixels = (const char*)srcPixels + srcRB;
462 }
reedb184f7f2014-07-13 04:32:32 -0700463 return true;
464 }
465
466 if (dstInfo.alphaType() == kUnpremul_SkAlphaType) {
467 // We do not support drawing to unpremultiplied bitmaps.
468 return false;
469 }
470
471 // Final fall-back, draw with a canvas
472 //
473 // Always clear the dest in case one of the blitters accesses it
474 // TODO: switch the allocation of tmpDst to call sk_calloc_throw
475 {
476 SkBitmap bm;
halcanary96fcdcc2015-08-27 07:41:13 -0700477 if (!bm.installPixels(srcInfo, const_cast<void*>(srcPixels), srcRB, ctable, nullptr, nullptr)) {
reedb184f7f2014-07-13 04:32:32 -0700478 return false;
479 }
Mike Reed5df49342016-11-12 08:06:55 -0600480 std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(dstInfo, dstPixels, dstRB);
481 if (!canvas) {
reedb184f7f2014-07-13 04:32:32 -0700482 return false;
483 }
484
485 SkPaint paint;
486 paint.setDither(true);
487
488 canvas->clear(0);
489 canvas->drawBitmap(bm, 0, 0, &paint);
490 return true;
491 }
492}