blob: 341193057bd8d3b70ba765d59900438f70abfdac [file] [log] [blame]
Brian Osman273944a2020-05-29 16:44:26 -04001/*
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"
Robert Phillips550de7f2021-07-06 16:28:52 -040011#include "src/gpu/effects/GrTextureEffect.h"
Brian Osman273944a2020-05-29 16:44:26 -040012#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
13#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
14#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
Brian Osman273944a2020-05-29 16:44:26 -040015#include "src/sksl/SkSLUtil.h"
16
Brian Salomon3176e862021-08-09 11:23:04 -040017class GrGLSLMatrixEffect : public GrFragmentProcessor::ProgramImpl {
Brian Osman273944a2020-05-29 16:44:26 -040018public:
19 GrGLSLMatrixEffect() {}
20
21 void emitCode(EmitArgs& args) override {
Brian Osmanb1e9cb02021-05-21 12:03:51 -040022 fMatrixVar = args.fUniformHandler->addUniform(&args.fFp,
23 kFragment_GrShaderFlag,
24 kFloat3x3_GrSLType,
25 SkSL::SampleUsage::MatrixUniformName());
John Stiles52e9a7a2020-09-30 14:20:36 -040026 args.fFragBuilder->codeAppendf("return %s;\n",
27 this->invokeChildWithMatrix(0, args).c_str());
Brian Osman273944a2020-05-29 16:44:26 -040028 }
29
30private:
31 void onSetData(const GrGLSLProgramDataManager& pdman,
32 const GrFragmentProcessor& proc) override {
33 const GrMatrixEffect& mtx = proc.cast<GrMatrixEffect>();
Brian Salomon816db162021-04-16 08:39:35 -040034 if (auto te = mtx.childProcessor(0)->asTextureEffect()) {
35 SkMatrix m = te->coordAdjustmentMatrix();
36 m.preConcat(mtx.matrix());
37 pdman.setSkMatrix(fMatrixVar, m);
38 } else {
39 pdman.setSkMatrix(fMatrixVar, mtx.matrix());
40 }
Brian Osman273944a2020-05-29 16:44:26 -040041 }
42
43 UniformHandle fMatrixVar;
44};
45
Brian Salomon4bbb2902021-04-13 10:04:20 -040046std::unique_ptr<GrFragmentProcessor> GrMatrixEffect::Make(
47 const SkMatrix& matrix, std::unique_ptr<GrFragmentProcessor> child) {
Brian Salomon4bbb2902021-04-13 10:04:20 -040048 if (child->classID() == kGrMatrixEffect_ClassID) {
49 auto me = static_cast<GrMatrixEffect*>(child.get());
50 // registerChild's sample usage records whether the matrix used has perspective or not,
51 // so we can't add perspective to 'me' if it doesn't already have it.
52 if (me->matrix().hasPerspective() || !matrix.hasPerspective()) {
53 me->fMatrix.preConcat(matrix);
54 return child;
55 }
56 }
57 return std::unique_ptr<GrFragmentProcessor>(new GrMatrixEffect(matrix, std::move(child)));
58}
59
Brian Salomon3176e862021-08-09 11:23:04 -040060std::unique_ptr<GrFragmentProcessor::ProgramImpl> GrMatrixEffect::onMakeProgramImpl() const {
Brian Salomon18ab2032021-02-23 10:07:05 -050061 return std::make_unique<GrGLSLMatrixEffect>();
Brian Osman273944a2020-05-29 16:44:26 -040062}
63
Brian Salomon13b28732021-08-06 15:33:58 -040064void GrMatrixEffect::onAddToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {}
Brian Osman273944a2020-05-29 16:44:26 -040065
66bool GrMatrixEffect::onIsEqual(const GrFragmentProcessor& other) const {
67 const GrMatrixEffect& that = other.cast<GrMatrixEffect>();
68 if (fMatrix != that.fMatrix) return false;
69 return true;
70}
71
72GrMatrixEffect::GrMatrixEffect(const GrMatrixEffect& src)
John Stiles307f8f52021-08-09 15:36:59 -040073 : INHERITED(src)
74 , fMatrix(src.fMatrix) {}
Brian Osman273944a2020-05-29 16:44:26 -040075
76std::unique_ptr<GrFragmentProcessor> GrMatrixEffect::clone() const {
77 return std::unique_ptr<GrFragmentProcessor>(new GrMatrixEffect(*this));
78}