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 "egl_wrapper_display.h"
17
18 #include "egl_defs.h"
19 #include "egl_wrapper_context.h"
20 #include "egl_wrapper_surface.h"
21 #include "../thread_private_data_ctl.h"
22 #include "../wrapper_log.h"
23 namespace OHOS {
24 EglWrapperDisplay EglWrapperDisplay::wrapperDisp_;
25
EglWrapperDisplay()26 EglWrapperDisplay::EglWrapperDisplay() noexcept : disp_(EGL_NO_DISPLAY), refCnt_(0)
27 {
28 WLOGD("");
29 }
30
~EglWrapperDisplay()31 EglWrapperDisplay::~EglWrapperDisplay()
32 {
33 WLOGD("");
34 }
35
Init(EGLint * major,EGLint * minor)36 EGLBoolean EglWrapperDisplay::Init(EGLint *major, EGLint *minor)
37 {
38 WLOGD("");
39 std::lock_guard<std::mutex> lock(refLockMutex_);
40 if (refCnt_ > 0) { // wait other thread init.
41 EglWrapperDispatchTablePtr table = &gWrapperHook;
42 if (major != nullptr) {
43 *major = table->major;
44 }
45 if (minor != nullptr) {
46 *minor = table->minor;
47 }
48 refCnt_++;
49 return EGL_TRUE;
50 }
51
52 EGLBoolean ret = EGL_FALSE;
53 ThreadPrivateDataCtl::SetGlHookTable(&gGlHookNoContext);
54 EglWrapperDispatchTablePtr table = &gWrapperHook;
55 table->major = -1;
56 table->minor = -1;
57 if (table->isLoad && table->egl.eglInitialize) {
58 if (table->egl.eglInitialize(disp_, &table->major, &table->minor)) {
59 WLOGI("initialized ver=%{public}d.%{public}d", table->major, table->minor);
60 ret = EGL_TRUE;
61 if (major != nullptr) {
62 *major = table->major;
63 }
64 if (minor != nullptr) {
65 *minor = table->minor;
66 }
67 refCnt_++;
68 } else {
69 WLOGE("eglInitialize Error.");
70 }
71 return ret;
72 }
73 WLOGE("eglInitialize is invalid.");
74 return ret;
75 }
76
Terminate()77 EGLBoolean EglWrapperDisplay::Terminate()
78 {
79 WLOGD("");
80 std::lock_guard<std::mutex> lock(refLockMutex_);
81 if (refCnt_ == 0) {
82 WLOGI("display is not Init.");
83 return EGL_TRUE;
84 }
85
86 refCnt_--;
87 if (refCnt_ > 0) {
88 return EGL_TRUE;
89 }
90
91 EglWrapperDispatchTablePtr table = &gWrapperHook;
92 if (table->isLoad) {
93 if (table->egl.eglTerminate) {
94 ClearObjects();
95 return table->egl.eglTerminate(disp_);
96 }
97 }
98
99 WLOGE("eglTerminate is invalid.");
100 return EGL_FALSE;
101 }
102
InternalMakeCurrent(EglWrapperSurface * draw,EglWrapperSurface * read,EglWrapperContext * ctx)103 EGLBoolean EglWrapperDisplay::InternalMakeCurrent(
104 EglWrapperSurface *draw, EglWrapperSurface *read, EglWrapperContext *ctx)
105 {
106 WLOGD("");
107 EGLContext actualCtx = EGL_NO_CONTEXT;
108 EGLSurface actualDraw = EGL_NO_SURFACE;
109 EGLSurface actualRead = EGL_NO_SURFACE;
110 if (draw != nullptr) {
111 actualDraw = draw->GetEglSurface();
112 }
113
114 if (read != nullptr) {
115 actualRead = read->GetEglSurface();
116 }
117
118 if (ctx != nullptr) {
119 actualCtx = ctx->GetEglContext();
120 }
121
122 EGLBoolean ret = EGL_FALSE;
123 EglWrapperDispatchTablePtr table = &gWrapperHook;
124 if (table->isLoad && table->egl.eglMakeCurrent) {
125 ret = table->egl.eglMakeCurrent(disp_, actualDraw, actualRead, actualCtx);
126 if (ret == EGL_TRUE) {
127 GlHookTable *hookTable = &gGlHookNoContext;
128 if (ctx != nullptr) {
129 hookTable = &gWrapperHook.gl;
130 ctx->SetCurrentSurface(draw, read);
131 }
132 ThreadPrivateDataCtl::SetGlHookTable(hookTable);
133 ThreadPrivateDataCtl::SetContext(ctx);
134 } else {
135 WLOGE("eglMakeCurrent error.");
136 }
137 } else {
138 WLOGE("eglMakeCurrent is invalid.");
139 }
140 return ret;
141 }
142
MakeCurrent(EGLSurface draw,EGLSurface read,EGLContext ctx)143 EGLBoolean EglWrapperDisplay::MakeCurrent(EGLSurface draw, EGLSurface read, EGLContext ctx)
144 {
145 std::lock_guard<std::mutex> lock(refLockMutex_);
146
147 EglWrapperContext *ctxPtr = nullptr;
148 EglWrapperSurface *surDrawPtr = nullptr;
149 EglWrapperSurface *surReadPtr = nullptr;
150
151 if (ctx != EGL_NO_CONTEXT) {
152 ctxPtr = EglWrapperContext::GetWrapperContext(ctx);
153 if (!CheckObject(ctxPtr)) {
154 WLOGE("EGLContext is invalid.");
155 ThreadPrivateDataCtl::SetError(EGL_BAD_CONTEXT);
156 return EGL_FALSE;
157 }
158 } else {
159 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
160 WLOGE("EGLContext and EGLSurface is bad match.");
161 ThreadPrivateDataCtl::SetError(EGL_BAD_MATCH);
162 return EGL_FALSE;
163 }
164 if (ThreadPrivateDataCtl::GetContext() == EGL_NO_CONTEXT) {
165 WLOGI("There is just no current context. skip");
166 return EGL_TRUE;
167 }
168 }
169
170 if (draw != EGL_NO_SURFACE) {
171 surDrawPtr = EglWrapperSurface::GetWrapperSurface(draw);
172 if (!CheckObject(surDrawPtr)) {
173 WLOGE("EGLSurface is invalid.");
174 ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
175 return EGL_FALSE;
176 }
177 }
178
179 if (read != EGL_NO_SURFACE) {
180 surReadPtr = EglWrapperSurface::GetWrapperSurface(read);
181 if (!CheckObject(surReadPtr)) {
182 WLOGE("EGLSurface is invalid.");
183 ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
184 return EGL_FALSE;
185 }
186 }
187
188 return InternalMakeCurrent(surDrawPtr, surReadPtr, ctxPtr);
189 }
190
GetWrapperDisplay(EGLDisplay display)191 EglWrapperDisplay *EglWrapperDisplay::GetWrapperDisplay(EGLDisplay display)
192 {
193 WLOGD("");
194 EglWrapperDisplay *disp = reinterpret_cast<EglWrapperDisplay *>(display);
195 if (disp == &wrapperDisp_) {
196 return disp;
197 }
198 WLOGE("invalid display pointer.");
199 return nullptr;
200 }
201
GetEglNativeDisplay(EGLenum platform,EGLNativeDisplayType disp,const EGLAttrib * attribList)202 EGLDisplay EglWrapperDisplay::GetEglNativeDisplay(EGLenum platform,
203 EGLNativeDisplayType disp, const EGLAttrib *attribList)
204 {
205 WLOGD("");
206 EglWrapperDispatchTablePtr table = &gWrapperHook;
207 if (table->isLoad) {
208 if (table->egl.eglGetPlatformDisplay) {
209 disp_ = table->egl.eglGetPlatformDisplay(platform, disp, attribList);
210 }
211
212 if (disp_ == EGL_NO_DISPLAY) {
213 if (attribList) {
214 WLOGW("attribList ignored.");
215 }
216
217 if (table->egl.eglGetDisplay) {
218 disp_ = table->egl.eglGetDisplay(disp);
219 } else {
220 WLOGE("eglGetDisplay is invalid.");
221 }
222 }
223 } else {
224 WLOGE("EglWrapperDispatchTable is not load.");
225 }
226
227 WLOGD("");
228
229 return (disp_ == EGL_NO_DISPLAY) ? disp_ : (EGLDisplay)&wrapperDisp_;
230 }
231
GetEglNativeDisplayExt(EGLenum platform,void * disp,const EGLint * attribList)232 EGLDisplay EglWrapperDisplay::GetEglNativeDisplayExt(EGLenum platform,
233 void *disp, const EGLint *attribList)
234 {
235 WLOGD("");
236 EglWrapperDispatchTablePtr table = &gWrapperHook;
237 if (table->isLoad && table->egl.eglGetPlatformDisplayEXT) {
238 disp_ = table->egl.eglGetPlatformDisplayEXT(platform, disp, attribList);
239 } else {
240 WLOGE("eglGetPlatformDisplayEXT is invalid.");
241 }
242
243 return (disp_ == EGL_NO_DISPLAY) ? disp_ : (EGLDisplay)&wrapperDisp_;
244 }
245
GetEglDisplay(EGLenum platform,EGLNativeDisplayType disp,const EGLAttrib * attribList)246 EGLDisplay EglWrapperDisplay::GetEglDisplay(EGLenum platform,
247 EGLNativeDisplayType disp, const EGLAttrib *attribList)
248 {
249 WLOGD("");
250 return wrapperDisp_.GetEglNativeDisplay(platform, disp, attribList);
251 }
252
GetEglDisplayExt(EGLenum platform,void * disp,const EGLint * attribList)253 EGLDisplay EglWrapperDisplay::GetEglDisplayExt(EGLenum platform,
254 void *disp, const EGLint *attribList)
255 {
256 return wrapperDisp_.GetEglNativeDisplayExt(platform, disp, attribList);
257 }
258
ValidateEglContext(EGLContext ctx)259 bool EglWrapperDisplay::ValidateEglContext(EGLContext ctx)
260 {
261 WLOGD("");
262 return false;
263 }
264
ValidateEglSurface(EGLSurface surf)265 bool EglWrapperDisplay::ValidateEglSurface(EGLSurface surf)
266 {
267 WLOGD("");
268 return false;
269 }
270
CreateEglContext(EGLConfig config,EGLContext shareList,const EGLint * attribList)271 EGLContext EglWrapperDisplay::CreateEglContext(EGLConfig config, EGLContext shareList, const EGLint *attribList)
272 {
273 WLOGD("");
274 std::lock_guard<std::mutex> lock(refLockMutex_);
275
276 EGLContext shareCtx = EGL_NO_CONTEXT;
277 if (shareList != EGL_NO_CONTEXT) {
278 EglWrapperContext *ctxPtr = EglWrapperContext::GetWrapperContext(shareList);
279 if (!CheckObject(ctxPtr)) {
280 WLOGE("EGLContext is invalid.");
281 ThreadPrivateDataCtl::SetError(EGL_BAD_CONTEXT);
282 return EGL_NO_CONTEXT;
283 }
284 shareCtx = ctxPtr->GetEglContext();
285 }
286
287 EglWrapperDispatchTablePtr table = &gWrapperHook;
288 if (table->isLoad && table->egl.eglCreateContext) {
289 EGLContext context = table->egl.eglCreateContext(disp_, config, shareCtx, attribList);
290 if (context != EGL_NO_CONTEXT) {
291 return new EglWrapperContext(this, context);
292 } else {
293 WLOGE("egl.eglCreateContext error.");
294 }
295 } else {
296 WLOGE("eglCreateContext is invalid.");
297 }
298
299 return EGL_NO_CONTEXT;
300 }
301
DestroyEglContext(EGLContext context)302 EGLBoolean EglWrapperDisplay::DestroyEglContext(EGLContext context)
303 {
304 WLOGD("");
305 std::lock_guard<std::mutex> lock(refLockMutex_);
306
307 EglWrapperContext *ctxPtr = EglWrapperContext::GetWrapperContext(context);
308 if (!CheckObject(ctxPtr)) {
309 WLOGE("EGLContext is invalid.");
310 ThreadPrivateDataCtl::SetError(EGL_BAD_CONTEXT);
311 return EGL_FALSE;
312 }
313 EGLContext ctx = ctxPtr->GetEglContext();
314
315 EGLBoolean ret = EGL_FALSE;
316 EglWrapperDispatchTablePtr table = &gWrapperHook;
317 if (table->isLoad && table->egl.eglDestroyContext) {
318 ret = table->egl.eglDestroyContext(disp_, ctx);
319 if (ret == EGL_TRUE) {
320 ctxPtr->Destroy();
321 } else {
322 WLOGE("eglDestroyContext error.");
323 }
324 } else {
325 WLOGE("eglDestroyContext is invalid.");
326 }
327
328 return ret;
329 }
330
CreateEglSurface(EGLConfig config,NativeWindowType window,const EGLint * attribList)331 EGLSurface EglWrapperDisplay::CreateEglSurface(EGLConfig config, NativeWindowType window, const EGLint *attribList)
332 {
333 WLOGD("");
334 std::lock_guard<std::mutex> lock(refLockMutex_);
335
336 if (!window) {
337 WLOGE("NativeWindowType window is invalid.");
338 ThreadPrivateDataCtl::SetError(EGL_BAD_NATIVE_WINDOW);
339 return EGL_NO_SURFACE;
340 }
341
342 EglWrapperDispatchTablePtr table = &gWrapperHook;
343 if (table->isLoad && table->egl.eglCreateWindowSurface) {
344 EGLSurface surf = table->egl.eglCreateWindowSurface(disp_, config, window, attribList);
345 if (surf != EGL_NO_SURFACE) {
346 return new EglWrapperSurface(this, surf);
347 } else {
348 WLOGE("egl.eglCreateWindowSurface error.");
349 }
350 } else {
351 WLOGE("eglCreateWindowSurface is invalid.");
352 }
353
354 return EGL_NO_SURFACE;
355 }
356
DestroyEglSurface(EGLSurface surf)357 EGLBoolean EglWrapperDisplay::DestroyEglSurface(EGLSurface surf)
358 {
359 WLOGD("");
360 std::lock_guard<std::mutex> lock(refLockMutex_);
361
362 EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
363 if (!CheckObject(surfPtr)) {
364 WLOGE("EGLSurface is invalid.");
365 ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
366 return EGL_FALSE;
367 }
368 EGLSurface sur = surfPtr->GetEglSurface();
369
370 EGLBoolean ret = EGL_FALSE;
371 EglWrapperDispatchTablePtr table = &gWrapperHook;
372 if (table->isLoad && table->egl.eglDestroySurface) {
373 ret = table->egl.eglDestroySurface(disp_, sur);
374 if (ret == EGL_TRUE) {
375 surfPtr->Destroy();
376 } else {
377 WLOGE("eglDestroySurface error.");
378 }
379 } else {
380 WLOGE("eglDestroySurface is invalid.");
381 }
382
383 return ret;
384 }
385
AddObject(EglWrapperObject * obj)386 void EglWrapperDisplay::AddObject(EglWrapperObject *obj)
387 {
388 std::lock_guard<std::mutex> lock(lockMutex_);
389 objects_.insert(obj);
390 }
391
RemoveObject(EglWrapperObject * obj)392 void EglWrapperDisplay::RemoveObject(EglWrapperObject *obj)
393 {
394 std::lock_guard<std::mutex> lock(lockMutex_);
395 objects_.erase(obj);
396 }
397
ClearObjects()398 void EglWrapperDisplay::ClearObjects()
399 {
400 std::lock_guard<std::mutex> lock(lockMutex_);
401 for (auto obj : objects_) {
402 obj->Release();
403 }
404 objects_.clear();
405 }
406
CheckObject(EglWrapperObject * obj)407 bool EglWrapperDisplay::CheckObject(EglWrapperObject *obj)
408 {
409 std::lock_guard<std::mutex> lock(lockMutex_);
410 if (objects_.find(obj) != objects_.end()) {
411 if (obj->GetDisplay() == this) {
412 return true;
413 }
414 }
415 return false;
416 }
417
CopyBuffers(EGLSurface surf,NativePixmapType target)418 EGLBoolean EglWrapperDisplay::CopyBuffers(EGLSurface surf, NativePixmapType target)
419 {
420 WLOGD("");
421 std::lock_guard<std::mutex> lock(refLockMutex_);
422
423 EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
424 if (!CheckObject(surfPtr)) {
425 WLOGE("EGLSurface is invalid.");
426 ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
427 return EGL_FALSE;
428 }
429
430 EGLBoolean ret = EGL_FALSE;
431 EglWrapperDispatchTablePtr table = &gWrapperHook;
432 if (table->isLoad && table->egl.eglCopyBuffers) {
433 ret = table->egl.eglCopyBuffers(disp_, surfPtr->GetEglSurface(), target);
434 } else {
435 WLOGE("eglCopyBuffers is invalid.");
436 }
437
438 return ret;
439 }
440
CreatePbufferSurface(EGLConfig config,const EGLint * attribList)441 EGLSurface EglWrapperDisplay::CreatePbufferSurface(EGLConfig config, const EGLint *attribList)
442 {
443 WLOGD("");
444 std::lock_guard<std::mutex> lock(refLockMutex_);
445
446 EglWrapperDispatchTablePtr table = &gWrapperHook;
447 if (table->isLoad && table->egl.eglCreatePbufferSurface) {
448 EGLSurface surf = table->egl.eglCreatePbufferSurface(disp_, config, attribList);
449 if (surf != EGL_NO_SURFACE) {
450 return new EglWrapperSurface(this, surf);
451 } else {
452 WLOGE("egl.eglCreatePbufferSurface error.");
453 }
454 } else {
455 WLOGE("eglCreatePbufferSurface is invalid.");
456 }
457
458 return EGL_NO_SURFACE;
459 }
460
CreatePixmapSurface(EGLConfig config,EGLNativePixmapType pixmap,const EGLint * attribList)461 EGLSurface EglWrapperDisplay::CreatePixmapSurface(EGLConfig config,
462 EGLNativePixmapType pixmap, const EGLint* attribList)
463 {
464 WLOGD("");
465 std::lock_guard<std::mutex> lock(refLockMutex_);
466
467 EglWrapperDispatchTablePtr table = &gWrapperHook;
468 if (table->isLoad && table->egl.eglCreatePixmapSurface) {
469 EGLSurface surf = table->egl.eglCreatePixmapSurface(disp_, config, pixmap, attribList);
470 if (surf != EGL_NO_SURFACE) {
471 return new EglWrapperSurface(this, surf);
472 } else {
473 WLOGE("egl.eglCreatePixmapSurface error.");
474 }
475 } else {
476 WLOGE("eglCreatePixmapSurface is invalid.");
477 }
478
479 return EGL_NO_SURFACE;
480 }
481
QueryContext(EGLContext ctx,EGLint attribute,EGLint * value)482 EGLBoolean EglWrapperDisplay::QueryContext(EGLContext ctx, EGLint attribute, EGLint *value)
483 {
484 WLOGD("");
485 std::lock_guard<std::mutex> lock(refLockMutex_);
486
487 EglWrapperContext *ctxPtr = EglWrapperContext::GetWrapperContext(ctx);
488 if (!CheckObject(ctxPtr)) {
489 WLOGE("EGLContext is invalid.");
490 ThreadPrivateDataCtl::SetError(EGL_BAD_CONTEXT);
491 return EGL_FALSE;
492 }
493
494 EGLBoolean ret = EGL_FALSE;
495 EglWrapperDispatchTablePtr table = &gWrapperHook;
496 if (table->isLoad && table->egl.eglQueryContext) {
497 ret = table->egl.eglQueryContext(disp_,
498 ctxPtr->GetEglContext(), attribute, value);
499 } else {
500 WLOGE("eglQueryContext is invalid.");
501 }
502
503 return ret;
504 }
505
QuerySurface(EGLSurface surf,EGLint attribute,EGLint * value)506 EGLBoolean EglWrapperDisplay::QuerySurface(EGLSurface surf, EGLint attribute, EGLint *value)
507 {
508 WLOGD("");
509 std::lock_guard<std::mutex> lock(refLockMutex_);
510
511 EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
512 if (!CheckObject(surfPtr)) {
513 WLOGE("EGLSurface is invalid.");
514 ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
515 return EGL_FALSE;
516 }
517
518 EGLBoolean ret = EGL_FALSE;
519 EglWrapperDispatchTablePtr table = &gWrapperHook;
520 if (table->isLoad && table->egl.eglQuerySurface) {
521 ret = table->egl.eglQuerySurface(disp_,
522 surfPtr->GetEglSurface(), attribute, value);
523 } else {
524 WLOGE("eglQuerySurface is invalid.");
525 }
526
527 return ret;
528 }
529
SwapBuffers(EGLSurface surf)530 EGLBoolean EglWrapperDisplay::SwapBuffers(EGLSurface surf)
531 {
532 WLOGD("");
533 std::lock_guard<std::mutex> lock(refLockMutex_);
534
535 EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
536 if (!CheckObject(surfPtr)) {
537 if (surfPtr->GetEglSurface() == nullptr || disp_ == EGL_NO_DISPLAY) {
538 WLOGE("INparament is invalid.");
539 return EGL_FALSE;
540 }
541 WLOGE("EGLSurface is invalid.");
542 ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
543 return EGL_FALSE;
544 }
545
546 EGLBoolean ret = EGL_FALSE;
547 EglWrapperDispatchTablePtr table = &gWrapperHook;
548 if (table->isLoad && table->egl.eglSwapBuffers) {
549 ret = table->egl.eglSwapBuffers(disp_, surfPtr->GetEglSurface());
550 } else {
551 WLOGE("eglSwapBuffers is invalid.");
552 }
553
554 return ret;
555 }
556
BindTexImage(EGLSurface surf,EGLint buffer)557 EGLBoolean EglWrapperDisplay::BindTexImage(EGLSurface surf, EGLint buffer)
558 {
559 WLOGD("");
560 std::lock_guard<std::mutex> lock(refLockMutex_);
561
562 EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
563 if (!CheckObject(surfPtr)) {
564 WLOGE("EGLSurface is invalid.");
565 ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
566 return EGL_FALSE;
567 }
568
569 EGLBoolean ret = EGL_FALSE;
570 EglWrapperDispatchTablePtr table = &gWrapperHook;
571 if (table->isLoad && table->egl.eglBindTexImage) {
572 ret = table->egl.eglBindTexImage(disp_, surfPtr->GetEglSurface(), buffer);
573 } else {
574 WLOGE("eglBindTexImage is invalid.");
575 }
576
577 return ret;
578 }
579
ReleaseTexImage(EGLSurface surf,EGLint buffer)580 EGLBoolean EglWrapperDisplay::ReleaseTexImage(EGLSurface surf, EGLint buffer)
581 {
582 WLOGD("");
583 std::lock_guard<std::mutex> lock(refLockMutex_);
584
585 EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
586 if (!CheckObject(surfPtr)) {
587 WLOGE("EGLSurface is invalid.");
588 ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
589 return EGL_FALSE;
590 }
591
592 EGLBoolean ret = EGL_FALSE;
593 EglWrapperDispatchTablePtr table = &gWrapperHook;
594 if (table->isLoad && table->egl.eglReleaseTexImage) {
595 ret = table->egl.eglReleaseTexImage(disp_, surfPtr->GetEglSurface(), buffer);
596 } else {
597 WLOGE("eglReleaseTexImage is invalid.");
598 }
599
600 return ret;
601 }
602
SurfaceAttrib(EGLSurface surf,EGLint attribute,EGLint value)603 EGLBoolean EglWrapperDisplay::SurfaceAttrib(EGLSurface surf, EGLint attribute, EGLint value)
604 {
605 WLOGD("");
606 std::lock_guard<std::mutex> lock(refLockMutex_);
607
608 EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
609 if (!CheckObject(surfPtr)) {
610 WLOGE("EGLSurface is invalid.");
611 ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
612 return EGL_FALSE;
613 }
614
615 EGLBoolean ret = EGL_FALSE;
616 EglWrapperDispatchTablePtr table = &gWrapperHook;
617 if (table->isLoad && table->egl.eglSurfaceAttrib) {
618 ret = table->egl.eglSurfaceAttrib(disp_,
619 surfPtr->GetEglSurface(), attribute, value);
620 } else {
621 WLOGE("eglSurfaceAttrib is invalid.");
622 }
623
624 return ret;
625 }
626
CreatePbufferFromClientBuffer(EGLenum buftype,EGLClientBuffer buffer,EGLConfig config,const EGLint * attribList)627 EGLSurface EglWrapperDisplay::CreatePbufferFromClientBuffer(
628 EGLenum buftype, EGLClientBuffer buffer,
629 EGLConfig config, const EGLint *attribList)
630 {
631 WLOGD("");
632 std::lock_guard<std::mutex> lock(refLockMutex_);
633
634 EglWrapperDispatchTablePtr table = &gWrapperHook;
635 if (table->isLoad && table->egl.eglCreatePbufferFromClientBuffer) {
636 EGLSurface surf = table->egl.eglCreatePbufferFromClientBuffer(
637 disp_, buftype, buffer, config, attribList);
638 if (surf != EGL_NO_SURFACE) {
639 return new EglWrapperSurface(this, surf);
640 } else {
641 WLOGE("egl.eglCreatePbufferFromClientBuffer error.");
642 }
643 } else {
644 WLOGE("eglCreatePbufferFromClientBuffer is invalid.");
645 }
646
647 return EGL_NO_SURFACE;
648 }
649
CreateImage(EGLContext ctx,EGLenum target,EGLClientBuffer buffer,const EGLAttrib * attribList)650 EGLImage EglWrapperDisplay::CreateImage(EGLContext ctx, EGLenum target,
651 EGLClientBuffer buffer, const EGLAttrib *attribList)
652 {
653 WLOGD("");
654 std::lock_guard<std::mutex> lock(refLockMutex_);
655
656 EGLContext actualCtx = EGL_NO_CONTEXT;
657 if (ctx != EGL_NO_CONTEXT) {
658 EglWrapperContext *ctxPtr = EglWrapperContext::GetWrapperContext(ctx);
659 if (CheckObject(ctxPtr)) {
660 actualCtx = ctxPtr->GetEglContext();
661 }
662 }
663
664 EglWrapperDispatchTablePtr table = &gWrapperHook;
665 if (table->isLoad && table->egl.eglCreateImage) {
666 return table->egl.eglCreateImage(disp_, actualCtx, target, buffer, attribList);
667 } else {
668 WLOGE("eglCreateImage is invalid.");
669 }
670
671 return EGL_NO_IMAGE_KHR;
672 }
673
DestroyImage(EGLImage img)674 EGLBoolean EglWrapperDisplay::DestroyImage(EGLImage img)
675 {
676 WLOGD("");
677 std::lock_guard<std::mutex> lock(refLockMutex_);
678
679 EGLBoolean ret = EGL_FALSE;
680 EglWrapperDispatchTablePtr table = &gWrapperHook;
681 if (table->isLoad && table->egl.eglDestroyImage) {
682 ret = table->egl.eglDestroyImage(disp_, img);
683 } else {
684 WLOGE("eglDestroyImage is invalid.");
685 }
686
687 return ret;
688 }
689
CreatePlatformWindowSurface(EGLConfig config,void * nativeWindow,const EGLAttrib * attribList)690 EGLSurface EglWrapperDisplay::CreatePlatformWindowSurface(EGLConfig config,
691 void *nativeWindow, const EGLAttrib *attribList)
692 {
693 WLOGD("");
694 std::lock_guard<std::mutex> lock(refLockMutex_);
695
696 if (!nativeWindow) {
697 WLOGE("nativeWindow is invalid.");
698 ThreadPrivateDataCtl::SetError(EGL_BAD_NATIVE_WINDOW);
699 return EGL_NO_SURFACE;
700 }
701
702 EglWrapperDispatchTablePtr table = &gWrapperHook;
703 if (table->isLoad && table->egl.eglCreatePlatformWindowSurface) {
704 EGLSurface surf = table->egl.eglCreatePlatformWindowSurface(disp_,
705 config, nativeWindow, attribList);
706 if (surf != EGL_NO_SURFACE) {
707 return new EglWrapperSurface(this, surf);
708 } else {
709 WLOGE("egl.eglCreatePlatformWindowSurface error.");
710 }
711 } else {
712 WLOGE("eglCreatePlatformWindowSurface is invalid.");
713 }
714
715 return EGL_NO_SURFACE;
716 }
717
CreatePlatformPixmapSurface(EGLConfig config,void * nativePixmap,const EGLAttrib * attribList)718 EGLSurface EglWrapperDisplay::CreatePlatformPixmapSurface(EGLConfig config,
719 void *nativePixmap, const EGLAttrib *attribList)
720 {
721 WLOGD("");
722 std::lock_guard<std::mutex> lock(refLockMutex_);
723
724 if (!nativePixmap) {
725 WLOGE("nativePixmap is invalid.");
726 ThreadPrivateDataCtl::SetError(EGL_BAD_NATIVE_WINDOW);
727 return EGL_NO_SURFACE;
728 }
729
730 EglWrapperDispatchTablePtr table = &gWrapperHook;
731 if (table->isLoad && table->egl.eglCreatePlatformPixmapSurface) {
732 EGLSurface surf = table->egl.eglCreatePlatformPixmapSurface(disp_,
733 config, nativePixmap, attribList);
734 if (surf != EGL_NO_SURFACE) {
735 return new EglWrapperSurface(this, surf);
736 } else {
737 WLOGE("egl.eglCreatePlatformPixmapSurface error.");
738 }
739 } else {
740 WLOGE("eglCreatePlatformPixmapSurface is invalid.");
741 }
742
743 return EGL_NO_SURFACE;
744 }
745
LockSurfaceKHR(EGLSurface surf,const EGLint * attribList)746 EGLBoolean EglWrapperDisplay::LockSurfaceKHR(EGLSurface surf, const EGLint *attribList)
747 {
748 WLOGD("");
749 std::lock_guard<std::mutex> lock(refLockMutex_);
750
751 EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
752 if (!CheckObject(surfPtr)) {
753 WLOGE("EGLSurface is invalid.");
754 ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
755 return EGL_FALSE;
756 }
757
758 EGLBoolean ret = EGL_FALSE;
759 EglWrapperDispatchTablePtr table = &gWrapperHook;
760 if (table->isLoad && table->egl.eglLockSurfaceKHR) {
761 ret = table->egl.eglLockSurfaceKHR(disp_,
762 surfPtr->GetEglSurface(), attribList);
763 } else {
764 WLOGE("eglLockSurfaceKHR is invalid.");
765 }
766
767 return ret;
768 }
769
UnlockSurfaceKHR(EGLSurface surf)770 EGLBoolean EglWrapperDisplay::UnlockSurfaceKHR(EGLSurface surf)
771 {
772 WLOGD("");
773 std::lock_guard<std::mutex> lock(refLockMutex_);
774
775 EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
776 if (!CheckObject(surfPtr)) {
777 WLOGE("EGLSurface is invalid.");
778 ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
779 return EGL_FALSE;
780 }
781
782 EGLBoolean ret = EGL_FALSE;
783 EglWrapperDispatchTablePtr table = &gWrapperHook;
784 if (table->isLoad && table->egl.eglUnlockSurfaceKHR) {
785 ret = table->egl.eglUnlockSurfaceKHR(disp_, surfPtr->GetEglSurface());
786 } else {
787 WLOGE("eglUnlockSurfaceKHR is invalid.");
788 }
789
790 return ret;
791 }
792
CreateImageKHR(EGLContext ctx,EGLenum target,EGLClientBuffer buffer,const EGLint * attribList)793 EGLImageKHR EglWrapperDisplay::CreateImageKHR(EGLContext ctx, EGLenum target,
794 EGLClientBuffer buffer, const EGLint *attribList)
795 {
796 WLOGD("");
797 std::lock_guard<std::mutex> lock(refLockMutex_);
798
799 EGLContext actualCtx = EGL_NO_CONTEXT;
800 if (ctx != EGL_NO_CONTEXT) {
801 EglWrapperContext *ctxPtr = EglWrapperContext::GetWrapperContext(ctx);
802 if (CheckObject(ctxPtr)) {
803 actualCtx = ctxPtr->GetEglContext();
804 }
805 }
806
807 EglWrapperDispatchTablePtr table = &gWrapperHook;
808 if (table->isLoad && table->egl.eglCreateImageKHR) {
809 return table->egl.eglCreateImageKHR(disp_, actualCtx, target, buffer, attribList);
810 } else {
811 WLOGE("eglCreateImageKHR is invalid.");
812 }
813
814 return EGL_NO_IMAGE_KHR;
815 }
816
DestroyImageKHR(EGLImageKHR img)817 EGLBoolean EglWrapperDisplay::DestroyImageKHR(EGLImageKHR img)
818 {
819 WLOGD("");
820 std::lock_guard<std::mutex> lock(refLockMutex_);
821
822 EGLBoolean ret = EGL_FALSE;
823 EglWrapperDispatchTablePtr table = &gWrapperHook;
824 if (table->isLoad && table->egl.eglDestroyImageKHR) {
825 ret = table->egl.eglDestroyImageKHR(disp_, img);
826 } else {
827 WLOGE("eglDestroyImageKHR is invalid.");
828 }
829
830 return ret;
831 }
832
CreateStreamProducerSurfaceKHR(EGLConfig config,EGLStreamKHR stream,const EGLint * attribList)833 EGLSurface EglWrapperDisplay::CreateStreamProducerSurfaceKHR(EGLConfig config,
834 EGLStreamKHR stream, const EGLint *attribList)
835 {
836 WLOGD("");
837 std::lock_guard<std::mutex> lock(refLockMutex_);
838
839 EglWrapperDispatchTablePtr table = &gWrapperHook;
840 if (table->isLoad && table->egl.eglCreateStreamProducerSurfaceKHR) {
841 EGLSurface surf = table->egl.eglCreateStreamProducerSurfaceKHR(
842 disp_, config, stream, attribList);
843 if (surf != EGL_NO_SURFACE) {
844 return new EglWrapperSurface(this, surf);
845 } else {
846 WLOGE("egl.eglCreateStreamProducerSurfaceKHR error.");
847 }
848 } else {
849 WLOGE("eglCreateStreamProducerSurfaceKHR is invalid.");
850 }
851
852 return EGL_NO_SURFACE;
853 }
854
SwapBuffersWithDamageKHR(EGLSurface draw,EGLint * rects,EGLint nRects)855 EGLBoolean EglWrapperDisplay::SwapBuffersWithDamageKHR(EGLSurface draw, EGLint *rects, EGLint nRects)
856 {
857 WLOGD("");
858 std::lock_guard<std::mutex> lock(refLockMutex_);
859
860 EglWrapperSurface *surfacePtr = EglWrapperSurface::GetWrapperSurface(draw);
861 if (!CheckObject(surfacePtr)) {
862 WLOGE("EGLSurface is invalid.");
863 ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
864 return EGL_FALSE;
865 }
866
867 if (nRects < 0 || (nRects > 0 && rects == NULL)) {
868 WLOGE("Paramter error.");
869 ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
870 return EGL_FALSE;
871 }
872
873 EGLBoolean ret = EGL_FALSE;
874 EglWrapperDispatchTablePtr table = &gWrapperHook;
875 if (table->isLoad && table->egl.eglSwapBuffersWithDamageKHR) {
876 ret = table->egl.eglSwapBuffersWithDamageKHR(
877 disp_, surfacePtr->GetEglSurface(), rects, nRects);
878 } else {
879 WLOGE("eglSwapBuffersWithDamageKHR is invalid.");
880 }
881
882 return ret;
883 }
884
SetDamageRegionKHR(EGLSurface surf,EGLint * rects,EGLint nRects)885 EGLBoolean EglWrapperDisplay::SetDamageRegionKHR(EGLSurface surf, EGLint *rects, EGLint nRects)
886 {
887 WLOGD("");
888 std::lock_guard<std::mutex> lock(refLockMutex_);
889
890 EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
891 if (!CheckObject(surfPtr)) {
892 WLOGE("EGLSurface is invalid.");
893 ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
894 return EGL_FALSE;
895 }
896
897 if (nRects < 0 || (nRects > 0 && rects == nullptr)) {
898 WLOGE("Paramter error.");
899 ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
900 return EGL_FALSE;
901 }
902
903 EGLBoolean ret = EGL_FALSE;
904 EglWrapperDispatchTablePtr table = &gWrapperHook;
905 if (table->isLoad && table->egl.eglSetDamageRegionKHR) {
906 ret = table->egl.eglSetDamageRegionKHR(
907 disp_, surfPtr->GetEglSurface(), rects, nRects);
908 } else {
909 WLOGE("eglSetDamageRegionKHR is invalid.");
910 }
911
912 return ret;
913 }
914 } // namespace OHOS
915