1 /*
2 * Copyright (c) 2021 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/ohos/entrance/flutter_ace_view.h"
17
18 #include <algorithm>
19 #include <fstream>
20
21 #include "key_event.h"
22 #include "pointer_event.h"
23
24 #include "base/log/dump_log.h"
25 #include "base/log/event_report.h"
26 #include "base/log/log.h"
27 #include "base/utils/macros.h"
28 #include "base/utils/system_properties.h"
29 #include "base/utils/utils.h"
30 #include "core/common/ace_engine.h"
31 #include "core/common/container_scope.h"
32 #include "core/components/theme/app_theme.h"
33 #include "core/components/theme/theme_manager.h"
34 #include "core/event/axis_event.h"
35 #include "core/event/key_event.h"
36 #include "core/event/mouse_event.h"
37 #include "core/event/touch_event.h"
38 #include "core/image/image_cache.h"
39 #include "core/pipeline/layers/flutter_scene_builder.h"
40
41 namespace OHOS::Ace::Platform {
42 namespace {
43
44 constexpr int32_t ROTATION_DIVISOR = 64;
45 constexpr double PERMIT_ANGLE_VALUE = 0.5;
46
47 template<typename E>
GetEventDevice(int32_t sourceType,E & event)48 void GetEventDevice(int32_t sourceType, E& event)
49 {
50 switch (sourceType) {
51 case OHOS::MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN:
52 event.sourceType = SourceType::TOUCH;
53 break;
54 case OHOS::MMI::PointerEvent::SOURCE_TYPE_TOUCHPAD:
55 event.sourceType = SourceType::TOUCH_PAD;
56 break;
57 case OHOS::MMI::PointerEvent::SOURCE_TYPE_MOUSE:
58 event.sourceType = SourceType::MOUSE;
59 break;
60 default:
61 event.sourceType = SourceType::NONE;
62 break;
63 }
64 }
65
ConvertTouchPoint(const MMI::PointerEvent::PointerItem & pointerItem)66 TouchPoint ConvertTouchPoint(const MMI::PointerEvent::PointerItem& pointerItem)
67 {
68 TouchPoint touchPoint;
69 // just get the max of width and height
70 touchPoint.size = std::max(pointerItem.GetWidth(), pointerItem.GetHeight()) / 2.0;
71 touchPoint.id = pointerItem.GetPointerId();
72 touchPoint.force = pointerItem.GetPressure();
73 touchPoint.downTime = TimeStamp(std::chrono::microseconds(pointerItem.GetDownTime()));
74 touchPoint.x = pointerItem.GetLocalX();
75 touchPoint.y = pointerItem.GetLocalY();
76 touchPoint.screenX = pointerItem.GetGlobalX();
77 touchPoint.screenY = pointerItem.GetGlobalY();
78 touchPoint.isPressed = pointerItem.IsPressed();
79 return touchPoint;
80 }
81
UpdateTouchEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent,TouchEvent & touchEvent)82 void UpdateTouchEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, TouchEvent& touchEvent)
83 {
84 auto ids = pointerEvent->GetPointersIdList();
85 for (auto&& id : ids) {
86 MMI::PointerEvent::PointerItem item;
87 bool ret = pointerEvent->GetPointerItem(id, item);
88 if (!ret) {
89 LOGE("get pointer item failed.");
90 continue;
91 }
92 auto touchPoint = ConvertTouchPoint(item);
93 touchEvent.pointers.emplace_back(std::move(touchPoint));
94 }
95 }
96
ConvertTouchEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)97 TouchEvent ConvertTouchEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
98 {
99 int32_t pointerID = pointerEvent->GetPointerId();
100 MMI::PointerEvent::PointerItem item;
101 bool ret = pointerEvent->GetPointerItem(pointerID, item);
102 if (!ret) {
103 LOGE("get pointer item failed.");
104 return TouchEvent();
105 }
106 auto touchPoint = ConvertTouchPoint(item);
107 std::chrono::microseconds microseconds(pointerEvent->GetActionTime());
108 TimeStamp time(microseconds);
109 TouchEvent event { touchPoint.id, touchPoint.x, touchPoint.y, touchPoint.screenX, touchPoint.screenY,
110 TouchType::UNKNOWN, time, touchPoint.size, touchPoint.force, pointerEvent->GetDeviceId() };
111 int32_t orgDevice = pointerEvent->GetSourceType();
112 GetEventDevice(orgDevice, event);
113 int32_t orgAction = pointerEvent->GetPointerAction();
114 switch (orgAction) {
115 case OHOS::MMI::PointerEvent::POINTER_ACTION_CANCEL:
116 event.type = TouchType::CANCEL;
117 break;
118 case OHOS::MMI::PointerEvent::POINTER_ACTION_DOWN:
119 event.type = TouchType::DOWN;
120 break;
121 case OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE:
122 event.type = TouchType::MOVE;
123 break;
124 case OHOS::MMI::PointerEvent::POINTER_ACTION_UP:
125 event.type = TouchType::UP;
126 break;
127 default:
128 LOGW("unknown type");
129 break;
130 }
131 UpdateTouchEvent(pointerEvent, event);
132 return event;
133 }
134
GetMouseEventAction(int32_t action,MouseEvent & events)135 void GetMouseEventAction(int32_t action, MouseEvent& events)
136 {
137 switch (action) {
138 case OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN:
139 events.action = MouseAction::PRESS;
140 break;
141 case OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_UP:
142 events.action = MouseAction::RELEASE;
143 break;
144 case OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE:
145 events.action = MouseAction::MOVE;
146 break;
147 default:
148 events.action = MouseAction::NONE;
149 break;
150 }
151 }
152
GetMouseEventButton(int32_t button,MouseEvent & events)153 void GetMouseEventButton(int32_t button, MouseEvent& events)
154 {
155 switch (button) {
156 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_LEFT:
157 events.button = MouseButton::LEFT_BUTTON;
158 break;
159 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_RIGHT:
160 events.button = MouseButton::RIGHT_BUTTON;
161 break;
162 case OHOS::MMI::PointerEvent::MOUSE_BUTTON_MIDDLE:
163 events.button = MouseButton::MIDDLE_BUTTON;
164 break;
165 default:
166 events.button = MouseButton::NONE_BUTTON;
167 break;
168 }
169 }
170
ConvertMouseEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent,MouseEvent & events)171 void ConvertMouseEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, MouseEvent& events)
172 {
173 int32_t pointerID = pointerEvent->GetPointerId();
174 MMI::PointerEvent::PointerItem item;
175 bool ret = pointerEvent->GetPointerItem(pointerID, item);
176 if (!ret) {
177 LOGE("get pointer item failed.");
178 return;
179 }
180
181 events.x = item.GetLocalX();
182 events.y = item.GetLocalY();
183 events.screenX = item.GetGlobalX();
184 events.screenY = item.GetGlobalY();
185 int32_t orgAction = pointerEvent->GetPointerAction();
186 GetMouseEventAction(orgAction, events);
187 int32_t orgButton = pointerEvent->GetButtonId();
188 GetMouseEventButton(orgButton, events);
189 int32_t orgDevice = pointerEvent->GetSourceType();
190 GetEventDevice(orgDevice, events);
191
192 std::set<int32_t> pressedSet = pointerEvent->GetPressedButtons();
193 uint32_t pressedButtons = 0;
194 if (pressedSet.find(OHOS::MMI::PointerEvent::MOUSE_BUTTON_LEFT) != pressedSet.end()) {
195 pressedButtons &= static_cast<uint32_t>(MouseButton::LEFT_BUTTON);
196 }
197 if (pressedSet.find(OHOS::MMI::PointerEvent::MOUSE_BUTTON_RIGHT) != pressedSet.end()) {
198 pressedButtons &= static_cast<uint32_t>(MouseButton::RIGHT_BUTTON);
199 }
200 if (pressedSet.find(OHOS::MMI::PointerEvent::MOUSE_BUTTON_MIDDLE) != pressedSet.end()) {
201 pressedButtons &= static_cast<uint32_t>(MouseButton::MIDDLE_BUTTON);
202 }
203 events.pressedButtons = static_cast<int32_t>(pressedButtons);
204
205 std::chrono::microseconds microseconds(pointerEvent->GetActionTime());
206 TimeStamp time(microseconds);
207 events.time = time;
208 LOGI("ConvertMouseEvent: (x,y): (%{public}f,%{public}f). Button: %{public}d. Action: %{public}d. "
209 "DeviceType: %{public}d. PressedButton: %{public}d. Time: %{public}lld",
210 events.x, events.y, events.button, events.action, events.sourceType, events.pressedButtons,
211 pointerEvent->GetActionTime());
212 }
213
ConvertAxisEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent,AxisEvent & event)214 void ConvertAxisEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, AxisEvent& event)
215 {
216 int32_t pointerID = pointerEvent->GetPointerId();
217 MMI::PointerEvent::PointerItem item;
218 bool ret = pointerEvent->GetPointerItem(pointerID, item);
219 if (!ret) {
220 LOGE("get pointer item failed.");
221 return;
222 }
223
224 event.x = item.GetLocalX();
225 event.y = item.GetLocalY();
226 event.horizontalAxis = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_SCROLL_HORIZONTAL);
227 event.verticalAxis = pointerEvent->GetAxisValue(OHOS::MMI::PointerEvent::AxisType::AXIS_TYPE_SCROLL_VERTICAL);
228 int32_t orgDevice = pointerEvent->GetSourceType();
229 GetEventDevice(orgDevice, event);
230
231 std::chrono::microseconds microseconds(pointerEvent->GetActionTime());
232 TimeStamp time(microseconds);
233 event.time = time;
234 LOGI("ConvertAxisEvent: (x,y): (%{public}f,%{public}f). HorizontalAxis: %{public}f. VerticalAxis: %{public}f. "
235 "DeviceType: %{public}d. Time: %{public}lld",
236 event.x, event.y, event.horizontalAxis, event.verticalAxis, event.sourceType, pointerEvent->GetActionTime());
237 }
238
ConvertKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,KeyEvent & event)239 void ConvertKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent, KeyEvent& event)
240 {
241 event.code = static_cast<KeyCode>(keyEvent->GetKeyCode());
242 if (keyEvent->GetKeyAction() == OHOS::MMI::KeyEvent::KEY_ACTION_UP) {
243 event.action = KeyAction::UP;
244 } else if (keyEvent->GetKeyAction() == OHOS::MMI::KeyEvent::KEY_ACTION_DOWN) {
245 event.action = KeyAction::DOWN;
246 } else {
247 event.action = KeyAction::UNKNOWN;
248 }
249 std::chrono::microseconds microseconds(keyEvent->GetActionTime());
250 TimeStamp time(microseconds);
251 event.timeStamp = time;
252 event.key = KeyToString(static_cast<int32_t>(event.code));
253 event.deviceId = keyEvent->GetDeviceId();
254 event.sourceType = SourceType::KEYBOARD;
255 std::string pressedKeyStr = "Pressed Keys: ";
256 for (const auto& curCode : keyEvent->GetPressedKeys()) {
257 pressedKeyStr += (std::to_string(curCode) + " ");
258 event.pressedCodes.emplace_back(static_cast<KeyCode>(curCode));
259 }
260 LOGI("ConvertKeyEvent: KeyCode: %{private}d. KeyAction: %{public}d. PressedCodes: %{private}s. Time: %{public}lld",
261 event.code, event.action, pressedKeyStr.c_str(), (long long)(keyEvent->GetActionTime()));
262 }
263
LogPointInfo(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)264 void LogPointInfo(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
265 {
266 LOGI("point source: %{public}d", pointerEvent->GetSourceType());
267 auto actionId = pointerEvent->GetPointerId();
268 MMI::PointerEvent::PointerItem item;
269 if (pointerEvent->GetPointerItem(actionId, item)) {
270 LOGI("action point info: id: %{public}d, x: %{public}d, y: %{public}d, action: %{public}d", actionId,
271 item.GetLocalX(), item.GetLocalY(), pointerEvent->GetPointerAction());
272 }
273 auto ids = pointerEvent->GetPointersIdList();
274 for (auto&& id : ids) {
275 MMI::PointerEvent::PointerItem item;
276 if (pointerEvent->GetPointerItem(id, item)) {
277 LOGI("all point info: id: %{public}d, x: %{public}d, y: %{public}d, isPressed: %{public}d", actionId,
278 item.GetLocalX(), item.GetLocalY(), item.IsPressed());
279 }
280 }
281 }
282
283 } // namespace
284
CreateView(int32_t instanceId,bool useCurrentEventRunner,bool usePlatformThread)285 FlutterAceView* FlutterAceView::CreateView(int32_t instanceId, bool useCurrentEventRunner, bool usePlatformThread)
286 {
287 FlutterAceView* aceSurface = new Platform::FlutterAceView(instanceId);
288 flutter::Settings settings;
289 settings.instanceId = instanceId;
290 settings.platform = flutter::AcePlatform::ACE_PLATFORM_OHOS;
291 #ifndef GPU_DISABLED
292 settings.enable_software_rendering = false;
293 #else
294 settings.enable_software_rendering = true;
295 #endif
296 settings.platform_as_ui_thread = usePlatformThread;
297 settings.use_current_event_runner = useCurrentEventRunner;
298 LOGI("software render: %{public}s", settings.enable_software_rendering ? "true" : "false");
299 LOGI("use platform as ui thread: %{public}s", settings.platform_as_ui_thread ? "true" : "false");
300 settings.idle_notification_callback = [instanceId](int64_t deadline) {
301 ContainerScope scope(instanceId);
302 auto container = Container::Current();
303 if (!container) {
304 return;
305 }
306 auto context = container->GetPipelineContext();
307 if (!context) {
308 return;
309 }
310 context->GetTaskExecutor()->PostTask(
311 [context, deadline]() { context->OnIdle(deadline); }, TaskExecutor::TaskType::UI);
312 };
313 auto shell_holder = std::make_unique<flutter::OhosShellHolder>(settings, false);
314 if (aceSurface != nullptr) {
315 aceSurface->SetShellHolder(std::move(shell_holder));
316 }
317 return aceSurface;
318 }
319
SurfaceCreated(FlutterAceView * view,OHOS::sptr<OHOS::Rosen::Window> window)320 void FlutterAceView::SurfaceCreated(FlutterAceView* view, OHOS::sptr<OHOS::Rosen::Window> window)
321 {
322 LOGI(">>> FlutterAceView::SurfaceCreated, pWnd:%{public}p", &(*window));
323 if (window == nullptr) {
324 LOGE("FlutterAceView::SurfaceCreated, window is nullptr");
325 return;
326 }
327 if (view == nullptr) {
328 LOGE("FlutterAceView::SurfaceCreated, view is nullptr");
329 return;
330 }
331
332 auto platformView = view->GetShellHolder()->GetPlatformView();
333 LOGI("FlutterAceView::SurfaceCreated, GetPlatformView");
334 if (platformView && !SystemProperties::GetRosenBackendEnabled()) {
335 LOGI("FlutterAceView::SurfaceCreated, call NotifyCreated");
336 platformView->NotifyCreated(window);
337 }
338
339 LOGI("<<< FlutterAceView::SurfaceCreated, end");
340 }
341
SurfaceChanged(FlutterAceView * view,int32_t width,int32_t height,int32_t orientation,WindowSizeChangeReason type)342 void FlutterAceView::SurfaceChanged(
343 FlutterAceView* view, int32_t width, int32_t height, int32_t orientation, WindowSizeChangeReason type)
344 {
345 if (view == nullptr) {
346 LOGE("FlutterAceView::SurfaceChanged, view is nullptr");
347 return;
348 }
349
350 view->NotifySurfaceChanged(width, height, type);
351 auto platformView = view->GetShellHolder()->GetPlatformView();
352 LOGI("FlutterAceView::SurfaceChanged, GetPlatformView");
353 if (platformView) {
354 LOGI("FlutterAceView::SurfaceChanged, call NotifyChanged");
355 platformView->NotifyChanged(SkISize::Make(width, height));
356 }
357 LOGI("<<< FlutterAceView::SurfaceChanged, end");
358 }
359
SetViewportMetrics(FlutterAceView * view,const flutter::ViewportMetrics & metrics)360 void FlutterAceView::SetViewportMetrics(FlutterAceView* view, const flutter::ViewportMetrics& metrics)
361 {
362 if (view) {
363 view->NotifyDensityChanged(metrics.device_pixel_ratio);
364 view->NotifySystemBarHeightChanged(metrics.physical_padding_top, metrics.physical_view_inset_bottom);
365 auto platformView = view->GetShellHolder()->GetPlatformView();
366 if (platformView) {
367 platformView->SetViewportMetrics(metrics);
368 }
369 }
370 }
371
DispatchTouchEvent(FlutterAceView * view,const std::shared_ptr<MMI::PointerEvent> & pointerEvent)372 void FlutterAceView::DispatchTouchEvent(FlutterAceView* view, const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
373 {
374 LogPointInfo(pointerEvent);
375 if (pointerEvent->GetSourceType() == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
376 // mouse event
377 if (pointerEvent->GetPointerAction() >= MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN &&
378 pointerEvent->GetPointerAction() <= MMI::PointerEvent::POINTER_ACTION_AXIS_END) {
379 LOGD("ProcessAxisEvent");
380 view->ProcessAxisEvent(pointerEvent);
381 } else {
382 LOGD("ProcessMouseEvent");
383 view->ProcessMouseEvent(pointerEvent);
384 }
385 } else {
386 // touch event
387 LOGD("ProcessTouchEvent");
388 view->ProcessTouchEvent(pointerEvent);
389 }
390 pointerEvent->MarkProcessed();
391 }
392
DispatchKeyEvent(FlutterAceView * view,const std::shared_ptr<MMI::KeyEvent> & keyEvent)393 bool FlutterAceView::DispatchKeyEvent(FlutterAceView* view, const std::shared_ptr<MMI::KeyEvent>& keyEvent)
394 {
395 if (view != nullptr) {
396 return view->ProcessKeyEvent(keyEvent);
397 }
398 LOGE("view is null, return false!");
399 return false;
400 }
401
DispatchRotationEvent(FlutterAceView * view,float rotationValue)402 bool FlutterAceView::DispatchRotationEvent(FlutterAceView* view, float rotationValue)
403 {
404 if (view) {
405 return view->ProcessRotationEvent(rotationValue);
406 }
407 LOGE("view is null, return false!");
408 return false;
409 }
410
RegisterTouchEventCallback(TouchEventCallback && callback)411 void FlutterAceView::RegisterTouchEventCallback(TouchEventCallback&& callback)
412 {
413 ACE_DCHECK(callback);
414 touchEventCallback_ = std::move(callback);
415 }
416
RegisterDragEventCallback(DragEventCallBack && callback)417 void FlutterAceView::RegisterDragEventCallback(DragEventCallBack&& callback)
418 {
419 ACE_DCHECK(callback);
420 dragEventCallback_ = std::move(callback);
421 }
422
RegisterKeyEventCallback(KeyEventCallback && callback)423 void FlutterAceView::RegisterKeyEventCallback(KeyEventCallback&& callback)
424 {
425 ACE_DCHECK(callback);
426 keyEventCallback_ = std::move(callback);
427 }
428
RegisterMouseEventCallback(MouseEventCallback && callback)429 void FlutterAceView::RegisterMouseEventCallback(MouseEventCallback&& callback)
430 {
431 ACE_DCHECK(callback);
432 mouseEventCallback_ = std::move(callback);
433 }
434
RegisterAxisEventCallback(AxisEventCallback && callback)435 void FlutterAceView::RegisterAxisEventCallback(AxisEventCallback&& callback)
436 {
437 ACE_DCHECK(callback);
438 axisEventCallback_ = std::move(callback);
439 }
440
RegisterRotationEventCallback(RotationEventCallBack && callback)441 void FlutterAceView::RegisterRotationEventCallback(RotationEventCallBack&& callback)
442 {
443 ACE_DCHECK(callback);
444 rotationEventCallBack_ = std::move(callback);
445 }
446
Launch()447 void FlutterAceView::Launch()
448 {
449 LOGD("Launch shell holder.");
450 if (!viewLaunched_) {
451 flutter::RunConfiguration config;
452 shell_holder_->Launch(std::move(config));
453 viewLaunched_ = true;
454 }
455 }
456
SetShellHolder(std::unique_ptr<flutter::OhosShellHolder> holder)457 void FlutterAceView::SetShellHolder(std::unique_ptr<flutter::OhosShellHolder> holder)
458 {
459 shell_holder_ = std::move(holder);
460 }
461
ProcessTouchEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)462 void FlutterAceView::ProcessTouchEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
463 {
464 TouchEvent touchPoint = ConvertTouchEvent(pointerEvent);
465 if (touchPoint.type != TouchType::UNKNOWN) {
466 if (touchEventCallback_) {
467 touchEventCallback_(touchPoint);
468 }
469 } else {
470 LOGW("Unknown event.");
471 }
472 }
473
ProcessDragEvent(int32_t x,int32_t y,const DragEventAction & action)474 void FlutterAceView::ProcessDragEvent(int32_t x, int32_t y, const DragEventAction& action)
475 {
476 LOGD("ProcessDragEvent");
477
478 if (dragEventCallback_) {
479 dragEventCallback_(x, y, action);
480 }
481 }
482
ProcessMouseEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)483 void FlutterAceView::ProcessMouseEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
484 {
485 MouseEvent event;
486 ConvertMouseEvent(pointerEvent, event);
487
488 if (mouseEventCallback_) {
489 mouseEventCallback_(event);
490 }
491 }
492
ProcessAxisEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)493 void FlutterAceView::ProcessAxisEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
494 {
495 AxisEvent event;
496 ConvertAxisEvent(pointerEvent, event);
497
498 if (axisEventCallback_) {
499 axisEventCallback_(event);
500 }
501 }
502
ProcessKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent)503 bool FlutterAceView::ProcessKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
504 {
505 if (!keyEventCallback_) {
506 return false;
507 }
508 KeyEvent event;
509 ConvertKeyEvent(keyEvent, event);
510 return keyEventCallback_(event);
511 }
512
GetNativeWindowById(uint64_t textureId)513 const void* FlutterAceView::GetNativeWindowById(uint64_t textureId)
514 {
515 return nullptr;
516 }
517
ProcessRotationEvent(float rotationValue)518 bool FlutterAceView::ProcessRotationEvent(float rotationValue)
519 {
520 if (!rotationEventCallBack_) {
521 return false;
522 }
523
524 RotationEvent event { .value = rotationValue * ROTATION_DIVISOR };
525
526 return rotationEventCallBack_(event);
527 }
528
Dump(const std::vector<std::string> & params)529 bool FlutterAceView::Dump(const std::vector<std::string>& params)
530 {
531 if (params.empty() || params[0] != "-drawcmd") {
532 LOGE("Unsupported parameters.");
533 return false;
534 }
535 #ifdef DUMP_DRAW_CMD
536 if (shell_holder_) {
537 auto screenShot = shell_holder_->Screenshot(flutter::Rasterizer::ScreenshotType::SkiaPicture, false);
538 if (screenShot.data->data() != nullptr) {
539 auto byteData = screenShot.data;
540 static int32_t count = 0;
541 auto path = ImageCache::GetImageCacheFilePath() + "/picture_" + std::to_string(count++) + ".mskp";
542 if (DumpLog::GetInstance().GetDumpFile()) {
543 DumpLog::GetInstance().AddDesc("Dump draw command to path: " + path);
544 DumpLog::GetInstance().Print(0, "Info:", 0);
545 }
546 std::ofstream outFile(path, std::fstream::out | std::fstream::binary);
547 if (!outFile.is_open()) {
548 LOGE("Open file %{private}s failed.", path.c_str());
549 return false;
550 }
551 outFile.write(reinterpret_cast<const char*>(byteData->data()), byteData->size());
552 outFile.close();
553 return true;
554 }
555 }
556 #else
557 if (DumpLog::GetInstance().GetDumpFile()) {
558 DumpLog::GetInstance().AddDesc("Dump draw command not support on this version.");
559 DumpLog::GetInstance().Print(0, "Info:", 0);
560 return true;
561 }
562 #endif
563 return false;
564 }
565
InitCacheFilePath(const std::string & path)566 void FlutterAceView::InitCacheFilePath(const std::string& path)
567 {
568 if (!path.empty()) {
569 ImageCache::SetImageCacheFilePath(path);
570 ImageCache::SetCacheFileInfo();
571 } else {
572 LOGW("image cache path empty");
573 }
574 }
575
IsLastPage() const576 bool FlutterAceView::IsLastPage() const
577 {
578 auto container = AceEngine::Get().GetContainer(instanceId_);
579 if (!container) {
580 return false;
581 }
582
583 ContainerScope scope(instanceId_);
584 auto context = container->GetPipelineContext();
585 if (!context) {
586 return false;
587 }
588 auto taskExecutor = context->GetTaskExecutor();
589
590 bool isLastPage = false;
591 if (taskExecutor) {
592 taskExecutor->PostSyncTask(
593 [context, &isLastPage]() { isLastPage = context->IsLastPage(); }, TaskExecutor::TaskType::UI);
594 }
595 return isLastPage;
596 }
597
GetBackgroundColor()598 uint32_t FlutterAceView::GetBackgroundColor()
599 {
600 return Color::WHITE.GetValue();
601 }
602
603 // On watch device, it's probable to quit the application unexpectedly when we slide our finger diagonally upward on the
604 // screen, so we do restrictions here.
IsNeedForbidToPlatform(TouchEvent point)605 bool FlutterAceView::IsNeedForbidToPlatform(TouchEvent point)
606 {
607 if (point.type == TouchType::DOWN) {
608 auto result = touchPointInfoMap_.try_emplace(point.id, TouchPointInfo(point.GetOffset()));
609 if (!result.second) {
610 result.first->second = TouchPointInfo(point.GetOffset());
611 }
612
613 return false;
614 }
615
616 auto iter = touchPointInfoMap_.find(point.id);
617 if (iter == touchPointInfoMap_.end()) {
618 return false;
619 }
620 if (iter->second.eventState_ == EventState::HORIZONTAL_STATE) {
621 return false;
622 } else if (iter->second.eventState_ == EventState::VERTICAL_STATE) {
623 return true;
624 }
625
626 Offset offset = point.GetOffset() - iter->second.offset_;
627 double deltaX = offset.GetX();
628 double deltaY = std::abs(offset.GetY());
629
630 if (point.type == TouchType::MOVE) {
631 if (deltaX > 0.0) {
632 if (deltaY / deltaX > PERMIT_ANGLE_VALUE) {
633 iter->second.eventState_ = EventState::VERTICAL_STATE;
634 return true;
635 } else {
636 iter->second.eventState_ = EventState::HORIZONTAL_STATE;
637 }
638 }
639
640 return false;
641 }
642
643 touchPointInfoMap_.erase(point.id);
644 return deltaX > 0.0 && deltaY / deltaX > PERMIT_ANGLE_VALUE;
645 }
646
GetDrawDelegate()647 std::unique_ptr<DrawDelegate> FlutterAceView::GetDrawDelegate()
648 {
649 auto darwDelegate = std::make_unique<DrawDelegate>();
650
651 darwDelegate->SetDrawFrameCallback([this](RefPtr<Flutter::Layer>& layer, const Rect& dirty) {
652 if (!layer) {
653 LOGE("layer is nullptr");
654 return;
655 }
656 RefPtr<Flutter::FlutterSceneBuilder> flutterSceneBuilder = AceType::MakeRefPtr<Flutter::FlutterSceneBuilder>();
657 layer->AddToScene(*flutterSceneBuilder, 0.0, 0.0);
658 auto scene = flutterSceneBuilder->Build();
659 if (!flutter::UIDartState::Current()) {
660 LOGE("uiDartState is nullptr");
661 return;
662 }
663 auto window = flutter::UIDartState::Current()->window();
664 if (window != nullptr && window->client() != nullptr) {
665 window->client()->Render(scene.get());
666 } else {
667 LOGE("window is nullptr");
668 }
669 });
670
671 return darwDelegate;
672 }
673
GetPlatformWindow()674 std::unique_ptr<PlatformWindow> FlutterAceView::GetPlatformWindow()
675 {
676 return nullptr;
677 }
678
679 } // namespace OHOS::Ace::Platform