1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/events/gesture_detection/motion_event_buffer.h"
#include "base/debug/trace_event.h"
#include "ui/events/gesture_detection/motion_event_generic.h"
namespace ui {
namespace {
// Latency added during resampling. A few milliseconds doesn't hurt much but
// reduces the impact of mispredicted touch positions.
const int kResampleLatencyMs = 5;
// Minimum time difference between consecutive samples before attempting to
// resample.
const int kResampleMinDeltaMs = 2;
// Maximum time to predict forward from the last known state, to avoid
// predicting too far into the future. This time is further bounded by 50% of
// the last time delta.
const int kResampleMaxPredictionMs = 8;
typedef ScopedVector<MotionEventGeneric> MotionEventVector;
float Lerp(float a, float b, float alpha) {
return a + alpha * (b - a);
}
bool CanAddSample(const MotionEvent& event0, const MotionEvent& event1) {
DCHECK_EQ(event0.GetAction(), MotionEvent::ACTION_MOVE);
if (event1.GetAction() != MotionEvent::ACTION_MOVE)
return false;
const size_t pointer_count = event0.GetPointerCount();
if (pointer_count != event1.GetPointerCount())
return false;
for (size_t event0_i = 0; event0_i < pointer_count; ++event0_i) {
const int id = event0.GetPointerId(event0_i);
const int event1_i = event1.FindPointerIndexOfId(id);
if (event1_i == -1)
return false;
if (event0.GetToolType(event0_i) != event1.GetToolType(event1_i))
return false;
}
return true;
}
bool ShouldResampleTool(MotionEvent::ToolType tool) {
return tool == MotionEvent::TOOL_TYPE_UNKNOWN ||
tool == MotionEvent::TOOL_TYPE_FINGER;
}
size_t CountSamplesNoLaterThan(const MotionEventVector& batch,
base::TimeTicks time) {
size_t count = 0;
while (count < batch.size() && batch[count]->GetEventTime() <= time)
++count;
return count;
}
MotionEventVector ConsumeSamplesNoLaterThan(MotionEventVector* batch,
base::TimeTicks time) {
DCHECK(batch);
size_t count = CountSamplesNoLaterThan(*batch, time);
DCHECK_GE(batch->size(), count);
if (count == 0)
return MotionEventVector();
if (count == batch->size())
return batch->Pass();
// TODO(jdduke): Use a ScopedDeque to work around this mess.
MotionEventVector unconsumed_batch;
unconsumed_batch.insert(
unconsumed_batch.begin(), batch->begin() + count, batch->end());
batch->weak_erase(batch->begin() + count, batch->end());
unconsumed_batch.swap(*batch);
DCHECK_GE(unconsumed_batch.size(), 1U);
return unconsumed_batch.Pass();
}
// Linearly interpolate the pointer position between two MotionEvent samples.
// Only pointers of finger or unknown type will be resampled.
PointerProperties ResamplePointer(const MotionEvent& event0,
const MotionEvent& event1,
size_t event0_pointer_index,
size_t event1_pointer_index,
float alpha) {
DCHECK_EQ(event0.GetPointerId(event0_pointer_index),
event1.GetPointerId(event1_pointer_index));
// If the tool should not be resampled, use the latest event in the valid
// horizon (i.e., the event no later than the time interpolated by alpha).
if (!ShouldResampleTool(event0.GetToolType(event0_pointer_index))) {
if (alpha > 1)
return PointerProperties(event1, event1_pointer_index);
else
return PointerProperties(event0, event0_pointer_index);
}
PointerProperties p(event0, event0_pointer_index);
p.x = Lerp(p.x, event1.GetX(event1_pointer_index), alpha);
p.y = Lerp(p.y, event1.GetY(event1_pointer_index), alpha);
p.raw_x = Lerp(p.raw_x, event1.GetRawX(event1_pointer_index), alpha);
p.raw_y = Lerp(p.raw_y, event1.GetRawY(event1_pointer_index), alpha);
return p;
}
// Linearly interpolate the pointers between two event samples using the
// provided |resample_time|.
scoped_ptr<MotionEventGeneric> ResampleMotionEvent(
const MotionEvent& event0,
const MotionEvent& event1,
base::TimeTicks resample_time) {
DCHECK_EQ(MotionEvent::ACTION_MOVE, event0.GetAction());
DCHECK_EQ(event0.GetPointerCount(), event1.GetPointerCount());
const base::TimeTicks time0 = event0.GetEventTime();
const base::TimeTicks time1 = event1.GetEventTime();
DCHECK(time0 < time1);
DCHECK(time0 <= resample_time);
const float alpha = (resample_time - time0).InMillisecondsF() /
(time1 - time0).InMillisecondsF();
scoped_ptr<MotionEventGeneric> event;
const size_t pointer_count = event0.GetPointerCount();
DCHECK_EQ(pointer_count, event1.GetPointerCount());
for (size_t event0_i = 0; event0_i < pointer_count; ++event0_i) {
int event1_i = event1.FindPointerIndexOfId(event0.GetPointerId(event0_i));
DCHECK_NE(event1_i, -1);
PointerProperties pointer = ResamplePointer(
event0, event1, event0_i, static_cast<size_t>(event1_i), alpha);
if (event0_i == 0) {
event.reset(new MotionEventGeneric(
MotionEvent::ACTION_MOVE, resample_time, pointer));
} else {
event->PushPointer(pointer);
}
}
DCHECK(event);
event->set_id(event0.GetId());
event->set_button_state(event0.GetButtonState());
return event.Pass();
}
// Synthesize a compound MotionEventGeneric event from a sequence of events.
// Events must be in non-decreasing (time) order.
scoped_ptr<MotionEventGeneric> ConsumeSamples(MotionEventVector events) {
DCHECK(!events.empty());
scoped_ptr<MotionEventGeneric> event(events.back());
for (size_t i = 0; i + 1 < events.size(); ++i)
event->PushHistoricalEvent(scoped_ptr<MotionEvent>(events[i]));
events.weak_clear();
return event.Pass();
}
// Consume a series of event samples, attempting to synthesize a new, synthetic
// event if the samples and sample time meet certain interpolation/extrapolation
// conditions. If such conditions are met, the provided samples will be added
// to the synthetic event's history, otherwise, the samples will be used to
// generate a basic, compound event.
// TODO(jdduke): Revisit resampling to handle cases where alternating frames
// are resampled or resampling is otherwise inconsistent, e.g., a 90hz input
// and 60hz frame signal could phase-align such that even frames yield an
// extrapolated event and odd frames are not resampled, crbug.com/399381.
scoped_ptr<MotionEventGeneric> ConsumeSamplesAndTryResampling(
base::TimeTicks resample_time,
MotionEventVector events,
const MotionEvent* next) {
const ui::MotionEvent* event0 = nullptr;
const ui::MotionEvent* event1 = nullptr;
if (next) {
DCHECK(resample_time < next->GetEventTime());
// Interpolate between current sample and future sample.
event0 = events.back();
event1 = next;
} else if (events.size() >= 2) {
// Extrapolate future sample using current sample and past sample.
event0 = events[events.size() - 2];
event1 = events[events.size() - 1];
const base::TimeTicks time1 = event1->GetEventTime();
base::TimeTicks max_predict =
time1 +
std::min((event1->GetEventTime() - event0->GetEventTime()) / 2,
base::TimeDelta::FromMilliseconds(kResampleMaxPredictionMs));
if (resample_time > max_predict) {
TRACE_EVENT_INSTANT2("input",
"MotionEventBuffer::TryResample prediction adjust",
TRACE_EVENT_SCOPE_THREAD,
"original(ms)",
(resample_time - time1).InMilliseconds(),
"adjusted(ms)",
(max_predict - time1).InMilliseconds());
resample_time = max_predict;
}
} else {
TRACE_EVENT_INSTANT0("input",
"MotionEventBuffer::TryResample insufficient data",
TRACE_EVENT_SCOPE_THREAD);
return ConsumeSamples(events.Pass());
}
DCHECK(event0);
DCHECK(event1);
const base::TimeTicks time0 = event0->GetEventTime();
const base::TimeTicks time1 = event1->GetEventTime();
base::TimeDelta delta = time1 - time0;
if (delta < base::TimeDelta::FromMilliseconds(kResampleMinDeltaMs)) {
TRACE_EVENT_INSTANT1("input",
"MotionEventBuffer::TryResample failure",
TRACE_EVENT_SCOPE_THREAD,
"event_delta_too_small(ms)",
delta.InMilliseconds());
return ConsumeSamples(events.Pass());
}
scoped_ptr<MotionEventGeneric> resampled_event =
ResampleMotionEvent(*event0, *event1, resample_time);
for (size_t i = 0; i < events.size(); ++i)
resampled_event->PushHistoricalEvent(scoped_ptr<MotionEvent>(events[i]));
events.weak_clear();
return resampled_event.Pass();
}
} // namespace
MotionEventBuffer::MotionEventBuffer(MotionEventBufferClient* client,
bool enable_resampling)
: client_(client), resample_(enable_resampling) {
}
MotionEventBuffer::~MotionEventBuffer() {
}
void MotionEventBuffer::OnMotionEvent(const MotionEvent& event) {
DCHECK_EQ(0U, event.GetHistorySize());
if (event.GetAction() != MotionEvent::ACTION_MOVE) {
last_extrapolated_event_time_ = base::TimeTicks();
if (!buffered_events_.empty())
FlushWithoutResampling(buffered_events_.Pass());
client_->ForwardMotionEvent(event);
return;
}
// Guard against events that are *older* than the last one that may have been
// artificially synthesized.
if (!last_extrapolated_event_time_.is_null()) {
DCHECK(buffered_events_.empty());
if (event.GetEventTime() < last_extrapolated_event_time_)
return;
last_extrapolated_event_time_ = base::TimeTicks();
}
scoped_ptr<MotionEventGeneric> clone = MotionEventGeneric::CloneEvent(event);
if (buffered_events_.empty()) {
buffered_events_.push_back(clone.release());
client_->SetNeedsFlush();
return;
}
if (CanAddSample(*buffered_events_.front(), *clone)) {
DCHECK(buffered_events_.back()->GetEventTime() <= clone->GetEventTime());
} else {
FlushWithoutResampling(buffered_events_.Pass());
}
buffered_events_.push_back(clone.release());
// No need to request another flush as the first event will have requested it.
}
void MotionEventBuffer::Flush(base::TimeTicks frame_time) {
if (buffered_events_.empty())
return;
// Shifting the sample time back slightly minimizes the potential for
// misprediction when extrapolating events.
if (resample_)
frame_time -= base::TimeDelta::FromMilliseconds(kResampleLatencyMs);
// TODO(jdduke): Use a persistent MotionEventVector vector for temporary
// storage.
MotionEventVector events(
ConsumeSamplesNoLaterThan(&buffered_events_, frame_time));
if (events.empty()) {
DCHECK(!buffered_events_.empty());
client_->SetNeedsFlush();
return;
}
if (!resample_ || (events.size() == 1 && buffered_events_.empty())) {
FlushWithoutResampling(events.Pass());
if (!buffered_events_.empty())
client_->SetNeedsFlush();
return;
}
FlushWithResampling(events.Pass(), frame_time);
}
void MotionEventBuffer::FlushWithResampling(MotionEventVector events,
base::TimeTicks resample_time) {
DCHECK(!events.empty());
base::TimeTicks original_event_time = events.back()->GetEventTime();
const MotionEvent* next_event =
!buffered_events_.empty() ? buffered_events_.front() : nullptr;
scoped_ptr<MotionEventGeneric> resampled_event =
ConsumeSamplesAndTryResampling(resample_time, events.Pass(), next_event);
DCHECK(resampled_event);
// Log the extrapolated event time, guarding against subsequently queued
// events that might have an earlier timestamp.
if (!next_event && resampled_event->GetEventTime() > original_event_time) {
last_extrapolated_event_time_ = resampled_event->GetEventTime();
} else {
last_extrapolated_event_time_ = base::TimeTicks();
}
client_->ForwardMotionEvent(*resampled_event);
if (!buffered_events_.empty())
client_->SetNeedsFlush();
}
void MotionEventBuffer::FlushWithoutResampling(MotionEventVector events) {
last_extrapolated_event_time_ = base::TimeTicks();
if (events.empty())
return;
client_->ForwardMotionEvent(*ConsumeSamples(events.Pass()));
}
} // namespace ui
|