blob: d0fe4d1544aa9918fb4dc24f1646ca5f027afbd1 [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
Kaylee Lubick1a25e4e2024-06-20 17:35:57 +000019#include <include/gpu/vk/VulkanBackendContext.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
27namespace android {
28namespace renderengine {
29namespace skia {
30
31class VulkanInterface {
32public:
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);
Nolan Scobie2526b2f2024-04-16 15:12:22 -040041 // 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();
Nolan Scobief52ad202024-03-06 18:18:28 -050045 void teardown();
46
Kaylee Lubick1a25e4e2024-06-20 17:35:57 +000047 // TODO(b/309785258) Combine these into one now that they are the same implementation.
48 VulkanBackendContext getGaneshBackendContext();
Nolan Scobie609e5972024-03-20 14:47:34 -040049 VulkanBackendContext getGraphiteBackendContext();
Nolan Scobief52ad202024-03-06 18:18:28 -050050 VkSemaphore createExportableSemaphore();
51 VkSemaphore importSemaphoreFromSyncFd(int syncFd);
52 int exportSemaphoreSyncFd(VkSemaphore semaphore);
53 void destroySemaphore(VkSemaphore semaphore);
54
55 bool isInitialized() const { return mInitialized; }
56 bool isRealtimePriority() const { return mIsRealtimePriority; }
57 const std::vector<std::string>& getInstanceExtensionNames() { return mInstanceExtensionNames; }
58 const std::vector<std::string>& getDeviceExtensionNames() { return mDeviceExtensionNames; }
59
60private:
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
Nolan Scobie2526b2f2024-04-16 15:12:22 -040077 // Note: keep all field defaults in sync with teardown()
Nolan Scobief52ad202024-03-06 18:18:28 -050078 bool mInitialized = false;
Nolan Scobie2526b2f2024-04-16 15:12:22 -040079 bool mIsOwned = false;
Nolan Scobief52ad202024-03-06 18:18:28 -050080 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;
Kaylee Lubick1a25e4e2024-06-20 17:35:57 +000086 skgpu::VulkanExtensions mVulkanExtensions;
Nolan Scobief52ad202024-03-06 18:18:28 -050087 VkPhysicalDeviceFeatures2* mPhysicalDeviceFeatures2 = nullptr;
88 VkPhysicalDeviceSamplerYcbcrConversionFeatures* mSamplerYcbcrConversionFeatures = nullptr;
89 VkPhysicalDeviceProtectedMemoryFeatures* mProtectedMemoryFeatures = nullptr;
90 VkPhysicalDeviceFaultFeaturesEXT* mDeviceFaultFeatures = nullptr;
Kaylee Lubick925b0c12024-06-03 13:59:04 +000091 skgpu::VulkanGetProc mGrGetProc = nullptr;
Nolan Scobief52ad202024-03-06 18:18:28 -050092 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