blob: af0489a4707d5996838522b536559de525e557d4 [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>
20#include <include/gpu/vk/GrVkExtensions.h>
21
22#include <vulkan/vulkan.h>
23
24using namespace skgpu;
25
Nolan Scobie609e5972024-03-20 14:47:34 -040026namespace skgpu {
27struct VulkanBackendContext;
28} // namespace skgpu
29
Nolan Scobief52ad202024-03-06 18:18:28 -050030namespace android {
31namespace renderengine {
32namespace skia {
33
34class VulkanInterface {
35public:
36 // Create an uninitialized interface. Initialize with `init`.
37 VulkanInterface() = default;
38 ~VulkanInterface() = default;
39 VulkanInterface(const VulkanInterface&) = delete;
40 VulkanInterface& operator=(const VulkanInterface&) = delete;
41 VulkanInterface& operator=(VulkanInterface&&) = delete;
42
43 void init(bool protectedContent = false);
Nolan Scobie2526b2f2024-04-16 15:12:22 -040044 // Returns true and marks this VulkanInterface as "owned" if it is initialized but unused by any
45 // RenderEngine instances. Returns false if already owned, indicating that it must not be used
46 // by a new RE instance.
47 bool takeOwnership();
Nolan Scobief52ad202024-03-06 18:18:28 -050048 void teardown();
49
Nolan Scobiefc125ec2024-03-11 20:08:27 -040050 GrVkBackendContext getGaneshBackendContext();
Nolan Scobie609e5972024-03-20 14:47:34 -040051 VulkanBackendContext getGraphiteBackendContext();
Nolan Scobief52ad202024-03-06 18:18:28 -050052 VkSemaphore createExportableSemaphore();
53 VkSemaphore importSemaphoreFromSyncFd(int syncFd);
54 int exportSemaphoreSyncFd(VkSemaphore semaphore);
55 void destroySemaphore(VkSemaphore semaphore);
56
57 bool isInitialized() const { return mInitialized; }
58 bool isRealtimePriority() const { return mIsRealtimePriority; }
59 const std::vector<std::string>& getInstanceExtensionNames() { return mInstanceExtensionNames; }
60 const std::vector<std::string>& getDeviceExtensionNames() { return mDeviceExtensionNames; }
61
62private:
63 struct VulkanFuncs {
64 PFN_vkCreateSemaphore vkCreateSemaphore = nullptr;
65 PFN_vkImportSemaphoreFdKHR vkImportSemaphoreFdKHR = nullptr;
66 PFN_vkGetSemaphoreFdKHR vkGetSemaphoreFdKHR = nullptr;
67 PFN_vkDestroySemaphore vkDestroySemaphore = nullptr;
68
69 PFN_vkDeviceWaitIdle vkDeviceWaitIdle = nullptr;
70 PFN_vkDestroyDevice vkDestroyDevice = nullptr;
71 PFN_vkDestroyInstance vkDestroyInstance = nullptr;
72 };
73
74 static void onVkDeviceFault(void* callbackContext, const std::string& description,
75 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
76 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
77 const std::vector<std::byte>& vendorBinaryData);
78
Nolan Scobie2526b2f2024-04-16 15:12:22 -040079 // Note: keep all field defaults in sync with teardown()
Nolan Scobief52ad202024-03-06 18:18:28 -050080 bool mInitialized = false;
Nolan Scobie2526b2f2024-04-16 15:12:22 -040081 bool mIsOwned = false;
Nolan Scobief52ad202024-03-06 18:18:28 -050082 VkInstance mInstance = VK_NULL_HANDLE;
83 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
84 VkDevice mDevice = VK_NULL_HANDLE;
85 VkQueue mQueue = VK_NULL_HANDLE;
86 int mQueueIndex = 0;
87 uint32_t mApiVersion = 0;
88 GrVkExtensions mGrExtensions;
89 VkPhysicalDeviceFeatures2* mPhysicalDeviceFeatures2 = nullptr;
90 VkPhysicalDeviceSamplerYcbcrConversionFeatures* mSamplerYcbcrConversionFeatures = nullptr;
91 VkPhysicalDeviceProtectedMemoryFeatures* mProtectedMemoryFeatures = nullptr;
92 VkPhysicalDeviceFaultFeaturesEXT* mDeviceFaultFeatures = nullptr;
93 GrVkGetProc mGrGetProc = nullptr;
94 bool mIsProtected = false;
95 bool mIsRealtimePriority = false;
96
97 VulkanFuncs mFuncs;
98
99 std::vector<std::string> mInstanceExtensionNames;
100 std::vector<std::string> mDeviceExtensionNames;
101};
102
103} // namespace skia
104} // namespace renderengine
105} // namespace android