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 14 #include "include/gpu/vk/VulkanTypes.h" 15 #include "src/gpu/graphite/vk/VulkanCaps.h" 16 17 namespace skgpu { 18 struct VulkanBackendContext; 19 struct VulkanInterface; 20 class VulkanMemoryAllocator; 21 } 22 23 namespace skgpu::graphite { 24 25 struct ContextOptions; 26 class VulkanCaps; 27 28 class VulkanSharedContext final : public SharedContext { 29 public: 30 static sk_sp<SharedContext> Make(const VulkanBackendContext&, const ContextOptions&); 31 ~VulkanSharedContext() override; 32 vulkanCaps()33 const VulkanCaps& vulkanCaps() const { return static_cast<const VulkanCaps&>(*this->caps()); } 34 interface()35 const skgpu::VulkanInterface* interface() const { return fInterface.get(); } 36 memoryAllocator()37 skgpu::VulkanMemoryAllocator* memoryAllocator() const { return fMemoryAllocator.get(); } 38 physDevice()39 VkPhysicalDevice physDevice() const { return fPhysDevice; } device()40 VkDevice device() const { return fDevice; } queueIndex()41 uint32_t queueIndex() const { return fQueueIndex; } 42 43 std::unique_ptr<ResourceProvider> makeResourceProvider(SingleOwner*, 44 uint32_t recorderID, 45 size_t resourceBudget, 46 bool avoidBufferAlloc) override; 47 48 bool checkVkResult(VkResult result) const; 49 isDeviceLost()50 bool isDeviceLost() const override { 51 SkAutoMutexExclusive lock(fDeviceIsLostMutex); 52 return fDeviceIsLost; 53 } 54 55 private: 56 VulkanSharedContext(const VulkanBackendContext&, 57 sk_sp<const skgpu::VulkanInterface> interface, 58 sk_sp<skgpu::VulkanMemoryAllocator> memoryAllocator, 59 std::unique_ptr<const VulkanCaps> caps); 60 61 sk_sp<const skgpu::VulkanInterface> fInterface; 62 sk_sp<skgpu::VulkanMemoryAllocator> fMemoryAllocator; 63 64 VkPhysicalDevice fPhysDevice; 65 VkDevice fDevice; 66 uint32_t fQueueIndex; 67 68 mutable SkMutex fDeviceIsLostMutex; 69 // TODO(b/322207523): consider refactoring to remove the mutable keyword from fDeviceIsLost. 70 mutable bool fDeviceIsLost SK_GUARDED_BY(fDeviceIsLostMutex) = false; 71 skgpu::VulkanDeviceLostContext fDeviceLostContext; 72 skgpu::VulkanDeviceLostProc fDeviceLostProc; 73 }; 74 75 } // namespace skgpu::graphite 76 77 #endif // skgpu_graphite_VulkanSharedContext_DEFINED 78