1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2020 Google LLC
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "src/gpu/effects/GrMatrixEffect.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "src/gpu/GrTexture.h"
11cb93a386Sopenharmony_ci#include "src/gpu/effects/GrTextureEffect.h"
12cb93a386Sopenharmony_ci#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
13cb93a386Sopenharmony_ci#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
14cb93a386Sopenharmony_ci#include "src/sksl/SkSLUtil.h"
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_cistd::unique_ptr<GrFragmentProcessor> GrMatrixEffect::Make(
17cb93a386Sopenharmony_ci        const SkMatrix& matrix, std::unique_ptr<GrFragmentProcessor> child) {
18cb93a386Sopenharmony_ci    if (child->classID() == kGrMatrixEffect_ClassID) {
19cb93a386Sopenharmony_ci        auto me = static_cast<GrMatrixEffect*>(child.get());
20cb93a386Sopenharmony_ci        // registerChild's sample usage records whether the matrix used has perspective or not,
21cb93a386Sopenharmony_ci        // so we can't add perspective to 'me' if it doesn't already have it.
22cb93a386Sopenharmony_ci        if (me->fMatrix.hasPerspective() || !matrix.hasPerspective()) {
23cb93a386Sopenharmony_ci            me->fMatrix.preConcat(matrix);
24cb93a386Sopenharmony_ci            return child;
25cb93a386Sopenharmony_ci        }
26cb93a386Sopenharmony_ci    }
27cb93a386Sopenharmony_ci    return std::unique_ptr<GrFragmentProcessor>(new GrMatrixEffect(matrix, std::move(child)));
28cb93a386Sopenharmony_ci}
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_cistd::unique_ptr<GrFragmentProcessor::ProgramImpl> GrMatrixEffect::onMakeProgramImpl() const {
31cb93a386Sopenharmony_ci    class Impl : public ProgramImpl {
32cb93a386Sopenharmony_ci    public:
33cb93a386Sopenharmony_ci        void emitCode(EmitArgs& args) override {
34cb93a386Sopenharmony_ci            fMatrixVar = args.fUniformHandler->addUniform(&args.fFp,
35cb93a386Sopenharmony_ci                                                          kFragment_GrShaderFlag,
36cb93a386Sopenharmony_ci                                                          kFloat3x3_GrSLType,
37cb93a386Sopenharmony_ci                                                          SkSL::SampleUsage::MatrixUniformName());
38cb93a386Sopenharmony_ci            args.fFragBuilder->codeAppendf("return %s;\n",
39cb93a386Sopenharmony_ci                                           this->invokeChildWithMatrix(0, args).c_str());
40cb93a386Sopenharmony_ci        }
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ci    private:
43cb93a386Sopenharmony_ci        void onSetData(const GrGLSLProgramDataManager& pdman,
44cb93a386Sopenharmony_ci                       const GrFragmentProcessor& proc) override {
45cb93a386Sopenharmony_ci            const GrMatrixEffect& mtx = proc.cast<GrMatrixEffect>();
46cb93a386Sopenharmony_ci            if (auto te = mtx.childProcessor(0)->asTextureEffect()) {
47cb93a386Sopenharmony_ci                SkMatrix m = te->coordAdjustmentMatrix();
48cb93a386Sopenharmony_ci                m.preConcat(mtx.fMatrix);
49cb93a386Sopenharmony_ci                pdman.setSkMatrix(fMatrixVar, m);
50cb93a386Sopenharmony_ci            } else {
51cb93a386Sopenharmony_ci                pdman.setSkMatrix(fMatrixVar, mtx.fMatrix);
52cb93a386Sopenharmony_ci            }
53cb93a386Sopenharmony_ci        }
54cb93a386Sopenharmony_ci
55cb93a386Sopenharmony_ci        UniformHandle fMatrixVar;
56cb93a386Sopenharmony_ci    };
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci    return std::make_unique<Impl>();
59cb93a386Sopenharmony_ci}
60cb93a386Sopenharmony_ci
61cb93a386Sopenharmony_civoid GrMatrixEffect::onAddToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {}
62cb93a386Sopenharmony_ci
63cb93a386Sopenharmony_cibool GrMatrixEffect::onIsEqual(const GrFragmentProcessor& other) const {
64cb93a386Sopenharmony_ci    const GrMatrixEffect& that = other.cast<GrMatrixEffect>();
65cb93a386Sopenharmony_ci    if (fMatrix != that.fMatrix) return false;
66cb93a386Sopenharmony_ci    return true;
67cb93a386Sopenharmony_ci}
68cb93a386Sopenharmony_ci
69cb93a386Sopenharmony_ciGrMatrixEffect::GrMatrixEffect(const GrMatrixEffect& src)
70cb93a386Sopenharmony_ci        : INHERITED(src)
71cb93a386Sopenharmony_ci        , fMatrix(src.fMatrix) {}
72cb93a386Sopenharmony_ci
73cb93a386Sopenharmony_cistd::unique_ptr<GrFragmentProcessor> GrMatrixEffect::clone() const {
74cb93a386Sopenharmony_ci    return std::unique_ptr<GrFragmentProcessor>(new GrMatrixEffect(*this));
75cb93a386Sopenharmony_ci}
76