1 /*
2  * Copyright 2019 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 "gm/gm.h"
9 #include "include/effects/SkGradientShader.h"
10 #include "src/core/SkMatrixProvider.h"
11 #include "src/gpu/GrBitmapTextureMaker.h"
12 #include "src/gpu/GrDirectContextPriv.h"
13 #include "src/gpu/GrRenderTargetContextPriv.h"
14 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15 #include "src/gpu/ops/GrFillRectOp.h"
16 #include "tools/Resources.h"
17 
18 class SampleMatrixVariableEffect : public GrFragmentProcessor {
19 public:
20     static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 2;
21 
SampleMatrixVariableEffect(std::unique_ptr<GrFragmentProcessor> child,float xOffset,float yOffset)22     SampleMatrixVariableEffect(std::unique_ptr<GrFragmentProcessor> child,
23                                float xOffset,
24                                float yOffset)
25             : INHERITED(CLASS_ID, kNone_OptimizationFlags)
26             , fXOffset(xOffset)
27             , fYOffset(yOffset) {
28         this->registerChild(std::move(child), SkSL::SampleUsage::VariableMatrix());
29     }
30 
name() const31     const char* name() const override { return "SampleMatrixVariableEffect"; }
32 
clone() const33     std::unique_ptr<GrFragmentProcessor> clone() const override {
34         SkASSERT(false);
35         return nullptr;
36     }
37 
onGetGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder *) const38     void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
onIsEqual(const GrFragmentProcessor & that) const39     bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; }
40 
41 private:
42     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
43 
44     float fXOffset;
45     float fYOffset;
46 
47     using INHERITED = GrFragmentProcessor;
48     friend class GLSLSampleMatrixVariableEffect;
49 };
50 
51 class GLSLSampleMatrixVariableEffect : public GrGLSLFragmentProcessor {
emitCode(EmitArgs & args)52     void emitCode(EmitArgs& args) override {
53         auto& smve = args.fFp.cast<SampleMatrixVariableEffect>();
54         GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
55         SkString sample1 = this->invokeChildWithMatrix(0, args, "float3x3(1)");
56         SkString sample2 = this->invokeChildWithMatrix(0, args,
57                                                        SkStringPrintf("float3x3(1, -1, 0, 1, 0, 0, "
58                                                                       "%g, %g, 1)",
59                                                                       smve.fXOffset,
60                                                                       smve.fYOffset).c_str());
61         fragBuilder->codeAppendf("%s = (%s + %s) / 2;\n", args.fOutputColor, sample1.c_str(),
62                                  sample2.c_str());
63     }
64 };
65 
onCreateGLSLInstance() const66 GrGLSLFragmentProcessor* SampleMatrixVariableEffect::onCreateGLSLInstance() const {
67     return new GLSLSampleMatrixVariableEffect();
68 }
69 
70 DEF_SIMPLE_GPU_GM(sample_matrix_variable, ctx, rtCtx, canvas, 512, 256) {
71     auto draw = [rtCtx](std::unique_ptr<GrFragmentProcessor> baseFP, float ofsX, float ofsY,
__anon6141f0240102(std::unique_ptr<GrFragmentProcessor> baseFP, float ofsX, float ofsY, int tx, int ty) 72                         int tx, int ty) {
73         auto fp = std::unique_ptr<GrFragmentProcessor>(
74                 new SampleMatrixVariableEffect(std::move(baseFP), ofsX, ofsY));
75         GrPaint paint;
76         paint.setColorFragmentProcessor(std::move(fp));
77         rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::Translate(tx, ty),
78                         SkRect::MakeIWH(256, 256));
79     };
80 
81     {
82         SkBitmap bmp;
83         GetResourceAsBitmap("images/mandrill_256.png", &bmp);
84         GrBitmapTextureMaker maker(ctx, bmp, GrImageTexGenPolicy::kDraw);
85         auto view = maker.view(GrMipmapped::kNo);
86         std::unique_ptr<GrFragmentProcessor> imgFP =
87                 GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
88         draw(std::move(imgFP), -128, 256, 0, 0);
89     }
90 
91     {
92         static constexpr SkColor colors[] = { 0xff00ff00, 0xffff00ff };
93         const SkPoint pts[] = {{ 256, 0 }, { 512, 0 }};
94 
95         auto shader = SkGradientShader::MakeLinear(pts, colors, nullptr,
96                                                    SK_ARRAY_COUNT(colors),
97                                                    SkTileMode::kRepeat);
98         SkMatrix matrix;
99         SkSimpleMatrixProvider matrixProvider(matrix);
100         GrColorInfo colorInfo;
101         GrFPArgs args(ctx, matrixProvider, kHigh_SkFilterQuality, &colorInfo);
102         std::unique_ptr<GrFragmentProcessor> gradientFP = as_SB(shader)->asFragmentProcessor(args);
103         draw(std::move(gradientFP), -128, 256, 256, 0);
104     }
105 }
106