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 "adapter/preview/entrance/ace_view_preview.h"
17
18 #include "base/log/dump_log.h"
19 #include "base/log/event_report.h"
20 #include "base/log/log.h"
21 #include "base/utils/macros.h"
22 #include "base/utils/system_properties.h"
23 #include "base/utils/utils.h"
24 #include "core/common/ace_engine.h"
25 #include "core/components/theme/theme_manager.h"
26 #include "core/event/mouse_event.h"
27 #include "core/event/touch_event.h"
28 #include "core/image/image_cache.h"
29
30 namespace OHOS::Ace::Platform {
CreateView(int32_t instanceId,bool useCurrentEventRunner,bool usePlatformThread)31 AceViewPreview* AceViewPreview::CreateView(int32_t instanceId, bool useCurrentEventRunner, bool usePlatformThread)
32 {
33 auto* aceView = new AceViewPreview(instanceId, FlutterThreadModel::CreateThreadModel(
34 useCurrentEventRunner, !usePlatformThread, !SystemProperties::GetRosenBackendEnabled()));
35 if (aceView != nullptr) {
36 aceView->IncRefCount();
37 }
38 return aceView;
39 }
40
AceViewPreview(int32_t instanceId,std::unique_ptr<FlutterThreadModel> threadModel)41 AceViewPreview::AceViewPreview(int32_t instanceId, std::unique_ptr<FlutterThreadModel> threadModel)
42 : instanceId_(instanceId), threadModel_(std::move(threadModel))
43 {}
44
NotifySurfaceChanged(int32_t width,int32_t height,WindowSizeChangeReason type,const std::shared_ptr<Rosen::RSTransaction> & rsTransaction)45 void AceViewPreview::NotifySurfaceChanged(int32_t width, int32_t height,
46 WindowSizeChangeReason type, const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)
47 {
48 width_ = width;
49 height_ = height;
50 CHECK_NULL_VOID(viewChangeCallback_);
51 viewChangeCallback_(width, height, type, rsTransaction);
52 }
53
HandleMouseEvent(const MouseEvent & mouseEvent)54 bool AceViewPreview::HandleMouseEvent(const MouseEvent& mouseEvent)
55 {
56 int32_t eventID = mouseEvent.GetId();
57 auto markProcess = [eventID]() { LOGD("Mark %{public}d id Mouse Event Processed", eventID); };
58 mouseEventCallback_(mouseEvent, markProcess);
59 return true;
60 }
61
HandleAxisEvent(const AxisEvent & axisEvent)62 bool AceViewPreview::HandleAxisEvent(const AxisEvent& axisEvent)
63 {
64 int32_t eventID = axisEvent.id;
65 auto markProcess = [eventID]() { LOGD("Mark %{public}d id Axis Event Processed", eventID); };
66 axisEventCallback_(axisEvent, markProcess);
67 return true;
68 }
69
HandleTouchEvent(const TouchEvent & touchEvent)70 bool AceViewPreview::HandleTouchEvent(const TouchEvent& touchEvent)
71 {
72 LOGD("Event: [x, y, size] = [%{public}lf, %{public}lf, %{public}lf]", touchEvent.x, touchEvent.y, touchEvent.size);
73 if (touchEvent.type == TouchType::UNKNOWN) {
74 LOGW("Unknown event.");
75 return false;
76 }
77 CHECK_NULL_RETURN(touchEventCallback_, true);
78 auto event = touchEvent.UpdatePointers();
79 touchEventCallback_(event, nullptr);
80 return true;
81 }
82
GetDrawDelegate()83 std::unique_ptr<DrawDelegate> AceViewPreview::GetDrawDelegate()
84 {
85 auto drawDelegate = std::make_unique<DrawDelegate>();
86
87 #ifdef ENABLE_ROSEN_BACKEND
88 drawDelegate->SetDrawRSFrameCallback([](std::shared_ptr<Rosen::RSNode>& node, const Rect& rect) {});
89 #else
90 drawDelegate->SetDrawFrameCallback([this](RefPtr<Flutter::Layer>& layer, const Rect& dirty) {
91 if (!layer) {
92 return;
93 }
94 RefPtr<Flutter::FlutterSceneBuilder> flutterSceneBuilder = AceType::MakeRefPtr<Flutter::FlutterSceneBuilder>();
95 layer->AddToScene(*flutterSceneBuilder, 0.0, 0.0);
96 auto scene_ = flutterSceneBuilder->Build();
97 if (!flutter::UIDartState::Current()) {
98 LOGE("uiDartState is nullptr");
99 return;
100 }
101 auto window = flutter::UIDartState::Current()->window();
102 if (window != nullptr && window->client() != nullptr) {
103 window->client()->Render(scene_.get());
104 }
105 });
106 #endif
107
108 return drawDelegate;
109 }
110 } // namespace OHOS::Ace::Platform
111