blob: 32984960ab27f7c0709587c8b51f6b27b270b425 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtklein1b249332015-07-07 12:21:21 -07008#include "SkAtomics.h"
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00009#include "SkBitmapProcShader.h"
reed8367b8c2014-08-22 08:30:20 -070010#include "SkColorShader.h"
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +000011#include "SkEmptyShader.h"
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000012#include "SkMallocPixelRef.h"
13#include "SkPaint.h"
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000014#include "SkPicture.h"
15#include "SkPictureShader.h"
mtklein1b249332015-07-07 12:21:21 -070016#include "SkReadBuffer.h"
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +000017#include "SkScalar.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkShader.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000019#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000021//#define SK_TRACK_SHADER_LIFETIME
22
23#ifdef SK_TRACK_SHADER_LIFETIME
24 static int32_t gShaderCounter;
25#endif
26
27static inline void inc_shader_counter() {
28#ifdef SK_TRACK_SHADER_LIFETIME
29 int32_t prev = sk_atomic_inc(&gShaderCounter);
30 SkDebugf("+++ shader counter %d\n", prev + 1);
31#endif
32}
33static inline void dec_shader_counter() {
34#ifdef SK_TRACK_SHADER_LIFETIME
35 int32_t prev = sk_atomic_dec(&gShaderCounter);
36 SkDebugf("--- shader counter %d\n", prev - 1);
37#endif
38}
39
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000040SkShader::SkShader(const SkMatrix* localMatrix) {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000041 inc_shader_counter();
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000042 if (localMatrix) {
43 fLocalMatrix = *localMatrix;
44 } else {
45 fLocalMatrix.reset();
46 }
mtklein435eba72014-12-01 12:06:24 -080047 // Pre-cache so future calls to fLocalMatrix.getType() are threadsafe.
48 (void)fLocalMatrix.getType();
reed@android.com8a1c16f2008-12-17 15:59:43 +000049}
50
reed@android.com8a1c16f2008-12-17 15:59:43 +000051SkShader::~SkShader() {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000052 dec_shader_counter();
reed@android.com8a1c16f2008-12-17 15:59:43 +000053}
54
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000055void SkShader::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 this->INHERITED::flatten(buffer);
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000057 bool hasLocalM = !fLocalMatrix.isIdentity();
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000058 buffer.writeBool(hasLocalM);
59 if (hasLocalM) {
60 buffer.writeMatrix(fLocalMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 }
62}
63
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +000064bool SkShader::computeTotalInverse(const ContextRec& rec, SkMatrix* totalInverse) const {
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000065 SkMatrix total;
66 total.setConcat(*rec.fMatrix, fLocalMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000067
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000068 const SkMatrix* m = &total;
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +000069 if (rec.fLocalMatrix) {
70 total.setConcat(*m, *rec.fLocalMatrix);
71 m = &total;
72 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000073 return m->invert(totalInverse);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074}
75
reed8367b8c2014-08-22 08:30:20 -070076bool SkShader::asLuminanceColor(SkColor* colorPtr) const {
77 SkColor storage;
78 if (NULL == colorPtr) {
79 colorPtr = &storage;
80 }
81 if (this->onAsLuminanceColor(colorPtr)) {
82 *colorPtr = SkColorSetA(*colorPtr, 0xFF); // we only return opaque
83 return true;
84 }
85 return false;
86}
87
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +000088SkShader::Context* SkShader::createContext(const ContextRec& rec, void* storage) const {
89 if (!this->computeTotalInverse(rec, NULL)) {
90 return NULL;
91 }
92 return this->onCreateContext(rec, storage);
reed@google.coma641f3f2012-12-13 22:16:30 +000093}
94
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +000095SkShader::Context* SkShader::onCreateContext(const ContextRec& rec, void*) const {
commit-bot@chromium.orgf3e50592014-04-30 23:29:02 +000096 return NULL;
97}
98
99size_t SkShader::contextSize() const {
100 return 0;
101}
102
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000103SkShader::Context::Context(const SkShader& shader, const ContextRec& rec)
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000104 : fShader(shader), fCTM(*rec.fMatrix)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000105{
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000106 // Because the context parameters must be valid at this point, we know that the matrix is
107 // invertible.
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000108 SkAssertResult(fShader.computeTotalInverse(rec, &fTotalInverse));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000109 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
110
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000111 fPaintAlpha = rec.fPaint->getAlpha();
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000112}
113
114SkShader::Context::~Context() {}
115
116SkShader::Context::ShadeProc SkShader::Context::asAShadeProc(void** ctx) {
reed@google.com3bafe742012-10-12 18:56:18 +0000117 return NULL;
118}
119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120#include "SkColorPriv.h"
121
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000122void SkShader::Context::shadeSpan16(int x, int y, uint16_t span16[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 SkASSERT(span16);
124 SkASSERT(count > 0);
125 SkASSERT(this->canCallShadeSpan16());
126
127 // basically, if we get here, the subclass screwed up
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000128 SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129}
130
131#define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space
reed@google.com7c2f27d2011-03-07 19:29:00 +0000132#define kTempColorCount (kTempColorQuadCount << 2)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133
134#ifdef SK_CPU_BENDIAN
135 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
136#else
137 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
138#endif
139
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000140void SkShader::Context::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 SkASSERT(count > 0);
142
143 SkPMColor colors[kTempColorCount];
144
145 while ((count -= kTempColorCount) >= 0) {
146 this->shadeSpan(x, y, colors, kTempColorCount);
147 x += kTempColorCount;
148
149 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
150 int quads = kTempColorQuadCount;
151 do {
152 U8CPU a0 = srcA[0];
153 U8CPU a1 = srcA[4];
154 U8CPU a2 = srcA[8];
155 U8CPU a3 = srcA[12];
156 srcA += 4*4;
157 *alpha++ = SkToU8(a0);
158 *alpha++ = SkToU8(a1);
159 *alpha++ = SkToU8(a2);
160 *alpha++ = SkToU8(a3);
161 } while (--quads != 0);
162 }
163 SkASSERT(count < 0);
164 SkASSERT(count + kTempColorCount >= 0);
165 if (count += kTempColorCount) {
166 this->shadeSpan(x, y, colors, count);
167
168 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
169 do {
170 *alpha++ = *srcA;
171 srcA += 4;
172 } while (--count != 0);
173 }
174#if 0
175 do {
176 int n = count;
177 if (n > kTempColorCount)
178 n = kTempColorCount;
179 SkASSERT(n > 0);
180
181 this->shadeSpan(x, y, colors, n);
182 x += n;
183 count -= n;
184
185 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
186 do {
187 *alpha++ = *srcA;
188 srcA += 4;
189 } while (--n != 0);
190 } while (count > 0);
191#endif
192}
193
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000194SkShader::Context::MatrixClass SkShader::Context::ComputeMatrixClass(const SkMatrix& mat) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 MatrixClass mc = kLinear_MatrixClass;
196
tomhudson@google.com8d430182011-06-06 19:11:19 +0000197 if (mat.hasPerspective()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 if (mat.fixedStepInX(0, NULL, NULL)) {
199 mc = kFixedStepInX_MatrixClass;
200 } else {
201 mc = kPerspective_MatrixClass;
202 }
203 }
204 return mc;
205}
206
207//////////////////////////////////////////////////////////////////////////////
208
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000209SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*, TileMode*) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000210 return kNone_BitmapType;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211}
212
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000213SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
214 return kNone_GradientType;
215}
216
joshualitt5531d512014-12-17 15:50:11 -0800217bool SkShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix&, const SkMatrix*,
218 GrColor*, GrFragmentProcessor**) const {
dandov9de5b512014-06-10 14:38:28 -0700219 return false;
rileya@google.com03c1c352012-07-20 20:02:43 +0000220}
221
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000222SkShader* SkShader::refAsALocalMatrixShader(SkMatrix*) const {
223 return NULL;
224}
225
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000226SkShader* SkShader::CreateEmptyShader() {
227 return SkNEW(SkEmptyShader);
228}
229
reed8367b8c2014-08-22 08:30:20 -0700230SkShader* SkShader::CreateColorShader(SkColor color) {
231 return SkNEW_ARGS(SkColorShader, (color));
232}
233
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000234SkShader* SkShader::CreateBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
235 const SkMatrix* localMatrix) {
mtklein7ef849d2014-11-24 09:11:45 -0800236 return SkCreateBitmapShader(src, tmx, tmy, localMatrix, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237}
238
fmalita2be0fd82014-12-08 09:04:05 -0800239SkShader* SkShader::CreatePictureShader(const SkPicture* src, TileMode tmx, TileMode tmy,
fmalitab5f78262014-08-06 13:07:15 -0700240 const SkMatrix* localMatrix, const SkRect* tile) {
241 return SkPictureShader::Create(src, tmx, tmy, localMatrix, tile);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000242}
243
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000244#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000245void SkShader::toString(SkString* str) const {
commit-bot@chromium.org5970f622014-05-12 20:42:21 +0000246 if (!fLocalMatrix.isIdentity()) {
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000247 str->append(" ");
commit-bot@chromium.org5970f622014-05-12 20:42:21 +0000248 fLocalMatrix.toString(str);
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000249 }
250}
251#endif
252
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253//////////////////////////////////////////////////////////////////////////////
254
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255#include "SkUtils.h"
256
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000257SkColorShader::SkColorShader(SkColor c)
commit-bot@chromium.org76a3b2a2014-04-24 16:54:46 +0000258 : fColor(c) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000259}
260
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000261bool SkColorShader::isOpaque() const {
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000262 return SkColorGetA(fColor) == 255;
263}
264
reed9fa60da2014-08-21 07:59:51 -0700265SkFlattenable* SkColorShader::CreateProc(SkReadBuffer& buffer) {
266 return SkNEW_ARGS(SkColorShader, (buffer.readColor()));
267}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000269void SkColorShader::flatten(SkWriteBuffer& buffer) const {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000270 buffer.writeColor(fColor);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271}
272
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000273uint32_t SkColorShader::ColorShaderContext::getFlags() const {
reed@google.com59ccef62011-12-07 14:59:50 +0000274 return fFlags;
275}
276
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000277uint8_t SkColorShader::ColorShaderContext::getSpan16Alpha() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278 return SkGetPackedA32(fPMColor);
279}
280
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000281SkShader::Context* SkColorShader::onCreateContext(const ContextRec& rec, void* storage) const {
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000282 return SkNEW_PLACEMENT_ARGS(storage, ColorShaderContext, (*this, rec));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000283}
284
285SkColorShader::ColorShaderContext::ColorShaderContext(const SkColorShader& shader,
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000286 const ContextRec& rec)
287 : INHERITED(shader, rec)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000288{
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000289 SkColor color = shader.fColor;
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000290 unsigned a = SkAlphaMul(SkColorGetA(color), SkAlpha255To256(rec.fPaint->getAlpha()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000292 unsigned r = SkColorGetR(color);
293 unsigned g = SkColorGetG(color);
294 unsigned b = SkColorGetB(color);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295
296 // we want this before we apply any alpha
297 fColor16 = SkPack888ToRGB16(r, g, b);
298
299 if (a != 255) {
reed@android.com8f073382010-03-11 21:56:16 +0000300 r = SkMulDiv255Round(r, a);
301 g = SkMulDiv255Round(g, a);
302 b = SkMulDiv255Round(b, a);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303 }
304 fPMColor = SkPackARGB32(a, r, g, b);
305
reed@android.com8f073382010-03-11 21:56:16 +0000306 fFlags = kConstInY32_Flag;
reed@android.com5b815352010-03-11 22:20:43 +0000307 if (255 == a) {
reed@android.com5119bdb2009-06-12 21:27:03 +0000308 fFlags |= kOpaqueAlpha_Flag;
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000309 if (rec.fPaint->isDither() == false) {
reed@android.com5b815352010-03-11 22:20:43 +0000310 fFlags |= kHasSpan16_Flag;
311 }
reed@android.com5119bdb2009-06-12 21:27:03 +0000312 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000313}
314
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000315void SkColorShader::ColorShaderContext::shadeSpan(int x, int y, SkPMColor span[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316 sk_memset32(span, fPMColor, count);
317}
318
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000319void SkColorShader::ColorShaderContext::shadeSpan16(int x, int y, uint16_t span[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000320 sk_memset16(span, fColor16, count);
321}
322
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000323void SkColorShader::ColorShaderContext::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000324 memset(alpha, SkGetPackedA32(fPMColor), count);
325}
326
reed@android.comf2b98d62010-12-20 18:26:13 +0000327// if we had a asAColor method, that would be more efficient...
328SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000329 TileMode modes[]) const {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000330 return kNone_BitmapType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000331}
332
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000333SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const {
334 if (info) {
335 if (info->fColors && info->fColorCount >= 1) {
336 info->fColors[0] = fColor;
337 }
338 info->fColorCount = 1;
339 info->fTileMode = SkShader::kRepeat_TileMode;
340 }
341 return kColor_GradientType;
342}
reed@google.com37a20122011-07-05 18:54:12 +0000343
dandov9de5b512014-06-10 14:38:28 -0700344#if SK_SUPPORT_GPU
345
346#include "SkGr.h"
347
joshualitt5531d512014-12-17 15:50:11 -0800348bool SkColorShader::asFragmentProcessor(GrContext*, const SkPaint& paint, const SkMatrix&,
349 const SkMatrix*, GrColor* paintColor,
350 GrFragmentProcessor** fp) const {
joshualittb0a8a372014-09-23 09:50:21 -0700351 *fp = NULL;
dandov9de5b512014-06-10 14:38:28 -0700352 SkColor skColor = fColor;
353 U8CPU newA = SkMulDiv255Round(SkColorGetA(fColor), paint.getAlpha());
bsalomon83d081a2014-07-08 09:56:10 -0700354 *paintColor = SkColor2GrColor(SkColorSetA(skColor, newA));
dandov9de5b512014-06-10 14:38:28 -0700355 return true;
356}
357
358#else
359
joshualitt5531d512014-12-17 15:50:11 -0800360bool SkColorShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix&,
361 const SkMatrix*, GrColor*,
joshualittb0a8a372014-09-23 09:50:21 -0700362 GrFragmentProcessor**) const {
dandov9de5b512014-06-10 14:38:28 -0700363 SkDEBUGFAIL("Should not call in GPU-less build");
364 return false;
365}
366
367#endif
368
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000369#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000370void SkColorShader::toString(SkString* str) const {
371 str->append("SkColorShader: (");
372
commit-bot@chromium.org76a3b2a2014-04-24 16:54:46 +0000373 str->append("Color: ");
374 str->appendHex(fColor);
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000375
376 this->INHERITED::toString(str);
377
378 str->append(")");
379}
380#endif
381
reed@google.com37a20122011-07-05 18:54:12 +0000382///////////////////////////////////////////////////////////////////////////////
383
reed9fa60da2014-08-21 07:59:51 -0700384SkFlattenable* SkEmptyShader::CreateProc(SkReadBuffer&) {
385 return SkShader::CreateEmptyShader();
386}
387
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000388#ifndef SK_IGNORE_TO_STRING
reed@google.com37a20122011-07-05 18:54:12 +0000389#include "SkEmptyShader.h"
390
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000391void SkEmptyShader::toString(SkString* str) const {
392 str->append("SkEmptyShader: (");
393
394 this->INHERITED::toString(str);
395
396 str->append(")");
397}
398#endif