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 <GrContextOptions.h> 24 #include <SkSurface.h> 25 #include <utils/StrongPointer.h> 26 #include <vk/GrVkBackendContext.h> 27 #include <vk/GrVkExtensions.h> 28 #include <vulkan/vulkan.h> 29 30 #include "Frame.h" 31 #include "IRenderPipeline.h" 32 #include "VulkanSurface.h" 33 #include "private/hwui/DrawVkInfo.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(int 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(int* 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 // Instance Functions 111 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion; 112 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties; 113 VkPtr<PFN_vkCreateInstance> mCreateInstance; 114 115 VkPtr<PFN_vkDestroyInstance> mDestroyInstance; 116 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices; 117 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties; 118 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties; 119 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2; 120 VkPtr<PFN_vkGetPhysicalDeviceImageFormatProperties2> mGetPhysicalDeviceImageFormatProperties2; 121 VkPtr<PFN_vkCreateDevice> mCreateDevice; 122 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties; 123 124 // Device Functions 125 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue; 126 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle; 127 VkPtr<PFN_vkDestroyDevice> mDestroyDevice; 128 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool; 129 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool; 130 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers; 131 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers; 132 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer; 133 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer; 134 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer; 135 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier; 136 137 VkPtr<PFN_vkQueueSubmit> mQueueSubmit; 138 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle; 139 140 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore; 141 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore; 142 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR; 143 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR; 144 VkPtr<PFN_vkCreateFence> mCreateFence; 145 VkPtr<PFN_vkDestroyFence> mDestroyFence; 146 VkPtr<PFN_vkWaitForFences> mWaitForFences; 147 VkPtr<PFN_vkResetFences> mResetFences; 148 149 VkInstance mInstance = VK_NULL_HANDLE; 150 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE; 151 VkDevice mDevice = VK_NULL_HANDLE; 152 153 uint32_t mGraphicsQueueIndex; 154 VkQueue mGraphicsQueue = VK_NULL_HANDLE; 155 uint32_t mPresentQueueIndex; 156 VkQueue mPresentQueue = VK_NULL_HANDLE; 157 VkCommandPool mCommandPool = VK_NULL_HANDLE; 158 159 // Variables saved to populate VkFunctorInitParams. 160 static const uint32_t mAPIVersion = VK_MAKE_VERSION(1, 1, 0); 161 std::vector<VkExtensionProperties> mInstanceExtensionsOwner; 162 std::vector<const char*> mInstanceExtensions; 163 std::vector<VkExtensionProperties> mDeviceExtensionsOwner; 164 std::vector<const char*> mDeviceExtensions; 165 VkPhysicalDeviceFeatures2 mPhysicalDeviceFeatures2{}; 166 167 enum class SwapBehavior { 168 Discard, 169 BufferAge, 170 }; 171 SwapBehavior mSwapBehavior = SwapBehavior::Discard; 172 GrVkExtensions mExtensions; 173 uint32_t mDriverVersion = 0; 174 }; 175 176 } /* namespace renderthread */ 177 } /* namespace uirenderer */ 178 } /* namespace android */ 179 180 #endif /* VULKANMANAGER_H */ 181