Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 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/GrVkBackendContext.h> |
| 20 | #include <include/gpu/vk/GrVkExtensions.h> |
| 21 | |
| 22 | #include <vulkan/vulkan.h> |
| 23 | |
| 24 | using namespace skgpu; |
| 25 | |
Nolan Scobie | 609e597 | 2024-03-20 14:47:34 -0400 | [diff] [blame] | 26 | namespace skgpu { |
| 27 | struct VulkanBackendContext; |
| 28 | } // namespace skgpu |
| 29 | |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace renderengine { |
| 32 | namespace skia { |
| 33 | |
| 34 | class VulkanInterface { |
| 35 | public: |
| 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 Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 44 | // 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 Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 48 | void teardown(); |
| 49 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 50 | GrVkBackendContext getGaneshBackendContext(); |
Nolan Scobie | 609e597 | 2024-03-20 14:47:34 -0400 | [diff] [blame] | 51 | VulkanBackendContext getGraphiteBackendContext(); |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 52 | 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 | |
| 62 | private: |
| 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 Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 79 | // Note: keep all field defaults in sync with teardown() |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 80 | bool mInitialized = false; |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 81 | bool mIsOwned = false; |
Nolan Scobie | f52ad20 | 2024-03-06 18:18:28 -0500 | [diff] [blame] | 82 | 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 |