1 /*
2  * Copyright 2021 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/core/SkFont.h"
10 #include "include/effects/SkRuntimeEffect.h"
11 #include "src/gpu/GrDirectContextPriv.h"
12 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
13 #include "src/gpu/ops/GrFillRectOp.h"
14 #include "src/sksl/dsl/priv/DSLFPs.h"
15 #include "src/sksl/dsl/priv/DSLWriter.h"
16 #include "src/sksl/ir/SkSLVariable.h"
17 #include "tools/ToolUtils.h"
18 
19 class SimpleDSLEffect : public GrFragmentProcessor {
20 public:
21     static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 100;
22 
SimpleDSLEffect()23     SimpleDSLEffect() : GrFragmentProcessor(CLASS_ID, kNone_OptimizationFlags) {
24     }
25 
name() const26     const char* name() const override { return "DSLEffect"; }
onGetGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder *) const27     void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
onIsEqual(const GrFragmentProcessor & that) const28     bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; }
clone() const29     std::unique_ptr<GrFragmentProcessor> clone() const override { return nullptr; }
30 
onMakeProgramImpl() const31     std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override {
32         class Impl : public GrGLSLFragmentProcessor {
33             void emitCode(EmitArgs& args) override {
34                 using namespace SkSL::dsl;
35                 StartFragmentProcessor(this, &args);
36 
37                 // Test for skbug.com/11384
38                 Var x(kInt_Type, 1);
39                 Declare(x);
40                 SkASSERT(DSLWriter::Var(x).initialValue()->description() == "1");
41 
42                 Var blueAlpha(kUniform_Modifier, kHalf2_Type, "blueAlpha");
43                 DeclareGlobal(blueAlpha);
44                 fBlueAlphaUniform = VarUniformHandle(blueAlpha);
45                 Var coords(kFloat4_Type, sk_FragCoord());
46                 Declare(coords);
47                 Return(Half4(Swizzle(coords, X, Y) / 100, blueAlpha));
48                 EndFragmentProcessor();
49             }
50 
51             void onSetData(const GrGLSLProgramDataManager& pdman,
52                            const GrFragmentProcessor& effect) override {
53                 pdman.set2f(fBlueAlphaUniform, 0.0, 1.0);
54             }
55 
56             GrGLSLProgramDataManager::UniformHandle fBlueAlphaUniform;
57         };
58         return std::make_unique<Impl>();
59     }
60 };
61 
62 DEF_SIMPLE_GPU_GM(simple_dsl_test, ctx, rtCtx, canvas, 100, 100) {
63     auto fp = std::make_unique<SimpleDSLEffect>();
64     GrPaint paint;
65     paint.setColorFragmentProcessor(std::move(fp));
66     rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
67                     SkRect::MakeIWH(100, 100));
68 }
69