• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ohos/entrance/ace_view_ohos.h"
17 
18 #include <memory>
19 
20 #include "flutter/fml/message_loop.h"
21 #include "flutter/shell/platform/ohos/platform_task_runner_adapter.h"
22 
23 #include "adapter/ohos/entrance/ace_container.h"
24 #include "adapter/ohos/entrance/mmi_event_convertor.h"
25 #include "base/log/ace_trace.h"
26 #include "base/log/dump_log.h"
27 #include "base/log/event_report.h"
28 #include "base/log/log.h"
29 #include "base/utils/macros.h"
30 #include "base/utils/system_properties.h"
31 #include "base/utils/utils.h"
32 #include "core/common/ace_engine.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 
39 namespace OHOS::Ace::Platform {
40 namespace {
41 
42 constexpr int32_t ROTATION_DIVISOR = 64;
43 
44 } // namespace
45 
CreateView(int32_t instanceId,bool useCurrentEventRunner,bool usePlatformThread)46 AceViewOhos* AceViewOhos::CreateView(int32_t instanceId, bool useCurrentEventRunner, bool usePlatformThread)
47 {
48     auto* aceView = new AceViewOhos(instanceId, FlutterThreadModel::CreateThreadModel(useCurrentEventRunner,
49                                                     !usePlatformThread, !SystemProperties::GetRosenBackendEnabled()));
50     if (aceView != nullptr) {
51         aceView->IncRefCount();
52     }
53     return aceView;
54 }
55 
AceViewOhos(int32_t id,std::unique_ptr<FlutterThreadModel> threadModel)56 AceViewOhos::AceViewOhos(int32_t id, std::unique_ptr<FlutterThreadModel> threadModel)
57     : instanceId_(id), threadModel_(std::move(threadModel))
58 {}
59 
SurfaceCreated(AceViewOhos * view,OHOS::sptr<OHOS::Rosen::Window> window)60 void AceViewOhos::SurfaceCreated(AceViewOhos* view, OHOS::sptr<OHOS::Rosen::Window> window)
61 {
62     CHECK_NULL_VOID(window);
63     CHECK_NULL_VOID(view);
64 }
65 
SurfaceChanged(AceViewOhos * view,int32_t width,int32_t height,int32_t orientation,WindowSizeChangeReason type,const std::shared_ptr<Rosen::RSTransaction> & rsTransaction)66 void AceViewOhos::SurfaceChanged(AceViewOhos* view, int32_t width, int32_t height, int32_t orientation,
67     WindowSizeChangeReason type, const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)
68 {
69     CHECK_NULL_VOID(view);
70 
71     view->NotifySurfaceChanged(width, height, type, rsTransaction);
72 
73     auto instanceId = view->GetInstanceId();
74     auto container = Platform::AceContainer::GetContainer(instanceId);
75     if (container) {
76         auto pipelineContext = container->GetPipelineContext();
77         CHECK_NULL_VOID(pipelineContext);
78         pipelineContext->HideOverlays();
79     }
80 }
81 
SurfacePositionChanged(AceViewOhos * view,int32_t posX,int32_t posY)82 void AceViewOhos::SurfacePositionChanged(AceViewOhos* view, int32_t posX, int32_t posY)
83 {
84     CHECK_NULL_VOID_NOLOG(view);
85     view->NotifySurfacePositionChanged(posX, posY);
86 }
87 
SetViewportMetrics(AceViewOhos * view,const ViewportConfig & config)88 void AceViewOhos::SetViewportMetrics(AceViewOhos* view, const ViewportConfig& config)
89 {
90     CHECK_NULL_VOID_NOLOG(view);
91     view->NotifyDensityChanged(config.Density());
92 }
93 
DispatchTouchEvent(AceViewOhos * view,const std::shared_ptr<MMI::PointerEvent> & pointerEvent)94 void AceViewOhos::DispatchTouchEvent(AceViewOhos* view, const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
95 {
96     CHECK_NULL_VOID_NOLOG(view);
97     CHECK_NULL_VOID(pointerEvent);
98     LogPointInfo(pointerEvent);
99     DispatchEventToPerf(pointerEvent);
100     int32_t pointerAction = pointerEvent->GetPointerAction();
101     if (pointerEvent->GetSourceType() == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
102         // mouse event
103         if (pointerAction >= MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN &&
104             pointerAction <= MMI::PointerEvent::POINTER_ACTION_AXIS_END) {
105             LOGD("ProcessAxisEvent");
106             view->ProcessAxisEvent(pointerEvent);
107         } else {
108             LOGD("ProcessMouseEvent");
109 #ifdef ENABLE_DRAG_FRAMEWORK
110             view->ProcessDragEvent(pointerEvent);
111 #endif // ENABLE_DRAG_FRAMEWORK
112             view->ProcessMouseEvent(pointerEvent);
113         }
114     } else {
115         // touch event
116         LOGD("ProcessTouchEvent");
117 #ifdef ENABLE_DRAG_FRAMEWORK
118         view->ProcessDragEvent(pointerEvent);
119 #endif // ENABLE_DRAG_FRAMEWORK
120         int32_t instanceId = view->GetInstanceId();
121         auto container = Platform::AceContainer::GetContainer(instanceId);
122         CHECK_NULL_VOID(container);
123         if (container->IsScenceBoardWindow() &&
124             (pointerAction == MMI::PointerEvent::POINTER_ACTION_PULL_MOVE ||
125             pointerAction == MMI::PointerEvent::POINTER_ACTION_PULL_UP)) {
126             view->ProcessMouseEvent(pointerEvent);
127         } else {
128             view->ProcessTouchEvent(pointerEvent);
129         }
130     }
131 }
132 
DispatchEventToPerf(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)133 void AceViewOhos::DispatchEventToPerf(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
134 {
135     CHECK_NULL_VOID(pointerEvent);
136     static bool isFirstMove = false;
137     PerfMonitor* pMonitor = PerfMonitor::GetPerfMonitor();
138     if (pMonitor == nullptr) {
139         return;
140     }
141     int64_t inputTime = pointerEvent->GetSensorInputTime() * US_TO_MS;
142     if (inputTime <= 0) {
143         inputTime = pointerEvent->GetActionTime() * US_TO_MS;
144     }
145     if (inputTime <= 0) {
146         return;
147     }
148     PerfActionType inputType = UNKNOWN_ACTION;
149     PerfSourceType sourceType = UNKNOWN_SOURCE;
150     if (pointerEvent->GetSourceType() == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
151         sourceType = PERF_MOUSE_EVENT;
152     } else if (pointerEvent->GetSourceType() == MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
153         sourceType = PERF_TOUCH_EVENT;
154     } else if (pointerEvent->GetSourceType() == MMI::PointerEvent::SOURCE_TYPE_TOUCHPAD) {
155         sourceType = PERF_TOUCH_PAD;
156     } else if (pointerEvent->GetSourceType() == MMI::PointerEvent::SOURCE_TYPE_JOYSTICK) {
157         sourceType = PERF_JOY_STICK;
158     }
159     int32_t pointerAction = pointerEvent->GetPointerAction();
160     if (pointerAction == MMI::PointerEvent::POINTER_ACTION_DOWN) {
161         inputType = LAST_DOWN;
162         isFirstMove = true;
163     } else if (pointerAction == MMI::PointerEvent::POINTER_ACTION_UP) {
164         inputType = LAST_UP;
165         isFirstMove = false;
166     } else if (isFirstMove && pointerAction == MMI::PointerEvent::POINTER_ACTION_MOVE) {
167         inputType = FIRST_MOVE;
168         isFirstMove = false;
169     }
170     pMonitor->RecordInputEvent(inputType, sourceType, inputTime);
171 }
172 
DispatchEventToPerf(const std::shared_ptr<MMI::KeyEvent> & keyEvent)173 void AceViewOhos::DispatchEventToPerf(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
174 {
175     CHECK_NULL_VOID(keyEvent);
176     int32_t keyCode = keyEvent->GetKeyCode();
177     if (keyCode != MMI::KeyEvent::KEYCODE_VOLUME_DOWN
178         && keyCode != MMI::KeyEvent::KEYCODE_VOLUME_UP
179         && keyCode != MMI::KeyEvent::KEYCODE_POWER) {
180         return;
181     }
182     PerfMonitor* pMonitor = PerfMonitor::GetPerfMonitor();
183     if (pMonitor == nullptr) {
184         return;
185     }
186     PerfActionType inputType = UNKNOWN_ACTION;
187     int32_t action = keyEvent->GetKeyAction();
188     if (action == MMI::KeyEvent::KEY_ACTION_UP) {
189         inputType = LAST_UP;
190     } else if (action == MMI::KeyEvent::KEY_ACTION_DOWN) {
191         inputType = LAST_DOWN;
192     }
193     PerfSourceType sourceType = PERF_KEY_EVENT;
194     int64_t inputTime = (keyEvent->GetKeyItem())->GetDownTime() * US_TO_MS;
195     pMonitor->RecordInputEvent(inputType, sourceType, inputTime);
196 }
197 
DispatchKeyEvent(AceViewOhos * view,const std::shared_ptr<MMI::KeyEvent> & keyEvent)198 bool AceViewOhos::DispatchKeyEvent(AceViewOhos* view, const std::shared_ptr<MMI::KeyEvent>& keyEvent)
199 {
200     CHECK_NULL_RETURN(view, false);
201     DispatchEventToPerf(keyEvent);
202     return view->ProcessKeyEvent(keyEvent);
203 }
204 
DispatchRotationEvent(AceViewOhos * view,float rotationValue)205 bool AceViewOhos::DispatchRotationEvent(AceViewOhos* view, float rotationValue)
206 {
207     CHECK_NULL_RETURN(view, false);
208     return view->ProcessRotationEvent(rotationValue);
209 }
210 
RegisterTouchEventCallback(TouchEventCallback && callback)211 void AceViewOhos::RegisterTouchEventCallback(TouchEventCallback&& callback)
212 {
213     ACE_DCHECK(callback);
214     touchEventCallback_ = std::move(callback);
215 }
216 
RegisterDragEventCallback(DragEventCallBack && callback)217 void AceViewOhos::RegisterDragEventCallback(DragEventCallBack&& callback)
218 {
219     ACE_DCHECK(callback);
220     dragEventCallback_ = std::move(callback);
221 }
222 
RegisterKeyEventCallback(KeyEventCallback && callback)223 void AceViewOhos::RegisterKeyEventCallback(KeyEventCallback&& callback)
224 {
225     ACE_DCHECK(callback);
226     keyEventCallback_ = std::move(callback);
227 }
228 
RegisterMouseEventCallback(MouseEventCallback && callback)229 void AceViewOhos::RegisterMouseEventCallback(MouseEventCallback&& callback)
230 {
231     ACE_DCHECK(callback);
232     mouseEventCallback_ = std::move(callback);
233 }
234 
RegisterAxisEventCallback(AxisEventCallback && callback)235 void AceViewOhos::RegisterAxisEventCallback(AxisEventCallback&& callback)
236 {
237     ACE_DCHECK(callback);
238     axisEventCallback_ = std::move(callback);
239 }
240 
RegisterRotationEventCallback(RotationEventCallBack && callback)241 void AceViewOhos::RegisterRotationEventCallback(RotationEventCallBack&& callback)
242 {
243     ACE_DCHECK(callback);
244     rotationEventCallBack_ = std::move(callback);
245 }
246 
Launch()247 void AceViewOhos::Launch()
248 {
249     LOGD("Launch shell holder.");
250 }
251 
ProcessTouchEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)252 void AceViewOhos::ProcessTouchEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
253 {
254     CHECK_NULL_VOID_NOLOG(pointerEvent);
255     TouchEvent touchPoint = ConvertTouchEvent(pointerEvent);
256     if (SystemProperties::GetDebugEnabled()) {
257         ACE_SCOPED_TRACE("ProcessTouchEvent pointX=%f pointY=%f type=%d timeStamp=%lld id=%d", touchPoint.x,
258             touchPoint.y, (int)touchPoint.type, touchPoint.time.time_since_epoch().count(), touchPoint.id);
259     }
260     auto markProcess = [pointerEvent]() {
261         CHECK_NULL_VOID_NOLOG(pointerEvent);
262         LOGD("Mark %{public}d id Touch Event Processed", pointerEvent->GetPointerId());
263         pointerEvent->MarkProcessed();
264     };
265     if (touchPoint.type != TouchType::UNKNOWN) {
266         if (touchEventCallback_) {
267             touchEventCallback_(touchPoint, markProcess);
268         }
269     } else {
270         LOGW("Unknown event.");
271     }
272 }
273 
274 #ifdef ENABLE_DRAG_FRAMEWORK
ProcessDragEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)275 void AceViewOhos::ProcessDragEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
276 {
277     MMI::PointerEvent::PointerItem pointerItem;
278     pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem);
279     DragEventAction action;
280     int32_t orgAction = pointerEvent->GetPointerAction();
281     switch (orgAction) {
282         case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_MOVE: {
283             action = DragEventAction::DRAG_EVENT_MOVE;
284             ProcessDragEvent(pointerItem.GetWindowX(), pointerItem.GetWindowY(), action);
285             break;
286         }
287         case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_UP: {
288             action = DragEventAction::DRAG_EVENT_END;
289             ProcessDragEvent(pointerItem.GetWindowX(), pointerItem.GetWindowY(), action);
290             break;
291         }
292         case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_IN_WINDOW: {
293             action = DragEventAction::DRAG_EVENT_START;
294             ProcessDragEvent(pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), action);
295             break;
296         }
297         case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW: {
298             action = DragEventAction::DRAG_EVENT_OUT;
299             ProcessDragEvent(pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), action);
300             break;
301         }
302         default:
303             LOGD("unknown type %{public}d", orgAction);
304             break;
305     }
306 }
307 #endif // ENABLE_DRAG_FRAMEWORK
308 
ProcessDragEvent(int32_t x,int32_t y,const DragEventAction & action)309 void AceViewOhos::ProcessDragEvent(int32_t x, int32_t y, const DragEventAction& action)
310 {
311     LOGD("ProcessDragEvent");
312     CHECK_NULL_VOID_NOLOG(dragEventCallback_);
313     dragEventCallback_(x, y, action);
314 }
315 
ProcessMouseEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)316 void AceViewOhos::ProcessMouseEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
317 {
318     MouseEvent event;
319     if (pointerEvent) {
320         auto container = Platform::AceContainer::GetContainer(instanceId_);
321         CHECK_NULL_VOID(container);
322         ConvertMouseEvent(pointerEvent, event, container->IsScenceBoardWindow());
323     }
324     auto markProcess = [pointerEvent]() {
325         CHECK_NULL_VOID_NOLOG(pointerEvent);
326         LOGD("Mark %{public}d id Mouse Event Processed", pointerEvent->GetPointerId());
327         pointerEvent->MarkProcessed();
328     };
329 
330     CHECK_NULL_VOID_NOLOG(mouseEventCallback_);
331     mouseEventCallback_(event, markProcess);
332 }
333 
ProcessAxisEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)334 void AceViewOhos::ProcessAxisEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
335 {
336     AxisEvent event;
337     if (pointerEvent) {
338         ConvertAxisEvent(pointerEvent, event);
339     }
340     auto markProcess = [pointerEvent]() {
341         CHECK_NULL_VOID_NOLOG(pointerEvent);
342         LOGD("Mark %{public}d id Axis Event Processed", pointerEvent->GetPointerId());
343         pointerEvent->MarkProcessed();
344     };
345 
346     CHECK_NULL_VOID_NOLOG(axisEventCallback_);
347     axisEventCallback_(event, markProcess);
348 }
349 
ProcessKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent)350 bool AceViewOhos::ProcessKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
351 {
352     CHECK_NULL_RETURN_NOLOG(keyEventCallback_, false);
353     KeyEvent event;
354     ConvertKeyEvent(keyEvent, event);
355     return keyEventCallback_(event);
356 }
357 
GetNativeWindowById(uint64_t textureId)358 const void* AceViewOhos::GetNativeWindowById(uint64_t textureId)
359 {
360     return nullptr;
361 }
362 
ProcessRotationEvent(float rotationValue)363 bool AceViewOhos::ProcessRotationEvent(float rotationValue)
364 {
365     CHECK_NULL_RETURN_NOLOG(rotationEventCallBack_, false);
366     RotationEvent event { .value = rotationValue * ROTATION_DIVISOR };
367     return rotationEventCallBack_(event);
368 }
369 
Dump(const std::vector<std::string> & params)370 bool AceViewOhos::Dump(const std::vector<std::string>& params)
371 {
372     if (params.empty() || params[0] != "-drawcmd") {
373         LOGE("Unsupported parameters.");
374         return false;
375     }
376     if (DumpLog::GetInstance().GetDumpFile()) {
377         DumpLog::GetInstance().AddDesc("Dump draw command not support on this version.");
378         DumpLog::GetInstance().Print(0, "Info:", 0);
379         return true;
380     }
381     return false;
382 }
383 
GetBackgroundColor()384 uint32_t AceViewOhos::GetBackgroundColor()
385 {
386     return Color::WHITE.GetValue();
387 }
388 
389 } // namespace OHOS::Ace::Platform
390