blob: cd40b7f2c8728aed2dfb7faad5b229723984f54a [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"
12#include "SkPaint.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000013#include "SkMallocPixelRef.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000015SK_DEFINE_INST_COUNT(SkShader)
16
reed@android.com8a1c16f2008-12-17 15:59:43 +000017SkShader::SkShader() : fLocalMatrix(NULL) {
18 SkDEBUGCODE(fInSession = false;)
19}
20
21SkShader::SkShader(SkFlattenableReadBuffer& buffer)
22 : INHERITED(buffer), fLocalMatrix(NULL) {
23 if (buffer.readBool()) {
24 SkMatrix matrix;
djsollen@google.com2b2ede32012-04-12 13:24:04 +000025 buffer.readMatrix(&matrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000026 setLocalMatrix(matrix);
27 }
28 SkDEBUGCODE(fInSession = false;)
29}
30
31SkShader::~SkShader() {
32 SkASSERT(!fInSession);
33 sk_free(fLocalMatrix);
34}
35
36void SkShader::beginSession() {
37 SkASSERT(!fInSession);
38 SkDEBUGCODE(fInSession = true;)
39}
40
41void SkShader::endSession() {
42 SkASSERT(fInSession);
43 SkDEBUGCODE(fInSession = false;)
44}
45
djsollen@google.com54924242012-03-29 15:18:04 +000046void SkShader::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 this->INHERITED::flatten(buffer);
48 buffer.writeBool(fLocalMatrix != NULL);
49 if (fLocalMatrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +000050 buffer.writeMatrix(*fLocalMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 }
52}
53
54bool SkShader::getLocalMatrix(SkMatrix* localM) const {
55 if (fLocalMatrix) {
56 if (localM) {
57 *localM = *fLocalMatrix;
58 }
59 return true;
60 } else {
61 if (localM) {
62 localM->reset();
63 }
64 return false;
65 }
66}
67
68void SkShader::setLocalMatrix(const SkMatrix& localM) {
69 if (localM.isIdentity()) {
70 this->resetLocalMatrix();
71 } else {
72 if (fLocalMatrix == NULL) {
73 fLocalMatrix = (SkMatrix*)sk_malloc_throw(sizeof(SkMatrix));
74 }
75 *fLocalMatrix = localM;
76 }
77}
78
79void SkShader::resetLocalMatrix() {
80 if (fLocalMatrix) {
81 sk_free(fLocalMatrix);
82 fLocalMatrix = NULL;
83 }
84}
85
86bool SkShader::setContext(const SkBitmap& device,
87 const SkPaint& paint,
88 const SkMatrix& matrix) {
89 const SkMatrix* m = &matrix;
90 SkMatrix total;
91
92 fDeviceConfig = SkToU8(device.getConfig());
93 fPaintAlpha = paint.getAlpha();
94 if (fLocalMatrix) {
95 total.setConcat(matrix, *fLocalMatrix);
96 m = &total;
97 }
98 if (m->invert(&fTotalInverse)) {
99 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
100 return true;
101 }
102 return false;
103}
104
105#include "SkColorPriv.h"
106
107void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) {
108 SkASSERT(span16);
109 SkASSERT(count > 0);
110 SkASSERT(this->canCallShadeSpan16());
111
112 // basically, if we get here, the subclass screwed up
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000113 SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114}
115
116#define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space
reed@google.com7c2f27d2011-03-07 19:29:00 +0000117#define kTempColorCount (kTempColorQuadCount << 2)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118
119#ifdef SK_CPU_BENDIAN
120 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
121#else
122 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
123#endif
124
125void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
126 SkASSERT(count > 0);
127
128 SkPMColor colors[kTempColorCount];
129
130 while ((count -= kTempColorCount) >= 0) {
131 this->shadeSpan(x, y, colors, kTempColorCount);
132 x += kTempColorCount;
133
134 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
135 int quads = kTempColorQuadCount;
136 do {
137 U8CPU a0 = srcA[0];
138 U8CPU a1 = srcA[4];
139 U8CPU a2 = srcA[8];
140 U8CPU a3 = srcA[12];
141 srcA += 4*4;
142 *alpha++ = SkToU8(a0);
143 *alpha++ = SkToU8(a1);
144 *alpha++ = SkToU8(a2);
145 *alpha++ = SkToU8(a3);
146 } while (--quads != 0);
147 }
148 SkASSERT(count < 0);
149 SkASSERT(count + kTempColorCount >= 0);
150 if (count += kTempColorCount) {
151 this->shadeSpan(x, y, colors, count);
152
153 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
154 do {
155 *alpha++ = *srcA;
156 srcA += 4;
157 } while (--count != 0);
158 }
159#if 0
160 do {
161 int n = count;
162 if (n > kTempColorCount)
163 n = kTempColorCount;
164 SkASSERT(n > 0);
165
166 this->shadeSpan(x, y, colors, n);
167 x += n;
168 count -= n;
169
170 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
171 do {
172 *alpha++ = *srcA;
173 srcA += 4;
174 } while (--n != 0);
175 } while (count > 0);
176#endif
177}
178
179SkShader::MatrixClass SkShader::ComputeMatrixClass(const SkMatrix& mat) {
180 MatrixClass mc = kLinear_MatrixClass;
181
tomhudson@google.com8d430182011-06-06 19:11:19 +0000182 if (mat.hasPerspective()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 if (mat.fixedStepInX(0, NULL, NULL)) {
184 mc = kFixedStepInX_MatrixClass;
185 } else {
186 mc = kPerspective_MatrixClass;
187 }
188 }
189 return mc;
190}
191
192//////////////////////////////////////////////////////////////////////////////
193
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000194SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*,
reed@google.com7c2f27d2011-03-07 19:29:00 +0000195 TileMode*, SkScalar*) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000196 return kNone_BitmapType;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197}
198
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000199SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
200 return kNone_GradientType;
201}
202
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203SkShader* SkShader::CreateBitmapShader(const SkBitmap& src,
204 TileMode tmx, TileMode tmy) {
205 return SkShader::CreateBitmapShader(src, tmx, tmy, NULL, 0);
206}
207
208//////////////////////////////////////////////////////////////////////////////
209
210#include "SkColorShader.h"
211#include "SkUtils.h"
212
reed@android.comf2b98d62010-12-20 18:26:13 +0000213SkColorShader::SkColorShader() {
214 fFlags = 0;
215 fInheritColor = true;
reed@android.comf2b98d62010-12-20 18:26:13 +0000216}
217
218SkColorShader::SkColorShader(SkColor c) {
219 fFlags = 0;
220 fColor = c;
221 fInheritColor = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000222}
223
reed@google.com2be9e8b2011-07-06 21:18:09 +0000224SkColorShader::~SkColorShader() {}
reed@android.comf2b98d62010-12-20 18:26:13 +0000225
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000226bool SkColorShader::isOpaque() const {
227 if (fInheritColor) {
228 return true; // using paint's alpha
229 }
230 return SkColorGetA(fColor) == 255;
231}
232
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233SkColorShader::SkColorShader(SkFlattenableReadBuffer& b) : INHERITED(b) {
reed@android.com5119bdb2009-06-12 21:27:03 +0000234 fFlags = 0; // computed in setContext
reed@android.comf2b98d62010-12-20 18:26:13 +0000235
236 fInheritColor = b.readU8();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 if (fInheritColor) {
238 return;
239 }
240 fColor = b.readU32();
241}
242
djsollen@google.com54924242012-03-29 15:18:04 +0000243void SkColorShader::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000244 this->INHERITED::flatten(buffer);
245 buffer.write8(fInheritColor);
246 if (fInheritColor) {
247 return;
248 }
249 buffer.write32(fColor);
250}
251
reed@google.com59ccef62011-12-07 14:59:50 +0000252uint32_t SkColorShader::getFlags() {
253 return fFlags;
254}
255
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256uint8_t SkColorShader::getSpan16Alpha() const {
257 return SkGetPackedA32(fPMColor);
258}
259
260bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint,
261 const SkMatrix& matrix) {
262 if (!this->INHERITED::setContext(device, paint, matrix)) {
263 return false;
264 }
265
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266 unsigned a;
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000267
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268 if (fInheritColor) {
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000269 fColor = paint.getColor();
270 a = SkColorGetA(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271 } else {
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000272 a = SkAlphaMul(SkColorGetA(fColor), SkAlpha255To256(paint.getAlpha()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 }
274
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000275 unsigned r = SkColorGetR(fColor);
276 unsigned g = SkColorGetG(fColor);
277 unsigned b = SkColorGetB(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278
279 // we want this before we apply any alpha
280 fColor16 = SkPack888ToRGB16(r, g, b);
281
282 if (a != 255) {
reed@android.com8f073382010-03-11 21:56:16 +0000283 r = SkMulDiv255Round(r, a);
284 g = SkMulDiv255Round(g, a);
285 b = SkMulDiv255Round(b, a);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 }
287 fPMColor = SkPackARGB32(a, r, g, b);
288
reed@android.com8f073382010-03-11 21:56:16 +0000289 fFlags = kConstInY32_Flag;
reed@android.com5b815352010-03-11 22:20:43 +0000290 if (255 == a) {
reed@android.com5119bdb2009-06-12 21:27:03 +0000291 fFlags |= kOpaqueAlpha_Flag;
reed@android.com5b815352010-03-11 22:20:43 +0000292 if (paint.isDither() == false) {
293 fFlags |= kHasSpan16_Flag;
294 }
reed@android.com5119bdb2009-06-12 21:27:03 +0000295 }
296
reed@android.com8a1c16f2008-12-17 15:59:43 +0000297 return true;
298}
299
300void SkColorShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
301 sk_memset32(span, fPMColor, count);
302}
303
304void SkColorShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
305 sk_memset16(span, fColor16, count);
306}
307
308void SkColorShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
309 memset(alpha, SkGetPackedA32(fPMColor), count);
310}
311
reed@android.comf2b98d62010-12-20 18:26:13 +0000312// if we had a asAColor method, that would be more efficient...
313SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
reed@google.com7c2f27d2011-03-07 19:29:00 +0000314 TileMode modes[],
reed@google.com8cad5862011-03-08 14:51:44 +0000315 SkScalar* twoPointRadialParams) const {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000316 return kNone_BitmapType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000317}
318
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000319SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const {
320 if (info) {
321 if (info->fColors && info->fColorCount >= 1) {
322 info->fColors[0] = fColor;
323 }
324 info->fColorCount = 1;
325 info->fTileMode = SkShader::kRepeat_TileMode;
326 }
327 return kColor_GradientType;
328}
reed@google.com37a20122011-07-05 18:54:12 +0000329
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000330SK_DEFINE_FLATTENABLE_REGISTRAR(SkColorShader)
331
reed@google.com37a20122011-07-05 18:54:12 +0000332///////////////////////////////////////////////////////////////////////////////
333
334#include "SkEmptyShader.h"
335
reed@google.com37a20122011-07-05 18:54:12 +0000336uint32_t SkEmptyShader::getFlags() { return 0; }
337uint8_t SkEmptyShader::getSpan16Alpha() const { return 0; }
reed@google.com59ccef62011-12-07 14:59:50 +0000338
339bool SkEmptyShader::setContext(const SkBitmap&, const SkPaint&,
340 const SkMatrix&) { return false; }
341
342void SkEmptyShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000343 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com37a20122011-07-05 18:54:12 +0000344}
reed@google.com59ccef62011-12-07 14:59:50 +0000345
346void SkEmptyShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000347 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com59ccef62011-12-07 14:59:50 +0000348}
349
350void SkEmptyShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000351 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com59ccef62011-12-07 14:59:50 +0000352}
reed@google.com37a20122011-07-05 18:54:12 +0000353
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000354SK_DEFINE_FLATTENABLE_REGISTRAR(SkEmptyShader)