• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <surface/native_buffer.h>
17 
18 #include <base/util/formats.h>
19 
20 #include "device/device.h"
21 #include "util/log.h"
22 
23 RENDER_BEGIN_NAMESPACE()
24 namespace {
25 using BASE_NS::Format;
26 constexpr BASE_NS::pair<OH_NativeBuffer_Format, Format> FORMATS[] = {
27     { NATIVEBUFFER_PIXEL_FMT_CLUT8, Format::BASE_FORMAT_UNDEFINED },
28     { NATIVEBUFFER_PIXEL_FMT_CLUT1, Format::BASE_FORMAT_UNDEFINED },
29     { NATIVEBUFFER_PIXEL_FMT_CLUT4, Format::BASE_FORMAT_UNDEFINED },
30     { NATIVEBUFFER_PIXEL_FMT_RGB_565, Format::BASE_FORMAT_R5G6B5_UNORM_PACK16 },           // RGB565
31     { NATIVEBUFFER_PIXEL_FMT_RGBA_5658, Format::BASE_FORMAT_UNDEFINED },                   // RGBA5658
32     { NATIVEBUFFER_PIXEL_FMT_RGBX_4444, Format::BASE_FORMAT_UNDEFINED },                   // RGBX4444
33     { NATIVEBUFFER_PIXEL_FMT_RGBA_4444, Format::BASE_FORMAT_R4G4B4A4_UNORM_PACK16 },       // RGBA4444
34     { NATIVEBUFFER_PIXEL_FMT_RGB_444, Format::BASE_FORMAT_UNDEFINED },                     // RGB444
35     { NATIVEBUFFER_PIXEL_FMT_RGBX_5551, Format::BASE_FORMAT_UNDEFINED },                   // RGBX5551
36     { NATIVEBUFFER_PIXEL_FMT_RGBA_5551, Format::BASE_FORMAT_R5G5B5A1_UNORM_PACK16 },       // RGBA5551
37     { NATIVEBUFFER_PIXEL_FMT_RGB_555, Format::BASE_FORMAT_UNDEFINED },                     // RGB555
38     { NATIVEBUFFER_PIXEL_FMT_RGBX_8888, Format::BASE_FORMAT_UNDEFINED },                   // RGBX8888
39     { NATIVEBUFFER_PIXEL_FMT_RGBA_8888, Format::BASE_FORMAT_R8G8B8A8_UNORM },              // RGB888
40     { NATIVEBUFFER_PIXEL_FMT_RGB_888, Format::BASE_FORMAT_R8G8B8_UNORM },                  // RGBA8888
41     { NATIVEBUFFER_PIXEL_FMT_BGR_565, Format::BASE_FORMAT_B5G6R5_UNORM_PACK16 },           // BGR565
42     { NATIVEBUFFER_PIXEL_FMT_BGRX_4444, Format::BASE_FORMAT_UNDEFINED },                   // BGRX4444
43     { NATIVEBUFFER_PIXEL_FMT_BGRA_4444, Format::BASE_FORMAT_B4G4R4A4_UNORM_PACK16 },       // BGRA4444
44     { NATIVEBUFFER_PIXEL_FMT_BGRX_5551, Format::BASE_FORMAT_UNDEFINED },                   // BGRX5551
45     { NATIVEBUFFER_PIXEL_FMT_BGRA_5551, Format::BASE_FORMAT_B5G5R5A1_UNORM_PACK16 },       // BGRA5551
46     { NATIVEBUFFER_PIXEL_FMT_BGRX_8888, Format::BASE_FORMAT_UNDEFINED },                   // BGRX8888
47     { NATIVEBUFFER_PIXEL_FMT_BGRA_8888, Format::BASE_FORMAT_B8G8R8A8_UNORM },              // BGRA8888
48     { NATIVEBUFFER_PIXEL_FMT_YUV_422_I, Format::BASE_FORMAT_UNDEFINED },                   // YUV422 interleaved
49     { NATIVEBUFFER_PIXEL_FMT_YCBCR_422_SP, Format::BASE_FORMAT_G8_B8R8_2PLANE_422_UNORM }, // YCBCR422 semi-plannar
50     { NATIVEBUFFER_PIXEL_FMT_YCRCB_422_SP, Format::BASE_FORMAT_UNDEFINED },                // YCRCB422 semi-plannar
51     { NATIVEBUFFER_PIXEL_FMT_YCBCR_420_SP, Format::BASE_FORMAT_G8_B8R8_2PLANE_420_UNORM }, // YCBCR420 semi-plannar
52     { NATIVEBUFFER_PIXEL_FMT_YCRCB_420_SP, Format::BASE_FORMAT_UNDEFINED },                // YCRCB420 semi-plannar
53     { NATIVEBUFFER_PIXEL_FMT_YCBCR_422_P, Format::BASE_FORMAT_G8_B8_R8_3PLANE_422_UNORM }, // YCBCR422 plannar
54     { NATIVEBUFFER_PIXEL_FMT_YCRCB_422_P, Format::BASE_FORMAT_UNDEFINED },                 // YCRCB422 plannar
55     { NATIVEBUFFER_PIXEL_FMT_YCBCR_420_P, Format::BASE_FORMAT_G8_B8_R8_3PLANE_420_UNORM }, // YCBCR420 plannar NV12
56     { NATIVEBUFFER_PIXEL_FMT_YCRCB_420_P, Format::BASE_FORMAT_UNDEFINED },                 // YCRCB420 plannar NV21
57     { NATIVEBUFFER_PIXEL_FMT_YUYV_422_PKG, Format::BASE_FORMAT_UNDEFINED },                // YUYV422 packed
58     { NATIVEBUFFER_PIXEL_FMT_UYVY_422_PKG, Format::BASE_FORMAT_UNDEFINED },                // UYVY422 packed
59     { NATIVEBUFFER_PIXEL_FMT_YVYU_422_PKG, Format::BASE_FORMAT_UNDEFINED },                // YVYU422 packed
60     { NATIVEBUFFER_PIXEL_FMT_VYUY_422_PKG, Format::BASE_FORMAT_UNDEFINED },                // VYUY422 packed
61     { NATIVEBUFFER_PIXEL_FMT_RGBA_1010102, Format::BASE_FORMAT_A2R10G10B10_UNORM_PACK32 }, // RGBA_1010102 packed
62     { NATIVEBUFFER_PIXEL_FMT_YCBCR_P010, Format::BASE_FORMAT_UNDEFINED }, // YCBCR420 semi-planar 10bit packed
63     { NATIVEBUFFER_PIXEL_FMT_YCRCB_P010, Format::BASE_FORMAT_UNDEFINED }, // YCRCB420 semi-planar 10bit packed
64     { NATIVEBUFFER_PIXEL_FMT_RAW10, Format::BASE_FORMAT_UNDEFINED },      // Raw 10bit packed
65 };
66 
GetCoreFormatFromNativeBufferFormat(uint32_t nativeBufferFormat)67 BASE_NS::Format GetCoreFormatFromNativeBufferFormat(uint32_t nativeBufferFormat)
68 {
69     if (nativeBufferFormat < BASE_NS::countof(FORMATS)) {
70         return FORMATS[nativeBufferFormat].second;
71     }
72     return Format::BASE_FORMAT_UNDEFINED;
73 }
74 } // namespace
75 
GetImageDescFromHwBufferDesc(uintptr_t platformHwBuffer)76 GpuImageDesc GetImageDescFromHwBufferDesc(uintptr_t platformHwBuffer)
77 {
78     auto* aHwBuffer = reinterpret_cast<OH_NativeBuffer*>(platformHwBuffer);
79     OH_NativeBuffer_Config config;
80     OH_NativeBuffer_GetConfig(aHwBuffer, &config);
81     OH_NativeBuffer_ColorSpace colorSpace = OH_COLORSPACE_NONE;
82     if (OH_NativeBuffer_GetColorSpace(aHwBuffer, &colorSpace) != 0) {
83         PLUGIN_LOG_W("Could not get NativeBuffer colors space.");
84     } else {
85         PLUGIN_LOG_I("NativeBuffer colors space %d.", colorSpace);
86     }
87     ImageViewType imageViewType = ImageViewType::CORE_IMAGE_VIEW_TYPE_2D;
88     ImageCreateFlags createFlags = 0;
89 
90     BASE_NS::Format format = GetCoreFormatFromNativeBufferFormat(config.format);
91     if ((colorSpace == OH_COLORSPACE_SRGB_FULL) || (colorSpace == OH_COLORSPACE_DISPLAY_SRGB)) {
92         if (format == BASE_NS::Format::BASE_FORMAT_R8G8B8A8_UNORM) {
93             format = BASE_NS::Format::BASE_FORMAT_R8G8B8A8_SRGB;
94         } else if (format == BASE_NS::Format::BASE_FORMAT_R8G8B8_UNORM) {
95             format = BASE_NS::Format::BASE_FORMAT_R8G8B8_SRGB;
96         } else if (format == BASE_NS::Format::BASE_FORMAT_B8G8R8A8_UNORM) {
97             format = BASE_NS::Format::BASE_FORMAT_B8G8R8A8_SRGB;
98         }
99     }
100 
101     ImageUsageFlags usage = CORE_IMAGE_USAGE_SAMPLED_BIT | CORE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
102     if (format == BASE_NS::BASE_FORMAT_UNDEFINED) {
103         usage &= CORE_IMAGE_USAGE_SAMPLED_BIT;
104     }
105 
106     return GpuImageDesc {
107         ImageType::CORE_IMAGE_TYPE_2D,
108         imageViewType,
109         format,
110         CORE_IMAGE_TILING_OPTIMAL,
111         usage,
112         MemoryPropertyFlagBits::CORE_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
113         createFlags,
114         CORE_ENGINE_IMAGE_CREATION_DYNAMIC_BARRIERS | CORE_ENGINE_IMAGE_CREATION_RESET_STATE_ON_FRAME_BORDERS,
115         config.width,
116         config.height,
117         1,
118         1,
119         1,
120         SampleCountFlagBits::CORE_SAMPLE_COUNT_1_BIT,
121         { CORE_COMPONENT_SWIZZLE_R, CORE_COMPONENT_SWIZZLE_G, CORE_COMPONENT_SWIZZLE_B, CORE_COMPONENT_SWIZZLE_A },
122     };
123 }
124 
GetBufferDescFromHwBufferDesc(uintptr_t platformHwBuffer)125 GpuBufferDesc GetBufferDescFromHwBufferDesc(uintptr_t platformHwBuffer)
126 {
127     auto* aHwBuffer = reinterpret_cast<OH_NativeBuffer*>(platformHwBuffer);
128     OH_NativeBuffer_Config config;
129     OH_NativeBuffer_GetConfig(aHwBuffer, &config);
130 
131     GpuBufferDesc ret;
132     ret.usageFlags |= BufferUsageFlagBits::CORE_BUFFER_USAGE_STORAGE_BUFFER_BIT |
133                       BufferUsageFlagBits::CORE_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
134     ret.byteSize = config.width;
135     ret.format = GetCoreFormatFromNativeBufferFormat(config.format);
136     return ret;
137 }
138 RENDER_END_NAMESPACE()
139