Flutter Engine
pointer_data_packet_converter.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/lib/ui/window/pointer_data_packet_converter.h"
6 
7 #include <cstring>
8 
9 #include "flutter/fml/logging.h"
10 
11 namespace flutter {
12 
14 
16 
17 std::unique_ptr<PointerDataPacket> PointerDataPacketConverter::Convert(
18  std::unique_ptr<PointerDataPacket> packet) {
19  size_t kBytesPerPointerData = kPointerDataFieldCount * kBytesPerField;
20  auto buffer = packet->data();
21  size_t buffer_length = buffer.size();
22 
23  std::vector<PointerData> converted_pointers;
24  // Converts each pointer data in the buffer and stores it in the
25  // converted_pointers.
26  for (size_t i = 0; i < buffer_length / kBytesPerPointerData; i++) {
27  PointerData pointer_data;
28  memcpy(&pointer_data, &buffer[i * kBytesPerPointerData],
29  sizeof(PointerData));
30  ConvertPointerData(pointer_data, converted_pointers);
31  }
32 
33  // Writes converted_pointers into converted_packet.
34  auto converted_packet =
35  std::make_unique<flutter::PointerDataPacket>(converted_pointers.size());
36  size_t count = 0;
37  for (auto& converted_pointer : converted_pointers) {
38  converted_packet->SetPointerData(count++, converted_pointer);
39  }
40 
41  return converted_packet;
42 }
43 
44 void PointerDataPacketConverter::ConvertPointerData(
45  PointerData pointer_data,
46  std::vector<PointerData>& converted_pointers) {
47  if (pointer_data.signal_kind == PointerData::SignalKind::kNone) {
48  switch (pointer_data.change) {
50  // Android's three finger gesture will send a cancel event
51  // to a non-existing pointer. Drops the cancel if pointer
52  // is not previously added.
53  // https://github.com/flutter/flutter/issues/20517
54  auto iter = states_.find(pointer_data.device);
55  if (iter != states_.end()) {
56  PointerState state = iter->second;
57  FML_DCHECK(state.isDown);
58  UpdatePointerIdentifier(pointer_data, state, false);
59 
60  if (LocationNeedsUpdate(pointer_data, state)) {
61  // Synthesizes a move event if the location does not match.
62  PointerData synthesized_move_event = pointer_data;
63  synthesized_move_event.change = PointerData::Change::kMove;
64  synthesized_move_event.synthesized = 1;
65 
66  UpdateDeltaAndState(synthesized_move_event, state);
67  converted_pointers.push_back(synthesized_move_event);
68  }
69 
70  state.isDown = false;
71  states_[pointer_data.device] = state;
72  converted_pointers.push_back(pointer_data);
73  }
74  break;
75  }
77  FML_DCHECK(states_.find(pointer_data.device) == states_.end());
78  EnsurePointerState(pointer_data);
79  converted_pointers.push_back(pointer_data);
80  break;
81  }
83  // Makes sure we have an existing pointer
84  auto iter = states_.find(pointer_data.device);
85  FML_DCHECK(iter != states_.end());
86  PointerState state = iter->second;
87 
88  if (state.isDown) {
89  // Synthesizes cancel event if the pointer is down.
90  PointerData synthesized_cancel_event = pointer_data;
91  synthesized_cancel_event.change = PointerData::Change::kCancel;
92  synthesized_cancel_event.synthesized = 1;
93  UpdatePointerIdentifier(synthesized_cancel_event, state, false);
94 
95  state.isDown = false;
96  states_[synthesized_cancel_event.device] = state;
97  converted_pointers.push_back(synthesized_cancel_event);
98  }
99 
100  if (LocationNeedsUpdate(pointer_data, state)) {
101  // Synthesizes a hover event if the location does not match.
102  PointerData synthesized_hover_event = pointer_data;
103  synthesized_hover_event.change = PointerData::Change::kHover;
104  synthesized_hover_event.synthesized = 1;
105 
106  UpdateDeltaAndState(synthesized_hover_event, state);
107  converted_pointers.push_back(synthesized_hover_event);
108  }
109 
110  states_.erase(pointer_data.device);
111  converted_pointers.push_back(pointer_data);
112  break;
113  }
115  auto iter = states_.find(pointer_data.device);
116  PointerState state;
117  if (iter == states_.end()) {
118  // Synthesizes add event if the pointer is not previously added.
119  PointerData synthesized_add_event = pointer_data;
120  synthesized_add_event.change = PointerData::Change::kAdd;
121  synthesized_add_event.synthesized = 1;
122  state = EnsurePointerState(synthesized_add_event);
123  converted_pointers.push_back(synthesized_add_event);
124  } else {
125  state = iter->second;
126  }
127 
128  FML_DCHECK(!state.isDown);
129  if (LocationNeedsUpdate(pointer_data, state)) {
130  UpdateDeltaAndState(pointer_data, state);
131  converted_pointers.push_back(pointer_data);
132  }
133  break;
134  }
136  auto iter = states_.find(pointer_data.device);
137  PointerState state;
138  if (iter == states_.end()) {
139  // Synthesizes a add event if the pointer is not previously added.
140  PointerData synthesized_add_event = pointer_data;
141  synthesized_add_event.change = PointerData::Change::kAdd;
142  synthesized_add_event.synthesized = 1;
143  state = EnsurePointerState(synthesized_add_event);
144  converted_pointers.push_back(synthesized_add_event);
145  } else {
146  state = iter->second;
147  }
148 
149  FML_DCHECK(!state.isDown);
150  if (LocationNeedsUpdate(pointer_data, state)) {
151  // Synthesizes a hover event if the location does not match.
152  PointerData synthesized_hover_event = pointer_data;
153  synthesized_hover_event.change = PointerData::Change::kHover;
154  synthesized_hover_event.synthesized = 1;
155 
156  UpdateDeltaAndState(synthesized_hover_event, state);
157  converted_pointers.push_back(synthesized_hover_event);
158  }
159 
160  UpdatePointerIdentifier(pointer_data, state, true);
161  state.isDown = true;
162  states_[pointer_data.device] = state;
163  converted_pointers.push_back(pointer_data);
164  break;
165  }
167  // Makes sure we have an existing pointer in down state
168  auto iter = states_.find(pointer_data.device);
169  FML_DCHECK(iter != states_.end());
170  PointerState state = iter->second;
171  FML_DCHECK(state.isDown);
172 
173  UpdatePointerIdentifier(pointer_data, state, false);
174  UpdateDeltaAndState(pointer_data, state);
175  converted_pointers.push_back(pointer_data);
176  break;
177  }
179  // Makes sure we have an existing pointer in down state
180  auto iter = states_.find(pointer_data.device);
181  FML_DCHECK(iter != states_.end());
182  PointerState state = iter->second;
183  FML_DCHECK(state.isDown);
184 
185  UpdatePointerIdentifier(pointer_data, state, false);
186 
187  if (LocationNeedsUpdate(pointer_data, state)) {
188  // Synthesizes a move event if the location does not match.
189  PointerData synthesized_move_event = pointer_data;
190  synthesized_move_event.change = PointerData::Change::kMove;
191  synthesized_move_event.synthesized = 1;
192 
193  UpdateDeltaAndState(synthesized_move_event, state);
194  converted_pointers.push_back(synthesized_move_event);
195  }
196 
197  state.isDown = false;
198  states_[pointer_data.device] = state;
199  converted_pointers.push_back(pointer_data);
200  break;
201  }
202  default: {
203  converted_pointers.push_back(pointer_data);
204  break;
205  }
206  }
207  } else {
208  switch (pointer_data.signal_kind) {
210  // Makes sure we have an existing pointer
211  auto iter = states_.find(pointer_data.device);
212  FML_DCHECK(iter != states_.end());
213 
214  PointerState state = iter->second;
215  if (LocationNeedsUpdate(pointer_data, state)) {
216  if (state.isDown) {
217  // Synthesizes a move event if the pointer is down.
218  PointerData synthesized_move_event = pointer_data;
219  synthesized_move_event.signal_kind = PointerData::SignalKind::kNone;
220  synthesized_move_event.change = PointerData::Change::kMove;
221  synthesized_move_event.synthesized = 1;
222 
223  UpdateDeltaAndState(synthesized_move_event, state);
224  converted_pointers.push_back(synthesized_move_event);
225  } else {
226  // Synthesizes a hover event if the pointer is up.
227  PointerData synthesized_hover_event = pointer_data;
228  synthesized_hover_event.signal_kind =
230  synthesized_hover_event.change = PointerData::Change::kHover;
231  synthesized_hover_event.synthesized = 1;
232 
233  UpdateDeltaAndState(synthesized_hover_event, state);
234  converted_pointers.push_back(synthesized_hover_event);
235  }
236  }
237 
238  converted_pointers.push_back(pointer_data);
239  break;
240  }
241  default: {
242  // Ignores unknown signal kind.
243  break;
244  }
245  }
246  }
247 }
248 
249 PointerState PointerDataPacketConverter::EnsurePointerState(
250  PointerData pointer_data) {
251  PointerState state;
252  state.pointer_identifier = 0;
253  state.isDown = false;
254  state.physical_x = pointer_data.physical_x;
255  state.physical_y = pointer_data.physical_y;
256  states_[pointer_data.device] = state;
257  return state;
258 }
259 
260 void PointerDataPacketConverter::UpdateDeltaAndState(PointerData& pointer_data,
261  PointerState& state) {
262  pointer_data.physical_delta_x = pointer_data.physical_x - state.physical_x;
263  pointer_data.physical_delta_y = pointer_data.physical_y - state.physical_y;
264  state.physical_x = pointer_data.physical_x;
265  state.physical_y = pointer_data.physical_y;
266  states_[pointer_data.device] = state;
267 }
268 
269 bool PointerDataPacketConverter::LocationNeedsUpdate(
270  const PointerData pointer_data,
271  const PointerState state) {
272  return state.physical_x != pointer_data.physical_x ||
273  state.physical_y != pointer_data.physical_y;
274 }
275 
276 void PointerDataPacketConverter::UpdatePointerIdentifier(
277  PointerData& pointer_data,
278  PointerState& state,
279  bool start_new_pointer) {
280  if (start_new_pointer) {
281  state.pointer_identifier = ++pointer_;
282  states_[pointer_data.device] = state;
283  }
284  pointer_data.pointer_identifier = state.pointer_identifier;
285 }
286 
287 } // namespace flutter
static constexpr int kBytesPerField
Definition: pointer_data.h:14
static constexpr int kPointerDataFieldCount
Definition: pointer_data.h:13
#define FML_DCHECK(condition)
Definition: logging.h:86
SignalKind signal_kind
Definition: pointer_data.h:65
int64_t pointer_identifier
Definition: pointer_data.h:67
std::unique_ptr< PointerDataPacket > Convert(std::unique_ptr< PointerDataPacket > packet)
Converts pointer data packet into a form that framework understands. The raw pointer data packet from...