blob: 3c9d989814f471204684e81793c4a925799b71c0 [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
Florin Malita9206c762017-01-30 12:08:05 -05008#include "SkArenaAlloc.h"
herbb906daf2015-09-29 09:37:59 -07009#include "SkAtomics.h"
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000010#include "SkBitmapProcShader.h"
reed8367b8c2014-08-22 08:30:20 -070011#include "SkColorShader.h"
Florin Malita47e55a52017-06-06 12:26:54 -040012#include "SkColorSpaceXformer.h"
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +000013#include "SkEmptyShader.h"
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000014#include "SkMallocPixelRef.h"
15#include "SkPaint.h"
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000016#include "SkPicture.h"
17#include "SkPictureShader.h"
Florin Malita9206c762017-01-30 12:08:05 -050018#include "SkPM4fPriv.h"
19#include "SkRasterPipeline.h"
mtklein1b249332015-07-07 12:21:21 -070020#include "SkReadBuffer.h"
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +000021#include "SkScalar.h"
Florin Malita4aed1382017-05-25 10:38:07 -040022#include "SkShaderBase.h"
Florin Malita9206c762017-01-30 12:08:05 -050023#include "SkTLazy.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000024#include "SkWriteBuffer.h"
Mike Kleindc80eaa2017-04-21 12:39:08 -040025#include "../jumper/SkJumper.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
bungeman06ca8ec2016-06-09 08:01:03 -070027#if SK_SUPPORT_GPU
28#include "GrFragmentProcessor.h"
29#endif
30
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000031//#define SK_TRACK_SHADER_LIFETIME
32
33#ifdef SK_TRACK_SHADER_LIFETIME
34 static int32_t gShaderCounter;
35#endif
36
37static inline void inc_shader_counter() {
38#ifdef SK_TRACK_SHADER_LIFETIME
39 int32_t prev = sk_atomic_inc(&gShaderCounter);
40 SkDebugf("+++ shader counter %d\n", prev + 1);
41#endif
42}
43static inline void dec_shader_counter() {
44#ifdef SK_TRACK_SHADER_LIFETIME
45 int32_t prev = sk_atomic_dec(&gShaderCounter);
46 SkDebugf("--- shader counter %d\n", prev - 1);
47#endif
48}
49
Florin Malita4aed1382017-05-25 10:38:07 -040050SkShaderBase::SkShaderBase(const SkMatrix* localMatrix)
51 : fLocalMatrix(localMatrix ? *localMatrix : SkMatrix::I()) {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000052 inc_shader_counter();
mtklein435eba72014-12-01 12:06:24 -080053 // Pre-cache so future calls to fLocalMatrix.getType() are threadsafe.
54 (void)fLocalMatrix.getType();
reed@android.com8a1c16f2008-12-17 15:59:43 +000055}
56
Florin Malita4aed1382017-05-25 10:38:07 -040057SkShaderBase::~SkShaderBase() {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000058 dec_shader_counter();
reed@android.com8a1c16f2008-12-17 15:59:43 +000059}
60
Florin Malita4aed1382017-05-25 10:38:07 -040061void SkShaderBase::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 this->INHERITED::flatten(buffer);
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000063 bool hasLocalM = !fLocalMatrix.isIdentity();
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000064 buffer.writeBool(hasLocalM);
65 if (hasLocalM) {
66 buffer.writeMatrix(fLocalMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 }
68}
69
Florin Malita4aed1382017-05-25 10:38:07 -040070bool SkShaderBase::computeTotalInverse(const SkMatrix& ctm,
71 const SkMatrix* outerLocalMatrix,
72 SkMatrix* totalInverse) const {
Florin Malita26368c32017-05-08 13:03:24 -040073 SkMatrix total = SkMatrix::Concat(ctm, fLocalMatrix);
74 if (outerLocalMatrix) {
75 total.preConcat(*outerLocalMatrix);
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +000076 }
Florin Malitabbeb5732017-01-26 16:23:06 -050077
78 return total.invert(totalInverse);
reed@android.com8a1c16f2008-12-17 15:59:43 +000079}
80
Florin Malita4aed1382017-05-25 10:38:07 -040081bool SkShaderBase::asLuminanceColor(SkColor* colorPtr) const {
reed8367b8c2014-08-22 08:30:20 -070082 SkColor storage;
halcanary96fcdcc2015-08-27 07:41:13 -070083 if (nullptr == colorPtr) {
reed8367b8c2014-08-22 08:30:20 -070084 colorPtr = &storage;
85 }
86 if (this->onAsLuminanceColor(colorPtr)) {
87 *colorPtr = SkColorSetA(*colorPtr, 0xFF); // we only return opaque
88 return true;
89 }
90 return false;
91}
92
Florin Malita4aed1382017-05-25 10:38:07 -040093SkShaderBase::Context* SkShaderBase::makeContext(const ContextRec& rec, SkArenaAlloc* alloc) const {
Florin Malita8b9566a2017-07-05 10:59:12 -040094 return this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, nullptr)
95 ? this->onMakeContext(rec, alloc)
96 : nullptr;
commit-bot@chromium.orgf3e50592014-04-30 23:29:02 +000097}
98
Florin Malita47e55a52017-06-06 12:26:54 -040099SkShaderBase::Context* SkShaderBase::makeBurstPipelineContext(const ContextRec& rec,
100 SkArenaAlloc* alloc) const {
101
102 SkASSERT(rec.fPreferredDstType == ContextRec::kPM4f_DstType);
103
Florin Malita5769dd22017-07-12 13:31:25 -0400104 // Always use vanilla stages for perspective.
105 if (rec.fMatrix->hasPerspective() || fLocalMatrix.hasPerspective()) {
106 return nullptr;
107 }
108
Florin Malita8b9566a2017-07-05 10:59:12 -0400109 return this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, nullptr)
110 ? this->onMakeBurstPipelineContext(rec, alloc)
111 : nullptr;
Florin Malita47e55a52017-06-06 12:26:54 -0400112}
113
Florin Malita4aed1382017-05-25 10:38:07 -0400114SkShaderBase::Context::Context(const SkShaderBase& shader, const ContextRec& rec)
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000115 : fShader(shader), fCTM(*rec.fMatrix)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000116{
Florin Malita7d022e02017-05-15 15:06:39 -0400117 // We should never use a context for RP-only shaders.
118 SkASSERT(!shader.isRasterPipelineOnly());
Florin Malita5769dd22017-07-12 13:31:25 -0400119 // ... or for perspective.
120 SkASSERT(!rec.fMatrix->hasPerspective());
121 SkASSERT(!rec.fLocalMatrix || !rec.fLocalMatrix->hasPerspective());
Florin Malita7d022e02017-05-15 15:06:39 -0400122
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000123 // Because the context parameters must be valid at this point, we know that the matrix is
124 // invertible.
Florin Malita26368c32017-05-08 13:03:24 -0400125 SkAssertResult(fShader.computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &fTotalInverse));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000126
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000127 fPaintAlpha = rec.fPaint->getAlpha();
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000128}
129
Florin Malita4aed1382017-05-25 10:38:07 -0400130SkShaderBase::Context::~Context() {}
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000131
Florin Malita4aed1382017-05-25 10:38:07 -0400132SkShaderBase::Context::ShadeProc SkShaderBase::Context::asAShadeProc(void** ctx) {
halcanary96fcdcc2015-08-27 07:41:13 -0700133 return nullptr;
reed@google.com3bafe742012-10-12 18:56:18 +0000134}
135
Florin Malita4aed1382017-05-25 10:38:07 -0400136void SkShaderBase::Context::shadeSpan4f(int x, int y, SkPM4f dst[], int count) {
reeda34be682016-02-15 07:48:35 -0800137 const int N = 128;
138 SkPMColor tmp[N];
139 while (count > 0) {
140 int n = SkTMin(count, N);
141 this->shadeSpan(x, y, tmp, n);
142 for (int i = 0; i < n; ++i) {
143 dst[i] = SkPM4f::FromPMColor(tmp[i]);
144 }
145 dst += n;
146 x += n;
147 count -= n;
148 }
reed6d3cef92016-01-22 01:04:29 -0800149}
150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151#include "SkColorPriv.h"
152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153#define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space
reed@google.com7c2f27d2011-03-07 19:29:00 +0000154#define kTempColorCount (kTempColorQuadCount << 2)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155
156#ifdef SK_CPU_BENDIAN
157 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
158#else
159 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
160#endif
161
Florin Malita4aed1382017-05-25 10:38:07 -0400162void SkShaderBase::Context::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 SkASSERT(count > 0);
164
165 SkPMColor colors[kTempColorCount];
166
167 while ((count -= kTempColorCount) >= 0) {
168 this->shadeSpan(x, y, colors, kTempColorCount);
169 x += kTempColorCount;
170
171 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
172 int quads = kTempColorQuadCount;
173 do {
174 U8CPU a0 = srcA[0];
175 U8CPU a1 = srcA[4];
176 U8CPU a2 = srcA[8];
177 U8CPU a3 = srcA[12];
178 srcA += 4*4;
179 *alpha++ = SkToU8(a0);
180 *alpha++ = SkToU8(a1);
181 *alpha++ = SkToU8(a2);
182 *alpha++ = SkToU8(a3);
183 } while (--quads != 0);
184 }
185 SkASSERT(count < 0);
186 SkASSERT(count + kTempColorCount >= 0);
187 if (count += kTempColorCount) {
188 this->shadeSpan(x, y, colors, count);
189
190 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
191 do {
192 *alpha++ = *srcA;
193 srcA += 4;
194 } while (--count != 0);
195 }
196#if 0
197 do {
198 int n = count;
199 if (n > kTempColorCount)
200 n = kTempColorCount;
201 SkASSERT(n > 0);
202
203 this->shadeSpan(x, y, colors, n);
204 x += n;
205 count -= n;
206
207 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
208 do {
209 *alpha++ = *srcA;
210 srcA += 4;
211 } while (--n != 0);
212 } while (count > 0);
213#endif
214}
215
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216//////////////////////////////////////////////////////////////////////////////
217
Florin Malita4aed1382017-05-25 10:38:07 -0400218const SkMatrix& SkShader::getLocalMatrix() const {
219 return as_SB(this)->getLocalMatrix();
220}
221
222#ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP
223bool SkShader::isABitmap(SkBitmap* outTexture, SkMatrix* outMatrix, TileMode xy[2]) const {
224 return as_SB(this)->onIsABitmap(outTexture, outMatrix, xy);
225}
226#endif
227
228SkImage* SkShader::isAImage(SkMatrix* localMatrix, TileMode xy[2]) const {
229 return as_SB(this)->onIsAImage(localMatrix, xy);
230}
231
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000232SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
233 return kNone_GradientType;
234}
235
bungeman06ca8ec2016-06-09 08:01:03 -0700236#if SK_SUPPORT_GPU
Florin Malita4aed1382017-05-25 10:38:07 -0400237sk_sp<GrFragmentProcessor> SkShaderBase::asFragmentProcessor(const AsFPArgs&) const {
bsalomonc21b09e2015-08-28 18:46:56 -0700238 return nullptr;
rileya@google.com03c1c352012-07-20 20:02:43 +0000239}
bungeman06ca8ec2016-06-09 08:01:03 -0700240#endif
rileya@google.com03c1c352012-07-20 20:02:43 +0000241
Florin Malitad93e11c2017-05-24 21:15:46 +0000242sk_sp<SkShader> SkShader::makeAsALocalMatrixShader(SkMatrix*) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700243 return nullptr;
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000244}
245
reed8a21c9f2016-03-08 18:50:00 -0800246sk_sp<SkShader> SkShader::MakeEmptyShader() { return sk_make_sp<SkEmptyShader>(); }
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000247
reed8a21c9f2016-03-08 18:50:00 -0800248sk_sp<SkShader> SkShader::MakeColorShader(SkColor color) { return sk_make_sp<SkColorShader>(color); }
reed8367b8c2014-08-22 08:30:20 -0700249
reed8a21c9f2016-03-08 18:50:00 -0800250sk_sp<SkShader> SkShader::MakeBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
251 const SkMatrix* localMatrix) {
Florin Malita8d3ffad2017-02-03 18:21:17 +0000252 if (localMatrix && !localMatrix->invert(nullptr)) {
253 return nullptr;
254 }
Herb Derbybfdc87a2017-02-14 15:06:23 +0000255 return SkMakeBitmapShader(src, tmx, tmy, localMatrix, kIfMutable_SkCopyPixelsMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256}
257
reed7fb4f8b2016-03-11 04:33:52 -0800258sk_sp<SkShader> SkShader::MakePictureShader(sk_sp<SkPicture> src, TileMode tmx, TileMode tmy,
reed8a21c9f2016-03-08 18:50:00 -0800259 const SkMatrix* localMatrix, const SkRect* tile) {
Florin Malita8d3ffad2017-02-03 18:21:17 +0000260 if (localMatrix && !localMatrix->invert(nullptr)) {
261 return nullptr;
262 }
reed8a21c9f2016-03-08 18:50:00 -0800263 return SkPictureShader::Make(std::move(src), tmx, tmy, localMatrix, tile);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000264}
265
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000266#ifndef SK_IGNORE_TO_STRING
Florin Malita4aed1382017-05-25 10:38:07 -0400267void SkShaderBase::toString(SkString* str) const {
commit-bot@chromium.org5970f622014-05-12 20:42:21 +0000268 if (!fLocalMatrix.isIdentity()) {
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000269 str->append(" ");
commit-bot@chromium.org5970f622014-05-12 20:42:21 +0000270 fLocalMatrix.toString(str);
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000271 }
272}
273#endif
274
Florin Malita4aed1382017-05-25 10:38:07 -0400275bool SkShaderBase::appendStages(SkRasterPipeline* p,
276 SkColorSpace* dstCS,
277 SkArenaAlloc* alloc,
278 const SkMatrix& ctm,
279 const SkPaint& paint,
280 const SkMatrix* localM) const {
Mike Klein3b840e92017-05-23 15:06:17 -0400281 return this->onAppendStages(p, dstCS, alloc, ctm, paint, localM);
Florin Malita9206c762017-01-30 12:08:05 -0500282}
283
Florin Malita4aed1382017-05-25 10:38:07 -0400284bool SkShaderBase::onAppendStages(SkRasterPipeline* p,
285 SkColorSpace* dstCS,
286 SkArenaAlloc* alloc,
287 const SkMatrix& ctm,
288 const SkPaint& paint,
289 const SkMatrix* localM) const {
Mike Reed6867eee2017-06-02 13:25:15 -0400290 // SkShader::Context::shadeSpan4f() handles the paint opacity internally,
291 // but SkRasterPipelineBlitter applies it as a separate stage.
292 // We skip the internal shadeSpan4f() step by forcing the paint opaque.
293 SkTCopyOnFirstWrite<SkPaint> opaquePaint(paint);
294 if (paint.getAlpha() != SK_AlphaOPAQUE) {
295 opaquePaint.writable()->setAlpha(SK_AlphaOPAQUE);
296 }
297
298 ContextRec rec(*opaquePaint, ctm, localM, ContextRec::kPM4f_DstType, dstCS);
299
300 struct CallbackCtx : SkJumper_CallbackCtx {
301 sk_sp<SkShader> shader;
302 Context* ctx;
303 };
304 auto cb = alloc->make<CallbackCtx>();
305 cb->shader = dstCS ? SkColorSpaceXformer::Make(sk_ref_sp(dstCS))->apply(this)
306 : sk_ref_sp((SkShader*)this);
307 cb->ctx = as_SB(cb->shader)->makeContext(rec, alloc);
308 cb->fn = [](SkJumper_CallbackCtx* self, int active_pixels) {
309 auto c = (CallbackCtx*)self;
310 int x = (int)c->rgba[0],
311 y = (int)c->rgba[1];
312 c->ctx->shadeSpan4f(x,y, (SkPM4f*)c->rgba, active_pixels);
313 };
314
315 if (cb->ctx) {
316 p->append(SkRasterPipeline::seed_shader);
317 p->append(SkRasterPipeline::callback, cb);
318 return true;
319 }
Mike Klein44d32792017-05-10 12:29:38 -0400320 return false;
321}
322
reed0ccc62d2016-05-04 13:09:39 -0700323///////////////////////////////////////////////////////////////////////////////////////////////////
reed830dfd82016-03-16 12:29:01 -0700324
reed60c9b582016-04-03 09:11:13 -0700325sk_sp<SkFlattenable> SkEmptyShader::CreateProc(SkReadBuffer&) {
326 return SkShader::MakeEmptyShader();
reed9fa60da2014-08-21 07:59:51 -0700327}
328
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000329#ifndef SK_IGNORE_TO_STRING
reed@google.com37a20122011-07-05 18:54:12 +0000330#include "SkEmptyShader.h"
331
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000332void SkEmptyShader::toString(SkString* str) const {
333 str->append("SkEmptyShader: (");
334
335 this->INHERITED::toString(str);
336
337 str->append(")");
338}
339#endif