gfx/skia/trunk/src/core/SkShader.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 "SkBitmapProcShader.h"
michael@0 9 #include "SkReadBuffer.h"
michael@0 10 #include "SkMallocPixelRef.h"
michael@0 11 #include "SkPaint.h"
michael@0 12 #include "SkScalar.h"
michael@0 13 #include "SkShader.h"
michael@0 14 #include "SkWriteBuffer.h"
michael@0 15
michael@0 16 SkShader::SkShader() {
michael@0 17 fLocalMatrix.reset();
michael@0 18 SkDEBUGCODE(fInSetContext = false;)
michael@0 19 }
michael@0 20
michael@0 21 SkShader::SkShader(SkReadBuffer& buffer)
michael@0 22 : INHERITED(buffer) {
michael@0 23 if (buffer.readBool()) {
michael@0 24 buffer.readMatrix(&fLocalMatrix);
michael@0 25 } else {
michael@0 26 fLocalMatrix.reset();
michael@0 27 }
michael@0 28
michael@0 29 SkDEBUGCODE(fInSetContext = false;)
michael@0 30 }
michael@0 31
michael@0 32 SkShader::~SkShader() {
michael@0 33 SkASSERT(!fInSetContext);
michael@0 34 }
michael@0 35
michael@0 36 void SkShader::flatten(SkWriteBuffer& buffer) const {
michael@0 37 this->INHERITED::flatten(buffer);
michael@0 38 bool hasLocalM = this->hasLocalMatrix();
michael@0 39 buffer.writeBool(hasLocalM);
michael@0 40 if (hasLocalM) {
michael@0 41 buffer.writeMatrix(fLocalMatrix);
michael@0 42 }
michael@0 43 }
michael@0 44
michael@0 45 bool SkShader::setContext(const SkBitmap& device,
michael@0 46 const SkPaint& paint,
michael@0 47 const SkMatrix& matrix) {
michael@0 48 SkASSERT(!this->setContextHasBeenCalled());
michael@0 49
michael@0 50 const SkMatrix* m = &matrix;
michael@0 51 SkMatrix total;
michael@0 52
michael@0 53 fPaintAlpha = paint.getAlpha();
michael@0 54 if (this->hasLocalMatrix()) {
michael@0 55 total.setConcat(matrix, this->getLocalMatrix());
michael@0 56 m = &total;
michael@0 57 }
michael@0 58 if (m->invert(&fTotalInverse)) {
michael@0 59 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
michael@0 60 SkDEBUGCODE(fInSetContext = true;)
michael@0 61 return true;
michael@0 62 }
michael@0 63 return false;
michael@0 64 }
michael@0 65
michael@0 66 void SkShader::endContext() {
michael@0 67 SkASSERT(fInSetContext);
michael@0 68 SkDEBUGCODE(fInSetContext = false;)
michael@0 69 }
michael@0 70
michael@0 71 SkShader::ShadeProc SkShader::asAShadeProc(void** ctx) {
michael@0 72 return NULL;
michael@0 73 }
michael@0 74
michael@0 75 #include "SkColorPriv.h"
michael@0 76
michael@0 77 void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) {
michael@0 78 SkASSERT(span16);
michael@0 79 SkASSERT(count > 0);
michael@0 80 SkASSERT(this->canCallShadeSpan16());
michael@0 81
michael@0 82 // basically, if we get here, the subclass screwed up
michael@0 83 SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented");
michael@0 84 }
michael@0 85
michael@0 86 #define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space
michael@0 87 #define kTempColorCount (kTempColorQuadCount << 2)
michael@0 88
michael@0 89 #ifdef SK_CPU_BENDIAN
michael@0 90 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
michael@0 91 #else
michael@0 92 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
michael@0 93 #endif
michael@0 94
michael@0 95 void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
michael@0 96 SkASSERT(count > 0);
michael@0 97
michael@0 98 SkPMColor colors[kTempColorCount];
michael@0 99
michael@0 100 while ((count -= kTempColorCount) >= 0) {
michael@0 101 this->shadeSpan(x, y, colors, kTempColorCount);
michael@0 102 x += kTempColorCount;
michael@0 103
michael@0 104 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
michael@0 105 int quads = kTempColorQuadCount;
michael@0 106 do {
michael@0 107 U8CPU a0 = srcA[0];
michael@0 108 U8CPU a1 = srcA[4];
michael@0 109 U8CPU a2 = srcA[8];
michael@0 110 U8CPU a3 = srcA[12];
michael@0 111 srcA += 4*4;
michael@0 112 *alpha++ = SkToU8(a0);
michael@0 113 *alpha++ = SkToU8(a1);
michael@0 114 *alpha++ = SkToU8(a2);
michael@0 115 *alpha++ = SkToU8(a3);
michael@0 116 } while (--quads != 0);
michael@0 117 }
michael@0 118 SkASSERT(count < 0);
michael@0 119 SkASSERT(count + kTempColorCount >= 0);
michael@0 120 if (count += kTempColorCount) {
michael@0 121 this->shadeSpan(x, y, colors, count);
michael@0 122
michael@0 123 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
michael@0 124 do {
michael@0 125 *alpha++ = *srcA;
michael@0 126 srcA += 4;
michael@0 127 } while (--count != 0);
michael@0 128 }
michael@0 129 #if 0
michael@0 130 do {
michael@0 131 int n = count;
michael@0 132 if (n > kTempColorCount)
michael@0 133 n = kTempColorCount;
michael@0 134 SkASSERT(n > 0);
michael@0 135
michael@0 136 this->shadeSpan(x, y, colors, n);
michael@0 137 x += n;
michael@0 138 count -= n;
michael@0 139
michael@0 140 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
michael@0 141 do {
michael@0 142 *alpha++ = *srcA;
michael@0 143 srcA += 4;
michael@0 144 } while (--n != 0);
michael@0 145 } while (count > 0);
michael@0 146 #endif
michael@0 147 }
michael@0 148
michael@0 149 SkShader::MatrixClass SkShader::ComputeMatrixClass(const SkMatrix& mat) {
michael@0 150 MatrixClass mc = kLinear_MatrixClass;
michael@0 151
michael@0 152 if (mat.hasPerspective()) {
michael@0 153 if (mat.fixedStepInX(0, NULL, NULL)) {
michael@0 154 mc = kFixedStepInX_MatrixClass;
michael@0 155 } else {
michael@0 156 mc = kPerspective_MatrixClass;
michael@0 157 }
michael@0 158 }
michael@0 159 return mc;
michael@0 160 }
michael@0 161
michael@0 162 //////////////////////////////////////////////////////////////////////////////
michael@0 163
michael@0 164 SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*,
michael@0 165 TileMode*) const {
michael@0 166 return kNone_BitmapType;
michael@0 167 }
michael@0 168
michael@0 169 SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
michael@0 170 return kNone_GradientType;
michael@0 171 }
michael@0 172
michael@0 173 GrEffectRef* SkShader::asNewEffect(GrContext*, const SkPaint&) const {
michael@0 174 return NULL;
michael@0 175 }
michael@0 176
michael@0 177 SkShader* SkShader::CreateBitmapShader(const SkBitmap& src,
michael@0 178 TileMode tmx, TileMode tmy) {
michael@0 179 return ::CreateBitmapShader(src, tmx, tmy, NULL);
michael@0 180 }
michael@0 181
michael@0 182 #ifndef SK_IGNORE_TO_STRING
michael@0 183 void SkShader::toString(SkString* str) const {
michael@0 184 if (this->hasLocalMatrix()) {
michael@0 185 str->append(" ");
michael@0 186 this->getLocalMatrix().toString(str);
michael@0 187 }
michael@0 188 }
michael@0 189 #endif
michael@0 190
michael@0 191 //////////////////////////////////////////////////////////////////////////////
michael@0 192
michael@0 193 #include "SkColorShader.h"
michael@0 194 #include "SkUtils.h"
michael@0 195
michael@0 196 SkColorShader::SkColorShader() {
michael@0 197 fFlags = 0;
michael@0 198 fInheritColor = true;
michael@0 199 }
michael@0 200
michael@0 201 SkColorShader::SkColorShader(SkColor c) {
michael@0 202 fFlags = 0;
michael@0 203 fColor = c;
michael@0 204 fInheritColor = false;
michael@0 205 }
michael@0 206
michael@0 207 SkColorShader::~SkColorShader() {}
michael@0 208
michael@0 209 bool SkColorShader::isOpaque() const {
michael@0 210 if (fInheritColor) {
michael@0 211 return true; // using paint's alpha
michael@0 212 }
michael@0 213 return SkColorGetA(fColor) == 255;
michael@0 214 }
michael@0 215
michael@0 216 SkColorShader::SkColorShader(SkReadBuffer& b) : INHERITED(b) {
michael@0 217 fFlags = 0; // computed in setContext
michael@0 218
michael@0 219 fInheritColor = b.readBool();
michael@0 220 if (fInheritColor) {
michael@0 221 return;
michael@0 222 }
michael@0 223 fColor = b.readColor();
michael@0 224 }
michael@0 225
michael@0 226 void SkColorShader::flatten(SkWriteBuffer& buffer) const {
michael@0 227 this->INHERITED::flatten(buffer);
michael@0 228 buffer.writeBool(fInheritColor);
michael@0 229 if (fInheritColor) {
michael@0 230 return;
michael@0 231 }
michael@0 232 buffer.writeColor(fColor);
michael@0 233 }
michael@0 234
michael@0 235 uint32_t SkColorShader::getFlags() {
michael@0 236 return fFlags;
michael@0 237 }
michael@0 238
michael@0 239 uint8_t SkColorShader::getSpan16Alpha() const {
michael@0 240 return SkGetPackedA32(fPMColor);
michael@0 241 }
michael@0 242
michael@0 243 bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint,
michael@0 244 const SkMatrix& matrix) {
michael@0 245 if (!this->INHERITED::setContext(device, paint, matrix)) {
michael@0 246 return false;
michael@0 247 }
michael@0 248
michael@0 249 unsigned a;
michael@0 250
michael@0 251 if (fInheritColor) {
michael@0 252 fColor = paint.getColor();
michael@0 253 a = SkColorGetA(fColor);
michael@0 254 } else {
michael@0 255 a = SkAlphaMul(SkColorGetA(fColor), SkAlpha255To256(paint.getAlpha()));
michael@0 256 }
michael@0 257
michael@0 258 unsigned r = SkColorGetR(fColor);
michael@0 259 unsigned g = SkColorGetG(fColor);
michael@0 260 unsigned b = SkColorGetB(fColor);
michael@0 261
michael@0 262 // we want this before we apply any alpha
michael@0 263 fColor16 = SkPack888ToRGB16(r, g, b);
michael@0 264
michael@0 265 if (a != 255) {
michael@0 266 r = SkMulDiv255Round(r, a);
michael@0 267 g = SkMulDiv255Round(g, a);
michael@0 268 b = SkMulDiv255Round(b, a);
michael@0 269 }
michael@0 270 fPMColor = SkPackARGB32(a, r, g, b);
michael@0 271
michael@0 272 fFlags = kConstInY32_Flag;
michael@0 273 if (255 == a) {
michael@0 274 fFlags |= kOpaqueAlpha_Flag;
michael@0 275 if (paint.isDither() == false) {
michael@0 276 fFlags |= kHasSpan16_Flag;
michael@0 277 }
michael@0 278 }
michael@0 279
michael@0 280 return true;
michael@0 281 }
michael@0 282
michael@0 283 void SkColorShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
michael@0 284 sk_memset32(span, fPMColor, count);
michael@0 285 }
michael@0 286
michael@0 287 void SkColorShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
michael@0 288 sk_memset16(span, fColor16, count);
michael@0 289 }
michael@0 290
michael@0 291 void SkColorShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
michael@0 292 memset(alpha, SkGetPackedA32(fPMColor), count);
michael@0 293 }
michael@0 294
michael@0 295 // if we had a asAColor method, that would be more efficient...
michael@0 296 SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
michael@0 297 TileMode modes[]) const {
michael@0 298 return kNone_BitmapType;
michael@0 299 }
michael@0 300
michael@0 301 SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const {
michael@0 302 if (info) {
michael@0 303 if (info->fColors && info->fColorCount >= 1) {
michael@0 304 info->fColors[0] = fColor;
michael@0 305 }
michael@0 306 info->fColorCount = 1;
michael@0 307 info->fTileMode = SkShader::kRepeat_TileMode;
michael@0 308 }
michael@0 309 return kColor_GradientType;
michael@0 310 }
michael@0 311
michael@0 312 #ifndef SK_IGNORE_TO_STRING
michael@0 313 void SkColorShader::toString(SkString* str) const {
michael@0 314 str->append("SkColorShader: (");
michael@0 315
michael@0 316 if (fInheritColor) {
michael@0 317 str->append("Color: inherited from paint");
michael@0 318 } else {
michael@0 319 str->append("Color: ");
michael@0 320 str->appendHex(fColor);
michael@0 321 }
michael@0 322
michael@0 323 this->INHERITED::toString(str);
michael@0 324
michael@0 325 str->append(")");
michael@0 326 }
michael@0 327 #endif
michael@0 328
michael@0 329 ///////////////////////////////////////////////////////////////////////////////
michael@0 330
michael@0 331 #include "SkEmptyShader.h"
michael@0 332
michael@0 333 uint32_t SkEmptyShader::getFlags() { return 0; }
michael@0 334 uint8_t SkEmptyShader::getSpan16Alpha() const { return 0; }
michael@0 335
michael@0 336 bool SkEmptyShader::setContext(const SkBitmap&, const SkPaint&,
michael@0 337 const SkMatrix&) { return false; }
michael@0 338
michael@0 339 void SkEmptyShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
michael@0 340 SkDEBUGFAIL("should never get called, since setContext() returned false");
michael@0 341 }
michael@0 342
michael@0 343 void SkEmptyShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
michael@0 344 SkDEBUGFAIL("should never get called, since setContext() returned false");
michael@0 345 }
michael@0 346
michael@0 347 void SkEmptyShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
michael@0 348 SkDEBUGFAIL("should never get called, since setContext() returned false");
michael@0 349 }
michael@0 350
michael@0 351 #ifndef SK_IGNORE_TO_STRING
michael@0 352 void SkEmptyShader::toString(SkString* str) const {
michael@0 353 str->append("SkEmptyShader: (");
michael@0 354
michael@0 355 this->INHERITED::toString(str);
michael@0 356
michael@0 357 str->append(")");
michael@0 358 }
michael@0 359 #endif

mercurial