1 /*
2 * Copyright (c) 2025 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 "adapter/preview/entrance/ace_container.h"
17 #include "adapter/preview/external/ability/context.h"
18 #include "bridge/common/utils/engine_helper.h"
19
20 #include "frameworks/base/i18n/localization.h"
21 #include "frameworks/core/common/ace_engine.h"
22 #include "frameworks/core/components_ng/render/adapter/rosen_window.h"
23
24 #include "frameworks/core/common/window_free_container.h"
25
26 namespace OHOS::Ace::Platform {
27 static RefPtr<AceContainer> g_windowFreeContainer = nullptr;
28 static RefPtr<Framework::JsEngine> g_jsEngine = nullptr;
CreateWindowFreeContainer(void * runtime,void * ctx)29 RefPtr<Container> WindowFreeContainer::CreateWindowFreeContainer(void *runtime, void *ctx)
30 {
31 LOGI("Create container without window.");
32 auto container = AceContainer::GetContainerInstance(WINDOW_FREE_CONTAINER_ID);
33 if (container) {
34 return container;
35 }
36
37 if (g_windowFreeContainer) {
38 AceEngine::Get().AddContainer(WINDOW_FREE_CONTAINER_ID, g_windowFreeContainer);
39 EngineHelper::AddEngine(WINDOW_FREE_CONTAINER_ID, g_jsEngine);
40 return g_windowFreeContainer;
41 }
42
43 CHECK_NULL_RETURN(runtime, nullptr);
44
45 AceContainer::CreateContainer(WINDOW_FREE_CONTAINER_ID, FrontendType::DECLARATIVE_JS, true);
46 auto ncontainer = AceContainer::GetContainerInstance(WINDOW_FREE_CONTAINER_ID);
47 ncontainer->SetSharedRuntime(runtime);
48
49 auto& setting = ncontainer->GetSettings();
50 setting.usePlatformAsUIThread = true;
51 setting.useUIAsJSThread = true;
52 if (!Localization::GetInstance()->IsInit()) {
53 AceApplicationInfo::GetInstance().SetLocale("", "", "", "");
54 }
55
56 auto view = AceViewPreview::CreateView(WINDOW_FREE_CONTAINER_ID, false, true);
57 auto window = std::make_shared<NG::RosenWindow>(nullptr, ncontainer->GetTaskExecutor(), WINDOW_FREE_CONTAINER_ID);
58 float width = 0.0f;
59 float height = 0.0f;
60 double density = 0.0;
61 ncontainer->AttachView(window, view, density, width, height, nullptr);
62 g_windowFreeContainer = ncontainer;
63 g_jsEngine = EngineHelper::GetEngine(WINDOW_FREE_CONTAINER_ID);
64 return ncontainer;
65 }
66
DestroyWindowFreeContainer()67 void WindowFreeContainer::DestroyWindowFreeContainer()
68 {
69 LOGI("Destroy container without window.");
70 // Destroy any container will make the rs being background, and stop rendering.
71 // So just keep the container here.
72 auto container = AceEngine::Get().GetContainer(WINDOW_FREE_CONTAINER_ID);
73 if (!container) {
74 return;
75 }
76 EngineHelper::RemoveEngine(WINDOW_FREE_CONTAINER_ID);
77 AceEngine::Get().RemoveContainer(WINDOW_FREE_CONTAINER_ID);
78 }
79 } // namespace OHOS::Ace
80