| 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/effects/GrMatrixEffect.h" | 
|---|
| 9 |  | 
|---|
| 10 | #include "src/gpu/GrTexture.h" | 
|---|
| 11 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" | 
|---|
| 12 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" | 
|---|
| 13 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" | 
|---|
| 14 | #include "src/sksl/SkSLCPP.h" | 
|---|
| 15 | #include "src/sksl/SkSLUtil.h" | 
|---|
| 16 |  | 
|---|
| 17 | class GrGLSLMatrixEffect : public GrGLSLFragmentProcessor { | 
|---|
| 18 | public: | 
|---|
| 19 | GrGLSLMatrixEffect() {} | 
|---|
| 20 |  | 
|---|
| 21 | void emitCode(EmitArgs& args) override { | 
|---|
| 22 | fMatrixVar = args.fUniformHandler->addUniform(&args.fFp, kFragment_GrShaderFlag, | 
|---|
| 23 | kFloat3x3_GrSLType, "matrix"); | 
|---|
| 24 | SkString child = this->invokeChildWithMatrix(0, args); | 
|---|
| 25 | args.fFragBuilder->codeAppendf( "%s = %s;\n", args.fOutputColor, child.c_str()); | 
|---|
| 26 | } | 
|---|
| 27 |  | 
|---|
| 28 | private: | 
|---|
| 29 | void onSetData(const GrGLSLProgramDataManager& pdman, | 
|---|
| 30 | const GrFragmentProcessor& proc) override { | 
|---|
| 31 | const GrMatrixEffect& mtx = proc.cast<GrMatrixEffect>(); | 
|---|
| 32 | pdman.setSkMatrix(fMatrixVar, mtx.matrix()); | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | UniformHandle fMatrixVar; | 
|---|
| 36 | }; | 
|---|
| 37 |  | 
|---|
| 38 | GrGLSLFragmentProcessor* GrMatrixEffect::onCreateGLSLInstance() const { | 
|---|
| 39 | return new GrGLSLMatrixEffect(); | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | void GrMatrixEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, | 
|---|
| 43 | GrProcessorKeyBuilder* b) const {} | 
|---|
| 44 |  | 
|---|
| 45 | bool GrMatrixEffect::onIsEqual(const GrFragmentProcessor& other) const { | 
|---|
| 46 | const GrMatrixEffect& that = other.cast<GrMatrixEffect>(); | 
|---|
| 47 | if (fMatrix != that.fMatrix) return false; | 
|---|
| 48 | return true; | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | GrMatrixEffect::GrMatrixEffect(const GrMatrixEffect& src) | 
|---|
| 52 | : INHERITED(kGrMatrixEffect_ClassID, src.optimizationFlags()) | 
|---|
| 53 | , fMatrix(src.fMatrix) { | 
|---|
| 54 | this->cloneAndRegisterAllChildProcessors(src); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | std::unique_ptr<GrFragmentProcessor> GrMatrixEffect::clone() const { | 
|---|
| 58 | return std::unique_ptr<GrFragmentProcessor>(new GrMatrixEffect(*this)); | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|