commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/core/SkTLazy.h" |
Mike Klein | c376cb4 | 2020-03-10 15:40:23 -0500 | [diff] [blame] | 9 | #include "src/core/SkVM.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/shaders/SkLocalMatrixShader.h" |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 11 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 12 | #if SK_SUPPORT_GPU |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrFragmentProcessor.h" |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 14 | #endif |
| 15 | |
| 16 | #if SK_SUPPORT_GPU |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 17 | std::unique_ptr<GrFragmentProcessor> SkLocalMatrixShader::asFragmentProcessor( |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 18 | const GrFPArgs& args) const { |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 19 | return as_SB(fProxyShader)->asFragmentProcessor( |
| 20 | GrFPArgs::WithPreLocalMatrix(args, this->getLocalMatrix())); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 21 | } |
| 22 | #endif |
| 23 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 24 | sk_sp<SkFlattenable> SkLocalMatrixShader::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 25 | SkMatrix lm; |
| 26 | buffer.readMatrix(&lm); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 27 | auto baseShader(buffer.readShader()); |
reed | f880e45 | 2015-12-30 13:39:41 -0800 | [diff] [blame] | 28 | if (!baseShader) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 29 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 30 | } |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 31 | return baseShader->makeWithLocalMatrix(lm); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 32 | } |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 33 | |
| 34 | void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 35 | buffer.writeMatrix(this->getLocalMatrix()); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 36 | buffer.writeFlattenable(fProxyShader.get()); |
| 37 | } |
| 38 | |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 39 | #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 40 | SkShaderBase::Context* SkLocalMatrixShader::onMakeContext( |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 41 | const ContextRec& rec, SkArenaAlloc* alloc) const |
| 42 | { |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame] | 43 | SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix()); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 44 | if (rec.fLocalMatrix) { |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame] | 45 | lm.writable()->preConcat(*rec.fLocalMatrix); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 46 | } |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame] | 47 | |
| 48 | ContextRec newRec(rec); |
| 49 | newRec.fLocalMatrix = lm; |
| 50 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 51 | return as_SB(fProxyShader)->makeContext(newRec, alloc); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 52 | } |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 53 | #endif |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 54 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 55 | SkImage* SkLocalMatrixShader::onIsAImage(SkMatrix* outMatrix, SkTileMode* mode) const { |
Matt Sarett | 547a727 | 2017-04-12 11:52:47 -0400 | [diff] [blame] | 56 | 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 Reed | 1386b2d | 2019-03-13 21:15:05 -0400 | [diff] [blame] | 66 | bool SkLocalMatrixShader::onAppendStages(const SkStageRec& rec) const { |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame] | 67 | SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix()); |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 68 | if (rec.fLocalM) { |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame] | 69 | lm.writable()->preConcat(*rec.fLocalM); |
Florin Malita | 882ccaf | 2017-01-27 10:51:58 -0500 | [diff] [blame] | 70 | } |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame] | 71 | |
Mike Reed | 1386b2d | 2019-03-13 21:15:05 -0400 | [diff] [blame] | 72 | SkStageRec newRec = rec; |
Florin Malita | 325ea32 | 2018-04-04 14:17:30 -0400 | [diff] [blame] | 73 | newRec.fLocalM = lm; |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 74 | return as_SB(fProxyShader)->appendStages(newRec); |
Florin Malita | 882ccaf | 2017-01-27 10:51:58 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Mike Klein | c376cb4 | 2020-03-10 15:40:23 -0500 | [diff] [blame] | 77 | |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame^] | 78 | skvm::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 Klein | c376cb4 | 2020-03-10 15:40:23 -0500 | [diff] [blame] | 83 | SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix()); |
| 84 | if (localM) { |
| 85 | lm.writable()->preConcat(*localM); |
| 86 | } |
Mike Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame^] | 87 | return as_SB(fProxyShader)->program(p, ctm,lm.get(), quality,dstCS, uniforms,alloc, x,y); |
Mike Klein | c376cb4 | 2020-03-10 15:40:23 -0500 | [diff] [blame] | 88 | } |
| 89 | |
reed | 150835e | 2016-03-10 06:36:49 -0800 | [diff] [blame] | 90 | sk_sp<SkShader> SkShader::makeWithLocalMatrix(const SkMatrix& localMatrix) const { |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 91 | if (localMatrix.isIdentity()) { |
reed | 150835e | 2016-03-10 06:36:49 -0800 | [diff] [blame] | 92 | return sk_ref_sp(const_cast<SkShader*>(this)); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 93 | } |
skia.committer@gmail.com | b2c82c9 | 2014-05-08 03:05:29 +0000 | [diff] [blame] | 94 | |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 95 | const SkMatrix* lm = &localMatrix; |
skia.committer@gmail.com | b2c82c9 | 2014-05-08 03:05:29 +0000 | [diff] [blame] | 96 | |
Ben Wagner | a8834bb | 2016-10-24 11:36:21 -0400 | [diff] [blame] | 97 | sk_sp<SkShader> baseShader; |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 98 | SkMatrix otherLocalMatrix; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 99 | sk_sp<SkShader> proxy(as_SB(this)->makeAsALocalMatrixShader(&otherLocalMatrix)); |
reed | f880e45 | 2015-12-30 13:39:41 -0800 | [diff] [blame] | 100 | if (proxy) { |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 101 | otherLocalMatrix.preConcat(localMatrix); |
| 102 | lm = &otherLocalMatrix; |
Ben Wagner | a8834bb | 2016-10-24 11:36:21 -0400 | [diff] [blame] | 103 | baseShader = proxy; |
| 104 | } else { |
| 105 | baseShader = sk_ref_sp(const_cast<SkShader*>(this)); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 106 | } |
skia.committer@gmail.com | b2c82c9 | 2014-05-08 03:05:29 +0000 | [diff] [blame] | 107 | |
Ben Wagner | a8834bb | 2016-10-24 11:36:21 -0400 | [diff] [blame] | 108 | return sk_make_sp<SkLocalMatrixShader>(std::move(baseShader), *lm); |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 109 | } |
Mike Reed | 121c2af | 2020-03-10 14:02:56 -0400 | [diff] [blame] | 110 | |
| 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 | */ |
| 118 | class SkCTMShader final : public SkShaderBase { |
| 119 | public: |
| 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 | |
| 133 | protected: |
| 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 Klein | 272c56b | 2020-03-10 14:24:35 -0500 | [diff] [blame] | 140 | 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 Reed | 6352f00 | 2020-03-14 23:30:10 -0400 | [diff] [blame^] | 153 | 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 Klein | 272c56b | 2020-03-10 14:24:35 -0500 | [diff] [blame] | 159 | } |
Mike Reed | 121c2af | 2020-03-10 14:02:56 -0400 | [diff] [blame] | 160 | |
| 161 | private: |
| 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 |
| 172 | std::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 | |
| 179 | sk_sp<SkFlattenable> SkCTMShader::CreateProc(SkReadBuffer& buffer) { |
| 180 | SkASSERT(false); |
| 181 | return nullptr; |
| 182 | } |
| 183 | |
Mike Reed | 121c2af | 2020-03-10 14:02:56 -0400 | [diff] [blame] | 184 | sk_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 | } |