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_manager.h"
17
18 #include <sstream>
19 #include <string>
20
21 #include "window.h"
22 #include "drawing_utils.h"
23
24 namespace OHOS {
25 namespace Rosen {
EGLManager()26 EGLManager::EGLManager() noexcept
27 : nativeWindow_(nullptr),
28 eglDisplay_(EGL_NO_DISPLAY),
29 eglContext_(EGL_NO_CONTEXT),
30 eglSurface_(EGL_NO_SURFACE),
31 eglConfig_(nullptr)
32 {
33 }
34
~EGLManager()35 EGLManager::~EGLManager()
36 {
37 if (eglDisplay_ == EGL_NO_DISPLAY) {
38 return;
39 }
40 eglDestroyContext(eglDisplay_, eglContext_);
41 eglMakeCurrent(eglDisplay_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
42 eglTerminate(eglDisplay_);
43 eglReleaseThread();
44
45 eglDisplay_ = EGL_NO_DISPLAY;
46 eglContext_ = EGL_NO_CONTEXT;
47 eglSurface_ = EGL_NO_SURFACE;
48 }
49
50 using GetPlatformDisplayExt = PFNEGLGETPLATFORMDISPLAYEXTPROC;
51 constexpr const char* EGL_EXT_PLATFORM_WAYLAND = "EGL_EXT_platform_wayland";
52 constexpr const char* EGL_KHR_PLATFORM_WAYLAND = "EGL_KHR_platform_wayland";
53 constexpr int32_t EGL_CONTEXT_CLIENT_VERSION_NUM = 2;
54 constexpr char CHARACTER_WHITESPACE = ' ';
55 constexpr const char* CHARACTER_STRING_WHITESPACE = " ";
56 constexpr const char* EGL_GET_PLATFORM_DISPLAY_EXT = "eglGetPlatformDisplayEXT";
57
CheckEglExtension(const char * extensions,const char * extension)58 static bool CheckEglExtension(const char* extensions, const char* extension)
59 {
60 size_t extlen = strlen(extension);
61 const char* end = extensions + strlen(extensions);
62
63 while (extensions < end) {
64 size_t n = 0;
65 /* Skip whitespaces, if any */
66 if (*extensions == CHARACTER_WHITESPACE) {
67 extensions++;
68 continue;
69 }
70
71 n = strcspn(extensions, CHARACTER_STRING_WHITESPACE);
72 /* Compare strings */
73 if (n == extlen && strncmp(extension, extensions, n) == 0) {
74 return true; /* Found */
75 }
76 extensions += n;
77 }
78 /* Not found */
79 return false;
80 }
81
GetPlatformEglDisplay(EGLenum platform,void * native_display,const EGLint * attrib_list)82 static EGLDisplay GetPlatformEglDisplay(EGLenum platform, void* native_display, const EGLint* attrib_list)
83 {
84 static GetPlatformDisplayExt eglGetPlatformDisplayExt = NULL;
85
86 if (!eglGetPlatformDisplayExt) {
87 const char* extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
88 if (extensions &&
89 (CheckEglExtension(extensions, EGL_EXT_PLATFORM_WAYLAND) ||
90 CheckEglExtension(extensions, EGL_KHR_PLATFORM_WAYLAND))) {
91 eglGetPlatformDisplayExt = (GetPlatformDisplayExt)eglGetProcAddress(EGL_GET_PLATFORM_DISPLAY_EXT);
92 }
93 }
94
95 if (eglGetPlatformDisplayExt) {
96 return eglGetPlatformDisplayExt(platform, native_display, attrib_list);
97 }
98
99 return eglGetDisplay((EGLNativeDisplayType)native_display);
100 }
101
Init()102 void EGLManager::Init()
103 {
104 if (IsEglContextReady()) {
105 return;
106 }
107
108 LOGI("Start to creating EGLContext!!!");
109 eglDisplay_ = GetPlatformEglDisplay(EGL_PLATFORM_OHOS_KHR, EGL_DEFAULT_DISPLAY, NULL);
110 if (eglDisplay_ == EGL_NO_DISPLAY) {
111 LOGW("Failed to create EGLDisplay gl errno : %{public}x", eglGetError());
112 return;
113 }
114
115 EGLint major, minor;
116 if (eglInitialize(eglDisplay_, &major, &minor) == EGL_FALSE) {
117 LOGE("Failed to initialize EGLDisplay");
118 return;
119 }
120
121 if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) {
122 LOGE("Failed to bind OpenGL ES API");
123 return;
124 }
125
126 EGLint count;
127 EGLint config_attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
128 EGL_ALPHA_SIZE, 8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, EGL_NONE };
129
130 EGLBoolean ret = eglChooseConfig(eglDisplay_, config_attribs, &eglConfig_, 1, &count);
131 if (!(ret && count >= 1)) {
132 LOGE("Failed to eglChooseConfig");
133 return;
134 }
135
136 static const EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, EGL_CONTEXT_CLIENT_VERSION_NUM, EGL_NONE };
137
138 eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, context_attribs);
139 if (eglContext_ == EGL_NO_CONTEXT) {
140 LOGE("Failed to create egl context %{public}x", eglGetError());
141 return;
142 }
143
144 LOGD("Create EGL context successfully, version %{public}d.%{public}d", major, minor);
145 }
146
IsEglContextReady()147 bool EGLManager::IsEglContextReady()
148 {
149 return (eglContext_ != EGL_NO_DISPLAY);
150 }
151
CreateSurface(EGLNativeWindowType eglNativeWindow)152 EGLSurface EGLManager::CreateSurface(EGLNativeWindowType eglNativeWindow)
153 {
154 if (!IsEglContextReady()) {
155 LOGE("EGL context has not initialized");
156 return EGL_NO_SURFACE;
157 }
158 nativeWindow_ = eglNativeWindow;
159
160 eglMakeCurrent(eglDisplay_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
161
162 EGLint winAttribs[] = { EGL_GL_COLORSPACE_KHR, EGL_GL_COLORSPACE_SRGB_KHR, EGL_NONE };
163 EGLSurface surface = eglCreateWindowSurface(eglDisplay_, eglConfig_, nativeWindow_, winAttribs);
164 if (surface == EGL_NO_SURFACE) {
165 LOGW("Failed to create eglsurface!!! %{public}x", eglGetError());
166 return EGL_NO_SURFACE;
167 }
168
169 LOGD("CreateEGLSurface");
170
171 eglSurface_ = surface;
172 return surface;
173 }
174
DestroySurface()175 void EGLManager::DestroySurface()
176 {
177 if (!eglMakeCurrent(eglDisplay_, EGL_NO_SURFACE, EGL_NO_SURFACE, eglContext_)) {
178 LOGE("Failed to make current on surface, error=%{public}x", eglGetError());
179 }
180
181 if (!eglDestroySurface(eglDisplay_, eglSurface_)) {
182 LOGE("Failed to destroy surface, error=%{public}x", eglGetError());
183 }
184 }
185
MakeCurrent()186 void EGLManager::MakeCurrent()
187 {
188 if (!eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_)) {
189 LOGE("Failed to make current on surface, error=%{public}x", eglGetError());
190 }
191 }
192
SwapBuffers()193 void EGLManager::SwapBuffers()
194 {
195 if (!eglSwapBuffers(eglDisplay_, eglSurface_)) {
196 LOGE("Failed to SwapBuffers on surface, error=%{public}x", eglGetError());
197 } else {
198 LOGD("SwapBuffers successfully");
199 }
200 }
201
SetDamageRegion(int32_t left,int32_t top,int32_t width,int32_t height)202 void EGLManager::SetDamageRegion(int32_t left, int32_t top, int32_t width, int32_t height)
203 {
204 }
205 }
206 }