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 #include "src/sksl/SkSLDehydrator.h"
9
10 #include <map>
11
12 #include "include/private/SkSLProgramElement.h"
13 #include "include/private/SkSLStatement.h"
14 #include "include/private/SkSLSymbol.h"
15 #include "src/sksl/SkSLRehydrator.h"
16 #include "src/sksl/ir/SkSLBinaryExpression.h"
17 #include "src/sksl/ir/SkSLBreakStatement.h"
18 #include "src/sksl/ir/SkSLConstructor.h"
19 #include "src/sksl/ir/SkSLConstructorArray.h"
20 #include "src/sksl/ir/SkSLConstructorArrayCast.h"
21 #include "src/sksl/ir/SkSLConstructorCompound.h"
22 #include "src/sksl/ir/SkSLConstructorCompoundCast.h"
23 #include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
24 #include "src/sksl/ir/SkSLConstructorMatrixResize.h"
25 #include "src/sksl/ir/SkSLConstructorScalarCast.h"
26 #include "src/sksl/ir/SkSLConstructorSplat.h"
27 #include "src/sksl/ir/SkSLConstructorStruct.h"
28 #include "src/sksl/ir/SkSLContinueStatement.h"
29 #include "src/sksl/ir/SkSLDiscardStatement.h"
30 #include "src/sksl/ir/SkSLDoStatement.h"
31 #include "src/sksl/ir/SkSLExpressionStatement.h"
32 #include "src/sksl/ir/SkSLField.h"
33 #include "src/sksl/ir/SkSLFieldAccess.h"
34 #include "src/sksl/ir/SkSLForStatement.h"
35 #include "src/sksl/ir/SkSLFunctionCall.h"
36 #include "src/sksl/ir/SkSLFunctionDeclaration.h"
37 #include "src/sksl/ir/SkSLFunctionDefinition.h"
38 #include "src/sksl/ir/SkSLFunctionPrototype.h"
39 #include "src/sksl/ir/SkSLIfStatement.h"
40 #include "src/sksl/ir/SkSLIndexExpression.h"
41 #include "src/sksl/ir/SkSLInlineMarker.h"
42 #include "src/sksl/ir/SkSLInterfaceBlock.h"
43 #include "src/sksl/ir/SkSLLiteral.h"
44 #include "src/sksl/ir/SkSLPostfixExpression.h"
45 #include "src/sksl/ir/SkSLPrefixExpression.h"
46 #include "src/sksl/ir/SkSLReturnStatement.h"
47 #include "src/sksl/ir/SkSLSetting.h"
48 #include "src/sksl/ir/SkSLStructDefinition.h"
49 #include "src/sksl/ir/SkSLSwitchCase.h"
50 #include "src/sksl/ir/SkSLSwitchStatement.h"
51 #include "src/sksl/ir/SkSLSwizzle.h"
52 #include "src/sksl/ir/SkSLSymbolTable.h"
53 #include "src/sksl/ir/SkSLTernaryExpression.h"
54 #include "src/sksl/ir/SkSLUnresolvedFunction.h"
55 #include "src/sksl/ir/SkSLVarDeclarations.h"
56 #include "src/sksl/ir/SkSLVariable.h"
57
58 namespace SkSL {
59
60 static constexpr int HEADER_SIZE = 2;
61
62 class AutoDehydratorSymbolTable {
63 public:
AutoDehydratorSymbolTable(Dehydrator * dehydrator,const std::shared_ptr<SymbolTable> & symbols)64 AutoDehydratorSymbolTable(Dehydrator* dehydrator, const std::shared_ptr<SymbolTable>& symbols)
65 : fDehydrator(dehydrator) {
66 dehydrator->fSymbolMap.emplace_back();
67 if (symbols) {
68 dehydrator->write(*symbols);
69 } else {
70 dehydrator->writeCommand(Rehydrator::kVoid_Command);
71 }
72 }
73
~AutoDehydratorSymbolTable()74 ~AutoDehydratorSymbolTable() {
75 fDehydrator->fSymbolMap.pop_back();
76 }
77
78 private:
79 Dehydrator* fDehydrator;
80 };
81
writeId(const Symbol * s)82 void Dehydrator::writeId(const Symbol* s) {
83 uint16_t id = this->symbolId(s);
84 if (id) {
85 this->writeU16(id);
86 } else {
87 this->writeU16(Rehydrator::kBuiltin_Symbol);
88 this->write(s->name());
89 }
90 }
91
write(Layout l)92 void Dehydrator::write(Layout l) {
93 if (l == Layout()) {
94 this->writeCommand(Rehydrator::kDefaultLayout_Command);
95 } else if (l == Layout::builtin(l.fBuiltin)) {
96 this->writeCommand(Rehydrator::kBuiltinLayout_Command);
97 this->writeS16(l.fBuiltin);
98 } else {
99 this->writeCommand(Rehydrator::kLayout_Command);
100 fBody.write32(l.fFlags);
101 this->writeS8(l.fLocation);
102 this->writeS16(l.fOffset);
103 this->writeS16(l.fBinding);
104 this->writeS8(l.fIndex);
105 this->writeS8(l.fSet);
106 this->writeS16(l.fBuiltin);
107 this->writeS8(l.fInputAttachmentIndex);
108 }
109 }
110
write(Modifiers m)111 void Dehydrator::write(Modifiers m) {
112 if (m == Modifiers()) {
113 this->writeCommand(Rehydrator::kDefaultModifiers_Command);
114 } else {
115 if (m.fFlags <= 255) {
116 this->writeCommand(Rehydrator::kModifiers8Bit_Command);
117 this->write(m.fLayout);
118 this->writeU8(m.fFlags);
119 } else {
120 this->writeCommand(Rehydrator::kModifiers_Command);
121 this->write(m.fLayout);
122 this->writeS32(m.fFlags);
123 }
124 }
125 }
126
write(std::string_view s)127 void Dehydrator::write(std::string_view s) {
128 this->write(std::string(s));
129 }
130
write(std::string s)131 void Dehydrator::write(std::string s) {
132 auto found = fStrings.find(s);
133 int offset;
134 if (found == fStrings.end()) {
135 offset = fStringBuffer.bytesWritten() + HEADER_SIZE;
136 fStrings.insert({ s, offset });
137 SkASSERT(s.length() <= 255);
138 fStringBreaks.add(fStringBuffer.bytesWritten());
139 fStringBuffer.write8(s.length());
140 fStringBuffer.writeString(s);
141 } else {
142 offset = found->second;
143 }
144 this->writeU16(offset);
145 }
146
write(const Symbol & s)147 void Dehydrator::write(const Symbol& s) {
148 uint16_t id = this->symbolId(&s);
149 if (id) {
150 this->writeCommand(Rehydrator::kSymbolRef_Command);
151 this->writeU16(id);
152 return;
153 }
154 switch (s.kind()) {
155 case Symbol::Kind::kFunctionDeclaration: {
156 this->allocSymbolId(&s);
157 const FunctionDeclaration& f = s.as<FunctionDeclaration>();
158 this->writeCommand(Rehydrator::kFunctionDeclaration_Command);
159 this->writeId(&f);
160 this->write(f.modifiers());
161 this->write(f.name());
162 this->writeU8(f.parameters().size());
163 for (const Variable* p : f.parameters()) {
164 this->writeU16(this->symbolId(p));
165 }
166 this->write(f.returnType());
167 break;
168 }
169 case Symbol::Kind::kUnresolvedFunction: {
170 this->allocSymbolId(&s);
171 const UnresolvedFunction& f = s.as<UnresolvedFunction>();
172 this->writeCommand(Rehydrator::kUnresolvedFunction_Command);
173 this->writeId(&f);
174 this->writeU8(f.functions().size());
175 for (const FunctionDeclaration* funcDecl : f.functions()) {
176 this->write(*funcDecl);
177 }
178 break;
179 }
180 case Symbol::Kind::kType: {
181 const Type& t = s.as<Type>();
182 switch (t.typeKind()) {
183 case Type::TypeKind::kArray:
184 this->allocSymbolId(&s);
185 this->writeCommand(Rehydrator::kArrayType_Command);
186 this->writeId(&t);
187 this->write(t.componentType());
188 this->writeS8(t.columns());
189 break;
190 case Type::TypeKind::kStruct:
191 this->allocSymbolId(&s);
192 this->writeCommand(Rehydrator::kStructType_Command);
193 this->writeId(&t);
194 this->write(t.name());
195 this->writeU8(t.fields().size());
196 for (const Type::Field& f : t.fields()) {
197 this->write(f.fModifiers);
198 this->write(f.fName);
199 this->write(*f.fType);
200 }
201 this->writeU8(t.isInterfaceBlock());
202 break;
203 default:
204 this->writeCommand(Rehydrator::kSymbolRef_Command);
205 this->writeU16(Rehydrator::kBuiltin_Symbol);
206 this->write(t.name());
207 break;
208 }
209 break;
210 }
211 case Symbol::Kind::kVariable: {
212 this->allocSymbolId(&s);
213 const Variable& v = s.as<Variable>();
214 this->writeCommand(Rehydrator::kVariable_Command);
215 this->writeId(&v);
216 this->write(v.modifiers());
217 this->write(v.name());
218 this->write(v.type());
219 this->writeU8((int8_t) v.storage());
220 break;
221 }
222 case Symbol::Kind::kField: {
223 const Field& f = s.as<Field>();
224 this->writeCommand(Rehydrator::kField_Command);
225 this->writeU16(this->symbolId(&f.owner()));
226 this->writeU8(f.fieldIndex());
227 break;
228 }
229 case Symbol::Kind::kExternal:
230 SkASSERT(false);
231 break;
232 }
233 }
234
write(const SymbolTable & symbols)235 void Dehydrator::write(const SymbolTable& symbols) {
236 this->writeCommand(Rehydrator::kSymbolTable_Command);
237 this->writeU8(symbols.isBuiltin());
238 this->writeU16(symbols.fOwnedSymbols.size());
239
240 // write owned symbols
241 for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) {
242 this->write(*s);
243 }
244
245 // write symbols
246 this->writeU16(symbols.fSymbols.count());
247 std::map<std::string_view, const Symbol*> ordered;
248 symbols.foreach([&](std::string_view name, const Symbol* symbol) {
249 ordered.insert({name, symbol});
250 });
251 for (std::pair<std::string_view, const Symbol*> p : ordered) {
252 bool found = false;
253 for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) {
254 if (symbols.fOwnedSymbols[i].get() == p.second) {
255 fCommandBreaks.add(fBody.bytesWritten());
256 this->writeU16(i);
257 found = true;
258 break;
259 }
260 }
261 if (!found) {
262 // we should only fail to find builtin types
263 SkASSERT(p.second->is<Type>() && p.second->as<Type>().isInBuiltinTypes());
264 this->writeU16(Rehydrator::kBuiltin_Symbol);
265 this->write(p.second->name());
266 }
267 }
268 }
269
270
writeExpressionSpan(const SkSpan<const std::unique_ptr<Expression>> & span)271 void Dehydrator::writeExpressionSpan(const SkSpan<const std::unique_ptr<Expression>>& span) {
272 this->writeU8(span.size());
273 for (const auto& expr : span) {
274 this->write(expr.get());
275 }
276 }
277
write(const Expression * e)278 void Dehydrator::write(const Expression* e) {
279 if (e) {
280 switch (e->kind()) {
281 case Expression::Kind::kBinary: {
282 const BinaryExpression& b = e->as<BinaryExpression>();
283 this->writeCommand(Rehydrator::kBinary_Command);
284 this->write(b.left().get());
285 this->writeU8((int) b.getOperator().kind());
286 this->write(b.right().get());
287 break;
288 }
289 case Expression::Kind::kChildCall:
290 SkDEBUGFAIL("unimplemented--not expected to be used from within an include file");
291 break;
292
293 case Expression::Kind::kCodeString:
294 SkDEBUGFAIL("shouldn't be able to receive kCodeString here");
295 break;
296
297 case Expression::Kind::kConstructorArray:
298 this->writeCommand(Rehydrator::kConstructorArray_Command);
299 this->write(e->type());
300 this->writeExpressionSpan(e->as<ConstructorArray>().argumentSpan());
301 break;
302
303 case Expression::Kind::kConstructorArrayCast:
304 this->writeCommand(Rehydrator::kConstructorArrayCast_Command);
305 this->write(e->type());
306 this->writeExpressionSpan(e->as<ConstructorArrayCast>().argumentSpan());
307 break;
308
309 case Expression::Kind::kConstructorCompound:
310 this->writeCommand(Rehydrator::kConstructorCompound_Command);
311 this->write(e->type());
312 this->writeExpressionSpan(e->as<ConstructorCompound>().argumentSpan());
313 break;
314
315 case Expression::Kind::kConstructorCompoundCast:
316 this->writeCommand(Rehydrator::kConstructorCompoundCast_Command);
317 this->write(e->type());
318 this->writeExpressionSpan(e->as<ConstructorCompoundCast>().argumentSpan());
319 break;
320
321 case Expression::Kind::kConstructorDiagonalMatrix:
322 this->writeCommand(Rehydrator::kConstructorDiagonalMatrix_Command);
323 this->write(e->type());
324 this->writeExpressionSpan(e->as<ConstructorDiagonalMatrix>().argumentSpan());
325 break;
326
327 case Expression::Kind::kConstructorMatrixResize:
328 this->writeCommand(Rehydrator::kConstructorMatrixResize_Command);
329 this->write(e->type());
330 this->writeExpressionSpan(e->as<ConstructorMatrixResize>().argumentSpan());
331 break;
332
333 case Expression::Kind::kConstructorScalarCast:
334 this->writeCommand(Rehydrator::kConstructorScalarCast_Command);
335 this->write(e->type());
336 this->writeExpressionSpan(e->as<ConstructorScalarCast>().argumentSpan());
337 break;
338
339 case Expression::Kind::kConstructorSplat:
340 this->writeCommand(Rehydrator::kConstructorSplat_Command);
341 this->write(e->type());
342 this->writeExpressionSpan(e->as<ConstructorSplat>().argumentSpan());
343 break;
344
345 case Expression::Kind::kConstructorStruct:
346 this->writeCommand(Rehydrator::kConstructorStruct_Command);
347 this->write(e->type());
348 this->writeExpressionSpan(e->as<ConstructorStruct>().argumentSpan());
349 break;
350
351 case Expression::Kind::kExternalFunctionCall:
352 case Expression::Kind::kExternalFunctionReference:
353 SkDEBUGFAIL("unimplemented--not expected to be used from within an include file");
354 break;
355
356 case Expression::Kind::kFieldAccess: {
357 const FieldAccess& f = e->as<FieldAccess>();
358 this->writeCommand(Rehydrator::kFieldAccess_Command);
359 this->write(f.base().get());
360 this->writeU8(f.fieldIndex());
361 this->writeU8((int8_t) f.ownerKind());
362 break;
363 }
364 case Expression::Kind::kFunctionCall: {
365 const FunctionCall& f = e->as<FunctionCall>();
366 this->writeCommand(Rehydrator::kFunctionCall_Command);
367 this->write(f.type());
368 this->writeId(&f.function());
369 this->writeU8(f.arguments().size());
370 for (const auto& a : f.arguments()) {
371 this->write(a.get());
372 }
373 break;
374 }
375 case Expression::Kind::kIndex: {
376 const IndexExpression& i = e->as<IndexExpression>();
377 this->writeCommand(Rehydrator::kIndex_Command);
378 this->write(i.base().get());
379 this->write(i.index().get());
380 break;
381 }
382 case Expression::Kind::kLiteral: {
383 const Literal& l = e->as<Literal>();
384 if (l.type().isFloat()) {
385 float value = l.floatValue();
386 int32_t floatBits;
387 memcpy(&floatBits, &value, sizeof(floatBits));
388 this->writeCommand(Rehydrator::kFloatLiteral_Command);
389 this->write(l.type());
390 this->writeS32(floatBits);
391 } else if (l.type().isBoolean()) {
392 this->writeCommand(Rehydrator::kBoolLiteral_Command);
393 this->writeU8(l.boolValue());
394 } else {
395 SkASSERT(l.type().isInteger());
396 this->writeCommand(Rehydrator::kIntLiteral_Command);
397 this->write(l.type());
398 if (l.type().isUnsigned()) {
399 this->writeU32(l.intValue());
400 } else {
401 this->writeS32(l.intValue());
402 }
403 }
404 break;
405 }
406 case Expression::Kind::kPostfix: {
407 const PostfixExpression& p = e->as<PostfixExpression>();
408 this->writeCommand(Rehydrator::kPostfix_Command);
409 this->writeU8((int) p.getOperator().kind());
410 this->write(p.operand().get());
411 break;
412 }
413 case Expression::Kind::kPrefix: {
414 const PrefixExpression& p = e->as<PrefixExpression>();
415 this->writeCommand(Rehydrator::kPrefix_Command);
416 this->writeU8((int) p.getOperator().kind());
417 this->write(p.operand().get());
418 break;
419 }
420 case Expression::Kind::kSetting: {
421 const Setting& s = e->as<Setting>();
422 this->writeCommand(Rehydrator::kSetting_Command);
423 this->write(s.name());
424 break;
425 }
426 case Expression::Kind::kSwizzle: {
427 const Swizzle& s = e->as<Swizzle>();
428 this->writeCommand(Rehydrator::kSwizzle_Command);
429 this->write(s.base().get());
430 this->writeU8(s.components().size());
431 for (int c : s.components()) {
432 this->writeU8(c);
433 }
434 break;
435 }
436 case Expression::Kind::kTernary: {
437 const TernaryExpression& t = e->as<TernaryExpression>();
438 this->writeCommand(Rehydrator::kTernary_Command);
439 this->write(t.test().get());
440 this->write(t.ifTrue().get());
441 this->write(t.ifFalse().get());
442 break;
443 }
444 case Expression::Kind::kVariableReference: {
445 const VariableReference& v = e->as<VariableReference>();
446 this->writeCommand(Rehydrator::kVariableReference_Command);
447 this->writeId(v.variable());
448 this->writeU8((int8_t) v.refKind());
449 break;
450 }
451 case Expression::Kind::kFunctionReference:
452 case Expression::Kind::kMethodReference:
453 case Expression::Kind::kPoison:
454 case Expression::Kind::kTypeReference:
455 SkDEBUGFAIL("this expression shouldn't appear in finished code");
456 break;
457 }
458 } else {
459 this->writeCommand(Rehydrator::kVoid_Command);
460 }
461 }
462
write(const Statement * s)463 void Dehydrator::write(const Statement* s) {
464 if (s) {
465 switch (s->kind()) {
466 case Statement::Kind::kBlock: {
467 const Block& b = s->as<Block>();
468 this->writeCommand(Rehydrator::kBlock_Command);
469 AutoDehydratorSymbolTable symbols(this, b.symbolTable());
470 this->writeU8(b.children().size());
471 for (const std::unique_ptr<Statement>& blockStmt : b.children()) {
472 this->write(blockStmt.get());
473 }
474 this->writeU8(b.isScope());
475 break;
476 }
477 case Statement::Kind::kBreak:
478 this->writeCommand(Rehydrator::kBreak_Command);
479 break;
480 case Statement::Kind::kContinue:
481 this->writeCommand(Rehydrator::kContinue_Command);
482 break;
483 case Statement::Kind::kDiscard:
484 this->writeCommand(Rehydrator::kDiscard_Command);
485 break;
486 case Statement::Kind::kDo: {
487 const DoStatement& d = s->as<DoStatement>();
488 this->writeCommand(Rehydrator::kDo_Command);
489 this->write(d.statement().get());
490 this->write(d.test().get());
491 break;
492 }
493 case Statement::Kind::kExpression: {
494 const ExpressionStatement& e = s->as<ExpressionStatement>();
495 this->writeCommand(Rehydrator::kExpressionStatement_Command);
496 this->write(e.expression().get());
497 break;
498 }
499 case Statement::Kind::kFor: {
500 const ForStatement& f = s->as<ForStatement>();
501 this->writeCommand(Rehydrator::kFor_Command);
502 AutoDehydratorSymbolTable symbols(this, f.symbols());
503 this->write(f.initializer().get());
504 this->write(f.test().get());
505 this->write(f.next().get());
506 this->write(f.statement().get());
507 break;
508 }
509 case Statement::Kind::kIf: {
510 const IfStatement& i = s->as<IfStatement>();
511 this->writeCommand(Rehydrator::kIf_Command);
512 this->writeU8(i.isStatic());
513 this->write(i.test().get());
514 this->write(i.ifTrue().get());
515 this->write(i.ifFalse().get());
516 break;
517 }
518 case Statement::Kind::kInlineMarker: {
519 const InlineMarker& i = s->as<InlineMarker>();
520 this->writeCommand(Rehydrator::kInlineMarker_Command);
521 this->writeId(&i.function());
522 break;
523 }
524 case Statement::Kind::kNop:
525 this->writeCommand(Rehydrator::kNop_Command);
526 break;
527 case Statement::Kind::kReturn: {
528 const ReturnStatement& r = s->as<ReturnStatement>();
529 this->writeCommand(Rehydrator::kReturn_Command);
530 this->write(r.expression().get());
531 break;
532 }
533 case Statement::Kind::kSwitch: {
534 const SwitchStatement& ss = s->as<SwitchStatement>();
535 this->writeCommand(Rehydrator::kSwitch_Command);
536 this->writeU8(ss.isStatic());
537 AutoDehydratorSymbolTable symbols(this, ss.symbols());
538 this->write(ss.value().get());
539 this->writeU8(ss.cases().size());
540 for (const std::unique_ptr<Statement>& stmt : ss.cases()) {
541 const SwitchCase& sc = stmt->as<SwitchCase>();
542 if (sc.isDefault()) {
543 this->writeU8(1);
544 } else {
545 this->writeU8(0);
546 this->writeS32(sc.value());
547 }
548 this->write(sc.statement().get());
549 }
550 break;
551 }
552 case Statement::Kind::kSwitchCase:
553 SkDEBUGFAIL("SwitchCase statements shouldn't appear here");
554 break;
555 case Statement::Kind::kVarDeclaration: {
556 const VarDeclaration& v = s->as<VarDeclaration>();
557 this->writeCommand(Rehydrator::kVarDeclaration_Command);
558 this->writeU16(this->symbolId(&v.var()));
559 this->write(v.baseType());
560 this->writeU8(v.arraySize());
561 this->write(v.value().get());
562 break;
563 }
564 }
565 } else {
566 this->writeCommand(Rehydrator::kVoid_Command);
567 }
568 }
569
write(const ProgramElement & e)570 void Dehydrator::write(const ProgramElement& e) {
571 switch (e.kind()) {
572 case ProgramElement::Kind::kExtension:
573 SkASSERT(false);
574 break;
575 case ProgramElement::Kind::kFunction: {
576 const FunctionDefinition& f = e.as<FunctionDefinition>();
577 this->writeCommand(Rehydrator::kFunctionDefinition_Command);
578 this->writeU16(this->symbolId(&f.declaration()));
579 this->write(f.body().get());
580 break;
581 }
582 case ProgramElement::Kind::kFunctionPrototype: {
583 const FunctionPrototype& f = e.as<FunctionPrototype>();
584 if (!f.isBuiltin()) {
585 this->writeCommand(Rehydrator::kFunctionPrototype_Command);
586 this->writeU16(this->symbolId(&f.declaration()));
587 }
588 break;
589 }
590 case ProgramElement::Kind::kInterfaceBlock: {
591 const InterfaceBlock& i = e.as<InterfaceBlock>();
592 this->writeCommand(Rehydrator::kInterfaceBlock_Command);
593 this->write(i.variable());
594 this->write(i.typeName());
595 this->write(i.instanceName());
596 this->writeU8(i.arraySize());
597 break;
598 }
599 case ProgramElement::Kind::kModifiers:
600 SkASSERT(false);
601 break;
602 case ProgramElement::Kind::kStructDefinition: {
603 const StructDefinition& structDef = e.as<StructDefinition>();
604 this->writeCommand(Rehydrator::kStructDefinition_Command);
605 this->write(structDef.type());
606 break;
607 }
608 case ProgramElement::Kind::kGlobalVar: {
609 const GlobalVarDeclaration& v = e.as<GlobalVarDeclaration>();
610 this->writeCommand(Rehydrator::kGlobalVar_Command);
611 this->write(v.declaration().get());
612 break;
613 }
614 }
615 }
616
write(const std::vector<std::unique_ptr<ProgramElement>> & elements)617 void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) {
618 this->writeCommand(Rehydrator::kElements_Command);
619 for (const auto& e : elements) {
620 this->write(*e);
621 }
622 this->writeCommand(Rehydrator::kElementsComplete_Command);
623 }
624
write(const Program & program)625 void Dehydrator::write(const Program& program) {
626 this->writeCommand(Rehydrator::kProgram_Command);
627 this->writeU8((int)program.fConfig->fKind);
628 this->write(*program.fSymbols);
629
630 // Write the elements
631 this->writeCommand(Rehydrator::kElements_Command);
632 for (const auto& e : program.fSharedElements) {
633 this->writeCommand(Rehydrator::kSharedFunction_Command);
634 const FunctionDefinition& f = e->as<FunctionDefinition>();
635 const FunctionDeclaration& decl = f.declaration();
636 this->writeU8(decl.parameters().size());
637 for (const Variable* param : decl.parameters()) {
638 this->write(*param);
639 }
640 this->write(f.declaration());
641 this->write(*e);
642 }
643 for (const auto& e : program.fOwnedElements) {
644 this->write(*e);
645 }
646 this->writeCommand(Rehydrator::kElementsComplete_Command);
647
648 // Write the inputs
649 struct KnownSkSLProgramInputs { bool useRTFlipUniform; };
650 // Since it would be easy to forget to update this code in the face of Inputs changes and any
651 // resulting bugs could be very subtle, assert that the struct hasn't changed:
652 static_assert(sizeof(SkSL::Program::Inputs) == sizeof(KnownSkSLProgramInputs));
653 this->writeU8(program.fInputs.fUseFlipRTUniform);
654 }
655
finish(OutputStream & out)656 void Dehydrator::finish(OutputStream& out) {
657 out.write16(Rehydrator::kVersion);
658 std::string stringBuffer = fStringBuffer.str();
659 std::string commandBuffer = fBody.str();
660 out.write16(fStringBuffer.str().size());
661 fStringBufferStart = 4;
662 out.writeString(stringBuffer);
663 fCommandStart = fStringBufferStart + stringBuffer.size();
664 out.writeString(commandBuffer);
665 }
666
prefixAtOffset(size_t byte)667 const char* Dehydrator::prefixAtOffset(size_t byte) {
668 if (byte >= fCommandStart) {
669 return fCommandBreaks.contains(byte - fCommandStart) ? "\n" : "";
670 }
671 if (byte >= fStringBufferStart) {
672 return fStringBreaks.contains(byte - fStringBufferStart) ? "\n" : "";
673 }
674 return "";
675 }
676
677 } // namespace SkSL
678