2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkReadBuffer.h"
12 #include "SkWriteBuffer.h"
14 #include "SkMallocPixelRef.h"
16 SkShader::SkShader() {
18 SkDEBUGCODE(fInSetContext = false;)
21 SkShader::SkShader(SkReadBuffer& buffer)
23 if (buffer.readBool()) {
24 buffer.readMatrix(&fLocalMatrix);
29 SkDEBUGCODE(fInSetContext = false;)
32 SkShader::~SkShader() {
33 SkASSERT(!fInSetContext);
36 void SkShader::flatten(SkWriteBuffer& buffer) const {
37 this->INHERITED::flatten(buffer);
38 bool hasLocalM = this->hasLocalMatrix();
39 buffer.writeBool(hasLocalM);
41 buffer.writeMatrix(fLocalMatrix);
45 bool SkShader::setContext(const SkBitmap& device,
47 const SkMatrix& matrix) {
48 SkASSERT(!this->setContextHasBeenCalled());
50 const SkMatrix* m = &matrix;
53 fPaintAlpha = paint.getAlpha();
54 if (this->hasLocalMatrix()) {
55 total.setConcat(matrix, this->getLocalMatrix());
58 if (m->invert(&fTotalInverse)) {
59 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
60 SkDEBUGCODE(fInSetContext = true;)
66 void SkShader::endContext() {
67 SkASSERT(fInSetContext);
68 SkDEBUGCODE(fInSetContext = false;)
71 SkShader::ShadeProc SkShader::asAShadeProc(void** ctx) {
75 #include "SkColorPriv.h"
77 void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) {
80 SkASSERT(this->canCallShadeSpan16());
82 // basically, if we get here, the subclass screwed up
83 SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented");
86 #define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space
87 #define kTempColorCount (kTempColorQuadCount << 2)
90 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
92 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
95 void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
98 SkPMColor colors[kTempColorCount];
100 while ((count -= kTempColorCount) >= 0) {
101 this->shadeSpan(x, y, colors, kTempColorCount);
102 x += kTempColorCount;
104 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
105 int quads = kTempColorQuadCount;
112 *alpha++ = SkToU8(a0);
113 *alpha++ = SkToU8(a1);
114 *alpha++ = SkToU8(a2);
115 *alpha++ = SkToU8(a3);
116 } while (--quads != 0);
119 SkASSERT(count + kTempColorCount >= 0);
120 if (count += kTempColorCount) {
121 this->shadeSpan(x, y, colors, count);
123 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
127 } while (--count != 0);
132 if (n > kTempColorCount)
136 this->shadeSpan(x, y, colors, n);
140 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
149 SkShader::MatrixClass SkShader::ComputeMatrixClass(const SkMatrix& mat) {
150 MatrixClass mc = kLinear_MatrixClass;
152 if (mat.hasPerspective()) {
153 if (mat.fixedStepInX(0, NULL, NULL)) {
154 mc = kFixedStepInX_MatrixClass;
156 mc = kPerspective_MatrixClass;
162 //////////////////////////////////////////////////////////////////////////////
164 SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*,
166 return kNone_BitmapType;
169 SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
170 return kNone_GradientType;
173 GrEffectRef* SkShader::asNewEffect(GrContext*, const SkPaint&) const {
177 SkShader* SkShader::CreateBitmapShader(const SkBitmap& src,
178 TileMode tmx, TileMode tmy) {
179 return SkShader::CreateBitmapShader(src, tmx, tmy, NULL, 0);
183 void SkShader::toString(SkString* str) const {
184 if (this->hasLocalMatrix()) {
186 this->getLocalMatrix().toString(str);
191 //////////////////////////////////////////////////////////////////////////////
193 #include "SkColorShader.h"
196 SkColorShader::SkColorShader() {
198 fInheritColor = true;
201 SkColorShader::SkColorShader(SkColor c) {
204 fInheritColor = false;
207 SkColorShader::~SkColorShader() {}
209 bool SkColorShader::isOpaque() const {
211 return true; // using paint's alpha
213 return SkColorGetA(fColor) == 255;
216 SkColorShader::SkColorShader(SkReadBuffer& b) : INHERITED(b) {
217 fFlags = 0; // computed in setContext
219 fInheritColor = b.readBool();
223 fColor = b.readColor();
226 void SkColorShader::flatten(SkWriteBuffer& buffer) const {
227 this->INHERITED::flatten(buffer);
228 buffer.writeBool(fInheritColor);
232 buffer.writeColor(fColor);
235 uint32_t SkColorShader::getFlags() {
239 uint8_t SkColorShader::getSpan16Alpha() const {
240 return SkGetPackedA32(fPMColor);
243 bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint,
244 const SkMatrix& matrix) {
245 if (!this->INHERITED::setContext(device, paint, matrix)) {
252 fColor = paint.getColor();
253 a = SkColorGetA(fColor);
255 a = SkAlphaMul(SkColorGetA(fColor), SkAlpha255To256(paint.getAlpha()));
258 unsigned r = SkColorGetR(fColor);
259 unsigned g = SkColorGetG(fColor);
260 unsigned b = SkColorGetB(fColor);
262 // we want this before we apply any alpha
263 fColor16 = SkPack888ToRGB16(r, g, b);
266 r = SkMulDiv255Round(r, a);
267 g = SkMulDiv255Round(g, a);
268 b = SkMulDiv255Round(b, a);
270 fPMColor = SkPackARGB32(a, r, g, b);
272 fFlags = kConstInY32_Flag;
274 fFlags |= kOpaqueAlpha_Flag;
275 if (paint.isDither() == false) {
276 fFlags |= kHasSpan16_Flag;
283 void SkColorShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
284 sk_memset32(span, fPMColor, count);
287 void SkColorShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
288 sk_memset16(span, fColor16, count);
291 void SkColorShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
292 memset(alpha, SkGetPackedA32(fPMColor), count);
295 // if we had a asAColor method, that would be more efficient...
296 SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
297 TileMode modes[]) const {
298 return kNone_BitmapType;
301 SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const {
303 if (info->fColors && info->fColorCount >= 1) {
304 info->fColors[0] = fColor;
306 info->fColorCount = 1;
307 info->fTileMode = SkShader::kRepeat_TileMode;
309 return kColor_GradientType;
313 void SkColorShader::toString(SkString* str) const {
314 str->append("SkColorShader: (");
317 str->append("Color: inherited from paint");
319 str->append("Color: ");
320 str->appendHex(fColor);
323 this->INHERITED::toString(str);
329 ///////////////////////////////////////////////////////////////////////////////
331 #include "SkEmptyShader.h"
333 uint32_t SkEmptyShader::getFlags() { return 0; }
334 uint8_t SkEmptyShader::getSpan16Alpha() const { return 0; }
336 bool SkEmptyShader::setContext(const SkBitmap&, const SkPaint&,
337 const SkMatrix&) { return false; }
339 void SkEmptyShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
340 SkDEBUGFAIL("should never get called, since setContext() returned false");
343 void SkEmptyShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
344 SkDEBUGFAIL("should never get called, since setContext() returned false");
347 void SkEmptyShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
348 SkDEBUGFAIL("should never get called, since setContext() returned false");
352 void SkEmptyShader::toString(SkString* str) const {
353 str->append("SkEmptyShader: (");
355 this->INHERITED::toString(str);