Debian Codegraph @

tools/graphite/PipelineCallbackHandler.h

raw
1 /*
2 * Copyright 2025 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 PipelineCallbackHandler_DEFINED
9 #define PipelineCallbackHandler_DEFINED
10
11 #include "include/core/SkData.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/gpu/graphite/ContextOptions.h"
14 #include "src/base/SkSpinlock.h"
15 #include "src/core/SkChecksum.h"
16 #include "src/core/SkTHash.h"
17
18 #include <vector>
19
20 class SkData;
21
22 namespace skiatools::graphite {
23
24 // This is intended to be an example of a Precompilation Callback handler.
25 // For the gr*testprecompile configs it is used to:
26 // collect all the Android-style keys that are used by a given source (e.g., gm, or skp)
27 // recreate the Pipelines from the collected keys in `resetAndRecreatePipelines`
28 // For the gr*testtracking configs it is used to:
29 // collect all the Pipeline labels for a given source
30 // print out a list of the labels at the end using `report`
31 class PipelineCallBackHandler {
32 public:
33 static void CallBack(void* context,
34 skgpu::graphite::ContextOptions::PipelineCacheOp op,
35 const std::string& label,
36 uint32_t uniqueKeyHash,
37 bool fromPrecompile,
38 sk_sp<SkData> androidStyleKey) {
39 PipelineCallBackHandler* handler = reinterpret_cast<PipelineCallBackHandler*>(context);
40
41 handler->add(op, label, uniqueKeyHash, fromPrecompile, std::move(androidStyleKey));
42 }
43
44 void add(skgpu::graphite::ContextOptions::PipelineCacheOp op,
45 const std::string& label,
46 uint32_t uniqueKeyHash,
47 bool fromPrecompile,
48 sk_sp<SkData> androidStyleKey) SK_EXCLUDES(fSpinLock);
49
50 void retrieveKeys(std::vector<sk_sp<SkData>>* result) SK_EXCLUDES(fSpinLock) {
51 SkAutoSpinlock lock{ fSpinLock };
52
53 result->reserve(fMap.count());
54
55 fMap.foreach([result](std::unique_ptr<PipelineData>* data) {
56 // Because not all Pipelines are serializable we need to check for nulls here
57 if ((*data)->fAndroidStyleKey) {
58 result->push_back((*data)->fAndroidStyleKey);
59 }
60 });
61 }
62
63 void reset() SK_EXCLUDES(fSpinLock) {
64 SkAutoSpinlock lock{ fSpinLock };
65
66 fMap.reset();
67 }
68
69 void report() SK_EXCLUDES(fSpinLock);
70
71 private:
72 mutable SkSpinlock fSpinLock;
73
74 struct PipelineData {
75 PipelineData(const std::string& label,
76 uint32_t uniqueKeyHash,
77 bool fromPrecompile,
78 sk_sp<SkData> androidStyleKey) :
79 fLabel(label),
80 fAndroidStyleKey(std::move(androidStyleKey)),
81 fUniqueKeyHash(uniqueKeyHash),
82 fUses(fromPrecompile ? 0 : 1),
83 fFromPrecompile(fromPrecompile) {
84 }
85
86 std::string fLabel;
87 sk_sp<SkData> fAndroidStyleKey;
88 uint32_t fUniqueKeyHash;
89 uint32_t fUses;
90 bool fFromPrecompile;
91 };
92
93 struct PipelineKey {
94 const std::string* fLabel;
95 uint32_t fUniqueKeyHash;
96
97 static PipelineKey GetKey(const std::unique_ptr<PipelineData>& v) {
98 return { &v->fLabel, v->fUniqueKeyHash };
99 }
100 static uint32_t Hash(const PipelineKey& k) { return k.fUniqueKeyHash; }
101
102 bool operator==(const PipelineKey& other) const {
103 return fUniqueKeyHash == other.fUniqueKeyHash && *fLabel == *other.fLabel;
104 }
105 };
106
107 using Map = skia_private::THashTable<std::unique_ptr<PipelineData>, PipelineKey, PipelineKey>;
108 Map fMap SK_GUARDED_BY(fSpinLock);
109 };
110
111 } // namespace skiatools::graphite
112
113 #endif // PipelineCallbackHandler_DEFINED