1 /*
2 * Copyright (c) 2025 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 #include "buffer_helper.h"
16 #include <mutex>
17 #include <unistd.h>
18 #include "v1_0/imapper.h"
19 #include "v1_1/imetadata.h"
20 #include "v1_0/display_composer_type.h"
21 #include "codec_log_wrapper.h"
22
23 namespace OHOS::Codec::Omx {
24
Create(int fd,bool transferOwnership)25 std::shared_ptr<UniqueFd> UniqueFd::Create(int fd, bool transferOwnership)
26 {
27 int finalFd = transferOwnership ? fd : dup(fd);
28 if (finalFd < 0) {
29 return nullptr;
30 }
31 return std::shared_ptr<UniqueFd>(new UniqueFd(finalFd));
32 }
33
UniqueFd(int fd)34 UniqueFd::UniqueFd(int fd) : fd_(fd) {}
35
~UniqueFd()36 UniqueFd::~UniqueFd()
37 {
38 if (fd_ >= 0) {
39 close(fd_);
40 fd_ = -1;
41 }
42 }
43
Get()44 int UniqueFd::Get()
45 {
46 return fd_;
47 }
48
Release()49 int UniqueFd::Release()
50 {
51 int fd = fd_;
52 fd_ = -1;
53 return fd;
54 }
55
56 std::mutex g_mapperMtx;
57 sptr<OHOS::HDI::Display::Buffer::V1_0::IMapper> g_mapperService;
58
GetMapperService()59 sptr<OHOS::HDI::Display::Buffer::V1_0::IMapper> GetMapperService()
60 {
61 std::lock_guard<std::mutex> lk(g_mapperMtx);
62 if (g_mapperService) {
63 return g_mapperService;
64 }
65 g_mapperService = OHOS::HDI::Display::Buffer::V1_0::IMapper::Get(true);
66 if (g_mapperService) {
67 CODEC_LOGI("get IMapper succ");
68 return g_mapperService;
69 }
70 CODEC_LOGE("get IMapper failed");
71 return nullptr;
72 }
73
74 std::mutex g_metaMtx;
75 sptr<OHOS::HDI::Display::Buffer::V1_1::IMetadata> g_metaService;
76
GetMetaService()77 sptr<OHOS::HDI::Display::Buffer::V1_1::IMetadata> GetMetaService()
78 {
79 std::lock_guard<std::mutex> lk(g_metaMtx);
80 if (g_metaService) {
81 return g_metaService;
82 }
83 g_metaService = OHOS::HDI::Display::Buffer::V1_1::IMetadata::Get(true);
84 if (g_metaService) {
85 CODEC_LOGI("get IMetadata succ");
86 return g_metaService;
87 }
88 CODEC_LOGE("get IMetadata failed");
89 return nullptr;
90 }
91
BufferDestructor(BufferHandle * handle)92 void BufferDestructor(BufferHandle* handle)
93 {
94 if (handle == nullptr) {
95 return;
96 }
97 sptr<OHOS::HDI::Display::Buffer::V1_0::IMapper> mapper = GetMapperService();
98 if (mapper == nullptr) {
99 CODEC_LOGE("destruct bufferHandle failed: unable to get mapper");
100 return;
101 }
102 sptr<NativeBuffer> buffer = sptr<NativeBuffer>::MakeSptr();
103 if (buffer == nullptr) {
104 CODEC_LOGE("destruct bufferHandle failed: unable to get nativeBuffer");
105 return;
106 }
107 buffer->SetBufferHandle(handle, true);
108 mapper->FreeMem(buffer);
109 }
110
ReWrap(const sptr<NativeBuffer> & src,bool isIpcMode)111 sptr<NativeBuffer> ReWrap(const sptr<NativeBuffer>& src, bool isIpcMode)
112 {
113 if (!isIpcMode) {
114 return src;
115 }
116 CHECK_AND_RETURN_RET((src != nullptr), nullptr);
117 BufferHandle* handle = src->Move();
118 CHECK_AND_RETURN_RET((handle != nullptr), nullptr);
119 sptr<NativeBuffer> buffer = sptr<NativeBuffer>::MakeSptr();
120 CHECK_AND_RETURN_RET((buffer != nullptr), nullptr);
121 buffer->SetBufferHandle(handle, true, BufferDestructor);
122 sptr<OHOS::HDI::Display::Buffer::V1_1::IMetadata> meta = GetMetaService();
123 CHECK_AND_RETURN_RET((meta != nullptr), nullptr);
124 int32_t ret = meta->RegisterBuffer(buffer);
125 if (ret != OHOS::HDI::Display::Composer::V1_0::DISPLAY_SUCCESS &&
126 ret != OHOS::HDI::Display::Composer::V1_0::DISPLAY_NOT_SUPPORT) {
127 CODEC_LOGE("RegisterBuffer failed, ret = %{public}d", ret);
128 return nullptr;
129 }
130 return buffer;
131 }
132
Mmap(const sptr<NativeBuffer> & handle)133 int32_t Mmap(const sptr<NativeBuffer>& handle)
134 {
135 sptr<OHOS::HDI::Display::Buffer::V1_0::IMapper> mapper = GetMapperService();
136 if (mapper == nullptr) {
137 return HDF_FAILURE;
138 }
139 return mapper->Mmap(handle);
140 }
141
Unmap(const sptr<NativeBuffer> & handle)142 int32_t Unmap(const sptr<NativeBuffer>& handle)
143 {
144 sptr<OHOS::HDI::Display::Buffer::V1_0::IMapper> mapper = GetMapperService();
145 if (mapper == nullptr) {
146 return HDF_FAILURE;
147 }
148 return mapper->Unmap(handle);
149 }
150 }