xref: /aosp_15_r20/frameworks/native/libs/renderengine/skia/VulkanInterface.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
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/VulkanBackendContext.h>
20 #include <include/gpu/vk/VulkanExtensions.h>
21 #include <include/gpu/vk/VulkanTypes.h>
22 
23 #include <vulkan/vulkan.h>
24 
25 using namespace skgpu;
26 
27 namespace android {
28 namespace renderengine {
29 namespace skia {
30 
31 class VulkanInterface {
32 public:
33     // Create an uninitialized interface. Initialize with `init`.
34     VulkanInterface() = default;
35     ~VulkanInterface() = default;
36     VulkanInterface(const VulkanInterface&) = delete;
37     VulkanInterface& operator=(const VulkanInterface&) = delete;
38     VulkanInterface& operator=(VulkanInterface&&) = delete;
39 
40     void init(bool protectedContent = false);
41     // Returns true and marks this VulkanInterface as "owned" if it is initialized but unused by any
42     // RenderEngine instances. Returns false if already owned, indicating that it must not be used
43     // by a new RE instance.
44     bool takeOwnership();
45     void teardown();
46 
47     // TODO(b/309785258) Combine these into one now that they are the same implementation.
48     VulkanBackendContext getGaneshBackendContext();
49     VulkanBackendContext getGraphiteBackendContext();
50     VkSemaphore createExportableSemaphore();
51     VkSemaphore importSemaphoreFromSyncFd(int syncFd);
52     int exportSemaphoreSyncFd(VkSemaphore semaphore);
53     void destroySemaphore(VkSemaphore semaphore);
54 
isInitialized()55     bool isInitialized() const { return mInitialized; }
isRealtimePriority()56     bool isRealtimePriority() const { return mIsRealtimePriority; }
getInstanceExtensionNames()57     const std::vector<std::string>& getInstanceExtensionNames() { return mInstanceExtensionNames; }
getDeviceExtensionNames()58     const std::vector<std::string>& getDeviceExtensionNames() { return mDeviceExtensionNames; }
59 
60 private:
61     struct VulkanFuncs {
62         PFN_vkCreateSemaphore vkCreateSemaphore = nullptr;
63         PFN_vkImportSemaphoreFdKHR vkImportSemaphoreFdKHR = nullptr;
64         PFN_vkGetSemaphoreFdKHR vkGetSemaphoreFdKHR = nullptr;
65         PFN_vkDestroySemaphore vkDestroySemaphore = nullptr;
66 
67         PFN_vkDeviceWaitIdle vkDeviceWaitIdle = nullptr;
68         PFN_vkDestroyDevice vkDestroyDevice = nullptr;
69         PFN_vkDestroyInstance vkDestroyInstance = nullptr;
70     };
71 
72     static void onVkDeviceFault(void* callbackContext, const std::string& description,
73                                 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos,
74                                 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos,
75                                 const std::vector<std::byte>& vendorBinaryData);
76 
77     // Note: keep all field defaults in sync with teardown()
78     bool mInitialized = false;
79     bool mIsOwned = false;
80     VkInstance mInstance = VK_NULL_HANDLE;
81     VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
82     VkDevice mDevice = VK_NULL_HANDLE;
83     VkQueue mQueue = VK_NULL_HANDLE;
84     int mQueueIndex = 0;
85     uint32_t mApiVersion = 0;
86     skgpu::VulkanExtensions mVulkanExtensions;
87     VkPhysicalDeviceFeatures2* mPhysicalDeviceFeatures2 = nullptr;
88     VkPhysicalDeviceSamplerYcbcrConversionFeatures* mSamplerYcbcrConversionFeatures = nullptr;
89     VkPhysicalDeviceProtectedMemoryFeatures* mProtectedMemoryFeatures = nullptr;
90     VkPhysicalDeviceFaultFeaturesEXT* mDeviceFaultFeatures = nullptr;
91     skgpu::VulkanGetProc mGrGetProc = nullptr;
92     bool mIsProtected = false;
93     bool mIsRealtimePriority = false;
94 
95     VulkanFuncs mFuncs;
96 
97     std::vector<std::string> mInstanceExtensionNames;
98     std::vector<std::string> mDeviceExtensionNames;
99 };
100 
101 } // namespace skia
102 } // namespace renderengine
103 } // namespace android
104