reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Sarett | 909d379 | 2016-12-22 10:52:25 -0500 | [diff] [blame] | 10 | #include "SkColorSpaceXform.h" |
| 11 | #include "SkColorSpaceXformPriv.h" |
bsalomon@google.com | fb0d741 | 2012-02-22 21:25:34 +0000 | [diff] [blame] | 12 | #include "SkConfig8888.h" |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 13 | #include "SkColorPriv.h" |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 14 | #include "SkDither.h" |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 15 | #include "SkImageInfoPriv.h" |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 16 | #include "SkMathPriv.h" |
Matt Sarett | 909d379 | 2016-12-22 10:52:25 -0500 | [diff] [blame] | 17 | #include "SkPM4fPriv.h" |
Mike Reed | 6f0286f | 2016-11-28 17:26:27 -0500 | [diff] [blame] | 18 | #include "SkRasterPipeline.h" |
commit-bot@chromium.org | 61c49f3 | 2013-06-14 13:38:56 +0000 | [diff] [blame] | 19 | #include "SkUnPreMultiply.h" |
bsalomon@google.com | fb0d741 | 2012-02-22 21:25:34 +0000 | [diff] [blame] | 20 | |
Matt Sarett | cf3525a | 2017-01-24 12:43:41 -0500 | [diff] [blame] | 21 | // Fast Path 1: The memcpy() case. |
| 22 | static 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 Reed | 6f0286f | 2016-11-28 17:26:27 -0500 | [diff] [blame] | 39 | // 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 | |
| 44 | static bool is_srgb(const SkImageInfo& info) { |
| 45 | return info.colorSpace() && info.colorSpace()->gammaCloseToSRGB(); |
| 46 | } |
| 47 | |
| 48 | static 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 Reed | 6f0286f | 2016-11-28 17:26:27 -0500 | [diff] [blame] | 60 | SkRasterPipeline pipeline; |
| 61 | |
| 62 | switch (srcInfo.colorType()) { |
| 63 | case kRGBA_8888_SkColorType: |
| 64 | case kBGRA_8888_SkColorType: |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 65 | pipeline.append(SkRasterPipeline::load_8888, &srcRow); |
Mike Reed | 6f0286f | 2016-11-28 17:26:27 -0500 | [diff] [blame] | 66 | if (src_srgb) { |
Mike Klein | d37d5d9 | 2016-12-14 13:38:24 +0000 | [diff] [blame] | 67 | pipeline.append_from_srgb(srcInfo.alphaType()); |
Mike Reed | 6f0286f | 2016-11-28 17:26:27 -0500 | [diff] [blame] | 68 | } |
Matt Sarett | 909d379 | 2016-12-22 10:52:25 -0500 | [diff] [blame] | 69 | if (kBGRA_8888_SkColorType == srcInfo.colorType()) { |
Mike Reed | 6f0286f | 2016-11-28 17:26:27 -0500 | [diff] [blame] | 70 | pipeline.append(SkRasterPipeline::swap_rb); |
| 71 | } |
| 72 | break; |
| 73 | #ifdef PIPELINE_HANDLES_565 |
| 74 | case kRGB_565_SkColorType: |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 75 | pipeline.append(SkRasterPipeline::load_565, &srcRow); |
Mike Reed | 6f0286f | 2016-11-28 17:26:27 -0500 | [diff] [blame] | 76 | break; |
| 77 | #endif |
| 78 | case kRGBA_F16_SkColorType: |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 79 | pipeline.append(SkRasterPipeline::load_f16, &srcRow); |
Mike Reed | 6f0286f | 2016-11-28 17:26:27 -0500 | [diff] [blame] | 80 | break; |
| 81 | default: |
| 82 | return false; // src colortype unsupported |
| 83 | } |
| 84 | |
Matt Sarett | 909d379 | 2016-12-22 10:52:25 -0500 | [diff] [blame] | 85 | float matrix[12]; |
| 86 | if (!append_gamut_transform(&pipeline, matrix, srcInfo.colorSpace(), dstInfo.colorSpace())) { |
| 87 | return false; |
| 88 | } |
| 89 | |
Mike Reed | 6f0286f | 2016-11-28 17:26:27 -0500 | [diff] [blame] | 90 | 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 Sarett | 909d379 | 2016-12-22 10:52:25 -0500 | [diff] [blame] | 101 | if (kBGRA_8888_SkColorType == dstInfo.colorType()) { |
| 102 | pipeline.append(SkRasterPipeline::swap_rb); |
| 103 | } |
Mike Reed | 6f0286f | 2016-11-28 17:26:27 -0500 | [diff] [blame] | 104 | 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 Klein | 319ba3d | 2017-01-20 15:11:54 -0500 | [diff] [blame] | 124 | p(0,srcInfo.width()); |
Mike Reed | 6f0286f | 2016-11-28 17:26:27 -0500 | [diff] [blame] | 125 | // 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.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 133 | enum AlphaVerb { |
| 134 | kNothing_AlphaVerb, |
| 135 | kPremul_AlphaVerb, |
| 136 | kUnpremul_AlphaVerb, |
| 137 | }; |
bsalomon@google.com | fb0d741 | 2012-02-22 21:25:34 +0000 | [diff] [blame] | 138 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 139 | template <bool doSwapRB, AlphaVerb doAlpha> uint32_t convert32(uint32_t c) { |
| 140 | if (doSwapRB) { |
| 141 | c = SkSwizzle_RB(c); |
commit-bot@chromium.org | 231f6b8 | 2014-03-25 13:38:44 +0000 | [diff] [blame] | 142 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 143 | |
| 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.org | 231f6b8 | 2014-03-25 13:38:44 +0000 | [diff] [blame] | 159 | } |
| 160 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 161 | template <bool doSwapRB, AlphaVerb doAlpha> |
| 162 | void 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.com | fb0d741 | 2012-02-22 21:25:34 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 169 | static bool is_32bit_colortype(SkColorType ct) { |
| 170 | return kRGBA_8888_SkColorType == ct || kBGRA_8888_SkColorType == ct; |
| 171 | } |
| 172 | |
| 173 | static AlphaVerb compute_AlphaVerb(SkAlphaType src, SkAlphaType dst) { |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame] | 174 | SkASSERT(kUnknown_SkAlphaType != src); |
| 175 | SkASSERT(kUnknown_SkAlphaType != dst); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 176 | |
| 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.com | fb0d741 | 2012-02-22 21:25:34 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 190 | bool SkSrcPixelInfo::convertPixelsTo(SkDstPixelInfo* dst, int width, int height) const { |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 191 | SkASSERT(width > 0 && height > 0); |
commit-bot@chromium.org | 231f6b8 | 2014-03-25 13:38:44 +0000 | [diff] [blame] | 192 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 193 | if (!is_32bit_colortype(fColorType) || !is_32bit_colortype(dst->fColorType)) { |
| 194 | return false; |
commit-bot@chromium.org | 231f6b8 | 2014-03-25 13:38:44 +0000 | [diff] [blame] | 195 | } |
commit-bot@chromium.org | 231f6b8 | 2014-03-25 13:38:44 +0000 | [diff] [blame] | 196 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 197 | 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.org | 231f6b8 | 2014-03-25 13:38:44 +0000 | [diff] [blame] | 200 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 201 | switch (doAlpha) { |
| 202 | case kNothing_AlphaVerb: |
Matt Sarett | cf3525a | 2017-01-24 12:43:41 -0500 | [diff] [blame] | 203 | SkASSERT(doSwapRB); |
| 204 | proc = convert32_row<true, kNothing_AlphaVerb>; |
commit-bot@chromium.org | 231f6b8 | 2014-03-25 13:38:44 +0000 | [diff] [blame] | 205 | break; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 206 | 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.org | 231f6b8 | 2014-03-25 13:38:44 +0000 | [diff] [blame] | 212 | break; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 213 | 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.org | 231f6b8 | 2014-03-25 13:38:44 +0000 | [diff] [blame] | 219 | break; |
| 220 | } |
commit-bot@chromium.org | 231f6b8 | 2014-03-25 13:38:44 +0000 | [diff] [blame] | 221 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 222 | 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.org | 231f6b8 | 2014-03-25 13:38:44 +0000 | [diff] [blame] | 230 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 231 | return true; |
bsalomon@google.com | fb0d741 | 2012-02-22 21:25:34 +0000 | [diff] [blame] | 232 | } |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 233 | |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 234 | static 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; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 237 | |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 238 | 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 | |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 247 | static 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 Sarett | 909d379 | 2016-12-22 10:52:25 -0500 | [diff] [blame] | 305 | static 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 | |
| 331 | static 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 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 372 | bool SkPixelInfo::CopyPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
| 373 | const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRB, |
| 374 | SkColorTable* ctable) { |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 375 | SkASSERT(dstInfo.dimensions() == srcInfo.dimensions()); |
| 376 | SkASSERT(SkImageInfoValidConversion(dstInfo, srcInfo)); |
Matt Sarett | 909d379 | 2016-12-22 10:52:25 -0500 | [diff] [blame] | 377 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 378 | const int width = srcInfo.width(); |
| 379 | const int height = srcInfo.height(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 380 | |
Matt Sarett | cf3525a | 2017-01-24 12:43:41 -0500 | [diff] [blame] | 381 | // Fast Path 1: The memcpy() case. |
| 382 | if (can_memcpy(dstInfo, srcInfo)) { |
| 383 | SkRectMemcpy(dstPixels, dstRB, srcPixels, srcRB, dstInfo.minRowBytes(), dstInfo.height()); |
reed | d96e7e5 | 2016-02-17 07:15:29 -0800 | [diff] [blame] | 384 | return true; |
| 385 | } |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 386 | |
Matt Sarett | 909d379 | 2016-12-22 10:52:25 -0500 | [diff] [blame] | 387 | const bool isColorAware = srcInfo.colorSpace() && dstInfo.colorSpace(); |
| 388 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 389 | // Handle fancy alpha swizzling if both are ARGB32 |
Matt Sarett | 909d379 | 2016-12-22 10:52:25 -0500 | [diff] [blame] | 390 | if (4 == srcInfo.bytesPerPixel() && 4 == dstInfo.bytesPerPixel() && !isColorAware) { |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 391 | SkDstPixelInfo dstPI; |
| 392 | dstPI.fColorType = dstInfo.colorType(); |
| 393 | dstPI.fAlphaType = dstInfo.alphaType(); |
| 394 | dstPI.fPixels = dstPixels; |
| 395 | dstPI.fRowBytes = dstRB; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 396 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 397 | SkSrcPixelInfo srcPI; |
| 398 | srcPI.fColorType = srcInfo.colorType(); |
| 399 | srcPI.fAlphaType = srcInfo.alphaType(); |
| 400 | srcPI.fPixels = srcPixels; |
| 401 | srcPI.fRowBytes = srcRB; |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 402 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 403 | return srcPI.convertPixelsTo(&dstPI, width, height); |
| 404 | } |
| 405 | |
Matt Sarett | 909d379 | 2016-12-22 10:52:25 -0500 | [diff] [blame] | 406 | if (isColorAware && optimized_color_xform(dstInfo, srcInfo)) { |
| 407 | apply_color_xform(dstInfo, dstPixels, dstRB, srcInfo, srcPixels, srcRB); |
| 408 | return true; |
| 409 | } |
| 410 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 411 | /* |
| 412 | * Begin section where we try to change colorTypes along the way. Not all combinations |
| 413 | * are supported. |
| 414 | */ |
| 415 | |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 416 | if (kGray_8_SkColorType == srcInfo.colorType() && 4 == dstInfo.bytesPerPixel()) { |
| 417 | copy_g8_to_32(dstPixels, dstRB, srcPixels, srcRB, width, height); |
| 418 | return true; |
| 419 | } |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 420 | |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 421 | if (kAlpha_8_SkColorType == dstInfo.colorType() && |
| 422 | extract_alpha(dstPixels, dstRB, srcPixels, srcRB, srcInfo, ctable)) { |
| 423 | return true; |
| 424 | } |
| 425 | |
Mike Reed | 6f0286f | 2016-11-28 17:26:27 -0500 | [diff] [blame] | 426 | // Try the pipeline |
| 427 | // |
| 428 | if (copy_pipeline_pixels(dstInfo, dstPixels, dstRB, srcInfo, srcPixels, srcRB, ctable)) { |
| 429 | return true; |
| 430 | } |
| 431 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 432 | // 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 | } |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 439 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 440 | const SkPMColor* table = nullptr; |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 441 | if (kIndex_8_SkColorType == srcInfo.colorType()) { |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 442 | SkASSERT(ctable); |
mtklein | 775b819 | 2014-12-02 09:11:25 -0800 | [diff] [blame] | 443 | table = ctable->readColors(); |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 444 | } |
| 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 | } |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 463 | 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; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 477 | if (!bm.installPixels(srcInfo, const_cast<void*>(srcPixels), srcRB, ctable, nullptr, nullptr)) { |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 478 | return false; |
| 479 | } |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 480 | std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(dstInfo, dstPixels, dstRB); |
| 481 | if (!canvas) { |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 482 | 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 | } |