1 /* 2 * Copyright (c) 2021 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 #ifndef FRAMEWORKS_SURFACE_INCLUDE_EGL_MANAGER_H 17 #define FRAMEWORKS_SURFACE_INCLUDE_EGL_MANAGER_H 18 19 #include <mutex> 20 #include <thread> 21 22 #include <EGL/egl.h> 23 #include <EGL/eglext.h> 24 #include <GLES2/gl2.h> 25 #include <GLES2/gl2ext.h> 26 #include <refbase.h> 27 #include "surface_type.h" 28 29 using EglCreateImageFunc = PFNEGLCREATEIMAGEKHRPROC; 30 using EglDestroyImageFunc = PFNEGLDESTROYIMAGEKHRPROC; 31 using EglImageTargetTexture2DFunc = PFNGLEGLIMAGETARGETTEXTURE2DOESPROC; 32 using EglCreateSyncFunc = PFNEGLCREATESYNCKHRPROC; 33 using EglDestroySyncFunc = PFNEGLDESTROYSYNCKHRPROC; 34 using EglClientWaitSyncFunc = PFNEGLCLIENTWAITSYNCKHRPROC; 35 using EglDupNativeFenceFdFunc = PFNEGLDUPNATIVEFENCEFDANDROIDPROC; 36 using EglWaitSyncFunc = PFNEGLWAITSYNCKHRPROC; 37 using GetPlatformDisplayExt = PFNEGLGETPLATFORMDISPLAYEXTPROC; 38 39 namespace OHOS { 40 class EglManager : public RefBase { 41 public: 42 static sptr<EglManager> GetInstance(); 43 44 EGLDisplay GetEGLDisplay() const; 45 EGLContext GetEGLContext() const; 46 47 EGLImageKHR EglCreateImage(EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attribList); 48 EGLImageKHR EglCreateImage(EGLenum target, EGLClientBuffer buffer, const EGLint *attribList); 49 EGLBoolean EglDestroyImage(EGLImageKHR image); 50 void EglImageTargetTexture2D(GLenum target, GLeglImageOES image); 51 EGLSyncKHR EglCreateSync(EGLenum type, const EGLint *attribList); 52 EGLint EglWaitSync(EGLSyncKHR sync, EGLint flags); 53 EGLint EglClientWaitSync(EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout); 54 EGLBoolean EglDestroySync(EGLSyncKHR sync); 55 EGLint EglDupNativeFenceFd(EGLSyncKHR sync); 56 EGLBoolean EglMakeCurrent(EGLSurface draw, EGLSurface read, EGLContext ctx); 57 EGLBoolean EglMakeCurrent(EGLSurface draw, EGLSurface read); 58 EGLBoolean EglMakeCurrent(); 59 60 GSError Init(EGLContext ctx = EGL_NO_CONTEXT); 61 bool IsInit() const; 62 63 private: 64 EglManager(); 65 virtual ~EglManager(); 66 void Deinit(); 67 GSError GbmInit(); 68 GSError EglInit(EGLContext ctx = EGL_NO_CONTEXT); 69 GSError EglCheckExt(); 70 GSError EglFuncInit(); 71 72 thread_local static inline sptr<EglManager> instance_ = nullptr; 73 bool initFlag_ = false; 74 int drmFd_ = -1; 75 void *device_ = nullptr; 76 77 EGLDisplay display_ = EGL_NO_DISPLAY; 78 EGLContext context_ = EGL_NO_CONTEXT; 79 80 std::map<uint32_t, EGLContext> sharedContextCache; 81 82 bool ctxReleaseFlg_ = false; 83 EGLConfig conf_ = NULL; 84 EglCreateImageFunc createImage_ = nullptr; 85 EglDestroyImageFunc destroyImage_ = nullptr; 86 EglImageTargetTexture2DFunc imageTargetTexture2d_ = nullptr; 87 EglCreateSyncFunc createSync_ = nullptr; 88 EglDestroySyncFunc destroySync_ = nullptr; 89 EglWaitSyncFunc waitSync_ = nullptr; 90 EglClientWaitSyncFunc clientWaitSync_ = nullptr; 91 EglDupNativeFenceFdFunc dupNativeFenceFd_ = nullptr; 92 }; 93 } // namespace OHOS 94 95 #endif // FRAMEWORKS_SURFACE_INCLUDE_EGL_MANAGER_H 96