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/private/SkSLString.h"
12 
13 #if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
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 
getMangledName(const char * name)25         virtual String getMangledName(const char* name) { return name; }
26         virtual void   defineFunction(const char* declaration, const char* body, bool isMain) = 0;
27         virtual void   defineStruct(const char* definition) = 0;
28         virtual void   declareGlobal(const char* declaration) = 0;
29 
30         virtual String declareUniform(const VarDeclaration*) = 0;
31         virtual String sampleChild(int index, String coords, String color) = 0;
32     };
33 
34     /*
35      * Processes 'program' for use in a GrFragmentProcessor, or other context that wants SkSL-like
36      * code as input. To support fragment processor usage, there are callbacks that allow elements
37      * to be declared programmatically and to rename those elements (mangling to avoid collisions).
38      *
39      * - Any reference to the main coords builtin variable will be replaced with 'sampleCoords'.
40      * - Any reference to the input color builtin variable will be replaced with 'inputColor'.
41      * - Each uniform variable declaration triggers a call to 'declareUniform', which should emit
42      *   the declaration, and return the (possibly different) name to use for the variable.
43      * - Each function definition triggers a call to 'defineFunction', which should emit the
44      *   definition, and return the (possibly different) name to use for calls to that function.
45      * - Each invocation of sample() triggers a call to 'sampleChild', which should return the full
46      *   text of the call expression.
47      */
48     void ConvertProgram(const Program& program,
49                         const char* sampleCoords,
50                         const char* inputColor,
51                         Callbacks* callbacks);
52 }  // namespace PipelineStage
53 
54 }  // namespace SkSL
55 
56 #endif
57 
58 #endif
59