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 "ohos_native_buffer_adapter_impl.h"
17 #include "surface_buffer_impl.h"
18 #include "nweb_log.h"
19
20 #include <EGL/egl.h>
21 #include <EGL/eglext.h>
22 #include <GLES3/gl3.h>
23
24 namespace OHOS::NWeb {
25
GetInstance()26 OhosNativeBufferAdapter& OhosNativeBufferAdapterImpl::GetInstance()
27 {
28 WVLOG_D("Native buffer adapter impl get instance.");
29 static OhosNativeBufferAdapterImpl instance;
30 return instance;
31 }
32
OhosNativeBufferAdapterImpl()33 OhosNativeBufferAdapterImpl::OhosNativeBufferAdapterImpl()
34 {
35 WVLOG_D("Native buffer adapter impl constructor.");
36 }
37
~OhosNativeBufferAdapterImpl()38 OhosNativeBufferAdapterImpl::~OhosNativeBufferAdapterImpl()
39 {
40 WVLOG_D("Native buffer adapter impl destructor.");
41 }
42
IsBufferLocked(OH_NativeBuffer * buffer) const43 bool OhosNativeBufferAdapterImpl::IsBufferLocked(OH_NativeBuffer* buffer) const
44 {
45 if (buffer == nullptr) {
46 return false;
47 }
48 auto it = lockedBuffers_.find(buffer);
49 bool result = (it != lockedBuffers_.end() && it->second);
50 WVLOG_D("Native buffer is locked: %{public}s", result ? "yes" : "no");
51 return result;
52 }
53
AcquireBuffer(void * buffer)54 void OhosNativeBufferAdapterImpl::AcquireBuffer(void* buffer)
55 {
56 if (buffer == nullptr) {
57 WVLOG_E("native buffer acquire, buffer is null.");
58 return;
59 }
60 WVLOG_D("native buffer acquired buffer %{private}p.", buffer);
61 OH_NativeBuffer_Reference(static_cast<OH_NativeBuffer*>(buffer));
62 }
63
Release(void * buffer)64 void OhosNativeBufferAdapterImpl::Release(void* buffer)
65 {
66 if (buffer == nullptr) {
67 WVLOG_E("native buffer release, buffer is null.");
68 return;
69 }
70
71 WVLOG_D("native buffer release buffer %{private}p.", buffer);
72 if (OH_NativeBuffer_Unreference(static_cast<OH_NativeBuffer*>(buffer)) == 0) {
73 WVLOG_D("native buffer release, unreference buffer.");
74 }
75
76 buffer = nullptr;
77 }
78
GetEGLBuffer(void * buffer,void ** eglBuffer)79 int OhosNativeBufferAdapterImpl::GetEGLBuffer(void* buffer, void** eglBuffer)
80 {
81 if (buffer == nullptr) {
82 WVLOG_E("native buffer get egl buffer, buffer is null.");
83 return -1;
84 }
85 if (eglBuffer == nullptr) {
86 WVLOG_E("native buffer get egl buffer, eglBuffer pointer is null.");
87 return -1;
88 }
89 WVLOG_D("native buffer GetEGLBuffer %{private}p.", buffer);
90
91 OHNativeWindowBuffer* nativeWindowBuffer =
92 OH_NativeWindow_CreateNativeWindowBufferFromNativeBuffer(static_cast<OH_NativeBuffer*>(buffer));
93 if (nativeWindowBuffer == nullptr) {
94 WVLOG_E("native buffer failed to create native window buffer from native buffer.");
95 return -1;
96 } else {
97 WVLOG_D("native buffer create native window buffer from native bufferL %{private}p.", *eglBuffer);
98 *eglBuffer = nativeWindowBuffer;
99 return 0;
100 }
101 }
102
FreeEGLBuffer(void * eglBuffer)103 int OhosNativeBufferAdapterImpl::FreeEGLBuffer(void* eglBuffer)
104 {
105 if (eglBuffer == nullptr) {
106 WVLOG_E("native buffer free EGLBuffer is null.");
107 return -1;
108 }
109
110 WVLOG_D("native buffer free EGLBuffer %{private}p", eglBuffer);
111 OH_NativeWindow_NativeObjectUnreference(eglBuffer);
112 return 0;
113 }
114
NativeBufferFromNativeWindowBuffer(void * nativeWindowBuffer,void ** nativeBuffer)115 int OhosNativeBufferAdapterImpl::NativeBufferFromNativeWindowBuffer(void* nativeWindowBuffer, void** nativeBuffer)
116 {
117 if (nativeWindowBuffer == nullptr) {
118 WVLOG_E("native buffer NativeBufferFromNativeWindowBuffer, native window buffer is null.");
119 return -1;
120 }
121
122 if (nativeBuffer == nullptr) {
123 WVLOG_E("output nativeBuffer pointer is null");
124 return -1;
125 }
126
127 *nativeBuffer = OH_NativeBufferFromNativeWindowBuffer(static_cast<NativeWindowBuffer*>(nativeWindowBuffer));
128 if (*nativeBuffer == nullptr) {
129 WVLOG_E("native buffer NativeBufferFromNativeWindowBuffer, native buffer is null.");
130 return -1;
131 }
132 WVLOG_D("native buffer NativeBufferFromNativeWindowBuffer %{private}p", nativeWindowBuffer);
133 return 0;
134 }
135
GetSeqNum(void * nativeBuffer)136 uint32_t OhosNativeBufferAdapterImpl::GetSeqNum(void* nativeBuffer)
137 {
138 if (nativeBuffer == nullptr) {
139 WVLOG_E("native buffer GetSeqNum, nativeBuffer is null.");
140 return 0;
141 }
142 SurfaceBufferImpl* buffer = reinterpret_cast<SurfaceBufferImpl *>(nativeBuffer);
143 return buffer->GetSeqNum();
144 }
145
Allocate(const std::shared_ptr<NativeBufferConfigAdapter> bufferConfig,void ** outBuffer)146 void OhosNativeBufferAdapterImpl::Allocate(
147 const std::shared_ptr<NativeBufferConfigAdapter> bufferConfig, void** outBuffer)
148 {
149 if (bufferConfig == nullptr) {
150 return;
151 }
152 if (outBuffer == nullptr) {
153 WVLOG_E("Output buffer pointer is null");
154 return;
155 }
156 int width = bufferConfig->GetBufferWidth();
157 int height = bufferConfig->GetBufferHeight();
158 int usage = bufferConfig->GetBufferUsage();
159 OH_NativeBuffer_Config config = {
160 .width = width,
161 .height = height,
162 .format = OH_NativeBuffer_Format::NATIVEBUFFER_PIXEL_FMT_RGBA_8888,
163 .usage = usage,
164 };
165
166 // create a new OH_NativeBuffer using the OHOS native buffer allocation function
167 // The plan here is that the actual buffer holder will be held onto by chromium.
168 OH_NativeBuffer* buffer = OH_NativeBuffer_Alloc(&config);
169 if (buffer != nullptr) {
170 WVLOG_D("native buffer allocate success, rawbuffer stored %{private}p", buffer);
171 *outBuffer = buffer;
172 } else {
173 WVLOG_E("native buffer allocate failed.");
174 *outBuffer = nullptr;
175 }
176 }
177
Describe(std::shared_ptr<NativeBufferConfigAdapter> bufferConfig,void * buffer)178 void OhosNativeBufferAdapterImpl::Describe(std::shared_ptr<NativeBufferConfigAdapter> bufferConfig, void* buffer)
179 {
180 if (buffer == nullptr) {
181 WVLOG_E("native buffer describe, buffer is null.");
182 return;
183 }
184 WVLOG_D("native buffer describe buffer %{private}p.", buffer);
185
186 OH_NativeBuffer_Config config = {};
187 OH_NativeBuffer_GetConfig(static_cast<OH_NativeBuffer*>(buffer), &config);
188
189 bufferConfig->SetBufferWidth(config.width);
190 bufferConfig->SetBufferHeight(config.height);
191 bufferConfig->SetBufferFormat(config.format);
192 bufferConfig->SetBufferUsage(config.usage);
193 bufferConfig->SetBufferStride(config.stride);
194
195 return;
196 }
197
Lock(void * buffer,uint64_t usage,int32_t fence,void ** out_virtual_address)198 int OhosNativeBufferAdapterImpl::Lock(void* buffer, uint64_t usage, int32_t fence, void** out_virtual_address)
199 {
200 WVLOG_D("native buffer waiting for lock.");
201 if (buffer == nullptr) {
202 WVLOG_E("native buffer lock, buffer is null.");
203 return -1;
204 }
205
206 if (out_virtual_address == nullptr) {
207 WVLOG_E("output address pointer is null.");
208 return -1;
209 }
210
211 if (IsBufferLocked(static_cast<OH_NativeBuffer*>(buffer))) {
212 WVLOG_D("native buffer lock - buffer already locked.");
213 return -1;
214 }
215
216 lockedBuffers_[static_cast<OH_NativeBuffer*>(buffer)] = true;
217
218 return OH_NativeBuffer_Map(static_cast<OH_NativeBuffer*>(buffer), out_virtual_address);
219 }
220
RecvHandleFromUnixSocket(int socketFd,void ** outBuffer)221 int OhosNativeBufferAdapterImpl::RecvHandleFromUnixSocket(int socketFd, void** outBuffer)
222 {
223 WVLOG_D("native buffer receive handle from unix socket.");
224 return 0;
225 }
226
SendHandleToUnixSocket(const void * buffer,int socketFd)227 int OhosNativeBufferAdapterImpl::SendHandleToUnixSocket(const void* buffer, int socketFd)
228 {
229 WVLOG_D("native buffer send handle to unix socket.");
230
231 if (buffer == nullptr) {
232 WVLOG_E("native buffer SendHandleToUnixSocket, buffer is null.");
233 return -1;
234 }
235
236 return 0;
237 }
238
Unlock(void * buffer,int32_t * fence)239 int OhosNativeBufferAdapterImpl::Unlock(void* buffer, int32_t* fence)
240 {
241 WVLOG_D("native buffer waiting for unlock.");
242 if (buffer == nullptr) {
243 WVLOG_E("native buffer lock, buffer is null.");
244 return -1;
245 }
246
247 if (!IsBufferLocked(static_cast<OH_NativeBuffer*>(buffer))) {
248 WVLOG_D("native buffer unlock - buffer is already unlocked.");
249 return -1;
250 }
251
252 int result = OH_NativeBuffer_Unmap(static_cast<OH_NativeBuffer*>(buffer));
253 lockedBuffers_.erase(static_cast<OH_NativeBuffer*>(buffer));
254 return result;
255 }
256
FreeNativeBuffer(void * nativeBuffer)257 int OhosNativeBufferAdapterImpl::FreeNativeBuffer(void* nativeBuffer)
258 {
259 if (nativeBuffer == nullptr) {
260 WVLOG_E("native buffer FreeNativeBuffer, native buffer is null.");
261 return -1;
262 }
263 WVLOG_D("native buffer FreeNativeBuffer freeing: %{private}p.", nativeBuffer);
264 OH_NativeBuffer_Unreference(static_cast<OH_NativeBuffer*>(nativeBuffer));
265 return 0;
266 }
267 } // namespace OHOS::NWeb
268