xref: /aosp_15_r20/external/skia/src/shaders/SkLocalMatrixShader.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #include "src/shaders/SkLocalMatrixShader.h"
8 
9 #include "src/core/SkReadBuffer.h"
10 #include "src/core/SkWriteBuffer.h"
11 
12 class SkImage;
13 enum class SkTileMode;
14 struct SkStageRec;
15 
isConstant() const16 bool SkLocalMatrixShader::isConstant() const {
17     return as_SB(fWrappedShader)->isConstant();
18 }
19 
asGradient(GradientInfo * info,SkMatrix * localMatrix) const20 SkShaderBase::GradientType SkLocalMatrixShader::asGradient(GradientInfo* info,
21                                                            SkMatrix* localMatrix) const {
22     GradientType type = as_SB(fWrappedShader)->asGradient(info, localMatrix);
23     if (type != SkShaderBase::GradientType::kNone && localMatrix) {
24         *localMatrix = ConcatLocalMatrices(fLocalMatrix, *localMatrix);
25     }
26     return type;
27 }
28 
CreateProc(SkReadBuffer & buffer)29 sk_sp<SkFlattenable> SkLocalMatrixShader::CreateProc(SkReadBuffer& buffer) {
30     SkMatrix lm;
31     buffer.readMatrix(&lm);
32     auto baseShader(buffer.readShader());
33     if (!baseShader) {
34         return nullptr;
35     }
36     return baseShader->makeWithLocalMatrix(lm);
37 }
38 
flatten(SkWriteBuffer & buffer) const39 void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const {
40     buffer.writeMatrix(fLocalMatrix);
41     buffer.writeFlattenable(fWrappedShader.get());
42 }
43 
44 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
onMakeContext(const ContextRec & rec,SkArenaAlloc * alloc) const45 SkShaderBase::Context* SkLocalMatrixShader::onMakeContext(const ContextRec& rec,
46                                                           SkArenaAlloc* alloc) const {
47     return as_SB(fWrappedShader)->makeContext(ContextRec::Concat(rec, fLocalMatrix), alloc);
48 }
49 #endif
50 
onIsAImage(SkMatrix * outMatrix,SkTileMode * mode) const51 SkImage* SkLocalMatrixShader::onIsAImage(SkMatrix* outMatrix, SkTileMode* mode) const {
52     SkMatrix imageMatrix;
53     SkImage* image = fWrappedShader->isAImage(&imageMatrix, mode);
54     if (image && outMatrix) {
55         *outMatrix = ConcatLocalMatrices(fLocalMatrix, imageMatrix);
56     }
57 
58     return image;
59 }
60 
onAsLuminanceColor(SkColor4f * color) const61 bool SkLocalMatrixShader::onAsLuminanceColor(SkColor4f* color) const {
62     return as_SB(fWrappedShader)->asLuminanceColor(color);
63 }
64 
appendStages(const SkStageRec & rec,const SkShaders::MatrixRec & mRec) const65 bool SkLocalMatrixShader::appendStages(const SkStageRec& rec,
66                                        const SkShaders::MatrixRec& mRec) const {
67     return as_SB(fWrappedShader)->appendStages(rec, mRec.concat(fLocalMatrix));
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 
SkCTMShader(sk_sp<SkShader> proxy,const SkMatrix & ctm)72 SkCTMShader::SkCTMShader(sk_sp<SkShader> proxy, const SkMatrix& ctm)
73         : fProxyShader(std::move(proxy)), fCTM(ctm) {}
74 
isConstant() const75 bool SkCTMShader::isConstant() const {
76     return as_SB(fProxyShader)->isConstant();
77 }
78 
asGradient(GradientInfo * info,SkMatrix * localMatrix) const79 SkShaderBase::GradientType SkCTMShader::asGradient(GradientInfo* info,
80                                                    SkMatrix* localMatrix) const {
81     return as_SB(fProxyShader)->asGradient(info, localMatrix);
82 }
83 
appendStages(const SkStageRec & rec,const SkShaders::MatrixRec &) const84 bool SkCTMShader::appendStages(const SkStageRec& rec, const SkShaders::MatrixRec&) const {
85     return as_SB(fProxyShader)->appendRootStages(rec, fCTM);
86 }
87 
CreateProc(SkReadBuffer & buffer)88 sk_sp<SkFlattenable> SkCTMShader::CreateProc(SkReadBuffer& buffer) {
89     SkASSERT(false);
90     return nullptr;
91 }
92