blob: 2f1def62c18f7a1c762542cfc14071d3bd911d5c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +000010#include "SkScalar.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkShader.h"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000012#include "SkFlattenableBuffers.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkPaint.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000014#include "SkMallocPixelRef.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000016SK_DEFINE_INST_COUNT(SkShader)
17
reed@android.com8a1c16f2008-12-17 15:59:43 +000018SkShader::SkShader() : fLocalMatrix(NULL) {
19 SkDEBUGCODE(fInSession = false;)
20}
21
22SkShader::SkShader(SkFlattenableReadBuffer& buffer)
23 : INHERITED(buffer), fLocalMatrix(NULL) {
24 if (buffer.readBool()) {
25 SkMatrix matrix;
djsollen@google.com2b2ede32012-04-12 13:24:04 +000026 buffer.readMatrix(&matrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 setLocalMatrix(matrix);
28 }
29 SkDEBUGCODE(fInSession = false;)
30}
31
32SkShader::~SkShader() {
33 SkASSERT(!fInSession);
34 sk_free(fLocalMatrix);
35}
36
37void SkShader::beginSession() {
38 SkASSERT(!fInSession);
39 SkDEBUGCODE(fInSession = true;)
40}
41
42void SkShader::endSession() {
43 SkASSERT(fInSession);
44 SkDEBUGCODE(fInSession = false;)
45}
46
djsollen@google.com54924242012-03-29 15:18:04 +000047void SkShader::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 this->INHERITED::flatten(buffer);
49 buffer.writeBool(fLocalMatrix != NULL);
50 if (fLocalMatrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +000051 buffer.writeMatrix(*fLocalMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 }
53}
54
55bool SkShader::getLocalMatrix(SkMatrix* localM) const {
56 if (fLocalMatrix) {
57 if (localM) {
58 *localM = *fLocalMatrix;
59 }
60 return true;
61 } else {
62 if (localM) {
63 localM->reset();
64 }
65 return false;
66 }
67}
68
69void SkShader::setLocalMatrix(const SkMatrix& localM) {
70 if (localM.isIdentity()) {
71 this->resetLocalMatrix();
72 } else {
73 if (fLocalMatrix == NULL) {
74 fLocalMatrix = (SkMatrix*)sk_malloc_throw(sizeof(SkMatrix));
75 }
76 *fLocalMatrix = localM;
77 }
78}
79
80void SkShader::resetLocalMatrix() {
81 if (fLocalMatrix) {
82 sk_free(fLocalMatrix);
83 fLocalMatrix = NULL;
84 }
85}
86
87bool SkShader::setContext(const SkBitmap& device,
88 const SkPaint& paint,
89 const SkMatrix& matrix) {
90 const SkMatrix* m = &matrix;
91 SkMatrix total;
92
93 fDeviceConfig = SkToU8(device.getConfig());
94 fPaintAlpha = paint.getAlpha();
95 if (fLocalMatrix) {
96 total.setConcat(matrix, *fLocalMatrix);
97 m = &total;
98 }
99 if (m->invert(&fTotalInverse)) {
100 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
101 return true;
102 }
103 return false;
104}
105
reed@google.com3bafe742012-10-12 18:56:18 +0000106SkShader::ShadeProc SkShader::asAShadeProc(void** ctx) {
107 return NULL;
108}
109
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110#include "SkColorPriv.h"
111
112void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) {
113 SkASSERT(span16);
114 SkASSERT(count > 0);
115 SkASSERT(this->canCallShadeSpan16());
116
117 // basically, if we get here, the subclass screwed up
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000118 SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119}
120
121#define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space
reed@google.com7c2f27d2011-03-07 19:29:00 +0000122#define kTempColorCount (kTempColorQuadCount << 2)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123
124#ifdef SK_CPU_BENDIAN
125 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
126#else
127 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
128#endif
129
130void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
131 SkASSERT(count > 0);
132
133 SkPMColor colors[kTempColorCount];
134
135 while ((count -= kTempColorCount) >= 0) {
136 this->shadeSpan(x, y, colors, kTempColorCount);
137 x += kTempColorCount;
138
139 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
140 int quads = kTempColorQuadCount;
141 do {
142 U8CPU a0 = srcA[0];
143 U8CPU a1 = srcA[4];
144 U8CPU a2 = srcA[8];
145 U8CPU a3 = srcA[12];
146 srcA += 4*4;
147 *alpha++ = SkToU8(a0);
148 *alpha++ = SkToU8(a1);
149 *alpha++ = SkToU8(a2);
150 *alpha++ = SkToU8(a3);
151 } while (--quads != 0);
152 }
153 SkASSERT(count < 0);
154 SkASSERT(count + kTempColorCount >= 0);
155 if (count += kTempColorCount) {
156 this->shadeSpan(x, y, colors, count);
157
158 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
159 do {
160 *alpha++ = *srcA;
161 srcA += 4;
162 } while (--count != 0);
163 }
164#if 0
165 do {
166 int n = count;
167 if (n > kTempColorCount)
168 n = kTempColorCount;
169 SkASSERT(n > 0);
170
171 this->shadeSpan(x, y, colors, n);
172 x += n;
173 count -= n;
174
175 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
176 do {
177 *alpha++ = *srcA;
178 srcA += 4;
179 } while (--n != 0);
180 } while (count > 0);
181#endif
182}
183
184SkShader::MatrixClass SkShader::ComputeMatrixClass(const SkMatrix& mat) {
185 MatrixClass mc = kLinear_MatrixClass;
186
tomhudson@google.com8d430182011-06-06 19:11:19 +0000187 if (mat.hasPerspective()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 if (mat.fixedStepInX(0, NULL, NULL)) {
189 mc = kFixedStepInX_MatrixClass;
190 } else {
191 mc = kPerspective_MatrixClass;
192 }
193 }
194 return mc;
195}
196
197//////////////////////////////////////////////////////////////////////////////
198
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000199SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*,
rileya@google.com91f319c2012-07-25 17:18:31 +0000200 TileMode*) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000201 return kNone_BitmapType;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202}
203
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000204SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
205 return kNone_GradientType;
206}
207
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000208GrCustomStage* SkShader::asNewCustomStage(GrContext* context,
rileya@google.com03c1c352012-07-20 20:02:43 +0000209 GrSamplerState* sampler) const {
210 return NULL;
211}
212
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213SkShader* SkShader::CreateBitmapShader(const SkBitmap& src,
214 TileMode tmx, TileMode tmy) {
215 return SkShader::CreateBitmapShader(src, tmx, tmy, NULL, 0);
216}
217
218//////////////////////////////////////////////////////////////////////////////
219
220#include "SkColorShader.h"
221#include "SkUtils.h"
222
reed@android.comf2b98d62010-12-20 18:26:13 +0000223SkColorShader::SkColorShader() {
224 fFlags = 0;
225 fInheritColor = true;
reed@android.comf2b98d62010-12-20 18:26:13 +0000226}
227
228SkColorShader::SkColorShader(SkColor c) {
229 fFlags = 0;
230 fColor = c;
231 fInheritColor = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000232}
233
reed@google.com2be9e8b2011-07-06 21:18:09 +0000234SkColorShader::~SkColorShader() {}
reed@android.comf2b98d62010-12-20 18:26:13 +0000235
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000236bool SkColorShader::isOpaque() const {
237 if (fInheritColor) {
238 return true; // using paint's alpha
239 }
240 return SkColorGetA(fColor) == 255;
241}
242
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243SkColorShader::SkColorShader(SkFlattenableReadBuffer& b) : INHERITED(b) {
reed@android.com5119bdb2009-06-12 21:27:03 +0000244 fFlags = 0; // computed in setContext
reed@android.comf2b98d62010-12-20 18:26:13 +0000245
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000246 fInheritColor = b.readBool();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247 if (fInheritColor) {
248 return;
249 }
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000250 fColor = b.readColor();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251}
252
djsollen@google.com54924242012-03-29 15:18:04 +0000253void SkColorShader::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254 this->INHERITED::flatten(buffer);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000255 buffer.writeBool(fInheritColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 if (fInheritColor) {
257 return;
258 }
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000259 buffer.writeColor(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260}
261
reed@google.com59ccef62011-12-07 14:59:50 +0000262uint32_t SkColorShader::getFlags() {
263 return fFlags;
264}
265
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266uint8_t SkColorShader::getSpan16Alpha() const {
267 return SkGetPackedA32(fPMColor);
268}
269
270bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint,
271 const SkMatrix& matrix) {
272 if (!this->INHERITED::setContext(device, paint, matrix)) {
273 return false;
274 }
275
reed@android.com8a1c16f2008-12-17 15:59:43 +0000276 unsigned a;
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000277
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278 if (fInheritColor) {
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000279 fColor = paint.getColor();
280 a = SkColorGetA(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000281 } else {
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000282 a = SkAlphaMul(SkColorGetA(fColor), SkAlpha255To256(paint.getAlpha()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 }
284
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000285 unsigned r = SkColorGetR(fColor);
286 unsigned g = SkColorGetG(fColor);
287 unsigned b = SkColorGetB(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288
289 // we want this before we apply any alpha
290 fColor16 = SkPack888ToRGB16(r, g, b);
291
292 if (a != 255) {
reed@android.com8f073382010-03-11 21:56:16 +0000293 r = SkMulDiv255Round(r, a);
294 g = SkMulDiv255Round(g, a);
295 b = SkMulDiv255Round(b, a);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296 }
297 fPMColor = SkPackARGB32(a, r, g, b);
298
reed@android.com8f073382010-03-11 21:56:16 +0000299 fFlags = kConstInY32_Flag;
reed@android.com5b815352010-03-11 22:20:43 +0000300 if (255 == a) {
reed@android.com5119bdb2009-06-12 21:27:03 +0000301 fFlags |= kOpaqueAlpha_Flag;
reed@android.com5b815352010-03-11 22:20:43 +0000302 if (paint.isDither() == false) {
303 fFlags |= kHasSpan16_Flag;
304 }
reed@android.com5119bdb2009-06-12 21:27:03 +0000305 }
306
reed@android.com8a1c16f2008-12-17 15:59:43 +0000307 return true;
308}
309
310void SkColorShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
311 sk_memset32(span, fPMColor, count);
312}
313
314void SkColorShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
315 sk_memset16(span, fColor16, count);
316}
317
318void SkColorShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
319 memset(alpha, SkGetPackedA32(fPMColor), count);
320}
321
reed@android.comf2b98d62010-12-20 18:26:13 +0000322// if we had a asAColor method, that would be more efficient...
323SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000324 TileMode modes[]) const {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000325 return kNone_BitmapType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000326}
327
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000328SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const {
329 if (info) {
330 if (info->fColors && info->fColorCount >= 1) {
331 info->fColors[0] = fColor;
332 }
333 info->fColorCount = 1;
334 info->fTileMode = SkShader::kRepeat_TileMode;
335 }
336 return kColor_GradientType;
337}
reed@google.com37a20122011-07-05 18:54:12 +0000338
339///////////////////////////////////////////////////////////////////////////////
340
341#include "SkEmptyShader.h"
342
reed@google.com37a20122011-07-05 18:54:12 +0000343uint32_t SkEmptyShader::getFlags() { return 0; }
344uint8_t SkEmptyShader::getSpan16Alpha() const { return 0; }
reed@google.com59ccef62011-12-07 14:59:50 +0000345
346bool SkEmptyShader::setContext(const SkBitmap&, const SkPaint&,
347 const SkMatrix&) { return false; }
348
349void SkEmptyShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000350 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com37a20122011-07-05 18:54:12 +0000351}
reed@google.com59ccef62011-12-07 14:59:50 +0000352
353void SkEmptyShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000354 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com59ccef62011-12-07 14:59:50 +0000355}
356
357void SkEmptyShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000358 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com59ccef62011-12-07 14:59:50 +0000359}