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/VulkanExtensions.h> 21 #include <include/gpu/vk/VulkanTypes.h> 22 23 #include <vulkan/vulkan.h> 24 25 using namespace skgpu; 26 27 namespace skgpu { 28 struct VulkanBackendContext; 29 } // namespace skgpu 30 31 namespace android { 32 namespace renderengine { 33 namespace skia { 34 35 class VulkanInterface { 36 public: 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); 45 // 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(); 49 void teardown(); 50 51 GrVkBackendContext getGaneshBackendContext(); 52 VulkanBackendContext getGraphiteBackendContext(); 53 VkSemaphore createExportableSemaphore(); 54 VkSemaphore importSemaphoreFromSyncFd(int syncFd); 55 int exportSemaphoreSyncFd(VkSemaphore semaphore); 56 void destroySemaphore(VkSemaphore semaphore); 57 isInitialized()58 bool isInitialized() const { return mInitialized; } isRealtimePriority()59 bool isRealtimePriority() const { return mIsRealtimePriority; } getInstanceExtensionNames()60 const std::vector<std::string>& getInstanceExtensionNames() { return mInstanceExtensionNames; } getDeviceExtensionNames()61 const std::vector<std::string>& getDeviceExtensionNames() { return mDeviceExtensionNames; } 62 63 private: 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 80 // Note: keep all field defaults in sync with teardown() 81 bool mInitialized = false; 82 bool mIsOwned = false; 83 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; 89 skgpu::VulkanExtensions mGrExtensions; 90 VkPhysicalDeviceFeatures2* mPhysicalDeviceFeatures2 = nullptr; 91 VkPhysicalDeviceSamplerYcbcrConversionFeatures* mSamplerYcbcrConversionFeatures = nullptr; 92 VkPhysicalDeviceProtectedMemoryFeatures* mProtectedMemoryFeatures = nullptr; 93 VkPhysicalDeviceFaultFeaturesEXT* mDeviceFaultFeatures = nullptr; 94 skgpu::VulkanGetProc mGrGetProc = nullptr; 95 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 107