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