• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 #include "SkSLCPPCodeGenerator.h"
9 
10 #include "SkSLCompiler.h"
11 #include "SkSLCPPUniformCTypes.h"
12 #include "SkSLHCodeGenerator.h"
13 
14 #include <algorithm>
15 
16 namespace SkSL {
17 
needs_uniform_var(const Variable & var)18 static bool needs_uniform_var(const Variable& var) {
19     return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
20            var.fType.kind() != Type::kSampler_Kind;
21 }
22 
CPPCodeGenerator(const Context * context,const Program * program,ErrorReporter * errors,String name,OutputStream * out)23 CPPCodeGenerator::CPPCodeGenerator(const Context* context, const Program* program,
24                                    ErrorReporter* errors, String name, OutputStream* out)
25 : INHERITED(context, program, errors, out)
26 , fName(std::move(name))
27 , fFullName(String::printf("Gr%s", fName.c_str()))
28 , fSectionAndParameterHelper(*program, *errors) {
29     fLineEnding = "\\n";
30 }
31 
writef(const char * s,va_list va)32 void CPPCodeGenerator::writef(const char* s, va_list va) {
33     static constexpr int BUFFER_SIZE = 1024;
34     va_list copy;
35     va_copy(copy, va);
36     char buffer[BUFFER_SIZE];
37     int length = vsnprintf(buffer, BUFFER_SIZE, s, va);
38     if (length < BUFFER_SIZE) {
39         fOut->write(buffer, length);
40     } else {
41         std::unique_ptr<char[]> heap(new char[length + 1]);
42         vsprintf(heap.get(), s, copy);
43         fOut->write(heap.get(), length);
44     }
45     va_end(copy);
46 }
47 
writef(const char * s,...)48 void CPPCodeGenerator::writef(const char* s, ...) {
49     va_list va;
50     va_start(va, s);
51     this->writef(s, va);
52     va_end(va);
53 }
54 
writeHeader()55 void CPPCodeGenerator::writeHeader() {
56 }
57 
usesPrecisionModifiers() const58 bool CPPCodeGenerator::usesPrecisionModifiers() const {
59     return false;
60 }
61 
getTypeName(const Type & type)62 String CPPCodeGenerator::getTypeName(const Type& type) {
63     return type.name();
64 }
65 
writeBinaryExpression(const BinaryExpression & b,Precedence parentPrecedence)66 void CPPCodeGenerator::writeBinaryExpression(const BinaryExpression& b,
67                                              Precedence parentPrecedence) {
68     if (b.fOperator == Token::PERCENT) {
69         // need to use "%%" instead of "%" b/c the code will be inside of a printf
70         Precedence precedence = GetBinaryPrecedence(b.fOperator);
71         if (precedence >= parentPrecedence) {
72             this->write("(");
73         }
74         this->writeExpression(*b.fLeft, precedence);
75         this->write(" %% ");
76         this->writeExpression(*b.fRight, precedence);
77         if (precedence >= parentPrecedence) {
78             this->write(")");
79         }
80     } else {
81         INHERITED::writeBinaryExpression(b, parentPrecedence);
82     }
83 }
84 
writeIndexExpression(const IndexExpression & i)85 void CPPCodeGenerator::writeIndexExpression(const IndexExpression& i) {
86     const Expression& base = *i.fBase;
87     if (base.fKind == Expression::kVariableReference_Kind) {
88         int builtin = ((VariableReference&) base).fVariable.fModifiers.fLayout.fBuiltin;
89         if (SK_TRANSFORMEDCOORDS2D_BUILTIN == builtin) {
90             this->write("%s");
91             if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
92                 fErrors.error(i.fIndex->fOffset,
93                               "index into sk_TransformedCoords2D must be an integer literal");
94                 return;
95             }
96             int64_t index = ((IntLiteral&) *i.fIndex).fValue;
97             String name = "sk_TransformedCoords2D_" + to_string(index);
98             fFormatArgs.push_back(name + ".c_str()");
99             if (fWrittenTransformedCoords.find(index) == fWrittenTransformedCoords.end()) {
100                 addExtraEmitCodeLine("SkString " + name +
101                                      " = fragBuilder->ensureCoords2D(args.fTransformedCoords[" +
102                                      to_string(index) + "]);");
103                 fWrittenTransformedCoords.insert(index);
104             }
105             return;
106         } else if (SK_TEXTURESAMPLERS_BUILTIN == builtin) {
107             this->write("%s");
108             if (i.fIndex->fKind != Expression::kIntLiteral_Kind) {
109                 fErrors.error(i.fIndex->fOffset,
110                               "index into sk_TextureSamplers must be an integer literal");
111                 return;
112             }
113             int64_t index = ((IntLiteral&) *i.fIndex).fValue;
114             fFormatArgs.push_back("        fragBuilder->getProgramBuilder()->samplerVariable("
115                                             "args.fTexSamplers[" + to_string(index) + "]).c_str()");
116             return;
117         }
118     }
119     INHERITED::writeIndexExpression(i);
120 }
121 
default_value(const Type & type)122 static String default_value(const Type& type) {
123     if (type.fName == "bool") {
124         return "false";
125     }
126     switch (type.kind()) {
127         case Type::kScalar_Kind: return "0";
128         case Type::kVector_Kind: return type.name() + "(0)";
129         case Type::kMatrix_Kind: return type.name() + "(1)";
130         default: ABORT("unsupported default_value type\n");
131     }
132 }
133 
default_value(const Variable & var)134 static String default_value(const Variable& var) {
135     if (var.fModifiers.fLayout.fCType == SkSL::Layout::CType::kSkPMColor4f) {
136         return "{SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN}";
137     }
138     return default_value(var.fType);
139 }
140 
is_private(const Variable & var)141 static bool is_private(const Variable& var) {
142     return !(var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
143            !(var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
144            var.fStorage == Variable::kGlobal_Storage &&
145            var.fModifiers.fLayout.fBuiltin == -1;
146 }
147 
is_uniform_in(const Variable & var)148 static bool is_uniform_in(const Variable& var) {
149     return (var.fModifiers.fFlags & Modifiers::kUniform_Flag) &&
150            (var.fModifiers.fFlags & Modifiers::kIn_Flag) &&
151            var.fType.kind() != Type::kSampler_Kind;
152 }
153 
writeRuntimeValue(const Type & type,const Layout & layout,const String & cppCode)154 void CPPCodeGenerator::writeRuntimeValue(const Type& type, const Layout& layout,
155                                          const String& cppCode) {
156     if (type.isFloat()) {
157         this->write("%f");
158         fFormatArgs.push_back(cppCode);
159     } else if (type == *fContext.fInt_Type) {
160         this->write("%d");
161         fFormatArgs.push_back(cppCode);
162     } else if (type == *fContext.fBool_Type) {
163         this->write("%s");
164         fFormatArgs.push_back("(" + cppCode + " ? \"true\" : \"false\")");
165     } else if (type == *fContext.fFloat2_Type || type == *fContext.fHalf2_Type) {
166         this->write(type.name() + "(%f, %f)");
167         fFormatArgs.push_back(cppCode + ".fX");
168         fFormatArgs.push_back(cppCode + ".fY");
169     } else if (type == *fContext.fFloat4_Type || type == *fContext.fHalf4_Type) {
170         this->write(type.name() + "(%f, %f, %f, %f)");
171         switch (layout.fCType) {
172             case Layout::CType::kSkPMColor:
173                 fFormatArgs.push_back("SkGetPackedR32(" + cppCode + ") / 255.0");
174                 fFormatArgs.push_back("SkGetPackedG32(" + cppCode + ") / 255.0");
175                 fFormatArgs.push_back("SkGetPackedB32(" + cppCode + ") / 255.0");
176                 fFormatArgs.push_back("SkGetPackedA32(" + cppCode + ") / 255.0");
177                 break;
178             case Layout::CType::kSkPMColor4f:
179                 fFormatArgs.push_back(cppCode + ".fR");
180                 fFormatArgs.push_back(cppCode + ".fG");
181                 fFormatArgs.push_back(cppCode + ".fB");
182                 fFormatArgs.push_back(cppCode + ".fA");
183                 break;
184             case Layout::CType::kSkRect: // fall through
185             case Layout::CType::kDefault:
186                 fFormatArgs.push_back(cppCode + ".left()");
187                 fFormatArgs.push_back(cppCode + ".top()");
188                 fFormatArgs.push_back(cppCode + ".right()");
189                 fFormatArgs.push_back(cppCode + ".bottom()");
190                 break;
191             default:
192                 SkASSERT(false);
193         }
194     } else if (type.kind() == Type::kEnum_Kind) {
195         this->write("%d");
196         fFormatArgs.push_back("(int) " + cppCode);
197     } else if (type == *fContext.fInt4_Type ||
198                type == *fContext.fShort4_Type ||
199                type == *fContext.fByte4_Type) {
200         this->write(type.name() + "(%d, %d, %d, %d)");
201         fFormatArgs.push_back(cppCode + ".left()");
202         fFormatArgs.push_back(cppCode + ".top()");
203         fFormatArgs.push_back(cppCode + ".right()");
204         fFormatArgs.push_back(cppCode + ".bottom()");
205     } else {
206         printf("unsupported runtime value type '%s'\n", String(type.fName).c_str());
207         SkASSERT(false);
208     }
209 }
210 
writeVarInitializer(const Variable & var,const Expression & value)211 void CPPCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) {
212     if (is_private(var)) {
213         this->writeRuntimeValue(var.fType, var.fModifiers.fLayout, var.fName);
214     } else {
215         this->writeExpression(value, kTopLevel_Precedence);
216     }
217 }
218 
getSamplerHandle(const Variable & var)219 String CPPCodeGenerator::getSamplerHandle(const Variable& var) {
220     int samplerCount = 0;
221     for (const auto param : fSectionAndParameterHelper.getParameters()) {
222         if (&var == param) {
223             return "args.fTexSamplers[" + to_string(samplerCount) + "]";
224         }
225         if (param->fType.kind() == Type::kSampler_Kind) {
226             ++samplerCount;
227         }
228     }
229     ABORT("should have found sampler in parameters\n");
230 }
231 
writeIntLiteral(const IntLiteral & i)232 void CPPCodeGenerator::writeIntLiteral(const IntLiteral& i) {
233     this->write(to_string((int32_t) i.fValue));
234 }
235 
writeSwizzle(const Swizzle & swizzle)236 void CPPCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
237     if (fCPPMode) {
238         SkASSERT(swizzle.fComponents.size() == 1); // no support for multiple swizzle components yet
239         this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
240         switch (swizzle.fComponents[0]) {
241             case 0: this->write(".left()");   break;
242             case 1: this->write(".top()");    break;
243             case 2: this->write(".right()");  break;
244             case 3: this->write(".bottom()"); break;
245         }
246     } else {
247         INHERITED::writeSwizzle(swizzle);
248     }
249 }
250 
writeVariableReference(const VariableReference & ref)251 void CPPCodeGenerator::writeVariableReference(const VariableReference& ref) {
252     if (fCPPMode) {
253         this->write(ref.fVariable.fName);
254         return;
255     }
256     switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
257         case SK_INCOLOR_BUILTIN:
258             this->write("%s");
259             // EmitArgs.fInputColor is automatically set to half4(1) if
260             // no input was specified
261             fFormatArgs.push_back(String("args.fInputColor"));
262             break;
263         case SK_OUTCOLOR_BUILTIN:
264             this->write("%s");
265             fFormatArgs.push_back(String("args.fOutputColor"));
266             break;
267         case SK_WIDTH_BUILTIN:
268             this->write("sk_Width");
269             break;
270         case SK_HEIGHT_BUILTIN:
271             this->write("sk_Height");
272             break;
273         default:
274             if (ref.fVariable.fType.kind() == Type::kSampler_Kind) {
275                 this->write("%s");
276                 fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerVariable(" +
277                                       this->getSamplerHandle(ref.fVariable) + ").c_str()");
278                 return;
279             }
280             if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag) {
281                 this->write("%s");
282                 String name = ref.fVariable.fName;
283                 String var = String::printf("args.fUniformHandler->getUniformCStr(%sVar)",
284                                             HCodeGenerator::FieldName(name.c_str()).c_str());
285                 String code;
286                 if (ref.fVariable.fModifiers.fLayout.fWhen.size()) {
287                     code = String::printf("%sVar.isValid() ? %s : \"%s\"",
288                                           HCodeGenerator::FieldName(name.c_str()).c_str(),
289                                           var.c_str(),
290                                           default_value(ref.fVariable.fType).c_str());
291                 } else {
292                     code = var;
293                 }
294                 fFormatArgs.push_back(code);
295             } else if (SectionAndParameterHelper::IsParameter(ref.fVariable)) {
296                 String name(ref.fVariable.fName);
297                 this->writeRuntimeValue(ref.fVariable.fType, ref.fVariable.fModifiers.fLayout,
298                                         String::printf("_outer.%s()", name.c_str()).c_str());
299             } else {
300                 this->write(ref.fVariable.fName);
301             }
302     }
303 }
304 
writeIfStatement(const IfStatement & s)305 void CPPCodeGenerator::writeIfStatement(const IfStatement& s) {
306     if (s.fIsStatic) {
307         this->write("@");
308     }
309     INHERITED::writeIfStatement(s);
310 }
311 
writeReturnStatement(const ReturnStatement & s)312 void CPPCodeGenerator::writeReturnStatement(const ReturnStatement& s) {
313     if (fInMain) {
314         fErrors.error(s.fOffset, "fragmentProcessor main() may not contain return statements");
315     }
316     INHERITED::writeReturnStatement(s);
317 }
318 
writeSwitchStatement(const SwitchStatement & s)319 void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
320     if (s.fIsStatic) {
321         this->write("@");
322     }
323     INHERITED::writeSwitchStatement(s);
324 }
325 
writeFieldAccess(const FieldAccess & access)326 void CPPCodeGenerator::writeFieldAccess(const FieldAccess& access) {
327     if (access.fBase->fType.name() == "fragmentProcessor") {
328         // Special field access on fragment processors are converted into function calls on
329         // GrFragmentProcessor's getters.
330         if (access.fBase->fKind != Expression::kVariableReference_Kind) {
331             fErrors.error(access.fBase->fOffset, "fragmentProcessor must be a reference\n");
332             return;
333         }
334 
335         const Type::Field& field = fContext.fFragmentProcessor_Type->fields()[access.fFieldIndex];
336         int index = getChildFPIndex((const VariableReference&) *access.fBase);
337         String cppAccess = String::printf("_outer.childProcessor(%s).%s()",
338                                           to_string(index).c_str(), String(field.fName).c_str());
339 
340         if (fCPPMode) {
341             this->write(cppAccess.c_str());
342         } else {
343             writeRuntimeValue(*field.fType, Layout(), cppAccess);
344         }
345         return;
346     }
347     INHERITED::writeFieldAccess(access);
348 }
349 
getChildFPIndex(const VariableReference & reference) const350 int CPPCodeGenerator::getChildFPIndex(const VariableReference& reference) const {
351     int index = 0;
352     bool found = false;
353     for (const auto& p : fProgram) {
354         if (ProgramElement::kVar_Kind == p.fKind) {
355             const VarDeclarations& decls = (const VarDeclarations&) p;
356             for (const auto& raw : decls.fVars) {
357                 const VarDeclaration& decl = (VarDeclaration&) *raw;
358                 if (decl.fVar == &reference.fVariable) {
359                     found = true;
360                 } else if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
361                     ++index;
362                 }
363             }
364         }
365         if (found) {
366             break;
367         }
368     }
369     SkASSERT(found);
370     return index;
371 }
372 
writeFunctionCall(const FunctionCall & c)373 void CPPCodeGenerator::writeFunctionCall(const FunctionCall& c) {
374     if (c.fFunction.fBuiltin && c.fFunction.fName == "process") {
375         // Sanity checks that are detected by function definition in sksl_fp.inc
376         SkASSERT(c.fArguments.size() == 1 || c.fArguments.size() == 2);
377         SkASSERT("fragmentProcessor" == c.fArguments[0]->fType.name());
378 
379         // Actually fail during compilation if arguments with valid types are
380         // provided that are not variable references, since process() is a
381         // special function that impacts code emission.
382         if (c.fArguments[0]->fKind != Expression::kVariableReference_Kind) {
383             fErrors.error(c.fArguments[0]->fOffset,
384                     "process()'s fragmentProcessor argument must be a variable reference\n");
385             return;
386         }
387         if (c.fArguments.size() > 1) {
388             // Second argument must also be a half4 expression
389             SkASSERT("half4" == c.fArguments[1]->fType.name());
390         }
391         int index = getChildFPIndex((const VariableReference&) *c.fArguments[0]);
392 
393         // Start a new extra emit code section so that the emitted child processor can depend on
394         // sksl variables defined in earlier sksl code.
395         this->newExtraEmitCodeBlock();
396 
397         // Set to the empty string when no input color parameter should be emitted, which means this
398         // must be properly formatted with a prefixed comma when the parameter should be inserted
399         // into the emitChild() parameter list.
400         String inputArg;
401         if (c.fArguments.size() > 1) {
402             SkASSERT(c.fArguments.size() == 2);
403             // Use the emitChild() variant that accepts an input color, so convert the 2nd
404             // argument's expression into C++ code that produces sksl stored in an SkString.
405             String inputName = "_input" + to_string(index);
406             addExtraEmitCodeLine(convertSKSLExpressionToCPP(*c.fArguments[1], inputName));
407 
408             // emitChild() needs a char*
409             inputArg = ", " + inputName + ".c_str()";
410         }
411 
412         // Write the output handling after the possible input handling
413         String childName = "_child" + to_string(index);
414         addExtraEmitCodeLine("SkString " + childName + "(\"" + childName + "\");");
415         addExtraEmitCodeLine("this->emitChild(" + to_string(index) + inputArg +
416                              ", &" + childName + ", args);");
417 
418         this->write("%s");
419         fFormatArgs.push_back(childName + ".c_str()");
420         return;
421     }
422     INHERITED::writeFunctionCall(c);
423     if (c.fFunction.fBuiltin && c.fFunction.fName == "texture") {
424         this->write(".%s");
425         SkASSERT(c.fArguments.size() >= 1);
426         SkASSERT(c.fArguments[0]->fKind == Expression::kVariableReference_Kind);
427         String sampler = this->getSamplerHandle(((VariableReference&) *c.fArguments[0]).fVariable);
428         fFormatArgs.push_back("fragBuilder->getProgramBuilder()->samplerSwizzle(" + sampler +
429                               ").c_str()");
430     }
431 }
432 
writeFunction(const FunctionDefinition & f)433 void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
434     if (f.fDeclaration.fName == "main") {
435         fFunctionHeader = "";
436         OutputStream* oldOut = fOut;
437         StringStream buffer;
438         fOut = &buffer;
439         fInMain = true;
440         for (const auto& s : ((Block&) *f.fBody).fStatements) {
441             this->writeStatement(*s);
442             this->writeLine();
443         }
444         fInMain = false;
445 
446         fOut = oldOut;
447         this->write(fFunctionHeader);
448         this->write(buffer.str());
449     } else {
450         INHERITED::writeFunction(f);
451     }
452 }
453 
writeSetting(const Setting & s)454 void CPPCodeGenerator::writeSetting(const Setting& s) {
455     static constexpr const char* kPrefix = "sk_Args.";
456     if (!strncmp(s.fName.c_str(), kPrefix, strlen(kPrefix))) {
457         const char* name = s.fName.c_str() + strlen(kPrefix);
458         this->writeRuntimeValue(s.fType, Layout(), HCodeGenerator::FieldName(name).c_str());
459     } else {
460         this->write(s.fName.c_str());
461     }
462 }
463 
writeSection(const char * name,const char * prefix)464 bool CPPCodeGenerator::writeSection(const char* name, const char* prefix) {
465     const Section* s = fSectionAndParameterHelper.getSection(name);
466     if (s) {
467         this->writef("%s%s", prefix, s->fText.c_str());
468         return true;
469     }
470     return false;
471 }
472 
writeProgramElement(const ProgramElement & p)473 void CPPCodeGenerator::writeProgramElement(const ProgramElement& p) {
474     if (p.fKind == ProgramElement::kSection_Kind) {
475         return;
476     }
477     if (p.fKind == ProgramElement::kVar_Kind) {
478         const VarDeclarations& decls = (const VarDeclarations&) p;
479         if (!decls.fVars.size()) {
480             return;
481         }
482         const Variable& var = *((VarDeclaration&) *decls.fVars[0]).fVar;
483         if (var.fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kUniform_Flag) ||
484             -1 != var.fModifiers.fLayout.fBuiltin) {
485             return;
486         }
487     }
488     INHERITED::writeProgramElement(p);
489 }
490 
addUniform(const Variable & var)491 void CPPCodeGenerator::addUniform(const Variable& var) {
492     if (!needs_uniform_var(var)) {
493         return;
494     }
495     const char* precision;
496     if (var.fModifiers.fFlags & Modifiers::kHighp_Flag) {
497         precision = "kHigh_GrSLPrecision";
498     } else if (var.fModifiers.fFlags & Modifiers::kMediump_Flag) {
499         precision = "kMedium_GrSLPrecision";
500     } else if (var.fModifiers.fFlags & Modifiers::kLowp_Flag) {
501         precision = "kLow_GrSLPrecision";
502     } else {
503         precision = "kDefault_GrSLPrecision";
504     }
505     const char* type;
506     if (var.fType == *fContext.fFloat_Type) {
507         type = "kFloat_GrSLType";
508     } else if (var.fType == *fContext.fHalf_Type) {
509         type = "kHalf_GrSLType";
510     } else if (var.fType == *fContext.fFloat2_Type) {
511         type = "kFloat2_GrSLType";
512     } else if (var.fType == *fContext.fHalf2_Type) {
513         type = "kHalf2_GrSLType";
514     } else if (var.fType == *fContext.fFloat4_Type) {
515         type = "kFloat4_GrSLType";
516     } else if (var.fType == *fContext.fHalf4_Type) {
517         type = "kHalf4_GrSLType";
518     } else if (var.fType == *fContext.fFloat4x4_Type) {
519         type = "kFloat4x4_GrSLType";
520     } else if (var.fType == *fContext.fHalf4x4_Type) {
521         type = "kHalf4x4_GrSLType";
522     } else {
523         ABORT("unsupported uniform type: %s %s;\n", String(var.fType.fName).c_str(),
524               String(var.fName).c_str());
525     }
526     if (var.fModifiers.fLayout.fWhen.size()) {
527         this->writef("        if (%s) {\n    ", var.fModifiers.fLayout.fWhen.c_str());
528     }
529     String name(var.fName);
530     this->writef("        %sVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, %s, "
531                  "%s, \"%s\");\n", HCodeGenerator::FieldName(name.c_str()).c_str(), type, precision,
532                  name.c_str());
533     if (var.fModifiers.fLayout.fWhen.size()) {
534         this->write("        }\n");
535     }
536 }
537 
writeInputVars()538 void CPPCodeGenerator::writeInputVars() {
539 }
540 
writePrivateVars()541 void CPPCodeGenerator::writePrivateVars() {
542     for (const auto& p : fProgram) {
543         if (ProgramElement::kVar_Kind == p.fKind) {
544             const VarDeclarations& decls = (const VarDeclarations&) p;
545             for (const auto& raw : decls.fVars) {
546                 VarDeclaration& decl = (VarDeclaration&) *raw;
547                 if (is_private(*decl.fVar)) {
548                     if (decl.fVar->fType == *fContext.fFragmentProcessor_Type) {
549                         fErrors.error(decl.fOffset,
550                                       "fragmentProcessor variables must be declared 'in'");
551                         return;
552                     }
553                     this->writef("%s %s = %s;\n",
554                                  HCodeGenerator::FieldType(fContext, decl.fVar->fType,
555                                                            decl.fVar->fModifiers.fLayout).c_str(),
556                                  String(decl.fVar->fName).c_str(),
557                                  default_value(*decl.fVar).c_str());
558                 } else if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
559                     // An auto-tracked uniform in variable, so add a field to hold onto the prior
560                     // state. Note that tracked variables must be uniform in's and that is validated
561                     // before writePrivateVars() is called.
562                     const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *decl.fVar);
563                     SkASSERT(mapper && mapper->supportsTracking());
564 
565                     String name = HCodeGenerator::FieldName(String(decl.fVar->fName).c_str());
566                     // The member statement is different if the mapper reports a default value
567                     if (mapper->defaultValue().size() > 0) {
568                         this->writef("%s %sPrev = %s;\n",
569                                      Layout::CTypeToStr(mapper->ctype()), name.c_str(),
570                                      mapper->defaultValue().c_str());
571                     } else {
572                         this->writef("%s %sPrev;\n",
573                                      Layout::CTypeToStr(mapper->ctype()), name.c_str());
574                     }
575                 }
576             }
577         }
578     }
579 }
580 
writePrivateVarValues()581 void CPPCodeGenerator::writePrivateVarValues() {
582     for (const auto& p : fProgram) {
583         if (ProgramElement::kVar_Kind == p.fKind) {
584             const VarDeclarations& decls = (const VarDeclarations&) p;
585             for (const auto& raw : decls.fVars) {
586                 VarDeclaration& decl = (VarDeclaration&) *raw;
587                 if (is_private(*decl.fVar) && decl.fValue) {
588                     this->writef("%s = ", String(decl.fVar->fName).c_str());
589                     fCPPMode = true;
590                     this->writeExpression(*decl.fValue, kAssignment_Precedence);
591                     fCPPMode = false;
592                     this->write(";\n");
593                 }
594             }
595         }
596     }
597 }
598 
is_accessible(const Variable & var)599 static bool is_accessible(const Variable& var) {
600     return Type::kSampler_Kind != var.fType.kind() &&
601            Type::kOther_Kind != var.fType.kind();
602 }
603 
newExtraEmitCodeBlock()604 void CPPCodeGenerator::newExtraEmitCodeBlock() {
605     // This should only be called when emitting SKSL for emitCode(), which can be detected if the
606     // cpp buffer is not null, and the cpp buffer is not the current output.
607     SkASSERT(fCPPBuffer && fCPPBuffer != fOut);
608 
609     // Start a new block as an empty string
610     fExtraEmitCodeBlocks.push_back("");
611     // Mark its location in the output buffer, uses ${\d} for the token since ${} will not occur in
612     // valid sksl and makes detection trivial.
613     this->writef("${%zu}", fExtraEmitCodeBlocks.size() - 1);
614 }
615 
addExtraEmitCodeLine(const String & toAppend)616 void CPPCodeGenerator::addExtraEmitCodeLine(const String& toAppend) {
617     SkASSERT(fExtraEmitCodeBlocks.size() > 0);
618     String& currentBlock = fExtraEmitCodeBlocks[fExtraEmitCodeBlocks.size() - 1];
619     // Automatically add indentation and newline
620     currentBlock += "        " + toAppend + "\n";
621 }
622 
flushEmittedCode()623 void CPPCodeGenerator::flushEmittedCode() {
624     if (fCPPBuffer == nullptr) {
625         // Not actually within writeEmitCode() so nothing to flush
626         return;
627     }
628 
629     StringStream* skslBuffer = static_cast<StringStream*>(fOut);
630 
631     String sksl = skslBuffer->str();
632     // Empty the accumulation buffer since its current contents are consumed.
633     skslBuffer->reset();
634 
635     // Switch to the cpp buffer
636     fOut = fCPPBuffer;
637 
638     // Iterate through the sksl, keeping track of where the last statement ended (e.g. the latest
639     // encountered ';', '{', or '}'). If an extra emit code block token is encountered then the
640     // code from 0 to last statement end is sent to writeCodeAppend, the extra code block is
641     // appended to the cpp buffer, and then the sksl string is trimmed to start where the last
642     // statement left off (minus the encountered token).
643     size_t i = 0;
644     int flushPoint = -1;
645     int tokenStart = -1;
646     while (i < sksl.size()) {
647         if (tokenStart >= 0) {
648             // Looking for the end of the token
649             if (sksl[i] == '}') {
650                 // Must append the sksl from 0 to flushPoint (inclusive) then the extra code
651                 // accumulated in the block with index parsed from chars [tokenStart+2, i-1]
652                 String toFlush = String(sksl.c_str(), flushPoint + 1);
653                 // writeCodeAppend automatically removes the format args that it consumed, so
654                 // fFormatArgs will be in a valid state for any future sksl
655                 this->writeCodeAppend(toFlush);
656 
657                 int codeBlock = stoi(String(sksl.c_str() + tokenStart + 2, i - tokenStart - 2));
658                 SkASSERT(codeBlock < (int) fExtraEmitCodeBlocks.size());
659                 if (fExtraEmitCodeBlocks[codeBlock].size() > 0) {
660                     this->write(fExtraEmitCodeBlocks[codeBlock].c_str());
661                 }
662 
663                 // Now reset the sksl buffer to start after the flush point, but remove the token.
664                 String compacted = String(sksl.c_str() + flushPoint + 1,
665                                           tokenStart - flushPoint - 1);
666                 if (i < sksl.size() - 1) {
667                     compacted += String(sksl.c_str() + i + 1, sksl.size() - i - 1);
668                 }
669                 sksl = compacted;
670 
671                 // And reset iteration
672                 i = -1;
673                 flushPoint = -1;
674                 tokenStart = -1;
675             }
676         } else {
677             // Looking for the start of extra emit block tokens, and tracking when statements end
678             if (sksl[i] == ';' || sksl[i] == '{' || sksl[i] == '}') {
679                 flushPoint = i;
680             } else if (i < sksl.size() - 1 && sksl[i] == '$' && sksl[i + 1] == '{') {
681                 // found an extra emit code block token
682                 tokenStart = i++;
683             }
684         }
685         i++;
686     }
687 
688     // Once we've gone through the sksl string to this point, there are no remaining extra emit
689     // code blocks to interleave, so append the remainder as usual.
690     this->writeCodeAppend(sksl);
691 
692     // After appending, switch back to the emptied sksl buffer and reset the extra code blocks
693     fOut = skslBuffer;
694     fExtraEmitCodeBlocks.clear();
695 }
696 
writeCodeAppend(const String & code)697 void CPPCodeGenerator::writeCodeAppend(const String& code) {
698     // codeAppendf can only handle appending 1024 bytes at a time, so we need to break the string
699     // into chunks. Unfortunately we can't tell exactly how long the string is going to end up,
700     // because printf escape sequences get replaced by strings of unknown length, but keeping the
701     // format string below 512 bytes is probably safe.
702     static constexpr size_t maxChunkSize = 512;
703     size_t start = 0;
704     size_t index = 0;
705     size_t argStart = 0;
706     size_t argCount;
707     while (index < code.size()) {
708         argCount = 0;
709         this->write("        fragBuilder->codeAppendf(\"");
710         while (index < code.size() && index < start + maxChunkSize) {
711             if ('%' == code[index]) {
712                 if (index == start + maxChunkSize - 1 || index == code.size() - 1) {
713                     break;
714                 }
715                 if (code[index + 1] != '%') {
716                     ++argCount;
717                 }
718             } else if ('\\' == code[index] && index == start + maxChunkSize - 1) {
719                 // avoid splitting an escape sequence that happens to fall across a chunk boundary
720                 break;
721             }
722             ++index;
723         }
724         fOut->write(code.c_str() + start, index - start);
725         this->write("\"");
726         for (size_t i = argStart; i < argStart + argCount; ++i) {
727             this->writef(", %s", fFormatArgs[i].c_str());
728         }
729         this->write(");\n");
730         argStart += argCount;
731         start = index;
732     }
733 
734     // argStart is equal to the number of fFormatArgs that were consumed
735     // so they should be removed from the list
736     if (argStart > 0) {
737         fFormatArgs.erase(fFormatArgs.begin(), fFormatArgs.begin() + argStart);
738     }
739 }
740 
convertSKSLExpressionToCPP(const Expression & e,const String & cppVar)741 String CPPCodeGenerator::convertSKSLExpressionToCPP(const Expression& e,
742                                                     const String& cppVar) {
743     // To do this conversion, we temporarily switch the sksl output stream
744     // to an empty stringstream and reset the format args to empty.
745     OutputStream* oldSKSL = fOut;
746     StringStream exprBuffer;
747     fOut = &exprBuffer;
748 
749     std::vector<String> oldArgs(fFormatArgs);
750     fFormatArgs.clear();
751 
752     // Convert the argument expression into a format string and args
753     this->writeExpression(e, Precedence::kTopLevel_Precedence);
754     std::vector<String> newArgs(fFormatArgs);
755     String expr = exprBuffer.str();
756 
757     // After generating, restore the original output stream and format args
758     fFormatArgs = oldArgs;
759     fOut = oldSKSL;
760 
761     // The sksl written to exprBuffer is not processed by flushEmittedCode(), so any extra emit code
762     // block tokens won't get handled. So we need to strip them from the expression and stick them
763     // to the end of the original sksl stream.
764     String exprFormat = "";
765     int tokenStart = -1;
766     for (size_t i = 0; i < expr.size(); i++) {
767         if (tokenStart >= 0) {
768             if (expr[i] == '}') {
769                 // End of the token, so append the token to fOut
770                 fOut->write(expr.c_str() + tokenStart, i - tokenStart + 1);
771                 tokenStart = -1;
772             }
773         } else {
774             if (i < expr.size() - 1 && expr[i] == '$' && expr[i + 1] == '{') {
775                 tokenStart = i++;
776             } else {
777                 exprFormat += expr[i];
778             }
779         }
780     }
781 
782     // Now build the final C++ code snippet from the format string and args
783     String cppExpr;
784     if (newArgs.size() == 0) {
785         // This was a static expression, so we can simplify the input
786         // color declaration in the emitted code to just a static string
787         cppExpr = "SkString " + cppVar + "(\"" + exprFormat + "\");";
788     } else {
789         // String formatting must occur dynamically, so have the C++ declaration
790         // use SkStringPrintf with the format args that were accumulated
791         // when the expression was written.
792         cppExpr = "SkString " + cppVar + " = SkStringPrintf(\"" + exprFormat + "\"";
793         for (size_t i = 0; i < newArgs.size(); i++) {
794             cppExpr += ", " + newArgs[i];
795         }
796         cppExpr += ");";
797     }
798     return cppExpr;
799 }
800 
writeEmitCode(std::vector<const Variable * > & uniforms)801 bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
802     this->write("    void emitCode(EmitArgs& args) override {\n"
803                 "        GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n");
804     this->writef("        const %s& _outer = args.fFp.cast<%s>();\n"
805                  "        (void) _outer;\n",
806                  fFullName.c_str(), fFullName.c_str());
807     for (const auto& p : fProgram) {
808         if (ProgramElement::kVar_Kind == p.fKind) {
809             const VarDeclarations& decls = (const VarDeclarations&) p;
810             for (const auto& raw : decls.fVars) {
811                 VarDeclaration& decl = (VarDeclaration&) *raw;
812                 String nameString(decl.fVar->fName);
813                 const char* name = nameString.c_str();
814                 if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
815                     is_accessible(*decl.fVar)) {
816                     this->writef("        auto %s = _outer.%s();\n"
817                                  "        (void) %s;\n",
818                                  name, name, name);
819                 }
820             }
821         }
822     }
823     this->writePrivateVarValues();
824     for (const auto u : uniforms) {
825         this->addUniform(*u);
826     }
827     this->writeSection(EMIT_CODE_SECTION);
828 
829     // Save original buffer as the CPP buffer for flushEmittedCode()
830     fCPPBuffer = fOut;
831     StringStream skslBuffer;
832     fOut = &skslBuffer;
833 
834     this->newExtraEmitCodeBlock();
835     bool result = INHERITED::generateCode();
836     this->flushEmittedCode();
837 
838     // Then restore the original CPP buffer and close the function
839     fOut = fCPPBuffer;
840     fCPPBuffer = nullptr;
841     this->write("    }\n");
842     return result;
843 }
844 
writeSetData(std::vector<const Variable * > & uniforms)845 void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
846     const char* fullName = fFullName.c_str();
847     const Section* section = fSectionAndParameterHelper.getSection(SET_DATA_SECTION);
848     const char* pdman = section ? section->fArgument.c_str() : "pdman";
849     this->writef("    void onSetData(const GrGLSLProgramDataManager& %s, "
850                                     "const GrFragmentProcessor& _proc) override {\n",
851                  pdman);
852     bool wroteProcessor = false;
853     for (const auto u : uniforms) {
854         if (is_uniform_in(*u)) {
855             if (!wroteProcessor) {
856                 this->writef("        const %s& _outer = _proc.cast<%s>();\n", fullName, fullName);
857                 wroteProcessor = true;
858                 this->writef("        {\n");
859             }
860 
861             const UniformCTypeMapper* mapper = UniformCTypeMapper::Get(fContext, *u);
862             SkASSERT(mapper);
863 
864             String nameString(u->fName);
865             const char* name = nameString.c_str();
866 
867             // Switches for setData behavior in the generated code
868             bool conditionalUniform = u->fModifiers.fLayout.fWhen != "";
869             bool isTracked = u->fModifiers.fLayout.fFlags & Layout::kTracked_Flag;
870             bool needsValueDeclaration = isTracked || !mapper->canInlineUniformValue();
871 
872             String uniformName = HCodeGenerator::FieldName(name) + "Var";
873 
874             String indent = "        "; // 8 by default, 12 when nested for conditional uniforms
875             if (conditionalUniform) {
876                 // Add a pre-check to make sure the uniform was emitted
877                 // before trying to send any data to the GPU
878                 this->writef("        if (%s.isValid()) {\n", uniformName.c_str());
879                 indent += "    ";
880             }
881 
882             String valueVar = "";
883             if (needsValueDeclaration) {
884                 valueVar.appendf("%sValue", name);
885                 // Use AccessType since that will match the return type of _outer's public API.
886                 String valueType = HCodeGenerator::AccessType(fContext, u->fType,
887                                                               u->fModifiers.fLayout);
888                 this->writef("%s%s %s = _outer.%s();\n",
889                              indent.c_str(), valueType.c_str(), valueVar.c_str(), name);
890             } else {
891                 // Not tracked and the mapper only needs to use the value once
892                 // so send it a safe expression instead of the variable name
893                 valueVar.appendf("(_outer.%s())", name);
894             }
895 
896             if (isTracked) {
897                 SkASSERT(mapper->supportsTracking());
898 
899                 String prevVar = HCodeGenerator::FieldName(name) + "Prev";
900                 this->writef("%sif (%s) {\n"
901                              "%s    %s;\n"
902                              "%s    %s;\n"
903                              "%s}\n", indent.c_str(),
904                         mapper->dirtyExpression(valueVar, prevVar).c_str(), indent.c_str(),
905                         mapper->saveState(valueVar, prevVar).c_str(), indent.c_str(),
906                         mapper->setUniform(pdman, uniformName, valueVar).c_str(), indent.c_str());
907             } else {
908                 this->writef("%s%s;\n", indent.c_str(),
909                         mapper->setUniform(pdman, uniformName, valueVar).c_str());
910             }
911 
912             if (conditionalUniform) {
913                 // Close the earlier precheck block
914                 this->writef("        }\n");
915             }
916         }
917     }
918     if (wroteProcessor) {
919         this->writef("        }\n");
920     }
921     if (section) {
922         int samplerIndex = 0;
923         for (const auto& p : fProgram) {
924             if (ProgramElement::kVar_Kind == p.fKind) {
925                 const VarDeclarations& decls = (const VarDeclarations&) p;
926                 for (const auto& raw : decls.fVars) {
927                     VarDeclaration& decl = (VarDeclaration&) *raw;
928                     String nameString(decl.fVar->fName);
929                     const char* name = nameString.c_str();
930                     if (decl.fVar->fType.kind() == Type::kSampler_Kind) {
931                         this->writef("        GrSurfaceProxy& %sProxy = "
932                                      "*_outer.textureSampler(%d).proxy();\n",
933                                      name, samplerIndex);
934                         this->writef("        GrTexture& %s = *%sProxy.peekTexture();\n",
935                                      name, name);
936                         this->writef("        (void) %s;\n", name);
937                         ++samplerIndex;
938                     } else if (needs_uniform_var(*decl.fVar)) {
939                         this->writef("        UniformHandle& %s = %sVar;\n"
940                                      "        (void) %s;\n",
941                                      name, HCodeGenerator::FieldName(name).c_str(), name);
942                     } else if (SectionAndParameterHelper::IsParameter(*decl.fVar) &&
943                                decl.fVar->fType != *fContext.fFragmentProcessor_Type) {
944                         if (!wroteProcessor) {
945                             this->writef("        const %s& _outer = _proc.cast<%s>();\n", fullName,
946                                          fullName);
947                             wroteProcessor = true;
948                         }
949                         this->writef("        auto %s = _outer.%s();\n"
950                                      "        (void) %s;\n",
951                                      name, name, name);
952                     }
953                 }
954             }
955         }
956         this->writeSection(SET_DATA_SECTION);
957     }
958     this->write("    }\n");
959 }
960 
writeOnTextureSampler()961 void CPPCodeGenerator::writeOnTextureSampler() {
962     bool foundSampler = false;
963     for (const auto& param : fSectionAndParameterHelper.getParameters()) {
964         if (param->fType.kind() == Type::kSampler_Kind) {
965             if (!foundSampler) {
966                 this->writef(
967                         "const GrFragmentProcessor::TextureSampler& %s::onTextureSampler(int "
968                         "index) const {\n",
969                         fFullName.c_str());
970                 this->writef("    return IthTextureSampler(index, %s",
971                              HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
972                 foundSampler = true;
973             } else {
974                 this->writef(", %s",
975                              HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
976             }
977         }
978     }
979     if (foundSampler) {
980         this->write(");\n}\n");
981     }
982 }
983 
writeClone()984 void CPPCodeGenerator::writeClone() {
985     if (!this->writeSection(CLONE_SECTION)) {
986         if (fSectionAndParameterHelper.getSection(FIELDS_SECTION)) {
987             fErrors.error(0, "fragment processors with custom @fields must also have a custom"
988                              "@clone");
989         }
990         this->writef("%s::%s(const %s& src)\n"
991                      ": INHERITED(k%s_ClassID, src.optimizationFlags())", fFullName.c_str(),
992                      fFullName.c_str(), fFullName.c_str(), fFullName.c_str());
993         for (const auto& param : fSectionAndParameterHelper.getParameters()) {
994             if (param->fType == *fContext.fFragmentProcessor_Type) {
995                 continue;
996             }
997             String fieldName = HCodeGenerator::FieldName(String(param->fName).c_str());
998             this->writef("\n, %s(src.%s)",
999                          fieldName.c_str(),
1000                          fieldName.c_str());
1001         }
1002         const auto transforms = fSectionAndParameterHelper.getSections(COORD_TRANSFORM_SECTION);
1003         for (size_t i = 0; i < transforms.size(); ++i) {
1004             const Section& s = *transforms[i];
1005             String fieldName = HCodeGenerator::CoordTransformName(s.fArgument, i);
1006             this->writef("\n, %s(src.%s)", fieldName.c_str(), fieldName.c_str());
1007         }
1008         this->writef(" {\n");
1009         int childCount = 0;
1010         int samplerCount = 0;
1011         for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1012             if (param->fType.kind() == Type::kSampler_Kind) {
1013                 ++samplerCount;
1014             } else if (param->fType == *fContext.fFragmentProcessor_Type) {
1015                 this->writef("    this->registerChildProcessor(src.childProcessor(%d).clone());"
1016                              "\n", childCount++);
1017             }
1018         }
1019         if (samplerCount) {
1020             this->writef("     this->setTextureSamplerCnt(%d);", samplerCount);
1021         }
1022         for (size_t i = 0; i < transforms.size(); ++i) {
1023             const Section& s = *transforms[i];
1024             String fieldName = HCodeGenerator::CoordTransformName(s.fArgument, i);
1025             this->writef("    this->addCoordTransform(&%s);\n", fieldName.c_str());
1026         }
1027         this->write("}\n");
1028         this->writef("std::unique_ptr<GrFragmentProcessor> %s::clone() const {\n",
1029                      fFullName.c_str());
1030         this->writef("    return std::unique_ptr<GrFragmentProcessor>(new %s(*this));\n",
1031                      fFullName.c_str());
1032         this->write("}\n");
1033     }
1034 }
1035 
writeTest()1036 void CPPCodeGenerator::writeTest() {
1037     const Section* test = fSectionAndParameterHelper.getSection(TEST_CODE_SECTION);
1038     if (test) {
1039         this->writef(
1040                 "GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n"
1041                 "#if GR_TEST_UTILS\n"
1042                 "std::unique_ptr<GrFragmentProcessor> %s::TestCreate(GrProcessorTestData* %s) {\n",
1043                 fFullName.c_str(),
1044                 fFullName.c_str(),
1045                 test->fArgument.c_str());
1046         this->writeSection(TEST_CODE_SECTION);
1047         this->write("}\n"
1048                     "#endif\n");
1049     }
1050 }
1051 
writeGetKey()1052 void CPPCodeGenerator::writeGetKey() {
1053     this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
1054                                                 "GrProcessorKeyBuilder* b) const {\n",
1055                  fFullName.c_str());
1056     for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1057         String nameString(param->fName);
1058         const char* name = nameString.c_str();
1059         if (param->fModifiers.fLayout.fKey != Layout::kNo_Key &&
1060             (param->fModifiers.fFlags & Modifiers::kUniform_Flag)) {
1061             fErrors.error(param->fOffset,
1062                           "layout(key) may not be specified on uniforms");
1063         }
1064         switch (param->fModifiers.fLayout.fKey) {
1065             case Layout::kKey_Key:
1066                 if (param->fType == *fContext.fFloat4x4_Type) {
1067                     ABORT("no automatic key handling for float4x4\n");
1068                 } else if (param->fType == *fContext.fFloat2_Type) {
1069                     this->writef("    b->add32(%s.fX);\n",
1070                                  HCodeGenerator::FieldName(name).c_str());
1071                     this->writef("    b->add32(%s.fY);\n",
1072                                  HCodeGenerator::FieldName(name).c_str());
1073                 } else if (param->fType == *fContext.fFloat4_Type) {
1074                     this->writef("    b->add32(%s.x());\n",
1075                                  HCodeGenerator::FieldName(name).c_str());
1076                     this->writef("    b->add32(%s.y());\n",
1077                                  HCodeGenerator::FieldName(name).c_str());
1078                     this->writef("    b->add32(%s.width());\n",
1079                                  HCodeGenerator::FieldName(name).c_str());
1080                     this->writef("    b->add32(%s.height());\n",
1081                                  HCodeGenerator::FieldName(name).c_str());
1082                 } else {
1083                     this->writef("    b->add32((int32_t) %s);\n",
1084                                  HCodeGenerator::FieldName(name).c_str());
1085                 }
1086                 break;
1087             case Layout::kIdentity_Key:
1088                 if (param->fType.kind() != Type::kMatrix_Kind) {
1089                     fErrors.error(param->fOffset,
1090                                   "layout(key=identity) requires matrix type");
1091                 }
1092                 this->writef("    b->add32(%s.isIdentity() ? 1 : 0);\n",
1093                              HCodeGenerator::FieldName(name).c_str());
1094                 break;
1095             case Layout::kNo_Key:
1096                 break;
1097         }
1098     }
1099     this->write("}\n");
1100 }
1101 
generateCode()1102 bool CPPCodeGenerator::generateCode() {
1103     std::vector<const Variable*> uniforms;
1104     for (const auto& p : fProgram) {
1105         if (ProgramElement::kVar_Kind == p.fKind) {
1106             const VarDeclarations& decls = (const VarDeclarations&) p;
1107             for (const auto& raw : decls.fVars) {
1108                 VarDeclaration& decl = (VarDeclaration&) *raw;
1109                 if ((decl.fVar->fModifiers.fFlags & Modifiers::kUniform_Flag) &&
1110                            decl.fVar->fType.kind() != Type::kSampler_Kind) {
1111                     uniforms.push_back(decl.fVar);
1112                 }
1113 
1114                 if (is_uniform_in(*decl.fVar)) {
1115                     // Validate the "uniform in" declarations to make sure they are fully supported,
1116                     // instead of generating surprising C++
1117                     const UniformCTypeMapper* mapper =
1118                             UniformCTypeMapper::Get(fContext, *decl.fVar);
1119                     if (mapper == nullptr) {
1120                         fErrors.error(decl.fOffset, String(decl.fVar->fName)
1121                                 + "'s type is not supported for use as a 'uniform in'");
1122                         return false;
1123                     }
1124                     if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1125                         if (!mapper->supportsTracking()) {
1126                             fErrors.error(decl.fOffset, String(decl.fVar->fName)
1127                                     + "'s type does not support state tracking");
1128                             return false;
1129                         }
1130                     }
1131 
1132                 } else {
1133                     // If it's not a uniform_in, it's an error to be tracked
1134                     if (decl.fVar->fModifiers.fLayout.fFlags & Layout::kTracked_Flag) {
1135                         fErrors.error(decl.fOffset, "Non-'in uniforms' cannot be tracked");
1136                         return false;
1137                     }
1138                 }
1139             }
1140         }
1141     }
1142     const char* baseName = fName.c_str();
1143     const char* fullName = fFullName.c_str();
1144     this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str());
1145     this->writef(kFragmentProcessorHeader, fullName);
1146     this->writef("#include \"%s.h\"\n", fullName);
1147     this->writeSection(CPP_SECTION);
1148     this->writef("#include \"glsl/GrGLSLFragmentProcessor.h\"\n"
1149                  "#include \"glsl/GrGLSLFragmentShaderBuilder.h\"\n"
1150                  "#include \"glsl/GrGLSLProgramBuilder.h\"\n"
1151                  "#include \"GrTexture.h\"\n"
1152                  "#include \"SkSLCPP.h\"\n"
1153                  "#include \"SkSLUtil.h\"\n"
1154                  "class GrGLSL%s : public GrGLSLFragmentProcessor {\n"
1155                  "public:\n"
1156                  "    GrGLSL%s() {}\n",
1157                  baseName, baseName);
1158     bool result = this->writeEmitCode(uniforms);
1159     this->write("private:\n");
1160     this->writeSetData(uniforms);
1161     this->writePrivateVars();
1162     for (const auto& u : uniforms) {
1163         if (needs_uniform_var(*u) && !(u->fModifiers.fFlags & Modifiers::kIn_Flag)) {
1164             this->writef("    UniformHandle %sVar;\n",
1165                          HCodeGenerator::FieldName(String(u->fName).c_str()).c_str());
1166         }
1167     }
1168     for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1169         if (needs_uniform_var(*param)) {
1170             this->writef("    UniformHandle %sVar;\n",
1171                          HCodeGenerator::FieldName(String(param->fName).c_str()).c_str());
1172         }
1173     }
1174     this->writef("};\n"
1175                  "GrGLSLFragmentProcessor* %s::onCreateGLSLInstance() const {\n"
1176                  "    return new GrGLSL%s();\n"
1177                  "}\n",
1178                  fullName, baseName);
1179     this->writeGetKey();
1180     this->writef("bool %s::onIsEqual(const GrFragmentProcessor& other) const {\n"
1181                  "    const %s& that = other.cast<%s>();\n"
1182                  "    (void) that;\n",
1183                  fullName, fullName, fullName);
1184     for (const auto& param : fSectionAndParameterHelper.getParameters()) {
1185         if (param->fType == *fContext.fFragmentProcessor_Type) {
1186             continue;
1187         }
1188         String nameString(param->fName);
1189         const char* name = nameString.c_str();
1190         this->writef("    if (%s != that.%s) return false;\n",
1191                      HCodeGenerator::FieldName(name).c_str(),
1192                      HCodeGenerator::FieldName(name).c_str());
1193     }
1194     this->write("    return true;\n"
1195                 "}\n");
1196     this->writeClone();
1197     this->writeOnTextureSampler();
1198     this->writeTest();
1199     this->writeSection(CPP_END_SECTION);
1200 
1201     result &= 0 == fErrors.errorCount();
1202     return result;
1203 }
1204 
1205 } // namespace
1206