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     CHECK_AND_RETURN_RET_LOG(display_ != nullptr, false, "RenderSurface eglGetDisplay fail.");
47     std::vector<int> attributeList = attribute_.ToEGLAttribList();
48     EGLBoolean ret = eglChooseConfig(display_, attributeList.data(), &config_, 1, &retNum);
49     if (ret != EGL_TRUE) {
50         EGLint error = eglGetError();
51         EFFECT_LOGE("RenderSurface create failed, code: %{public}d", error);
52         return false;
53     }
54     EGLint surfaceAttribs[] = {
55         EGL_NONE
56     };
57     EGLNativeWindowType mEglWindow = reinterpret_cast<EGLNativeWindowType>(window);
58     surface_ = eglCreateWindowSurface(display_, config_, mEglWindow, surfaceAttribs);
59     if (surface_ == EGL_NO_SURFACE) {
60         EGLint error = eglGetError();
61         EFFECT_LOGE("RenderSurface create failed, code: %{public}d", error);
62         return false;
63     }
64     surfaceType_ = SurfaceType::SURFACE_TYPE_ON_SCREEN;
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<int> attributeList = attribute_.ToEGLAttribList();
74     EGLBoolean ret = eglChooseConfig(display_, attributeList.data(), &config_, 1, &retNum);
75     if (ret != EGL_TRUE) {
76         EGLint error = eglGetError();
77         EFFECT_LOGE("RenderSurface create failed, 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         EFFECT_LOGE("RenderSurface create failed, code: %{public}d", error);
87         return false;
88     }
89     surfaceType_ = SurfaceType::SURFACE_TYPE_OFF_SCREEN;
90     SetReady(true);
91     return true;
92 }
93 
Release()94 bool RenderSurface::Release()
95 {
96     if (IsReady()) {
97         EGLBoolean ret = eglDestroySurface(display_, surface_);
98         if (ret != EGL_TRUE) {
99             EGLint error = eglGetError();
100             EFFECT_LOGE("RenderSurface create failed, code: %{public}d", error);
101             return false;
102         }
103         surfaceType_ = SurfaceType::SURFACE_TYPE_NULL;
104         SetReady(false);
105     }
106     return true;
107 }
108 
GetRawSurface() const109 void *RenderSurface::GetRawSurface() const
110 {
111     return static_cast<void*>(surface_);
112 }
113 
GetSurfaceType() const114 RenderSurface::SurfaceType RenderSurface::GetSurfaceType() const
115 {
116     return surfaceType_;
117 }
118 } // namespace Effect
119 } // namespace Media
120 } // namespace OHOS