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 hapInfo_ = hapInfo;
129 auto client = EngineFactory::CreateEngine(type);
130 client->Clone(engine_.get());
131 return client;
132 }
133
GetEngine(EngineFactory::EngineType type,int32_t key)134 std::unique_ptr<IEngine> GraphicsManagerCommon::GetEngine(EngineFactory::EngineType type, int32_t key)
135 {
136 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::GetEngine");
137 if (viewTextures_.size() > 1u) {
138 WIDGET_LOGD("view is not unique and view size is %zu", viewTextures_.size());
139 }
140 auto backend = backends_.find(key);
141 if (backend == backends_.end() || backend->second == RenderBackend::UNDEFINE) {
142 WIDGET_LOGE("Get engine before register");
143 return nullptr;
144 }
145
146 if (backend->second != RenderBackend::GLES) {
147 WIDGET_LOGE("not support backend yet");
148 return nullptr;
149 }
150
151 // gles context
152 if (engine_ == nullptr) {
153 auto context = offScreenContextHelper_.CreateOffScreenContext(EGL_NO_CONTEXT);
154 engine_ = EngineFactory::CreateEngine(type);
155 WIDGET_LOGD("create proto engine");
156 if (!LoadEngineLib()) {
157 WIDGET_LOGE("load engine lib fail");
158 return nullptr;
159 }
160
161 if (!InitEngine(context, GetPlatformData())) {
162 WIDGET_LOGE("init engine fail");
163 return nullptr;
164 } else {
165 WIDGET_LOGD("engine is initialized");
166 }
167 } else {
168 WIDGET_LOGD("engine is initialized");
169 }
170
171 auto client = EngineFactory::CreateEngine(type);
172 client->Clone(engine_.get());
173 return client;
174 }
175
GetOrCreateOffScreenContext(EGLContext eglContext)176 EGLContext GraphicsManagerCommon::GetOrCreateOffScreenContext(EGLContext eglContext)
177 {
178 AutoRestore scope;
179 return offScreenContextHelper_.CreateOffScreenContext(eglContext);
180 }
181
BindOffScreenContext()182 void GraphicsManagerCommon::BindOffScreenContext()
183 {
184 offScreenContextHelper_.BindOffScreenContext();
185 }
186
UnRegister(int32_t key)187 void GraphicsManagerCommon::UnRegister(int32_t key)
188 {
189 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::UnRegister");
190 WIDGET_LOGD("view unregiser %d total %zu", key, viewTextures_.size());
191
192 auto it = viewTextures_.find(key);
193 if (it == viewTextures_.end()) {
194 WIDGET_LOGE("view unregiser has not regester");
195 return;
196 }
197
198 viewTextures_.erase(it);
199 auto backend = backends_.find(key);
200 if (backend != backends_.end()) {
201 backends_.erase(backend);
202 }
203
204 if (viewTextures_.empty()) {
205 // Destroy proto engine
206 WIDGET_LOGE("view reset proto engine");
207 DeInitEngine();
208 engine_.reset();
209 offScreenContextHelper_.DestroyOffScreenContext();
210 }
211 // need graphics task exit!!!
212 }
213
GetRenderBackendType(int32_t key)214 RenderBackend GraphicsManagerCommon::GetRenderBackendType(int32_t key)
215 {
216 RenderBackend backend = RenderBackend::UNDEFINE;
217 auto it = backends_.find(key);
218 if (it != backends_.end()) {
219 backend = it->second;
220 }
221 return backend;
222 }
223
GetHapInfo() const224 const HapInfo& GraphicsManagerCommon::GetHapInfo() const
225 {
226 return hapInfo_;
227 }
228
HasMultiEcs()229 bool GraphicsManagerCommon::HasMultiEcs()
230 {
231 return viewTextures_.size() > 1;
232 }
233
234 #if defined(MULTI_ECS_UPDATE_AT_ONCE) && (MULTI_ECS_UPDATE_AT_ONCE == 1)
UnloadEcs(void * ecs)235 void GraphicsManagerCommon::UnloadEcs(void* ecs)
236 {
237 WIDGET_LOGD("ACE-3D GraphicsService::UnloadEcs()");
238 ecss_.erase(ecs);
239 }
240
DrawFrame(void * ecs,void * handles)241 void GraphicsManagerCommon::DrawFrame(void* ecs, void* handles)
242 {
243 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::DrawFrame");
244 ecss_[ecs] = handles;
245 WIDGET_LOGD("ACE-3D DrawFrame ecss size %zu", ecss_.size());
246 }
247
PerformDraw()248 void GraphicsManagerCommon::PerformDraw()
249 {
250 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::PerformDraw");
251 if (engine_ == nullptr) {
252 WIDGET_LOGE("ACE-3D PerformDraw but engine is null");
253 return;
254 }
255
256 WIDGET_LOGD("ACE-3D PerformDraw");
257 engine_->DrawMultiEcs(ecss_);
258 engine_->AddTextureMemoryBarrrier();
259 ecss_.clear();
260 }
261
AttachContext(const OHOS::Ace::WeakPtr<OHOS::Ace::PipelineBase> & context)262 void GraphicsManagerCommon::AttachContext(const OHOS::Ace::WeakPtr<OHOS::Ace::PipelineBase>& context)
263 {
264 WIDGET_SCOPED_TRACE("GraphicsManagerCommon::AttachContext");
265 static bool once = false;
266 if (once) {
267 return;
268 }
269
270 auto pipelineContext = context.Upgrade();
271 if (!pipelineContext) {
272 WIDGET_LOGE("ACE-3D GraphicsManagerCommon::AttachContext() GetContext failed.");
273 return;
274 }
275
276 once = true;
277 pipelineContext->SetGSVsyncCallback([&] {
278 // here we could only push sync task to graphic task, if async manner we
279 // have no chance to update the rendering future
280 PerformDraw();
281 });
282 }
283 #endif
284 } // namespace OHOS::Render3D
285