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 "effect_log.h"
18 
19 namespace OHOS {
20 namespace Media {
21 namespace Effect {
RenderSurface(const std::string &tag)22 RenderSurface::RenderSurface(const std::string &tag)
23     : display_(EGL_NO_DISPLAY),
24       config_(EGL_NO_CONFIG_KHR),
25       surface_(EGL_NO_SURFACE),
26       surfaceType_(SurfaceType::SURFACE_TYPE_NULL)
27 {}
28 
~RenderSurface()29 RenderSurface::~RenderSurface() {}
30 
SetAttrib(const RenderAttribute &attrib)31 void RenderSurface::SetAttrib(const RenderAttribute &attrib)
32 {
33     attribute_ = attrib;
34 }
35 
GetAttrib()36 RenderAttribute RenderSurface::GetAttrib()
37 {
38     return attribute_;
39 }
40 
Create(void *window)41 bool RenderSurface::Create(void *window)
42 {
43     CHECK_AND_RETURN_RET_LOG(window != nullptr, false, "RenderSurface Create window is null!");
44     EGLint retNum = 0;
45     display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
46     std::vector<int> attributeList = attribute_.ToEGLAttribList();
47     EGLBoolean ret = eglChooseConfig(display_, attributeList.data(), &config_, 1, &retNum);
48     if (ret != EGL_TRUE) {
49         EGLint error = eglGetError();
50         EFFECT_LOGE("RenderSurface create failed, code: %{public}d", error);
51         return false;
52     }
53     EGLint surfaceAttribs[] = {
54         EGL_NONE
55     };
56     EGLNativeWindowType mEglWindow = reinterpret_cast<EGLNativeWindowType>(window);
57     surface_ = eglCreateWindowSurface(display_, config_, mEglWindow, surfaceAttribs);
58     if (surface_ == EGL_NO_SURFACE) {
59         EGLint error = eglGetError();
60         EFFECT_LOGE("RenderSurface create failed, code: %{public}d", error);
61         return false;
62     }
63     surfaceType_ = SurfaceType::SURFACE_TYPE_ON_SCREEN;
64     SetReady(true);
65     return true;
66 }
67 
Init()68 bool RenderSurface::Init()
69 {
70     EGLint retNum = 0;
71     display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
72     std::vector<int> attributeList = attribute_.ToEGLAttribList();
73     EGLBoolean ret = eglChooseConfig(display_, attributeList.data(), &config_, 1, &retNum);
74     if (ret != EGL_TRUE) {
75         EGLint error = eglGetError();
76         EFFECT_LOGE("RenderSurface create failed, code: %{public}d", error);
77         return false;
78     }
79     EGLint surfaceAttribs[] = {
80         EGL_NONE
81     };
82     surface_ = eglCreatePbufferSurface(display_, config_, surfaceAttribs);
83     if (surface_ == EGL_NO_SURFACE) {
84         EGLint error = eglGetError();
85         EFFECT_LOGE("RenderSurface create failed, code: %{public}d", error);
86         return false;
87     }
88     surfaceType_ = SurfaceType::SURFACE_TYPE_OFF_SCREEN;
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             EFFECT_LOGE("RenderSurface create failed, code: %{public}d", error);
100             return false;
101         }
102         surfaceType_ = SurfaceType::SURFACE_TYPE_NULL;
103         SetReady(false);
104     }
105     return true;
106 }
107 
GetRawSurface() const108 void *RenderSurface::GetRawSurface() const
109 {
110     return static_cast<void*>(surface_);
111 }
112 
GetSurfaceType() const113 RenderSurface::SurfaceType RenderSurface::GetSurfaceType() const
114 {
115     return surfaceType_;
116 }
117 } // namespace Effect
118 } // namespace Media
119 } // namespace OHOS