GrPersistentCacheUtils.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #ifndef GrPersistentCacheEntry_DEFINED
  8. #define GrPersistentCacheEntry_DEFINED
  9. #include "include/core/SkData.h"
  10. #include "include/private/GrTypesPriv.h"
  11. #include "src/core/SkReader32.h"
  12. #include "src/core/SkWriter32.h"
  13. #include "src/sksl/SkSLString.h"
  14. #include "src/sksl/ir/SkSLProgram.h"
  15. // The GrPersistentCache stores opaque blobs, as far as clients are concerned. It's helpful to
  16. // inspect certain kinds of cached data within our tools, so for those cases (GLSL, SPIR-V), we
  17. // put the serialization logic here, to be shared by the backend code and the tool code.
  18. namespace GrPersistentCacheUtils {
  19. static inline sk_sp<SkData> PackCachedShaders(SkFourByteTag shaderType,
  20. const SkSL::String shaders[],
  21. const SkSL::Program::Inputs inputs[],
  22. int numInputs) {
  23. // For consistency (so tools can blindly pack and unpack cached shaders), we always write
  24. // kGrShaderTypeCount inputs. If the backend gives us fewer, we just replicate the last one.
  25. SkASSERT(numInputs >= 1 && numInputs <= kGrShaderTypeCount);
  26. SkWriter32 writer;
  27. writer.write32(shaderType);
  28. for (int i = 0; i < kGrShaderTypeCount; ++i) {
  29. writer.writeString(shaders[i].c_str(), shaders[i].size());
  30. writer.writePad(&inputs[SkTMin(i, numInputs - 1)], sizeof(SkSL::Program::Inputs));
  31. }
  32. return writer.snapshotAsData();
  33. }
  34. static inline SkFourByteTag UnpackCachedShaders(const SkData* data,
  35. SkSL::String shaders[],
  36. SkSL::Program::Inputs inputs[],
  37. int numInputs) {
  38. SkReader32 reader(data->data(), data->size());
  39. SkFourByteTag shaderType = reader.readU32();
  40. for (int i = 0; i < kGrShaderTypeCount; ++i) {
  41. size_t stringLen = 0;
  42. const char* string = reader.readString(&stringLen);
  43. shaders[i] = SkSL::String(string, stringLen);
  44. // GL, for example, only wants one set of Inputs
  45. if (i < numInputs) {
  46. reader.read(&inputs[i], sizeof(inputs[i]));
  47. } else {
  48. reader.skip(sizeof(SkSL::Program::Inputs));
  49. }
  50. }
  51. return shaderType;
  52. }
  53. }
  54. #endif