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