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 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 namespace OHOS {
23 namespace Rosen {
24 using namespace std;
25
GetConfig(int version,EGLDisplay eglDisplay)26 EGLConfig EglManager::GetConfig(int version, EGLDisplay eglDisplay)
27 {
28 int attribList[] = {
29 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
30 EGL_RED_SIZE, 8,
31 EGL_GREEN_SIZE, 8,
32 EGL_BLUE_SIZE, 8,
33 EGL_ALPHA_SIZE, 8,
34 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
35 EGL_NONE
36 };
37 EGLConfig configs = NULL;
38 int numConfigs;
39 if (!eglChooseConfig(eglDisplay, attribList, &configs, 1, &numConfigs)) {
40 LOGE("eglChooseConfig ERROR");
41 return NULL;
42 }
43 return configs;
44 }
45
Init()46 void EglManager::Init()
47 {
48 if (initialized_) {
49 return;
50 }
51 initialized_ = true;
52 LOGI("EglManager ----- Init.\n");
53 if (EGLContext_ != nullptr) {
54 LOGE("EglManager Init EGLContext_ is already init.\n");
55 return;
56 }
57
58 EGLDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
59 if (EGLDisplay_ == EGL_NO_DISPLAY) {
60 LOGE("EglManager Init unable to get EGL display.\n");
61 return;
62 }
63
64 EGLint eglMajVers, eglMinVers;
65 if (!eglInitialize(EGLDisplay_, &eglMajVers, &eglMinVers)) {
66 EGLDisplay_ = EGL_NO_DISPLAY;
67 LOGE("EglManager Init unable to initialize display");
68 return;
69 }
70
71 LOGI("EglManager Init eglMinVers = %{public}u", eglMinVers);
72 int version = EGL_SUPPORT_VERSION;
73 if (eglMinVers >= EGL_LIMIT_VERSION) {
74 version = EGL_SUPPORT_VERSION;
75 }
76
77 EGLConfig_ = EglManager::GetConfig(version, EGLDisplay_);
78 if (EGLConfig_ == NULL) {
79 LOGE("EglManager Init config ERROR");
80 return;
81 }
82
83 if (EGLWindow_ != nullptr) {
84 LOGI("EglManager Init eglSurface from eglWindow");
85 currentSurface_ = eglCreateWindowSurface(EGLDisplay_, EGLConfig_, EGLWindow_, NULL);
86 if (currentSurface_ == NULL) {
87 LOGE("EglManager Init eglSurface = null");
88 return;
89 }
90 } else {
91 LOGI("EglManager Init eglSurface from PBuffer width = %{public}d, height = %{public}d",
92 EGLWidth_, EGLHeight_);
93 int surfaceAttributes[] = {
94 EGL_WIDTH, EGLWidth_,
95 EGL_HEIGHT, EGLHeight_,
96 EGL_NONE
97 };
98 currentSurface_ = eglCreatePbufferSurface(EGLDisplay_, EGLConfig_, surfaceAttributes);
99 if (currentSurface_ == NULL) {
100 LOGE("EglManager Init eglCreateContext eglSurface = null");
101 return;
102 }
103 }
104
105 int attrib3List[] = {
106 EGL_CONTEXT_CLIENT_VERSION, version,
107 EGL_NONE
108 };
109 EGLContext_ = eglCreateContext(EGLDisplay_, EGLConfig_, nullptr, attrib3List);
110 int error = eglGetError();
111 if (error == EGL_SUCCESS) {
112 LOGI("EglManager Init Create EGLContext_ ok");
113 } else {
114 LOGE("EglManager Init eglCreateContext error %x", error);
115 }
116 eglMakeCurrent(EGLDisplay_, currentSurface_, currentSurface_, EGLContext_);
117 }
118 } // namespace Rosen
119 } // namespace OHOS
120
121 #ifdef __cplusplus
122 }
123 #endif
124