1 /*
2 * Copyright (C) 2023 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 "graphics_manager_common.h"
17
18 #include "3d_widget_adapter_log.h"
19 #include "engine_factory.h"
20 #include "i_engine.h"
21 #include "platform_data.h"
22 #include "widget_trace.h"
23
24 namespace OHOS::Render3D {
~GraphicsManagerCommon()25 GraphicsManagerCommon::~GraphicsManagerCommon()
26 {
27 // should never be called
28 }
29
Register(int32_t key,RenderBackend backend)30 void GraphicsManagerCommon::Register(int32_t key, RenderBackend backend)
31 {
32 if (viewTextures_.find(key) != viewTextures_.end()) {
33 return;
34 }
35
36 viewTextures_.insert(key);
37 backends_[key] = backend;
38 return;
39 }
40
LoadEngineLib()41 bool GraphicsManagerCommon::LoadEngineLib()
42 {
43 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::LoadEngineLib");
44 if (engine_ == nullptr) {
45 return false;
46 }
47
48 if (engineLoaded_) {
49 return true;
50 }
51
52 auto success = engine_->LoadEngineLib();
53 if (success) {
54 engineLoaded_ = true;
55 }
56
57 return success;
58 }
59
InitEngine(EGLContext eglContext,PlatformData data)60 bool GraphicsManagerCommon::InitEngine(EGLContext eglContext, PlatformData data)
61 {
62 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::InitEngine");
63 if (engine_ == nullptr) {
64 return false;
65 }
66
67 if (engineInited_) {
68 return true;
69 }
70
71 auto success = engine_->InitEngine(eglContext, data);
72 if (success) {
73 engineInited_ = true;
74 }
75
76 return success;
77 }
78
DeInitEngine()79 void GraphicsManagerCommon::DeInitEngine()
80 {
81 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::DeInitEngine");
82 if (engineInited_ && engine_ != nullptr) {
83 engine_->DeInitEngine();
84 engineInited_ = false;
85 }
86 }
87
UnloadEngineLib()88 void GraphicsManagerCommon::UnloadEngineLib()
89 {
90 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::UnLoadEngineLib");
91 if (engineLoaded_ && engine_ != nullptr) {
92 engine_->UnloadEngineLib();
93 engineLoaded_ = false;
94 }
95 }
96
GetEngine(EngineFactory::EngineType type,int32_t key,const HapInfo & hapInfo)97 std::unique_ptr<IEngine> GraphicsManagerCommon::GetEngine(EngineFactory::EngineType type, int32_t key,
98 const HapInfo& hapInfo)
99 {
100 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::GetEngine");
101 auto backend = backends_.find(key);
102 if (backend == backends_.end() || backend->second == RenderBackend::UNDEFINE) {
103 WIDGET_LOGE("Get engine before register");
104 return nullptr;
105 }
106
107 if (backend->second != RenderBackend::GLES) {
108 WIDGET_LOGE("not support backend yet");
109 return nullptr;
110 }
111
112 // gles context
113 if (engine_ == nullptr) {
114 auto context = offScreenContextHelper_.CreateOffScreenContext(EGL_NO_CONTEXT);
115 engine_ = EngineFactory::CreateEngine(type);
116 WIDGET_LOGD("create proto engine");
117 if (!LoadEngineLib()) {
118 WIDGET_LOGE("load engine lib fail");
119 return nullptr;
120 }
121
122 if (!InitEngine(context, GetPlatformData(hapInfo))) {
123 WIDGET_LOGE("init engine fail");
124 return nullptr;
125 }
126 }
127
128 auto client = EngineFactory::CreateEngine(type);
129 client->Clone(engine_.get());
130 return client;
131 }
132
GetEngine(EngineFactory::EngineType type,int32_t key)133 std::unique_ptr<IEngine> GraphicsManagerCommon::GetEngine(EngineFactory::EngineType type, int32_t key)
134 {
135 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::GetEngine");
136 auto backend = backends_.find(key);
137 if (backend == backends_.end() || backend->second == RenderBackend::UNDEFINE) {
138 WIDGET_LOGE("Get engine before register");
139 return nullptr;
140 }
141
142 if (backend->second != RenderBackend::GLES) {
143 WIDGET_LOGE("not support backend yet");
144 return nullptr;
145 }
146
147 // gles context
148 if (engine_ == nullptr) {
149 auto context = offScreenContextHelper_.CreateOffScreenContext(EGL_NO_CONTEXT);
150 engine_ = EngineFactory::CreateEngine(type);
151 WIDGET_LOGD("create proto engine");
152 if (!LoadEngineLib()) {
153 WIDGET_LOGE("load engine lib fail");
154 return nullptr;
155 }
156
157 if (!InitEngine(context, GetPlatformData())) {
158 WIDGET_LOGE("init engine fail");
159 return nullptr;
160 }
161 }
162
163 auto client = EngineFactory::CreateEngine(type);
164 client->Clone(engine_.get());
165 return client;
166 }
167
GetOrCreateOffScreenContext(EGLContext eglContext)168 EGLContext GraphicsManagerCommon::GetOrCreateOffScreenContext(EGLContext eglContext)
169 {
170 AutoRestore scope;
171 return offScreenContextHelper_.CreateOffScreenContext(eglContext);
172 }
173
BindOffScreenContext()174 void GraphicsManagerCommon::BindOffScreenContext()
175 {
176 offScreenContextHelper_.BindOffScreenContext();
177 }
178
UnRegister(int32_t key)179 void GraphicsManagerCommon::UnRegister(int32_t key)
180 {
181 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::UnRegister");
182 WIDGET_LOGD("view unregiser %d total %zu", key, viewTextures_.size());
183
184 auto it = viewTextures_.find(key);
185 if (it == viewTextures_.end()) {
186 WIDGET_LOGE("view unregiser has not regester");
187 return;
188 }
189
190 viewTextures_.erase(it);
191 auto backend = backends_.find(key);
192 if (backend != backends_.end()) {
193 backends_.erase(backend);
194 }
195
196 if (viewTextures_.empty()) {
197 // Destroy proto engine
198 WIDGET_LOGE("view reset proto engine");
199 DeInitEngine();
200 engine_.reset();
201 offScreenContextHelper_.DestroyOffScreenContext();
202 }
203 // need graphics task exit!!!
204 }
205
GetRenderBackendType(int32_t key)206 RenderBackend GraphicsManagerCommon::GetRenderBackendType(int32_t key)
207 {
208 RenderBackend backend = RenderBackend::UNDEFINE;
209 auto it = backends_.find(key);
210 if (it != backends_.end()) {
211 backend = it->second;
212 }
213 return backend;
214 }
215
HasMultiEcs()216 bool GraphicsManagerCommon::HasMultiEcs()
217 {
218 return viewTextures_.size() > 1;
219 }
220
221 #if defined(MULTI_ECS_UPDATE_AT_ONCE) && (MULTI_ECS_UPDATE_AT_ONCE == 1)
UnloadEcs(void * ecs)222 void GraphicsManagerCommon::UnloadEcs(void* ecs)
223 {
224 WIDGET_LOGD("ACE-3D GraphicsService::UnloadEcs()");
225 ecss_.erase(ecs);
226 }
227
DrawFrame(void * ecs,void * handles)228 void GraphicsManagerCommon::DrawFrame(void* ecs, void* handles)
229 {
230 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::DrawFrame");
231 ecss_[ecs] = handles;
232 WIDGET_LOGD("ACE-3D DrawFrame ecss size %zu", ecss_.size());
233 }
234
PerformDraw()235 void GraphicsManagerCommon::PerformDraw()
236 {
237 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::PerformDraw");
238 if (engine_ == nullptr) {
239 WIDGET_LOGE("ACE-3D PerformDraw but engine is null");
240 return;
241 }
242
243 WIDGET_LOGD("ACE-3D PerformDraw");
244 engine_->DrawMultiEcs(ecss_);
245 engine_->AddTextureMemoryBarrrier();
246 ecss_.clear();
247 }
248
AttachContext(const OHOS::Ace::WeakPtr<OHOS::Ace::PipelineBase> & context)249 void GraphicsManagerCommon::AttachContext(const OHOS::Ace::WeakPtr<OHOS::Ace::PipelineBase>& context)
250 {
251 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::AttachContext");
252 static bool once = false;
253 if (once) {
254 return;
255 }
256
257 auto pipelineContext = context.Upgrade();
258 if (!pipelineContext) {
259 WIDGET_LOGE("ACE-3D GraphicsManagerCommon::AttachContext() GetContext failed.");
260 return;
261 }
262
263 once = true;
264 pipelineContext->SetGSVsyncCallback([&] {
265 // here we could only push sync task to graphic task, if async manner we
266 // have no chance to update the rendering future
267 PerformDraw();
268 });
269 }
270 #endif
271 } // namespace OHOS::Render3D
272