• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <vulkan/vulkan.h>
17 
18 #include "vulkan/device_vk.h"
19 #include "vulkan/gpu_image_vk.h"
20 #include "vulkan/platform_hardware_buffer_util_vk.h"
21 #include "vulkan/validate_vk.h"
22 
23 RENDER_BEGIN_NAMESPACE()
24 namespace {
CreateYcbcrSamplerCreateInfo()25 VkSamplerCreateInfo CreateYcbcrSamplerCreateInfo()
26 {
27     return VkSamplerCreateInfo {
28         VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, // sType
29         nullptr,                               // pNext
30         0,                                     // flags
31         VK_FILTER_NEAREST,                     // magFilter
32         VK_FILTER_NEAREST,                     // minFilter
33         VK_SAMPLER_MIPMAP_MODE_NEAREST,        // mipmapMode
34         VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, // addressModeU
35         VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, // addressModeV
36         VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, // addressModeW
37         0.0f,                                  // mipLodBias
38         false,                                 // anisotropyEnable
39         1.0f,                                  // maxAnisotropy
40         false,                                 // compareEnabled
41         VK_COMPARE_OP_NEVER,                   // compareOp
42         0.0f,                                  // minLod
43         0.0f,                                  // maxLod
44         VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK,    // borderColor
45         false,                                 // unnormalizedCoordinates
46     };
47 }
48 } // namespace
49 
CreatePlatformHwBuffer()50 void GpuImageVk::CreatePlatformHwBuffer()
51 {
52     const DeviceVk& deviceVk = (const DeviceVk&)device_;
53     const PlatformDeviceExtensions& deviceExtensions = deviceVk.GetPlatformDeviceExtensions();
54     if ((hwBuffer_ != 0) && deviceExtensions.externalMemoryHardwareBuffer) {
55         const PlatformHardwareBufferUtil::HardwareBufferProperties hwBufferProperties =
56             PlatformHardwareBufferUtil::QueryHwBufferFormatProperties(deviceVk, hwBuffer_);
57         if (hwBufferProperties.allocationSize > 0) {
58             PlatformHardwareBufferUtil::HardwareBufferImage hwBufferImage =
59                 PlatformHardwareBufferUtil::CreateHwPlatformImage(deviceVk, hwBufferProperties, desc_, hwBuffer_);
60             PLUGIN_ASSERT((hwBufferImage.image != VK_NULL_HANDLE) && ((hwBufferImage.deviceMemory != VK_NULL_HANDLE)));
61             plat_.image = hwBufferImage.image;
62             mem_.allocationInfo.deviceMemory = hwBufferImage.deviceMemory;
63 
64             if (plat_.format == VK_FORMAT_UNDEFINED) {
65                 // with external format, chained conversion info is needed
66                 VkSamplerYcbcrConversionCreateInfo ycbcrConversionCreateInfo;
67                 PlatformHardwareBufferUtil::FillYcbcrConversionInfo(hwBufferProperties, ycbcrConversionCreateInfo);
68 
69                 VkExternalFormatOHOS externalFormat {
70                     VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_OHOS, // sType
71                     nullptr,                                // pNext
72                     hwBufferProperties.externalFormat,      // externalFormat
73                 };
74                 ycbcrConversionCreateInfo.pNext = &externalFormat;
75 
76                 const DevicePlatformDataVk& devicePlat = (const DevicePlatformDataVk&)device_.GetPlatformData();
77                 const VkDevice vkDevice = devicePlat.device;
78 
79                 VALIDATE_VK_RESULT(deviceVk.GetExtFunctions().vkCreateSamplerYcbcrConversion(
80                     vkDevice, &ycbcrConversionCreateInfo, nullptr, &platConversion_.samplerConversion));
81 
82                 const VkSamplerYcbcrConversionInfo yCbcrConversionInfo {
83                     VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO, // sType
84                     nullptr,                                         // pNext
85                     platConversion_.samplerConversion,               // conversion
86                 };
87 
88                 VkSamplerCreateInfo samplerCreateInfo = CreateYcbcrSamplerCreateInfo();
89                 samplerCreateInfo.pNext = &yCbcrConversionInfo;
90                 VALIDATE_VK_RESULT(vkCreateSampler(vkDevice, // device
91                     &samplerCreateInfo,                      // pCreateInfo
92                     nullptr,                                 // pAllocator
93                     &platConversion_.sampler));              // pSampler
94 
95                 CreateVkImageViews(VK_IMAGE_ASPECT_COLOR_BIT, &yCbcrConversionInfo);
96             } else {
97                 CreateVkImageViews(VK_IMAGE_ASPECT_COLOR_BIT, nullptr);
98             }
99         } else {
100             hwBuffer_ = 0;
101         }
102     }
103 }
104 
DestroyPlatformHwBuffer()105 void GpuImageVk::DestroyPlatformHwBuffer()
106 {
107     const DeviceVk& deviceVk = (const DeviceVk&)device_;
108     PlatformHardwareBufferUtil::DestroyHwPlatformImage(deviceVk, plat_.image, mem_.allocationInfo.deviceMemory);
109     const DevicePlatformDataVk& devicePlat = (const DevicePlatformDataVk&)device_.GetPlatformData();
110     const VkDevice device = devicePlat.device;
111     if (platConversion_.samplerConversion != VK_NULL_HANDLE) {
112         deviceVk.GetExtFunctions().vkDestroySamplerYcbcrConversion(device, // device
113             platConversion_.samplerConversion,                             // ycbcrConversion
114             nullptr);                                                      // pAllocator
115     }
116     if (platConversion_.sampler != VK_NULL_HANDLE) {
117         vkDestroySampler(device,     // device
118             platConversion_.sampler, // sampler
119             nullptr);                // pAllocator
120     }
121 }
122 RENDER_END_NAMESPACE()
123