1 /*
2  * Copyright (C) 2024 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 "render_context.h"
17 #include "render_environment.h"
18 #include "effect_log.h"
19 #include "effect_trace.h"
20 
21 namespace OHOS {
22 namespace Media {
23 namespace Effect {
RenderContext()24 RenderContext::RenderContext() : display_(EGL_NO_DISPLAY), context_(EGL_NO_CONTEXT) {}
25 
~RenderContext()26 RenderContext::~RenderContext() {}
27 
Create(RenderContext * sharedContext)28 bool RenderContext::Create(RenderContext *sharedContext)
29 {
30     EFFECT_TRACE_NAME("RenderContext::Create()");
31     display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
32     if (display_ == EGL_NO_DISPLAY) {
33         EFFECT_LOGE("RenderContext: unable to get EGL display.");
34         return false;
35     }
36     int attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE };
37     int *attribList = attribs;
38     context_ = eglCreateContext(display_, EGL_NO_CONFIG_KHR, sharedContext, attribList);
39     if (context_ == nullptr) {
40         EFFECT_LOGE("RenderContext: unable to create egl context, code: %{public}d", eglGetError());
41         return false;
42     }
43 
44     eglSwapInterval(display_, 0);
45 
46     SetReady(true);
47     return true;
48 }
49 
Init()50 bool RenderContext::Init()
51 {
52     return Create(nullptr);
53 }
54 
Release()55 bool RenderContext::Release()
56 {
57     if (IsReady()) {
58         EGLBoolean ret = eglDestroyContext(display_, context_);
59         if (ret != EGL_TRUE) {
60             EGLint error = eglGetError();
61             EFFECT_LOGE("RenderSurface Release Failed, code: %{public}d", error);
62             return false;
63         }
64         SetReady(false);
65     }
66     return true;
67 }
68 
MakeCurrent(const RenderSurface * surface)69 bool RenderContext::MakeCurrent(const RenderSurface *surface)
70 {
71     if (!IsReady()) {
72         EFFECT_LOGE("EGL MakeCurrent failed");
73         return false;
74     }
75     EGLSurface rawSurface = EGL_NO_SURFACE;
76     if (surface) {
77         rawSurface = static_cast<EGLSurface>(surface->GetRawSurface());
78     }
79     EGLBoolean ret = eglMakeCurrent(display_, rawSurface, rawSurface, context_);
80     if (ret != EGL_TRUE) {
81         EGLint error = eglGetError();
82         EFFECT_LOGE("RenderSurface eglMakeCurrent failed, code: %{public}d", error);
83         return false;
84     }
85     eglSwapInterval(display_, 0);
86     return true;
87 }
88 
ReleaseCurrent()89 bool RenderContext::ReleaseCurrent()
90 {
91     if (!IsReady()) {
92         EFFECT_LOGE("EGL ReleaseCurrent failed");
93         return false;
94     }
95     EGLBoolean ret = eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
96     if (ret != EGL_TRUE) {
97         EGLint error = eglGetError();
98         EFFECT_LOGE("RenderSurface ReleaseCurrent failed, code: %{public}d", error);
99         return false;
100     }
101     return true;
102 }
103 
SwapBuffers(const RenderSurface * surface)104 bool RenderContext::SwapBuffers(const RenderSurface *surface)
105 {
106     if (!IsReady()) {
107         EFFECT_LOGE("EGL SwapBuffers failed");
108         return false;
109     }
110     if (surface == nullptr) {
111         EFFECT_LOGE("EGL SwapBuffers surface is null");
112         return false;
113     }
114     EGLSurface rawSurface = reinterpret_cast<EGLSurface>(surface->GetRawSurface());
115     EGLBoolean ret = eglSwapBuffers(display_, rawSurface);
116     if (ret != EGL_TRUE) {
117         EGLint error = eglGetError();
118         EFFECT_LOGE("RenderSurface eglSwapBuffers failed, code: %{public}d", error);
119         return false;
120     }
121     return true;
122 }
123 }
124 }
125 }