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