1 /*
2 * Copyright 2020 Google LLC
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/gpu/ganesh/effects/GrMatrixEffect.h"
9
10 #include "include/core/SkString.h"
11 #include "include/private/gpu/ganesh/GrTypesPriv.h"
12 #include "src/core/SkSLTypeShared.h"
13 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
14 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
15 #include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h"
16 #include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h"
17
18 namespace skgpu { class KeyBuilder; }
19 struct GrShaderCaps;
20
Make(const SkMatrix & matrix,std::unique_ptr<GrFragmentProcessor> child)21 std::unique_ptr<GrFragmentProcessor> GrMatrixEffect::Make(
22 const SkMatrix& matrix, std::unique_ptr<GrFragmentProcessor> child) {
23 if (child->classID() == kGrMatrixEffect_ClassID) {
24 auto me = static_cast<GrMatrixEffect*>(child.get());
25 // registerChild's sample usage records whether the matrix used has perspective or not,
26 // so we can't add perspective to 'me' if it doesn't already have it.
27 if (me->fMatrix.hasPerspective() || !matrix.hasPerspective()) {
28 me->fMatrix.preConcat(matrix);
29 return child;
30 }
31 }
32 return std::unique_ptr<GrFragmentProcessor>(new GrMatrixEffect(matrix, std::move(child)));
33 }
34
onMakeProgramImpl() const35 std::unique_ptr<GrFragmentProcessor::ProgramImpl> GrMatrixEffect::onMakeProgramImpl() const {
36 class Impl : public ProgramImpl {
37 public:
38 void emitCode(EmitArgs& args) override {
39 fMatrixVar = args.fUniformHandler->addUniform(&args.fFp,
40 kFragment_GrShaderFlag,
41 SkSLType::kFloat3x3,
42 SkSL::SampleUsage::MatrixUniformName());
43 args.fFragBuilder->codeAppendf("return %s;\n",
44 this->invokeChildWithMatrix(0, args).c_str());
45 }
46
47 private:
48 void onSetData(const GrGLSLProgramDataManager& pdman,
49 const GrFragmentProcessor& proc) override {
50 const GrMatrixEffect& mtx = proc.cast<GrMatrixEffect>();
51 if (auto te = mtx.childProcessor(0)->asTextureEffect()) {
52 SkMatrix m = te->coordAdjustmentMatrix();
53 m.preConcat(mtx.fMatrix);
54 pdman.setSkMatrix(fMatrixVar, m);
55 } else {
56 pdman.setSkMatrix(fMatrixVar, mtx.fMatrix);
57 }
58 }
59
60 UniformHandle fMatrixVar;
61 };
62
63 return std::make_unique<Impl>();
64 }
65
onAddToKey(const GrShaderCaps & caps,skgpu::KeyBuilder * b) const66 void GrMatrixEffect::onAddToKey(const GrShaderCaps& caps, skgpu::KeyBuilder* b) const {}
67
onIsEqual(const GrFragmentProcessor & other) const68 bool GrMatrixEffect::onIsEqual(const GrFragmentProcessor& other) const {
69 const GrMatrixEffect& that = other.cast<GrMatrixEffect>();
70 if (fMatrix != that.fMatrix) return false;
71 return true;
72 }
73
GrMatrixEffect(const GrMatrixEffect & src)74 GrMatrixEffect::GrMatrixEffect(const GrMatrixEffect& src)
75 : INHERITED(src)
76 , fMatrix(src.fMatrix) {}
77
clone() const78 std::unique_ptr<GrFragmentProcessor> GrMatrixEffect::clone() const {
79 return std::unique_ptr<GrFragmentProcessor>(new GrMatrixEffect(*this));
80 }
81