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 #ifndef GrMatrixEffect_DEFINED
9 #define GrMatrixEffect_DEFINED
10 
11 #include "include/core/SkM44.h"
12 #include "include/core/SkTypes.h"
13 
14 #include "src/gpu/GrFragmentProcessor.h"
15 
16 class GrMatrixEffect : public GrFragmentProcessor {
17 public:
Make(const SkMatrix & matrix,std::unique_ptr<GrFragmentProcessor> child)18     static std::unique_ptr<GrFragmentProcessor> Make(const SkMatrix& matrix,
19                                                      std::unique_ptr<GrFragmentProcessor> child) {
20         if (matrix.isIdentity()) {
21             return child;
22         }
23         return std::unique_ptr<GrFragmentProcessor>(new GrMatrixEffect(matrix, std::move(child)));
24     }
25 
usesExplicitReturn()26     bool usesExplicitReturn() const override { return true; }
27     std::unique_ptr<GrFragmentProcessor> clone() const override;
name()28     const char* name() const override { return "MatrixEffect"; }
matrix()29     const SkMatrix& matrix() const { return fMatrix; }
30 
31 private:
32     GrMatrixEffect(const GrMatrixEffect& src);
33 
GrMatrixEffect(SkMatrix matrix,std::unique_ptr<GrFragmentProcessor> child)34     GrMatrixEffect(SkMatrix matrix, std::unique_ptr<GrFragmentProcessor> child)
35             : INHERITED(kGrMatrixEffect_ClassID, ProcessorOptimizationFlags(child.get()))
36             , fMatrix(matrix) {
37         SkASSERT(child);
38         this->registerChild(std::move(child),
39                             SkSL::SampleUsage::UniformMatrix("matrix", matrix.hasPerspective()));
40     }
41 
42     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
43     void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
44     bool onIsEqual(const GrFragmentProcessor&) const override;
constantOutputForConstantInput(const SkPMColor4f & inputColor)45     SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inputColor) const override {
46         return ConstantOutputForConstantInput(this->childProcessor(0), inputColor);
47     }
48 
49     SkMatrix fMatrix;
50 
51     GR_DECLARE_FRAGMENT_PROCESSOR_TEST
52     using INHERITED = GrFragmentProcessor;
53 };
54 #endif
55