• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(GrVkGpu * gpu,const GrVkYcbcrConversionInfo & info)12 GrVkSamplerYcbcrConversion* GrVkSamplerYcbcrConversion::Create(
13         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     VkResult result;
75     GR_VK_CALL_RESULT(gpu, result, CreateSamplerYcbcrConversion(gpu->device(), &ycbcrCreateInfo,
76                                                                 nullptr, &conversion));
77     if (result != VK_SUCCESS) {
78         return nullptr;
79     }
80 
81     return new GrVkSamplerYcbcrConversion(gpu, conversion, GenerateKey(info));
82 }
83 
freeGPUData() const84 void GrVkSamplerYcbcrConversion::freeGPUData() const {
85     SkASSERT(fYcbcrConversion);
86     GR_VK_CALL(fGpu->vkInterface(), DestroySamplerYcbcrConversion(fGpu->device(),
87                                                                   fYcbcrConversion, nullptr));
88 }
89 
GenerateKey(const GrVkYcbcrConversionInfo & ycbcrInfo)90 GrVkSamplerYcbcrConversion::Key GrVkSamplerYcbcrConversion::GenerateKey(
91         const GrVkYcbcrConversionInfo& ycbcrInfo) {
92     SkASSERT(static_cast<int>(ycbcrInfo.fYcbcrModel <= 7));
93     static const int kRangeShift = 3;
94     SkASSERT(static_cast<int>(ycbcrInfo.fYcbcrRange) <= 1);
95     static const int kXChromaOffsetShift = kRangeShift + 1;
96     SkASSERT(static_cast<int>(ycbcrInfo.fXChromaOffset) <= 1);
97     static const int kYChromaOffsetShift = kXChromaOffsetShift + 1;
98     SkASSERT(static_cast<int>(ycbcrInfo.fXChromaOffset) <= 1);
99     static const int kChromaFilterShift = kYChromaOffsetShift + 1;
100     SkASSERT(static_cast<int>(ycbcrInfo.fChromaFilter) <= 1);
101     static const int kReconShift = kChromaFilterShift + 1;
102     SkASSERT(static_cast<int>(ycbcrInfo.fForceExplicitReconstruction) <= 1);
103     static_assert(kReconShift <= 7);
104 
105     uint8_t ycbcrKey = static_cast<uint8_t>(ycbcrInfo.fYcbcrModel);
106     ycbcrKey |= (static_cast<uint8_t>(ycbcrInfo.fYcbcrRange) << kRangeShift);
107     ycbcrKey |= (static_cast<uint8_t>(ycbcrInfo.fXChromaOffset) << kXChromaOffsetShift);
108     ycbcrKey |= (static_cast<uint8_t>(ycbcrInfo.fYChromaOffset) << kYChromaOffsetShift);
109     ycbcrKey |= (static_cast<uint8_t>(ycbcrInfo.fChromaFilter) << kChromaFilterShift);
110     ycbcrKey |= (static_cast<uint8_t>(ycbcrInfo.fForceExplicitReconstruction) << kReconShift);
111 
112     return Key{ycbcrInfo.fFormat, ycbcrInfo.fExternalFormat, ycbcrKey};
113 }
114