Debian Codegraph @

src/gpu/graphite/vk/VulkanSharedContext.h

raw
1 /*
2 * Copyright 2022 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_VulkanSharedContext_DEFINED
9 #define skgpu_graphite_VulkanSharedContext_DEFINED
10
11 #include "include/private/base/SkMutex.h"
12 #include "src/gpu/graphite/SharedContext.h"
13 #include "src/gpu/graphite/ThreadSafeResourceProvider.h"
14
15 #include "include/gpu/vk/VulkanTypes.h"
16 #include "src/gpu/graphite/vk/VulkanCaps.h"
17
18 #include <atomic>
19
20 namespace skgpu {
21 struct VulkanBackendContext;
22 struct VulkanInterface;
23 class VulkanMemoryAllocator;
24 }
25
26 namespace skgpu::graphite {
27
28 struct ContextOptions;
29 class VulkanCaps;
30 class VulkanRenderPass;
31
32 class VulkanThreadSafeResourceProvider final : public ThreadSafeResourceProvider {
33 public:
34 VulkanThreadSafeResourceProvider(std::unique_ptr<ResourceProvider>);
35 sk_sp<VulkanRenderPass> findOrCreateRenderPass(const RenderPassDesc&, bool compatibleOnly);
36 };
37
38 class VulkanSharedContext final : public SharedContext {
39 public:
40 static sk_sp<SharedContext> Make(const VulkanBackendContext&, const ContextOptions&);
41 ~VulkanSharedContext() override;
42
43 const VulkanCaps& vulkanCaps() const { return static_cast<const VulkanCaps&>(*this->caps()); }
44
45 const skgpu::VulkanInterface* interface() const { return fInterface.get(); }
46
47 skgpu::VulkanMemoryAllocator* memoryAllocator() const { return fMemoryAllocator.get(); }
48
49 VkPhysicalDevice physDevice() const { return fPhysDevice; }
50 VkDevice device() const { return fDevice; }
51 uint32_t queueIndex() const { return fQueueIndex; }
52
53 VulkanThreadSafeResourceProvider* threadSafeResourceProvider() const;
54
55 std::unique_ptr<ResourceProvider> makeResourceProvider(SingleOwner*,
56 uint32_t recorderID,
57 size_t resourceBudget) override;
58
59 bool checkVkResult(VkResult result) const;
60
61 bool isDeviceLost() const override {
62 SkAutoMutexExclusive lock(fDeviceIsLostMutex);
63 return fDeviceIsLost;
64 }
65
66 VkPipelineCache getPipelineCache() const { return fPipelineCache; }
67
68 void pipelineCompileWasRequired() { fHasNewVkPipelineCacheData = true; }
69 void syncPipelineData(PersistentPipelineStorage*, size_t maxSize) override;
70
71 private:
72 VulkanSharedContext(const VulkanBackendContext&,
73 sk_sp<const skgpu::VulkanInterface>,
74 sk_sp<skgpu::VulkanMemoryAllocator>,
75 std::unique_ptr<const VulkanCaps>,
76 SkExecutor*,
77 PersistentPipelineStorage*,
78 SkSpan<sk_sp<SkRuntimeEffect>> userDefinedKnownRuntimeEffects);
79
80 VkPipelineCache createPipelineCache(VkPhysicalDevice, PersistentPipelineStorage*);
81
82 sk_sp<GraphicsPipeline> createGraphicsPipeline(const RuntimeEffectDictionary*,
83 const UniqueKey&,
84 const GraphicsPipelineDesc&,
85 const RenderPassDesc&,
86 SkEnumBitMask<PipelineCreationFlags>,
87 uint32_t compilationID) override;
88
89 sk_sp<const skgpu::VulkanInterface> fInterface;
90 sk_sp<skgpu::VulkanMemoryAllocator> fMemoryAllocator;
91
92 VkPhysicalDevice fPhysDevice;
93 VkDevice fDevice;
94 uint32_t fQueueIndex;
95
96 mutable SkMutex fDeviceIsLostMutex;
97 // TODO(b/322207523): consider refactoring to remove the mutable keyword from fDeviceIsLost.
98 mutable bool fDeviceIsLost SK_GUARDED_BY(fDeviceIsLostMutex) = false;
99 skgpu::VulkanDeviceLostContext fDeviceLostContext;
100 skgpu::VulkanDeviceLostProc fDeviceLostProc;
101
102 VkPipelineCache fPipelineCache = VK_NULL_HANDLE;
103 std::atomic<bool> fHasNewVkPipelineCacheData = false;
104 size_t fLastKnownPersistentPipelineStorageSize = 0;
105 };
106
107 } // namespace skgpu::graphite
108
109 #endif // skgpu_graphite_VulkanSharedContext_DEFINED