blob: d76efb839e484a063d515c3eb325def0308c5ff6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkAvoidXfermode.h"
9#include "SkColorPriv.h"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000010#include "SkFlattenableBuffers.h"
robertphillips@google.comb83b6b42013-01-22 14:32:09 +000011#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012
reed@google.com30da7452012-12-17 19:55:24 +000013SkAvoidXfermode::SkAvoidXfermode(SkColor opColor, U8CPU tolerance, Mode mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000014 if (tolerance > 255) {
15 tolerance = 255;
16 }
17
18 fOpColor = opColor;
19 fDistMul = (256 << 14) / (tolerance + 1);
20 fMode = mode;
21}
22
23SkAvoidXfermode::SkAvoidXfermode(SkFlattenableReadBuffer& buffer)
reed@google.com30da7452012-12-17 19:55:24 +000024 : INHERITED(buffer) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000025 fOpColor = buffer.readColor();
26 fDistMul = buffer.readUInt();
27 fMode = (Mode)buffer.readUInt();
reed@android.com8a1c16f2008-12-17 15:59:43 +000028}
29
reed@google.com30da7452012-12-17 19:55:24 +000030void SkAvoidXfermode::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 this->INHERITED::flatten(buffer);
32
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000033 buffer.writeColor(fOpColor);
34 buffer.writeUInt(fDistMul);
35 buffer.writeUInt(fMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000036}
37
reed@android.com8a1c16f2008-12-17 15:59:43 +000038// returns 0..31
reed@google.com30da7452012-12-17 19:55:24 +000039static unsigned color_dist16(uint16_t c, unsigned r, unsigned g, unsigned b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 SkASSERT(r <= SK_R16_MASK);
41 SkASSERT(g <= SK_G16_MASK);
42 SkASSERT(b <= SK_B16_MASK);
43
44 unsigned dr = SkAbs32(SkGetPackedR16(c) - r);
45 unsigned dg = SkAbs32(SkGetPackedG16(c) - g) >> (SK_G16_BITS - SK_R16_BITS);
46 unsigned db = SkAbs32(SkGetPackedB16(c) - b);
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000047
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 return SkMax32(dr, SkMax32(dg, db));
49}
50
reed@android.com8a1c16f2008-12-17 15:59:43 +000051// returns 0..255
reed@google.com30da7452012-12-17 19:55:24 +000052static unsigned color_dist32(SkPMColor c, U8CPU r, U8CPU g, U8CPU b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 SkASSERT(r <= 0xFF);
54 SkASSERT(g <= 0xFF);
55 SkASSERT(b <= 0xFF);
56
57 unsigned dr = SkAbs32(SkGetPackedR32(c) - r);
58 unsigned dg = SkAbs32(SkGetPackedG32(c) - g);
59 unsigned db = SkAbs32(SkGetPackedB32(c) - b);
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000060
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 return SkMax32(dr, SkMax32(dg, db));
62}
63
reed@google.com30da7452012-12-17 19:55:24 +000064static int scale_dist_14(int dist, uint32_t mul, uint32_t sub) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 int tmp = dist * mul - sub;
66 int result = (tmp + (1 << 13)) >> 14;
67
68 return result;
69}
70
reed@android.com370eeeb2010-03-12 19:23:27 +000071static inline unsigned Accurate255To256(unsigned x) {
72 return x + (x >> 7);
73}
74
reed@android.com8a1c16f2008-12-17 15:59:43 +000075void SkAvoidXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], int count,
reed@google.com30da7452012-12-17 19:55:24 +000076 const SkAlpha aa[]) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 unsigned opR = SkColorGetR(fOpColor);
78 unsigned opG = SkColorGetG(fOpColor);
79 unsigned opB = SkColorGetB(fOpColor);
80 uint32_t mul = fDistMul;
81 uint32_t sub = (fDistMul - (1 << 14)) << 8;
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000082
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 int MAX, mask;
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000084
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 if (kTargetColor_Mode == fMode) {
86 mask = -1;
87 MAX = 255;
88 } else {
89 mask = 0;
90 MAX = 0;
91 }
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 for (int i = 0; i < count; i++) {
94 int d = color_dist32(dst[i], opR, opG, opB);
95 // now reverse d if we need to
96 d = MAX + (d ^ mask) - mask;
97 SkASSERT((unsigned)d <= 255);
reed@android.com370eeeb2010-03-12 19:23:27 +000098 d = Accurate255To256(d);
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000099
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 d = scale_dist_14(d, mul, sub);
101 SkASSERT(d <= 256);
tomhudson@google.com1447c6f2011-04-27 14:09:52 +0000102
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 if (d > 0) {
104 if (NULL != aa) {
reed@android.com370eeeb2010-03-12 19:23:27 +0000105 d = SkAlphaMul(d, Accurate255To256(*aa++));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 if (0 == d) {
107 continue;
108 }
109 }
djsollen@google.com8ff1e512012-03-12 14:39:20 +0000110 dst[i] = SkFourByteInterp256(src[i], dst[i], d);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 }
112 }
113}
114
reed@google.com30da7452012-12-17 19:55:24 +0000115static inline U16CPU SkBlend3216(SkPMColor src, U16CPU dst, unsigned scale) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 SkASSERT(scale <= 32);
117 scale <<= 3;
118
119 return SkPackRGB16( SkAlphaBlend(SkPacked32ToR16(src), SkGetPackedR16(dst), scale),
120 SkAlphaBlend(SkPacked32ToG16(src), SkGetPackedG16(dst), scale),
121 SkAlphaBlend(SkPacked32ToB16(src), SkGetPackedB16(dst), scale));
122}
123
124void SkAvoidXfermode::xfer16(uint16_t dst[], const SkPMColor src[], int count,
reed@google.com30da7452012-12-17 19:55:24 +0000125 const SkAlpha aa[]) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 unsigned opR = SkColorGetR(fOpColor) >> (8 - SK_R16_BITS);
127 unsigned opG = SkColorGetG(fOpColor) >> (8 - SK_G16_BITS);
128 unsigned opB = SkColorGetB(fOpColor) >> (8 - SK_R16_BITS);
129 uint32_t mul = fDistMul;
reed@android.com0db5a7f2009-11-09 16:01:36 +0000130 uint32_t sub = (fDistMul - (1 << 14)) << SK_R16_BITS;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131
132 int MAX, mask;
tomhudson@google.com1447c6f2011-04-27 14:09:52 +0000133
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 if (kTargetColor_Mode == fMode) {
135 mask = -1;
136 MAX = 31;
137 } else {
138 mask = 0;
139 MAX = 0;
140 }
141
142 for (int i = 0; i < count; i++) {
143 int d = color_dist16(dst[i], opR, opG, opB);
144 // now reverse d if we need to
145 d = MAX + (d ^ mask) - mask;
146 SkASSERT((unsigned)d <= 31);
147 // convert from 0..31 to 0..32
148 d += d >> 4;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 d = scale_dist_14(d, mul, sub);
150 SkASSERT(d <= 32);
151
152 if (d > 0) {
153 if (NULL != aa) {
reed@android.com370eeeb2010-03-12 19:23:27 +0000154 d = SkAlphaMul(d, Accurate255To256(*aa++));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 if (0 == d) {
156 continue;
157 }
158 }
159 dst[i] = SkBlend3216(src[i], dst[i], d);
160 }
161 }
162}
163
reed@google.com30da7452012-12-17 19:55:24 +0000164void SkAvoidXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count,
165 const SkAlpha aa[]) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 // override in subclass
167}
robertphillips@google.comb83b6b42013-01-22 14:32:09 +0000168
169#ifdef SK_DEVELOPER
170void SkAvoidXfermode::toString(SkString* str) const {
171 str->append("SkAvoidXfermode: opColor: ");
172 str->appendHex(fOpColor);
173 str->appendf("distMul: %d ", fDistMul);
174
175 static const char* gModeStrings[] = { "Avoid", "Target" };
176
177 str->appendf("mode: %s", gModeStrings[fMode]);
178}
179#endif