Debian Codegraph @

src/gpu/graphite/SharedContext.h

raw
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_graphite_SharedContext_DEFINED
9 #define skgpu_graphite_SharedContext_DEFINED
10
11 #include <memory>
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkSize.h"
14 #include "include/private/base/SingleOwner.h"
15
16 #include "include/gpu/graphite/GraphiteTypes.h"
17 #include "src/capture/SkCaptureManager.h"
18 #include "src/gpu/graphite/GlobalCache.h"
19 #include "src/gpu/graphite/PipelineManager.h"
20 #include "src/gpu/graphite/ShaderCodeDictionary.h"
21
22 class SkCaptureManager;
23 class SkExecutor;
24
25 namespace skgpu {
26 class SingleOwner;
27 }
28
29 namespace skgpu::graphite {
30
31 class BackendTexture;
32 class Caps;
33 class CommandBuffer;
34 class Context;
35 class PersistentPipelineStorage;
36 class RendererProvider;
37 class ResourceProvider;
38 class TextureInfo;
39 class ThreadSafeResourceProvider;
40
41 class SharedContext : public SkRefCnt {
42 public:
43 ~SharedContext() override;
44
45 /**
46 * Gets the capabilities of the draw target.
47 */
48 const Caps* caps() const { return fCaps.get(); }
49
50 BackendApi backend() const { return fBackend; }
51 Protected isProtected() const;
52
53 GlobalCache* globalCache() { return &fGlobalCache; }
54 const GlobalCache* globalCache() const { return &fGlobalCache; }
55
56 PipelineManager* pipelineManager() { return &fPipelineManager; }
57
58 const RendererProvider* rendererProvider() const { return fRendererProvider.get(); }
59
60 ShaderCodeDictionary* shaderCodeDictionary() { return &fShaderDictionary; }
61 const ShaderCodeDictionary* shaderCodeDictionary() const { return &fShaderDictionary; }
62
63 virtual std::unique_ptr<ResourceProvider> makeResourceProvider(SingleOwner*,
64 uint32_t recorderID,
65 size_t resourceBudget) = 0;
66
67 // The runtime effect dictionary provides a link between SkCodeSnippetIds referenced in the
68 // paint key and the current SkRuntimeEffect that provides the SkSL for that id.
69 sk_sp<GraphicsPipeline> findOrCreateGraphicsPipeline(
70 const RuntimeEffectDictionary*,
71 const UniqueKey& pipelineKey,
72 const GraphicsPipelineDesc&,
73 const RenderPassDesc&,
74 SkEnumBitMask<PipelineCreationFlags>);
75
76 // Called by Context::isContextLost(). Returns true if the backend-specific SharedContext has
77 // gotten into an unrecoverable, lost state.
78 virtual bool isDeviceLost() const { return false; }
79
80 virtual void deviceTick(Context*) {}
81
82 virtual void syncPipelineData(PersistentPipelineStorage*, size_t maxSize) {}
83
84 SkCaptureManager* captureManager() { return fCaptureManager.get(); }
85
86 #if defined(SK_DEBUG)
87 size_t getResourceCacheLimit() const;
88 size_t getResourceCacheCurrentBudgetedBytes() const;
89 size_t getResourceCacheCurrentPurgeableBytes() const;
90 #endif
91
92 void dumpMemoryStatistics(SkTraceMemoryDump*) const;
93 void freeGpuResources();
94 void purgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime);
95 void forceProcessReturnedResources();
96
97 protected:
98 SharedContext(std::unique_ptr<const Caps>,
99 BackendApi,
100 SkExecutor* executor,
101 SkSpan<sk_sp<SkRuntimeEffect>> userDefinedKnownRuntimeEffects);
102
103 // All the resources in the ThreadSafeResourceProvider should be 0-sized.
104 static constexpr size_t kThreadedSafeResourceBudget = 256;
105
106 // This SingleOwner is for ResourceProvider wrapped by the fThreadSafeResourceProvider.
107 // It can't be in that class bc each backend-specific ShareContext must make its own
108 // backend-specific wrapped ResourceProvider in its constructor.
109 mutable SingleOwner fSingleOwner;
110 std::unique_ptr<ThreadSafeResourceProvider> fThreadSafeResourceProvider;
111
112 private:
113 friend class Context; // for setRendererProvider() and setCaptureManager()
114
115 virtual sk_sp<GraphicsPipeline> createGraphicsPipeline(
116 const RuntimeEffectDictionary*,
117 const UniqueKey&,
118 const GraphicsPipelineDesc&,
119 const RenderPassDesc&,
120 SkEnumBitMask<PipelineCreationFlags>,
121 uint32_t compilationID) = 0;
122
123 // Must be created out-of-band to allow RenderSteps to use a QueueManager.
124 void setRendererProvider(std::unique_ptr<RendererProvider> rendererProvider);
125
126 void setCaptureManager(sk_sp<SkCaptureManager> captureManager);
127
128 std::unique_ptr<const Caps> fCaps; // Provided by backend subclass
129
130 BackendApi fBackend;
131 GlobalCache fGlobalCache;
132 PipelineManager fPipelineManager;
133 std::unique_ptr<RendererProvider> fRendererProvider;
134 ShaderCodeDictionary fShaderDictionary;
135 sk_sp<SkCaptureManager> fCaptureManager;
136 };
137
138 } // namespace skgpu::graphite
139
140 #endif // skgpu_graphite_SharedContext_DEFINED