• 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 "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         WLOGE("EGLSurface is invalid.");
538         ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
539         return EGL_FALSE;
540     }
541 
542     EGLBoolean ret = EGL_FALSE;
543     EglWrapperDispatchTablePtr table = &gWrapperHook;
544     if (table->isLoad && table->egl.eglSwapBuffers) {
545         ret = table->egl.eglSwapBuffers(disp_, surfPtr->GetEglSurface());
546     } else {
547         WLOGE("eglSwapBuffers is invalid.");
548     }
549 
550     return ret;
551 }
552 
BindTexImage(EGLSurface surf,EGLint buffer)553 EGLBoolean EglWrapperDisplay::BindTexImage(EGLSurface surf, EGLint buffer)
554 {
555     WLOGD("");
556     std::lock_guard<std::mutex> lock(refLockMutex_);
557 
558     EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
559     if (!CheckObject(surfPtr)) {
560         WLOGE("EGLSurface is invalid.");
561         ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
562         return EGL_FALSE;
563     }
564 
565     EGLBoolean ret = EGL_FALSE;
566     EglWrapperDispatchTablePtr table = &gWrapperHook;
567     if (table->isLoad && table->egl.eglBindTexImage) {
568         ret = table->egl.eglBindTexImage(disp_, surfPtr->GetEglSurface(), buffer);
569     } else {
570         WLOGE("eglBindTexImage is invalid.");
571     }
572 
573     return ret;
574 }
575 
ReleaseTexImage(EGLSurface surf,EGLint buffer)576 EGLBoolean EglWrapperDisplay::ReleaseTexImage(EGLSurface surf, EGLint buffer)
577 {
578     WLOGD("");
579     std::lock_guard<std::mutex> lock(refLockMutex_);
580 
581     EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
582     if (!CheckObject(surfPtr)) {
583         WLOGE("EGLSurface is invalid.");
584         ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
585         return EGL_FALSE;
586     }
587 
588     EGLBoolean ret = EGL_FALSE;
589     EglWrapperDispatchTablePtr table = &gWrapperHook;
590     if (table->isLoad && table->egl.eglReleaseTexImage) {
591         ret = table->egl.eglReleaseTexImage(disp_, surfPtr->GetEglSurface(), buffer);
592     } else {
593         WLOGE("eglReleaseTexImage is invalid.");
594     }
595 
596     return ret;
597 }
598 
SurfaceAttrib(EGLSurface surf,EGLint attribute,EGLint value)599 EGLBoolean EglWrapperDisplay::SurfaceAttrib(EGLSurface surf, EGLint attribute, EGLint value)
600 {
601     WLOGD("");
602     std::lock_guard<std::mutex> lock(refLockMutex_);
603 
604     EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
605     if (!CheckObject(surfPtr)) {
606         WLOGE("EGLSurface is invalid.");
607         ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
608         return EGL_FALSE;
609     }
610 
611     EGLBoolean ret = EGL_FALSE;
612     EglWrapperDispatchTablePtr table = &gWrapperHook;
613     if (table->isLoad && table->egl.eglSurfaceAttrib) {
614         ret = table->egl.eglSurfaceAttrib(disp_,
615             surfPtr->GetEglSurface(), attribute, value);
616     } else {
617         WLOGE("eglSurfaceAttrib is invalid.");
618     }
619 
620     return ret;
621 }
622 
CreatePbufferFromClientBuffer(EGLenum buftype,EGLClientBuffer buffer,EGLConfig config,const EGLint * attribList)623 EGLSurface EglWrapperDisplay::CreatePbufferFromClientBuffer(
624     EGLenum buftype, EGLClientBuffer buffer,
625     EGLConfig config, const EGLint *attribList)
626 {
627     WLOGD("");
628     std::lock_guard<std::mutex> lock(refLockMutex_);
629 
630     EglWrapperDispatchTablePtr table = &gWrapperHook;
631     if (table->isLoad && table->egl.eglCreatePbufferFromClientBuffer) {
632         EGLSurface surf = table->egl.eglCreatePbufferFromClientBuffer(
633             disp_, buftype, buffer, config, attribList);
634         if (surf != EGL_NO_SURFACE) {
635             return new EglWrapperSurface(this, surf);
636         } else {
637             WLOGE("egl.eglCreatePbufferFromClientBuffer error.");
638         }
639     } else {
640         WLOGE("eglCreatePbufferFromClientBuffer is invalid.");
641     }
642 
643     return EGL_NO_SURFACE;
644 }
645 
CreateImage(EGLContext ctx,EGLenum target,EGLClientBuffer buffer,const EGLAttrib * attribList)646 EGLImage EglWrapperDisplay::CreateImage(EGLContext ctx, EGLenum target,
647     EGLClientBuffer buffer, const EGLAttrib *attribList)
648 {
649     WLOGD("");
650     std::lock_guard<std::mutex> lock(refLockMutex_);
651 
652     EGLContext actualCtx  = EGL_NO_CONTEXT;
653     if (ctx != EGL_NO_CONTEXT) {
654         EglWrapperContext *ctxPtr = EglWrapperContext::GetWrapperContext(ctx);
655         if (CheckObject(ctxPtr)) {
656             actualCtx = ctxPtr->GetEglContext();
657         }
658     }
659 
660     EglWrapperDispatchTablePtr table = &gWrapperHook;
661     if (table->isLoad && table->egl.eglCreateImage) {
662         return table->egl.eglCreateImage(disp_, actualCtx, target, buffer, attribList);
663     } else {
664         WLOGE("eglCreateImage is invalid.");
665     }
666 
667     return EGL_NO_IMAGE_KHR;
668 }
669 
DestroyImage(EGLImage img)670 EGLBoolean EglWrapperDisplay::DestroyImage(EGLImage img)
671 {
672     WLOGD("");
673     std::lock_guard<std::mutex> lock(refLockMutex_);
674 
675     EGLBoolean ret = EGL_FALSE;
676     EglWrapperDispatchTablePtr table = &gWrapperHook;
677     if (table->isLoad && table->egl.eglDestroyImage) {
678         ret = table->egl.eglDestroyImage(disp_, img);
679     } else {
680         WLOGE("eglDestroyImage is invalid.");
681     }
682 
683     return ret;
684 }
685 
CreatePlatformWindowSurface(EGLConfig config,void * nativeWindow,const EGLAttrib * attribList)686 EGLSurface EglWrapperDisplay::CreatePlatformWindowSurface(EGLConfig config,
687     void *nativeWindow, const EGLAttrib *attribList)
688 {
689     WLOGD("");
690     std::lock_guard<std::mutex> lock(refLockMutex_);
691 
692     if (!nativeWindow) {
693         WLOGE("nativeWindow is invalid.");
694         ThreadPrivateDataCtl::SetError(EGL_BAD_NATIVE_WINDOW);
695         return EGL_NO_SURFACE;
696     }
697 
698     EglWrapperDispatchTablePtr table = &gWrapperHook;
699     if (table->isLoad && table->egl.eglCreatePlatformWindowSurface) {
700         EGLSurface surf = table->egl.eglCreatePlatformWindowSurface(disp_,
701             config, nativeWindow, attribList);
702         if (surf != EGL_NO_SURFACE) {
703             return new EglWrapperSurface(this, surf);
704         } else {
705             WLOGE("egl.eglCreatePlatformWindowSurface error.");
706         }
707     } else {
708         WLOGE("eglCreatePlatformWindowSurface is invalid.");
709     }
710 
711     return EGL_NO_SURFACE;
712 }
713 
CreatePlatformPixmapSurface(EGLConfig config,void * nativePixmap,const EGLAttrib * attribList)714 EGLSurface EglWrapperDisplay::CreatePlatformPixmapSurface(EGLConfig config,
715     void *nativePixmap, const EGLAttrib *attribList)
716 {
717     WLOGD("");
718     std::lock_guard<std::mutex> lock(refLockMutex_);
719 
720     if (!nativePixmap) {
721         WLOGE("nativePixmap is invalid.");
722         ThreadPrivateDataCtl::SetError(EGL_BAD_NATIVE_WINDOW);
723         return EGL_NO_SURFACE;
724     }
725 
726     EglWrapperDispatchTablePtr table = &gWrapperHook;
727     if (table->isLoad && table->egl.eglCreatePlatformPixmapSurface) {
728         EGLSurface surf = table->egl.eglCreatePlatformPixmapSurface(disp_,
729             config, nativePixmap, attribList);
730         if (surf != EGL_NO_SURFACE) {
731             return new EglWrapperSurface(this, surf);
732         } else {
733             WLOGE("egl.eglCreatePlatformPixmapSurface error.");
734         }
735     } else {
736         WLOGE("eglCreatePlatformPixmapSurface is invalid.");
737     }
738 
739     return EGL_NO_SURFACE;
740 }
741 
LockSurfaceKHR(EGLSurface surf,const EGLint * attribList)742 EGLBoolean EglWrapperDisplay::LockSurfaceKHR(EGLSurface surf, const EGLint *attribList)
743 {
744     WLOGD("");
745     std::lock_guard<std::mutex> lock(refLockMutex_);
746 
747     EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
748     if (!CheckObject(surfPtr)) {
749         WLOGE("EGLSurface is invalid.");
750         ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
751         return EGL_FALSE;
752     }
753 
754     EGLBoolean ret = EGL_FALSE;
755     EglWrapperDispatchTablePtr table = &gWrapperHook;
756     if (table->isLoad && table->egl.eglLockSurfaceKHR) {
757         ret = table->egl.eglLockSurfaceKHR(disp_,
758             surfPtr->GetEglSurface(), attribList);
759     } else {
760         WLOGE("eglLockSurfaceKHR is invalid.");
761     }
762 
763     return ret;
764 }
765 
UnlockSurfaceKHR(EGLSurface surf)766 EGLBoolean EglWrapperDisplay::UnlockSurfaceKHR(EGLSurface surf)
767 {
768     WLOGD("");
769     std::lock_guard<std::mutex> lock(refLockMutex_);
770 
771     EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
772     if (!CheckObject(surfPtr)) {
773         WLOGE("EGLSurface is invalid.");
774         ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
775         return EGL_FALSE;
776     }
777 
778     EGLBoolean ret = EGL_FALSE;
779     EglWrapperDispatchTablePtr table = &gWrapperHook;
780     if (table->isLoad && table->egl.eglUnlockSurfaceKHR) {
781         ret = table->egl.eglUnlockSurfaceKHR(disp_, surfPtr->GetEglSurface());
782     } else {
783         WLOGE("eglUnlockSurfaceKHR is invalid.");
784     }
785 
786     return ret;
787 }
788 
CreateImageKHR(EGLContext ctx,EGLenum target,EGLClientBuffer buffer,const EGLint * attribList)789 EGLImageKHR EglWrapperDisplay::CreateImageKHR(EGLContext ctx, EGLenum target,
790     EGLClientBuffer buffer, const EGLint *attribList)
791 {
792     WLOGD("");
793     std::lock_guard<std::mutex> lock(refLockMutex_);
794 
795     EGLContext actualCtx  = EGL_NO_CONTEXT;
796     if (ctx != EGL_NO_CONTEXT) {
797         EglWrapperContext *ctxPtr = EglWrapperContext::GetWrapperContext(ctx);
798         if (CheckObject(ctxPtr)) {
799             actualCtx = ctxPtr->GetEglContext();
800         }
801     }
802 
803     EglWrapperDispatchTablePtr table = &gWrapperHook;
804     if (table->isLoad && table->egl.eglCreateImageKHR) {
805         return table->egl.eglCreateImageKHR(disp_, actualCtx, target, buffer, attribList);
806     } else {
807         WLOGE("eglCreateImageKHR is invalid.");
808     }
809 
810     return EGL_NO_IMAGE_KHR;
811 }
812 
DestroyImageKHR(EGLImageKHR img)813 EGLBoolean EglWrapperDisplay::DestroyImageKHR(EGLImageKHR img)
814 {
815     WLOGD("");
816     std::lock_guard<std::mutex> lock(refLockMutex_);
817 
818     EGLBoolean ret = EGL_FALSE;
819     EglWrapperDispatchTablePtr table = &gWrapperHook;
820     if (table->isLoad && table->egl.eglDestroyImageKHR) {
821         ret = table->egl.eglDestroyImageKHR(disp_, img);
822     } else {
823         WLOGE("eglDestroyImageKHR is invalid.");
824     }
825 
826     return ret;
827 }
828 
CreateStreamProducerSurfaceKHR(EGLConfig config,EGLStreamKHR stream,const EGLint * attribList)829 EGLSurface EglWrapperDisplay::CreateStreamProducerSurfaceKHR(EGLConfig config,
830     EGLStreamKHR stream, const EGLint *attribList)
831 {
832     WLOGD("");
833     std::lock_guard<std::mutex> lock(refLockMutex_);
834 
835     EglWrapperDispatchTablePtr table = &gWrapperHook;
836     if (table->isLoad && table->egl.eglCreateStreamProducerSurfaceKHR) {
837         EGLSurface surf = table->egl.eglCreateStreamProducerSurfaceKHR(
838             disp_, config, stream, attribList);
839         if (surf != EGL_NO_SURFACE) {
840             return new EglWrapperSurface(this, surf);
841         } else {
842             WLOGE("egl.eglCreateStreamProducerSurfaceKHR error.");
843         }
844     } else {
845         WLOGE("eglCreateStreamProducerSurfaceKHR is invalid.");
846     }
847 
848     return EGL_NO_SURFACE;
849 }
850 
SwapBuffersWithDamageKHR(EGLSurface draw,EGLint * rects,EGLint nRects)851 EGLBoolean EglWrapperDisplay::SwapBuffersWithDamageKHR(EGLSurface draw, EGLint *rects, EGLint nRects)
852 {
853     WLOGD("");
854     std::lock_guard<std::mutex> lock(refLockMutex_);
855 
856     EglWrapperSurface *surfacePtr = EglWrapperSurface::GetWrapperSurface(draw);
857     if (!CheckObject(surfacePtr)) {
858         WLOGE("EGLSurface is invalid.");
859         ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
860         return EGL_FALSE;
861     }
862 
863     if (nRects < 0 || (nRects > 0 && rects == NULL)) {
864         WLOGE("Paramter error.");
865         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
866         return EGL_FALSE;
867     }
868 
869     EGLBoolean ret = EGL_FALSE;
870     EglWrapperDispatchTablePtr table = &gWrapperHook;
871     if (table->isLoad && table->egl.eglSwapBuffersWithDamageKHR) {
872         ret = table->egl.eglSwapBuffersWithDamageKHR(
873             disp_, surfacePtr->GetEglSurface(), rects, nRects);
874     } else {
875         WLOGE("eglSwapBuffersWithDamageKHR is invalid.");
876     }
877 
878     return ret;
879 }
880 
SetDamageRegionKHR(EGLSurface surf,EGLint * rects,EGLint nRects)881 EGLBoolean EglWrapperDisplay::SetDamageRegionKHR(EGLSurface surf, EGLint *rects, EGLint nRects)
882 {
883     WLOGD("");
884     std::lock_guard<std::mutex> lock(refLockMutex_);
885 
886     EglWrapperSurface *surfPtr = EglWrapperSurface::GetWrapperSurface(surf);
887     if (!CheckObject(surfPtr)) {
888         WLOGE("EGLSurface is invalid.");
889         ThreadPrivateDataCtl::SetError(EGL_BAD_SURFACE);
890         return EGL_FALSE;
891     }
892 
893     if (nRects < 0 || (nRects > 0 && rects == nullptr)) {
894         WLOGE("Paramter error.");
895         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
896         return EGL_FALSE;
897     }
898 
899     EGLBoolean ret = EGL_FALSE;
900     EglWrapperDispatchTablePtr table = &gWrapperHook;
901     if (table->isLoad && table->egl.eglSetDamageRegionKHR) {
902         ret = table->egl.eglSetDamageRegionKHR(
903             disp_, surfPtr->GetEglSurface(), rects, nRects);
904     } else {
905         WLOGE("eglSetDamageRegionKHR is invalid.");
906     }
907 
908     return ret;
909 }
910 } // namespace OHOS
911