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 #ifndef FRAMEWORKS_OPENGL_WRAPPER_EGL_WRAPPER_DISPLAY_H
16 #define FRAMEWORKS_OPENGL_WRAPPER_EGL_WRAPPER_DISPLAY_H
17 
18 #include <mutex>
19 #include <unordered_set>
20 #include <EGL/egl.h>
21 #include <EGL/eglext.h>
22 
23 namespace OHOS {
24 class EglWrapperObject;
25 class EglWrapperContext;
26 class EglWrapperSurface;
27 #if USE_IGRAPHICS_EXTENDS_HOOKS
28 struct GlHookTable;
29 #endif
30 
31 class EglWrapperDisplay {
32 public:
33     EGLBoolean Init(EGLint *major, EGLint *minor);
34     EGLBoolean Terminate();
35     EGLBoolean MakeCurrent(EGLSurface draw, EGLSurface read, EGLContext ctx);
36 #if USE_IGRAPHICS_EXTENDS_HOOKS
37     EGLBoolean MakeCurrentAfterHook(EGLSurface draw, EGLSurface read, EGLContext ctx);
38 #endif
39     static EglWrapperDisplay *GetWrapperDisplay(EGLDisplay display);
40     static EGLDisplay GetEglDisplay(EGLenum platform, EGLNativeDisplayType disp, const EGLAttrib *attribList);
41     static EGLDisplay GetEglDisplayExt(EGLenum platform, void *disp, const EGLint *attribList);
42     static bool ValidateEglContext(EGLContext ctx);
43     static bool ValidateEglSurface(EGLSurface surf);
44     EGLContext CreateEglContext(EGLConfig config, EGLContext shareList, const EGLint *attribList);
45     EGLSurface CreateEglSurface(EGLConfig config, NativeWindowType window, const EGLint *attribList);
46     EGLBoolean DestroyEglContext(EGLContext context);
47     EGLBoolean DestroyEglSurface(EGLSurface surf);
48 
49     void AddObject(EglWrapperObject *obj);
50     void RemoveObject(EglWrapperObject *obj);
51 
52     EGLBoolean CopyBuffers(EGLSurface surf, NativePixmapType target);
53     EGLSurface CreatePbufferSurface(EGLConfig config, const EGLint *attribList);
54     EGLSurface CreatePixmapSurface(EGLConfig config, EGLNativePixmapType pixmap, const EGLint* attribList);
IsReady()55     inline bool IsReady() const
56     {
57         return (refCnt_ > 0);
58     };
GetEglDisplay()59     inline EGLDisplay GetEglDisplay() const
60     {
61         return disp_;
62     };
63 
GetVendorValue()64     inline const char *GetVendorValue() const
65     {
66         return vendorValue_.c_str();
67     }
68 
GetVersionValue()69     inline const char *GetVersionValue() const
70     {
71         return versionValue_.c_str();
72     }
73 
GetClientApiValue()74     inline const char *GetClientApiValue() const
75     {
76         return clientApiValue_.c_str();
77     }
78 
GetExtensionValue()79     inline const char *GetExtensionValue() const
80     {
81         return extensionValue_.c_str();
82     }
83 
84     EGLBoolean QueryContext(EGLContext ctx, EGLint attribute, EGLint *value);
85     EGLBoolean QuerySurface(EGLSurface surf, EGLint attribute, EGLint *value);
86     EGLBoolean SwapBuffers(EGLSurface surf);
87     EGLBoolean BindTexImage(EGLSurface surf, EGLint buffer);
88     EGLBoolean ReleaseTexImage(EGLSurface surf, EGLint buffer);
89     EGLBoolean SurfaceAttrib(EGLSurface surf, EGLint attribute, EGLint value);
90     EGLSurface CreatePbufferFromClientBuffer(EGLenum buftype,
91         EGLClientBuffer buffer, EGLConfig config, const EGLint *attribList);
92     EGLImage CreateImage(EGLContext ctx, EGLenum target,
93         EGLClientBuffer buffer, const EGLAttrib *attribList);
94     EGLBoolean DestroyImage(EGLImage img);
95     EGLSurface CreatePlatformWindowSurface(EGLConfig config,
96         void *nativeWindow, const EGLAttrib *attribList);
97     EGLSurface CreatePlatformPixmapSurface(EGLConfig config,
98         void *nativePixmap, const EGLAttrib *attribList);
99     EGLBoolean LockSurfaceKHR(EGLSurface surf, const EGLint *attribList);
100     EGLBoolean UnlockSurfaceKHR(EGLSurface surf);
101 
102     EGLImageKHR CreateImageKHR(EGLContext ctx, EGLenum target,
103         EGLClientBuffer buffer, const EGLint *attribList);
104     EGLBoolean DestroyImageKHR(EGLImageKHR img);
105 
106     EGLSurface CreateStreamProducerSurfaceKHR(EGLConfig config,
107         EGLStreamKHR stream, const EGLint *attribList);
108 
109     EGLBoolean SwapBuffersWithDamageKHR(EGLSurface draw, EGLint *rects, EGLint nRects);
110     EGLBoolean SetDamageRegionKHR(EGLSurface surf, EGLint *rects, EGLint nRects);
111     EGLBoolean GetCompositorTimingSupportedANDROID(EGLSurface surface, EGLint name);
112     EGLBoolean GetFrameTimestampSupportedANDROID(EGLSurface surface, EGLint timestamp);
113     EGLBoolean PresentationTimeANDROID(EGLSurface surface, EGLnsecsANDROID time);
114     EGLSurface CreatePlatformWindowSurfaceEXT(EGLConfig config, void *nativeWindow, const EGLint *attribList);
115     EGLSurface CreatePlatformPixmapSurfaceEXT(EGLConfig config, void *nativePixmap, const EGLint *attribList);
116     EGLBoolean SwapBuffersWithDamageEXT(EGLSurface surface, const EGLint *rects, EGLint nRects);
117 private:
118     EglWrapperDisplay() noexcept;
119     ~EglWrapperDisplay();
120     void UpdateQueryValue(EGLint *major, EGLint *minor);
121     EGLDisplay GetEglNativeDisplay(EGLenum platform, EGLNativeDisplayType disp, const EGLAttrib *attribList);
122     EGLDisplay GetEglNativeDisplayExt(EGLenum platform, void *disp, const EGLint *attribList);
123     bool CheckObject(EglWrapperObject *obj);
124     void ClearObjects();
125     EGLBoolean InternalMakeCurrent(EglWrapperSurface *draw, EglWrapperSurface *read, EglWrapperContext *ctx,
126         bool isAfterHook = false, EglWrapperContext *curCtx = nullptr);
127 
128 #if USE_IGRAPHICS_EXTENDS_HOOKS
129     void ChooseHookTable(bool isAfterHook, const EglWrapperContext *ctx, const EglWrapperContext *curCtx,
130         GlHookTable **ppHookTable);
131     static int ChooseGlesVersion(const EGLint *attribList);
132 #endif
133 
134     static EglWrapperDisplay wrapperDisp_;
135     EGLDisplay  disp_;
136     std::mutex  lockMutex_;
137     std::recursive_mutex refLockMutex_;
138     std::unordered_set<EglWrapperObject *> objects_;
139     uint32_t refCnt_;
140     std::string versionValue_ {};
141     std::string vendorValue_ {};
142     std::string clientApiValue_ {};
143     std::string extensionValue_ {};
144 };
145 } // namespace OHOS
146 #endif // FRAMEWORKS_OPENGL_WRAPPER_EGL_WRAPPER_DISPLAY_H
147