| 1 |
/* |
| 2 |
* Copyright 2020 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 |
#include "tools/gpu/vk/VkYcbcrSamplerHelper.h" |
| 9 |
|
| 10 |
#if defined(SK_VULKAN) |
| 11 |
|
| 12 |
#include "include/gpu/vk/VulkanTypes.h" |
| 13 |
#include "src/gpu/vk/VulkanInterface.h" |
| 14 |
|
| 15 |
#if defined(SK_GRAPHITE) |
| 16 |
#include "include/gpu/GpuTypes.h" |
| 17 |
#include "include/gpu/graphite/BackendTexture.h" |
| 18 |
#include "include/gpu/graphite/Recorder.h" |
| 19 |
#include "include/gpu/graphite/vk/VulkanGraphiteTypes.h" |
| 20 |
#include "src/gpu/graphite/vk/VulkanGraphiteUtils.h" |
| 21 |
#include "src/gpu/graphite/vk/VulkanSharedContext.h" |
| 22 |
#endif |
| 23 |
|
| 24 |
#if defined(SK_GANESH) |
| 25 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 26 |
#include "include/gpu/ganesh/vk/GrVkBackendSurface.h" |
| 27 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 28 |
#include "src/gpu/ganesh/vk/GrVkGpu.h" |
| 29 |
#include "src/gpu/ganesh/vk/GrVkUtil.h" |
| 30 |
#endif |
| 31 |
|
| 32 |
int VkYcbcrSamplerHelper::GetExpectedY(int x, int y, int width, int height) { |
| 33 |
return 16 + (x + y) * 219 / (width + height - 2); |
| 34 |
} |
| 35 |
|
| 36 |
std::pair<int, int> VkYcbcrSamplerHelper::GetExpectedUV(int x, int y, int width, int height) { |
| 37 |
return { 16 + x * 224 / (width - 1), 16 + y * 224 / (height - 1) }; |
| 38 |
} |
| 39 |
|
| 40 |
namespace { |
| 41 |
|
| 42 |
void populate_ycbcr_image_info(VkImageCreateInfo* outImageInfo, uint32_t width, uint32_t height) { |
| 43 |
outImageInfo->sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; |
| 44 |
outImageInfo->pNext = nullptr; |
| 45 |
outImageInfo->flags = 0; |
| 46 |
outImageInfo->imageType = VK_IMAGE_TYPE_2D; |
| 47 |
outImageInfo->format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM; |
| 48 |
outImageInfo->extent = VkExtent3D{width, height, 1}; |
| 49 |
outImageInfo->mipLevels = 1; |
| 50 |
outImageInfo->arrayLayers = 1; |
| 51 |
outImageInfo->samples = VK_SAMPLE_COUNT_1_BIT; |
| 52 |
outImageInfo->tiling = VK_IMAGE_TILING_LINEAR; |
| 53 |
outImageInfo->usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | |
| 54 |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
| 55 |
outImageInfo->sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 56 |
outImageInfo->queueFamilyIndexCount = 0; |
| 57 |
outImageInfo->pQueueFamilyIndices = nullptr; |
| 58 |
outImageInfo->initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 59 |
} |
| 60 |
|
| 61 |
bool find_memory_type_index(const VkPhysicalDeviceMemoryProperties& phyDevMemProps, |
| 62 |
const VkMemoryRequirements& memoryRequirements, |
| 63 |
uint32_t* memoryTypeIndex) { |
| 64 |
for (uint32_t i = 0; i < phyDevMemProps.memoryTypeCount; ++i) { |
| 65 |
if (memoryRequirements.memoryTypeBits & (1 << i)) { |
| 66 |
// Map host-visible memory. |
| 67 |
if (phyDevMemProps.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { |
| 68 |
*memoryTypeIndex = i; |
| 69 |
return true; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
#ifdef SK_GRAPHITE |
| 79 |
// TODO(b/339211930): When graphite and ganesh can share a macro for certain Vulkan driver calls, |
| 80 |
// much more code can be shared between this method and createGrBackendTexture. |
| 81 |
bool VkYcbcrSamplerHelper::createBackendTexture(uint32_t width, uint32_t height) { |
| 82 |
// Create YCbCr image. |
| 83 |
VkImageCreateInfo vkImageInfo; |
| 84 |
populate_ycbcr_image_info(&vkImageInfo, width, height); |
| 85 |
SkASSERT(fImage == VK_NULL_HANDLE); |
| 86 |
|
| 87 |
VkResult result; |
| 88 |
VULKAN_CALL_RESULT(fSharedCtxt, result, CreateImage(fSharedCtxt->device(), |
| 89 |
&vkImageInfo, |
| 90 |
/*pAllocator=*/nullptr, |
| 91 |
&fImage)); |
| 92 |
if (result != VK_SUCCESS) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
VkMemoryRequirements requirements; |
| 97 |
VULKAN_CALL(fSharedCtxt->interface(), GetImageMemoryRequirements(fSharedCtxt->device(), |
| 98 |
fImage, |
| 99 |
&requirements)); |
| 100 |
uint32_t memoryTypeIndex = 0; |
| 101 |
const VkPhysicalDeviceMemoryProperties& phyDevMemProps = |
| 102 |
fSharedCtxt->vulkanCaps().physicalDeviceMemoryProperties2().memoryProperties; |
| 103 |
if (!find_memory_type_index(phyDevMemProps, requirements, &memoryTypeIndex)) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
VkMemoryAllocateInfo allocInfo; |
| 108 |
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
| 109 |
allocInfo.pNext = nullptr; |
| 110 |
allocInfo.allocationSize = requirements.size; |
| 111 |
allocInfo.memoryTypeIndex = memoryTypeIndex; |
| 112 |
|
| 113 |
SkASSERT(fImageMemory == VK_NULL_HANDLE); |
| 114 |
VULKAN_CALL_RESULT(fSharedCtxt, result, AllocateMemory(fSharedCtxt->device(), |
| 115 |
&allocInfo, |
| 116 |
nullptr, |
| 117 |
&fImageMemory)); |
| 118 |
if (result != VK_SUCCESS) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
void* mappedBuffer; |
| 123 |
VULKAN_CALL_RESULT(fSharedCtxt, result, MapMemory(fSharedCtxt->device(), |
| 124 |
fImageMemory, |
| 125 |
/*offset=*/0u, |
| 126 |
requirements.size, |
| 127 |
/*flags=*/0u, |
| 128 |
&mappedBuffer)); |
| 129 |
if (result != VK_SUCCESS) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
// Write Y channel. |
| 134 |
VkImageSubresource subresource; |
| 135 |
subresource.aspectMask = VK_IMAGE_ASPECT_PLANE_0_BIT; |
| 136 |
subresource.mipLevel = 0; |
| 137 |
subresource.arrayLayer = 0; |
| 138 |
|
| 139 |
VkSubresourceLayout yLayout; |
| 140 |
VULKAN_CALL(fSharedCtxt->interface(), |
| 141 |
GetImageSubresourceLayout(fSharedCtxt->device(), fImage, &subresource, &yLayout)); |
| 142 |
uint8_t* bufferData = reinterpret_cast<uint8_t*>(mappedBuffer) + yLayout.offset; |
| 143 |
for (size_t y = 0; y < height; ++y) { |
| 144 |
for (size_t x = 0; x < width; ++x) { |
| 145 |
bufferData[y * yLayout.rowPitch + x] = GetExpectedY(x, y, width, height); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// Write UV channels. |
| 150 |
subresource.aspectMask = VK_IMAGE_ASPECT_PLANE_1_BIT; |
| 151 |
VkSubresourceLayout uvLayout; |
| 152 |
VULKAN_CALL(fSharedCtxt->interface(), GetImageSubresourceLayout(fSharedCtxt->device(), |
| 153 |
fImage, |
| 154 |
&subresource, |
| 155 |
&uvLayout)); |
| 156 |
bufferData = reinterpret_cast<uint8_t*>(mappedBuffer) + uvLayout.offset; |
| 157 |
for (size_t y = 0; y < height / 2; ++y) { |
| 158 |
for (size_t x = 0; x < width / 2; ++x) { |
| 159 |
auto [u, v] = GetExpectedUV(2*x, 2*y, width, height); |
| 160 |
bufferData[y * uvLayout.rowPitch + x * 2] = u; |
| 161 |
bufferData[y * uvLayout.rowPitch + x * 2 + 1] = v; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
VkMappedMemoryRange flushRange; |
| 166 |
flushRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; |
| 167 |
flushRange.pNext = nullptr; |
| 168 |
flushRange.memory = fImageMemory; |
| 169 |
flushRange.offset = 0; |
| 170 |
flushRange.size = VK_WHOLE_SIZE; |
| 171 |
VULKAN_CALL_RESULT(fSharedCtxt, result, FlushMappedMemoryRanges(fSharedCtxt->device(), |
| 172 |
/*memoryRangeCount=*/1, |
| 173 |
&flushRange)); |
| 174 |
if (result != VK_SUCCESS) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
VULKAN_CALL(fSharedCtxt->interface(), UnmapMemory(fSharedCtxt->device(), fImageMemory)); |
| 178 |
|
| 179 |
// Bind image memory. |
| 180 |
VULKAN_CALL_RESULT(fSharedCtxt, result, BindImageMemory(fSharedCtxt->device(), |
| 181 |
fImage, |
| 182 |
fImageMemory, |
| 183 |
/*memoryOffset=*/0u)); |
| 184 |
if (result != VK_SUCCESS) { |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
// Wrap the image into SkImage. |
| 189 |
VkFormatProperties formatProperties; |
| 190 |
SkASSERT(fSharedCtxt->physDevice() != VK_NULL_HANDLE); |
| 191 |
VULKAN_CALL(fSharedCtxt->interface(), |
| 192 |
GetPhysicalDeviceFormatProperties(fSharedCtxt->physDevice(), |
| 193 |
VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, |
| 194 |
&formatProperties)); |
| 195 |
SkDEBUGCODE(auto linFlags = formatProperties.linearTilingFeatures;) |
| 196 |
SkASSERT((linFlags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && |
| 197 |
(linFlags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT) && |
| 198 |
(linFlags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT) && |
| 199 |
(linFlags & VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT)); |
| 200 |
|
| 201 |
skgpu::VulkanYcbcrConversionInfo ycbcrInfo(vkImageInfo.format, |
| 202 |
VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709, |
| 203 |
VK_SAMPLER_YCBCR_RANGE_ITU_NARROW, |
| 204 |
VK_CHROMA_LOCATION_COSITED_EVEN, |
| 205 |
VK_CHROMA_LOCATION_COSITED_EVEN, |
| 206 |
VK_FILTER_LINEAR, |
| 207 |
/*forceExplicitReconstruction=*/false, |
| 208 |
/*components=*/{}, |
| 209 |
formatProperties.linearTilingFeatures); |
| 210 |
skgpu::VulkanAlloc alloc; |
| 211 |
alloc.fMemory = fImageMemory; |
| 212 |
alloc.fOffset = 0; |
| 213 |
alloc.fSize = requirements.size; |
| 214 |
|
| 215 |
skgpu::graphite::VulkanTextureInfo imageInfo = { |
| 216 |
vkImageInfo.samples, |
| 217 |
skgpu::Mipmapped::kNo, |
| 218 |
VK_IMAGE_CREATE_PROTECTED_BIT, |
| 219 |
vkImageInfo.format, |
| 220 |
vkImageInfo.tiling, |
| 221 |
vkImageInfo.usage, |
| 222 |
vkImageInfo.sharingMode, |
| 223 |
VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT, |
| 224 |
ycbcrInfo}; |
| 225 |
|
| 226 |
fTexture = skgpu::graphite::BackendTextures::MakeVulkan({(int32_t)width, (int32_t)height}, |
| 227 |
imageInfo, |
| 228 |
VK_IMAGE_LAYOUT_UNDEFINED, |
| 229 |
/*queueFamilyIndex=*/0, |
| 230 |
fImage, |
| 231 |
alloc); |
| 232 |
return true; |
| 233 |
} |
| 234 |
#endif // SK_GRAPHITE |
| 235 |
|
| 236 |
#if defined(SK_GANESH) |
| 237 |
bool VkYcbcrSamplerHelper::createGrBackendTexture(uint32_t width, uint32_t height) { |
| 238 |
GrVkGpu* vkGpu = this->vkGpu(); |
| 239 |
VkResult result; |
| 240 |
|
| 241 |
// Create YCbCr image. |
| 242 |
VkImageCreateInfo vkImageInfo; |
| 243 |
populate_ycbcr_image_info(&vkImageInfo, width, height); |
| 244 |
SkASSERT(fImage == VK_NULL_HANDLE); |
| 245 |
|
| 246 |
GR_VK_CALL_RESULT(vkGpu, result, CreateImage(vkGpu->device(), &vkImageInfo, nullptr, &fImage)); |
| 247 |
if (result != VK_SUCCESS) { |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
VkMemoryRequirements requirements; |
| 252 |
GR_VK_CALL(vkGpu->vkInterface(), GetImageMemoryRequirements(vkGpu->device(), |
| 253 |
fImage, |
| 254 |
&requirements)); |
| 255 |
|
| 256 |
uint32_t memoryTypeIndex = 0; |
| 257 |
VkPhysicalDeviceMemoryProperties phyDevMemProps; |
| 258 |
GR_VK_CALL(vkGpu->vkInterface(), GetPhysicalDeviceMemoryProperties(vkGpu->physicalDevice(), |
| 259 |
&phyDevMemProps)); |
| 260 |
if (!find_memory_type_index(phyDevMemProps, requirements, &memoryTypeIndex)) { |
| 261 |
return false; |
| 262 |
} |
| 263 |
|
| 264 |
VkMemoryAllocateInfo allocInfo = {}; |
| 265 |
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
| 266 |
allocInfo.allocationSize = requirements.size; |
| 267 |
allocInfo.memoryTypeIndex = memoryTypeIndex; |
| 268 |
|
| 269 |
SkASSERT(fImageMemory == VK_NULL_HANDLE); |
| 270 |
GR_VK_CALL_RESULT(vkGpu, result, AllocateMemory(vkGpu->device(), &allocInfo, |
| 271 |
nullptr, &fImageMemory)); |
| 272 |
if (result != VK_SUCCESS) { |
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
void* mappedBuffer; |
| 277 |
GR_VK_CALL_RESULT(vkGpu, result, MapMemory(vkGpu->device(), fImageMemory, 0u, |
| 278 |
requirements.size, 0u, &mappedBuffer)); |
| 279 |
if (result != VK_SUCCESS) { |
| 280 |
return false; |
| 281 |
} |
| 282 |
|
| 283 |
// Write Y channel. |
| 284 |
VkImageSubresource subresource; |
| 285 |
subresource.aspectMask = VK_IMAGE_ASPECT_PLANE_0_BIT; |
| 286 |
subresource.mipLevel = 0; |
| 287 |
subresource.arrayLayer = 0; |
| 288 |
|
| 289 |
VkSubresourceLayout yLayout; |
| 290 |
GR_VK_CALL(vkGpu->vkInterface(), GetImageSubresourceLayout(vkGpu->device(), fImage, |
| 291 |
&subresource, &yLayout)); |
| 292 |
uint8_t* bufferData = reinterpret_cast<uint8_t*>(mappedBuffer) + yLayout.offset; |
| 293 |
for (size_t y = 0; y < height; ++y) { |
| 294 |
for (size_t x = 0; x < width; ++x) { |
| 295 |
bufferData[y * yLayout.rowPitch + x] = GetExpectedY(x, y, width, height); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
// Write UV channels. |
| 300 |
subresource.aspectMask = VK_IMAGE_ASPECT_PLANE_1_BIT; |
| 301 |
VkSubresourceLayout uvLayout; |
| 302 |
GR_VK_CALL(vkGpu->vkInterface(), GetImageSubresourceLayout(vkGpu->device(), fImage, |
| 303 |
&subresource, &uvLayout)); |
| 304 |
bufferData = reinterpret_cast<uint8_t*>(mappedBuffer) + uvLayout.offset; |
| 305 |
for (size_t y = 0; y < height / 2; ++y) { |
| 306 |
for (size_t x = 0; x < width / 2; ++x) { |
| 307 |
auto [u, v] = GetExpectedUV(2*x, 2*y, width, height); |
| 308 |
bufferData[y * uvLayout.rowPitch + x * 2] = u; |
| 309 |
bufferData[y * uvLayout.rowPitch + x * 2 + 1] = v; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
VkMappedMemoryRange flushRange; |
| 314 |
flushRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; |
| 315 |
flushRange.pNext = nullptr; |
| 316 |
flushRange.memory = fImageMemory; |
| 317 |
flushRange.offset = 0; |
| 318 |
flushRange.size = VK_WHOLE_SIZE; |
| 319 |
GR_VK_CALL_RESULT(vkGpu, result, FlushMappedMemoryRanges(vkGpu->device(), 1, &flushRange)); |
| 320 |
if (result != VK_SUCCESS) { |
| 321 |
return false; |
| 322 |
} |
| 323 |
GR_VK_CALL(vkGpu->vkInterface(), UnmapMemory(vkGpu->device(), fImageMemory)); |
| 324 |
|
| 325 |
// Bind image memory. |
| 326 |
GR_VK_CALL_RESULT(vkGpu, result, BindImageMemory(vkGpu->device(), fImage, fImageMemory, 0u)); |
| 327 |
if (result != VK_SUCCESS) { |
| 328 |
return false; |
| 329 |
} |
| 330 |
|
| 331 |
// Wrap the image into SkImage. |
| 332 |
VkFormatProperties formatProperties; |
| 333 |
GR_VK_CALL(vkGpu->vkInterface(), |
| 334 |
GetPhysicalDeviceFormatProperties(vkGpu->physicalDevice(), |
| 335 |
VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, |
| 336 |
&formatProperties)); |
| 337 |
SkDEBUGCODE(auto linFlags = formatProperties.linearTilingFeatures;) |
| 338 |
SkASSERT((linFlags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && |
| 339 |
(linFlags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT) && |
| 340 |
(linFlags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT) && |
| 341 |
(linFlags & VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT)); |
| 342 |
|
| 343 |
skgpu::VulkanYcbcrConversionInfo ycbcrInfo(vkImageInfo.format, |
| 344 |
VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709, |
| 345 |
VK_SAMPLER_YCBCR_RANGE_ITU_NARROW, |
| 346 |
VK_CHROMA_LOCATION_COSITED_EVEN, |
| 347 |
VK_CHROMA_LOCATION_COSITED_EVEN, |
| 348 |
VK_FILTER_LINEAR, |
| 349 |
/*forceExplicitReconstruction=*/false, |
| 350 |
/*components=*/{}, |
| 351 |
formatProperties.linearTilingFeatures); |
| 352 |
skgpu::VulkanAlloc alloc; |
| 353 |
alloc.fMemory = fImageMemory; |
| 354 |
alloc.fOffset = 0; |
| 355 |
alloc.fSize = requirements.size; |
| 356 |
|
| 357 |
GrVkImageInfo imageInfo = {fImage, |
| 358 |
alloc, |
| 359 |
VK_IMAGE_TILING_LINEAR, |
| 360 |
VK_IMAGE_LAYOUT_UNDEFINED, |
| 361 |
vkImageInfo.format, |
| 362 |
vkImageInfo.usage, |
| 363 |
1 /* sample count */, |
| 364 |
1 /* levelCount */, |
| 365 |
VK_QUEUE_FAMILY_IGNORED, |
| 366 |
GrProtected::kNo, |
| 367 |
ycbcrInfo}; |
| 368 |
|
| 369 |
fGrTexture = GrBackendTextures::MakeVk(width, height, imageInfo); |
| 370 |
return true; |
| 371 |
} |
| 372 |
|
| 373 |
GrVkGpu* VkYcbcrSamplerHelper::vkGpu() { |
| 374 |
return fDContext ? (GrVkGpu*) fDContext->priv().getGpu() : nullptr; |
| 375 |
} |
| 376 |
|
| 377 |
VkYcbcrSamplerHelper::VkYcbcrSamplerHelper(GrDirectContext* dContext) : fDContext(dContext) { |
| 378 |
SkASSERT_RELEASE(dContext->backend() == GrBackendApi::kVulkan); |
| 379 |
} |
| 380 |
#endif // SK_GANESH |
| 381 |
|
| 382 |
VkYcbcrSamplerHelper::~VkYcbcrSamplerHelper() { |
| 383 |
#ifdef SK_GRAPHITE |
| 384 |
if (fSharedCtxt) { |
| 385 |
if (fImage != VK_NULL_HANDLE) { |
| 386 |
VULKAN_CALL(fSharedCtxt->interface(), |
| 387 |
DestroyImage(fSharedCtxt->device(), fImage, nullptr)); |
| 388 |
fImage = VK_NULL_HANDLE; |
| 389 |
} |
| 390 |
if (fImageMemory != VK_NULL_HANDLE) { |
| 391 |
VULKAN_CALL(fSharedCtxt->interface(), |
| 392 |
FreeMemory(fSharedCtxt->device(), fImageMemory, nullptr)); |
| 393 |
fImageMemory = VK_NULL_HANDLE; |
| 394 |
} |
| 395 |
} |
| 396 |
#endif // SK_GRAPHITE |
| 397 |
#if defined (SK_GANESH) |
| 398 |
if (GrVkGpu* vkGpu = this->vkGpu()) { |
| 399 |
if (fImage != VK_NULL_HANDLE) { |
| 400 |
GR_VK_CALL(vkGpu->vkInterface(), DestroyImage(vkGpu->device(), fImage, nullptr)); |
| 401 |
fImage = VK_NULL_HANDLE; |
| 402 |
} |
| 403 |
if (fImageMemory != VK_NULL_HANDLE) { |
| 404 |
GR_VK_CALL(vkGpu->vkInterface(), FreeMemory(vkGpu->device(), fImageMemory, nullptr)); |
| 405 |
fImageMemory = VK_NULL_HANDLE; |
| 406 |
} |
| 407 |
} |
| 408 |
#endif |
| 409 |
} |
| 410 |
|
| 411 |
bool VkYcbcrSamplerHelper::isYCbCrSupported() { |
| 412 |
VkFormatProperties formatProperties; |
| 413 |
#ifdef SK_GRAPHITE |
| 414 |
if (fSharedCtxt) { |
| 415 |
if (!fSharedCtxt->vulkanCaps().supportsYcbcrConversion()) { |
| 416 |
return false; |
| 417 |
} |
| 418 |
|
| 419 |
SkASSERT(fSharedCtxt->physDevice() != VK_NULL_HANDLE); |
| 420 |
VULKAN_CALL(fSharedCtxt->interface(), |
| 421 |
GetPhysicalDeviceFormatProperties(fSharedCtxt->physDevice(), |
| 422 |
VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, |
| 423 |
&formatProperties)); |
| 424 |
} |
| 425 |
#endif |
| 426 |
#if defined(SK_GANESH) |
| 427 |
if (GrVkGpu* vkGpu = this->vkGpu()) { |
| 428 |
if (!vkGpu->vkCaps().supportsYcbcrConversion()) { |
| 429 |
return false; |
| 430 |
} |
| 431 |
|
| 432 |
GR_VK_CALL(vkGpu->vkInterface(), |
| 433 |
GetPhysicalDeviceFormatProperties(vkGpu->physicalDevice(), |
| 434 |
VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, |
| 435 |
&formatProperties)); |
| 436 |
} |
| 437 |
#endif // SK_GANESH |
| 438 |
|
| 439 |
// The createBackendTexture call (which is the point of this helper class) requires linear |
| 440 |
// support for VK_FORMAT_G8_B8R8_2PLANE_420_UNORM including sampling and cosited chroma. |
| 441 |
// Verify that the image format is supported. |
| 442 |
auto linFlags = formatProperties.linearTilingFeatures; |
| 443 |
if (!(linFlags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) || |
| 444 |
!(linFlags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT) || |
| 445 |
!(linFlags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT) || |
| 446 |
!(linFlags & VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT)) { |
| 447 |
// VK_FORMAT_G8_B8R8_2PLANE_420_UNORM is not supported |
| 448 |
return false; |
| 449 |
} |
| 450 |
return true; |
| 451 |
} |
| 452 |
#endif // SK_VULKAN |