Flutter Engine
The Flutter Engine
KeyBuilder.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 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 skgpu_KeyBuilder_DEFINED
9#define skgpu_KeyBuilder_DEFINED
10
14
15#include <cstdint>
16#include <string_view>
17
18namespace skgpu {
19
21public:
23
24 virtual ~KeyBuilder() {
25 // Ensure that flush was called before we went out of scope
26 SkASSERT(fBitsUsed == 0);
27 }
28
29 virtual void addBits(uint32_t numBits, uint32_t val, std::string_view label) {
30 SkASSERT(numBits > 0 && numBits <= 32);
31 SkASSERT(numBits == 32 || (val < (1u << numBits)));
32
33 fCurValue |= (val << fBitsUsed);
34 fBitsUsed += numBits;
35
36 if (fBitsUsed >= 32) {
37 // Overflow, start a new working value
38 fData->push_back(fCurValue);
39 uint32_t excess = fBitsUsed - 32;
40 fCurValue = excess ? (val >> (numBits - excess)) : 0;
41 fBitsUsed = excess;
42 }
43
44 SkASSERT(fCurValue < (1u << fBitsUsed));
45 }
46
47 void addBytes(uint32_t numBytes, const void* data, std::string_view label) {
48 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
49 for (; numBytes --> 0; bytes++) {
50 this->addBits(8, *bytes, label);
51 }
52 }
53
54 void addBool(bool b, std::string_view label) {
55 this->addBits(1, b, label);
56 }
57
58 void add32(uint32_t v, std::string_view label = "unknown") {
59 this->addBits(32, v, label);
60 }
61
62 virtual void appendComment(const char* comment) {}
63
64 // Introduces a word-boundary in the key. Must be called before using the key with any cache,
65 // but can also be called to create a break between generic data and backend-specific data.
66 void flush() {
67 if (fBitsUsed) {
68 fData->push_back(fCurValue);
69 fCurValue = 0;
70 fBitsUsed = 0;
71 }
72 }
73
74private:
76 uint32_t fCurValue = 0;
77 uint32_t fBitsUsed = 0; // ... in current value
78};
79
81public:
83
84 void addBits(uint32_t numBits, uint32_t val, std::string_view label) override {
85 KeyBuilder::addBits(numBits, val, label);
86 fDescription.appendf("%.*s: %u\n", (int)label.size(), label.data(), val);
87 }
88
89 void appendComment(const char* comment) override {
90 fDescription.appendf("%s\n", comment);
91 }
92
93 SkString description() const { return fDescription; }
94
95private:
96 SkString fDescription;
97};
98
99} // namespace skgpu
100
101#endif // skgpu_KeyBuilder_DEFINED
#define SkASSERT(cond)
Definition: SkAssert.h:116
void void void appendf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:550
void add32(uint32_t v, std::string_view label="unknown")
Definition: KeyBuilder.h:58
virtual ~KeyBuilder()
Definition: KeyBuilder.h:24
virtual void appendComment(const char *comment)
Definition: KeyBuilder.h:62
virtual void addBits(uint32_t numBits, uint32_t val, std::string_view label)
Definition: KeyBuilder.h:29
void addBytes(uint32_t numBytes, const void *data, std::string_view label)
Definition: KeyBuilder.h:47
void addBool(bool b, std::string_view label)
Definition: KeyBuilder.h:54
KeyBuilder(skia_private::TArray< uint32_t, true > *data)
Definition: KeyBuilder.h:22
void appendComment(const char *comment) override
Definition: KeyBuilder.h:89
StringKeyBuilder(skia_private::TArray< uint32_t, true > *data)
Definition: KeyBuilder.h:82
void addBits(uint32_t numBits, uint32_t val, std::string_view label) override
Definition: KeyBuilder.h:84
SkString description() const
Definition: KeyBuilder.h:93
static bool b
Definition: GpuTools.h:21
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63