Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: ui/events/gesture_detection/motion_event_buffer.cc

Issue 502993004: Remove abstract Clone and Cancel methods from MotionEvent (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/events/gesture_detection/motion_event_buffer.h" 5 #include "ui/events/gesture_detection/motion_event_buffer.h"
6 6
7 #include "base/debug/trace_event.h" 7 #include "base/debug/trace_event.h"
8 #include "ui/events/gesture_detection/motion_event.h"
9 #include "ui/events/gesture_detection/motion_event_generic.h" 8 #include "ui/events/gesture_detection/motion_event_generic.h"
10 9
11 namespace ui { 10 namespace ui {
12 namespace { 11 namespace {
13 12
14 // Latency added during resampling. A few milliseconds doesn't hurt much but 13 // Latency added during resampling. A few milliseconds doesn't hurt much but
15 // reduces the impact of mispredicted touch positions. 14 // reduces the impact of mispredicted touch positions.
16 const int kResampleLatencyMs = 5; 15 const int kResampleLatencyMs = 5;
17 16
18 // Minimum time difference between consecutive samples before attempting to 17 // Minimum time difference between consecutive samples before attempting to
19 // resample. 18 // resample.
20 const int kResampleMinDeltaMs = 2; 19 const int kResampleMinDeltaMs = 2;
21 20
22 // Maximum time to predict forward from the last known state, to avoid 21 // Maximum time to predict forward from the last known state, to avoid
23 // predicting too far into the future. This time is further bounded by 50% of 22 // predicting too far into the future. This time is further bounded by 50% of
24 // the last time delta. 23 // the last time delta.
25 const int kResampleMaxPredictionMs = 8; 24 const int kResampleMaxPredictionMs = 8;
26 25
27 typedef ScopedVector<MotionEvent> MotionEventVector; 26 typedef ScopedVector<MotionEventGeneric> MotionEventVector;
28 27
29 float Lerp(float a, float b, float alpha) { 28 float Lerp(float a, float b, float alpha) {
30 return a + alpha * (b - a); 29 return a + alpha * (b - a);
31 } 30 }
32 31
33 bool CanAddSample(const MotionEvent& event0, const MotionEvent& event1) { 32 bool CanAddSample(const MotionEvent& event0, const MotionEvent& event1) {
34 DCHECK_EQ(event0.GetAction(), MotionEvent::ACTION_MOVE); 33 DCHECK_EQ(event0.GetAction(), MotionEvent::ACTION_MOVE);
35 if (event1.GetAction() != MotionEvent::ACTION_MOVE) 34 if (event1.GetAction() != MotionEvent::ACTION_MOVE)
36 return false; 35 return false;
37 36
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 MotionEventVector unconsumed_batch; 78 MotionEventVector unconsumed_batch;
80 unconsumed_batch.insert( 79 unconsumed_batch.insert(
81 unconsumed_batch.begin(), batch->begin() + count, batch->end()); 80 unconsumed_batch.begin(), batch->begin() + count, batch->end());
82 batch->weak_erase(batch->begin() + count, batch->end()); 81 batch->weak_erase(batch->begin() + count, batch->end());
83 82
84 unconsumed_batch.swap(*batch); 83 unconsumed_batch.swap(*batch);
85 DCHECK_GE(unconsumed_batch.size(), 1U); 84 DCHECK_GE(unconsumed_batch.size(), 1U);
86 return unconsumed_batch.Pass(); 85 return unconsumed_batch.Pass();
87 } 86 }
88 87
89 PointerProperties PointerFromMotionEvent(const MotionEvent& event, 88 // Linearly interpolate the pointer position between two MotionEvent samples.
90 size_t pointer_index) { 89 // Only pointers of finger or unknown type will be resampled.
91 PointerProperties result;
92 result.id = event.GetPointerId(pointer_index);
93 result.tool_type = event.GetToolType(pointer_index);
94 result.x = event.GetX(pointer_index);
95 result.y = event.GetY(pointer_index);
96 result.raw_x = event.GetRawX(pointer_index);
97 result.raw_y = event.GetRawY(pointer_index);
98 result.pressure = event.GetPressure(pointer_index);
99 result.touch_major = event.GetTouchMajor(pointer_index);
100 result.touch_minor = event.GetTouchMinor(pointer_index);
101 result.orientation = event.GetOrientation(pointer_index);
102 return result;
103 }
104
105 PointerProperties ResamplePointer(const MotionEvent& event0, 90 PointerProperties ResamplePointer(const MotionEvent& event0,
106 const MotionEvent& event1, 91 const MotionEvent& event1,
107 size_t event0_pointer_index, 92 size_t event0_pointer_index,
108 size_t event1_pointer_index, 93 size_t event1_pointer_index,
109 float alpha) { 94 float alpha) {
110 DCHECK_EQ(event0.GetPointerId(event0_pointer_index), 95 DCHECK_EQ(event0.GetPointerId(event0_pointer_index),
111 event1.GetPointerId(event1_pointer_index)); 96 event1.GetPointerId(event1_pointer_index));
112 // If the tool should not be resampled, use the latest event in the valid 97 // If the tool should not be resampled, use the latest event in the valid
113 // horizon (i.e., the event no later than the time interpolated by alpha). 98 // horizon (i.e., the event no later than the time interpolated by alpha).
114 if (!ShouldResampleTool(event0.GetToolType(event0_pointer_index))) { 99 if (!ShouldResampleTool(event0.GetToolType(event0_pointer_index))) {
115 if (alpha > 1) 100 if (alpha > 1)
116 return PointerFromMotionEvent(event1, event1_pointer_index); 101 return PointerProperties(event1, event1_pointer_index);
117 else 102 else
118 return PointerFromMotionEvent(event0, event0_pointer_index); 103 return PointerProperties(event0, event0_pointer_index);
119 } 104 }
120 105
121 PointerProperties p(PointerFromMotionEvent(event0, event0_pointer_index)); 106 PointerProperties p(event0, event0_pointer_index);
122 p.x = Lerp(p.x, event1.GetX(event1_pointer_index), alpha); 107 p.x = Lerp(p.x, event1.GetX(event1_pointer_index), alpha);
123 p.y = Lerp(p.y, event1.GetY(event1_pointer_index), alpha); 108 p.y = Lerp(p.y, event1.GetY(event1_pointer_index), alpha);
124 p.raw_x = Lerp(p.raw_x, event1.GetRawX(event1_pointer_index), alpha); 109 p.raw_x = Lerp(p.raw_x, event1.GetRawX(event1_pointer_index), alpha);
125 p.raw_y = Lerp(p.raw_y, event1.GetRawY(event1_pointer_index), alpha); 110 p.raw_y = Lerp(p.raw_y, event1.GetRawY(event1_pointer_index), alpha);
126 return p; 111 return p;
127 } 112 }
128 113
129 scoped_ptr<MotionEvent> ResampleMotionEvent(const MotionEvent& event0, 114 // Linearly interpolate the pointers between two event samples using the
130 const MotionEvent& event1, 115 // provided |resample_time|.
131 base::TimeTicks resample_time) { 116 scoped_ptr<MotionEventGeneric> ResampleMotionEvent(
117 const MotionEvent& event0,
118 const MotionEvent& event1,
119 base::TimeTicks resample_time) {
132 DCHECK_EQ(MotionEvent::ACTION_MOVE, event0.GetAction()); 120 DCHECK_EQ(MotionEvent::ACTION_MOVE, event0.GetAction());
133 DCHECK_EQ(event0.GetPointerCount(), event1.GetPointerCount()); 121 DCHECK_EQ(event0.GetPointerCount(), event1.GetPointerCount());
134 122
135 const base::TimeTicks time0 = event0.GetEventTime(); 123 const base::TimeTicks time0 = event0.GetEventTime();
136 const base::TimeTicks time1 = event1.GetEventTime(); 124 const base::TimeTicks time1 = event1.GetEventTime();
137 DCHECK(time0 < time1); 125 DCHECK(time0 < time1);
138 DCHECK(time0 <= resample_time); 126 DCHECK(time0 <= resample_time);
139 127
140 const float alpha = (resample_time - time0).InMillisecondsF() / 128 const float alpha = (resample_time - time0).InMillisecondsF() /
141 (time1 - time0).InMillisecondsF(); 129 (time1 - time0).InMillisecondsF();
(...skipping 12 matching lines...) Expand all
154 MotionEvent::ACTION_MOVE, resample_time, pointer)); 142 MotionEvent::ACTION_MOVE, resample_time, pointer));
155 } else { 143 } else {
156 event->PushPointer(pointer); 144 event->PushPointer(pointer);
157 } 145 }
158 } 146 }
159 147
160 DCHECK(event); 148 DCHECK(event);
161 event->set_id(event0.GetId()); 149 event->set_id(event0.GetId());
162 event->set_action_index(event0.GetActionIndex()); 150 event->set_action_index(event0.GetActionIndex());
163 event->set_button_state(event0.GetButtonState()); 151 event->set_button_state(event0.GetButtonState());
164 152 return event.Pass();
165 return event.PassAs<MotionEvent>();
166 } 153 }
167 154
168 // MotionEvent implementation for storing multiple events, with the most 155 // Synthesize a compound MotionEventGeneric event from a sequence of events.
169 // recent event used as the base event, and prior events used as the history. 156 // Note that it is assumed that the events are in non-decreasing (time) order.
tdresser 2014/10/20 15:19:15 I'd prefer: "Note that it is assumed that the even
jdduke (slow) 2014/10/21 22:19:58 Done.
170 class CompoundMotionEvent : public ui::MotionEvent { 157 scoped_ptr<MotionEventGeneric> ConsumeSamples(MotionEventVector events) {
171 public: 158 DCHECK(!events.empty());
172 explicit CompoundMotionEvent(MotionEventVector events) 159 scoped_ptr<MotionEventGeneric> event(events.back());
173 : events_(events.Pass()) { 160 for (size_t i = 0; i + 1 < events.size(); ++i)
174 DCHECK_GE(events_.size(), 1U); 161 event->PushHistoricalEvent(scoped_ptr<MotionEvent>(events[i]));
175 } 162 events.weak_clear();
176 virtual ~CompoundMotionEvent() {} 163 return event.Pass();
164 }
177 165
178 virtual int GetId() const override { return latest().GetId(); } 166 // Consume a series of event samples, attempting to synthesize a new, synthetic
167 // event if the samples and sample time meet certain interpolation/extrapolation
168 // conditions. If such conditions are met, the provided samples will be added
169 // to the synthetic event's history, otherwise, the samples will be used to
170 // generate a basic, compound event.
171 // TODO(jdduke): Revisit resampling to handle cases where alternating frames
172 // are resampled or resampling is otherwise inconsistent, e.g., a 90hz input
173 // and 60hz frame signal could phase-align such that even frames yield an
174 // extrapolated event and odd frames are not resampled, crbug.com/399381.
175 scoped_ptr<MotionEventGeneric> ConsumeSamplesAndTryResampling(
176 base::TimeTicks resample_time,
177 MotionEventVector events,
178 const MotionEvent* next) {
179 const ui::MotionEvent* event0 = NULL;
tdresser 2014/10/20 15:19:15 Might as well nullptr'ify these while you're here.
jdduke (slow) 2014/10/21 22:19:58 Done.
180 const ui::MotionEvent* event1 = NULL;
181 if (next) {
182 DCHECK(resample_time < next->GetEventTime());
183 // Interpolate between current sample and future sample.
184 event0 = events.back();
185 event1 = next;
186 } else if (events.size() >= 2) {
187 // Extrapolate future sample using current sample and past sample.
188 event0 = events[events.size() - 2];
189 event1 = events[events.size() - 1];
179 190
180 virtual Action GetAction() const override { return latest().GetAction(); } 191 const base::TimeTicks time1 = event1->GetEventTime();
181 192 base::TimeTicks max_predict =
182 virtual int GetActionIndex() const override { 193 time1 +
183 return latest().GetActionIndex(); 194 std::min((event1->GetEventTime() - event0->GetEventTime()) / 2,
195 base::TimeDelta::FromMilliseconds(kResampleMaxPredictionMs));
196 if (resample_time > max_predict) {
197 TRACE_EVENT_INSTANT2("input",
198 "MotionEventBuffer::TryResample prediction adjust",
199 TRACE_EVENT_SCOPE_THREAD,
200 "original(ms)",
201 (resample_time - time1).InMilliseconds(),
202 "adjusted(ms)",
203 (max_predict - time1).InMilliseconds());
204 resample_time = max_predict;
205 }
206 } else {
207 TRACE_EVENT_INSTANT0("input",
208 "MotionEventBuffer::TryResample insufficient data",
209 TRACE_EVENT_SCOPE_THREAD);
210 return ConsumeSamples(events.Pass());
184 } 211 }
185 212
186 virtual size_t GetPointerCount() const override { 213 DCHECK(event0);
187 return latest().GetPointerCount(); 214 DCHECK(event1);
215 const base::TimeTicks time0 = event0->GetEventTime();
216 const base::TimeTicks time1 = event1->GetEventTime();
217 base::TimeDelta delta = time1 - time0;
218 if (delta < base::TimeDelta::FromMilliseconds(kResampleMinDeltaMs)) {
219 TRACE_EVENT_INSTANT1("input",
220 "MotionEventBuffer::TryResample failure",
221 TRACE_EVENT_SCOPE_THREAD,
222 "event_delta_too_small(ms)",
223 delta.InMilliseconds());
224 return ConsumeSamples(events.Pass());
188 } 225 }
189 226
190 virtual int GetPointerId(size_t pointer_index) const override { 227 scoped_ptr<MotionEventGeneric> resampled_event =
191 return latest().GetPointerId(pointer_index); 228 ResampleMotionEvent(*event0, *event1, resample_time);
192 } 229 for (size_t i = 0; i < events.size(); ++i)
193 230 resampled_event->PushHistoricalEvent(scoped_ptr<MotionEvent>(events[i]));
194 virtual float GetX(size_t pointer_index) const override { 231 events.weak_clear();
195 return latest().GetX(pointer_index); 232 return resampled_event.Pass();
196 } 233 }
197
198 virtual float GetY(size_t pointer_index) const override {
199 return latest().GetY(pointer_index);
200 }
201
202 virtual float GetRawX(size_t pointer_index) const override {
203 return latest().GetRawX(pointer_index);
204 }
205
206 virtual float GetRawY(size_t pointer_index) const override {
207 return latest().GetRawY(pointer_index);
208 }
209
210 virtual float GetTouchMajor(size_t pointer_index) const override {
211 return latest().GetTouchMajor(pointer_index);
212 }
213
214 virtual float GetTouchMinor(size_t pointer_index) const override {
215 return latest().GetTouchMinor(pointer_index);
216 }
217
218 virtual float GetOrientation(size_t pointer_index) const override {
219 return latest().GetOrientation(pointer_index);
220 }
221
222 virtual float GetPressure(size_t pointer_index) const override {
223 return latest().GetPressure(pointer_index);
224 }
225
226 virtual ToolType GetToolType(size_t pointer_index) const override {
227 return latest().GetToolType(pointer_index);
228 }
229
230 virtual int GetButtonState() const override {
231 return latest().GetButtonState();
232 }
233
234 virtual int GetFlags() const override { return latest().GetFlags(); }
235
236 virtual base::TimeTicks GetEventTime() const override {
237 return latest().GetEventTime();
238 }
239
240 virtual size_t GetHistorySize() const override { return events_.size() - 1; }
241
242 virtual base::TimeTicks GetHistoricalEventTime(
243 size_t historical_index) const override {
244 DCHECK_LT(historical_index, GetHistorySize());
245 return events_[historical_index]->GetEventTime();
246 }
247
248 virtual float GetHistoricalTouchMajor(
249 size_t pointer_index,
250 size_t historical_index) const override {
251 DCHECK_LT(historical_index, GetHistorySize());
252 return events_[historical_index]->GetTouchMajor();
253 }
254
255 virtual float GetHistoricalX(size_t pointer_index,
256 size_t historical_index) const override {
257 DCHECK_LT(historical_index, GetHistorySize());
258 return events_[historical_index]->GetX(pointer_index);
259 }
260
261 virtual float GetHistoricalY(size_t pointer_index,
262 size_t historical_index) const override {
263 DCHECK_LT(historical_index, GetHistorySize());
264 return events_[historical_index]->GetY(pointer_index);
265 }
266
267 virtual scoped_ptr<MotionEvent> Clone() const override {
268 MotionEventVector cloned_events;
269 cloned_events.reserve(events_.size());
270 for (size_t i = 0; i < events_.size(); ++i)
271 cloned_events.push_back(events_[i]->Clone().release());
272 return scoped_ptr<MotionEvent>(
273 new CompoundMotionEvent(cloned_events.Pass()));
274 }
275
276 virtual scoped_ptr<MotionEvent> Cancel() const override {
277 return latest().Cancel();
278 }
279
280 // Returns the new, resampled event, or NULL if none was created.
281 // TODO(jdduke): Revisit resampling to handle cases where alternating frames
282 // are resampled or resampling is otherwise inconsistent, e.g., a 90hz input
283 // and 60hz frame signal could phase-align such that even frames yield an
284 // extrapolated event and odd frames are not resampled, crbug.com/399381.
285 const MotionEvent* TryResample(base::TimeTicks resample_time,
286 const ui::MotionEvent* next) {
287 DCHECK_EQ(GetAction(), ACTION_MOVE);
288 const ui::MotionEvent* event0 = NULL;
289 const ui::MotionEvent* event1 = NULL;
290 if (next) {
291 DCHECK(resample_time < next->GetEventTime());
292 // Interpolate between current sample and future sample.
293 event0 = events_.back();
294 event1 = next;
295 } else if (events_.size() >= 2) {
296 // Extrapolate future sample using current sample and past sample.
297 event0 = events_[events_.size() - 2];
298 event1 = events_[events_.size() - 1];
299
300 const base::TimeTicks time1 = event1->GetEventTime();
301 base::TimeTicks max_predict =
302 time1 +
303 std::min((event1->GetEventTime() - event0->GetEventTime()) / 2,
304 base::TimeDelta::FromMilliseconds(kResampleMaxPredictionMs));
305 if (resample_time > max_predict) {
306 TRACE_EVENT_INSTANT2("input",
307 "MotionEventBuffer::TryResample prediction adjust",
308 TRACE_EVENT_SCOPE_THREAD,
309 "original(ms)",
310 (resample_time - time1).InMilliseconds(),
311 "adjusted(ms)",
312 (max_predict - time1).InMilliseconds());
313 resample_time = max_predict;
314 }
315 } else {
316 TRACE_EVENT_INSTANT0("input",
317 "MotionEventBuffer::TryResample insufficient data",
318 TRACE_EVENT_SCOPE_THREAD);
319 return NULL;
320 }
321
322 DCHECK(event0);
323 DCHECK(event1);
324 const base::TimeTicks time0 = event0->GetEventTime();
325 const base::TimeTicks time1 = event1->GetEventTime();
326 base::TimeDelta delta = time1 - time0;
327 if (delta < base::TimeDelta::FromMilliseconds(kResampleMinDeltaMs)) {
328 TRACE_EVENT_INSTANT1("input",
329 "MotionEventBuffer::TryResample failure",
330 TRACE_EVENT_SCOPE_THREAD,
331 "event_delta_too_small(ms)",
332 delta.InMilliseconds());
333 return NULL;
334 }
335
336 events_.push_back(
337 ResampleMotionEvent(*event0, *event1, resample_time).release());
338 return events_.back();
339 }
340
341 size_t samples() const { return events_.size(); }
342
343 private:
344 const MotionEvent& latest() const { return *events_.back(); }
345
346 // Events are in order from oldest to newest.
347 MotionEventVector events_;
348
349 DISALLOW_COPY_AND_ASSIGN(CompoundMotionEvent);
350 };
351 234
352 } // namespace 235 } // namespace
353 236
354 MotionEventBuffer::MotionEventBuffer(MotionEventBufferClient* client, 237 MotionEventBuffer::MotionEventBuffer(MotionEventBufferClient* client,
355 bool enable_resampling) 238 bool enable_resampling)
356 : client_(client), resample_(enable_resampling) { 239 : client_(client), resample_(enable_resampling) {
357 } 240 }
358 241
359 MotionEventBuffer::~MotionEventBuffer() { 242 MotionEventBuffer::~MotionEventBuffer() {
360 } 243 }
361 244
362 void MotionEventBuffer::OnMotionEvent(const MotionEvent& event) { 245 void MotionEventBuffer::OnMotionEvent(const MotionEvent& event) {
246 DCHECK_EQ(0U, event.GetHistorySize());
363 if (event.GetAction() != MotionEvent::ACTION_MOVE) { 247 if (event.GetAction() != MotionEvent::ACTION_MOVE) {
364 last_extrapolated_event_time_ = base::TimeTicks(); 248 last_extrapolated_event_time_ = base::TimeTicks();
365 if (!buffered_events_.empty()) 249 if (!buffered_events_.empty())
366 FlushWithoutResampling(buffered_events_.Pass()); 250 FlushWithoutResampling(buffered_events_.Pass());
367 client_->ForwardMotionEvent(event); 251 client_->ForwardMotionEvent(event);
368 return; 252 return;
369 } 253 }
370 254
371 // Guard against events that are *older* than the last one that may have been 255 // Guard against events that are *older* than the last one that may have been
372 // artificially synthesized. 256 // artificially synthesized.
373 if (!last_extrapolated_event_time_.is_null()) { 257 if (!last_extrapolated_event_time_.is_null()) {
374 DCHECK(buffered_events_.empty()); 258 DCHECK(buffered_events_.empty());
375 if (event.GetEventTime() < last_extrapolated_event_time_) 259 if (event.GetEventTime() < last_extrapolated_event_time_)
376 return; 260 return;
377 last_extrapolated_event_time_ = base::TimeTicks(); 261 last_extrapolated_event_time_ = base::TimeTicks();
378 } 262 }
379 263
380 scoped_ptr<MotionEvent> clone = event.Clone(); 264 scoped_ptr<MotionEventGeneric> clone = MotionEventGeneric::CloneEvent(event);
381 if (buffered_events_.empty()) { 265 if (buffered_events_.empty()) {
382 buffered_events_.push_back(clone.release()); 266 buffered_events_.push_back(clone.release());
383 client_->SetNeedsFlush(); 267 client_->SetNeedsFlush();
384 return; 268 return;
385 } 269 }
386 270
387 if (CanAddSample(*buffered_events_.front(), *clone)) { 271 if (CanAddSample(*buffered_events_.front(), *clone)) {
388 DCHECK(buffered_events_.back()->GetEventTime() <= clone->GetEventTime()); 272 DCHECK(buffered_events_.back()->GetEventTime() <= clone->GetEventTime());
389 } else { 273 } else {
390 FlushWithoutResampling(buffered_events_.Pass()); 274 FlushWithoutResampling(buffered_events_.Pass());
(...skipping 22 matching lines...) Expand all
413 return; 297 return;
414 } 298 }
415 299
416 if (!resample_ || (events.size() == 1 && buffered_events_.empty())) { 300 if (!resample_ || (events.size() == 1 && buffered_events_.empty())) {
417 FlushWithoutResampling(events.Pass()); 301 FlushWithoutResampling(events.Pass());
418 if (!buffered_events_.empty()) 302 if (!buffered_events_.empty())
419 client_->SetNeedsFlush(); 303 client_->SetNeedsFlush();
420 return; 304 return;
421 } 305 }
422 306
423 CompoundMotionEvent resampled_event(events.Pass()); 307 FlushWithResampling(events.Pass(), frame_time);
424 base::TimeTicks original_event_time = resampled_event.GetEventTime(); 308 }
309
310 void MotionEventBuffer::FlushWithResampling(MotionEventVector events,
311 base::TimeTicks resample_time) {
312 DCHECK(!events.empty());
313 base::TimeTicks original_event_time = events.back()->GetEventTime();
425 const MotionEvent* next_event = 314 const MotionEvent* next_event =
426 !buffered_events_.empty() ? buffered_events_.front() : NULL; 315 !buffered_events_.empty() ? buffered_events_.front() : NULL;
427 316
428 // Try to interpolate/extrapolate a new event at |frame_time|. Note that 317 scoped_ptr<MotionEventGeneric> resampled_event =
429 // |new_event|, if non-NULL, is owned by |resampled_event_|. 318 ConsumeSamplesAndTryResampling(resample_time, events.Pass(), next_event);
430 const MotionEvent* new_event = 319 DCHECK(resampled_event);
431 resampled_event.TryResample(frame_time, next_event);
432 320
433 // Log the extrapolated event time, guarding against subsequently queued 321 // Log the extrapolated event time, guarding against subsequently queued
434 // events that might have an earlier timestamp. 322 // events that might have an earlier timestamp.
435 if (!next_event && new_event && 323 if (!next_event && resampled_event->GetEventTime() > original_event_time) {
436 new_event->GetEventTime() > original_event_time) { 324 last_extrapolated_event_time_ = resampled_event->GetEventTime();
437 last_extrapolated_event_time_ = new_event->GetEventTime();
438 } else { 325 } else {
439 last_extrapolated_event_time_ = base::TimeTicks(); 326 last_extrapolated_event_time_ = base::TimeTicks();
440 } 327 }
441 328
442 client_->ForwardMotionEvent(resampled_event); 329 client_->ForwardMotionEvent(*resampled_event);
443 if (!buffered_events_.empty()) 330 if (!buffered_events_.empty())
444 client_->SetNeedsFlush(); 331 client_->SetNeedsFlush();
445 } 332 }
446 333
447 void MotionEventBuffer::FlushWithoutResampling(MotionEventVector events) { 334 void MotionEventBuffer::FlushWithoutResampling(MotionEventVector events) {
448 last_extrapolated_event_time_ = base::TimeTicks(); 335 last_extrapolated_event_time_ = base::TimeTicks();
449 if (events.empty()) 336 if (events.empty())
450 return; 337 return;
451 338
452 if (events.size() == 1) { 339 client_->ForwardMotionEvent(*ConsumeSamples(events.Pass()));
453 // Avoid CompoundEvent creation to prevent unnecessary allocations.
454 scoped_ptr<MotionEvent> event(events.front());
455 events.weak_clear();
456 client_->ForwardMotionEvent(*event);
457 return;
458 }
459
460 CompoundMotionEvent compound_event(events.Pass());
461 client_->ForwardMotionEvent(compound_event);
462 } 340 }
463 341
464 } // namespace ui 342 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698