• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 #ifndef SKSL_DSL_TYPE
9 #define SKSL_DSL_TYPE
10 
11 #include "include/core/SkSpan.h"
12 #include "include/private/SkSLString.h"
13 #include "include/sksl/DSLExpression.h"
14 #include "include/sksl/DSLModifiers.h"
15 
16 #include <cstdint>
17 
18 namespace SkSL {
19 
20 class Compiler;
21 class Type;
22 
23 namespace dsl {
24 
25 class DSLExpression;
26 class DSLField;
27 class DSLVarBase;
28 
29 enum TypeConstant : uint8_t {
30     kBool_Type,
31     kBool2_Type,
32     kBool3_Type,
33     kBool4_Type,
34     kHalf_Type,
35     kHalf2_Type,
36     kHalf3_Type,
37     kHalf4_Type,
38     kHalf2x2_Type,
39     kHalf3x2_Type,
40     kHalf4x2_Type,
41     kHalf2x3_Type,
42     kHalf3x3_Type,
43     kHalf4x3_Type,
44     kHalf2x4_Type,
45     kHalf3x4_Type,
46     kHalf4x4_Type,
47     kFloat_Type,
48     kFloat2_Type,
49     kFloat3_Type,
50     kFloat4_Type,
51     kFragmentProcessor_Type,
52     kFloat2x2_Type,
53     kFloat3x2_Type,
54     kFloat4x2_Type,
55     kFloat2x3_Type,
56     kFloat3x3_Type,
57     kFloat4x3_Type,
58     kFloat2x4_Type,
59     kFloat3x4_Type,
60     kFloat4x4_Type,
61     kInt_Type,
62     kInt2_Type,
63     kInt3_Type,
64     kInt4_Type,
65     kShader_Type,
66     kShort_Type,
67     kShort2_Type,
68     kShort3_Type,
69     kShort4_Type,
70     kUInt_Type,
71     kUInt2_Type,
72     kUInt3_Type,
73     kUInt4_Type,
74     kUShort_Type,
75     kUShort2_Type,
76     kUShort3_Type,
77     kUShort4_Type,
78     kVoid_Type,
79     kPoison_Type,
80 };
81 
82 class DSLType {
83 public:
DSLType(TypeConstant tc)84     DSLType(TypeConstant tc)
85         : fTypeConstant(tc) {}
86 
87     DSLType(const SkSL::Type* type);
88 
89     DSLType(std::string_view name);
90 
91     DSLType(std::string_view name,
92             DSLModifiers* modifiers,
93             PositionInfo pos = PositionInfo::Capture());
94 
95     /**
96      * Returns true if this type is a bool.
97      */
98     bool isBoolean() const;
99 
100     /**
101      * Returns true if this is a numeric scalar type.
102      */
103     bool isNumber() const;
104 
105     /**
106      * Returns true if this is a floating-point scalar type (float or half).
107      */
108     bool isFloat() const;
109 
110     /**
111      * Returns true if this is a signed scalar type (int or short).
112      */
113     bool isSigned() const;
114 
115     /**
116      * Returns true if this is an unsigned scalar type (uint or ushort).
117      */
118     bool isUnsigned() const;
119 
120     /**
121      * Returns true if this is a signed or unsigned integer.
122      */
123     bool isInteger() const;
124 
125     /**
126      * Returns true if this is a scalar type.
127      */
128     bool isScalar() const;
129 
130     /**
131      * Returns true if this is a vector type.
132      */
133     bool isVector() const;
134 
135     /**
136      * Returns true if this is a matrix type.
137      */
138     bool isMatrix() const;
139 
140     /**
141      * Returns true if this is a array type.
142      */
143     bool isArray() const;
144 
145     /**
146      * Returns true if this is a struct type.
147      */
148     bool isStruct() const;
149 
150     /**
151      * Returns true if this is a Skia object type (shader, colorFilter, blender).
152      */
153     bool isEffectChild() const;
154 
155     template<typename... Args>
Construct(DSLType type,DSLVarBase & var,Args &&...args)156     static DSLPossibleExpression Construct(DSLType type, DSLVarBase& var, Args&&... args) {
157         DSLExpression argArray[] = {var, args...};
158         return Construct(type, SkMakeSpan(argArray));
159     }
160 
161     template<typename... Args>
Construct(DSLType type,DSLExpression expr,Args &&...args)162     static DSLPossibleExpression Construct(DSLType type, DSLExpression expr, Args&&... args) {
163         DSLExpression argArray[] = {std::move(expr), std::move(args)...};
164         return Construct(type, SkMakeSpan(argArray));
165     }
166 
167     static DSLPossibleExpression Construct(DSLType type, SkSpan<DSLExpression> argArray);
168 
169 private:
170     const SkSL::Type& skslType() const;
171 
172     const SkSL::Type* fSkSLType = nullptr;
173 
174     TypeConstant fTypeConstant = kPoison_Type;
175 
176     friend DSLType Array(const DSLType& base, int count, PositionInfo pos);
177     friend DSLType Struct(std::string_view name, SkSpan<DSLField> fields, PositionInfo pos);
178     friend class DSLCore;
179     friend class DSLFunction;
180     friend class DSLVarBase;
181     friend class DSLWriter;
182     friend class SkSL::Compiler;
183 };
184 
185 #define TYPE(T)                                                                                    \
186     template<typename... Args>                                                                     \
187     DSLExpression T(Args&&... args) {                                                              \
188         return DSLType::Construct(k ## T ## _Type, std::forward<Args>(args)...);                   \
189     }
190 
191 #define VECTOR_TYPE(T)                                                                             \
192     TYPE(T)                                                                                        \
193     TYPE(T ## 2)                                                                                   \
194     TYPE(T ## 3)                                                                                   \
195     TYPE(T ## 4)
196 
197 #define MATRIX_TYPE(T)                                                                             \
198     TYPE(T ## 2x2)                                                                                 \
199     TYPE(T ## 3x2)                                                                                 \
200     TYPE(T ## 4x2)                                                                                 \
201     TYPE(T ## 2x3)                                                                                 \
202     TYPE(T ## 3x3)                                                                                 \
203     TYPE(T ## 4x3)                                                                                 \
204     TYPE(T ## 2x4)                                                                                 \
205     TYPE(T ## 3x4)                                                                                 \
206     TYPE(T ## 4x4)
207 
208 VECTOR_TYPE(Bool)
209 VECTOR_TYPE(Float)
210 VECTOR_TYPE(Half)
211 VECTOR_TYPE(Int)
212 VECTOR_TYPE(UInt)
213 VECTOR_TYPE(Short)
214 VECTOR_TYPE(UShort)
215 
216 MATRIX_TYPE(Float)
217 MATRIX_TYPE(Half)
218 
219 #undef TYPE
220 #undef VECTOR_TYPE
221 #undef MATRIX_TYPE
222 
223 DSLType Array(const DSLType& base, int count, PositionInfo pos = PositionInfo::Capture());
224 
225 class DSLField {
226 public:
227     DSLField(const DSLType type, std::string_view name,
228              PositionInfo pos = PositionInfo::Capture())
DSLField(DSLModifiers (),type,name,pos)229         : DSLField(DSLModifiers(), type, name, pos) {}
230 
231     DSLField(const DSLModifiers& modifiers, const DSLType type, std::string_view name,
232              PositionInfo pos = PositionInfo::Capture())
fModifiers(modifiers)233         : fModifiers(modifiers)
234         , fType(type)
235         , fName(name)
236         , fPosition(pos) {}
237 
238 private:
239     DSLModifiers fModifiers;
240     const DSLType fType;
241     std::string_view fName;
242     PositionInfo fPosition;
243 
244     friend class DSLCore;
245     friend DSLType Struct(std::string_view name, SkSpan<DSLField> fields, PositionInfo pos);
246 };
247 
248 DSLType Struct(std::string_view name, SkSpan<DSLField> fields,
249                PositionInfo pos = PositionInfo::Capture());
250 
251 template<typename... Field>
Struct(std::string_view name,Field...fields)252 DSLType Struct(std::string_view name, Field... fields) {
253     DSLField fieldTypes[] = {std::move(fields)...};
254     return Struct(name, SkMakeSpan(fieldTypes), PositionInfo());
255 }
256 
257 } // namespace dsl
258 
259 } // namespace SkSL
260 
261 #endif
262