blob: b832f9519aed7f97929aef8b262b8d2260fa7378 [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
15SkShader::SkShader() : fLocalMatrix(NULL) {
16 SkDEBUGCODE(fInSession = false;)
17}
18
19SkShader::SkShader(SkFlattenableReadBuffer& buffer)
20 : INHERITED(buffer), fLocalMatrix(NULL) {
21 if (buffer.readBool()) {
22 SkMatrix matrix;
reed@google.comf2eb5ab2011-05-10 22:56:42 +000023 SkReadMatrix(&buffer, &matrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 setLocalMatrix(matrix);
25 }
26 SkDEBUGCODE(fInSession = false;)
27}
28
29SkShader::~SkShader() {
30 SkASSERT(!fInSession);
31 sk_free(fLocalMatrix);
32}
33
34void SkShader::beginSession() {
35 SkASSERT(!fInSession);
36 SkDEBUGCODE(fInSession = true;)
37}
38
39void SkShader::endSession() {
40 SkASSERT(fInSession);
41 SkDEBUGCODE(fInSession = false;)
42}
43
44void SkShader::flatten(SkFlattenableWriteBuffer& buffer) {
45 this->INHERITED::flatten(buffer);
46 buffer.writeBool(fLocalMatrix != NULL);
47 if (fLocalMatrix) {
reed@google.comf2eb5ab2011-05-10 22:56:42 +000048 SkWriteMatrix(&buffer, *fLocalMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 }
50}
51
52bool SkShader::getLocalMatrix(SkMatrix* localM) const {
53 if (fLocalMatrix) {
54 if (localM) {
55 *localM = *fLocalMatrix;
56 }
57 return true;
58 } else {
59 if (localM) {
60 localM->reset();
61 }
62 return false;
63 }
64}
65
66void SkShader::setLocalMatrix(const SkMatrix& localM) {
67 if (localM.isIdentity()) {
68 this->resetLocalMatrix();
69 } else {
70 if (fLocalMatrix == NULL) {
71 fLocalMatrix = (SkMatrix*)sk_malloc_throw(sizeof(SkMatrix));
72 }
73 *fLocalMatrix = localM;
74 }
75}
76
77void SkShader::resetLocalMatrix() {
78 if (fLocalMatrix) {
79 sk_free(fLocalMatrix);
80 fLocalMatrix = NULL;
81 }
82}
83
84bool SkShader::setContext(const SkBitmap& device,
85 const SkPaint& paint,
86 const SkMatrix& matrix) {
87 const SkMatrix* m = &matrix;
88 SkMatrix total;
89
90 fDeviceConfig = SkToU8(device.getConfig());
91 fPaintAlpha = paint.getAlpha();
92 if (fLocalMatrix) {
93 total.setConcat(matrix, *fLocalMatrix);
94 m = &total;
95 }
96 if (m->invert(&fTotalInverse)) {
97 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
98 return true;
99 }
100 return false;
101}
102
103#include "SkColorPriv.h"
104
105void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) {
106 SkASSERT(span16);
107 SkASSERT(count > 0);
108 SkASSERT(this->canCallShadeSpan16());
109
110 // basically, if we get here, the subclass screwed up
111 SkASSERT(!"kHasSpan16 flag is set, but shadeSpan16() not implemented");
112}
113
114#define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space
reed@google.com7c2f27d2011-03-07 19:29:00 +0000115#define kTempColorCount (kTempColorQuadCount << 2)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116
117#ifdef SK_CPU_BENDIAN
118 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
119#else
120 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
121#endif
122
123void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
124 SkASSERT(count > 0);
125
126 SkPMColor colors[kTempColorCount];
127
128 while ((count -= kTempColorCount) >= 0) {
129 this->shadeSpan(x, y, colors, kTempColorCount);
130 x += kTempColorCount;
131
132 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
133 int quads = kTempColorQuadCount;
134 do {
135 U8CPU a0 = srcA[0];
136 U8CPU a1 = srcA[4];
137 U8CPU a2 = srcA[8];
138 U8CPU a3 = srcA[12];
139 srcA += 4*4;
140 *alpha++ = SkToU8(a0);
141 *alpha++ = SkToU8(a1);
142 *alpha++ = SkToU8(a2);
143 *alpha++ = SkToU8(a3);
144 } while (--quads != 0);
145 }
146 SkASSERT(count < 0);
147 SkASSERT(count + kTempColorCount >= 0);
148 if (count += kTempColorCount) {
149 this->shadeSpan(x, y, colors, count);
150
151 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
152 do {
153 *alpha++ = *srcA;
154 srcA += 4;
155 } while (--count != 0);
156 }
157#if 0
158 do {
159 int n = count;
160 if (n > kTempColorCount)
161 n = kTempColorCount;
162 SkASSERT(n > 0);
163
164 this->shadeSpan(x, y, colors, n);
165 x += n;
166 count -= n;
167
168 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
169 do {
170 *alpha++ = *srcA;
171 srcA += 4;
172 } while (--n != 0);
173 } while (count > 0);
174#endif
175}
176
177SkShader::MatrixClass SkShader::ComputeMatrixClass(const SkMatrix& mat) {
178 MatrixClass mc = kLinear_MatrixClass;
179
tomhudson@google.com8d430182011-06-06 19:11:19 +0000180 if (mat.hasPerspective()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 if (mat.fixedStepInX(0, NULL, NULL)) {
182 mc = kFixedStepInX_MatrixClass;
183 } else {
184 mc = kPerspective_MatrixClass;
185 }
186 }
187 return mc;
188}
189
190//////////////////////////////////////////////////////////////////////////////
191
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000192SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*,
reed@google.com7c2f27d2011-03-07 19:29:00 +0000193 TileMode*, SkScalar*) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000194 return kNone_BitmapType;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195}
196
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000197SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
198 return kNone_GradientType;
199}
200
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201SkShader* SkShader::CreateBitmapShader(const SkBitmap& src,
202 TileMode tmx, TileMode tmy) {
203 return SkShader::CreateBitmapShader(src, tmx, tmy, NULL, 0);
204}
205
206//////////////////////////////////////////////////////////////////////////////
207
208#include "SkColorShader.h"
209#include "SkUtils.h"
210
reed@android.comf2b98d62010-12-20 18:26:13 +0000211SkColorShader::SkColorShader() {
212 fFlags = 0;
213 fInheritColor = true;
reed@android.comf2b98d62010-12-20 18:26:13 +0000214}
215
216SkColorShader::SkColorShader(SkColor c) {
217 fFlags = 0;
218 fColor = c;
219 fInheritColor = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000220}
221
reed@google.com2be9e8b2011-07-06 21:18:09 +0000222SkColorShader::~SkColorShader() {}
reed@android.comf2b98d62010-12-20 18:26:13 +0000223
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224SkColorShader::SkColorShader(SkFlattenableReadBuffer& b) : INHERITED(b) {
reed@android.com5119bdb2009-06-12 21:27:03 +0000225 fFlags = 0; // computed in setContext
reed@android.comf2b98d62010-12-20 18:26:13 +0000226
227 fInheritColor = b.readU8();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228 if (fInheritColor) {
229 return;
230 }
231 fColor = b.readU32();
232}
233
234void SkColorShader::flatten(SkFlattenableWriteBuffer& buffer) {
235 this->INHERITED::flatten(buffer);
236 buffer.write8(fInheritColor);
237 if (fInheritColor) {
238 return;
239 }
240 buffer.write32(fColor);
241}
242
reed@google.com59ccef62011-12-07 14:59:50 +0000243SkFlattenable* SkColorShader::CreateProc(SkFlattenableReadBuffer& buffer) {
244 return SkNEW_ARGS(SkColorShader, (buffer));
245}
246
247SkFlattenable::Factory SkColorShader::getFactory() {
248 return CreateProc;
249}
250
251uint32_t SkColorShader::getFlags() {
252 return fFlags;
253}
254
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255uint8_t SkColorShader::getSpan16Alpha() const {
256 return SkGetPackedA32(fPMColor);
257}
258
259bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint,
260 const SkMatrix& matrix) {
261 if (!this->INHERITED::setContext(device, paint, matrix)) {
262 return false;
263 }
264
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 unsigned a;
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000266
reed@android.com8a1c16f2008-12-17 15:59:43 +0000267 if (fInheritColor) {
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000268 fColor = paint.getColor();
269 a = SkColorGetA(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270 } else {
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000271 a = SkAlphaMul(SkColorGetA(fColor), SkAlpha255To256(paint.getAlpha()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272 }
273
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000274 unsigned r = SkColorGetR(fColor);
275 unsigned g = SkColorGetG(fColor);
276 unsigned b = SkColorGetB(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277
278 // we want this before we apply any alpha
279 fColor16 = SkPack888ToRGB16(r, g, b);
280
281 if (a != 255) {
reed@android.com8f073382010-03-11 21:56:16 +0000282 r = SkMulDiv255Round(r, a);
283 g = SkMulDiv255Round(g, a);
284 b = SkMulDiv255Round(b, a);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 }
286 fPMColor = SkPackARGB32(a, r, g, b);
287
reed@android.com8f073382010-03-11 21:56:16 +0000288 fFlags = kConstInY32_Flag;
reed@android.com5b815352010-03-11 22:20:43 +0000289 if (255 == a) {
reed@android.com5119bdb2009-06-12 21:27:03 +0000290 fFlags |= kOpaqueAlpha_Flag;
reed@android.com5b815352010-03-11 22:20:43 +0000291 if (paint.isDither() == false) {
292 fFlags |= kHasSpan16_Flag;
293 }
reed@android.com5119bdb2009-06-12 21:27:03 +0000294 }
295
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296 return true;
297}
298
299void SkColorShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
300 sk_memset32(span, fPMColor, count);
301}
302
303void SkColorShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
304 sk_memset16(span, fColor16, count);
305}
306
307void SkColorShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
308 memset(alpha, SkGetPackedA32(fPMColor), count);
309}
310
reed@android.comf2b98d62010-12-20 18:26:13 +0000311// if we had a asAColor method, that would be more efficient...
312SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
reed@google.com7c2f27d2011-03-07 19:29:00 +0000313 TileMode modes[],
reed@google.com8cad5862011-03-08 14:51:44 +0000314 SkScalar* twoPointRadialParams) const {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000315 return kNone_BitmapType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000316}
317
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000318SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const {
319 if (info) {
320 if (info->fColors && info->fColorCount >= 1) {
321 info->fColors[0] = fColor;
322 }
323 info->fColorCount = 1;
324 info->fTileMode = SkShader::kRepeat_TileMode;
325 }
326 return kColor_GradientType;
327}
reed@google.com37a20122011-07-05 18:54:12 +0000328
329///////////////////////////////////////////////////////////////////////////////
330
331#include "SkEmptyShader.h"
332
reed@google.com37a20122011-07-05 18:54:12 +0000333SkEmptyShader::SkEmptyShader(SkFlattenableReadBuffer& b) : INHERITED(b) {}
334
335uint32_t SkEmptyShader::getFlags() { return 0; }
336uint8_t SkEmptyShader::getSpan16Alpha() const { return 0; }
reed@google.com59ccef62011-12-07 14:59:50 +0000337
338bool SkEmptyShader::setContext(const SkBitmap&, const SkPaint&,
339 const SkMatrix&) { return false; }
340
341void SkEmptyShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
342 SkASSERT(!"should never get called, since setContext() returned false");
reed@google.com37a20122011-07-05 18:54:12 +0000343}
reed@google.com59ccef62011-12-07 14:59:50 +0000344
345void SkEmptyShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
346 SkASSERT(!"should never get called, since setContext() returned false");
347}
348
349void SkEmptyShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
350 SkASSERT(!"should never get called, since setContext() returned false");
351}
reed@google.com37a20122011-07-05 18:54:12 +0000352
353SkFlattenable::Factory SkEmptyShader::getFactory() { return NULL; }
reed@google.com59ccef62011-12-07 14:59:50 +0000354
reed@google.com37a20122011-07-05 18:54:12 +0000355void SkEmptyShader::flatten(SkFlattenableWriteBuffer& buffer) {
356 this->INHERITED::flatten(buffer);
357}
358