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 
8 #include "src/core/SkTLazy.h"
9 #include "src/shaders/SkLocalMatrixShader.h"
10 
11 #if SK_SUPPORT_GPU
12 #include "src/gpu/GrFragmentProcessor.h"
13 #endif
14 
15 #if SK_SUPPORT_GPU
asFragmentProcessor(const GrFPArgs & args) const16 std::unique_ptr<GrFragmentProcessor> SkLocalMatrixShader::asFragmentProcessor(
17         const GrFPArgs& args) const {
18     return as_SB(fProxyShader)->asFragmentProcessor(
19         GrFPArgs::WithPreLocalMatrix(args, this->getLocalMatrix()));
20 }
21 #endif
22 
CreateProc(SkReadBuffer & buffer)23 sk_sp<SkFlattenable> SkLocalMatrixShader::CreateProc(SkReadBuffer& buffer) {
24     SkMatrix lm;
25     buffer.readMatrix(&lm);
26     auto baseShader(buffer.readShader());
27     if (!baseShader) {
28         return nullptr;
29     }
30     return baseShader->makeWithLocalMatrix(lm);
31 }
32 
flatten(SkWriteBuffer & buffer) const33 void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const {
34     buffer.writeMatrix(this->getLocalMatrix());
35     buffer.writeFlattenable(fProxyShader.get());
36 }
37 
38 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
onMakeContext(const ContextRec & rec,SkArenaAlloc * alloc) const39 SkShaderBase::Context* SkLocalMatrixShader::onMakeContext(
40     const ContextRec& rec, SkArenaAlloc* alloc) const
41 {
42     SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix());
43     if (rec.fLocalMatrix) {
44         lm.writable()->preConcat(*rec.fLocalMatrix);
45     }
46 
47     ContextRec newRec(rec);
48     newRec.fLocalMatrix = lm;
49 
50     return as_SB(fProxyShader)->makeContext(newRec, alloc);
51 }
52 #endif
53 
onIsAImage(SkMatrix * outMatrix,SkTileMode * mode) const54 SkImage* SkLocalMatrixShader::onIsAImage(SkMatrix* outMatrix, SkTileMode* mode) const {
55     SkMatrix imageMatrix;
56     SkImage* image = fProxyShader->isAImage(&imageMatrix, mode);
57     if (image && outMatrix) {
58         // Local matrix must be applied first so it is on the right side of the concat.
59         *outMatrix = SkMatrix::Concat(imageMatrix, this->getLocalMatrix());
60     }
61 
62     return image;
63 }
64 
isAPicture(SkMatrix * matrix,SkTileMode tileModes[2],SkRect * tile) const65 SkPicture* SkLocalMatrixShader::isAPicture(SkMatrix* matrix,
66                                            SkTileMode tileModes[2],
67                                            SkRect* tile) const {
68     SkMatrix proxyMatrix;
69     SkPicture* picture = as_SB(fProxyShader)->isAPicture(&proxyMatrix, tileModes, tile);
70     if (picture && matrix) {
71         *matrix = SkMatrix::Concat(proxyMatrix, this->getLocalMatrix());
72     }
73     return picture;
74 }
75 
onAppendStages(const SkStageRec & rec) const76 bool SkLocalMatrixShader::onAppendStages(const SkStageRec& rec) const {
77     SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix());
78     if (rec.fLocalM) {
79         lm.writable()->preConcat(*rec.fLocalM);
80     }
81 
82     SkStageRec newRec = rec;
83     newRec.fLocalM = lm;
84     return as_SB(fProxyShader)->appendStages(newRec);
85 }
86 
makeWithLocalMatrix(const SkMatrix & localMatrix) const87 sk_sp<SkShader> SkShader::makeWithLocalMatrix(const SkMatrix& localMatrix) const {
88     if (localMatrix.isIdentity()) {
89         return sk_ref_sp(const_cast<SkShader*>(this));
90     }
91 
92     const SkMatrix* lm = &localMatrix;
93 
94     sk_sp<SkShader> baseShader;
95     SkMatrix otherLocalMatrix;
96     sk_sp<SkShader> proxy(as_SB(this)->makeAsALocalMatrixShader(&otherLocalMatrix));
97     if (proxy) {
98         otherLocalMatrix.preConcat(localMatrix);
99         lm = &otherLocalMatrix;
100         baseShader = proxy;
101     } else {
102         baseShader = sk_ref_sp(const_cast<SkShader*>(this));
103     }
104 
105     return sk_make_sp<SkLocalMatrixShader>(std::move(baseShader), *lm);
106 }
107