blob: 07ddc6d0061df1986be8c7879d27b4760a6907ec [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 Malita26368c32017-05-08 13:03:24 -040094 if (!this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, nullptr)) {
halcanary96fcdcc2015-08-27 07:41:13 -070095 return nullptr;
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +000096 }
Herb Derby83e939b2017-02-07 14:25:11 -050097 return this->onMakeContext(rec, alloc);
commit-bot@chromium.orgf3e50592014-04-30 23:29:02 +000098}
99
Florin Malita47e55a52017-06-06 12:26:54 -0400100SkShaderBase::Context* SkShaderBase::makeBurstPipelineContext(const ContextRec& rec,
101 SkArenaAlloc* alloc) const {
102
103 SkASSERT(rec.fPreferredDstType == ContextRec::kPM4f_DstType);
104
105 return this->onMakeBurstPipelineContext(rec, alloc);
106}
107
Florin Malita4aed1382017-05-25 10:38:07 -0400108SkShaderBase::Context::Context(const SkShaderBase& shader, const ContextRec& rec)
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000109 : fShader(shader), fCTM(*rec.fMatrix)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000110{
Florin Malita7d022e02017-05-15 15:06:39 -0400111 // We should never use a context for RP-only shaders.
112 SkASSERT(!shader.isRasterPipelineOnly());
113
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000114 // Because the context parameters must be valid at this point, we know that the matrix is
115 // invertible.
Florin Malita26368c32017-05-08 13:03:24 -0400116 SkAssertResult(fShader.computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &fTotalInverse));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000117 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
118
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000119 fPaintAlpha = rec.fPaint->getAlpha();
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000120}
121
Florin Malita4aed1382017-05-25 10:38:07 -0400122SkShaderBase::Context::~Context() {}
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000123
Florin Malita4aed1382017-05-25 10:38:07 -0400124SkShaderBase::Context::ShadeProc SkShaderBase::Context::asAShadeProc(void** ctx) {
halcanary96fcdcc2015-08-27 07:41:13 -0700125 return nullptr;
reed@google.com3bafe742012-10-12 18:56:18 +0000126}
127
Florin Malita4aed1382017-05-25 10:38:07 -0400128void SkShaderBase::Context::shadeSpan4f(int x, int y, SkPM4f dst[], int count) {
reeda34be682016-02-15 07:48:35 -0800129 const int N = 128;
130 SkPMColor tmp[N];
131 while (count > 0) {
132 int n = SkTMin(count, N);
133 this->shadeSpan(x, y, tmp, n);
134 for (int i = 0; i < n; ++i) {
135 dst[i] = SkPM4f::FromPMColor(tmp[i]);
136 }
137 dst += n;
138 x += n;
139 count -= n;
140 }
reed6d3cef92016-01-22 01:04:29 -0800141}
142
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143#include "SkColorPriv.h"
144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145#define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space
reed@google.com7c2f27d2011-03-07 19:29:00 +0000146#define kTempColorCount (kTempColorQuadCount << 2)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147
148#ifdef SK_CPU_BENDIAN
149 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
150#else
151 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
152#endif
153
Florin Malita4aed1382017-05-25 10:38:07 -0400154void SkShaderBase::Context::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 SkASSERT(count > 0);
156
157 SkPMColor colors[kTempColorCount];
158
159 while ((count -= kTempColorCount) >= 0) {
160 this->shadeSpan(x, y, colors, kTempColorCount);
161 x += kTempColorCount;
162
163 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
164 int quads = kTempColorQuadCount;
165 do {
166 U8CPU a0 = srcA[0];
167 U8CPU a1 = srcA[4];
168 U8CPU a2 = srcA[8];
169 U8CPU a3 = srcA[12];
170 srcA += 4*4;
171 *alpha++ = SkToU8(a0);
172 *alpha++ = SkToU8(a1);
173 *alpha++ = SkToU8(a2);
174 *alpha++ = SkToU8(a3);
175 } while (--quads != 0);
176 }
177 SkASSERT(count < 0);
178 SkASSERT(count + kTempColorCount >= 0);
179 if (count += kTempColorCount) {
180 this->shadeSpan(x, y, colors, count);
181
182 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
183 do {
184 *alpha++ = *srcA;
185 srcA += 4;
186 } while (--count != 0);
187 }
188#if 0
189 do {
190 int n = count;
191 if (n > kTempColorCount)
192 n = kTempColorCount;
193 SkASSERT(n > 0);
194
195 this->shadeSpan(x, y, colors, n);
196 x += n;
197 count -= n;
198
199 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
200 do {
201 *alpha++ = *srcA;
202 srcA += 4;
203 } while (--n != 0);
204 } while (count > 0);
205#endif
206}
207
Florin Malita4aed1382017-05-25 10:38:07 -0400208SkShaderBase::Context::MatrixClass SkShaderBase::Context::ComputeMatrixClass(const SkMatrix& mat) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209 MatrixClass mc = kLinear_MatrixClass;
210
tomhudson@google.com8d430182011-06-06 19:11:19 +0000211 if (mat.hasPerspective()) {
benjaminwagner8e175562016-02-16 10:09:40 -0800212 if (mat.isFixedStepInX()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213 mc = kFixedStepInX_MatrixClass;
214 } else {
215 mc = kPerspective_MatrixClass;
216 }
217 }
218 return mc;
219}
220
221//////////////////////////////////////////////////////////////////////////////
222
Florin Malita4aed1382017-05-25 10:38:07 -0400223const SkMatrix& SkShader::getLocalMatrix() const {
224 return as_SB(this)->getLocalMatrix();
225}
226
227#ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP
228bool SkShader::isABitmap(SkBitmap* outTexture, SkMatrix* outMatrix, TileMode xy[2]) const {
229 return as_SB(this)->onIsABitmap(outTexture, outMatrix, xy);
230}
231#endif
232
233SkImage* SkShader::isAImage(SkMatrix* localMatrix, TileMode xy[2]) const {
234 return as_SB(this)->onIsAImage(localMatrix, xy);
235}
236
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000237SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
238 return kNone_GradientType;
239}
240
bungeman06ca8ec2016-06-09 08:01:03 -0700241#if SK_SUPPORT_GPU
Florin Malita4aed1382017-05-25 10:38:07 -0400242sk_sp<GrFragmentProcessor> SkShaderBase::asFragmentProcessor(const AsFPArgs&) const {
bsalomonc21b09e2015-08-28 18:46:56 -0700243 return nullptr;
rileya@google.com03c1c352012-07-20 20:02:43 +0000244}
bungeman06ca8ec2016-06-09 08:01:03 -0700245#endif
rileya@google.com03c1c352012-07-20 20:02:43 +0000246
Florin Malitad93e11c2017-05-24 21:15:46 +0000247sk_sp<SkShader> SkShader::makeAsALocalMatrixShader(SkMatrix*) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700248 return nullptr;
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000249}
250
reed8a21c9f2016-03-08 18:50:00 -0800251sk_sp<SkShader> SkShader::MakeEmptyShader() { return sk_make_sp<SkEmptyShader>(); }
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000252
reed8a21c9f2016-03-08 18:50:00 -0800253sk_sp<SkShader> SkShader::MakeColorShader(SkColor color) { return sk_make_sp<SkColorShader>(color); }
reed8367b8c2014-08-22 08:30:20 -0700254
reed8a21c9f2016-03-08 18:50:00 -0800255sk_sp<SkShader> SkShader::MakeBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
256 const SkMatrix* localMatrix) {
Florin Malita8d3ffad2017-02-03 18:21:17 +0000257 if (localMatrix && !localMatrix->invert(nullptr)) {
258 return nullptr;
259 }
Herb Derbybfdc87a2017-02-14 15:06:23 +0000260 return SkMakeBitmapShader(src, tmx, tmy, localMatrix, kIfMutable_SkCopyPixelsMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261}
262
reed7fb4f8b2016-03-11 04:33:52 -0800263sk_sp<SkShader> SkShader::MakePictureShader(sk_sp<SkPicture> src, TileMode tmx, TileMode tmy,
reed8a21c9f2016-03-08 18:50:00 -0800264 const SkMatrix* localMatrix, const SkRect* tile) {
Florin Malita8d3ffad2017-02-03 18:21:17 +0000265 if (localMatrix && !localMatrix->invert(nullptr)) {
266 return nullptr;
267 }
reed8a21c9f2016-03-08 18:50:00 -0800268 return SkPictureShader::Make(std::move(src), tmx, tmy, localMatrix, tile);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000269}
270
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000271#ifndef SK_IGNORE_TO_STRING
Florin Malita4aed1382017-05-25 10:38:07 -0400272void SkShaderBase::toString(SkString* str) const {
commit-bot@chromium.org5970f622014-05-12 20:42:21 +0000273 if (!fLocalMatrix.isIdentity()) {
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000274 str->append(" ");
commit-bot@chromium.org5970f622014-05-12 20:42:21 +0000275 fLocalMatrix.toString(str);
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000276 }
277}
278#endif
279
Florin Malita4aed1382017-05-25 10:38:07 -0400280bool SkShaderBase::appendStages(SkRasterPipeline* p,
281 SkColorSpace* dstCS,
282 SkArenaAlloc* alloc,
283 const SkMatrix& ctm,
284 const SkPaint& paint,
285 const SkMatrix* localM) const {
Mike Klein3b840e92017-05-23 15:06:17 -0400286 return this->onAppendStages(p, dstCS, alloc, ctm, paint, localM);
Florin Malita9206c762017-01-30 12:08:05 -0500287}
288
Florin Malita4aed1382017-05-25 10:38:07 -0400289bool SkShaderBase::onAppendStages(SkRasterPipeline* p,
290 SkColorSpace* dstCS,
291 SkArenaAlloc* alloc,
292 const SkMatrix& ctm,
293 const SkPaint& paint,
294 const SkMatrix* localM) const {
Mike Reed6867eee2017-06-02 13:25:15 -0400295 // SkShader::Context::shadeSpan4f() handles the paint opacity internally,
296 // but SkRasterPipelineBlitter applies it as a separate stage.
297 // We skip the internal shadeSpan4f() step by forcing the paint opaque.
298 SkTCopyOnFirstWrite<SkPaint> opaquePaint(paint);
299 if (paint.getAlpha() != SK_AlphaOPAQUE) {
300 opaquePaint.writable()->setAlpha(SK_AlphaOPAQUE);
301 }
302
303 ContextRec rec(*opaquePaint, ctm, localM, ContextRec::kPM4f_DstType, dstCS);
304
305 struct CallbackCtx : SkJumper_CallbackCtx {
306 sk_sp<SkShader> shader;
307 Context* ctx;
308 };
309 auto cb = alloc->make<CallbackCtx>();
310 cb->shader = dstCS ? SkColorSpaceXformer::Make(sk_ref_sp(dstCS))->apply(this)
311 : sk_ref_sp((SkShader*)this);
312 cb->ctx = as_SB(cb->shader)->makeContext(rec, alloc);
313 cb->fn = [](SkJumper_CallbackCtx* self, int active_pixels) {
314 auto c = (CallbackCtx*)self;
315 int x = (int)c->rgba[0],
316 y = (int)c->rgba[1];
317 c->ctx->shadeSpan4f(x,y, (SkPM4f*)c->rgba, active_pixels);
318 };
319
320 if (cb->ctx) {
321 p->append(SkRasterPipeline::seed_shader);
322 p->append(SkRasterPipeline::callback, cb);
323 return true;
324 }
Mike Klein44d32792017-05-10 12:29:38 -0400325 return false;
326}
327
reed0ccc62d2016-05-04 13:09:39 -0700328///////////////////////////////////////////////////////////////////////////////////////////////////
reed830dfd82016-03-16 12:29:01 -0700329
reed60c9b582016-04-03 09:11:13 -0700330sk_sp<SkFlattenable> SkEmptyShader::CreateProc(SkReadBuffer&) {
331 return SkShader::MakeEmptyShader();
reed9fa60da2014-08-21 07:59:51 -0700332}
333
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000334#ifndef SK_IGNORE_TO_STRING
reed@google.com37a20122011-07-05 18:54:12 +0000335#include "SkEmptyShader.h"
336
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000337void SkEmptyShader::toString(SkString* str) const {
338 str->append("SkEmptyShader: (");
339
340 this->INHERITED::toString(str);
341
342 str->append(")");
343}
344#endif