xref: /aosp_15_r20/external/skia/src/sksl/codegen/SkSLPipelineStageCodeGenerator.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2017 Google Inc.
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 #ifndef SKSL_PIPELINESTAGECODEGENERATOR
9 #define SKSL_PIPELINESTAGECODEGENERATOR
10 
11 #include "include/core/SkTypes.h"
12 
13 #include <string>
14 
15 namespace SkSL {
16 
17 struct Program;
18 class VarDeclaration;
19 
20 namespace PipelineStage {
21     class Callbacks {
22     public:
23         virtual ~Callbacks() = default;
24 
getMainName()25         virtual std::string getMainName() { return "main"; }
getMangledName(const char * name)26         virtual std::string getMangledName(const char* name) { return name; }
27         virtual void   defineFunction(const char* declaration, const char* body, bool isMain) = 0;
28         virtual void   declareFunction(const char* declaration) = 0;
29         virtual void   defineStruct(const char* definition) = 0;
30         virtual void   declareGlobal(const char* declaration) = 0;
31 
32         virtual std::string declareUniform(const VarDeclaration*) = 0;
33         virtual std::string sampleShader(int index, std::string coords) = 0;
34         virtual std::string sampleColorFilter(int index, std::string color) = 0;
35         virtual std::string sampleBlender(int index, std::string src, std::string dst) = 0;
36 
37         virtual std::string toLinearSrgb(std::string color) = 0;
38         virtual std::string fromLinearSrgb(std::string color) = 0;
39     };
40 
41     /*
42      * Processes 'program' for use in a GrFragmentProcessor, or other context that wants SkSL-like
43      * code as input. To support fragment processor usage, there are callbacks that allow elements
44      * to be declared programmatically and to rename those elements (mangling to avoid collisions).
45      *
46      * - Any reference to the main coords builtin variable will be replaced with 'sampleCoords'.
47      * - Any reference to the input color builtin variable will be replaced with 'inputColor'.
48      * - Any reference to the dest color builtin variable will be replaced with 'destColor'.
49      *   Dest-color is used in blend programs.
50      * - Each uniform variable declaration triggers a call to 'declareUniform', which should emit
51      *   the declaration, and return the (possibly different) name to use for the variable.
52      * - Each function definition triggers a call to 'defineFunction', which should emit the
53      *   definition, and return the (possibly different) name to use for calls to that function.
54      * - Each invocation of sample() triggers a call to 'sampleChild', which should return the full
55      *   text of the call expression.
56      */
57     void ConvertProgram(const Program& program,
58                         const char* sampleCoords,
59                         const char* inputColor,
60                         const char* destColor,
61                         Callbacks* callbacks);
62 }  // namespace PipelineStage
63 
64 }  // namespace SkSL
65 
66 #endif
67