blob: 50546ffcc3ba24d4a21843993d3c1cf773184734 [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
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000208bool SkShader::asNewEffect(GrContext*, GrSamplerState*) const {
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000209 return false;
rileya@google.com03c1c352012-07-20 20:02:43 +0000210}
211
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212SkShader* SkShader::CreateBitmapShader(const SkBitmap& src,
213 TileMode tmx, TileMode tmy) {
214 return SkShader::CreateBitmapShader(src, tmx, tmy, NULL, 0);
215}
216
217//////////////////////////////////////////////////////////////////////////////
218
219#include "SkColorShader.h"
220#include "SkUtils.h"
221
reed@android.comf2b98d62010-12-20 18:26:13 +0000222SkColorShader::SkColorShader() {
223 fFlags = 0;
224 fInheritColor = true;
reed@android.comf2b98d62010-12-20 18:26:13 +0000225}
226
227SkColorShader::SkColorShader(SkColor c) {
228 fFlags = 0;
229 fColor = c;
230 fInheritColor = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000231}
232
reed@google.com2be9e8b2011-07-06 21:18:09 +0000233SkColorShader::~SkColorShader() {}
reed@android.comf2b98d62010-12-20 18:26:13 +0000234
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000235bool SkColorShader::isOpaque() const {
236 if (fInheritColor) {
237 return true; // using paint's alpha
238 }
239 return SkColorGetA(fColor) == 255;
240}
241
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242SkColorShader::SkColorShader(SkFlattenableReadBuffer& b) : INHERITED(b) {
reed@android.com5119bdb2009-06-12 21:27:03 +0000243 fFlags = 0; // computed in setContext
reed@android.comf2b98d62010-12-20 18:26:13 +0000244
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000245 fInheritColor = b.readBool();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 if (fInheritColor) {
247 return;
248 }
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000249 fColor = b.readColor();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250}
251
djsollen@google.com54924242012-03-29 15:18:04 +0000252void SkColorShader::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253 this->INHERITED::flatten(buffer);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000254 buffer.writeBool(fInheritColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255 if (fInheritColor) {
256 return;
257 }
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000258 buffer.writeColor(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259}
260
reed@google.com59ccef62011-12-07 14:59:50 +0000261uint32_t SkColorShader::getFlags() {
262 return fFlags;
263}
264
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265uint8_t SkColorShader::getSpan16Alpha() const {
266 return SkGetPackedA32(fPMColor);
267}
268
269bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint,
270 const SkMatrix& matrix) {
271 if (!this->INHERITED::setContext(device, paint, matrix)) {
272 return false;
273 }
274
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 unsigned a;
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000276
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277 if (fInheritColor) {
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000278 fColor = paint.getColor();
279 a = SkColorGetA(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280 } else {
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000281 a = SkAlphaMul(SkColorGetA(fColor), SkAlpha255To256(paint.getAlpha()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282 }
283
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000284 unsigned r = SkColorGetR(fColor);
285 unsigned g = SkColorGetG(fColor);
286 unsigned b = SkColorGetB(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287
288 // we want this before we apply any alpha
289 fColor16 = SkPack888ToRGB16(r, g, b);
290
291 if (a != 255) {
reed@android.com8f073382010-03-11 21:56:16 +0000292 r = SkMulDiv255Round(r, a);
293 g = SkMulDiv255Round(g, a);
294 b = SkMulDiv255Round(b, a);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295 }
296 fPMColor = SkPackARGB32(a, r, g, b);
297
reed@android.com8f073382010-03-11 21:56:16 +0000298 fFlags = kConstInY32_Flag;
reed@android.com5b815352010-03-11 22:20:43 +0000299 if (255 == a) {
reed@android.com5119bdb2009-06-12 21:27:03 +0000300 fFlags |= kOpaqueAlpha_Flag;
reed@android.com5b815352010-03-11 22:20:43 +0000301 if (paint.isDither() == false) {
302 fFlags |= kHasSpan16_Flag;
303 }
reed@android.com5119bdb2009-06-12 21:27:03 +0000304 }
305
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306 return true;
307}
308
309void SkColorShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
310 sk_memset32(span, fPMColor, count);
311}
312
313void SkColorShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
314 sk_memset16(span, fColor16, count);
315}
316
317void SkColorShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
318 memset(alpha, SkGetPackedA32(fPMColor), count);
319}
320
reed@android.comf2b98d62010-12-20 18:26:13 +0000321// if we had a asAColor method, that would be more efficient...
322SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000323 TileMode modes[]) const {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000324 return kNone_BitmapType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000325}
326
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000327SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const {
328 if (info) {
329 if (info->fColors && info->fColorCount >= 1) {
330 info->fColors[0] = fColor;
331 }
332 info->fColorCount = 1;
333 info->fTileMode = SkShader::kRepeat_TileMode;
334 }
335 return kColor_GradientType;
336}
reed@google.com37a20122011-07-05 18:54:12 +0000337
338///////////////////////////////////////////////////////////////////////////////
339
340#include "SkEmptyShader.h"
341
reed@google.com37a20122011-07-05 18:54:12 +0000342uint32_t SkEmptyShader::getFlags() { return 0; }
343uint8_t SkEmptyShader::getSpan16Alpha() const { return 0; }
reed@google.com59ccef62011-12-07 14:59:50 +0000344
345bool SkEmptyShader::setContext(const SkBitmap&, const SkPaint&,
346 const SkMatrix&) { return false; }
347
348void SkEmptyShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000349 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com37a20122011-07-05 18:54:12 +0000350}
reed@google.com59ccef62011-12-07 14:59:50 +0000351
352void SkEmptyShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000353 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com59ccef62011-12-07 14:59:50 +0000354}
355
356void SkEmptyShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000357 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com59ccef62011-12-07 14:59:50 +0000358}