1 /* 2 * Copyright 2016 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef GrVkBackendContext_DEFINED 9 #define GrVkBackendContext_DEFINED 10 11 #include "SkRefCnt.h" 12 13 #include "vk/GrVkDefines.h" 14 #include "vk/GrVkInterface.h" 15 16 enum GrVkExtensionFlags { 17 kEXT_debug_report_GrVkExtensionFlag = 0x0001, 18 kNV_glsl_shader_GrVkExtensionFlag = 0x0002, 19 kKHR_surface_GrVkExtensionFlag = 0x0004, 20 kKHR_swapchain_GrVkExtensionFlag = 0x0008, 21 kKHR_win32_surface_GrVkExtensionFlag = 0x0010, 22 kKHR_android_surface_GrVkExtensionFlag = 0x0020, 23 kKHR_xcb_surface_GrVkExtensionFlag = 0x0040, 24 }; 25 26 enum GrVkFeatureFlags { 27 kGeometryShader_GrVkFeatureFlag = 0x0001, 28 kDualSrcBlend_GrVkFeatureFlag = 0x0002, 29 kSampleRateShading_GrVkFeatureFlag = 0x0004, 30 }; 31 32 // The BackendContext contains all of the base Vulkan objects needed by the GrVkGpu. The assumption 33 // is that the client will set these up and pass them to the GrVkGpu constructor. The VkDevice 34 // created must support at least one graphics queue, which is passed in as well. 35 // The QueueFamilyIndex must match the family of the given queue. It is needed for CommandPool 36 // creation, and any GrBackendObjects handed to us (e.g., for wrapped textures) need to be created 37 // in or transitioned to that family. 38 struct GrVkBackendContext : public SkRefCnt { 39 VkInstance fInstance; 40 VkPhysicalDevice fPhysicalDevice; 41 VkDevice fDevice; 42 VkQueue fQueue; 43 uint32_t fGraphicsQueueIndex; 44 uint32_t fMinAPIVersion; 45 uint32_t fExtensions; 46 uint32_t fFeatures; 47 sk_sp<const GrVkInterface> fInterface; 48 /** 49 * Controls whether this object destroys the instance and device upon destruction. The default 50 * is temporarily 'true' to avoid breaking existing clients but will be changed to 'false'. 51 */ 52 bool fOwnsInstanceAndDevice = true; 53 54 using CanPresentFn = std::function<bool(VkInstance, VkPhysicalDevice, 55 uint32_t queueFamilyIndex)>; 56 57 /** 58 * Helper function to create the Vulkan objects needed for a Vulkan-backed GrContext. 59 * Note that the version that uses the unified "GetProc" instead of separate "GetInstanceProc" 60 * and "GetDeviceProc" functions will be removed. 61 * 62 * If presentQueueIndex is non-NULL, will try to set up presentQueue as part of device 63 * creation using the platform-specific canPresent() function. 64 * 65 * This will set fOwnsInstanceAndDevice to 'true'. If it is subsequently set to 'false' then 66 * the client owns the lifetime of the created VkDevice and VkInstance. 67 */ 68 static const GrVkBackendContext* Create(uint32_t* presentQueueIndex = nullptr, 69 CanPresentFn = CanPresentFn(), 70 GrVkInterface::GetProc getProc = nullptr); 71 72 static const GrVkBackendContext* Create(const GrVkInterface::GetInstanceProc& getInstanceProc, 73 const GrVkInterface::GetDeviceProc& getDeviceProc, 74 uint32_t* presentQueueIndex = nullptr, 75 CanPresentFn canPresent = CanPresentFn()) { 76 if (!getInstanceProc || !getDeviceProc) { 77 return nullptr; 78 } 79 auto getProc = [&getInstanceProc, &getDeviceProc](const char* proc_name, 80 VkInstance instance, VkDevice device) { 81 if (device != VK_NULL_HANDLE) { 82 return getDeviceProc(device, proc_name); 83 } 84 return getInstanceProc(instance, proc_name); 85 }; 86 return Create(presentQueueIndex, canPresent, getProc); 87 } 88 89 ~GrVkBackendContext() override; 90 }; 91 92 #endif 93