1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef RS_VULKAN_CONTEXT_H 17 #define RS_VULKAN_CONTEXT_H 18 19 #include <memory> 20 #include <mutex> 21 #include <string> 22 #include "include/gpu/vk/GrVkExtensions.h" 23 #include "vulkan/vulkan_core.h" 24 25 #define VK_NO_PROTOTYPES 1 26 27 #include "vulkan/vulkan.h" 28 #include "include/gpu/vk/GrVkBackendContext.h" 29 #include "include/gpu/GrDirectContext.h" 30 31 #ifdef USE_ROSEN_DRAWING 32 #include "image/gpu_context.h" 33 #endif 34 35 namespace OHOS { 36 namespace Rosen { 37 class MemoryHandler; 38 class RsVulkanContext { 39 public: 40 template <class T> 41 class Func { 42 public: 43 using Proto = T; func_(proc)44 explicit Func(T proc = nullptr) : func_(proc) {} ~Func()45 ~Func() { func_ = nullptr; } 46 47 Func operator=(T proc) 48 { 49 func_ = proc; 50 return *this; 51 } 52 53 Func operator=(PFN_vkVoidFunction proc) 54 { 55 func_ = reinterpret_cast<Proto>(proc); 56 return *this; 57 } 58 59 operator bool() const { return func_ != nullptr; } T()60 operator T() const { return func_; } 61 private: 62 T func_; 63 }; 64 65 static RsVulkanContext& GetSingleton(); 66 67 RsVulkanContext(); 68 ~RsVulkanContext(); 69 bool CreateInstance(); 70 bool SelectPhysicalDevice(); 71 bool CreateDevice(); 72 bool CreateSkiaBackendContext(GrVkBackendContext* context, bool createNew = false); 73 74 bool IsValid() const; 75 GrVkGetProc CreateSkiaGetProc() const; GetMemoryHandler()76 const std::shared_ptr<MemoryHandler> GetMemoryHandler() const 77 { 78 return memHandler_; 79 } 80 81 #define DEFINE_FUNC(name) Func<PFN_vk##name> vk##name 82 83 DEFINE_FUNC(AcquireNextImageKHR); 84 DEFINE_FUNC(AllocateCommandBuffers); 85 DEFINE_FUNC(AllocateMemory); 86 DEFINE_FUNC(BeginCommandBuffer); 87 DEFINE_FUNC(BindImageMemory); 88 DEFINE_FUNC(BindImageMemory2); 89 DEFINE_FUNC(CmdPipelineBarrier); 90 DEFINE_FUNC(CreateCommandPool); 91 DEFINE_FUNC(CreateDebugReportCallbackEXT); 92 DEFINE_FUNC(CreateDevice); 93 DEFINE_FUNC(CreateFence); 94 DEFINE_FUNC(CreateImage); 95 DEFINE_FUNC(CreateImageView); 96 DEFINE_FUNC(CreateInstance); 97 DEFINE_FUNC(CreateSemaphore); 98 DEFINE_FUNC(CreateSwapchainKHR); 99 DEFINE_FUNC(DestroyCommandPool); 100 DEFINE_FUNC(DestroyDebugReportCallbackEXT); 101 DEFINE_FUNC(DestroyDevice); 102 DEFINE_FUNC(DestroyFence); 103 DEFINE_FUNC(DestroyImage); 104 DEFINE_FUNC(DestroyImageView); 105 DEFINE_FUNC(DestroyInstance); 106 DEFINE_FUNC(DestroySemaphore); 107 DEFINE_FUNC(DestroySurfaceKHR); 108 DEFINE_FUNC(DestroySwapchainKHR); 109 DEFINE_FUNC(DeviceWaitIdle); 110 DEFINE_FUNC(EndCommandBuffer); 111 DEFINE_FUNC(EnumerateDeviceLayerProperties); 112 DEFINE_FUNC(EnumerateInstanceExtensionProperties); 113 DEFINE_FUNC(EnumerateInstanceLayerProperties); 114 DEFINE_FUNC(EnumeratePhysicalDevices); 115 DEFINE_FUNC(FreeCommandBuffers); 116 DEFINE_FUNC(FreeMemory); 117 DEFINE_FUNC(GetDeviceProcAddr); 118 DEFINE_FUNC(GetDeviceQueue); 119 DEFINE_FUNC(GetImageMemoryRequirements); 120 DEFINE_FUNC(GetInstanceProcAddr); 121 DEFINE_FUNC(GetPhysicalDeviceFeatures); 122 DEFINE_FUNC(GetPhysicalDeviceQueueFamilyProperties); 123 DEFINE_FUNC(QueueSubmit); 124 DEFINE_FUNC(QueueWaitIdle); 125 DEFINE_FUNC(ResetCommandBuffer); 126 DEFINE_FUNC(ResetFences); 127 DEFINE_FUNC(WaitForFences); 128 DEFINE_FUNC(GetPhysicalDeviceSurfaceCapabilitiesKHR); 129 DEFINE_FUNC(GetPhysicalDeviceSurfaceFormatsKHR); 130 DEFINE_FUNC(GetPhysicalDeviceSurfacePresentModesKHR); 131 DEFINE_FUNC(GetPhysicalDeviceSurfaceSupportKHR); 132 DEFINE_FUNC(GetSwapchainImagesKHR); 133 DEFINE_FUNC(QueuePresentKHR); 134 DEFINE_FUNC(CreateSurfaceOHOS); 135 DEFINE_FUNC(GetPhysicalDeviceMemoryProperties); 136 DEFINE_FUNC(GetPhysicalDeviceMemoryProperties2); 137 DEFINE_FUNC(GetNativeBufferPropertiesOHOS); 138 DEFINE_FUNC(QueueSignalReleaseImageOHOS); 139 DEFINE_FUNC(ImportSemaphoreFdKHR); 140 DEFINE_FUNC(GetPhysicalDeviceFeatures2); 141 #undef DEFINE_FUNC 142 GetPhysicalDevice()143 VkPhysicalDevice GetPhysicalDevice() const 144 { 145 return physicalDevice_; 146 } 147 GetDevice()148 VkDevice GetDevice() const 149 { 150 return device_; 151 } 152 GetQueue()153 VkQueue GetQueue() const 154 { 155 return queue_; 156 } 157 GetGrVkBackendContext()158 inline const GrVkBackendContext& GetGrVkBackendContext() const noexcept 159 { 160 return backendContext_; 161 } 162 GetVulkanVersion()163 inline const std::string GetVulkanVersion() const 164 { 165 return std::to_string(VK_API_VERSION_1_2); 166 } 167 168 #ifndef USE_ROSEN_DRAWING 169 sk_sp<GrDirectContext> CreateSkContext(bool independentContext = false); 170 sk_sp<GrDirectContext> GetSkContext(); 171 #else 172 std::shared_ptr<Drawing::GPUContext> CreateDrawingContext(bool independentContext = false); 173 std::shared_ptr<Drawing::GPUContext> GetDrawingContext(); 174 #endif 175 176 static VKAPI_ATTR VkResult HookedVkQueueSubmit(VkQueue queue, uint32_t submitCount, 177 const VkSubmitInfo* pSubmits, VkFence fence); 178 179 static VKAPI_ATTR VkResult HookedVkQueueSignalReleaseImageOHOS(VkQueue queue, uint32_t waitSemaphoreCount, 180 const VkSemaphore* pWaitSemaphores, VkImage image, int32_t* pNativeFenceFd); 181 #ifndef USE_ROSEN_DRAWING GetHardWareGrContext()182 sk_sp<GrDirectContext> GetHardWareGrContext() const 183 #else 184 std::shared_ptr<Drawing::GPUContext> GetHardWareGrContext() const 185 #endif 186 { 187 return hcontext_; 188 } 189 GetHardwareQueue()190 VkQueue GetHardwareQueue() const 191 { 192 return hbackendContext_.fQueue; 193 } 194 private: 195 std::mutex vkMutex_; 196 std::mutex graphicsQueueMutex_; 197 void* handle_; 198 bool acquiredMandatoryProcAddresses_; 199 VkInstance instance_ = VK_NULL_HANDLE; 200 VkPhysicalDevice physicalDevice_ = VK_NULL_HANDLE; 201 uint32_t graphicsQueueFamilyIndex_ = UINT32_MAX; 202 VkDevice device_ = VK_NULL_HANDLE; 203 VkQueue hardwareQueue_ = VK_NULL_HANDLE; 204 VkQueue queue_ = VK_NULL_HANDLE; 205 VkPhysicalDeviceFeatures2 physicalDeviceFeatures2_; 206 VkPhysicalDeviceSamplerYcbcrConversionFeatures ycbcrFeature_; 207 GrVkExtensions skVkExtensions_; 208 #ifndef USE_ROSEN_DRAWING 209 static thread_local sk_sp<GrDirectContext> skContext_; 210 sk_sp<GrDirectContext> hcontext_ = nullptr; 211 #else 212 static thread_local std::shared_ptr<Drawing::GPUContext> drawingContext_; 213 std::shared_ptr<Drawing::GPUContext> hcontext_ = nullptr; 214 #endif 215 // static thread_local GrVkBackendContext backendContext_; 216 GrVkBackendContext backendContext_; 217 GrVkBackendContext hbackendContext_; 218 219 RsVulkanContext(const RsVulkanContext &) = delete; 220 RsVulkanContext &operator=(const RsVulkanContext &) = delete; 221 222 RsVulkanContext(RsVulkanContext &&) = delete; 223 RsVulkanContext &operator=(RsVulkanContext &&) = delete; 224 225 bool OpenLibraryHandle(); 226 bool SetupLoaderProcAddresses(); 227 bool CloseLibraryHandle(); 228 bool SetupDeviceProcAddresses(VkDevice device); 229 PFN_vkVoidFunction AcquireProc( 230 const char* proc_name, 231 const VkInstance& instance) const; 232 PFN_vkVoidFunction AcquireProc(const char* proc_name, const VkDevice& device) const; 233 #ifndef USE_ROSEN_DRAWING 234 sk_sp<GrDirectContext> CreateNewSkContext(); 235 #else 236 std::shared_ptr<Drawing::GPUContext> CreateNewDrawingContext(); 237 #endif 238 std::shared_ptr<MemoryHandler> memHandler_; 239 }; 240 241 } // namespace Rosen 242 } // namespace OHOS 243 244 #endif