blob: 92a5d3f92b3d840c27c1562b770d7e10f9ed23a5 [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
106#include "SkColorPriv.h"
107
108void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) {
109 SkASSERT(span16);
110 SkASSERT(count > 0);
111 SkASSERT(this->canCallShadeSpan16());
112
113 // basically, if we get here, the subclass screwed up
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000114 SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115}
116
117#define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space
reed@google.com7c2f27d2011-03-07 19:29:00 +0000118#define kTempColorCount (kTempColorQuadCount << 2)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119
120#ifdef SK_CPU_BENDIAN
121 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
122#else
123 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
124#endif
125
126void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
127 SkASSERT(count > 0);
128
129 SkPMColor colors[kTempColorCount];
130
131 while ((count -= kTempColorCount) >= 0) {
132 this->shadeSpan(x, y, colors, kTempColorCount);
133 x += kTempColorCount;
134
135 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
136 int quads = kTempColorQuadCount;
137 do {
138 U8CPU a0 = srcA[0];
139 U8CPU a1 = srcA[4];
140 U8CPU a2 = srcA[8];
141 U8CPU a3 = srcA[12];
142 srcA += 4*4;
143 *alpha++ = SkToU8(a0);
144 *alpha++ = SkToU8(a1);
145 *alpha++ = SkToU8(a2);
146 *alpha++ = SkToU8(a3);
147 } while (--quads != 0);
148 }
149 SkASSERT(count < 0);
150 SkASSERT(count + kTempColorCount >= 0);
151 if (count += kTempColorCount) {
152 this->shadeSpan(x, y, colors, count);
153
154 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
155 do {
156 *alpha++ = *srcA;
157 srcA += 4;
158 } while (--count != 0);
159 }
160#if 0
161 do {
162 int n = count;
163 if (n > kTempColorCount)
164 n = kTempColorCount;
165 SkASSERT(n > 0);
166
167 this->shadeSpan(x, y, colors, n);
168 x += n;
169 count -= n;
170
171 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
172 do {
173 *alpha++ = *srcA;
174 srcA += 4;
175 } while (--n != 0);
176 } while (count > 0);
177#endif
178}
179
180SkShader::MatrixClass SkShader::ComputeMatrixClass(const SkMatrix& mat) {
181 MatrixClass mc = kLinear_MatrixClass;
182
tomhudson@google.com8d430182011-06-06 19:11:19 +0000183 if (mat.hasPerspective()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 if (mat.fixedStepInX(0, NULL, NULL)) {
185 mc = kFixedStepInX_MatrixClass;
186 } else {
187 mc = kPerspective_MatrixClass;
188 }
189 }
190 return mc;
191}
192
193//////////////////////////////////////////////////////////////////////////////
194
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000195SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*,
rileya@google.com91f319c2012-07-25 17:18:31 +0000196 TileMode*) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000197 return kNone_BitmapType;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198}
199
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000200SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
201 return kNone_GradientType;
202}
203
rileya@google.com03c1c352012-07-20 20:02:43 +0000204GrCustomStage* SkShader::asNewCustomStage(GrContext* context,
205 GrSamplerState* sampler) const {
206 return NULL;
207}
208
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209SkShader* SkShader::CreateBitmapShader(const SkBitmap& src,
210 TileMode tmx, TileMode tmy) {
211 return SkShader::CreateBitmapShader(src, tmx, tmy, NULL, 0);
212}
213
214//////////////////////////////////////////////////////////////////////////////
215
216#include "SkColorShader.h"
217#include "SkUtils.h"
218
reed@android.comf2b98d62010-12-20 18:26:13 +0000219SkColorShader::SkColorShader() {
220 fFlags = 0;
221 fInheritColor = true;
reed@android.comf2b98d62010-12-20 18:26:13 +0000222}
223
224SkColorShader::SkColorShader(SkColor c) {
225 fFlags = 0;
226 fColor = c;
227 fInheritColor = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000228}
229
reed@google.com2be9e8b2011-07-06 21:18:09 +0000230SkColorShader::~SkColorShader() {}
reed@android.comf2b98d62010-12-20 18:26:13 +0000231
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000232bool SkColorShader::isOpaque() const {
233 if (fInheritColor) {
234 return true; // using paint's alpha
235 }
236 return SkColorGetA(fColor) == 255;
237}
238
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239SkColorShader::SkColorShader(SkFlattenableReadBuffer& b) : INHERITED(b) {
reed@android.com5119bdb2009-06-12 21:27:03 +0000240 fFlags = 0; // computed in setContext
reed@android.comf2b98d62010-12-20 18:26:13 +0000241
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000242 fInheritColor = b.readBool();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 if (fInheritColor) {
244 return;
245 }
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000246 fColor = b.readColor();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247}
248
djsollen@google.com54924242012-03-29 15:18:04 +0000249void SkColorShader::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250 this->INHERITED::flatten(buffer);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000251 buffer.writeBool(fInheritColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252 if (fInheritColor) {
253 return;
254 }
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000255 buffer.writeColor(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256}
257
reed@google.com59ccef62011-12-07 14:59:50 +0000258uint32_t SkColorShader::getFlags() {
259 return fFlags;
260}
261
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262uint8_t SkColorShader::getSpan16Alpha() const {
263 return SkGetPackedA32(fPMColor);
264}
265
266bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint,
267 const SkMatrix& matrix) {
268 if (!this->INHERITED::setContext(device, paint, matrix)) {
269 return false;
270 }
271
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272 unsigned a;
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000273
reed@android.com8a1c16f2008-12-17 15:59:43 +0000274 if (fInheritColor) {
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000275 fColor = paint.getColor();
276 a = SkColorGetA(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277 } else {
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000278 a = SkAlphaMul(SkColorGetA(fColor), SkAlpha255To256(paint.getAlpha()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 }
280
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000281 unsigned r = SkColorGetR(fColor);
282 unsigned g = SkColorGetG(fColor);
283 unsigned b = SkColorGetB(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284
285 // we want this before we apply any alpha
286 fColor16 = SkPack888ToRGB16(r, g, b);
287
288 if (a != 255) {
reed@android.com8f073382010-03-11 21:56:16 +0000289 r = SkMulDiv255Round(r, a);
290 g = SkMulDiv255Round(g, a);
291 b = SkMulDiv255Round(b, a);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292 }
293 fPMColor = SkPackARGB32(a, r, g, b);
294
reed@android.com8f073382010-03-11 21:56:16 +0000295 fFlags = kConstInY32_Flag;
reed@android.com5b815352010-03-11 22:20:43 +0000296 if (255 == a) {
reed@android.com5119bdb2009-06-12 21:27:03 +0000297 fFlags |= kOpaqueAlpha_Flag;
reed@android.com5b815352010-03-11 22:20:43 +0000298 if (paint.isDither() == false) {
299 fFlags |= kHasSpan16_Flag;
300 }
reed@android.com5119bdb2009-06-12 21:27:03 +0000301 }
302
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303 return true;
304}
305
306void SkColorShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
307 sk_memset32(span, fPMColor, count);
308}
309
310void SkColorShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
311 sk_memset16(span, fColor16, count);
312}
313
314void SkColorShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
315 memset(alpha, SkGetPackedA32(fPMColor), count);
316}
317
reed@android.comf2b98d62010-12-20 18:26:13 +0000318// if we had a asAColor method, that would be more efficient...
319SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000320 TileMode modes[]) const {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000321 return kNone_BitmapType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000322}
323
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000324SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const {
325 if (info) {
326 if (info->fColors && info->fColorCount >= 1) {
327 info->fColors[0] = fColor;
328 }
329 info->fColorCount = 1;
330 info->fTileMode = SkShader::kRepeat_TileMode;
331 }
332 return kColor_GradientType;
333}
reed@google.com37a20122011-07-05 18:54:12 +0000334
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000335SK_DEFINE_FLATTENABLE_REGISTRAR(SkColorShader)
336
reed@google.com37a20122011-07-05 18:54:12 +0000337///////////////////////////////////////////////////////////////////////////////
338
339#include "SkEmptyShader.h"
340
reed@google.com37a20122011-07-05 18:54:12 +0000341uint32_t SkEmptyShader::getFlags() { return 0; }
342uint8_t SkEmptyShader::getSpan16Alpha() const { return 0; }
reed@google.com59ccef62011-12-07 14:59:50 +0000343
344bool SkEmptyShader::setContext(const SkBitmap&, const SkPaint&,
345 const SkMatrix&) { return false; }
346
347void SkEmptyShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000348 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com37a20122011-07-05 18:54:12 +0000349}
reed@google.com59ccef62011-12-07 14:59:50 +0000350
351void SkEmptyShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000352 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com59ccef62011-12-07 14:59:50 +0000353}
354
355void SkEmptyShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000356 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com59ccef62011-12-07 14:59:50 +0000357}
reed@google.com37a20122011-07-05 18:54:12 +0000358
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000359SK_DEFINE_FLATTENABLE_REGISTRAR(SkEmptyShader)