| 1 |
/* |
| 2 |
* Copyright 2022 Google LLC. |
| 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 skgpu_VulkanTypes_DEFINED |
| 9 |
#define skgpu_VulkanTypes_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkTypes.h" |
| 12 |
#include "include/private/base/SkTo.h" |
| 13 |
#include "include/private/gpu/vk/SkiaVulkan.h" |
| 14 |
|
| 15 |
#include <cstddef> |
| 16 |
#include <cstdint> |
| 17 |
#include <functional> |
| 18 |
#include <optional> |
| 19 |
#include <string> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
#ifndef VK_VERSION_1_1 |
| 23 |
#error Skia requires the use of Vulkan 1.1 headers |
| 24 |
#endif |
| 25 |
|
| 26 |
namespace skgpu { |
| 27 |
|
| 28 |
namespace graphite { |
| 29 |
class VulkanYcbcrConversion; |
| 30 |
} |
| 31 |
|
| 32 |
using VulkanGetProc = std::function<PFN_vkVoidFunction( |
| 33 |
const char*, // function name |
| 34 |
VkInstance, // instance or VK_NULL_HANDLE |
| 35 |
VkDevice // device or VK_NULL_HANDLE |
| 36 |
)>; |
| 37 |
|
| 38 |
typedef intptr_t VulkanBackendMemory; |
| 39 |
|
| 40 |
/** |
| 41 |
* Types for interacting with Vulkan resources created externally to Skia. |
| 42 |
*/ |
| 43 |
struct VulkanAlloc { |
| 44 |
// can be VK_NULL_HANDLE iff is an RT and is borrowed |
| 45 |
VkDeviceMemory fMemory = VK_NULL_HANDLE; |
| 46 |
VkDeviceSize fOffset = 0; |
| 47 |
VkDeviceSize fSize = 0; // this can be indeterminate iff Tex uses borrow semantics |
| 48 |
uint32_t fFlags = 0; |
| 49 |
// handle to memory allocated via skgpu::VulkanMemoryAllocator. |
| 50 |
VulkanBackendMemory fBackendMemory = 0; |
| 51 |
|
| 52 |
enum Flag { |
| 53 |
kNoncoherent_Flag = 0x1, // memory must be flushed to device after mapping |
| 54 |
kMappable_Flag = 0x2, // memory is able to be mapped. |
| 55 |
kLazilyAllocated_Flag = 0x4, // memory was created with lazy allocation |
| 56 |
}; |
| 57 |
|
| 58 |
bool operator==(const VulkanAlloc& that) const { |
| 59 |
return fMemory == that.fMemory && fOffset == that.fOffset && fSize == that.fSize && |
| 60 |
fFlags == that.fFlags && fUsesSystemHeap == that.fUsesSystemHeap; |
| 61 |
} |
| 62 |
|
| 63 |
private: |
| 64 |
bool fUsesSystemHeap = false; |
| 65 |
}; |
| 66 |
|
| 67 |
// Used to pass in the necessary information to create a VkSamplerYcbcrConversion object for an |
| 68 |
// VkExternalFormatANDROID. |
| 69 |
struct SK_API VulkanYcbcrConversionInfo { |
| 70 |
public: |
| 71 |
// Makes an invalid VulkanYcbcrConversionInfo |
| 72 |
VulkanYcbcrConversionInfo() = default; |
| 73 |
|
| 74 |
// For external images format features here should be those returned by a call to |
| 75 |
// vkAndroidHardwareBufferFormatPropertiesANDROID |
| 76 |
VulkanYcbcrConversionInfo(uint64_t externalFormat, |
| 77 |
VkSamplerYcbcrModelConversion ycbcrModel, |
| 78 |
VkSamplerYcbcrRange ycbcrRange, |
| 79 |
VkChromaLocation xChromaOffset, |
| 80 |
VkChromaLocation yChromaOffset, |
| 81 |
VkFilter chromaFilter, |
| 82 |
VkBool32 forceExplicitReconstruction, |
| 83 |
VkComponentMapping components, |
| 84 |
VkFormatFeatureFlags formatFeatures) |
| 85 |
: VulkanYcbcrConversionInfo(VK_FORMAT_UNDEFINED, |
| 86 |
externalFormat, |
| 87 |
ycbcrModel, |
| 88 |
ycbcrRange, |
| 89 |
xChromaOffset, |
| 90 |
yChromaOffset, |
| 91 |
chromaFilter, |
| 92 |
forceExplicitReconstruction, |
| 93 |
components, |
| 94 |
formatFeatures) {} |
| 95 |
|
| 96 |
VulkanYcbcrConversionInfo(VkFormat format, |
| 97 |
VkSamplerYcbcrModelConversion ycbcrModel, |
| 98 |
VkSamplerYcbcrRange ycbcrRange, |
| 99 |
VkChromaLocation xChromaOffset, |
| 100 |
VkChromaLocation yChromaOffset, |
| 101 |
VkFilter chromaFilter, |
| 102 |
VkBool32 forceExplicitReconstruction, |
| 103 |
VkComponentMapping components, |
| 104 |
VkFormatFeatureFlags formatFeatures) |
| 105 |
: VulkanYcbcrConversionInfo(format, |
| 106 |
0, |
| 107 |
ycbcrModel, |
| 108 |
ycbcrRange, |
| 109 |
xChromaOffset, |
| 110 |
yChromaOffset, |
| 111 |
chromaFilter, |
| 112 |
forceExplicitReconstruction, |
| 113 |
components, |
| 114 |
formatFeatures) {} |
| 115 |
|
| 116 |
bool operator==(const VulkanYcbcrConversionInfo& that) const { |
| 117 |
// Invalid objects are not required to have all other fields initialized or matching. |
| 118 |
if (!this->isValid() && !that.isValid()) { |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
// Note that we do not need to check for fSamplerFilterMustMatchChromaFilter equality. This |
| 123 |
// is because the Vulkan spec dictates that Android hardware buffers with the same external |
| 124 |
// format must have the same support for key features. See |
| 125 |
// https://docs.vulkan.org/spec/latest/chapters/memory.html#_android_hardware_buffer_external_memory |
| 126 |
// for more details. |
| 127 |
return this->fFormat == that.fFormat && |
| 128 |
this->fExternalFormat == that.fExternalFormat && |
| 129 |
this->fYcbcrModel == that.fYcbcrModel && |
| 130 |
this->fYcbcrRange == that.fYcbcrRange && |
| 131 |
this->fXChromaOffset == that.fXChromaOffset && |
| 132 |
this->fYChromaOffset == that.fYChromaOffset && |
| 133 |
this->fChromaFilter == that.fChromaFilter && |
| 134 |
this->fForceExplicitReconstruction == that.fForceExplicitReconstruction && |
| 135 |
this->fComponents.r == that.fComponents.r && |
| 136 |
this->fComponents.g == that.fComponents.g && |
| 137 |
this->fComponents.b == that.fComponents.b && |
| 138 |
this->fComponents.a == that.fComponents.a; |
| 139 |
} |
| 140 |
bool operator!=(const VulkanYcbcrConversionInfo& that) const { return !(*this == that); } |
| 141 |
|
| 142 |
bool isValid() const { |
| 143 |
return fYcbcrModel != VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY || |
| 144 |
this->hasExternalFormat(); |
| 145 |
} |
| 146 |
|
| 147 |
VkFormat format() const { return fFormat; } |
| 148 |
|
| 149 |
bool hasExternalFormat() const { return fExternalFormat != 0; } |
| 150 |
uint64_t externalFormat() const { return fExternalFormat; } |
| 151 |
|
| 152 |
VkSamplerYcbcrModelConversion model() const { return fYcbcrModel; } |
| 153 |
VkSamplerYcbcrRange range() const { return fYcbcrRange; } |
| 154 |
VkChromaLocation xChromaOffset() const { return fXChromaOffset; } |
| 155 |
VkChromaLocation yChromaOffset() const { return fYChromaOffset; } |
| 156 |
VkFilter chromaFilter() const { return fChromaFilter; } |
| 157 |
VkBool32 forceExplicitReconstruction() const { return fForceExplicitReconstruction; } |
| 158 |
VkComponentMapping components() const { return fComponents; } |
| 159 |
|
| 160 |
bool samplerFilterMustMatchChromaFilter() const { return fSamplerFilterMustMatchChromaFilter; } |
| 161 |
bool supportsLinearFilter() const { return fSupportsLinearFilter; } |
| 162 |
|
| 163 |
// Returns a populated VkSamplerYcbcrConversionCreateInfo object based on |
| 164 |
// VulkanYcbcrConversionInfo |
| 165 |
void toVkSamplerYcbcrConversionCreateInfo(VkSamplerYcbcrConversionCreateInfo* outInfo, |
| 166 |
std::optional<VkFilter>* requiredSamplerFilter) const; |
| 167 |
|
| 168 |
private: |
| 169 |
VulkanYcbcrConversionInfo(VkFormat format, |
| 170 |
uint64_t externalFormat, |
| 171 |
VkSamplerYcbcrModelConversion ycbcrModel, |
| 172 |
VkSamplerYcbcrRange ycbcrRange, |
| 173 |
VkChromaLocation xChromaOffset, |
| 174 |
VkChromaLocation yChromaOffset, |
| 175 |
VkFilter chromaFilter, |
| 176 |
VkBool32 forceExplicitReconstruction, |
| 177 |
VkComponentMapping components, |
| 178 |
VkFormatFeatureFlags formatFeatures); |
| 179 |
|
| 180 |
|
| 181 |
// For use internally to recreate from an ImmutableSamplerInfo. We don't have feature flags |
| 182 |
// and assume we've updated all the fields as required by the feature flags previously. |
| 183 |
friend class graphite::VulkanYcbcrConversion; |
| 184 |
|
| 185 |
VulkanYcbcrConversionInfo(VkFormat format, |
| 186 |
uint64_t externalFormat, |
| 187 |
VkSamplerYcbcrModelConversion ycbcrModel, |
| 188 |
VkSamplerYcbcrRange ycbcrRange, |
| 189 |
VkChromaLocation xChromaOffset, |
| 190 |
VkChromaLocation yChromaOffset, |
| 191 |
VkFilter chromaFilter, |
| 192 |
VkBool32 forceExplicitReconstruction, |
| 193 |
VkComponentMapping components, |
| 194 |
bool mustMatchChromaFilter, |
| 195 |
bool supportsLinearFilter) |
| 196 |
: fFormat(format) |
| 197 |
, fExternalFormat(externalFormat) |
| 198 |
, fYcbcrModel(ycbcrModel) |
| 199 |
, fYcbcrRange(ycbcrRange) |
| 200 |
, fXChromaOffset(xChromaOffset) |
| 201 |
, fYChromaOffset(yChromaOffset) |
| 202 |
, fChromaFilter(chromaFilter) |
| 203 |
, fForceExplicitReconstruction(forceExplicitReconstruction) |
| 204 |
, fComponents(components) |
| 205 |
, fSamplerFilterMustMatchChromaFilter(mustMatchChromaFilter) |
| 206 |
, fSupportsLinearFilter(supportsLinearFilter) {} |
| 207 |
|
| 208 |
// Format of the source image. Must be set to VK_FORMAT_UNDEFINED for external images or |
| 209 |
// a valid image format otherwise. |
| 210 |
VkFormat fFormat = VK_FORMAT_UNDEFINED; |
| 211 |
|
| 212 |
// The external format. Must be non-zero for external images, zero otherwise. |
| 213 |
// Should be compatible to be used in a VkExternalFormatANDROID struct. |
| 214 |
uint64_t fExternalFormat = 0; |
| 215 |
|
| 216 |
VkSamplerYcbcrModelConversion fYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY; |
| 217 |
VkSamplerYcbcrRange fYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL; |
| 218 |
VkChromaLocation fXChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN; |
| 219 |
VkChromaLocation fYChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN; |
| 220 |
VkFilter fChromaFilter = VK_FILTER_NEAREST; |
| 221 |
VkBool32 fForceExplicitReconstruction = false; |
| 222 |
|
| 223 |
// This is ignored when fExternalFormat is non-zero. |
| 224 |
VkComponentMapping fComponents = {VK_COMPONENT_SWIZZLE_IDENTITY, |
| 225 |
VK_COMPONENT_SWIZZLE_IDENTITY, |
| 226 |
VK_COMPONENT_SWIZZLE_IDENTITY, |
| 227 |
VK_COMPONENT_SWIZZLE_IDENTITY}; |
| 228 |
|
| 229 |
// Default these values to the most restrictive. These defaults only really matter until |
| 230 |
// we force all clients to go through our constructors. At that point these will be set to the |
| 231 |
// correct values. |
| 232 |
bool fSamplerFilterMustMatchChromaFilter = true; |
| 233 |
bool fSupportsLinearFilter = false; |
| 234 |
}; |
| 235 |
|
| 236 |
typedef void* VulkanDeviceLostContext; |
| 237 |
typedef void (*VulkanDeviceLostProc)(VulkanDeviceLostContext faultContext, |
| 238 |
const std::string& description, |
| 239 |
const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos, |
| 240 |
const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos, |
| 241 |
const std::vector<std::byte>& vendorBinaryData); |
| 242 |
|
| 243 |
} // namespace skgpu |
| 244 |
|
| 245 |
#endif // skgpu_VulkanTypes_DEFINED |