| 1 |
/* |
| 2 |
* Copyright 2018 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 "src/gpu/ganesh/vk/GrVkSamplerYcbcrConversion.h" |
| 9 |
|
| 10 |
#include "include/gpu/vk/VulkanTypes.h" |
| 11 |
#include "include/private/base/SkAssert.h" |
| 12 |
#include "src/gpu/ganesh/vk/GrVkCaps.h" |
| 13 |
#include "src/gpu/ganesh/vk/GrVkGpu.h" |
| 14 |
#include "src/gpu/ganesh/vk/GrVkUtil.h" |
| 15 |
#include "src/gpu/vk/VulkanUtilsPriv.h" |
| 16 |
|
| 17 |
GrVkSamplerYcbcrConversion* GrVkSamplerYcbcrConversion::Create( |
| 18 |
GrVkGpu* gpu, const skgpu::VulkanYcbcrConversionInfo& info) { |
| 19 |
if (!gpu->vkCaps().supportsYcbcrConversion()) { |
| 20 |
return nullptr; |
| 21 |
} |
| 22 |
|
| 23 |
VkSamplerYcbcrConversionCreateInfo ycbcrCreateInfo; |
| 24 |
std::optional<VkFilter> requiredSamplerFilter; |
| 25 |
info.toVkSamplerYcbcrConversionCreateInfo(&ycbcrCreateInfo, &requiredSamplerFilter); |
| 26 |
|
| 27 |
#ifdef SK_BUILD_FOR_ANDROID |
| 28 |
VkExternalFormatANDROID externalFormat; |
| 29 |
if (info.hasExternalFormat()) { |
| 30 |
// Format must not be specified for external images. |
| 31 |
SkASSERT(info.format() == VK_FORMAT_UNDEFINED); |
| 32 |
externalFormat.sType = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID; |
| 33 |
externalFormat.pNext = nullptr; |
| 34 |
externalFormat.externalFormat = info.externalFormat(); |
| 35 |
SkASSERT(ycbcrCreateInfo.pNext == nullptr); |
| 36 |
ycbcrCreateInfo.pNext = &externalFormat; |
| 37 |
} |
| 38 |
#else |
| 39 |
// External images are supported only on Android. |
| 40 |
SkASSERT(!info.hasExternalFormat()); |
| 41 |
#endif |
| 42 |
|
| 43 |
if (!info.hasExternalFormat()) { |
| 44 |
SkASSERT(info.format() != VK_FORMAT_UNDEFINED); |
| 45 |
} |
| 46 |
|
| 47 |
VkSamplerYcbcrConversion conversion; |
| 48 |
VkResult result; |
| 49 |
GR_VK_CALL_RESULT(gpu, result, CreateSamplerYcbcrConversion(gpu->device(), &ycbcrCreateInfo, |
| 50 |
nullptr, &conversion)); |
| 51 |
if (result != VK_SUCCESS) { |
| 52 |
return nullptr; |
| 53 |
} |
| 54 |
|
| 55 |
return new GrVkSamplerYcbcrConversion( |
| 56 |
gpu, conversion, requiredSamplerFilter, GenerateKey(info)); |
| 57 |
} |
| 58 |
|
| 59 |
void GrVkSamplerYcbcrConversion::freeGPUData() const { |
| 60 |
SkASSERT(fYcbcrConversion); |
| 61 |
GR_VK_CALL(fGpu->vkInterface(), DestroySamplerYcbcrConversion(fGpu->device(), |
| 62 |
fYcbcrConversion, nullptr)); |
| 63 |
} |
| 64 |
|
| 65 |
GrVkSamplerYcbcrConversion::Key GrVkSamplerYcbcrConversion::GenerateKey( |
| 66 |
const skgpu::VulkanYcbcrConversionInfo& ycbcrInfo) { |
| 67 |
SkASSERT(static_cast<int>(ycbcrInfo.model() <= 7)); |
| 68 |
static const int kRangeShift = 3; |
| 69 |
SkASSERT(static_cast<int>(ycbcrInfo.range()) <= 1); |
| 70 |
static const int kXChromaOffsetShift = kRangeShift + 1; |
| 71 |
SkASSERT(static_cast<int>(ycbcrInfo.xChromaOffset()) <= 1); |
| 72 |
static const int kYChromaOffsetShift = kXChromaOffsetShift + 1; |
| 73 |
SkASSERT(static_cast<int>(ycbcrInfo.yChromaOffset()) <= 1); |
| 74 |
static const int kChromaFilterShift = kYChromaOffsetShift + 1; |
| 75 |
SkASSERT(static_cast<int>(ycbcrInfo.chromaFilter()) <= 1); |
| 76 |
static const int kReconShift = kChromaFilterShift + 1; |
| 77 |
SkASSERT(static_cast<int>(ycbcrInfo.forceExplicitReconstruction()) <= 1); |
| 78 |
static const int kCompRShift = kReconShift + 1; |
| 79 |
static const int kCompGShift = kCompRShift + 3; |
| 80 |
static const int kCompBShift = kCompGShift + 3; |
| 81 |
static const int kCompAShift = kCompBShift + 3; |
| 82 |
SkASSERT(static_cast<int>(ycbcrInfo.components().r <= 6) && |
| 83 |
static_cast<int>(ycbcrInfo.components().g <= 6) && |
| 84 |
static_cast<int>(ycbcrInfo.components().b <= 6) && |
| 85 |
static_cast<int>(ycbcrInfo.components().a <= 6)); |
| 86 |
static_assert(kCompAShift <= 17); |
| 87 |
|
| 88 |
uint32_t ycbcrKey = static_cast<uint32_t>(ycbcrInfo.model()); |
| 89 |
ycbcrKey |= (static_cast<uint32_t>(ycbcrInfo.range()) << kRangeShift); |
| 90 |
ycbcrKey |= (static_cast<uint32_t>(ycbcrInfo.xChromaOffset()) << kXChromaOffsetShift); |
| 91 |
ycbcrKey |= (static_cast<uint32_t>(ycbcrInfo.yChromaOffset()) << kYChromaOffsetShift); |
| 92 |
ycbcrKey |= (static_cast<uint32_t>(ycbcrInfo.chromaFilter()) << kChromaFilterShift); |
| 93 |
ycbcrKey |= (static_cast<uint32_t>(ycbcrInfo.forceExplicitReconstruction()) << kReconShift); |
| 94 |
ycbcrKey |= (static_cast<uint32_t>(ycbcrInfo.components().r) << kCompRShift); |
| 95 |
ycbcrKey |= (static_cast<uint32_t>(ycbcrInfo.components().g) << kCompGShift); |
| 96 |
ycbcrKey |= (static_cast<uint32_t>(ycbcrInfo.components().b) << kCompBShift); |
| 97 |
ycbcrKey |= (static_cast<uint32_t>(ycbcrInfo.components().a) << kCompAShift); |
| 98 |
|
| 99 |
return Key{ycbcrInfo.format(), ycbcrInfo.externalFormat(), ycbcrKey}; |
| 100 |
} |