1 /*
2 * Copyright (c) 2021-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 <cstdint>
17 #include <fstream>
18 #include <iostream>
19 #include <sstream>
20 #include <thread>
21
22 #include "jsapp/rich/external/EventHandler.h"
23 #include "previewer/include/window.h"
24
25 #include "adapter/preview/entrance/ace_ability.h"
26 #include "adapter/preview/entrance/ace_run_args.h"
27 #include "adapter/preview/entrance/samples/event_adapter.h"
28 #include "base/log/log.h"
29 #include "base/utils/device_config.h"
30 #include "base/utils/utils.h"
31 #include "adapter/preview/entrance/ace_preview_helper.h"
32
33 namespace {
34
35 constexpr char ACE_VERSION_2[] = "2.0";
36 constexpr int MAX_ARGS_COUNT = 2;
37
38 #ifdef MAC_PLATFORM
39 const std::string assetPathJs = "/Volumes/SSD2T/daily-test/preview/js/default";
40 const std::string assetPathEts = "/Volumes/SSD2T/daily-test/preview/js/default_2.0";
41 #elif WINDOWS_PLATFORM
42 const std::string assetPathJs = "D:\\Workspace\\preview\\js\\default";
43 const std::string assetPathEts = "D:\\Workspace\\preview\\js\\default_2.0";
44 #else
45 const std::string assetPathJs = "/home/ubuntu/demo/preview/js/default";
46 const std::string assetPathEts = "/home/ubuntu/demo/preview/js/default_2.0";
47 #endif
48
49 auto&& renderCallback = [](
__anon8f472a250202( const void*, const size_t bufferSize, const int32_t width, const int32_t height, const uint64_t timestamp) 50 const void*, const size_t bufferSize, const int32_t width, const int32_t height, const uint64_t timestamp) -> bool {
51 LOGI("OnRender: bufferSize = %{public}zu, [width, height] = [%{public}d, %{public}d],
52 timestamp = %{public}llu", bufferSize, width, height, timestamp);
53 return true;
54 };
55
__anon8f472a250302(const std::string currentPagePath) 56 auto&& pageCallback = [](const std::string currentPagePath) -> bool {
57 LOGI("OnRouterChange: current page: %s", currentPagePath.c_str());
58 return true;
59 };
60
61 } // namespace
62
main(int argc,const char * argv[])63 int main(int argc, const char* argv[])
64 {
65 OHOS::Ace::Platform::AceRunArgs args = {
66 .assetPath = assetPathJs,
67 .deviceConfig.orientation = OHOS::Ace::DeviceOrientation::LANDSCAPE,
68 .deviceConfig.density = 1,
69 .deviceConfig.deviceType = OHOS::Ace::DeviceType::TV,
70 .windowTitle = "ACE TV",
71 .deviceWidth = 960,
72 .deviceHeight = 540,
73 .onRender = std::move(renderCallback),
74 .onRouterChange = std::move(pageCallback),
75 };
76
77 if (argc == MAX_ARGS_COUNT && !std::strcmp(argv[1], ACE_VERSION_2)) {
78 args.assetPath = assetPathEts;
79 args.aceVersion = OHOS::Ace::Platform::AceVersion::ACE_2_0;
80 }
81
82 // Initialize and create the glfw window.
83 auto ctx = OHOS::Rosen::GlfwRenderContext::GetGlobal();
84 if (!ctx->Init()) {
85 LOGI("Failed to initialize the glfw.");
86 return -1;
87 }
88 ctx->CreateGlfwWindow(args.deviceWidth, args.deviceHeight, true);
89 OHOS::Ace::Sample::EventAdapter::GetInstance().Initialize(ctx);
90 OHOS::Ace::Platform::AcePreviewHelper::GetInstance()->
91 SetCallbackOfPostTask(OHOS::AppExecFwk::EventHandler::PostTask);
92 OHOS::Ace::Platform::AcePreviewHelper::GetInstance()->
93 SetCallbackOfIsCurrentRunnerThread(OHOS::AppExecFwk::EventHandler::IsCurrentRunnerThread);
94
95 // Create the ace ability
96 auto ability = OHOS::Ace::Platform::AceAbility::CreateInstance(args);
97 CHECK_NULL_RETURN(ability, -1);
98
99 auto&& keyEventCallback = [&ability](
100 const std::shared_ptr<KeyEvent>& keyEvent) { ability->OnInputEvent(keyEvent); };
101 OHOS::Ace::Sample::EventAdapter::GetInstance().RegisterKeyEventCallback(keyEventCallback);
102
103 auto&& pointerEventCallback = [&ability](const std::shared_ptr<PointerEvent>& pointerEvent) {
104 ability->OnInputEvent(pointerEvent);
105 };
106 OHOS::Ace::Sample::EventAdapter::GetInstance().RegisterPointerEventCallback(pointerEventCallback);
107
108 auto&& inspectorCallback = [&ability]() {
109 constexpr char FILE_NAME[] = "InspectorTree.json";
110 std::string jsonTreeStr = ability->GetJSONTree();
111 std::ofstream fileCleaner(FILE_NAME, std::ios_base::out);
112 std::ofstream fileWriter(FILE_NAME, std::ofstream::app);
113 fileWriter << jsonTreeStr;
114 fileWriter << std::endl;
115 fileWriter.close();
116 };
117 OHOS::Ace::Sample::EventAdapter::GetInstance().RegisterInspectorCallback(inspectorCallback);
118
119 OHOS::Rosen::WMError errCode;
120 OHOS::sptr<OHOS::Rosen::WindowOption> sp = nullptr;
121 auto window = OHOS::Rosen::Window::Create("previewer", sp, nullptr, errCode);
122 window->CreateSurfaceNode("preview_surface", args.onRender);
123 ability->SetWindow(window);
124
125 ability->InitEnv();
126 LOGI("Ace initialize done. run event loop now");
127 while (!ctx->WindowShouldClose()) {
128 OHOS::AppExecFwk::EventHandler::Run();
129 ctx->PollEvents();
130 std::this_thread::sleep_for(std::chrono::milliseconds(1));
131 }
132
133 LOGI("Successfully exit the application.");
134 return 0;
135 }
136