blob: f20b00251b85d2f1b9108ffe9e7df527dc8d4820 [file] [log] [blame]
Nolan Scobief52ad202024-03-06 18:18:28 -05001/*
2 * Copyright 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <include/gpu/vk/GrVkBackendContext.h>
Kaylee Lubick925b0c12024-06-03 13:59:04 +000020#include <include/gpu/vk/VulkanExtensions.h>
21#include <include/gpu/vk/VulkanTypes.h>
Nolan Scobief52ad202024-03-06 18:18:28 -050022
23#include <vulkan/vulkan.h>
24
25using namespace skgpu;
26
Nolan Scobie609e5972024-03-20 14:47:34 -040027namespace skgpu {
28struct VulkanBackendContext;
29} // namespace skgpu
30
Nolan Scobief52ad202024-03-06 18:18:28 -050031namespace android {
32namespace renderengine {
33namespace skia {
34
35class VulkanInterface {
36public:
37 // Create an uninitialized interface. Initialize with `init`.
38 VulkanInterface() = default;
39 ~VulkanInterface() = default;
40 VulkanInterface(const VulkanInterface&) = delete;
41 VulkanInterface& operator=(const VulkanInterface&) = delete;
42 VulkanInterface& operator=(VulkanInterface&&) = delete;
43
44 void init(bool protectedContent = false);
Nolan Scobie2526b2f2024-04-16 15:12:22 -040045 // Returns true and marks this VulkanInterface as "owned" if it is initialized but unused by any
46 // RenderEngine instances. Returns false if already owned, indicating that it must not be used
47 // by a new RE instance.
48 bool takeOwnership();
Nolan Scobief52ad202024-03-06 18:18:28 -050049 void teardown();
50
Nolan Scobiefc125ec2024-03-11 20:08:27 -040051 GrVkBackendContext getGaneshBackendContext();
Nolan Scobie609e5972024-03-20 14:47:34 -040052 VulkanBackendContext getGraphiteBackendContext();
Nolan Scobief52ad202024-03-06 18:18:28 -050053 VkSemaphore createExportableSemaphore();
54 VkSemaphore importSemaphoreFromSyncFd(int syncFd);
55 int exportSemaphoreSyncFd(VkSemaphore semaphore);
56 void destroySemaphore(VkSemaphore semaphore);
57
58 bool isInitialized() const { return mInitialized; }
59 bool isRealtimePriority() const { return mIsRealtimePriority; }
60 const std::vector<std::string>& getInstanceExtensionNames() { return mInstanceExtensionNames; }
61 const std::vector<std::string>& getDeviceExtensionNames() { return mDeviceExtensionNames; }
62
63private:
64 struct VulkanFuncs {
65 PFN_vkCreateSemaphore vkCreateSemaphore = nullptr;
66 PFN_vkImportSemaphoreFdKHR vkImportSemaphoreFdKHR = nullptr;
67 PFN_vkGetSemaphoreFdKHR vkGetSemaphoreFdKHR = nullptr;
68 PFN_vkDestroySemaphore vkDestroySemaphore = nullptr;
69
70 PFN_vkDeviceWaitIdle vkDeviceWaitIdle = nullptr;
71 PFN_vkDestroyDevice vkDestroyDevice = nullptr;
72 PFN_vkDestroyInstance vkDestroyInstance = nullptr;
73 };
74
75 static void onVkDeviceFault(void* callbackContext, const std::string& description,
76 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
77 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
78 const std::vector<std::byte>& vendorBinaryData);
79
Nolan Scobie2526b2f2024-04-16 15:12:22 -040080 // Note: keep all field defaults in sync with teardown()
Nolan Scobief52ad202024-03-06 18:18:28 -050081 bool mInitialized = false;
Nolan Scobie2526b2f2024-04-16 15:12:22 -040082 bool mIsOwned = false;
Nolan Scobief52ad202024-03-06 18:18:28 -050083 VkInstance mInstance = VK_NULL_HANDLE;
84 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
85 VkDevice mDevice = VK_NULL_HANDLE;
86 VkQueue mQueue = VK_NULL_HANDLE;
87 int mQueueIndex = 0;
88 uint32_t mApiVersion = 0;
Kaylee Lubick925b0c12024-06-03 13:59:04 +000089 skgpu::VulkanExtensions mGrExtensions;
Nolan Scobief52ad202024-03-06 18:18:28 -050090 VkPhysicalDeviceFeatures2* mPhysicalDeviceFeatures2 = nullptr;
91 VkPhysicalDeviceSamplerYcbcrConversionFeatures* mSamplerYcbcrConversionFeatures = nullptr;
92 VkPhysicalDeviceProtectedMemoryFeatures* mProtectedMemoryFeatures = nullptr;
93 VkPhysicalDeviceFaultFeaturesEXT* mDeviceFaultFeatures = nullptr;
Kaylee Lubick925b0c12024-06-03 13:59:04 +000094 skgpu::VulkanGetProc mGrGetProc = nullptr;
Nolan Scobief52ad202024-03-06 18:18:28 -050095 bool mIsProtected = false;
96 bool mIsRealtimePriority = false;
97
98 VulkanFuncs mFuncs;
99
100 std::vector<std::string> mInstanceExtensionNames;
101 std::vector<std::string> mDeviceExtensionNames;
102};
103
104} // namespace skia
105} // namespace renderengine
106} // namespace android