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_surface.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 }
24
RenderSurface(std::string tag)25 RenderSurface::RenderSurface(std::string tag)
26 : display_(EGL_NO_DISPLAY), config_(EGL_NO_CONFIG_KHR), surface_(EGL_NO_SURFACE) {}
27
~RenderSurface()28 RenderSurface::~RenderSurface()
29 {
30 Release();
31 }
32
SetAttrib(const RenderAttribute & attrib)33 void RenderSurface::SetAttrib(const RenderAttribute& attrib)
34 {
35 attribute_ = attrib;
36 }
37
GetAttrib()38 RenderAttribute RenderSurface::GetAttrib()
39 {
40 return attribute_;
41 }
42
Create(OHNativeWindow * window)43 bool RenderSurface::Create(OHNativeWindow* window)
44 {
45 EGLint retNum = 0;
46 display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
47 std::vector<EGLint> attributeList = attribute_.ToEglAttribList();
48 EGLBoolean ret = eglChooseConfig(display_, attributeList.data(), &config_, 1, &retNum);
49 if (ret != EGL_TRUE) {
50 EGLint error = eglGetError();
51 MEDIA_LOGE("[Render] RenderSurface Init ON Fail 0! Code: %{public}d", error);
52 return false;
53 }
54 EGLint surfaceAttribs[] = {
55 EGL_NONE
56 };
57 EGLNativeWindowType mEglWindow = reinterpret_cast<EGLNativeWindowType>(window);
58
59 surface_ = eglCreateWindowSurface(display_, config_, mEglWindow, surfaceAttribs);
60 if (surface_ == EGL_NO_SURFACE) {
61 EGLint error = eglGetError();
62 MEDIA_LOGE("[Render] RenderSurface Init ON Fail 1! Code: %{public}d", error);
63 return false;
64 }
65 SetReady(true);
66 return true;
67 }
68
Init()69 bool RenderSurface::Init()
70 {
71 EGLint retNum = 0;
72 display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
73 std::vector<EGLint> attributeList = attribute_.ToEglAttribList();
74 EGLBoolean ret = eglChooseConfig(display_, attributeList.data(), &config_, 1, &retNum);
75 if (ret != EGL_TRUE) {
76 EGLint error = eglGetError();
77 MEDIA_LOGE("[Render] RenderSurface Init OFF Fail 0! Code: %{public}d", error);
78 return false;
79 }
80 EGLint surfaceAttribs[] = {
81 EGL_NONE
82 };
83 surface_ = eglCreatePbufferSurface(display_, config_, surfaceAttribs);
84 if (surface_ == EGL_NO_SURFACE) {
85 EGLint error = eglGetError();
86 MEDIA_LOGE("[Render] RenderSurface Init OFF Fail 1! Code: %{public}d", error);
87 return false;
88 }
89 SetReady(true);
90 return true;
91 }
92
Release()93 bool RenderSurface::Release()
94 {
95 if (IsReady()) {
96 EGLBoolean ret = eglDestroySurface(display_, surface_);
97 if (ret != EGL_TRUE) {
98 EGLint error = eglGetError();
99 MEDIA_LOGE("[Render] RenderSurface Release Fail 0! Code: %{public}d", error);
100 return false;
101 }
102 SetReady(false);
103 }
104 return true;
105 }
106
GetRawSurface() const107 void* RenderSurface::GetRawSurface() const
108 {
109 return static_cast<void*>(surface_);
110 }
111 }
112 }