mtklein | 4977983 | 2015-08-10 12:58:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | #ifndef SkBlitMask_opts_DEFINED |
| 9 | #define SkBlitMask_opts_DEFINED |
| 10 | |
| 11 | #include "Sk4px.h" |
| 12 | |
| 13 | namespace SK_OPTS_NS { |
| 14 | |
mtklein | 5015176 | 2015-08-26 12:35:14 -0700 | [diff] [blame^] | 15 | static void blit_mask_d32_a8_general(SkPMColor* dst, size_t dstRB, |
| 16 | const SkAlpha* mask, size_t maskRB, |
| 17 | SkColor color, int w, int h) { |
mtklein | 4977983 | 2015-08-10 12:58:17 -0700 | [diff] [blame] | 18 | auto s = Sk4px::DupPMColor(SkPreMultiplyColor(color)); |
mtklein | 4977983 | 2015-08-10 12:58:17 -0700 | [diff] [blame] | 19 | auto fn = [&](const Sk4px& d, const Sk4px& aa) { |
| 20 | // = (s + d(1-sa))aa + d(1-aa) |
| 21 | // = s*aa + d(1-sa*aa) |
| 22 | auto left = s.approxMulDiv255(aa), |
| 23 | right = d.approxMulDiv255(left.alphas().inv()); |
| 24 | return left + right; // This does not overflow (exhaustively checked). |
| 25 | }; |
mtklein | 4977983 | 2015-08-10 12:58:17 -0700 | [diff] [blame] | 26 | while (h --> 0) { |
| 27 | Sk4px::MapDstAlpha(w, dst, mask, fn); |
| 28 | dst += dstRB / sizeof(*dst); |
| 29 | mask += maskRB / sizeof(*mask); |
| 30 | } |
| 31 | } |
| 32 | |
mtklein | 5015176 | 2015-08-26 12:35:14 -0700 | [diff] [blame^] | 33 | // As above, but made slightly simpler by requiring that color is opaque. |
| 34 | static void blit_mask_d32_a8_opaque(SkPMColor* dst, size_t dstRB, |
| 35 | const SkAlpha* mask, size_t maskRB, |
| 36 | SkColor color, int w, int h) { |
| 37 | SkASSERT(SkColorGetA(color) == 0xFF); |
| 38 | auto s = Sk4px::DupPMColor(SkPreMultiplyColor(color)); |
| 39 | auto fn = [&](const Sk4px& d, const Sk4px& aa) { |
| 40 | // = (s + d(1-sa))aa + d(1-aa) |
| 41 | // = s*aa + d(1-sa*aa) |
| 42 | // ~~~> |
| 43 | // = s*aa + d(1-aa) |
| 44 | return s.approxMulDiv255(aa) + d.approxMulDiv255(aa.inv()); |
| 45 | }; |
| 46 | while (h --> 0) { |
| 47 | Sk4px::MapDstAlpha(w, dst, mask, fn); |
| 48 | dst += dstRB / sizeof(*dst); |
| 49 | mask += maskRB / sizeof(*mask); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Same as _opaque, but assumes color == SK_ColorBLACK, a very common and even simpler case. |
| 54 | static void blit_mask_d32_a8_black(SkPMColor* dst, size_t dstRB, |
| 55 | const SkAlpha* mask, size_t maskRB, |
| 56 | int w, int h) { |
| 57 | auto fn = [](const Sk4px& d, const Sk4px& aa) { |
| 58 | // = (s + d(1-sa))aa + d(1-aa) |
| 59 | // = s*aa + d(1-sa*aa) |
| 60 | // ~~~> |
| 61 | // a = 1*aa + d(1-1*aa) = aa + d(1-aa) |
| 62 | // c = 0*aa + d(1-1*aa) = d(1-aa) |
| 63 | return aa.zeroColors() + d.approxMulDiv255(aa.inv()); |
| 64 | }; |
| 65 | while (h --> 0) { |
| 66 | Sk4px::MapDstAlpha(w, dst, mask, fn); |
| 67 | dst += dstRB / sizeof(*dst); |
| 68 | mask += maskRB / sizeof(*mask); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static void blit_mask_d32_a8(SkPMColor* dst, size_t dstRB, |
| 73 | const SkAlpha* mask, size_t maskRB, |
| 74 | SkColor color, int w, int h) { |
| 75 | if (color == SK_ColorBLACK) { |
| 76 | blit_mask_d32_a8_black(dst, dstRB, mask, maskRB, w, h); |
| 77 | } else if (SkColorGetA(color) == 0xFF) { |
| 78 | blit_mask_d32_a8_opaque(dst, dstRB, mask, maskRB, color, w, h); |
| 79 | } else { |
| 80 | blit_mask_d32_a8_general(dst, dstRB, mask, maskRB, color, w, h); |
| 81 | } |
| 82 | } |
| 83 | |
mtklein | 4977983 | 2015-08-10 12:58:17 -0700 | [diff] [blame] | 84 | } // SK_OPTS_NS |
| 85 | |
| 86 | #endif//SkBlitMask_opts_DEFINED |