1 2 /* 3 * Copyright 2016 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 #ifndef GrVkTypes_DEFINED 10 #define GrVkTypes_DEFINED 11 12 #include "include/core/SkTypes.h" 13 #include "include/gpu/vk/GrVkVulkan.h" 14 15 #ifndef VK_VERSION_1_1 16 #error Skia requires the use of Vulkan 1.1 headers 17 #endif 18 19 #include <functional> 20 #include "include/gpu/GrTypes.h" 21 22 typedef intptr_t GrVkBackendMemory; 23 24 /** 25 * Types for interacting with Vulkan resources created externally to Skia. GrBackendObjects for 26 * Vulkan textures are really const GrVkImageInfo* 27 */ 28 struct GrVkAlloc { 29 // can be VK_NULL_HANDLE iff is an RT and is borrowed 30 VkDeviceMemory fMemory = VK_NULL_HANDLE; 31 VkDeviceSize fOffset = 0; 32 VkDeviceSize fSize = 0; // this can be indeterminate iff Tex uses borrow semantics 33 uint32_t fFlags = 0; 34 GrVkBackendMemory fBackendMemory = 0; // handle to memory allocated via GrVkMemoryAllocator. 35 36 enum Flag { 37 kNoncoherent_Flag = 0x1, // memory must be flushed to device after mapping 38 kMappable_Flag = 0x2, // memory is able to be mapped. 39 kLazilyAllocated_Flag = 0x4, // memory was created with lazy allocation 40 }; 41 42 bool operator==(const GrVkAlloc& that) const { 43 return fMemory == that.fMemory && fOffset == that.fOffset && fSize == that.fSize && 44 fFlags == that.fFlags && fUsesSystemHeap == that.fUsesSystemHeap; 45 } 46 47 private: 48 friend class GrVkHeap; // For access to usesSystemHeap 49 bool fUsesSystemHeap = false; 50 }; 51 52 // This struct is used to pass in the necessary information to create a VkSamplerYcbcrConversion 53 // object for an VkExternalFormatANDROID. 54 struct GrVkYcbcrConversionInfo { 55 bool operator==(const GrVkYcbcrConversionInfo& that) const { 56 // Invalid objects are not required to have all other fields initialized or matching. 57 if (!this->isValid() && !that.isValid()) { 58 return true; 59 } 60 return this->fFormat == that.fFormat && 61 this->fExternalFormat == that.fExternalFormat && 62 this->fYcbcrModel == that.fYcbcrModel && 63 this->fYcbcrRange == that.fYcbcrRange && 64 this->fXChromaOffset == that.fXChromaOffset && 65 this->fYChromaOffset == that.fYChromaOffset && 66 this->fChromaFilter == that.fChromaFilter && 67 this->fForceExplicitReconstruction == that.fForceExplicitReconstruction; 68 } 69 bool operator!=(const GrVkYcbcrConversionInfo& that) const { return !(*this == that); } 70 isValidGrVkYcbcrConversionInfo71 bool isValid() const { return fYcbcrModel != VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY; } 72 73 // Format of the source image. Must be set to VK_FORMAT_UNDEFINED for external images or 74 // a valid image format otherwise. 75 VkFormat fFormat = VK_FORMAT_UNDEFINED; 76 77 // The external format. Must be non-zero for external images, zero otherwise. 78 // Should be compatible to be used in a VkExternalFormatANDROID struct. 79 uint64_t fExternalFormat = 0; 80 81 VkSamplerYcbcrModelConversion fYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY; 82 VkSamplerYcbcrRange fYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL; 83 VkChromaLocation fXChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN; 84 VkChromaLocation fYChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN; 85 VkFilter fChromaFilter = VK_FILTER_NEAREST; 86 VkBool32 fForceExplicitReconstruction = false; 87 88 // For external images format features here should be those returned by a call to 89 // vkAndroidHardwareBufferFormatPropertiesANDROID 90 VkFormatFeatureFlags fFormatFeatures = 0; 91 }; 92 93 /* 94 * When wrapping a GrBackendTexture or GrBackendRendenderTarget, the fCurrentQueueFamily should 95 * either be VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_EXTERNAL, or VK_QUEUE_FAMILY_FOREIGN_EXT. If 96 * fSharingMode is VK_SHARING_MODE_EXCLUSIVE then fCurrentQueueFamily can also be the graphics 97 * queue index passed into Skia. 98 */ 99 struct GrVkImageInfo { 100 VkImage fImage = VK_NULL_HANDLE; 101 GrVkAlloc fAlloc; 102 VkImageTiling fImageTiling = VK_IMAGE_TILING_OPTIMAL; 103 VkImageLayout fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED; 104 VkFormat fFormat = VK_FORMAT_UNDEFINED; 105 VkImageUsageFlags fImageUsageFlags = 0; 106 uint32_t fSampleCount = 1; 107 uint32_t fLevelCount = 0; 108 uint32_t fCurrentQueueFamily = VK_QUEUE_FAMILY_IGNORED; 109 GrProtected fProtected = GrProtected::kNo; 110 GrVkYcbcrConversionInfo fYcbcrConversionInfo; 111 VkSharingMode fSharingMode = VK_SHARING_MODE_EXCLUSIVE; 112 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK 113 bool fPartOfSwapchainOrAndroidWindow = false; 114 #endif 115 116 #if GR_TEST_UTILS 117 bool operator==(const GrVkImageInfo& that) const { 118 bool equal = fImage == that.fImage && fAlloc == that.fAlloc && 119 fImageTiling == that.fImageTiling && 120 fImageLayout == that.fImageLayout && 121 fFormat == that.fFormat && 122 fImageUsageFlags == that.fImageUsageFlags && 123 fSampleCount == that.fSampleCount && 124 fLevelCount == that.fLevelCount && 125 fCurrentQueueFamily == that.fCurrentQueueFamily && 126 fProtected == that.fProtected && 127 fYcbcrConversionInfo == that.fYcbcrConversionInfo && 128 fSharingMode == that.fSharingMode; 129 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK 130 equal = equal && (fPartOfSwapchainOrAndroidWindow == that.fPartOfSwapchainOrAndroidWindow); 131 #endif 132 return equal; 133 } 134 #endif 135 }; 136 137 using GrVkGetProc = std::function<PFN_vkVoidFunction( 138 const char*, // function name 139 VkInstance, // instance or VK_NULL_HANDLE 140 VkDevice // device or VK_NULL_HANDLE 141 )>; 142 143 /** 144 * This object is wrapped in a GrBackendDrawableInfo and passed in as an argument to 145 * drawBackendGpu() calls on an SkDrawable. The drawable will use this info to inject direct 146 * Vulkan calls into our stream of GPU draws. 147 * 148 * The SkDrawable is given a secondary VkCommandBuffer in which to record draws. The GPU backend 149 * will then execute that command buffer within a render pass it is using for its own draws. The 150 * drawable is also given the attachment of the color index, a compatible VkRenderPass, and the 151 * VkFormat of the color attachment so that it can make VkPipeline objects for the draws. The 152 * SkDrawable must not alter the state of the VkRenderpass or sub pass. 153 * 154 * Additionally, the SkDrawable may fill in the passed in fDrawBounds with the bounds of the draws 155 * that it submits to the command buffer. This will be used by the GPU backend for setting the 156 * bounds in vkCmdBeginRenderPass. If fDrawBounds is not updated, we will assume that the entire 157 * attachment may have been written to. 158 * 159 * The SkDrawable is always allowed to create its own command buffers and submit them to the queue 160 * to render offscreen textures which will be sampled in draws added to the passed in 161 * VkCommandBuffer. If this is done the SkDrawable is in charge of adding the required memory 162 * barriers to the queue for the sampled images since the Skia backend will not do this. 163 */ 164 struct GrVkDrawableInfo { 165 VkCommandBuffer fSecondaryCommandBuffer; 166 uint32_t fColorAttachmentIndex; 167 VkRenderPass fCompatibleRenderPass; 168 VkFormat fFormat; 169 VkRect2D* fDrawBounds; 170 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK 171 bool fFromSwapchainOrAndroidWindow; 172 #endif 173 }; 174 175 struct GrVkSurfaceInfo { 176 uint32_t fSampleCount = 1; 177 uint32_t fLevelCount = 0; 178 GrProtected fProtected = GrProtected::kNo; 179 180 VkImageTiling fImageTiling = VK_IMAGE_TILING_OPTIMAL; 181 VkFormat fFormat = VK_FORMAT_UNDEFINED; 182 VkImageUsageFlags fImageUsageFlags = 0; 183 GrVkYcbcrConversionInfo fYcbcrConversionInfo; 184 VkSharingMode fSharingMode = VK_SHARING_MODE_EXCLUSIVE; 185 }; 186 187 #endif 188