blob: a54262ba4aab69d8ebe2708bc24d3686a7ce6bc5 [file] [log] [blame]
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +00001/*
2 * Copyright 2014 Google Inc.
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/core/SkTLazy.h"
Mike Kleinc376cb42020-03-10 15:40:23 -05009#include "src/core/SkVM.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/shaders/SkLocalMatrixShader.h"
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000011
bungeman06ca8ec2016-06-09 08:01:03 -070012#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrFragmentProcessor.h"
bungeman06ca8ec2016-06-09 08:01:03 -070014#endif
15
16#if SK_SUPPORT_GPU
Brian Salomonaff329b2017-08-11 09:40:37 -040017std::unique_ptr<GrFragmentProcessor> SkLocalMatrixShader::asFragmentProcessor(
Mike Reede3429e62018-01-19 11:43:34 -050018 const GrFPArgs& args) const {
Florin Malitac6c5ead2018-04-11 15:33:40 -040019 return as_SB(fProxyShader)->asFragmentProcessor(
20 GrFPArgs::WithPreLocalMatrix(args, this->getLocalMatrix()));
bungeman06ca8ec2016-06-09 08:01:03 -070021}
22#endif
23
reed60c9b582016-04-03 09:11:13 -070024sk_sp<SkFlattenable> SkLocalMatrixShader::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -070025 SkMatrix lm;
26 buffer.readMatrix(&lm);
reed8a21c9f2016-03-08 18:50:00 -080027 auto baseShader(buffer.readShader());
reedf880e452015-12-30 13:39:41 -080028 if (!baseShader) {
halcanary96fcdcc2015-08-27 07:41:13 -070029 return nullptr;
reed9fa60da2014-08-21 07:59:51 -070030 }
reed60c9b582016-04-03 09:11:13 -070031 return baseShader->makeWithLocalMatrix(lm);
reed9fa60da2014-08-21 07:59:51 -070032}
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000033
34void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const {
reed9fa60da2014-08-21 07:59:51 -070035 buffer.writeMatrix(this->getLocalMatrix());
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000036 buffer.writeFlattenable(fProxyShader.get());
37}
38
Mike Reede92aae62018-10-17 10:21:51 -040039#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Florin Malita4aed1382017-05-25 10:38:07 -040040SkShaderBase::Context* SkLocalMatrixShader::onMakeContext(
Herb Derby83e939b2017-02-07 14:25:11 -050041 const ContextRec& rec, SkArenaAlloc* alloc) const
42{
Florin Malita325ea322018-04-04 14:17:30 -040043 SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix());
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000044 if (rec.fLocalMatrix) {
Florin Malita325ea322018-04-04 14:17:30 -040045 lm.writable()->preConcat(*rec.fLocalMatrix);
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000046 }
Florin Malita325ea322018-04-04 14:17:30 -040047
48 ContextRec newRec(rec);
49 newRec.fLocalMatrix = lm;
50
Florin Malita4aed1382017-05-25 10:38:07 -040051 return as_SB(fProxyShader)->makeContext(newRec, alloc);
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000052}
Mike Reede92aae62018-10-17 10:21:51 -040053#endif
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000054
Mike Reedfae8fce2019-04-03 10:27:45 -040055SkImage* SkLocalMatrixShader::onIsAImage(SkMatrix* outMatrix, SkTileMode* mode) const {
Matt Sarett547a7272017-04-12 11:52:47 -040056 SkMatrix imageMatrix;
57 SkImage* image = fProxyShader->isAImage(&imageMatrix, mode);
58 if (image && outMatrix) {
59 // Local matrix must be applied first so it is on the right side of the concat.
60 *outMatrix = SkMatrix::Concat(imageMatrix, this->getLocalMatrix());
61 }
62
63 return image;
64}
65
Mike Reed1386b2d2019-03-13 21:15:05 -040066bool SkLocalMatrixShader::onAppendStages(const SkStageRec& rec) const {
Florin Malita325ea322018-04-04 14:17:30 -040067 SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix());
Mike Reed1d8c42e2017-08-29 14:58:19 -040068 if (rec.fLocalM) {
Florin Malita325ea322018-04-04 14:17:30 -040069 lm.writable()->preConcat(*rec.fLocalM);
Florin Malita882ccaf2017-01-27 10:51:58 -050070 }
Florin Malita325ea322018-04-04 14:17:30 -040071
Mike Reed1386b2d2019-03-13 21:15:05 -040072 SkStageRec newRec = rec;
Florin Malita325ea322018-04-04 14:17:30 -040073 newRec.fLocalM = lm;
Mike Reed1d8c42e2017-08-29 14:58:19 -040074 return as_SB(fProxyShader)->appendStages(newRec);
Florin Malita882ccaf2017-01-27 10:51:58 -050075}
76
Mike Kleinc376cb42020-03-10 15:40:23 -050077
78bool SkLocalMatrixShader::onProgram(skvm::Builder* p,
79 const SkMatrix& ctm, const SkMatrix* localM,
80 SkFilterQuality quality, SkColorSpace* dstCS,
81 skvm::Uniforms* uniforms, SkArenaAlloc* alloc,
82 skvm::F32 x, skvm::F32 y,
83 skvm::F32* r, skvm::F32* g, skvm::F32* b, skvm::F32* a) const {
84 SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix());
85 if (localM) {
86 lm.writable()->preConcat(*localM);
87 }
88 return as_SB(fProxyShader)
89 ->program(p, ctm,lm.get(), quality,dstCS, uniforms,alloc, x,y, r,g,b,a);
90}
91
reed150835e2016-03-10 06:36:49 -080092sk_sp<SkShader> SkShader::makeWithLocalMatrix(const SkMatrix& localMatrix) const {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000093 if (localMatrix.isIdentity()) {
reed150835e2016-03-10 06:36:49 -080094 return sk_ref_sp(const_cast<SkShader*>(this));
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000095 }
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +000096
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000097 const SkMatrix* lm = &localMatrix;
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +000098
Ben Wagnera8834bb2016-10-24 11:36:21 -040099 sk_sp<SkShader> baseShader;
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000100 SkMatrix otherLocalMatrix;
Florin Malita4aed1382017-05-25 10:38:07 -0400101 sk_sp<SkShader> proxy(as_SB(this)->makeAsALocalMatrixShader(&otherLocalMatrix));
reedf880e452015-12-30 13:39:41 -0800102 if (proxy) {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000103 otherLocalMatrix.preConcat(localMatrix);
104 lm = &otherLocalMatrix;
Ben Wagnera8834bb2016-10-24 11:36:21 -0400105 baseShader = proxy;
106 } else {
107 baseShader = sk_ref_sp(const_cast<SkShader*>(this));
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000108 }
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +0000109
Ben Wagnera8834bb2016-10-24 11:36:21 -0400110 return sk_make_sp<SkLocalMatrixShader>(std::move(baseShader), *lm);
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000111}
Mike Reed121c2af2020-03-10 14:02:56 -0400112
113////////////////////////////////////////////////////////////////////
114
115/**
116 * Replaces the CTM when used. Created to support clipShaders, which have to be evaluated
117 * using the CTM that was present at the time they were specified (which may be different
118 * from the CTM at the time something is drawn through the clip.
119 */
120class SkCTMShader final : public SkShaderBase {
121public:
122 SkCTMShader(sk_sp<SkShader> proxy, const SkMatrix& ctm)
123 : fProxyShader(std::move(proxy))
124 , fCTM(ctm)
125 {}
126
127 GradientType asAGradient(GradientInfo* info) const override {
128 return fProxyShader->asAGradient(info);
129 }
130
131#if SK_SUPPORT_GPU
132 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override;
133#endif
134
135protected:
136 void flatten(SkWriteBuffer&) const override { SkASSERT(false); }
137
138#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
139 Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override { return nullptr; }
140#endif
141
Mike Klein272c56b2020-03-10 14:24:35 -0500142 bool onAppendStages(const SkStageRec& rec) const override {
143 SkStageRec newRec = {
144 rec.fPipeline,
145 rec.fAlloc,
146 rec.fDstColorType,
147 rec.fDstCS,
148 rec.fPaint,
149 rec.fLocalM,
150 fCTM,
151 };
152 return as_SB(fProxyShader)->appendStages(newRec);
153 }
154
155 bool onProgram(skvm::Builder* p,
156 const SkMatrix& ctm, const SkMatrix* localM,
157 SkFilterQuality quality, SkColorSpace* dstCS,
158 skvm::Uniforms* uniforms, SkArenaAlloc* alloc,
159 skvm::F32 x, skvm::F32 y,
160 skvm::F32* r, skvm::F32* g, skvm::F32* b, skvm::F32* a) const override {
161 return as_SB(fProxyShader)
162 ->program(p, fCTM,localM, quality,dstCS, uniforms,alloc, x,y, r,g,b,a);
163 }
Mike Reed121c2af2020-03-10 14:02:56 -0400164
165private:
166 SK_FLATTENABLE_HOOKS(SkCTMShader)
167
168 sk_sp<SkShader> fProxyShader;
169 SkMatrix fCTM;
170
171 typedef SkShaderBase INHERITED;
172};
173
174
175#if SK_SUPPORT_GPU
176std::unique_ptr<GrFragmentProcessor> SkCTMShader::asFragmentProcessor(
177 const GrFPArgs& args) const {
178 return as_SB(fProxyShader)->asFragmentProcessor(
179 GrFPArgs::WithPreLocalMatrix(args, this->getLocalMatrix()));
180}
181#endif
182
183sk_sp<SkFlattenable> SkCTMShader::CreateProc(SkReadBuffer& buffer) {
184 SkASSERT(false);
185 return nullptr;
186}
187
Mike Reed121c2af2020-03-10 14:02:56 -0400188sk_sp<SkShader> SkShaderBase::makeWithCTM(const SkMatrix& postM) const {
189 return postM.isIdentity() ? sk_ref_sp(this)
190 : sk_sp<SkShader>(new SkCTMShader(sk_ref_sp(this), postM));
191}