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 }; 40 41 bool operator==(const GrVkAlloc& that) const { 42 return fMemory == that.fMemory && fOffset == that.fOffset && fSize == that.fSize && 43 fFlags == that.fFlags && fUsesSystemHeap == that.fUsesSystemHeap; 44 } 45 46 private: 47 friend class GrVkHeap; // For access to usesSystemHeap 48 bool fUsesSystemHeap = false; 49 }; 50 51 // This struct is used to pass in the necessary information to create a VkSamplerYcbcrConversion 52 // object for an VkExternalFormatANDROID. 53 struct GrVkYcbcrConversionInfo { 54 bool operator==(const GrVkYcbcrConversionInfo& that) const { 55 // Invalid objects are not required to have all other fields initialized or matching. 56 if (!this->isValid() && !that.isValid()) { 57 return true; 58 } 59 return this->fFormat == that.fFormat && 60 this->fExternalFormat == that.fExternalFormat && 61 this->fYcbcrModel == that.fYcbcrModel && 62 this->fYcbcrRange == that.fYcbcrRange && 63 this->fXChromaOffset == that.fXChromaOffset && 64 this->fYChromaOffset == that.fYChromaOffset && 65 this->fChromaFilter == that.fChromaFilter && 66 this->fForceExplicitReconstruction == that.fForceExplicitReconstruction; 67 } 68 bool operator!=(const GrVkYcbcrConversionInfo& that) const { return !(*this == that); } 69 isValidGrVkYcbcrConversionInfo70 bool isValid() const { return fYcbcrModel != VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY; } 71 72 // Format of the source image. Must be set to VK_FORMAT_UNDEFINED for external images or 73 // a valid image format otherwise. 74 VkFormat fFormat = VK_FORMAT_UNDEFINED; 75 76 // The external format. Must be non-zero for external images, zero otherwise. 77 // Should be compatible to be used in a VkExternalFormatANDROID struct. 78 uint64_t fExternalFormat = 0; 79 80 VkSamplerYcbcrModelConversion fYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY; 81 VkSamplerYcbcrRange fYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL; 82 VkChromaLocation fXChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN; 83 VkChromaLocation fYChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN; 84 VkFilter fChromaFilter = VK_FILTER_NEAREST; 85 VkBool32 fForceExplicitReconstruction = false; 86 87 // For external images format features here should be those returned by a call to 88 // vkAndroidHardwareBufferFormatPropertiesANDROID 89 VkFormatFeatureFlags fFormatFeatures = 0; 90 }; 91 92 /* 93 * When wrapping a GrBackendTexture or GrBackendRendenderTarget, the fCurrentQueueFamily should 94 * either be VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_EXTERNAL, or VK_QUEUE_FAMILY_FOREIGN_EXT. If 95 * fSharingMode is VK_SHARING_MODE_EXCLUSIVE then fCurrentQueueFamily can also be the graphics 96 * queue index passed into Skia. 97 */ 98 struct GrVkImageInfo { 99 VkImage fImage = VK_NULL_HANDLE; 100 GrVkAlloc fAlloc; 101 VkImageTiling fImageTiling = VK_IMAGE_TILING_OPTIMAL; 102 VkImageLayout fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED; 103 VkFormat fFormat = VK_FORMAT_UNDEFINED; 104 VkImageUsageFlags fImageUsageFlags = 0; 105 uint32_t fSampleCount = 1; 106 uint32_t fLevelCount = 0; 107 uint32_t fCurrentQueueFamily = VK_QUEUE_FAMILY_IGNORED; 108 GrProtected fProtected = GrProtected::kNo; 109 GrVkYcbcrConversionInfo fYcbcrConversionInfo; 110 VkSharingMode fSharingMode = VK_SHARING_MODE_EXCLUSIVE; 111 112 #if GR_TEST_UTILS 113 bool operator==(const GrVkImageInfo& that) const { 114 return fImage == that.fImage && fAlloc == that.fAlloc && 115 fImageTiling == that.fImageTiling && fImageLayout == that.fImageLayout && 116 fFormat == that.fFormat && fImageUsageFlags == that.fImageUsageFlags && 117 fSampleCount == that.fSampleCount && fLevelCount == that.fLevelCount && 118 fCurrentQueueFamily == that.fCurrentQueueFamily && fProtected == that.fProtected && 119 fYcbcrConversionInfo == that.fYcbcrConversionInfo && 120 fSharingMode == that.fSharingMode; 121 } 122 #endif 123 }; 124 125 using GrVkGetProc = std::function<PFN_vkVoidFunction( 126 const char*, // function name 127 VkInstance, // instance or VK_NULL_HANDLE 128 VkDevice // device or VK_NULL_HANDLE 129 )>; 130 131 /** 132 * This object is wrapped in a GrBackendDrawableInfo and passed in as an argument to 133 * drawBackendGpu() calls on an SkDrawable. The drawable will use this info to inject direct 134 * Vulkan calls into our stream of GPU draws. 135 * 136 * The SkDrawable is given a secondary VkCommandBuffer in which to record draws. The GPU backend 137 * will then execute that command buffer within a render pass it is using for its own draws. The 138 * drawable is also given the attachment of the color index, a compatible VkRenderPass, and the 139 * VkFormat of the color attachment so that it can make VkPipeline objects for the draws. The 140 * SkDrawable must not alter the state of the VkRenderpass or sub pass. 141 * 142 * Additionally, the SkDrawable may fill in the passed in fDrawBounds with the bounds of the draws 143 * that it submits to the command buffer. This will be used by the GPU backend for setting the 144 * bounds in vkCmdBeginRenderPass. If fDrawBounds is not updated, we will assume that the entire 145 * attachment may have been written to. 146 * 147 * The SkDrawable is always allowed to create its own command buffers and submit them to the queue 148 * to render offscreen textures which will be sampled in draws added to the passed in 149 * VkCommandBuffer. If this is done the SkDrawable is in charge of adding the required memory 150 * barriers to the queue for the sampled images since the Skia backend will not do this. 151 * 152 * The VkImage is informational only and should not be used or modified in any ways. 153 */ 154 struct GrVkDrawableInfo { 155 VkCommandBuffer fSecondaryCommandBuffer; 156 uint32_t fColorAttachmentIndex; 157 VkRenderPass fCompatibleRenderPass; 158 VkFormat fFormat; 159 VkRect2D* fDrawBounds; 160 VkImage fImage; 161 }; 162 163 #endif 164