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