blob: 8c8de53cfec5b4d72120dc795baffbde449052eb [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
Mike Reed6352f002020-03-14 23:30:10 -040078skvm::Color 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) const {
Mike Kleinc376cb42020-03-10 15:40:23 -050083 SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix());
84 if (localM) {
85 lm.writable()->preConcat(*localM);
86 }
Mike Reed6352f002020-03-14 23:30:10 -040087 return as_SB(fProxyShader)->program(p, ctm,lm.get(), quality,dstCS, uniforms,alloc, x,y);
Mike Kleinc376cb42020-03-10 15:40:23 -050088}
89
reed150835e2016-03-10 06:36:49 -080090sk_sp<SkShader> SkShader::makeWithLocalMatrix(const SkMatrix& localMatrix) const {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000091 if (localMatrix.isIdentity()) {
reed150835e2016-03-10 06:36:49 -080092 return sk_ref_sp(const_cast<SkShader*>(this));
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000093 }
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +000094
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000095 const SkMatrix* lm = &localMatrix;
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +000096
Ben Wagnera8834bb2016-10-24 11:36:21 -040097 sk_sp<SkShader> baseShader;
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000098 SkMatrix otherLocalMatrix;
Florin Malita4aed1382017-05-25 10:38:07 -040099 sk_sp<SkShader> proxy(as_SB(this)->makeAsALocalMatrixShader(&otherLocalMatrix));
reedf880e452015-12-30 13:39:41 -0800100 if (proxy) {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000101 otherLocalMatrix.preConcat(localMatrix);
102 lm = &otherLocalMatrix;
Ben Wagnera8834bb2016-10-24 11:36:21 -0400103 baseShader = proxy;
104 } else {
105 baseShader = sk_ref_sp(const_cast<SkShader*>(this));
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000106 }
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +0000107
Ben Wagnera8834bb2016-10-24 11:36:21 -0400108 return sk_make_sp<SkLocalMatrixShader>(std::move(baseShader), *lm);
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000109}
Mike Reed121c2af2020-03-10 14:02:56 -0400110
111////////////////////////////////////////////////////////////////////
112
113/**
114 * Replaces the CTM when used. Created to support clipShaders, which have to be evaluated
115 * using the CTM that was present at the time they were specified (which may be different
116 * from the CTM at the time something is drawn through the clip.
117 */
118class SkCTMShader final : public SkShaderBase {
119public:
120 SkCTMShader(sk_sp<SkShader> proxy, const SkMatrix& ctm)
121 : fProxyShader(std::move(proxy))
122 , fCTM(ctm)
123 {}
124
125 GradientType asAGradient(GradientInfo* info) const override {
126 return fProxyShader->asAGradient(info);
127 }
128
129#if SK_SUPPORT_GPU
130 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override;
131#endif
132
133protected:
134 void flatten(SkWriteBuffer&) const override { SkASSERT(false); }
135
136#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
137 Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override { return nullptr; }
138#endif
139
Mike Klein272c56b2020-03-10 14:24:35 -0500140 bool onAppendStages(const SkStageRec& rec) const override {
141 SkStageRec newRec = {
142 rec.fPipeline,
143 rec.fAlloc,
144 rec.fDstColorType,
145 rec.fDstCS,
146 rec.fPaint,
147 rec.fLocalM,
148 fCTM,
149 };
150 return as_SB(fProxyShader)->appendStages(newRec);
151 }
152
Mike Reed6352f002020-03-14 23:30:10 -0400153 skvm::Color onProgram(skvm::Builder* p,
154 const SkMatrix& ctm, const SkMatrix* localM,
155 SkFilterQuality quality, SkColorSpace* dstCS,
156 skvm::Uniforms* uniforms, SkArenaAlloc* alloc,
157 skvm::F32 x, skvm::F32 y) const override {
158 return as_SB(fProxyShader)->program(p, fCTM,localM, quality,dstCS, uniforms,alloc, x,y);
Mike Klein272c56b2020-03-10 14:24:35 -0500159 }
Mike Reed121c2af2020-03-10 14:02:56 -0400160
161private:
162 SK_FLATTENABLE_HOOKS(SkCTMShader)
163
164 sk_sp<SkShader> fProxyShader;
165 SkMatrix fCTM;
166
167 typedef SkShaderBase INHERITED;
168};
169
170
171#if SK_SUPPORT_GPU
172std::unique_ptr<GrFragmentProcessor> SkCTMShader::asFragmentProcessor(
173 const GrFPArgs& args) const {
174 return as_SB(fProxyShader)->asFragmentProcessor(
175 GrFPArgs::WithPreLocalMatrix(args, this->getLocalMatrix()));
176}
177#endif
178
179sk_sp<SkFlattenable> SkCTMShader::CreateProc(SkReadBuffer& buffer) {
180 SkASSERT(false);
181 return nullptr;
182}
183
Mike Reed121c2af2020-03-10 14:02:56 -0400184sk_sp<SkShader> SkShaderBase::makeWithCTM(const SkMatrix& postM) const {
185 return postM.isIdentity() ? sk_ref_sp(this)
186 : sk_sp<SkShader>(new SkCTMShader(sk_ref_sp(this), postM));
187}