blob: ffe6a219ab8f991a42565b76a60ef453d21c7e95 [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"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000010#include "SkReadBuffer.h"
11#include "SkWriteBuffer.h"
robertphillips@google.comb83b6b42013-01-22 14:32:09 +000012#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013
reed@google.com30da7452012-12-17 19:55:24 +000014SkAvoidXfermode::SkAvoidXfermode(SkColor opColor, U8CPU tolerance, Mode mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000015 if (tolerance > 255) {
16 tolerance = 255;
17 }
18
19 fOpColor = opColor;
20 fDistMul = (256 << 14) / (tolerance + 1);
21 fMode = mode;
22}
23
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000024SkAvoidXfermode::SkAvoidXfermode(SkReadBuffer& buffer)
reed@google.com30da7452012-12-17 19:55:24 +000025 : INHERITED(buffer) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000026 fOpColor = buffer.readColor();
27 fDistMul = buffer.readUInt();
28 fMode = (Mode)buffer.readUInt();
reed@android.com8a1c16f2008-12-17 15:59:43 +000029}
30
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000031void SkAvoidXfermode::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 this->INHERITED::flatten(buffer);
33
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000034 buffer.writeColor(fOpColor);
35 buffer.writeUInt(fDistMul);
36 buffer.writeUInt(fMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000037}
38
reed@android.com8a1c16f2008-12-17 15:59:43 +000039// returns 0..31
reed@google.com30da7452012-12-17 19:55:24 +000040static unsigned color_dist16(uint16_t c, unsigned r, unsigned g, unsigned b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 SkASSERT(r <= SK_R16_MASK);
42 SkASSERT(g <= SK_G16_MASK);
43 SkASSERT(b <= SK_B16_MASK);
44
45 unsigned dr = SkAbs32(SkGetPackedR16(c) - r);
46 unsigned dg = SkAbs32(SkGetPackedG16(c) - g) >> (SK_G16_BITS - SK_R16_BITS);
47 unsigned db = SkAbs32(SkGetPackedB16(c) - b);
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000048
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 return SkMax32(dr, SkMax32(dg, db));
50}
51
reed@android.com8a1c16f2008-12-17 15:59:43 +000052// returns 0..255
reed@google.com30da7452012-12-17 19:55:24 +000053static unsigned color_dist32(SkPMColor c, U8CPU r, U8CPU g, U8CPU b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 SkASSERT(r <= 0xFF);
55 SkASSERT(g <= 0xFF);
56 SkASSERT(b <= 0xFF);
57
58 unsigned dr = SkAbs32(SkGetPackedR32(c) - r);
59 unsigned dg = SkAbs32(SkGetPackedG32(c) - g);
60 unsigned db = SkAbs32(SkGetPackedB32(c) - b);
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000061
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 return SkMax32(dr, SkMax32(dg, db));
63}
64
reed@google.com30da7452012-12-17 19:55:24 +000065static int scale_dist_14(int dist, uint32_t mul, uint32_t sub) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 int tmp = dist * mul - sub;
67 int result = (tmp + (1 << 13)) >> 14;
68
69 return result;
70}
71
reed@android.com370eeeb2010-03-12 19:23:27 +000072static inline unsigned Accurate255To256(unsigned x) {
73 return x + (x >> 7);
74}
75
reed@android.com8a1c16f2008-12-17 15:59:43 +000076void SkAvoidXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], int count,
reed@google.com30da7452012-12-17 19:55:24 +000077 const SkAlpha aa[]) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 unsigned opR = SkColorGetR(fOpColor);
79 unsigned opG = SkColorGetG(fOpColor);
80 unsigned opB = SkColorGetB(fOpColor);
81 uint32_t mul = fDistMul;
82 uint32_t sub = (fDistMul - (1 << 14)) << 8;
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000083
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 int MAX, mask;
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000085
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 if (kTargetColor_Mode == fMode) {
87 mask = -1;
88 MAX = 255;
89 } else {
90 mask = 0;
91 MAX = 0;
92 }
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 for (int i = 0; i < count; i++) {
95 int d = color_dist32(dst[i], opR, opG, opB);
96 // now reverse d if we need to
97 d = MAX + (d ^ mask) - mask;
98 SkASSERT((unsigned)d <= 255);
reed@android.com370eeeb2010-03-12 19:23:27 +000099 d = Accurate255To256(d);
tomhudson@google.com1447c6f2011-04-27 14:09:52 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 d = scale_dist_14(d, mul, sub);
102 SkASSERT(d <= 256);
tomhudson@google.com1447c6f2011-04-27 14:09:52 +0000103
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 if (d > 0) {
105 if (NULL != aa) {
reed@android.com370eeeb2010-03-12 19:23:27 +0000106 d = SkAlphaMul(d, Accurate255To256(*aa++));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 if (0 == d) {
108 continue;
109 }
110 }
djsollen@google.com8ff1e512012-03-12 14:39:20 +0000111 dst[i] = SkFourByteInterp256(src[i], dst[i], d);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 }
113 }
114}
115
reed@google.com30da7452012-12-17 19:55:24 +0000116static inline U16CPU SkBlend3216(SkPMColor src, U16CPU dst, unsigned scale) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 SkASSERT(scale <= 32);
118 scale <<= 3;
119
120 return SkPackRGB16( SkAlphaBlend(SkPacked32ToR16(src), SkGetPackedR16(dst), scale),
121 SkAlphaBlend(SkPacked32ToG16(src), SkGetPackedG16(dst), scale),
122 SkAlphaBlend(SkPacked32ToB16(src), SkGetPackedB16(dst), scale));
123}
124
125void SkAvoidXfermode::xfer16(uint16_t dst[], const SkPMColor src[], int count,
reed@google.com30da7452012-12-17 19:55:24 +0000126 const SkAlpha aa[]) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 unsigned opR = SkColorGetR(fOpColor) >> (8 - SK_R16_BITS);
128 unsigned opG = SkColorGetG(fOpColor) >> (8 - SK_G16_BITS);
129 unsigned opB = SkColorGetB(fOpColor) >> (8 - SK_R16_BITS);
130 uint32_t mul = fDistMul;
reed@android.com0db5a7f2009-11-09 16:01:36 +0000131 uint32_t sub = (fDistMul - (1 << 14)) << SK_R16_BITS;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132
133 int MAX, mask;
tomhudson@google.com1447c6f2011-04-27 14:09:52 +0000134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 if (kTargetColor_Mode == fMode) {
136 mask = -1;
137 MAX = 31;
138 } else {
139 mask = 0;
140 MAX = 0;
141 }
142
143 for (int i = 0; i < count; i++) {
144 int d = color_dist16(dst[i], opR, opG, opB);
145 // now reverse d if we need to
146 d = MAX + (d ^ mask) - mask;
147 SkASSERT((unsigned)d <= 31);
148 // convert from 0..31 to 0..32
149 d += d >> 4;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 d = scale_dist_14(d, mul, sub);
151 SkASSERT(d <= 32);
152
153 if (d > 0) {
154 if (NULL != aa) {
reed@android.com370eeeb2010-03-12 19:23:27 +0000155 d = SkAlphaMul(d, Accurate255To256(*aa++));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 if (0 == d) {
157 continue;
158 }
159 }
160 dst[i] = SkBlend3216(src[i], dst[i], d);
161 }
162 }
163}
164
reed@google.com30da7452012-12-17 19:55:24 +0000165void SkAvoidXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count,
166 const SkAlpha aa[]) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 // override in subclass
168}
robertphillips@google.comb83b6b42013-01-22 14:32:09 +0000169
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000170#ifndef SK_IGNORE_TO_STRING
robertphillips@google.comb83b6b42013-01-22 14:32:09 +0000171void SkAvoidXfermode::toString(SkString* str) const {
172 str->append("SkAvoidXfermode: opColor: ");
173 str->appendHex(fOpColor);
174 str->appendf("distMul: %d ", fDistMul);
175
176 static const char* gModeStrings[] = { "Avoid", "Target" };
177
178 str->appendf("mode: %s", gModeStrings[fMode]);
179}
180#endif