blob: 706c1b79f5d6b6f8022e5f1c006e57954bd00351 [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
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000018SkShader::SkShader() {
19 fLocalMatrix.reset();
reed@google.coma641f3f2012-12-13 22:16:30 +000020 SkDEBUGCODE(fInSetContext = false;)
reed@android.com8a1c16f2008-12-17 15:59:43 +000021}
22
23SkShader::SkShader(SkFlattenableReadBuffer& buffer)
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000024 : INHERITED(buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 if (buffer.readBool()) {
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000026 buffer.readMatrix(&fLocalMatrix);
27 } else {
28 fLocalMatrix.reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 }
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000030
reed@google.coma641f3f2012-12-13 22:16:30 +000031 SkDEBUGCODE(fInSetContext = false;)
reed@android.com8a1c16f2008-12-17 15:59:43 +000032}
33
34SkShader::~SkShader() {
reed@google.coma641f3f2012-12-13 22:16:30 +000035 SkASSERT(!fInSetContext);
reed@android.com8a1c16f2008-12-17 15:59:43 +000036}
37
djsollen@google.com54924242012-03-29 15:18:04 +000038void SkShader::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 this->INHERITED::flatten(buffer);
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000040 bool hasLocalM = this->hasLocalMatrix();
41 buffer.writeBool(hasLocalM);
42 if (hasLocalM) {
43 buffer.writeMatrix(fLocalMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 }
45}
46
47bool SkShader::setContext(const SkBitmap& device,
48 const SkPaint& paint,
49 const SkMatrix& matrix) {
reed@google.coma641f3f2012-12-13 22:16:30 +000050 SkASSERT(!this->setContextHasBeenCalled());
51
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 const SkMatrix* m = &matrix;
53 SkMatrix total;
54
55 fDeviceConfig = SkToU8(device.getConfig());
56 fPaintAlpha = paint.getAlpha();
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000057 if (this->hasLocalMatrix()) {
58 total.setConcat(matrix, this->getLocalMatrix());
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 m = &total;
60 }
61 if (m->invert(&fTotalInverse)) {
62 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
reed@google.coma641f3f2012-12-13 22:16:30 +000063 SkDEBUGCODE(fInSetContext = true;)
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 return true;
65 }
66 return false;
67}
68
reed@google.coma641f3f2012-12-13 22:16:30 +000069void SkShader::endContext() {
70 SkASSERT(fInSetContext);
71 SkDEBUGCODE(fInSetContext = false;)
72}
73
reed@google.com3bafe742012-10-12 18:56:18 +000074SkShader::ShadeProc SkShader::asAShadeProc(void** ctx) {
75 return NULL;
76}
77
reed@android.com8a1c16f2008-12-17 15:59:43 +000078#include "SkColorPriv.h"
79
80void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) {
81 SkASSERT(span16);
82 SkASSERT(count > 0);
83 SkASSERT(this->canCallShadeSpan16());
84
85 // basically, if we get here, the subclass screwed up
tomhudson@google.com0c00f212011-12-28 14:59:50 +000086 SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented");
reed@android.com8a1c16f2008-12-17 15:59:43 +000087}
88
89#define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space
reed@google.com7c2f27d2011-03-07 19:29:00 +000090#define kTempColorCount (kTempColorQuadCount << 2)
reed@android.com8a1c16f2008-12-17 15:59:43 +000091
92#ifdef SK_CPU_BENDIAN
93 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
94#else
95 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
96#endif
97
98void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
99 SkASSERT(count > 0);
100
101 SkPMColor colors[kTempColorCount];
102
103 while ((count -= kTempColorCount) >= 0) {
104 this->shadeSpan(x, y, colors, kTempColorCount);
105 x += kTempColorCount;
106
107 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
108 int quads = kTempColorQuadCount;
109 do {
110 U8CPU a0 = srcA[0];
111 U8CPU a1 = srcA[4];
112 U8CPU a2 = srcA[8];
113 U8CPU a3 = srcA[12];
114 srcA += 4*4;
115 *alpha++ = SkToU8(a0);
116 *alpha++ = SkToU8(a1);
117 *alpha++ = SkToU8(a2);
118 *alpha++ = SkToU8(a3);
119 } while (--quads != 0);
120 }
121 SkASSERT(count < 0);
122 SkASSERT(count + kTempColorCount >= 0);
123 if (count += kTempColorCount) {
124 this->shadeSpan(x, y, colors, count);
125
126 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
127 do {
128 *alpha++ = *srcA;
129 srcA += 4;
130 } while (--count != 0);
131 }
132#if 0
133 do {
134 int n = count;
135 if (n > kTempColorCount)
136 n = kTempColorCount;
137 SkASSERT(n > 0);
138
139 this->shadeSpan(x, y, colors, n);
140 x += n;
141 count -= n;
142
143 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
144 do {
145 *alpha++ = *srcA;
146 srcA += 4;
147 } while (--n != 0);
148 } while (count > 0);
149#endif
150}
151
152SkShader::MatrixClass SkShader::ComputeMatrixClass(const SkMatrix& mat) {
153 MatrixClass mc = kLinear_MatrixClass;
154
tomhudson@google.com8d430182011-06-06 19:11:19 +0000155 if (mat.hasPerspective()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 if (mat.fixedStepInX(0, NULL, NULL)) {
157 mc = kFixedStepInX_MatrixClass;
158 } else {
159 mc = kPerspective_MatrixClass;
160 }
161 }
162 return mc;
163}
164
165//////////////////////////////////////////////////////////////////////////////
166
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000167SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*,
rileya@google.com91f319c2012-07-25 17:18:31 +0000168 TileMode*) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000169 return kNone_BitmapType;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170}
171
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000172SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
173 return kNone_GradientType;
174}
175
bsalomon@google.com08283af2012-10-26 13:01:20 +0000176bool SkShader::asNewEffect(GrContext*, GrEffectStage*) const {
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000177 return false;
rileya@google.com03c1c352012-07-20 20:02:43 +0000178}
179
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180SkShader* SkShader::CreateBitmapShader(const SkBitmap& src,
181 TileMode tmx, TileMode tmy) {
182 return SkShader::CreateBitmapShader(src, tmx, tmy, NULL, 0);
183}
184
185//////////////////////////////////////////////////////////////////////////////
186
187#include "SkColorShader.h"
188#include "SkUtils.h"
189
reed@android.comf2b98d62010-12-20 18:26:13 +0000190SkColorShader::SkColorShader() {
191 fFlags = 0;
192 fInheritColor = true;
reed@android.comf2b98d62010-12-20 18:26:13 +0000193}
194
195SkColorShader::SkColorShader(SkColor c) {
196 fFlags = 0;
197 fColor = c;
198 fInheritColor = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000199}
200
reed@google.com2be9e8b2011-07-06 21:18:09 +0000201SkColorShader::~SkColorShader() {}
reed@android.comf2b98d62010-12-20 18:26:13 +0000202
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000203bool SkColorShader::isOpaque() const {
204 if (fInheritColor) {
205 return true; // using paint's alpha
206 }
207 return SkColorGetA(fColor) == 255;
208}
209
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210SkColorShader::SkColorShader(SkFlattenableReadBuffer& b) : INHERITED(b) {
reed@android.com5119bdb2009-06-12 21:27:03 +0000211 fFlags = 0; // computed in setContext
reed@android.comf2b98d62010-12-20 18:26:13 +0000212
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000213 fInheritColor = b.readBool();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214 if (fInheritColor) {
215 return;
216 }
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000217 fColor = b.readColor();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218}
219
djsollen@google.com54924242012-03-29 15:18:04 +0000220void SkColorShader::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221 this->INHERITED::flatten(buffer);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000222 buffer.writeBool(fInheritColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000223 if (fInheritColor) {
224 return;
225 }
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000226 buffer.writeColor(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227}
228
reed@google.com59ccef62011-12-07 14:59:50 +0000229uint32_t SkColorShader::getFlags() {
230 return fFlags;
231}
232
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233uint8_t SkColorShader::getSpan16Alpha() const {
234 return SkGetPackedA32(fPMColor);
235}
236
237bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint,
238 const SkMatrix& matrix) {
239 if (!this->INHERITED::setContext(device, paint, matrix)) {
240 return false;
241 }
242
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 unsigned a;
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000244
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245 if (fInheritColor) {
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000246 fColor = paint.getColor();
247 a = SkColorGetA(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248 } else {
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000249 a = SkAlphaMul(SkColorGetA(fColor), SkAlpha255To256(paint.getAlpha()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250 }
251
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000252 unsigned r = SkColorGetR(fColor);
253 unsigned g = SkColorGetG(fColor);
254 unsigned b = SkColorGetB(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255
256 // we want this before we apply any alpha
257 fColor16 = SkPack888ToRGB16(r, g, b);
258
259 if (a != 255) {
reed@android.com8f073382010-03-11 21:56:16 +0000260 r = SkMulDiv255Round(r, a);
261 g = SkMulDiv255Round(g, a);
262 b = SkMulDiv255Round(b, a);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 }
264 fPMColor = SkPackARGB32(a, r, g, b);
265
reed@android.com8f073382010-03-11 21:56:16 +0000266 fFlags = kConstInY32_Flag;
reed@android.com5b815352010-03-11 22:20:43 +0000267 if (255 == a) {
reed@android.com5119bdb2009-06-12 21:27:03 +0000268 fFlags |= kOpaqueAlpha_Flag;
reed@android.com5b815352010-03-11 22:20:43 +0000269 if (paint.isDither() == false) {
270 fFlags |= kHasSpan16_Flag;
271 }
reed@android.com5119bdb2009-06-12 21:27:03 +0000272 }
273
reed@android.com8a1c16f2008-12-17 15:59:43 +0000274 return true;
275}
276
277void SkColorShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
278 sk_memset32(span, fPMColor, count);
279}
280
281void SkColorShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
282 sk_memset16(span, fColor16, count);
283}
284
285void SkColorShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
286 memset(alpha, SkGetPackedA32(fPMColor), count);
287}
288
reed@android.comf2b98d62010-12-20 18:26:13 +0000289// if we had a asAColor method, that would be more efficient...
290SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000291 TileMode modes[]) const {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000292 return kNone_BitmapType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000293}
294
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000295SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const {
296 if (info) {
297 if (info->fColors && info->fColorCount >= 1) {
298 info->fColors[0] = fColor;
299 }
300 info->fColorCount = 1;
301 info->fTileMode = SkShader::kRepeat_TileMode;
302 }
303 return kColor_GradientType;
304}
reed@google.com37a20122011-07-05 18:54:12 +0000305
306///////////////////////////////////////////////////////////////////////////////
307
308#include "SkEmptyShader.h"
309
reed@google.com37a20122011-07-05 18:54:12 +0000310uint32_t SkEmptyShader::getFlags() { return 0; }
311uint8_t SkEmptyShader::getSpan16Alpha() const { return 0; }
reed@google.com59ccef62011-12-07 14:59:50 +0000312
313bool SkEmptyShader::setContext(const SkBitmap&, const SkPaint&,
314 const SkMatrix&) { return false; }
315
316void SkEmptyShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000317 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com37a20122011-07-05 18:54:12 +0000318}
reed@google.com59ccef62011-12-07 14:59:50 +0000319
320void SkEmptyShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000321 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com59ccef62011-12-07 14:59:50 +0000322}
323
324void SkEmptyShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000325 SkDEBUGFAIL("should never get called, since setContext() returned false");
reed@google.com59ccef62011-12-07 14:59:50 +0000326}