gfx/skia/trunk/src/effects/SkAvoidXfermode.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright 2006 The Android Open Source Project
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkAvoidXfermode.h"
michael@0 9 #include "SkColorPriv.h"
michael@0 10 #include "SkReadBuffer.h"
michael@0 11 #include "SkWriteBuffer.h"
michael@0 12 #include "SkString.h"
michael@0 13
michael@0 14 SkAvoidXfermode::SkAvoidXfermode(SkColor opColor, U8CPU tolerance, Mode mode) {
michael@0 15 if (tolerance > 255) {
michael@0 16 tolerance = 255;
michael@0 17 }
michael@0 18
michael@0 19 fOpColor = opColor;
michael@0 20 fDistMul = (256 << 14) / (tolerance + 1);
michael@0 21 fMode = mode;
michael@0 22 }
michael@0 23
michael@0 24 SkAvoidXfermode::SkAvoidXfermode(SkReadBuffer& buffer)
michael@0 25 : INHERITED(buffer) {
michael@0 26 fOpColor = buffer.readColor();
michael@0 27 fDistMul = buffer.readUInt();
michael@0 28 fMode = (Mode)buffer.readUInt();
michael@0 29 }
michael@0 30
michael@0 31 void SkAvoidXfermode::flatten(SkWriteBuffer& buffer) const {
michael@0 32 this->INHERITED::flatten(buffer);
michael@0 33
michael@0 34 buffer.writeColor(fOpColor);
michael@0 35 buffer.writeUInt(fDistMul);
michael@0 36 buffer.writeUInt(fMode);
michael@0 37 }
michael@0 38
michael@0 39 // returns 0..31
michael@0 40 static unsigned color_dist16(uint16_t c, unsigned r, unsigned g, unsigned b) {
michael@0 41 SkASSERT(r <= SK_R16_MASK);
michael@0 42 SkASSERT(g <= SK_G16_MASK);
michael@0 43 SkASSERT(b <= SK_B16_MASK);
michael@0 44
michael@0 45 unsigned dr = SkAbs32(SkGetPackedR16(c) - r);
michael@0 46 unsigned dg = SkAbs32(SkGetPackedG16(c) - g) >> (SK_G16_BITS - SK_R16_BITS);
michael@0 47 unsigned db = SkAbs32(SkGetPackedB16(c) - b);
michael@0 48
michael@0 49 return SkMax32(dr, SkMax32(dg, db));
michael@0 50 }
michael@0 51
michael@0 52 // returns 0..255
michael@0 53 static unsigned color_dist32(SkPMColor c, U8CPU r, U8CPU g, U8CPU b) {
michael@0 54 SkASSERT(r <= 0xFF);
michael@0 55 SkASSERT(g <= 0xFF);
michael@0 56 SkASSERT(b <= 0xFF);
michael@0 57
michael@0 58 unsigned dr = SkAbs32(SkGetPackedR32(c) - r);
michael@0 59 unsigned dg = SkAbs32(SkGetPackedG32(c) - g);
michael@0 60 unsigned db = SkAbs32(SkGetPackedB32(c) - b);
michael@0 61
michael@0 62 return SkMax32(dr, SkMax32(dg, db));
michael@0 63 }
michael@0 64
michael@0 65 static int scale_dist_14(int dist, uint32_t mul, uint32_t sub) {
michael@0 66 int tmp = dist * mul - sub;
michael@0 67 int result = (tmp + (1 << 13)) >> 14;
michael@0 68
michael@0 69 return result;
michael@0 70 }
michael@0 71
michael@0 72 static inline unsigned Accurate255To256(unsigned x) {
michael@0 73 return x + (x >> 7);
michael@0 74 }
michael@0 75
michael@0 76 void SkAvoidXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], int count,
michael@0 77 const SkAlpha aa[]) const {
michael@0 78 unsigned opR = SkColorGetR(fOpColor);
michael@0 79 unsigned opG = SkColorGetG(fOpColor);
michael@0 80 unsigned opB = SkColorGetB(fOpColor);
michael@0 81 uint32_t mul = fDistMul;
michael@0 82 uint32_t sub = (fDistMul - (1 << 14)) << 8;
michael@0 83
michael@0 84 int MAX, mask;
michael@0 85
michael@0 86 if (kTargetColor_Mode == fMode) {
michael@0 87 mask = -1;
michael@0 88 MAX = 255;
michael@0 89 } else {
michael@0 90 mask = 0;
michael@0 91 MAX = 0;
michael@0 92 }
michael@0 93
michael@0 94 for (int i = 0; i < count; i++) {
michael@0 95 int d = color_dist32(dst[i], opR, opG, opB);
michael@0 96 // now reverse d if we need to
michael@0 97 d = MAX + (d ^ mask) - mask;
michael@0 98 SkASSERT((unsigned)d <= 255);
michael@0 99 d = Accurate255To256(d);
michael@0 100
michael@0 101 d = scale_dist_14(d, mul, sub);
michael@0 102 SkASSERT(d <= 256);
michael@0 103
michael@0 104 if (d > 0) {
michael@0 105 if (NULL != aa) {
michael@0 106 d = SkAlphaMul(d, Accurate255To256(*aa++));
michael@0 107 if (0 == d) {
michael@0 108 continue;
michael@0 109 }
michael@0 110 }
michael@0 111 dst[i] = SkFourByteInterp256(src[i], dst[i], d);
michael@0 112 }
michael@0 113 }
michael@0 114 }
michael@0 115
michael@0 116 static inline U16CPU SkBlend3216(SkPMColor src, U16CPU dst, unsigned scale) {
michael@0 117 SkASSERT(scale <= 32);
michael@0 118 scale <<= 3;
michael@0 119
michael@0 120 return SkPackRGB16( SkAlphaBlend(SkPacked32ToR16(src), SkGetPackedR16(dst), scale),
michael@0 121 SkAlphaBlend(SkPacked32ToG16(src), SkGetPackedG16(dst), scale),
michael@0 122 SkAlphaBlend(SkPacked32ToB16(src), SkGetPackedB16(dst), scale));
michael@0 123 }
michael@0 124
michael@0 125 void SkAvoidXfermode::xfer16(uint16_t dst[], const SkPMColor src[], int count,
michael@0 126 const SkAlpha aa[]) const {
michael@0 127 unsigned opR = SkColorGetR(fOpColor) >> (8 - SK_R16_BITS);
michael@0 128 unsigned opG = SkColorGetG(fOpColor) >> (8 - SK_G16_BITS);
michael@0 129 unsigned opB = SkColorGetB(fOpColor) >> (8 - SK_R16_BITS);
michael@0 130 uint32_t mul = fDistMul;
michael@0 131 uint32_t sub = (fDistMul - (1 << 14)) << SK_R16_BITS;
michael@0 132
michael@0 133 int MAX, mask;
michael@0 134
michael@0 135 if (kTargetColor_Mode == fMode) {
michael@0 136 mask = -1;
michael@0 137 MAX = 31;
michael@0 138 } else {
michael@0 139 mask = 0;
michael@0 140 MAX = 0;
michael@0 141 }
michael@0 142
michael@0 143 for (int i = 0; i < count; i++) {
michael@0 144 int d = color_dist16(dst[i], opR, opG, opB);
michael@0 145 // now reverse d if we need to
michael@0 146 d = MAX + (d ^ mask) - mask;
michael@0 147 SkASSERT((unsigned)d <= 31);
michael@0 148 // convert from 0..31 to 0..32
michael@0 149 d += d >> 4;
michael@0 150 d = scale_dist_14(d, mul, sub);
michael@0 151 SkASSERT(d <= 32);
michael@0 152
michael@0 153 if (d > 0) {
michael@0 154 if (NULL != aa) {
michael@0 155 d = SkAlphaMul(d, Accurate255To256(*aa++));
michael@0 156 if (0 == d) {
michael@0 157 continue;
michael@0 158 }
michael@0 159 }
michael@0 160 dst[i] = SkBlend3216(src[i], dst[i], d);
michael@0 161 }
michael@0 162 }
michael@0 163 }
michael@0 164
michael@0 165 void SkAvoidXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count,
michael@0 166 const SkAlpha aa[]) const {
michael@0 167 // override in subclass
michael@0 168 }
michael@0 169
michael@0 170 #ifndef SK_IGNORE_TO_STRING
michael@0 171 void SkAvoidXfermode::toString(SkString* str) const {
michael@0 172 str->append("SkAvoidXfermode: opColor: ");
michael@0 173 str->appendHex(fOpColor);
michael@0 174 str->appendf("distMul: %d ", fDistMul);
michael@0 175
michael@0 176 static const char* gModeStrings[] = { "Avoid", "Target" };
michael@0 177
michael@0 178 str->appendf("mode: %s", gModeStrings[fMode]);
michael@0 179 }
michael@0 180 #endif

mercurial