blob: b3b2c9a67e52e4a5f83595fba2aa31aac68e49e2 [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 Malitac6c5ead2018-04-11 15:33:40 -040070SkTCopyOnFirstWrite<SkMatrix>
71SkShaderBase::totalLocalMatrix(const SkMatrix* preLocalMatrix,
72 const SkMatrix* postLocalMatrix) const {
73 SkTCopyOnFirstWrite<SkMatrix> m(fLocalMatrix);
74
75 if (preLocalMatrix) {
76 m.writable()->preConcat(*preLocalMatrix);
77 }
78
79 if (postLocalMatrix) {
80 m.writable()->postConcat(*postLocalMatrix);
81 }
82
83 return m;
84}
85
Florin Malita4aed1382017-05-25 10:38:07 -040086bool SkShaderBase::computeTotalInverse(const SkMatrix& ctm,
87 const SkMatrix* outerLocalMatrix,
88 SkMatrix* totalInverse) const {
Florin Malitac6c5ead2018-04-11 15:33:40 -040089 return SkMatrix::Concat(ctm, *this->totalLocalMatrix(outerLocalMatrix)).invert(totalInverse);
reed@android.com8a1c16f2008-12-17 15:59:43 +000090}
91
Florin Malita4aed1382017-05-25 10:38:07 -040092bool SkShaderBase::asLuminanceColor(SkColor* colorPtr) const {
reed8367b8c2014-08-22 08:30:20 -070093 SkColor storage;
halcanary96fcdcc2015-08-27 07:41:13 -070094 if (nullptr == colorPtr) {
reed8367b8c2014-08-22 08:30:20 -070095 colorPtr = &storage;
96 }
97 if (this->onAsLuminanceColor(colorPtr)) {
98 *colorPtr = SkColorSetA(*colorPtr, 0xFF); // we only return opaque
99 return true;
100 }
101 return false;
102}
103
Florin Malita4aed1382017-05-25 10:38:07 -0400104SkShaderBase::Context* SkShaderBase::makeContext(const ContextRec& rec, SkArenaAlloc* alloc) const {
Florin Malitaaf2769d2018-04-04 13:46:35 -0400105 // We always fall back to raster pipeline when perspective is present.
106 if (rec.fMatrix->hasPerspective() ||
107 fLocalMatrix.hasPerspective() ||
108 (rec.fLocalMatrix && rec.fLocalMatrix->hasPerspective()) ||
109 !this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, nullptr)) {
110 return nullptr;
111 }
112
113 return this->onMakeContext(rec, alloc);
commit-bot@chromium.orgf3e50592014-04-30 23:29:02 +0000114}
115
Florin Malita47e55a52017-06-06 12:26:54 -0400116SkShaderBase::Context* SkShaderBase::makeBurstPipelineContext(const ContextRec& rec,
117 SkArenaAlloc* alloc) const {
118
Florin Malita5769dd22017-07-12 13:31:25 -0400119 // Always use vanilla stages for perspective.
120 if (rec.fMatrix->hasPerspective() || fLocalMatrix.hasPerspective()) {
121 return nullptr;
122 }
123
Florin Malita8b9566a2017-07-05 10:59:12 -0400124 return this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, nullptr)
125 ? this->onMakeBurstPipelineContext(rec, alloc)
126 : nullptr;
Florin Malita47e55a52017-06-06 12:26:54 -0400127}
128
Florin Malita4aed1382017-05-25 10:38:07 -0400129SkShaderBase::Context::Context(const SkShaderBase& shader, const ContextRec& rec)
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000130 : fShader(shader), fCTM(*rec.fMatrix)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000131{
Florin Malitaaf2769d2018-04-04 13:46:35 -0400132 // We should never use a context with perspective.
Florin Malita5769dd22017-07-12 13:31:25 -0400133 SkASSERT(!rec.fMatrix->hasPerspective());
134 SkASSERT(!rec.fLocalMatrix || !rec.fLocalMatrix->hasPerspective());
Florin Malitaaf2769d2018-04-04 13:46:35 -0400135 SkASSERT(!shader.getLocalMatrix().hasPerspective());
Florin Malita7d022e02017-05-15 15:06:39 -0400136
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000137 // Because the context parameters must be valid at this point, we know that the matrix is
138 // invertible.
Florin Malita26368c32017-05-08 13:03:24 -0400139 SkAssertResult(fShader.computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &fTotalInverse));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000140
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000141 fPaintAlpha = rec.fPaint->getAlpha();
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000142}
143
Florin Malita4aed1382017-05-25 10:38:07 -0400144SkShaderBase::Context::~Context() {}
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000145
Florin Malita4aed1382017-05-25 10:38:07 -0400146void SkShaderBase::Context::shadeSpan4f(int x, int y, SkPM4f dst[], int count) {
reeda34be682016-02-15 07:48:35 -0800147 const int N = 128;
148 SkPMColor tmp[N];
149 while (count > 0) {
150 int n = SkTMin(count, N);
151 this->shadeSpan(x, y, tmp, n);
152 for (int i = 0; i < n; ++i) {
153 dst[i] = SkPM4f::FromPMColor(tmp[i]);
154 }
155 dst += n;
156 x += n;
157 count -= n;
158 }
reed6d3cef92016-01-22 01:04:29 -0800159}
160
Florin Malita4aed1382017-05-25 10:38:07 -0400161const SkMatrix& SkShader::getLocalMatrix() const {
162 return as_SB(this)->getLocalMatrix();
163}
164
165#ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP
166bool SkShader::isABitmap(SkBitmap* outTexture, SkMatrix* outMatrix, TileMode xy[2]) const {
167 return as_SB(this)->onIsABitmap(outTexture, outMatrix, xy);
168}
169#endif
170
171SkImage* SkShader::isAImage(SkMatrix* localMatrix, TileMode xy[2]) const {
172 return as_SB(this)->onIsAImage(localMatrix, xy);
173}
174
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000175SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
176 return kNone_GradientType;
177}
178
bungeman06ca8ec2016-06-09 08:01:03 -0700179#if SK_SUPPORT_GPU
Mike Reede3429e62018-01-19 11:43:34 -0500180std::unique_ptr<GrFragmentProcessor> SkShaderBase::asFragmentProcessor(const GrFPArgs&) const {
bsalomonc21b09e2015-08-28 18:46:56 -0700181 return nullptr;
rileya@google.com03c1c352012-07-20 20:02:43 +0000182}
bungeman06ca8ec2016-06-09 08:01:03 -0700183#endif
rileya@google.com03c1c352012-07-20 20:02:43 +0000184
Florin Malitad93e11c2017-05-24 21:15:46 +0000185sk_sp<SkShader> SkShader::makeAsALocalMatrixShader(SkMatrix*) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700186 return nullptr;
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000187}
188
reed8a21c9f2016-03-08 18:50:00 -0800189sk_sp<SkShader> SkShader::MakeEmptyShader() { return sk_make_sp<SkEmptyShader>(); }
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000190
reed8a21c9f2016-03-08 18:50:00 -0800191sk_sp<SkShader> SkShader::MakeColorShader(SkColor color) { return sk_make_sp<SkColorShader>(color); }
reed8367b8c2014-08-22 08:30:20 -0700192
reed8a21c9f2016-03-08 18:50:00 -0800193sk_sp<SkShader> SkShader::MakeBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
194 const SkMatrix* localMatrix) {
Florin Malita8d3ffad2017-02-03 18:21:17 +0000195 if (localMatrix && !localMatrix->invert(nullptr)) {
196 return nullptr;
197 }
Herb Derbybfdc87a2017-02-14 15:06:23 +0000198 return SkMakeBitmapShader(src, tmx, tmy, localMatrix, kIfMutable_SkCopyPixelsMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199}
200
reed7fb4f8b2016-03-11 04:33:52 -0800201sk_sp<SkShader> SkShader::MakePictureShader(sk_sp<SkPicture> src, TileMode tmx, TileMode tmy,
reed8a21c9f2016-03-08 18:50:00 -0800202 const SkMatrix* localMatrix, const SkRect* tile) {
Florin Malita8d3ffad2017-02-03 18:21:17 +0000203 if (localMatrix && !localMatrix->invert(nullptr)) {
204 return nullptr;
205 }
reed8a21c9f2016-03-08 18:50:00 -0800206 return SkPictureShader::Make(std::move(src), tmx, tmy, localMatrix, tile);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000207}
208
Mike Reed1d8c42e2017-08-29 14:58:19 -0400209bool SkShaderBase::appendStages(const StageRec& rec) const {
210 return this->onAppendStages(rec);
Florin Malita9206c762017-01-30 12:08:05 -0500211}
212
Mike Reed1d8c42e2017-08-29 14:58:19 -0400213bool SkShaderBase::onAppendStages(const StageRec& rec) const {
Mike Reed6867eee2017-06-02 13:25:15 -0400214 // SkShader::Context::shadeSpan4f() handles the paint opacity internally,
215 // but SkRasterPipelineBlitter applies it as a separate stage.
216 // We skip the internal shadeSpan4f() step by forcing the paint opaque.
Mike Reed1d8c42e2017-08-29 14:58:19 -0400217 SkTCopyOnFirstWrite<SkPaint> opaquePaint(rec.fPaint);
218 if (rec.fPaint.getAlpha() != SK_AlphaOPAQUE) {
Mike Reed6867eee2017-06-02 13:25:15 -0400219 opaquePaint.writable()->setAlpha(SK_AlphaOPAQUE);
220 }
221
Brian Osman0d5d0652018-08-08 15:41:14 -0400222 ContextRec cr(*opaquePaint, rec.fCTM, rec.fLocalM, rec.fDstCS);
Mike Reed6867eee2017-06-02 13:25:15 -0400223
224 struct CallbackCtx : SkJumper_CallbackCtx {
225 sk_sp<SkShader> shader;
226 Context* ctx;
227 };
Mike Reed1d8c42e2017-08-29 14:58:19 -0400228 auto cb = rec.fAlloc->make<CallbackCtx>();
229 cb->shader = rec.fDstCS ? SkColorSpaceXformer::Make(sk_ref_sp(rec.fDstCS))->apply(this)
230 : sk_ref_sp((SkShader*)this);
231 cb->ctx = as_SB(cb->shader)->makeContext(cr, rec.fAlloc);
Mike Reed6867eee2017-06-02 13:25:15 -0400232 cb->fn = [](SkJumper_CallbackCtx* self, int active_pixels) {
233 auto c = (CallbackCtx*)self;
234 int x = (int)c->rgba[0],
235 y = (int)c->rgba[1];
236 c->ctx->shadeSpan4f(x,y, (SkPM4f*)c->rgba, active_pixels);
237 };
238
239 if (cb->ctx) {
Mike Kleine8de0242018-03-10 12:37:11 -0500240 rec.fPipeline->append(SkRasterPipeline::seed_shader);
Mike Reed1d8c42e2017-08-29 14:58:19 -0400241 rec.fPipeline->append(SkRasterPipeline::callback, cb);
Mike Reed6867eee2017-06-02 13:25:15 -0400242 return true;
243 }
Mike Klein44d32792017-05-10 12:29:38 -0400244 return false;
245}
246
reed0ccc62d2016-05-04 13:09:39 -0700247///////////////////////////////////////////////////////////////////////////////////////////////////
reed830dfd82016-03-16 12:29:01 -0700248
reed60c9b582016-04-03 09:11:13 -0700249sk_sp<SkFlattenable> SkEmptyShader::CreateProc(SkReadBuffer&) {
250 return SkShader::MakeEmptyShader();
reed9fa60da2014-08-21 07:59:51 -0700251}