1/*
2 * Copyright 2019 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 GrPersistentCacheEntry_DEFINED
9#define GrPersistentCacheEntry_DEFINED
10
11#include "include/core/SkData.h"
12#include "include/private/GrTypesPriv.h"
13#include "src/core/SkReader32.h"
14#include "src/core/SkWriter32.h"
15#include "src/sksl/SkSLString.h"
16#include "src/sksl/ir/SkSLProgram.h"
17
18// The GrPersistentCache stores opaque blobs, as far as clients are concerned. It's helpful to
19// inspect certain kinds of cached data within our tools, so for those cases (GLSL, SPIR-V), we
20// put the serialization logic here, to be shared by the backend code and the tool code.
21namespace GrPersistentCacheUtils {
22
23struct ShaderMetadata {
24 SkSL::Program::Settings* fSettings = nullptr;
25 SkTArray<SkSL::String> fAttributeNames;
26 bool fHasCustomColorOutput = false;
27 bool fHasSecondaryColorOutput = false;
28};
29
30// Increment this whenever the serialization format of cached shaders changes
31static constexpr int kCurrentVersion = 1;
32
33static inline sk_sp<SkData> PackCachedShaders(SkFourByteTag shaderType,
34 const SkSL::String shaders[],
35 const SkSL::Program::Inputs inputs[],
36 int numInputs,
37 const ShaderMetadata* meta = nullptr) {
38 // For consistency (so tools can blindly pack and unpack cached shaders), we always write
39 // kGrShaderTypeCount inputs. If the backend gives us fewer, we just replicate the last one.
40 SkASSERT(numInputs >= 1 && numInputs <= kGrShaderTypeCount);
41
42 SkWriter32 writer;
43 writer.write32(kCurrentVersion);
44 writer.write32(shaderType);
45 for (int i = 0; i < kGrShaderTypeCount; ++i) {
46 writer.writeString(shaders[i].c_str(), shaders[i].size());
47 writer.writePad(&inputs[std::min(i, numInputs - 1)], sizeof(SkSL::Program::Inputs));
48 }
49 writer.writeBool(SkToBool(meta));
50 if (meta) {
51 writer.writeBool(SkToBool(meta->fSettings));
52 if (meta->fSettings) {
53 writer.writeBool(meta->fSettings->fFlipY);
54 writer.writeBool(meta->fSettings->fFragColorIsInOut);
55 writer.writeBool(meta->fSettings->fForceHighPrecision);
56 }
57
58 writer.writeInt(meta->fAttributeNames.count());
59 for (const auto& attr : meta->fAttributeNames) {
60 writer.writeString(attr.c_str(), attr.size());
61 }
62
63 writer.writeBool(meta->fHasCustomColorOutput);
64 writer.writeBool(meta->fHasSecondaryColorOutput);
65 }
66 return writer.snapshotAsData();
67}
68
69static SkFourByteTag GetType(SkReader32* reader) {
70 constexpr SkFourByteTag kInvalidTag = ~0;
71 if (!reader->isAvailable(2 * sizeof(int))) {
72 return kInvalidTag;
73 }
74 if (reader->readInt() != kCurrentVersion) {
75 return kInvalidTag;
76 }
77 return reader->readU32();
78}
79
80static inline void UnpackCachedShaders(SkReader32* reader,
81 SkSL::String shaders[],
82 SkSL::Program::Inputs inputs[],
83 int numInputs,
84 ShaderMetadata* meta = nullptr) {
85 for (int i = 0; i < kGrShaderTypeCount; ++i) {
86 size_t stringLen = 0;
87 const char* string = reader->readString(&stringLen);
88 shaders[i] = SkSL::String(string, stringLen);
89
90 // GL, for example, only wants one set of Inputs
91 if (i < numInputs) {
92 reader->read(&inputs[i], sizeof(inputs[i]));
93 } else {
94 reader->skip(sizeof(SkSL::Program::Inputs));
95 }
96 }
97 if (reader->readBool() && meta) {
98 SkASSERT(meta->fSettings != nullptr);
99
100 if (reader->readBool()) {
101 meta->fSettings->fFlipY = reader->readBool();
102 meta->fSettings->fFragColorIsInOut = reader->readBool();
103 meta->fSettings->fForceHighPrecision = reader->readBool();
104 }
105
106 meta->fAttributeNames.resize(reader->readInt());
107 for (int i = 0; i < meta->fAttributeNames.count(); ++i) {
108 size_t stringLen = 0;
109 const char* string = reader->readString(&stringLen);
110 meta->fAttributeNames[i] = SkSL::String(string, stringLen);
111 }
112
113 meta->fHasCustomColorOutput = reader->readBool();
114 meta->fHasSecondaryColorOutput = reader->readBool();
115 }
116}
117
118}
119
120#endif
121