xref: /aosp_15_r20/external/skia/src/gpu/KeyBuilder.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
11 #include "include/core/SkString.h"
12 #include "include/private/base/SkAssert.h"
13 #include "include/private/base/SkTArray.h"
14 
15 #include <cstdint>
16 #include <string_view>
17 
18 namespace skgpu {
19 
20 class KeyBuilder {
21 public:
KeyBuilder(skia_private::TArray<uint32_t,true> * data)22     KeyBuilder(skia_private::TArray<uint32_t, true>* data) : fData(data) {}
23 
~KeyBuilder()24     virtual ~KeyBuilder() {
25         // Ensure that flush was called before we went out of scope
26         SkASSERT(fBitsUsed == 0);
27     }
28 
addBits(uint32_t numBits,uint32_t val,std::string_view label)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 
addBytes(uint32_t numBytes,const void * data,std::string_view label)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 
addBool(bool b,std::string_view label)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 
appendComment(const char * comment)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.
flush()66     void flush() {
67         if (fBitsUsed) {
68             fData->push_back(fCurValue);
69             fCurValue = 0;
70             fBitsUsed = 0;
71         }
72     }
73 
74 private:
75     skia_private::TArray<uint32_t, true>* fData;
76     uint32_t fCurValue = 0;
77     uint32_t fBitsUsed = 0;  // ... in current value
78 };
79 
80 class StringKeyBuilder : public KeyBuilder {
81 public:
StringKeyBuilder(skia_private::TArray<uint32_t,true> * data)82     StringKeyBuilder(skia_private::TArray<uint32_t, true>* data) : KeyBuilder(data) {}
83 
addBits(uint32_t numBits,uint32_t val,std::string_view label)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 
appendComment(const char * comment)89     void appendComment(const char* comment) override {
90         fDescription.appendf("%s\n", comment);
91     }
92 
description()93     SkString description() const { return fDescription; }
94 
95 private:
96     SkString fDescription;
97 };
98 
99 } // namespace skgpu
100 
101 #endif // skgpu_KeyBuilder_DEFINED
102