blob: f4d7e7973f3505fbfeecce5216417c793e676cef [file] [log] [blame]
mtklein49779832015-08-10 12:58:17 -07001/*
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"
mtkleine8e17cf2015-11-06 14:10:48 -080012#include "SkPx.h"
mtklein49779832015-08-10 12:58:17 -070013
14namespace SK_OPTS_NS {
15
mtkleine8e17cf2015-11-06 14:10:48 -080016template <typename Fn>
17static void blit_mask_d32_a8(const Fn& fn, SkPMColor* dst, size_t dstRB,
18 const SkAlpha* mask, size_t maskRB,
19 int w, int h) {
20 while (h --> 0) {
21 int n = w;
22 while (n >= SkPx::N) {
23 fn(SkPx::Load(dst), SkPx::Alpha::Load(mask)).store(dst);
24 dst += SkPx::N; mask += SkPx::N; n -= SkPx::N;
mtklein12d40c12015-09-01 11:03:11 -070025 }
mtkleine8e17cf2015-11-06 14:10:48 -080026 if (n > 0) {
27 fn(SkPx::Load(dst, n), SkPx::Alpha::Load(mask, n)).store(dst, n);
28 dst += n; mask += n;
29 }
30 dst += dstRB / sizeof(*dst) - w;
31 mask += maskRB / sizeof(*mask) - w;
mtklein50151762015-08-26 12:35:14 -070032 }
mtkleine8e17cf2015-11-06 14:10:48 -080033}
mtklein12d40c12015-09-01 11:03:11 -070034
mtkleine8e17cf2015-11-06 14:10:48 -080035static void blit_mask_d32_a8(SkPMColor* dst, size_t dstRB,
36 const SkAlpha* mask, size_t maskRB,
37 SkColor color, int w, int h) {
38 auto s = SkPx::Dup(SkPreMultiplyColor(color));
mtklein12d40c12015-09-01 11:03:11 -070039
mtkleine8e17cf2015-11-06 14:10:48 -080040 if (color == SK_ColorBLACK) {
41 auto fn = [](const SkPx& d, const SkPx::Alpha& aa) {
42 // = (s + d(1-sa))aa + d(1-aa)
43 // = s*aa + d(1-sa*aa)
44 // ~~~>
45 // a = 1*aa + d(1-1*aa) = aa + d(1-aa)
46 // c = 0*aa + d(1-1*aa) = d(1-aa)
47 return d.approxMulDiv255(aa.inv()).addAlpha(aa);
mtklein12d40c12015-09-01 11:03:11 -070048 };
mtkleine8e17cf2015-11-06 14:10:48 -080049 blit_mask_d32_a8(fn, dst, dstRB, mask, maskRB, w, h);
50 } else if (SkColorGetA(color) == 0xFF) {
51 auto fn = [&](const SkPx& d, const SkPx::Alpha& aa) {
mtklein12d40c12015-09-01 11:03:11 -070052 // = (s + d(1-sa))aa + d(1-aa)
53 // = s*aa + d(1-sa*aa)
54 // ~~~>
55 // = s*aa + d(1-aa)
56 return s.approxMulDiv255(aa) + d.approxMulDiv255(aa.inv());
57 };
mtkleine8e17cf2015-11-06 14:10:48 -080058 blit_mask_d32_a8(fn, dst, dstRB, mask, maskRB, w, h);
mtklein13580512015-11-06 11:34:06 -080059 } else {
mtkleine8e17cf2015-11-06 14:10:48 -080060 auto fn = [&](const SkPx& d, const SkPx::Alpha& aa) {
61 // = (s + d(1-sa))aa + d(1-aa)
62 // = s*aa + d(1-sa*aa)
63 auto left = s.approxMulDiv255(aa),
64 right = d.approxMulDiv255(left.alpha().inv());
65 return left + right; // This does not overflow (exhaustively checked).
66 };
67 blit_mask_d32_a8(fn, dst, dstRB, mask, maskRB, w, h);
mtklein50151762015-08-26 12:35:14 -070068 }
69}
70
mtklein49779832015-08-10 12:58:17 -070071} // SK_OPTS_NS
72
73#endif//SkBlitMask_opts_DEFINED