1 /* 2 * Copyright (C) 2016 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 #ifndef VULKANMANAGER_H 18 #define VULKANMANAGER_H 19 20 #if !defined(VK_USE_PLATFORM_ANDROID_KHR) 21 #define VK_USE_PLATFORM_ANDROID_KHR 22 #endif 23 #include <vulkan/vulkan.h> 24 25 #include <GrContextOptions.h> 26 #include <SkSurface.h> 27 #include <ui/Fence.h> 28 #include <utils/StrongPointer.h> 29 #include <vk/GrVkBackendContext.h> 30 #include <vk/GrVkExtensions.h> 31 #include "Frame.h" 32 #include "IRenderPipeline.h" 33 #include "VulkanSurface.h" 34 35 class GrVkExtensions; 36 37 namespace android { 38 namespace uirenderer { 39 namespace renderthread { 40 41 class RenderThread; 42 43 // This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue, 44 // which are re-used by CanvasContext. This class is created once and should be used by all vulkan 45 // windowing contexts. The VulkanManager must be initialized before use. 46 class VulkanManager { 47 public: VulkanManager()48 explicit VulkanManager() {} ~VulkanManager()49 ~VulkanManager() { destroy(); } 50 51 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must 52 // be call once before use of the VulkanManager. Multiple calls after the first will simiply 53 // return. 54 void initialize(); 55 56 // Quick check to see if the VulkanManager has been initialized. hasVkContext()57 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; } 58 59 // Create and destroy functions for wrapping an ANativeWindow in a VulkanSurface 60 VulkanSurface* createSurface(ANativeWindow* window, ColorMode colorMode, 61 sk_sp<SkColorSpace> surfaceColorSpace, 62 SkColorType surfaceColorType, GrContext* grContext, 63 uint32_t extraBuffers); 64 void destroySurface(VulkanSurface* surface); 65 66 Frame dequeueNextBuffer(VulkanSurface* surface); 67 void swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect); 68 69 // Cleans up all the global state in the VulkanManger. 70 void destroy(); 71 72 // Inserts a wait on fence command into the Vulkan command buffer. 73 status_t fenceWait(sp<Fence>& fence, GrContext* grContext); 74 75 // Creates a fence that is signaled when all the pending Vulkan commands are finished on the 76 // GPU. 77 status_t createReleaseFence(sp<Fence>& nativeFence, GrContext* grContext); 78 79 // Returned pointers are owned by VulkanManager. 80 // An instance of VkFunctorInitParams returned from getVkFunctorInitParams refers to 81 // the internal state of VulkanManager: VulkanManager must be alive to use the returned value. 82 VkFunctorInitParams getVkFunctorInitParams() const; 83 84 sk_sp<GrContext> createContext(const GrContextOptions& options); 85 getDriverVersion()86 uint32_t getDriverVersion() const { return mDriverVersion; } 87 88 private: 89 friend class VulkanSurface; 90 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in 91 // VkPhysicalDeviceFeatures struct. 92 void setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&); 93 94 // simple wrapper class that exists only to initialize a pointer to NULL 95 template <typename FNPTR_TYPE> 96 class VkPtr { 97 public: VkPtr()98 VkPtr() : fPtr(NULL) {} 99 VkPtr operator=(FNPTR_TYPE ptr) { 100 fPtr = ptr; 101 return *this; 102 } 103 // NOLINTNEXTLINE(google-explicit-constructor) FNPTR_TYPE()104 operator FNPTR_TYPE() const { return fPtr; } 105 106 private: 107 FNPTR_TYPE fPtr; 108 }; 109 110 // WSI interface functions 111 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR; 112 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR; 113 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR; 114 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR; 115 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR; 116 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR; 117 118 // Instance Functions 119 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion; 120 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties; 121 VkPtr<PFN_vkCreateInstance> mCreateInstance; 122 123 VkPtr<PFN_vkDestroyInstance> mDestroyInstance; 124 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices; 125 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties; 126 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties; 127 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2; 128 VkPtr<PFN_vkGetPhysicalDeviceImageFormatProperties2> mGetPhysicalDeviceImageFormatProperties2; 129 VkPtr<PFN_vkCreateDevice> mCreateDevice; 130 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties; 131 132 // Device Functions 133 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue; 134 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle; 135 VkPtr<PFN_vkDestroyDevice> mDestroyDevice; 136 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool; 137 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool; 138 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers; 139 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers; 140 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer; 141 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer; 142 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer; 143 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier; 144 145 VkPtr<PFN_vkQueueSubmit> mQueueSubmit; 146 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle; 147 148 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore; 149 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore; 150 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR; 151 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR; 152 VkPtr<PFN_vkCreateFence> mCreateFence; 153 VkPtr<PFN_vkDestroyFence> mDestroyFence; 154 VkPtr<PFN_vkWaitForFences> mWaitForFences; 155 VkPtr<PFN_vkResetFences> mResetFences; 156 157 VkInstance mInstance = VK_NULL_HANDLE; 158 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE; 159 VkDevice mDevice = VK_NULL_HANDLE; 160 161 uint32_t mGraphicsQueueIndex; 162 VkQueue mGraphicsQueue = VK_NULL_HANDLE; 163 uint32_t mPresentQueueIndex; 164 VkQueue mPresentQueue = VK_NULL_HANDLE; 165 VkCommandPool mCommandPool = VK_NULL_HANDLE; 166 167 // Variables saved to populate VkFunctorInitParams. 168 static const uint32_t mAPIVersion = VK_MAKE_VERSION(1, 1, 0); 169 std::vector<VkExtensionProperties> mInstanceExtensionsOwner; 170 std::vector<const char*> mInstanceExtensions; 171 std::vector<VkExtensionProperties> mDeviceExtensionsOwner; 172 std::vector<const char*> mDeviceExtensions; 173 VkPhysicalDeviceFeatures2 mPhysicalDeviceFeatures2{}; 174 175 enum class SwapBehavior { 176 Discard, 177 BufferAge, 178 }; 179 SwapBehavior mSwapBehavior = SwapBehavior::Discard; 180 GrVkExtensions mExtensions; 181 uint32_t mDriverVersion = 0; 182 }; 183 184 } /* namespace renderthread */ 185 } /* namespace uirenderer */ 186 } /* namespace android */ 187 188 #endif /* VULKANMANAGER_H */ 189