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