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/vk/GrVkSamplerYcbcrConversion.h"
9
10 #include "src/gpu/vk/GrVkGpu.h"
11
Create(const GrVkGpu * gpu,const GrVkYcbcrConversionInfo & info)12 GrVkSamplerYcbcrConversion* GrVkSamplerYcbcrConversion::Create(
13 const GrVkGpu* gpu, const GrVkYcbcrConversionInfo& info) {
14 if (!gpu->vkCaps().supportsYcbcrConversion()) {
15 return nullptr;
16 }
17
18 #ifdef SK_DEBUG
19 const VkFormatFeatureFlags& featureFlags = info.fFormatFeatures;
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
37
38 VkSamplerYcbcrConversionCreateInfo ycbcrCreateInfo;
39 ycbcrCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_CREATE_INFO;
40 ycbcrCreateInfo.pNext = nullptr;
41 ycbcrCreateInfo.format = info.fFormat;
42 ycbcrCreateInfo.ycbcrModel = info.fYcbcrModel;
43 ycbcrCreateInfo.ycbcrRange = info.fYcbcrRange;
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};
49 ycbcrCreateInfo.xChromaOffset = info.fXChromaOffset;
50 ycbcrCreateInfo.yChromaOffset = info.fYChromaOffset;
51 ycbcrCreateInfo.chromaFilter = info.fChromaFilter;
52 ycbcrCreateInfo.forceExplicitReconstruction = info.fForceExplicitReconstruction;
53
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
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 }
79
80 return new GrVkSamplerYcbcrConversion(conversion, GenerateKey(info));
81 }
82
freeGPUData(GrVkGpu * gpu) const83 void GrVkSamplerYcbcrConversion::freeGPUData(GrVkGpu* gpu) const {
84 SkASSERT(fYcbcrConversion);
85 GR_VK_CALL(gpu->vkInterface(), DestroySamplerYcbcrConversion(gpu->device(), fYcbcrConversion,
86 nullptr));
87 }
88
GenerateKey(const GrVkYcbcrConversionInfo & ycbcrInfo)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
111 return Key{ycbcrInfo.fFormat, ycbcrInfo.fExternalFormat, ycbcrKey};
112 }
113