Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/vk/GrVkSamplerYcbcrConversion.h" |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/vk/GrVkGpu.h" |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 11 | |
| 12 | GrVkSamplerYcbcrConversion* GrVkSamplerYcbcrConversion::Create( |
| 13 | const GrVkGpu* gpu, const GrVkYcbcrConversionInfo& info) { |
| 14 | if (!gpu->vkCaps().supportsYcbcrConversion()) { |
| 15 | return nullptr; |
| 16 | } |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 17 | |
| 18 | #ifdef SK_DEBUG |
Sergey Ulanov | 2739fd2 | 2019-08-11 22:46:33 -0700 | [diff] [blame] | 19 | const VkFormatFeatureFlags& featureFlags = info.fFormatFeatures; |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 20 | if (info.fXChromaOffset == VK_CHROMA_LOCATION_MIDPOINT || |
| 21 | info.fYChromaOffset == VK_CHROMA_LOCATION_MIDPOINT) { |
| 22 | SkASSERT(featureFlags & VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT); |
| 23 | } |
| 24 | if (info.fXChromaOffset == VK_CHROMA_LOCATION_COSITED_EVEN || |
| 25 | info.fYChromaOffset == VK_CHROMA_LOCATION_COSITED_EVEN) { |
| 26 | SkASSERT(featureFlags & VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT); |
| 27 | } |
| 28 | if (info.fChromaFilter == VK_FILTER_LINEAR) { |
| 29 | SkASSERT(featureFlags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT); |
| 30 | } |
| 31 | if (info.fForceExplicitReconstruction) { |
| 32 | SkASSERT(featureFlags & |
| 33 | VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_CHROMA_RECONSTRUCTION_EXPLICIT_FORCEABLE_BIT); |
| 34 | } |
| 35 | #endif |
| 36 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 37 | |
| 38 | VkSamplerYcbcrConversionCreateInfo ycbcrCreateInfo; |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 39 | ycbcrCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_CREATE_INFO; |
Sergey Ulanov | 2739fd2 | 2019-08-11 22:46:33 -0700 | [diff] [blame] | 40 | ycbcrCreateInfo.pNext = nullptr; |
| 41 | ycbcrCreateInfo.format = info.fFormat; |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 42 | ycbcrCreateInfo.ycbcrModel = info.fYcbcrModel; |
| 43 | ycbcrCreateInfo.ycbcrRange = info.fYcbcrRange; |
Sergey Ulanov | 2739fd2 | 2019-08-11 22:46:33 -0700 | [diff] [blame] | 44 | |
| 45 | // Components is ignored for external format conversions. For all other formats identity swizzle |
| 46 | // is used. It can be added to GrVkYcbcrConversionInfo if necessary. |
| 47 | ycbcrCreateInfo.components = {VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, |
| 48 | VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY}; |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 49 | ycbcrCreateInfo.xChromaOffset = info.fXChromaOffset; |
| 50 | ycbcrCreateInfo.yChromaOffset = info.fYChromaOffset; |
| 51 | ycbcrCreateInfo.chromaFilter = info.fChromaFilter; |
| 52 | ycbcrCreateInfo.forceExplicitReconstruction = info.fForceExplicitReconstruction; |
| 53 | |
Sergey Ulanov | 2739fd2 | 2019-08-11 22:46:33 -0700 | [diff] [blame] | 54 | #ifdef SK_BUILD_FOR_ANDROID |
| 55 | VkExternalFormatANDROID externalFormat; |
| 56 | if (info.fExternalFormat) { |
| 57 | // Format must not be specified for external images. |
| 58 | SkASSERT(info.fFormat == VK_FORMAT_UNDEFINED); |
| 59 | externalFormat.sType = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID; |
| 60 | externalFormat.pNext = nullptr; |
| 61 | externalFormat.externalFormat = info.fExternalFormat; |
| 62 | ycbcrCreateInfo.pNext = &externalFormat; |
| 63 | } |
| 64 | #else |
| 65 | // External images are supported only on Android; |
| 66 | SkASSERT(!info.fExternalFormat); |
| 67 | #endif |
| 68 | |
| 69 | if (!info.fExternalFormat) { |
| 70 | SkASSERT(info.fFormat != VK_FORMAT_UNDEFINED); |
| 71 | } |
| 72 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 73 | VkSamplerYcbcrConversion conversion; |
| 74 | GR_VK_CALL(gpu->vkInterface(), CreateSamplerYcbcrConversion(gpu->device(), &ycbcrCreateInfo, |
| 75 | nullptr, &conversion)); |
| 76 | if (conversion == VK_NULL_HANDLE) { |
| 77 | return nullptr; |
| 78 | } |
Sergey Ulanov | 2739fd2 | 2019-08-11 22:46:33 -0700 | [diff] [blame] | 79 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 80 | return new GrVkSamplerYcbcrConversion(conversion, GenerateKey(info)); |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 83 | void GrVkSamplerYcbcrConversion::freeGPUData(GrVkGpu* gpu) const { |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 84 | SkASSERT(fYcbcrConversion); |
| 85 | GR_VK_CALL(gpu->vkInterface(), DestroySamplerYcbcrConversion(gpu->device(), fYcbcrConversion, |
| 86 | nullptr)); |
| 87 | } |
| 88 | |
| 89 | GrVkSamplerYcbcrConversion::Key GrVkSamplerYcbcrConversion::GenerateKey( |
| 90 | const GrVkYcbcrConversionInfo& ycbcrInfo) { |
| 91 | SkASSERT(static_cast<int>(ycbcrInfo.fYcbcrModel <= 7)); |
| 92 | static const int kRangeShift = 3; |
| 93 | SkASSERT(static_cast<int>(ycbcrInfo.fYcbcrRange) <= 1); |
| 94 | static const int kXChromaOffsetShift = kRangeShift + 1; |
| 95 | SkASSERT(static_cast<int>(ycbcrInfo.fXChromaOffset) <= 1); |
| 96 | static const int kYChromaOffsetShift = kXChromaOffsetShift + 1; |
| 97 | SkASSERT(static_cast<int>(ycbcrInfo.fXChromaOffset) <= 1); |
| 98 | static const int kChromaFilterShift = kYChromaOffsetShift + 1; |
| 99 | SkASSERT(static_cast<int>(ycbcrInfo.fChromaFilter) <= 1); |
| 100 | static const int kReconShift = kChromaFilterShift + 1; |
| 101 | SkASSERT(static_cast<int>(ycbcrInfo.fForceExplicitReconstruction) <= 1); |
| 102 | GR_STATIC_ASSERT(kReconShift <= 7); |
| 103 | |
| 104 | uint8_t ycbcrKey = static_cast<uint8_t>(ycbcrInfo.fYcbcrModel); |
| 105 | ycbcrKey |= (static_cast<uint8_t>(ycbcrInfo.fYcbcrRange) << kRangeShift); |
| 106 | ycbcrKey |= (static_cast<uint8_t>(ycbcrInfo.fXChromaOffset) << kXChromaOffsetShift); |
| 107 | ycbcrKey |= (static_cast<uint8_t>(ycbcrInfo.fYChromaOffset) << kYChromaOffsetShift); |
| 108 | ycbcrKey |= (static_cast<uint8_t>(ycbcrInfo.fChromaFilter) << kChromaFilterShift); |
| 109 | ycbcrKey |= (static_cast<uint8_t>(ycbcrInfo.fForceExplicitReconstruction) << kReconShift); |
| 110 | |
Sergey Ulanov | 2739fd2 | 2019-08-11 22:46:33 -0700 | [diff] [blame] | 111 | return Key{ycbcrInfo.fFormat, ycbcrInfo.fExternalFormat, ycbcrKey}; |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 112 | } |