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 #include "egl_wrapper_entry.h"
16 
17 #include <map>
18 #include <string>
19 
20 #include <EGL/egl.h>
21 #include <EGL/eglext.h>
22 #include <dlfcn.h>
23 
24 #include "egl_defs.h"
25 #include "egl_wrapper_context.h"
26 #include "egl_wrapper_display.h"
27 #include "egl_wrapper_loader.h"
28 #include "thread_private_data_ctl.h"
29 #include "window.h"
30 #include "wrapper_log.h"
31 #include "egl_blob_cache.h"
32 #if USE_APS_IGAMESERVICE_FUNC
33 #include "egl_slice_report.h"
34 #include "aps_game_fps_controller.h"
35 #endif
36 
37 using namespace OHOS;
38 namespace OHOS {
ClearError()39 static inline void ClearError()
40 {
41     ThreadPrivateDataCtl::ClearError();
42 }
43 
ValidateDisplay(EGLDisplay dpy)44 static EglWrapperDisplay *ValidateDisplay(EGLDisplay dpy)
45 {
46     EglWrapperDisplay *display = EglWrapperDisplay::GetWrapperDisplay(dpy);
47     if (!display) {
48         WLOGE("EGLDislay is invalid.");
49         ThreadPrivateDataCtl::SetError(EGL_BAD_DISPLAY);
50         return nullptr;
51     }
52 
53     if (!display->IsReady()) {
54         WLOGE("display is not ready.");
55         ThreadPrivateDataCtl::SetError(EGL_NOT_INITIALIZED);
56         return nullptr;
57     }
58     return display;
59 }
60 
EglChooseConfigImpl(EGLDisplay dpy,const EGLint * attribList,EGLConfig * configs,EGLint configSize,EGLint * numConfig)61 EGLBoolean EglChooseConfigImpl(EGLDisplay dpy, const EGLint *attribList,
62     EGLConfig *configs, EGLint configSize, EGLint *numConfig)
63 {
64     ClearError();
65     WLOGD("");
66     EglWrapperDisplay *display = ValidateDisplay(dpy);
67     if (!display) {
68         return EGL_FALSE;
69     }
70 
71     if (numConfig == nullptr) {
72         WLOGE("EGLint *numConfig is nullptr.");
73         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
74         return EGL_FALSE;
75     }
76 
77     EGLBoolean ret = EGL_FALSE;
78     *numConfig = 0;
79 
80     EglWrapperDispatchTablePtr table = &gWrapperHook;
81     if (table->isLoad && table->egl.eglChooseConfig) {
82         ret = table->egl.eglChooseConfig(display->GetEglDisplay(),
83             attribList, configs, configSize, numConfig);
84     } else {
85         WLOGE("eglChooseConfig is invalid.");
86     }
87 
88     return ret;
89 }
90 
EglCopyBuffersImpl(EGLDisplay dpy,EGLSurface surf,NativePixmapType target)91 EGLBoolean EglCopyBuffersImpl(EGLDisplay dpy, EGLSurface surf, NativePixmapType target)
92 {
93     ClearError();
94     WLOGD("");
95     EglWrapperDisplay *display = ValidateDisplay(dpy);
96     if (!display) {
97         return EGL_FALSE;
98     }
99 
100     return display->CopyBuffers(surf, target);
101 }
102 
EglCreateContextImpl(EGLDisplay dpy,EGLConfig config,EGLContext shareList,const EGLint * attribList)103 EGLContext EglCreateContextImpl(EGLDisplay dpy, EGLConfig config,
104     EGLContext shareList, const EGLint *attribList)
105 {
106     ClearError();
107     WLOGD("");
108     EglWrapperDisplay *display = ValidateDisplay(dpy);
109     if (!display) {
110         return EGL_NO_CONTEXT;
111     }
112 
113     return display->CreateEglContext(config, shareList, attribList);
114 }
115 
EglCreatePbufferSurfaceImpl(EGLDisplay dpy,EGLConfig config,const EGLint * attribList)116 EGLSurface EglCreatePbufferSurfaceImpl(EGLDisplay dpy, EGLConfig config,
117     const EGLint* attribList)
118 {
119     ClearError();
120     WLOGD("");
121     EglWrapperDisplay *display = ValidateDisplay(dpy);
122     if (!display) {
123         return EGL_NO_CONTEXT;
124     }
125 
126     return display->CreatePbufferSurface(config, attribList);
127 }
128 
EglCreatePixmapSurfaceImpl(EGLDisplay dpy,EGLConfig config,EGLNativePixmapType pixmap,const EGLint * attribList)129 EGLSurface EglCreatePixmapSurfaceImpl(EGLDisplay dpy, EGLConfig config,
130     EGLNativePixmapType pixmap, const EGLint* attribList)
131 {
132     ClearError();
133     WLOGD("");
134     EglWrapperDisplay *display = ValidateDisplay(dpy);
135     if (!display) {
136         return EGL_NO_CONTEXT;
137     }
138 
139     return display->CreatePixmapSurface(config, pixmap, attribList);
140 }
141 
EglCreateWindowSurfaceImpl(EGLDisplay dpy,EGLConfig config,NativeWindowType window,const EGLint * attribList)142 EGLSurface EglCreateWindowSurfaceImpl(EGLDisplay dpy,
143     EGLConfig config, NativeWindowType window, const EGLint* attribList)
144 {
145     ClearError();
146     WLOGD("");
147     EglWrapperDisplay *display = ValidateDisplay(dpy);
148     if (!display) {
149         return EGL_NO_SURFACE;
150     }
151 
152     return display->CreateEglSurface(config, window, attribList);
153 }
154 
EglDestroyContextImpl(EGLDisplay dpy,EGLContext ctx)155 EGLBoolean EglDestroyContextImpl(EGLDisplay dpy, EGLContext ctx)
156 {
157     ClearError();
158     WLOGD("");
159     EglWrapperDisplay *display = ValidateDisplay(dpy);
160     if (!display) {
161         return EGL_FALSE;
162     }
163 
164     return display->DestroyEglContext(ctx);
165 }
166 
EglDestroySurfaceImpl(EGLDisplay dpy,EGLSurface surf)167 EGLBoolean EglDestroySurfaceImpl(EGLDisplay dpy, EGLSurface surf)
168 {
169     ClearError();
170     WLOGD("");
171     EglWrapperDisplay *display = ValidateDisplay(dpy);
172     if (!display) {
173         return EGL_FALSE;
174     }
175 
176     return display->DestroyEglSurface(surf);
177 }
178 
EglGetConfigAttribImpl(EGLDisplay dpy,EGLConfig config,EGLint attribute,EGLint * value)179 EGLBoolean EglGetConfigAttribImpl(EGLDisplay dpy, EGLConfig config,
180     EGLint attribute, EGLint *value)
181 {
182     ClearError();
183     WLOGD("");
184     EglWrapperDisplay *display = ValidateDisplay(dpy);
185     if (!display) {
186         return EGL_FALSE;
187     }
188 
189     EGLBoolean ret = EGL_FALSE;
190     EglWrapperDispatchTablePtr table = &gWrapperHook;
191     if (table->isLoad && table->egl.eglGetConfigAttrib) {
192         ret = table->egl.eglGetConfigAttrib(display->GetEglDisplay(), config, attribute, value);
193     } else {
194         WLOGE("eglGetConfigAttrib is not found.");
195     }
196     return ret;
197 }
198 
EglGetConfigsImpl(EGLDisplay dpy,EGLConfig * configs,EGLint configSize,EGLint * numConfig)199 EGLBoolean EglGetConfigsImpl(EGLDisplay dpy, EGLConfig *configs,
200     EGLint configSize, EGLint *numConfig)
201 {
202     ClearError();
203     WLOGD("");
204     EglWrapperDisplay *display = ValidateDisplay(dpy);
205     if (!display) {
206         return EGL_FALSE;
207     }
208 
209     if (numConfig == nullptr) {
210         WLOGE("EGLint *numConfig is nullptr.");
211         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
212     }
213 
214     EGLBoolean ret = EGL_FALSE;
215     EglWrapperDispatchTablePtr table = &gWrapperHook;
216     if (table->isLoad && table->egl.eglGetConfigs) {
217         ret = table->egl.eglGetConfigs(display->GetEglDisplay(), configs, configSize, numConfig);
218     } else {
219         WLOGE("eglGetConfigs is not found.");
220     }
221 
222     return ret;
223 }
224 
EglGetCurrentDisplayImpl(void)225 EGLDisplay EglGetCurrentDisplayImpl(void)
226 {
227     ClearError();
228     EGLContext ctx = ThreadPrivateDataCtl::GetContext();
229     if (ctx) {
230         EglWrapperContext *ctxPtr = EglWrapperContext::GetWrapperContext(ctx);
231         if (ctxPtr == nullptr) {
232             WLOGE("current is bad context.");
233             ThreadPrivateDataCtl::SetError(EGL_BAD_CONTEXT);
234             return EGL_NO_DISPLAY;
235         }
236         return ctxPtr->GetDisplay();
237     }
238     return EGL_NO_DISPLAY;
239 }
240 
EglGetCurrentSurfaceImpl(EGLint type)241 EGLSurface EglGetCurrentSurfaceImpl(EGLint type)
242 {
243     ClearError();
244     EGLContext ctx = ThreadPrivateDataCtl::GetContext();
245     if (ctx) {
246         EglWrapperContext *ctxPtr = EglWrapperContext::GetWrapperContext(ctx);
247         if (ctxPtr == nullptr) {
248             WLOGE("current is bad context.");
249             ThreadPrivateDataCtl::SetError(EGL_BAD_CONTEXT);
250             return EGL_NO_SURFACE;
251         }
252         return ctxPtr->GetCurrentSurface(type);
253     }
254     return EGL_NO_SURFACE;
255 }
256 
EglGetPlatformDisplayInternal(EGLenum platform,EGLNativeDisplayType type,const EGLAttrib * attribList)257 static EGLDisplay EglGetPlatformDisplayInternal(EGLenum platform,
258     EGLNativeDisplayType type, const EGLAttrib *attribList)
259 {
260     WLOGD("");
261     if (type != EGL_DEFAULT_DISPLAY) {
262         WLOGE("EGLNativeDisplayType is invalid.");
263         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
264         return EGL_NO_DISPLAY;
265     }
266 
267     if (platform != EGL_PLATFORM_OHOS_KHR) {
268         WLOGE("EGLenum platform is not EGL_PLATFORM_OHOS_KHR.");
269         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
270         return EGL_NO_DISPLAY;
271     }
272 
273     return EglWrapperDisplay::GetEglDisplay(platform, type, attribList);
274 }
275 
EglGetDisplayImpl(EGLNativeDisplayType type)276 EGLDisplay EglGetDisplayImpl(EGLNativeDisplayType type)
277 {
278     ClearError();
279     WLOGD("");
280     return EglGetPlatformDisplayInternal(EGL_PLATFORM_OHOS_KHR, type, nullptr);
281 }
282 
EglGetErrorImpl(void)283 EGLint EglGetErrorImpl(void)
284 {
285     EGLint ret = EGL_SUCCESS;
286     EglWrapperDispatchTablePtr table = &gWrapperHook;
287     if (table->isLoad && table->egl.eglGetError) {
288         ret = table->egl.eglGetError();
289     } else {
290         WLOGE("eglGetError is not found.");
291     }
292 
293     if (ret == EGL_SUCCESS) {
294         ret = ThreadPrivateDataCtl::GetError();
295     }
296 
297     return ret;
298 }
299 
FindBuiltinWrapper(const char * procname)300 static __eglMustCastToProperFunctionPointerType FindBuiltinWrapper(const char* procname)
301 {
302 #if (defined(__aarch64__) || defined(__x86_64__))
303     static void* dlglv3Handle = dlopen("/system/lib64/libGLESv3.so", RTLD_NOW | RTLD_LOCAL);
304 #else
305     static void* dlglv3Handle = dlopen("/system/lib/platformsdk/libGLESv3.so", RTLD_NOW | RTLD_LOCAL);
306 #endif
307 
308     void* proc = dlsym(dlglv3Handle, procname);
309     if (proc) {
310         return (__eglMustCastToProperFunctionPointerType)proc;
311     }
312     return nullptr;
313 }
314 
EglGetProcAddressImpl(const char * procname)315 __eglMustCastToProperFunctionPointerType EglGetProcAddressImpl(const char *procname)
316 {
317     ClearError();
318     WLOGD("");
319     if (gExtensionMap.find(procname) != gExtensionMap.end()) {
320         return gExtensionMap.at(procname);
321     }
322 
323     __eglMustCastToProperFunctionPointerType addr = FindBuiltinWrapper(procname);
324     if (addr) {
325         return __eglMustCastToProperFunctionPointerType(addr);
326     }
327 
328     EglWrapperLoader& loader(EglWrapperLoader::GetInstance());
329     void *func = loader.GetProcAddrFromDriver(procname);
330 
331     if (!func) {
332         EglWrapperDispatchTablePtr table = &gWrapperHook;
333         if (table->isLoad && table->egl.eglGetProcAddress) {
334             func = reinterpret_cast<void *>(table->egl.eglGetProcAddress(procname));
335         }
336     }
337 
338     if (func) {
339         return __eglMustCastToProperFunctionPointerType(func);
340     }
341 
342     WLOGW("FindEglExtApi did not find an entry for %{public}s", procname);
343     return nullptr;
344 }
345 
EglInitializeImpl(EGLDisplay dpy,EGLint * major,EGLint * minor)346 EGLBoolean EglInitializeImpl(EGLDisplay dpy, EGLint *major, EGLint *minor)
347 {
348     WLOGD("");
349 #if USE_APS_IGAMESERVICE_FUNC
350     OHOS::GameService::EglSliceReport::GetInstance().InitSliceReport();
351 #endif
352     EglWrapperDisplay *display = EglWrapperDisplay::GetWrapperDisplay(dpy);
353     if (!display) {
354         WLOGE("EGLDislay is invalid.");
355         ThreadPrivateDataCtl::SetError(EGL_BAD_DISPLAY);
356         return EGL_FALSE;
357     }
358 
359     return display->Init(major, minor);
360 }
361 
EglMakeCurrentImpl(EGLDisplay dpy,EGLSurface draw,EGLSurface read,EGLContext ctx)362 EGLBoolean EglMakeCurrentImpl(EGLDisplay dpy, EGLSurface draw,
363     EGLSurface read, EGLContext ctx)
364 {
365     ClearError();
366     WLOGD("");
367     EglWrapperDisplay *display = ValidateDisplay(dpy);
368     if (!display) {
369         return EGL_FALSE;
370     }
371 
372     return display->MakeCurrent(draw, read, ctx);
373 }
374 
EglQueryContextImpl(EGLDisplay dpy,EGLContext ctx,EGLint attribute,EGLint * value)375 EGLBoolean EglQueryContextImpl(EGLDisplay dpy, EGLContext ctx,
376     EGLint attribute, EGLint *value)
377 {
378     ClearError();
379     WLOGD("");
380     EglWrapperDisplay *display = ValidateDisplay(dpy);
381     if (!display) {
382         return EGL_FALSE;
383     }
384 
385     if (value == nullptr) {
386         WLOGE("EGLint *value is nullptr.");
387         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
388         return EGL_FALSE;
389     }
390 
391     return display->QueryContext(ctx, attribute, value);
392 }
393 
EglQueryStringImpl(EGLDisplay dpy,EGLint name)394 const char *EglQueryStringImpl(EGLDisplay dpy, EGLint name)
395 {
396     ClearError();
397 #ifdef IS_EMULATOR
398     EGLDisplay actualDpy = EGL_NO_DISPLAY;
399     if (dpy != EGL_NO_DISPLAY) {
400         EglWrapperDisplay *display = ValidateDisplay(dpy);
401         if (!display) {
402             return nullptr;
403         }
404         actualDpy = display->GetEglDisplay();
405     }
406     EglWrapperDispatchTablePtr table = &gWrapperHook;
407     if (table->isLoad && table->egl.eglQueryString) {
408         return table->egl.eglQueryString(actualDpy, name);
409     } else {
410         WLOGE("eglQueryString is not found.");
411     }
412     return nullptr;
413 #else
414     EglWrapperDisplay *display = ValidateDisplay(dpy);
415     if (!display) {
416         return nullptr;
417     }
418 
419     switch (name) {
420         case EGL_VENDOR:
421             return display->GetVendorValue();
422         case EGL_VERSION:
423             return display->GetVersionValue();
424         case EGL_EXTENSIONS:
425             return display->GetExtensionValue();
426         case EGL_CLIENT_APIS:
427             return display->GetClientApiValue();
428         default:
429             return nullptr;
430     }
431 #endif // IS_EMULATOR
432 }
433 
EglQuerySurfaceImpl(EGLDisplay dpy,EGLSurface surf,EGLint attribute,EGLint * value)434 EGLBoolean EglQuerySurfaceImpl(EGLDisplay dpy, EGLSurface surf,
435     EGLint attribute, EGLint* value)
436 {
437     ClearError();
438     WLOGD("");
439     EglWrapperDisplay *display = ValidateDisplay(dpy);
440     if (!display) {
441         return EGL_FALSE;
442     }
443 
444     if (value == nullptr) {
445         WLOGE("EGLint *value is nullptr.");
446         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
447         return EGL_FALSE;
448     }
449 
450     return display->QuerySurface(surf, attribute, value);
451 }
452 
EglSwapBuffersImpl(EGLDisplay dpy,EGLSurface surf)453 EGLBoolean EglSwapBuffersImpl(EGLDisplay dpy, EGLSurface surf)
454 {
455     ClearError();
456     WLOGD("");
457 #if USE_APS_IGAMESERVICE_FUNC
458     OHOS::GameService::EglSliceReport::GetInstance().AddGraphicCount();
459     OHOS::Rosen::ApsGameFpsController::GetInstance().PowerCtrllofEglswapbuffer();
460 #endif
461     EglWrapperDisplay *display = ValidateDisplay(dpy);
462     if (!display) {
463         return EGL_FALSE;
464     }
465 
466     return display->SwapBuffers(surf);
467 }
468 
EglTerminateImpl(EGLDisplay dpy)469 EGLBoolean EglTerminateImpl(EGLDisplay dpy)
470 {
471     ClearError();
472     WLOGD("");
473     EglWrapperDisplay *display = ValidateDisplay(dpy);
474     if (!display) {
475         return EGL_FALSE;
476     }
477 
478     return display->Terminate();
479 }
480 
EglWaitGLImpl(void)481 EGLBoolean EglWaitGLImpl(void)
482 {
483     ClearError();
484     WLOGD("");
485     EGLBoolean ret = EGL_FALSE;
486     EglWrapperDispatchTablePtr table = &gWrapperHook;
487     if (table->isLoad && table->egl.eglWaitGL) {
488         ret = table->egl.eglWaitGL();
489     } else {
490         WLOGE("eglWaitGL is not found.");
491     }
492     return ret;
493 }
494 
EglWaitNativeImpl(EGLint engine)495 EGLBoolean EglWaitNativeImpl(EGLint engine)
496 {
497     ClearError();
498     WLOGD("");
499     EGLBoolean ret = EGL_FALSE;
500     EglWrapperDispatchTablePtr table = &gWrapperHook;
501     if (table->isLoad && table->egl.eglWaitNative) {
502         ret = table->egl.eglWaitNative(engine);
503     } else {
504         WLOGE("eglWaitNative is not found.");
505     }
506     return ret;
507 }
508 
EglBindTexImageImpl(EGLDisplay dpy,EGLSurface surf,EGLint buffer)509 EGLBoolean EglBindTexImageImpl(EGLDisplay dpy, EGLSurface surf, EGLint buffer)
510 {
511     ClearError();
512     WLOGD("");
513     EglWrapperDisplay *display = ValidateDisplay(dpy);
514     if (!display) {
515         return EGL_FALSE;
516     }
517 
518     return display->BindTexImage(surf, buffer);
519 }
520 
EglReleaseTexImageImpl(EGLDisplay dpy,EGLSurface surf,EGLint buffer)521 EGLBoolean EglReleaseTexImageImpl(EGLDisplay dpy, EGLSurface surf, EGLint buffer)
522 {
523     ClearError();
524     WLOGD("");
525     EglWrapperDisplay *display = ValidateDisplay(dpy);
526     if (!display) {
527         return EGL_FALSE;
528     }
529 
530     return display->ReleaseTexImage(surf, buffer);
531 }
532 
EglSurfaceAttribImpl(EGLDisplay dpy,EGLSurface surf,EGLint attribute,EGLint value)533 EGLBoolean EglSurfaceAttribImpl(EGLDisplay dpy, EGLSurface surf,
534     EGLint attribute, EGLint value)
535 {
536     ClearError();
537     WLOGD("");
538     EglWrapperDisplay *display = ValidateDisplay(dpy);
539     if (!display) {
540         return EGL_FALSE;
541     }
542 
543     return display->SurfaceAttrib(surf, attribute, value);
544 }
545 
EglSwapIntervalImpl(EGLDisplay dpy,EGLint interval)546 EGLBoolean EglSwapIntervalImpl(EGLDisplay dpy, EGLint interval)
547 {
548     ClearError();
549     WLOGD("");
550     EglWrapperDisplay *display = ValidateDisplay(dpy);
551     if (!display) {
552         return EGL_FALSE;
553     }
554 
555     EGLBoolean ret = EGL_FALSE;
556     EglWrapperDispatchTablePtr table = &gWrapperHook;
557     if (table->isLoad && table->egl.eglSwapInterval) {
558         ret = table->egl.eglSwapInterval(display->GetEglDisplay(), interval);
559     } else {
560         WLOGE("eglQueryString is not found.");
561     }
562 
563     return ret;
564 }
565 
EglBindAPIImpl(EGLenum api)566 EGLBoolean EglBindAPIImpl(EGLenum api)
567 {
568     ClearError();
569     WLOGD("");
570     EGLBoolean ret = EGL_FALSE;
571     EglWrapperDispatchTablePtr table = &gWrapperHook;
572     if (table->isLoad && table->egl.eglBindAPI) {
573         ret = table->egl.eglBindAPI(api);
574     } else {
575         WLOGE("eglBindAPI is not found.");
576     }
577     return ret;
578 }
579 
EglQueryAPIImpl(void)580 EGLBoolean EglQueryAPIImpl(void)
581 {
582     ClearError();
583     WLOGD("");
584     EglWrapperDispatchTablePtr table = &gWrapperHook;
585     if (table->isLoad && table->egl.eglQueryAPI) {
586         return table->egl.eglQueryAPI();
587     } else {
588         WLOGE("eglQueryAPI is not found.");
589     }
590 
591     return EGL_OPENGL_ES_API;
592 }
593 
EglCreatePbufferFromClientBufferImpl(EGLDisplay dpy,EGLenum buftype,EGLClientBuffer buffer,EGLConfig config,const EGLint * attribList)594 EGLSurface EglCreatePbufferFromClientBufferImpl(EGLDisplay dpy,
595     EGLenum buftype, EGLClientBuffer buffer,
596     EGLConfig config, const EGLint *attribList)
597 {
598     ClearError();
599     WLOGD("");
600     EglWrapperDisplay *display = ValidateDisplay(dpy);
601     if (!display) {
602         return EGL_NO_SURFACE;
603     }
604 
605     return display->CreatePbufferFromClientBuffer(buftype, buffer, config, attribList);
606 }
607 
EglReleaseThreadImpl(void)608 EGLBoolean EglReleaseThreadImpl(void)
609 {
610     ClearError();
611     WLOGD("");
612     EglWrapperDispatchTablePtr table = &gWrapperHook;
613     if (table->isLoad && table->egl.eglReleaseThread) {
614         table->egl.eglReleaseThread();
615     } else {
616         WLOGE("eglReleaseThread is not found.");
617     }
618 
619     ThreadPrivateDataCtl::ClearPrivateData();
620     return EGL_TRUE;
621 }
622 
EglWaitClientImpl(void)623 EGLBoolean EglWaitClientImpl(void)
624 {
625     ClearError();
626     WLOGD("");
627     EGLBoolean ret = EGL_FALSE;
628     EglWrapperDispatchTablePtr table = &gWrapperHook;
629     if (table->isLoad && table->egl.eglWaitClient) {
630         ret = table->egl.eglWaitClient();
631     } else {
632         WLOGE("eglWaitClient is not found.");
633     }
634     return ret;
635 }
636 
EglGetCurrentContextImpl(void)637 EGLContext EglGetCurrentContextImpl(void)
638 {
639     ClearError();
640     EGLContext ctx = ThreadPrivateDataCtl::GetContext();
641     return ctx;
642 }
643 
EglCreateSyncImpl(EGLDisplay dpy,EGLenum type,const EGLAttrib * attribList)644 EGLSync EglCreateSyncImpl(EGLDisplay dpy, EGLenum type, const EGLAttrib *attribList)
645 {
646     ClearError();
647     WLOGD("");
648     EglWrapperDisplay *display = ValidateDisplay(dpy);
649     if (!display) {
650         return EGL_NO_SYNC;
651     }
652 
653     EGLSyncKHR ret = EGL_NO_SYNC;
654     EglWrapperDispatchTablePtr table = &gWrapperHook;
655     if (table->isLoad && table->egl.eglCreateSync) {
656         ret = table->egl.eglCreateSync(display->GetEglDisplay(), type, attribList);
657     } else {
658         WLOGE("eglCreateSync is not found.");
659     }
660 
661     return ret;
662 }
663 
EglDestroySyncImpl(EGLDisplay dpy,EGLSync sync)664 EGLBoolean EglDestroySyncImpl(EGLDisplay dpy, EGLSync sync)
665 {
666     ClearError();
667     WLOGD("");
668     EglWrapperDisplay *display = ValidateDisplay(dpy);
669     if (!display) {
670         return EGL_FALSE;
671     }
672 
673     EGLBoolean ret = EGL_FALSE;
674     EglWrapperDispatchTablePtr table = &gWrapperHook;
675     if (table->isLoad && table->egl.eglDestroySync) {
676         ret = table->egl.eglDestroySync(display->GetEglDisplay(), sync);
677     } else {
678         WLOGE("eglDestroySync is not found.");
679     }
680 
681     return ret;
682 }
683 
EglClientWaitSyncImpl(EGLDisplay dpy,EGLSync sync,EGLint flags,EGLTimeKHR timeout)684 EGLint EglClientWaitSyncImpl(EGLDisplay dpy, EGLSync sync,
685     EGLint flags, EGLTimeKHR timeout)
686 {
687     ClearError();
688     WLOGD("");
689     EglWrapperDisplay *display = ValidateDisplay(dpy);
690     if (!display) {
691         return EGL_FALSE;
692     }
693 
694     EGLBoolean ret = EGL_FALSE;
695     EglWrapperDispatchTablePtr table = &gWrapperHook;
696     if (table->isLoad && table->egl.eglClientWaitSync) {
697         ret = table->egl.eglClientWaitSync(display->GetEglDisplay(),
698             sync, flags, timeout);
699     } else {
700         WLOGE("eglClientWaitSync is not found.");
701     }
702 
703     return ret;
704 }
705 
EglGetSyncAttribImpl(EGLDisplay dpy,EGLSync sync,EGLint attribute,EGLAttrib * value)706 EGLBoolean EglGetSyncAttribImpl(EGLDisplay dpy, EGLSync sync,
707     EGLint attribute, EGLAttrib *value)
708 {
709     ClearError();
710     WLOGD("");
711     EglWrapperDisplay *display = ValidateDisplay(dpy);
712     if (!display) {
713         return EGL_FALSE;
714     }
715 
716     if (value == nullptr) {
717         WLOGE("EGLAttrib *value is nullptr.");
718         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
719         return EGL_FALSE;
720     }
721 
722     EGLBoolean ret = EGL_FALSE;
723     EglWrapperDispatchTablePtr table = &gWrapperHook;
724     if (table->isLoad && table->egl.eglGetSyncAttrib) {
725         ret = table->egl.eglGetSyncAttrib(display->GetEglDisplay(),
726             sync, attribute, value);
727     } else {
728         WLOGE("eglGetSyncAttrib is not found.");
729     }
730 
731     return ret;
732 }
733 
EglCreateImageImpl(EGLDisplay dpy,EGLContext ctx,EGLenum target,EGLClientBuffer buffer,const EGLAttrib * attribList)734 EGLImage EglCreateImageImpl(EGLDisplay dpy, EGLContext ctx,
735     EGLenum target, EGLClientBuffer buffer, const EGLAttrib *attribList)
736 {
737     ClearError();
738     WLOGD("");
739     EglWrapperDisplay *display = ValidateDisplay(dpy);
740     if (!display) {
741         return EGL_NO_IMAGE;
742     }
743 
744     return display->CreateImage(ctx, target, buffer, attribList);
745 }
746 
EglDestroyImageImpl(EGLDisplay dpy,EGLImage img)747 EGLBoolean EglDestroyImageImpl(EGLDisplay dpy, EGLImage img)
748 {
749     ClearError();
750     WLOGD("");
751     EglWrapperDisplay *display = ValidateDisplay(dpy);
752     if (!display) {
753         return EGL_FALSE;
754     }
755 
756     return display->DestroyImage(img);
757 }
758 
EglGetPlatformDisplayImpl(EGLenum platform,void * nativeDisplay,const EGLAttrib * attribList)759 EGLDisplay EglGetPlatformDisplayImpl(EGLenum platform,
760     void *nativeDisplay, const EGLAttrib *attribList)
761 {
762     ClearError();
763     return EglGetPlatformDisplayInternal(platform,
764         static_cast<EGLNativeDisplayType>(nativeDisplay), attribList);
765 }
766 
EglCreatePlatformWindowSurfaceImpl(EGLDisplay dpy,EGLConfig config,void * nativeWindow,const EGLAttrib * attribList)767 EGLSurface EglCreatePlatformWindowSurfaceImpl(EGLDisplay dpy,
768     EGLConfig config, void *nativeWindow, const EGLAttrib *attribList)
769 {
770     ClearError();
771     WLOGD("");
772     EglWrapperDisplay *display = ValidateDisplay(dpy);
773     if (!display) {
774         return EGL_NO_SURFACE;
775     }
776 
777     return display->CreatePlatformWindowSurface(config, nativeWindow, attribList);
778 }
779 
780 
EglCreatePlatformPixmapSurfaceImpl(EGLDisplay dpy,EGLConfig config,void * nativePixmap,const EGLAttrib * attribList)781 EGLSurface EglCreatePlatformPixmapSurfaceImpl(EGLDisplay dpy,
782     EGLConfig config, void *nativePixmap, const EGLAttrib *attribList)
783 {
784     ClearError();
785     WLOGD("");
786     EglWrapperDisplay *display = ValidateDisplay(dpy);
787     if (!display) {
788         return EGL_NO_SURFACE;
789     }
790 
791     return display->CreatePlatformPixmapSurface(config, nativePixmap, attribList);
792 }
793 
EglWaitSyncImpl(EGLDisplay dpy,EGLSync sync,EGLint flags)794 EGLBoolean EglWaitSyncImpl(EGLDisplay dpy, EGLSync sync, EGLint flags)
795 {
796     ClearError();
797     WLOGD("");
798     EglWrapperDisplay *display = ValidateDisplay(dpy);
799     if (!display) {
800         return EGL_FALSE;
801     }
802 
803     EGLBoolean ret = EGL_FALSE;
804     EglWrapperDispatchTablePtr table = &gWrapperHook;
805     if (table->isLoad && table->egl.eglWaitSync) {
806         ret = table->egl.eglWaitSync(display->GetEglDisplay(), sync, flags);
807     } else {
808         WLOGE("eglWaitSync is not found.");
809     }
810 
811     return ret;
812 }
813 
EglLockSurfaceKHRImpl(EGLDisplay dpy,EGLSurface surf,const EGLint * attribList)814 EGLBoolean EglLockSurfaceKHRImpl(EGLDisplay dpy, EGLSurface surf,
815     const EGLint *attribList)
816 {
817     ClearError();
818     WLOGD("");
819     EglWrapperDisplay *display = ValidateDisplay(dpy);
820     if (!display) {
821         return EGL_FALSE;
822     }
823 
824     return display->LockSurfaceKHR(surf, attribList);
825 }
826 
EglUnlockSurfaceKHRImpl(EGLDisplay dpy,EGLSurface surf)827 EGLBoolean EglUnlockSurfaceKHRImpl(EGLDisplay dpy, EGLSurface surf)
828 {
829     ClearError();
830     WLOGD("");
831     EglWrapperDisplay *display = ValidateDisplay(dpy);
832     if (!display) {
833         return EGL_FALSE;
834     }
835 
836     return display->UnlockSurfaceKHR(surf);
837 }
838 
EglCreateImageKHRImpl(EGLDisplay dpy,EGLContext ctx,EGLenum target,EGLClientBuffer buffer,const EGLint * attribList)839 EGLImageKHR EglCreateImageKHRImpl(EGLDisplay dpy, EGLContext ctx,
840     EGLenum target, EGLClientBuffer buffer, const EGLint *attribList)
841 {
842     ClearError();
843     WLOGD("");
844     EglWrapperDisplay *display = ValidateDisplay(dpy);
845     if (!display) {
846         return EGL_NO_IMAGE_KHR;
847     }
848 
849     return display->CreateImageKHR(ctx, target, buffer, attribList);
850 }
851 
EglDestroyImageKHRImpl(EGLDisplay dpy,EGLImageKHR img)852 EGLBoolean EglDestroyImageKHRImpl(EGLDisplay dpy, EGLImageKHR img)
853 {
854     ClearError();
855     WLOGD("");
856     EglWrapperDisplay *display = ValidateDisplay(dpy);
857     if (!display) {
858         return EGL_FALSE;
859     }
860 
861     return display->DestroyImageKHR(img);
862 }
863 
EglCreateSyncKHRImpl(EGLDisplay dpy,EGLenum type,const EGLint * attribList)864 EGLSyncKHR EglCreateSyncKHRImpl(EGLDisplay dpy, EGLenum type, const EGLint* attribList)
865 {
866     ClearError();
867     WLOGD("");
868     EglWrapperDisplay *display = ValidateDisplay(dpy);
869     if (!display) {
870         return EGL_NO_SYNC_KHR;
871     }
872 
873     EGLSyncKHR ret = EGL_NO_SYNC_KHR;
874     EglWrapperDispatchTablePtr table = &gWrapperHook;
875     if (table->isLoad && table->egl.eglCreateSyncKHR) {
876         ret = table->egl.eglCreateSyncKHR(display->GetEglDisplay(), type, attribList);
877     } else {
878         WLOGE("eglCreateSyncKHR is not found.");
879     }
880 
881     return ret;
882 }
883 
EglDestroySyncKHRImpl(EGLDisplay dpy,EGLSyncKHR sync)884 EGLBoolean EglDestroySyncKHRImpl(EGLDisplay dpy, EGLSyncKHR sync)
885 {
886     ClearError();
887     WLOGD("");
888     EglWrapperDisplay *display = ValidateDisplay(dpy);
889     if (!display) {
890         return EGL_FALSE;
891     }
892 
893     EGLBoolean ret = EGL_FALSE;
894     EglWrapperDispatchTablePtr table = &gWrapperHook;
895     if (table->isLoad && table->egl.eglDestroySyncKHR) {
896         ret = table->egl.eglDestroySyncKHR(display->GetEglDisplay(), sync);
897     } else {
898         WLOGE("eglDestroySyncKHR is not found.");
899     }
900 
901     return ret;
902 }
903 
EglClientWaitSyncKHRImpl(EGLDisplay dpy,EGLSyncKHR sync,EGLint flags,EGLTimeKHR timeout)904 EGLint EglClientWaitSyncKHRImpl(EGLDisplay dpy, EGLSyncKHR sync,
905     EGLint flags, EGLTimeKHR timeout)
906 {
907     ClearError();
908     WLOGD("");
909     EglWrapperDisplay *display = ValidateDisplay(dpy);
910     if (!display) {
911         return EGL_FALSE;
912     }
913 
914     EGLBoolean ret = EGL_FALSE;
915     EglWrapperDispatchTablePtr table = &gWrapperHook;
916     if (table->isLoad && table->egl.eglClientWaitSyncKHR) {
917         ret = table->egl.eglClientWaitSyncKHR(display->GetEglDisplay(),
918             sync, flags, timeout);
919     } else {
920         WLOGE("eglClientWaitSyncKHR is not found.");
921     }
922 
923     return ret;
924 }
925 
EglGetSyncAttribKHRImpl(EGLDisplay dpy,EGLSyncKHR sync,EGLint attribute,EGLint * value)926 EGLBoolean EglGetSyncAttribKHRImpl(EGLDisplay dpy, EGLSyncKHR sync,
927     EGLint attribute, EGLint *value)
928 {
929     ClearError();
930     WLOGD("");
931     EglWrapperDisplay *display = ValidateDisplay(dpy);
932     if (!display) {
933         return EGL_FALSE;
934     }
935 
936     if (value == nullptr) {
937         WLOGE("EGLint *value is nullptr.");
938         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
939         return EGL_FALSE;
940     }
941 
942     EGLBoolean ret = EGL_FALSE;
943     EglWrapperDispatchTablePtr table = &gWrapperHook;
944     if (table->isLoad && table->egl.eglGetSyncAttribKHR) {
945         ret = table->egl.eglGetSyncAttribKHR(display->GetEglDisplay(),
946             sync, attribute, value);
947     } else {
948         WLOGE("eglGetSyncAttribKHR is not found.");
949     }
950 
951     return ret;
952 }
953 
EglSignalSyncKHRImpl(EGLDisplay dpy,EGLSyncKHR sync,EGLenum mode)954 EGLBoolean EglSignalSyncKHRImpl(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode)
955 {
956     ClearError();
957     WLOGD("");
958     EglWrapperDisplay *display = ValidateDisplay(dpy);
959     if (!display) {
960         return EGL_FALSE;
961     }
962 
963     EGLBoolean ret = EGL_FALSE;
964     EglWrapperDispatchTablePtr table = &gWrapperHook;
965     if (table->isLoad && table->egl.eglSignalSyncKHR) {
966         ret = table->egl.eglSignalSyncKHR(display->GetEglDisplay(), sync, mode);
967     } else {
968         WLOGE("eglSignalSyncKHR is not found.");
969     }
970 
971     return ret;
972 }
973 
EglCreateStreamKHRImpl(EGLDisplay dpy,const EGLint * attribList)974 EGLStreamKHR EglCreateStreamKHRImpl(EGLDisplay dpy, const EGLint *attribList)
975 {
976     ClearError();
977     WLOGD("");
978     EglWrapperDisplay *display = ValidateDisplay(dpy);
979     if (!display) {
980         return EGL_NO_STREAM_KHR;
981     }
982 
983     EGLStreamKHR ret = EGL_NO_STREAM_KHR;
984     EglWrapperDispatchTablePtr table = &gWrapperHook;
985     if (table->isLoad && table->egl.eglCreateStreamKHR) {
986         ret = table->egl.eglCreateStreamKHR(display->GetEglDisplay(), attribList);
987     } else {
988         WLOGE("eglCreateStreamKHR is not found.");
989     }
990 
991     return ret;
992 }
993 
EglDestroyStreamKHRImpl(EGLDisplay dpy,EGLStreamKHR stream)994 EGLBoolean EglDestroyStreamKHRImpl(EGLDisplay dpy, EGLStreamKHR stream)
995 {
996     ClearError();
997     WLOGD("");
998     EglWrapperDisplay *display = ValidateDisplay(dpy);
999     if (!display) {
1000         return EGL_FALSE;
1001     }
1002 
1003     EGLBoolean ret = EGL_FALSE;
1004     EglWrapperDispatchTablePtr table = &gWrapperHook;
1005     if (table->isLoad && table->egl.eglDestroyStreamKHR) {
1006         ret = table->egl.eglDestroyStreamKHR(display->GetEglDisplay(), stream);
1007     } else {
1008         WLOGE("eglDestroyStreamKHR is not found.");
1009     }
1010 
1011     return ret;
1012 }
1013 
EglStreamAttribKHRImpl(EGLDisplay dpy,EGLStreamKHR stream,EGLenum attribute,EGLint value)1014 EGLBoolean EglStreamAttribKHRImpl(EGLDisplay dpy, EGLStreamKHR stream,
1015     EGLenum attribute, EGLint value)
1016 {
1017     ClearError();
1018     WLOGD("");
1019     EglWrapperDisplay *display = ValidateDisplay(dpy);
1020     if (!display) {
1021         return EGL_FALSE;
1022     }
1023 
1024     EGLBoolean ret = EGL_FALSE;
1025     EglWrapperDispatchTablePtr table = &gWrapperHook;
1026     if (table->isLoad && table->egl.eglStreamAttribKHR) {
1027         ret = table->egl.eglStreamAttribKHR(display->GetEglDisplay(),
1028             stream, attribute, value);
1029     } else {
1030         WLOGE("eglStreamAttribKHR is not found.");
1031     }
1032 
1033     return ret;
1034 }
1035 
EglQueryStreamKHRImpl(EGLDisplay dpy,EGLStreamKHR stream,EGLenum attribute,EGLint * value)1036 EGLBoolean EglQueryStreamKHRImpl(EGLDisplay dpy, EGLStreamKHR stream,
1037     EGLenum attribute, EGLint *value)
1038 {
1039     ClearError();
1040     WLOGD("");
1041     EglWrapperDisplay *display = ValidateDisplay(dpy);
1042     if (!display) {
1043         return EGL_FALSE;
1044     }
1045 
1046     if (value == nullptr) {
1047         WLOGE("EGLint *value is nullptr.");
1048         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
1049         return EGL_FALSE;
1050     }
1051 
1052     EGLBoolean ret = EGL_FALSE;
1053     EglWrapperDispatchTablePtr table = &gWrapperHook;
1054     if (table->isLoad && table->egl.eglQueryStreamKHR) {
1055         ret = table->egl.eglQueryStreamKHR(display->GetEglDisplay(),
1056             stream, attribute, value);
1057     } else {
1058         WLOGE("eglQueryStreamKHR is not found.");
1059     }
1060 
1061     return ret;
1062 }
1063 
EglQueryStreamu64KHRImpl(EGLDisplay dpy,EGLStreamKHR stream,EGLenum attribute,EGLuint64KHR * value)1064 EGLBoolean EglQueryStreamu64KHRImpl(EGLDisplay dpy, EGLStreamKHR stream,
1065     EGLenum attribute, EGLuint64KHR *value)
1066 {
1067     ClearError();
1068     WLOGD("");
1069     EglWrapperDisplay *display = ValidateDisplay(dpy);
1070     if (!display) {
1071         return EGL_FALSE;
1072     }
1073 
1074     if (value == nullptr) {
1075         WLOGE("EGLint *value is nullptr.");
1076         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
1077         return EGL_FALSE;
1078     }
1079 
1080     EGLBoolean ret = EGL_FALSE;
1081     EglWrapperDispatchTablePtr table = &gWrapperHook;
1082     if (table->isLoad && table->egl.eglQueryStreamu64KHR) {
1083         ret = table->egl.eglQueryStreamu64KHR(display->GetEglDisplay(),
1084             stream, attribute, value);
1085     } else {
1086         WLOGE("eglQueryStreamu64KHR is not found.");
1087     }
1088 
1089     return ret;
1090 }
1091 
EglStreamConsumerGLTextureExternalKHRImpl(EGLDisplay dpy,EGLStreamKHR stream)1092 EGLBoolean EglStreamConsumerGLTextureExternalKHRImpl(EGLDisplay dpy,
1093     EGLStreamKHR stream)
1094 {
1095     ClearError();
1096     WLOGD("");
1097     EglWrapperDisplay *display = ValidateDisplay(dpy);
1098     if (!display) {
1099         return EGL_FALSE;
1100     }
1101 
1102     EGLBoolean ret = EGL_FALSE;
1103     EglWrapperDispatchTablePtr table = &gWrapperHook;
1104     if (table->isLoad && table->egl.eglStreamConsumerGLTextureExternalKHR) {
1105         ret = table->egl.eglStreamConsumerGLTextureExternalKHR(
1106             display->GetEglDisplay(), stream);
1107     } else {
1108         WLOGE("eglStreamConsumerGLTextureExternalKHR is not found.");
1109     }
1110 
1111     return ret;
1112 }
1113 
EglStreamConsumerAcquireKHRImpl(EGLDisplay dpy,EGLStreamKHR stream)1114 EGLBoolean EglStreamConsumerAcquireKHRImpl(EGLDisplay dpy, EGLStreamKHR stream)
1115 {
1116     ClearError();
1117     WLOGD("");
1118     EglWrapperDisplay *display = ValidateDisplay(dpy);
1119     if (!display) {
1120         return EGL_FALSE;
1121     }
1122 
1123     EGLBoolean ret = EGL_FALSE;
1124     EglWrapperDispatchTablePtr table = &gWrapperHook;
1125     if (table->isLoad && table->egl.eglStreamConsumerAcquireKHR) {
1126         ret = table->egl.eglStreamConsumerAcquireKHR(
1127             display->GetEglDisplay(), stream);
1128     } else {
1129         WLOGE("eglStreamConsumerAcquireKHR is not found.");
1130     }
1131 
1132     return ret;
1133 }
1134 
EglStreamConsumerReleaseKHRImpl(EGLDisplay dpy,EGLStreamKHR stream)1135 EGLBoolean EglStreamConsumerReleaseKHRImpl(EGLDisplay dpy, EGLStreamKHR stream)
1136 {
1137     ClearError();
1138     WLOGD("");
1139     EglWrapperDisplay *display = ValidateDisplay(dpy);
1140     if (!display) {
1141         return EGL_FALSE;
1142     }
1143 
1144     EGLBoolean ret = EGL_FALSE;
1145     EglWrapperDispatchTablePtr table = &gWrapperHook;
1146     if (table->isLoad && table->egl.eglStreamConsumerReleaseKHR) {
1147         ret = table->egl.eglStreamConsumerReleaseKHR(
1148             display->GetEglDisplay(), stream);
1149     } else {
1150         WLOGE("eglStreamConsumerReleaseKHR is not found.");
1151     }
1152 
1153     return ret;
1154 }
1155 
EglCreateStreamProducerSurfaceKHRImpl(EGLDisplay dpy,EGLConfig config,EGLStreamKHR stream,const EGLint * attribList)1156 EGLSurface EglCreateStreamProducerSurfaceKHRImpl(EGLDisplay dpy, EGLConfig config,
1157     EGLStreamKHR stream, const EGLint *attribList)
1158 {
1159     ClearError();
1160     WLOGD("");
1161     EglWrapperDisplay *display = ValidateDisplay(dpy);
1162     if (!display) {
1163         return EGL_NO_SURFACE;
1164     }
1165 
1166     return display->CreateStreamProducerSurfaceKHR(config, stream, attribList);
1167 }
1168 
EglQueryStreamTimeKHRImpl(EGLDisplay dpy,EGLStreamKHR stream,EGLenum attribute,EGLTimeKHR * value)1169 EGLBoolean EglQueryStreamTimeKHRImpl(EGLDisplay dpy, EGLStreamKHR stream,
1170     EGLenum attribute, EGLTimeKHR *value)
1171 {
1172     ClearError();
1173     WLOGD("");
1174     EglWrapperDisplay *display = ValidateDisplay(dpy);
1175     if (!display) {
1176         return EGL_FALSE;
1177     }
1178 
1179     if (value == nullptr) {
1180         WLOGE("EGLint *value is nullptr.");
1181         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
1182         return EGL_FALSE;
1183     }
1184 
1185     EGLBoolean ret = EGL_FALSE;
1186     EglWrapperDispatchTablePtr table = &gWrapperHook;
1187     if (table->isLoad && table->egl.eglQueryStreamTimeKHR) {
1188         ret = table->egl.eglQueryStreamTimeKHR(display->GetEglDisplay(),
1189             stream, attribute, value);
1190     } else {
1191         WLOGE("eglQueryStreamTimeKHR is not found.");
1192     }
1193 
1194     return ret;
1195 }
1196 
EglGetStreamFileDescriptorKHRImpl(EGLDisplay dpy,EGLStreamKHR stream)1197 EGLNativeFileDescriptorKHR EglGetStreamFileDescriptorKHRImpl(
1198     EGLDisplay dpy, EGLStreamKHR stream)
1199 {
1200     WLOGD("");
1201     EglWrapperDisplay *display = ValidateDisplay(dpy);
1202     if (!display) {
1203         return EGL_NO_FILE_DESCRIPTOR_KHR;
1204     }
1205 
1206     EGLNativeFileDescriptorKHR ret = EGL_NO_FILE_DESCRIPTOR_KHR;
1207     EglWrapperDispatchTablePtr table = &gWrapperHook;
1208     if (table->isLoad && table->egl.eglGetStreamFileDescriptorKHR) {
1209         ret = table->egl.eglGetStreamFileDescriptorKHR(
1210             display->GetEglDisplay(), stream);
1211     } else {
1212         WLOGE("eglGetStreamFileDescriptorKHR is not found.");
1213     }
1214 
1215     return ret;
1216 }
1217 
EglCreateStreamFromFileDescriptorKHRImpl(EGLDisplay dpy,EGLNativeFileDescriptorKHR fd)1218 EGLStreamKHR EglCreateStreamFromFileDescriptorKHRImpl(
1219     EGLDisplay dpy, EGLNativeFileDescriptorKHR fd)
1220 {
1221     ClearError();
1222     WLOGD("");
1223     EglWrapperDisplay *display = ValidateDisplay(dpy);
1224     if (!display) {
1225         return EGL_NO_STREAM_KHR;
1226     }
1227 
1228     EGLStreamKHR ret = EGL_NO_STREAM_KHR;
1229     EglWrapperDispatchTablePtr table = &gWrapperHook;
1230     if (table->isLoad && table->egl.eglCreateStreamFromFileDescriptorKHR) {
1231         ret = table->egl.eglCreateStreamFromFileDescriptorKHR(
1232             display->GetEglDisplay(), fd);
1233     } else {
1234         WLOGE("eglCreateStreamFromFileDescriptorKHR is not found.");
1235     }
1236 
1237     return ret;
1238 }
1239 
EglWaitSyncKHRImpl(EGLDisplay dpy,EGLSyncKHR sync,EGLint flags)1240 EGLBoolean EglWaitSyncKHRImpl(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags)
1241 {
1242     ClearError();
1243     WLOGD("");
1244     EglWrapperDisplay *display = ValidateDisplay(dpy);
1245     if (!display) {
1246         return EGL_FALSE;
1247     }
1248 
1249     EGLBoolean ret = EGL_FALSE;
1250     EglWrapperDispatchTablePtr table = &gWrapperHook;
1251     if (table->isLoad && table->egl.eglWaitSyncKHR) {
1252         ret = table->egl.eglWaitSyncKHR(display->GetEglDisplay(), sync, flags);
1253     } else {
1254         WLOGE("eglWaitSyncKHR is not found.");
1255     }
1256 
1257     return ret;
1258 }
1259 
EglGetPlatformDisplayEXTImpl(EGLenum platform,void * nativeDisplay,const EGLint * attribList)1260 EGLDisplay EglGetPlatformDisplayEXTImpl(EGLenum platform,
1261     void *nativeDisplay, const EGLint *attribList)
1262 {
1263     WLOGD("");
1264     if (nativeDisplay != EGL_DEFAULT_DISPLAY) {
1265         WLOGE("nativeDisplay is invalid.");
1266         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
1267         return EGL_NO_DISPLAY;
1268     }
1269 
1270     if (platform != EGL_PLATFORM_OHOS_KHR) {
1271         WLOGE("EGLenum platform is not EGL_PLATFORM_OHOS_KHR.");
1272         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
1273         return EGL_NO_DISPLAY;
1274     }
1275 
1276     return EglWrapperDisplay::GetEglDisplayExt(platform, nativeDisplay, attribList);
1277 }
1278 
EglSwapBuffersWithDamageKHRImpl(EGLDisplay dpy,EGLSurface draw,EGLint * rects,EGLint nRects)1279 EGLBoolean EglSwapBuffersWithDamageKHRImpl(EGLDisplay dpy, EGLSurface draw,
1280     EGLint *rects, EGLint nRects)
1281 {
1282     ClearError();
1283     WLOGD("");
1284     EglWrapperDisplay *display = ValidateDisplay(dpy);
1285     if (!display) {
1286         return EGL_FALSE;
1287     }
1288 
1289     return display->SwapBuffersWithDamageKHR(draw, rects, nRects);
1290 }
1291 
EglSetDamageRegionKHRImpl(EGLDisplay dpy,EGLSurface surf,EGLint * rects,EGLint nRects)1292 EGLBoolean EglSetDamageRegionKHRImpl(EGLDisplay dpy, EGLSurface surf,
1293     EGLint *rects, EGLint nRects)
1294 {
1295     ClearError();
1296     WLOGD("");
1297     EglWrapperDisplay *display = ValidateDisplay(dpy);
1298     if (!display) {
1299         return EGL_FALSE;
1300     }
1301 
1302     return display->SetDamageRegionKHR(surf, rects, nRects);
1303 }
1304 
EglDupNativeFenceFDANDROIDImpl(EGLDisplay dpy,EGLSyncKHR sync)1305 EGLint EglDupNativeFenceFDANDROIDImpl(EGLDisplay dpy, EGLSyncKHR sync)
1306 {
1307     ClearError();
1308     WLOGD("");
1309     EglWrapperDisplay *display = ValidateDisplay(dpy);
1310     if (!display) {
1311         return EGL_FALSE;
1312     }
1313     EGLint ret = -1;
1314     EglWrapperDispatchTablePtr table = &gWrapperHook;
1315     if (table->isLoad && table->egl.eglDupNativeFenceFDANDROID) {
1316         ret = table->egl.eglDupNativeFenceFDANDROID(display->GetEglDisplay(), sync);
1317     } else {
1318         WLOGE("EglDupNativeFenceFDANDROID platform is not found.");
1319     }
1320     return ret;
1321 }
1322 
EglSetBlobCacheFuncsANDROIDImpl(EGLDisplay dpy,EGLSetBlobFuncANDROID set,EGLGetBlobFuncANDROID get)1323 void EglSetBlobCacheFuncsANDROIDImpl(EGLDisplay dpy, EGLSetBlobFuncANDROID set, EGLGetBlobFuncANDROID get)
1324 {
1325     WLOGD("");
1326     EglWrapperDisplay *display = ValidateDisplay(dpy);
1327     if (!display) {
1328         return;
1329     }
1330 
1331     EglWrapperDispatchTablePtr table = &gWrapperHook;
1332     if (table->isLoad && table->egl.eglSetBlobCacheFuncsANDROID) {
1333         table->egl.eglSetBlobCacheFuncsANDROID(display->GetEglDisplay(),
1334                                                BlobCache::SetBlobFunc, BlobCache::GetBlobFunc);
1335     } else {
1336         WLOGE("EglSetBlobCacheFuncsANDROIDImpl platform is not found.");
1337     }
1338 }
1339 
EglGetCompositorTimingSupportedANDROIDImpl(EGLDisplay dpy,EGLSurface surface,EGLint name)1340 EGLBoolean EglGetCompositorTimingSupportedANDROIDImpl(EGLDisplay dpy, EGLSurface surface, EGLint name)
1341 {
1342     ClearError();
1343     EglWrapperDisplay *display = ValidateDisplay(dpy);
1344     if (!display) {
1345         return EGL_FALSE;
1346     }
1347     return display->GetCompositorTimingSupportedANDROID(surface, name);
1348 }
1349 
EglGetFrameTimestampSupportedANDROIDImpl(EGLDisplay dpy,EGLSurface surface,EGLint timestamp)1350 EGLBoolean EglGetFrameTimestampSupportedANDROIDImpl(EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
1351 {
1352     ClearError();
1353     EglWrapperDisplay *display = ValidateDisplay(dpy);
1354     if (!display) {
1355         return EGL_FALSE;
1356     }
1357     return display->GetFrameTimestampSupportedANDROID(surface, timestamp);
1358 }
1359 
EglPresentationTimeANDROIDImpl(EGLDisplay dpy,EGLSurface surface,EGLnsecsANDROID time)1360 EGLBoolean EglPresentationTimeANDROIDImpl(EGLDisplay dpy, EGLSurface surface, EGLnsecsANDROID time)
1361 {
1362     ClearError();
1363     EglWrapperDisplay *display = ValidateDisplay(dpy);
1364     if (!display) {
1365         return EGL_FALSE;
1366     }
1367     return display->PresentationTimeANDROID(surface, time);
1368 }
1369 
EglCreatePlatformWindowSurfaceEXTImpl(EGLDisplay dpy,EGLConfig config,void * nativeWindow,const EGLint * attribList)1370 EGLSurface EglCreatePlatformWindowSurfaceEXTImpl(EGLDisplay dpy, EGLConfig config, void *nativeWindow,
1371     const EGLint *attribList)
1372 {
1373     ClearError();
1374     EglWrapperDisplay *display = ValidateDisplay(dpy);
1375     if (!display) {
1376         return EGL_FALSE;
1377     }
1378     return display->CreatePlatformWindowSurfaceEXT(config, nativeWindow, attribList);
1379 }
1380 
EglCreatePlatformPixmapSurfaceEXTImpl(EGLDisplay dpy,EGLConfig config,void * nativePixmap,const EGLint * attribList)1381 EGLSurface EglCreatePlatformPixmapSurfaceEXTImpl(EGLDisplay dpy, EGLConfig config, void *nativePixmap,
1382     const EGLint *attribList)
1383 {
1384     ClearError();
1385     EglWrapperDisplay *display = ValidateDisplay(dpy);
1386     if (!display) {
1387         return EGL_FALSE;
1388     }
1389     return display->CreatePlatformPixmapSurfaceEXT(config, nativePixmap, attribList);
1390 }
1391 
EglSwapBuffersWithDamageEXTImpl(EGLDisplay dpy,EGLSurface surface,const EGLint * rects,EGLint nRects)1392 EGLBoolean EglSwapBuffersWithDamageEXTImpl(EGLDisplay dpy, EGLSurface surface, const EGLint *rects, EGLint nRects)
1393 {
1394     ClearError();
1395     EglWrapperDisplay *display = ValidateDisplay(dpy);
1396     if (!display) {
1397         return EGL_FALSE;
1398     }
1399     return display->SwapBuffersWithDamageEXT(surface, rects, nRects);
1400 }
1401 
EglGetNativeClientBufferANDROIDImpl(const struct AHardwareBuffer * buffer)1402 EGLClientBuffer EglGetNativeClientBufferANDROIDImpl(const struct AHardwareBuffer *buffer)
1403 {
1404     ClearError();
1405     if (buffer == nullptr) {
1406         WLOGE("EGLDislay is invalid.");
1407         ThreadPrivateDataCtl::SetError(EGL_BAD_PARAMETER);
1408         return nullptr;
1409     }
1410 
1411     auto nativeWindowBuffer = CreateNativeWindowBufferFromNativeBuffer(reinterpret_cast<OH_NativeBuffer*>(
1412         const_cast<AHardwareBuffer*>(buffer)));
1413     if (nativeWindowBuffer == nullptr) {
1414         WLOGE("EglGetNativeClientBufferANDROIDImpl nativeWindowBuffer is nullptr.");
1415     }
1416     return nativeWindowBuffer;
1417 }
1418 
1419 static const std::map<std::string, EglWrapperFuncPointer> gEglWrapperMap = {
1420     /* EGL_VERSION_1_0 */
1421     { "eglChooseConfig", (EglWrapperFuncPointer)&EglChooseConfigImpl },
1422     { "eglCopyBuffers", (EglWrapperFuncPointer)&EglCopyBuffersImpl },
1423     { "eglCreateContext", (EglWrapperFuncPointer)&EglCreateContextImpl },
1424     { "eglCreatePbufferSurface", (EglWrapperFuncPointer)&EglCreatePbufferSurfaceImpl },
1425     { "eglCreatePixmapSurface", (EglWrapperFuncPointer)&EglCreatePixmapSurfaceImpl },
1426     { "eglCreateWindowSurface", (EglWrapperFuncPointer)&EglCreateWindowSurfaceImpl },
1427     { "eglDestroyContext", (EglWrapperFuncPointer)&EglDestroyContextImpl },
1428     { "eglDestroySurface", (EglWrapperFuncPointer)&EglDestroySurfaceImpl },
1429     { "eglGetConfigAttrib", (EglWrapperFuncPointer)&EglGetConfigAttribImpl },
1430     { "eglGetConfigs", (EglWrapperFuncPointer)&EglGetConfigsImpl },
1431     { "eglGetCurrentDisplay", (EglWrapperFuncPointer)&EglGetCurrentDisplayImpl },
1432     { "eglGetCurrentSurface", (EglWrapperFuncPointer)&EglGetCurrentSurfaceImpl },
1433     { "eglGetDisplay", (EglWrapperFuncPointer)&EglGetDisplayImpl },
1434     { "eglGetError", (EglWrapperFuncPointer)&EglGetErrorImpl },
1435     { "eglGetProcAddress", (EglWrapperFuncPointer)&EglGetProcAddressImpl },
1436     { "eglInitialize", (EglWrapperFuncPointer)&EglInitializeImpl },
1437     { "eglMakeCurrent", (EglWrapperFuncPointer)&EglMakeCurrentImpl },
1438     { "eglQueryContext", (EglWrapperFuncPointer)&EglQueryContextImpl },
1439     { "eglQueryString", (EglWrapperFuncPointer)&EglQueryStringImpl },
1440     { "eglQuerySurface", (EglWrapperFuncPointer)&EglQuerySurfaceImpl },
1441     { "eglSwapBuffers", (EglWrapperFuncPointer)&EglSwapBuffersImpl },
1442     { "eglTerminate", (EglWrapperFuncPointer)&EglTerminateImpl },
1443     { "eglWaitGL", (EglWrapperFuncPointer)&EglWaitGLImpl },
1444     { "eglWaitNative", (EglWrapperFuncPointer)&EglWaitNativeImpl },
1445 
1446     /* EGL_VERSION_1_1 */
1447     { "eglBindTexImage", (EglWrapperFuncPointer)&EglBindTexImageImpl },
1448     { "eglReleaseTexImage", (EglWrapperFuncPointer)&EglReleaseTexImageImpl },
1449     { "eglSurfaceAttrib", (EglWrapperFuncPointer)&EglSurfaceAttribImpl },
1450     { "eglSwapInterval", (EglWrapperFuncPointer)&EglSwapIntervalImpl },
1451 
1452     /* EGL_VERSION_1_2 */
1453     { "eglBindAPI", (EglWrapperFuncPointer)&EglBindAPIImpl },
1454     { "eglQueryAPI", (EglWrapperFuncPointer)&EglQueryAPIImpl },
1455     { "eglCreatePbufferFromClientBuffer", (EglWrapperFuncPointer)&EglCreatePbufferFromClientBufferImpl },
1456     { "eglReleaseThread", (EglWrapperFuncPointer)&EglReleaseThreadImpl },
1457     { "eglWaitClient", (EglWrapperFuncPointer)&EglWaitClientImpl },
1458 
1459     /* EGL_VERSION_1_3 */
1460     /* EGL_VERSION_1_4 */
1461     { "eglGetCurrentContext", (EglWrapperFuncPointer)&EglGetCurrentContextImpl },
1462 
1463     /* EGL_VERSION_1_5 */
1464     { "eglCreateSync", (EglWrapperFuncPointer)&EglCreateSyncImpl },
1465     { "eglDestroySync", (EglWrapperFuncPointer)&EglDestroySyncImpl },
1466     { "eglClientWaitSync", (EglWrapperFuncPointer)&EglClientWaitSyncImpl },
1467     { "eglGetSyncAttrib", (EglWrapperFuncPointer)&EglGetSyncAttribImpl },
1468     { "eglCreateImage", (EglWrapperFuncPointer)&EglCreateImageImpl },
1469     { "eglDestroyImage", (EglWrapperFuncPointer)&EglDestroyImageImpl },
1470     { "eglGetPlatformDisplay", (EglWrapperFuncPointer)&EglGetPlatformDisplayImpl },
1471     { "eglCreatePlatformWindowSurface", (EglWrapperFuncPointer)&EglCreatePlatformWindowSurfaceImpl },
1472     { "eglCreatePlatformPixmapSurface", (EglWrapperFuncPointer)&EglCreatePlatformPixmapSurfaceImpl },
1473     { "eglWaitSync", (EglWrapperFuncPointer)&EglWaitSyncImpl },
1474 
1475     /* EGL_EXTENTIONS */
1476     { "eglLockSurfaceKHR", (EglWrapperFuncPointer)&EglLockSurfaceKHRImpl },
1477     { "eglUnlockSurfaceKHR", (EglWrapperFuncPointer)&EglUnlockSurfaceKHRImpl },
1478     { "eglDupNativeFenceFDANDROID", (EglWrapperFuncPointer)&EglDupNativeFenceFDANDROIDImpl },
1479 
1480     { "eglCreateImageKHR", (EglWrapperFuncPointer)&EglCreateImageKHRImpl },
1481     { "eglDestroyImageKHR", (EglWrapperFuncPointer)&EglDestroyImageKHRImpl },
1482 
1483     { "eglCreateSyncKHR", (EglWrapperFuncPointer)&EglCreateSyncKHRImpl },
1484     { "eglDestroySyncKHR", (EglWrapperFuncPointer)&EglDestroySyncKHRImpl },
1485     { "eglClientWaitSyncKHR", (EglWrapperFuncPointer)&EglClientWaitSyncKHRImpl },
1486     { "eglGetSyncAttribKHR", (EglWrapperFuncPointer)&EglGetSyncAttribKHRImpl },
1487 
1488     { "eglSignalSyncKHR", (EglWrapperFuncPointer)&EglSignalSyncKHRImpl },
1489 
1490     { "eglCreateStreamKHR", (EglWrapperFuncPointer)&EglCreateStreamKHRImpl },
1491     { "eglDestroyStreamKHR", (EglWrapperFuncPointer)&EglDestroyStreamKHRImpl },
1492     { "eglStreamAttribKHR", (EglWrapperFuncPointer)&EglStreamAttribKHRImpl },
1493     { "eglQueryStreamKHR", (EglWrapperFuncPointer)&EglQueryStreamKHRImpl },
1494     { "eglQueryStreamu64KHR", (EglWrapperFuncPointer)&EglQueryStreamu64KHRImpl },
1495 
1496     { "eglStreamConsumerGLTextureExternalKHR", (EglWrapperFuncPointer)&EglStreamConsumerGLTextureExternalKHRImpl },
1497     { "eglStreamConsumerAcquireKHR", (EglWrapperFuncPointer)&EglStreamConsumerAcquireKHRImpl },
1498     { "eglStreamConsumerReleaseKHR", (EglWrapperFuncPointer)&EglStreamConsumerReleaseKHRImpl },
1499 
1500     { "eglCreateStreamProducerSurfaceKHR", (EglWrapperFuncPointer)&EglCreateStreamProducerSurfaceKHRImpl },
1501 
1502     { "eglQueryStreamTimeKHR", (EglWrapperFuncPointer)&EglQueryStreamTimeKHRImpl },
1503 
1504     { "eglGetStreamFileDescriptorKHR", (EglWrapperFuncPointer)&EglGetStreamFileDescriptorKHRImpl },
1505     { "eglCreateStreamFromFileDescriptorKHR", (EglWrapperFuncPointer)&EglCreateStreamFromFileDescriptorKHRImpl },
1506 
1507     { "eglWaitSyncKHR", (EglWrapperFuncPointer)&EglWaitSyncKHRImpl },
1508 
1509     /* EGL_EXT_platform_base */
1510     { "eglGetPlatformDisplayEXT", (EglWrapperFuncPointer)&EglGetPlatformDisplayEXTImpl },
1511     { "eglCreatePlatformWindowSurfaceEXT", (EglWrapperFuncPointer)&EglCreatePlatformWindowSurfaceEXTImpl },
1512     { "eglCreatePlatformPixmapSurfaceEXT", (EglWrapperFuncPointer)&EglCreatePlatformPixmapSurfaceEXTImpl },
1513 
1514     { "eglSwapBuffersWithDamageKHR", (EglWrapperFuncPointer)&EglSwapBuffersWithDamageKHRImpl },
1515     { "eglSetDamageRegionKHR", (EglWrapperFuncPointer)&EglSetDamageRegionKHRImpl },
1516 
1517     /* EGL_EXT_swap_buffers_with_damage */
1518     { "eglSwapBuffersWithDamageEXT", (EglWrapperFuncPointer)&EglSwapBuffersWithDamageEXTImpl },
1519 
1520     { "eglSetBlobCacheFuncsANDROID", (EglWrapperFuncPointer)&EglSetBlobCacheFuncsANDROIDImpl },
1521 
1522     /* EGL_ANDROID_get_frame_timestamps */
1523     { "eglGetCompositorTimingSupportedANDROID", (EglWrapperFuncPointer)&EglGetCompositorTimingSupportedANDROIDImpl },
1524     { "eglGetFrameTimestampSupportedANDROID", (EglWrapperFuncPointer)&EglGetFrameTimestampSupportedANDROIDImpl },
1525     { "eglPresentationTimeANDROID", (EglWrapperFuncPointer)&EglPresentationTimeANDROIDImpl },
1526 
1527     /* EGL_ANDROID_get_native_client_buffer */
1528     { "eglGetNativeClientBufferANDROID", (EglWrapperFuncPointer)&EglGetNativeClientBufferANDROIDImpl },
1529 };
1530 
FindEglWrapperApi(const std::string & name)1531 EglWrapperFuncPointer FindEglWrapperApi(const std::string &name)
1532 {
1533     if (gEglWrapperMap.find(name) != gEglWrapperMap.end()) {
1534         return gEglWrapperMap.at(name);
1535     }
1536 
1537     WLOGW("FindEglWrapperApi did not find an entry for %{public}s", name.c_str());
1538     return nullptr;
1539 }
1540 
CheckEglWrapperApi(const std::string & name)1541 bool CheckEglWrapperApi(const std::string &name)
1542 {
1543     if (gEglWrapperMap.find(name) != gEglWrapperMap.end()) {
1544         return true;
1545     }
1546     return false;
1547 }
1548 }; // namespace OHOS
1549