blob: bd202c1aed8d6822d19f4d17f5800eb91de0ae6f [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 Malita8b9566a2017-07-05 10:59:12 -0400104 return this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, nullptr)
105 ? this->onMakeBurstPipelineContext(rec, alloc)
106 : nullptr;
Florin Malita47e55a52017-06-06 12:26:54 -0400107}
108
Florin Malita4aed1382017-05-25 10:38:07 -0400109SkShaderBase::Context::Context(const SkShaderBase& shader, const ContextRec& rec)
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000110 : fShader(shader), fCTM(*rec.fMatrix)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000111{
Florin Malita7d022e02017-05-15 15:06:39 -0400112 // We should never use a context for RP-only shaders.
113 SkASSERT(!shader.isRasterPipelineOnly());
114
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000115 // Because the context parameters must be valid at this point, we know that the matrix is
116 // invertible.
Florin Malita26368c32017-05-08 13:03:24 -0400117 SkAssertResult(fShader.computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &fTotalInverse));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000118 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
119
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000120 fPaintAlpha = rec.fPaint->getAlpha();
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000121}
122
Florin Malita4aed1382017-05-25 10:38:07 -0400123SkShaderBase::Context::~Context() {}
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000124
Florin Malita4aed1382017-05-25 10:38:07 -0400125SkShaderBase::Context::ShadeProc SkShaderBase::Context::asAShadeProc(void** ctx) {
halcanary96fcdcc2015-08-27 07:41:13 -0700126 return nullptr;
reed@google.com3bafe742012-10-12 18:56:18 +0000127}
128
Florin Malita4aed1382017-05-25 10:38:07 -0400129void SkShaderBase::Context::shadeSpan4f(int x, int y, SkPM4f dst[], int count) {
reeda34be682016-02-15 07:48:35 -0800130 const int N = 128;
131 SkPMColor tmp[N];
132 while (count > 0) {
133 int n = SkTMin(count, N);
134 this->shadeSpan(x, y, tmp, n);
135 for (int i = 0; i < n; ++i) {
136 dst[i] = SkPM4f::FromPMColor(tmp[i]);
137 }
138 dst += n;
139 x += n;
140 count -= n;
141 }
reed6d3cef92016-01-22 01:04:29 -0800142}
143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144#include "SkColorPriv.h"
145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146#define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space
reed@google.com7c2f27d2011-03-07 19:29:00 +0000147#define kTempColorCount (kTempColorQuadCount << 2)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148
149#ifdef SK_CPU_BENDIAN
150 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
151#else
152 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
153#endif
154
Florin Malita4aed1382017-05-25 10:38:07 -0400155void SkShaderBase::Context::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 SkASSERT(count > 0);
157
158 SkPMColor colors[kTempColorCount];
159
160 while ((count -= kTempColorCount) >= 0) {
161 this->shadeSpan(x, y, colors, kTempColorCount);
162 x += kTempColorCount;
163
164 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
165 int quads = kTempColorQuadCount;
166 do {
167 U8CPU a0 = srcA[0];
168 U8CPU a1 = srcA[4];
169 U8CPU a2 = srcA[8];
170 U8CPU a3 = srcA[12];
171 srcA += 4*4;
172 *alpha++ = SkToU8(a0);
173 *alpha++ = SkToU8(a1);
174 *alpha++ = SkToU8(a2);
175 *alpha++ = SkToU8(a3);
176 } while (--quads != 0);
177 }
178 SkASSERT(count < 0);
179 SkASSERT(count + kTempColorCount >= 0);
180 if (count += kTempColorCount) {
181 this->shadeSpan(x, y, colors, count);
182
183 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
184 do {
185 *alpha++ = *srcA;
186 srcA += 4;
187 } while (--count != 0);
188 }
189#if 0
190 do {
191 int n = count;
192 if (n > kTempColorCount)
193 n = kTempColorCount;
194 SkASSERT(n > 0);
195
196 this->shadeSpan(x, y, colors, n);
197 x += n;
198 count -= n;
199
200 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
201 do {
202 *alpha++ = *srcA;
203 srcA += 4;
204 } while (--n != 0);
205 } while (count > 0);
206#endif
207}
208
Florin Malita4aed1382017-05-25 10:38:07 -0400209SkShaderBase::Context::MatrixClass SkShaderBase::Context::ComputeMatrixClass(const SkMatrix& mat) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210 MatrixClass mc = kLinear_MatrixClass;
211
tomhudson@google.com8d430182011-06-06 19:11:19 +0000212 if (mat.hasPerspective()) {
benjaminwagner8e175562016-02-16 10:09:40 -0800213 if (mat.isFixedStepInX()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214 mc = kFixedStepInX_MatrixClass;
215 } else {
216 mc = kPerspective_MatrixClass;
217 }
218 }
219 return mc;
220}
221
222//////////////////////////////////////////////////////////////////////////////
223
Florin Malita4aed1382017-05-25 10:38:07 -0400224const SkMatrix& SkShader::getLocalMatrix() const {
225 return as_SB(this)->getLocalMatrix();
226}
227
228#ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP
229bool SkShader::isABitmap(SkBitmap* outTexture, SkMatrix* outMatrix, TileMode xy[2]) const {
230 return as_SB(this)->onIsABitmap(outTexture, outMatrix, xy);
231}
232#endif
233
234SkImage* SkShader::isAImage(SkMatrix* localMatrix, TileMode xy[2]) const {
235 return as_SB(this)->onIsAImage(localMatrix, xy);
236}
237
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000238SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
239 return kNone_GradientType;
240}
241
bungeman06ca8ec2016-06-09 08:01:03 -0700242#if SK_SUPPORT_GPU
Florin Malita4aed1382017-05-25 10:38:07 -0400243sk_sp<GrFragmentProcessor> SkShaderBase::asFragmentProcessor(const AsFPArgs&) const {
bsalomonc21b09e2015-08-28 18:46:56 -0700244 return nullptr;
rileya@google.com03c1c352012-07-20 20:02:43 +0000245}
bungeman06ca8ec2016-06-09 08:01:03 -0700246#endif
rileya@google.com03c1c352012-07-20 20:02:43 +0000247
Florin Malitad93e11c2017-05-24 21:15:46 +0000248sk_sp<SkShader> SkShader::makeAsALocalMatrixShader(SkMatrix*) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700249 return nullptr;
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000250}
251
reed8a21c9f2016-03-08 18:50:00 -0800252sk_sp<SkShader> SkShader::MakeEmptyShader() { return sk_make_sp<SkEmptyShader>(); }
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000253
reed8a21c9f2016-03-08 18:50:00 -0800254sk_sp<SkShader> SkShader::MakeColorShader(SkColor color) { return sk_make_sp<SkColorShader>(color); }
reed8367b8c2014-08-22 08:30:20 -0700255
reed8a21c9f2016-03-08 18:50:00 -0800256sk_sp<SkShader> SkShader::MakeBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
257 const SkMatrix* localMatrix) {
Florin Malita8d3ffad2017-02-03 18:21:17 +0000258 if (localMatrix && !localMatrix->invert(nullptr)) {
259 return nullptr;
260 }
Herb Derbybfdc87a2017-02-14 15:06:23 +0000261 return SkMakeBitmapShader(src, tmx, tmy, localMatrix, kIfMutable_SkCopyPixelsMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262}
263
reed7fb4f8b2016-03-11 04:33:52 -0800264sk_sp<SkShader> SkShader::MakePictureShader(sk_sp<SkPicture> src, TileMode tmx, TileMode tmy,
reed8a21c9f2016-03-08 18:50:00 -0800265 const SkMatrix* localMatrix, const SkRect* tile) {
Florin Malita8d3ffad2017-02-03 18:21:17 +0000266 if (localMatrix && !localMatrix->invert(nullptr)) {
267 return nullptr;
268 }
reed8a21c9f2016-03-08 18:50:00 -0800269 return SkPictureShader::Make(std::move(src), tmx, tmy, localMatrix, tile);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000270}
271
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000272#ifndef SK_IGNORE_TO_STRING
Florin Malita4aed1382017-05-25 10:38:07 -0400273void SkShaderBase::toString(SkString* str) const {
commit-bot@chromium.org5970f622014-05-12 20:42:21 +0000274 if (!fLocalMatrix.isIdentity()) {
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000275 str->append(" ");
commit-bot@chromium.org5970f622014-05-12 20:42:21 +0000276 fLocalMatrix.toString(str);
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000277 }
278}
279#endif
280
Florin Malita4aed1382017-05-25 10:38:07 -0400281bool SkShaderBase::appendStages(SkRasterPipeline* p,
282 SkColorSpace* dstCS,
283 SkArenaAlloc* alloc,
284 const SkMatrix& ctm,
285 const SkPaint& paint,
286 const SkMatrix* localM) const {
Mike Klein3b840e92017-05-23 15:06:17 -0400287 return this->onAppendStages(p, dstCS, alloc, ctm, paint, localM);
Florin Malita9206c762017-01-30 12:08:05 -0500288}
289
Florin Malita4aed1382017-05-25 10:38:07 -0400290bool SkShaderBase::onAppendStages(SkRasterPipeline* p,
291 SkColorSpace* dstCS,
292 SkArenaAlloc* alloc,
293 const SkMatrix& ctm,
294 const SkPaint& paint,
295 const SkMatrix* localM) const {
Mike Reed6867eee2017-06-02 13:25:15 -0400296 // SkShader::Context::shadeSpan4f() handles the paint opacity internally,
297 // but SkRasterPipelineBlitter applies it as a separate stage.
298 // We skip the internal shadeSpan4f() step by forcing the paint opaque.
299 SkTCopyOnFirstWrite<SkPaint> opaquePaint(paint);
300 if (paint.getAlpha() != SK_AlphaOPAQUE) {
301 opaquePaint.writable()->setAlpha(SK_AlphaOPAQUE);
302 }
303
304 ContextRec rec(*opaquePaint, ctm, localM, ContextRec::kPM4f_DstType, dstCS);
305
306 struct CallbackCtx : SkJumper_CallbackCtx {
307 sk_sp<SkShader> shader;
308 Context* ctx;
309 };
310 auto cb = alloc->make<CallbackCtx>();
311 cb->shader = dstCS ? SkColorSpaceXformer::Make(sk_ref_sp(dstCS))->apply(this)
312 : sk_ref_sp((SkShader*)this);
313 cb->ctx = as_SB(cb->shader)->makeContext(rec, alloc);
314 cb->fn = [](SkJumper_CallbackCtx* self, int active_pixels) {
315 auto c = (CallbackCtx*)self;
316 int x = (int)c->rgba[0],
317 y = (int)c->rgba[1];
318 c->ctx->shadeSpan4f(x,y, (SkPM4f*)c->rgba, active_pixels);
319 };
320
321 if (cb->ctx) {
322 p->append(SkRasterPipeline::seed_shader);
323 p->append(SkRasterPipeline::callback, cb);
324 return true;
325 }
Mike Klein44d32792017-05-10 12:29:38 -0400326 return false;
327}
328
reed0ccc62d2016-05-04 13:09:39 -0700329///////////////////////////////////////////////////////////////////////////////////////////////////
reed830dfd82016-03-16 12:29:01 -0700330
reed60c9b582016-04-03 09:11:13 -0700331sk_sp<SkFlattenable> SkEmptyShader::CreateProc(SkReadBuffer&) {
332 return SkShader::MakeEmptyShader();
reed9fa60da2014-08-21 07:59:51 -0700333}
334
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000335#ifndef SK_IGNORE_TO_STRING
reed@google.com37a20122011-07-05 18:54:12 +0000336#include "SkEmptyShader.h"
337
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000338void SkEmptyShader::toString(SkString* str) const {
339 str->append("SkEmptyShader: (");
340
341 this->INHERITED::toString(str);
342
343 str->append(")");
344}
345#endif