• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "../include/util/egl_manager.h"  // for EglManager
17 
18 #include <cstddef>                       // for NULL
19 
20 #include "EGL/egl.h"                      // for eglCreatePbufferSurface
21 #include "EGL/eglplatform.h"              // for EGLint, NativeWindow
22 #include "__config"                       // for std
23 
24 #include "../include/util/log.h"          // for LOGE, LOGI
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 namespace OHOS {
31 namespace Rosen {
32 namespace {
33     const int OPENGL_ES2_VERSION = 2;
34     const int OPENGL_ES3_VERSION = 3;
35 }
36 using namespace std;
37 
GetConfig(int version,EGLDisplay eglDisplay)38 EGLConfig EglManager::GetConfig(int version, EGLDisplay eglDisplay)
39 {
40     EGLint renderableType = EGL_OPENGL_ES2_BIT;
41     if (version == OPENGL_ES3_VERSION) {
42         renderableType = EGL_OPENGL_ES3_BIT;
43     }
44     int attribList[] = {
45         EGL_SURFACE_TYPE, EGL_PBUFFER_BIT | EGL_WINDOW_BIT,
46         EGL_RENDERABLE_TYPE, renderableType,
47         EGL_RED_SIZE, 8,
48         EGL_GREEN_SIZE, 8,
49         EGL_BLUE_SIZE, 8,
50         EGL_ALPHA_SIZE, 8,
51         EGL_NONE
52     };
53     EGLConfig configs = NULL;
54     int numConfigs;
55     if (!eglChooseConfig(eglDisplay, attribList, &configs, 1, &numConfigs)) {
56         LOGE("eglChooseConfig ERROR");
57         return NULL;
58     }
59     return configs;
60 }
61 
MakeCurrentIfNeeded(EGLSurface newEGLSurface)62 void EglManager::MakeCurrentIfNeeded(EGLSurface newEGLSurface)
63 {
64     if (mCurrentSurface != newEGLSurface) {
65         mCurrentSurface = newEGLSurface;
66         eglMakeCurrent(mEGLDisplay, mCurrentSurface, mCurrentSurface, mEGLContext);
67     }
68 }
69 
CreateSurface(NativeWindow * window)70 EGLSurface EglManager::CreateSurface(NativeWindow* window)
71 {
72     EGLSurface eglSurface = nullptr;
73     if (window) {
74         LOGI("EglManager CreateSurface from eglWindow");
75         eglSurface = eglCreateWindowSurface(mEGLDisplay, mEGLConfig, window, NULL);
76     } else {
77         LOGI("EglManager CreateSurface from PBuffer width = %{public}d, height = %{public}d",
78             mEglWidth, mEglHeight);
79         int surfaceAttributes[] = {
80             EGL_WIDTH, mEglWidth,
81             EGL_HEIGHT, mEglHeight,
82             EGL_NONE
83         };
84         eglSurface = eglCreatePbufferSurface(mEGLDisplay, mEGLConfig, surfaceAttributes);
85     }
86     return eglSurface;
87 }
88 
Init()89 void EglManager::Init()
90 {
91     if (initialized) {
92         return;
93     }
94     initialized = true;
95     LOGI("EglManager ----- Init.\n");
96     if (mEGLContext != nullptr) {
97         LOGE("EglManager Init mEGLContext is already init.\n");
98         return;
99     }
100 
101     mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
102     if (mEGLDisplay == EGL_NO_DISPLAY) {
103         LOGE("EglManager Init unable to get EGL display.\n");
104         return;
105     }
106 
107     EGLint eglMajVers, eglMinVers;
108     if (!eglInitialize(mEGLDisplay, &eglMajVers, &eglMinVers)) {
109         mEGLDisplay = EGL_NO_DISPLAY;
110         LOGE("EglManager Init unable to initialize display");
111         return;
112     }
113 
114     int version = OPENGL_ES3_VERSION;
115     mEGLConfig = EglManager::GetConfig(version, mEGLDisplay);
116     if (mEGLConfig == NULL) {
117         LOGE("EglManager Init config ERROR, try again");
118         version = OPENGL_ES2_VERSION;
119         mEGLConfig = EglManager::GetConfig(version, mEGLDisplay);
120         if (mEGLConfig == NULL) {
121             LOGE("EglManager Init config ERROR again");
122             return;
123         }
124     }
125 
126     if (mEglWindow) {
127         LOGI("EglManager Init eglSurface from eglWindow");
128         mCurrentSurface = eglCreateWindowSurface(mEGLDisplay, mEGLConfig, mEglWindow, NULL);
129         if (mCurrentSurface == NULL) {
130             LOGE("EglManager Init eglSurface = null");
131             return;
132         }
133     } else {
134         LOGI("EglManager Init eglSurface from PBuffer width = %{public}d, height = %{public}d",
135             mEglWidth, mEglHeight);
136         int surfaceAttributes[] = {
137             EGL_WIDTH, mEglWidth,
138             EGL_HEIGHT, mEglHeight,
139             EGL_NONE
140         };
141         mCurrentSurface = eglCreatePbufferSurface(mEGLDisplay, mEGLConfig, surfaceAttributes);
142         if (mCurrentSurface == NULL) {
143             LOGE("EglManager Init eglCreateContext eglSurface = null");
144             return;
145         }
146     }
147 
148     int attrib3List[] = {
149         EGL_CONTEXT_CLIENT_VERSION, version,
150         EGL_NONE
151     };
152     mEGLContext = eglCreateContext(mEGLDisplay, mEGLConfig, nullptr, attrib3List);
153     int error = eglGetError();
154     if (error == EGL_SUCCESS) {
155         LOGI("EglManager Init Create mEGLContext ok");
156     } else {
157         LOGE("EglManager Init eglCreateContext error %{public}x", error);
158     }
159     eglMakeCurrent(mEGLDisplay, mCurrentSurface, mCurrentSurface, mEGLContext);
160 }
161 } // namespace Rosen
162 } // namespace OHOS
163 
164 #ifdef __cplusplus
165 }
166 #endif
167