Commit 85282366 authored by tapted's avatar tapted Committed by Commit bot

Decouple EventWithLatencyInfo from WebInputEventTraits

Only EventWithLatencyInfo uses [Can]Coalesce() from WebInputEventTraits.
WebInputEventTraits gets used for IPC stuff so is kinda sensitive, but
EventWithLatencyInfo isn't involved with IPC. So, coupling these
together also makes refactoring unnecessarily difficult.

E.g. the coupling means that if we want to move EventWithLatencyInfo to
a component, then WebInputEventTraits and a bunch of dependencies that
EventWithLatencyInfo doesn't care about need to come as well.

To decouple, the guts of [Can]Coalesce() moves to a new .cc file for
EventWithLatencyInfo. The types are known, so we get some added type
safety versus WebInputEventTraits which isn't templatized in the .h. The
instantiations in the .cc become a bit simpler and the runtime type
checks and static casts behind in web_input_event_trait.cc's Apply(..)
are not required.

BUG=615948
TBR=jochen@chromium.org (iwyu)

Review-Url: https://codereview.chromium.org/2111593003
Cr-Commit-Position: refs/heads/master@{#403402}
parent d7e9b95f
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/trace_event/trace_event.h" #include "base/trace_event/trace_event.h"
#include "content/browser/renderer_host/input/touchpad_tap_suppression_controller.h" #include "content/browser/renderer_host/input/touchpad_tap_suppression_controller.h"
#include "content/browser/renderer_host/input/touchscreen_tap_suppression_controller.h" #include "content/browser/renderer_host/input/touchscreen_tap_suppression_controller.h"
#include "content/common/input/web_input_event_traits.h"
using blink::WebGestureEvent; using blink::WebGestureEvent;
using blink::WebInputEvent; using blink::WebInputEvent;
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "content/common/edit_command.h" #include "content/common/edit_command.h"
#include "content/common/input/input_event_ack_state.h" #include "content/common/input/input_event_ack_state.h"
#include "content/common/input/touch_action.h" #include "content/common/input/touch_action.h"
#include "content/common/input/web_input_event_traits.h"
#include "content/common/input/web_touch_event_traits.h" #include "content/common/input/web_touch_event_traits.h"
#include "content/common/input_messages.h" #include "content/common/input_messages.h"
#include "content/common/view_messages.h" #include "content/common/view_messages.h"
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/trace_event/trace_event.h" #include "base/trace_event/trace_event.h"
#include "content/common/input/web_input_event_traits.h"
using blink::WebGestureEvent; using blink::WebGestureEvent;
using blink::WebInputEvent; using blink::WebInputEvent;
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/metrics/histogram.h" #include "base/metrics/histogram.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "content/browser/renderer_host/render_widget_host_impl.h" #include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/common/input/web_input_event_traits.h"
using blink::WebGestureEvent; using blink::WebGestureEvent;
using blink::WebInputEvent; using blink::WebInputEvent;
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "content/browser/renderer_host/render_widget_host_impl.h" #include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_view_base.h" #include "content/browser/renderer_host/render_widget_host_view_base.h"
#include "content/browser/renderer_host/ui_events_helper.h" #include "content/browser/renderer_host/ui_events_helper.h"
#include "content/common/input/web_input_event_traits.h"
#include "content/common/input_messages.h" #include "content/common/input_messages.h"
#include "third_party/WebKit/public/web/WebInputEvent.h" #include "third_party/WebKit/public/web/WebInputEvent.h"
#include "ui/events/event.h" #include "ui/events/event.h"
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "content/browser/renderer_host/render_widget_host_delegate.h" #include "content/browser/renderer_host/render_widget_host_delegate.h"
#include "content/browser/renderer_host/render_widget_host_view_base.h" #include "content/browser/renderer_host/render_widget_host_view_base.h"
#include "content/common/input/synthetic_web_input_event_builders.h" #include "content/common/input/synthetic_web_input_event_builders.h"
#include "content/common/input/web_input_event_traits.h"
#include "content/common/input_messages.h" #include "content/common/input_messages.h"
#include "content/common/resize_params.h" #include "content/common/resize_params.h"
#include "content/common/view_messages.h" #include "content/common/view_messages.h"
......
...@@ -46,6 +46,7 @@ ...@@ -46,6 +46,7 @@
#include "content/browser/web_contents/web_contents_view_aura.h" #include "content/browser/web_contents/web_contents_view_aura.h"
#include "content/common/host_shared_bitmap_manager.h" #include "content/common/host_shared_bitmap_manager.h"
#include "content/common/input/synthetic_web_input_event_builders.h" #include "content/common/input/synthetic_web_input_event_builders.h"
#include "content/common/input/web_input_event_traits.h"
#include "content/common/input_messages.h" #include "content/common/input_messages.h"
#include "content/common/text_input_state.h" #include "content/common/text_input_state.h"
#include "content/common/view_messages.h" #include "content/common/view_messages.h"
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "content/browser/frame_host/render_widget_host_view_guest.h" #include "content/browser/frame_host/render_widget_host_view_guest.h"
#include "content/browser/gpu/compositor_util.h" #include "content/browser/gpu/compositor_util.h"
#include "content/browser/renderer_host/render_widget_host_delegate.h" #include "content/browser/renderer_host/render_widget_host_delegate.h"
#include "content/common/input/web_input_event_traits.h"
#include "content/common/input_messages.h" #include "content/common/input_messages.h"
#include "content/common/view_messages.h" #include "content/common/view_messages.h"
#include "content/public/browser/notification_types.h" #include "content/public/browser/notification_types.h"
......
...@@ -77,6 +77,7 @@ ...@@ -77,6 +77,7 @@
#include "content/common/browser_plugin/browser_plugin_constants.h" #include "content/common/browser_plugin/browser_plugin_constants.h"
#include "content/common/browser_plugin/browser_plugin_messages.h" #include "content/common/browser_plugin/browser_plugin_messages.h"
#include "content/common/frame_messages.h" #include "content/common/frame_messages.h"
#include "content/common/input/web_input_event_traits.h"
#include "content/common/input_messages.h" #include "content/common/input_messages.h"
#include "content/common/page_messages.h" #include "content/common/page_messages.h"
#include "content/common/site_isolation_policy.h" #include "content/common/site_isolation_policy.h"
......
// Copyright 2016 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 "content/common/input/event_with_latency_info.h"
#include <bitset>
#include <limits>
using blink::WebGestureEvent;
using blink::WebInputEvent;
using blink::WebKeyboardEvent;
using blink::WebMouseEvent;
using blink::WebMouseWheelEvent;
using blink::WebTouchEvent;
using std::numeric_limits;
namespace content {
namespace {
const int kInvalidTouchIndex = -1;
float GetUnacceleratedDelta(float accelerated_delta, float acceleration_ratio) {
return accelerated_delta * acceleration_ratio;
}
float GetAccelerationRatio(float accelerated_delta, float unaccelerated_delta) {
if (unaccelerated_delta == 0.f || accelerated_delta == 0.f)
return 1.f;
return unaccelerated_delta / accelerated_delta;
}
// Returns |kInvalidTouchIndex| iff |event| lacks a touch with an ID of |id|.
int GetIndexOfTouchID(const WebTouchEvent& event, int id) {
for (unsigned i = 0; i < event.touchesLength; ++i) {
if (event.touches[i].id == id)
return i;
}
return kInvalidTouchIndex;
}
WebInputEvent::DispatchType MergeDispatchTypes(
WebInputEvent::DispatchType type_1,
WebInputEvent::DispatchType type_2) {
static_assert(WebInputEvent::DispatchType::Blocking <
WebInputEvent::DispatchType::EventNonBlocking,
"Enum not ordered correctly");
static_assert(WebInputEvent::DispatchType::EventNonBlocking <
WebInputEvent::DispatchType::ListenersNonBlockingPassive,
"Enum not ordered correctly");
static_assert(
WebInputEvent::DispatchType::ListenersNonBlockingPassive <
WebInputEvent::DispatchType::ListenersForcedNonBlockingPassive,
"Enum not ordered correctly");
return static_cast<WebInputEvent::DispatchType>(
std::min(static_cast<int>(type_1), static_cast<int>(type_2)));
}
} // namespace
namespace internal {
bool CanCoalesce(const WebMouseEvent& event_to_coalesce,
const WebMouseEvent& event) {
return event.type == event_to_coalesce.type &&
event.type == WebInputEvent::MouseMove;
}
void Coalesce(const WebMouseEvent& event_to_coalesce, WebMouseEvent* event) {
DCHECK(CanCoalesce(event_to_coalesce, *event));
// Accumulate movement deltas.
int x = event->movementX;
int y = event->movementY;
*event = event_to_coalesce;
event->movementX += x;
event->movementY += y;
}
bool CanCoalesce(const WebMouseWheelEvent& event_to_coalesce,
const WebMouseWheelEvent& event) {
return event.modifiers == event_to_coalesce.modifiers &&
event.scrollByPage == event_to_coalesce.scrollByPage &&
event.phase == event_to_coalesce.phase &&
event.momentumPhase == event_to_coalesce.momentumPhase &&
event.hasPreciseScrollingDeltas ==
event_to_coalesce.hasPreciseScrollingDeltas;
}
void Coalesce(const WebMouseWheelEvent& event_to_coalesce,
WebMouseWheelEvent* event) {
DCHECK(CanCoalesce(event_to_coalesce, *event));
float unaccelerated_x =
GetUnacceleratedDelta(event->deltaX, event->accelerationRatioX) +
GetUnacceleratedDelta(event_to_coalesce.deltaX,
event_to_coalesce.accelerationRatioX);
float unaccelerated_y =
GetUnacceleratedDelta(event->deltaY, event->accelerationRatioY) +
GetUnacceleratedDelta(event_to_coalesce.deltaY,
event_to_coalesce.accelerationRatioY);
event->deltaX += event_to_coalesce.deltaX;
event->deltaY += event_to_coalesce.deltaY;
event->wheelTicksX += event_to_coalesce.wheelTicksX;
event->wheelTicksY += event_to_coalesce.wheelTicksY;
event->accelerationRatioX =
GetAccelerationRatio(event->deltaX, unaccelerated_x);
event->accelerationRatioY =
GetAccelerationRatio(event->deltaY, unaccelerated_y);
}
bool CanCoalesce(const WebTouchEvent& event_to_coalesce,
const WebTouchEvent& event) {
if (event.type != event_to_coalesce.type ||
event.type != WebInputEvent::TouchMove ||
event.modifiers != event_to_coalesce.modifiers ||
event.touchesLength != event_to_coalesce.touchesLength ||
event.touchesLength > WebTouchEvent::touchesLengthCap)
return false;
static_assert(WebTouchEvent::touchesLengthCap <= sizeof(int32_t) * 8U,
"suboptimal touchesLengthCap size");
// Ensure that we have a 1-to-1 mapping of pointer ids between touches.
std::bitset<WebTouchEvent::touchesLengthCap> unmatched_event_touches(
(1 << event.touchesLength) - 1);
for (unsigned i = 0; i < event_to_coalesce.touchesLength; ++i) {
int event_touch_index =
GetIndexOfTouchID(event, event_to_coalesce.touches[i].id);
if (event_touch_index == kInvalidTouchIndex)
return false;
if (!unmatched_event_touches[event_touch_index])
return false;
unmatched_event_touches[event_touch_index] = false;
}
return unmatched_event_touches.none();
}
void Coalesce(const WebTouchEvent& event_to_coalesce, WebTouchEvent* event) {
DCHECK(CanCoalesce(event_to_coalesce, *event));
// The WebTouchPoints include absolute position information. So it is
// sufficient to simply replace the previous event with the new event->
// However, it is necessary to make sure that all the points have the
// correct state, i.e. the touch-points that moved in the last event, but
// didn't change in the current event, will have Stationary state. It is
// necessary to change them back to Moved state.
WebTouchEvent old_event = *event;
*event = event_to_coalesce;
for (unsigned i = 0; i < event->touchesLength; ++i) {
int i_old = GetIndexOfTouchID(old_event, event->touches[i].id);
if (old_event.touches[i_old].state == blink::WebTouchPoint::StateMoved)
event->touches[i].state = blink::WebTouchPoint::StateMoved;
}
event->movedBeyondSlopRegion |= old_event.movedBeyondSlopRegion;
event->dispatchType = MergeDispatchTypes(old_event.dispatchType,
event_to_coalesce.dispatchType);
}
bool CanCoalesce(const WebGestureEvent& event_to_coalesce,
const WebGestureEvent& event) {
if (event.type != event_to_coalesce.type ||
event.sourceDevice != event_to_coalesce.sourceDevice ||
event.modifiers != event_to_coalesce.modifiers)
return false;
if (event.type == WebInputEvent::GestureScrollUpdate)
return true;
// GesturePinchUpdate scales can be combined only if they share a focal point,
// e.g., with double-tap drag zoom.
if (event.type == WebInputEvent::GesturePinchUpdate &&
event.x == event_to_coalesce.x && event.y == event_to_coalesce.y)
return true;
return false;
}
void Coalesce(const WebGestureEvent& event_to_coalesce,
WebGestureEvent* event) {
DCHECK(CanCoalesce(event_to_coalesce, *event));
if (event->type == WebInputEvent::GestureScrollUpdate) {
event->data.scrollUpdate.deltaX +=
event_to_coalesce.data.scrollUpdate.deltaX;
event->data.scrollUpdate.deltaY +=
event_to_coalesce.data.scrollUpdate.deltaY;
DCHECK_EQ(
event->data.scrollUpdate.previousUpdateInSequencePrevented,
event_to_coalesce.data.scrollUpdate.previousUpdateInSequencePrevented);
} else if (event->type == WebInputEvent::GesturePinchUpdate) {
event->data.pinchUpdate.scale *= event_to_coalesce.data.pinchUpdate.scale;
// Ensure the scale remains bounded above 0 and below Infinity so that
// we can reliably perform operations like log on the values.
if (event->data.pinchUpdate.scale < numeric_limits<float>::min())
event->data.pinchUpdate.scale = numeric_limits<float>::min();
else if (event->data.pinchUpdate.scale > numeric_limits<float>::max())
event->data.pinchUpdate.scale = numeric_limits<float>::max();
}
}
} // namespace internal
} // namespace content
...@@ -5,18 +5,34 @@ ...@@ -5,18 +5,34 @@
#ifndef CONTENT_COMMON_INPUT_EVENT_WITH_LATENCY_INFO_H_ #ifndef CONTENT_COMMON_INPUT_EVENT_WITH_LATENCY_INFO_H_
#define CONTENT_COMMON_INPUT_EVENT_WITH_LATENCY_INFO_H_ #define CONTENT_COMMON_INPUT_EVENT_WITH_LATENCY_INFO_H_
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "content/common/content_export.h"
#include "third_party/WebKit/public/web/WebInputEvent.h"
#include "ui/events/latency_info.h" #include "ui/events/latency_info.h"
#include "content/common/input/web_input_event_traits.h" namespace content {
namespace internal {
namespace blink { bool CONTENT_EXPORT CanCoalesce(const blink::WebMouseEvent& event_to_coalesce,
class WebGestureEvent; const blink::WebMouseEvent& event);
class WebMouseEvent; void CONTENT_EXPORT Coalesce(const blink::WebMouseEvent& event_to_coalesce,
class WebMouseWheelEvent; blink::WebMouseEvent* event);
class WebTouchEvent; bool CONTENT_EXPORT
} CanCoalesce(const blink::WebMouseWheelEvent& event_to_coalesce,
const blink::WebMouseWheelEvent& event);
void CONTENT_EXPORT Coalesce(const blink::WebMouseWheelEvent& event_to_coalesce,
blink::WebMouseWheelEvent* event);
bool CONTENT_EXPORT CanCoalesce(const blink::WebTouchEvent& event_to_coalesce,
const blink::WebTouchEvent& event);
void CONTENT_EXPORT Coalesce(const blink::WebTouchEvent& event_to_coalesce,
blink::WebTouchEvent* event);
bool CONTENT_EXPORT CanCoalesce(const blink::WebGestureEvent& event_to_coalesce,
const blink::WebGestureEvent& event);
void CONTENT_EXPORT Coalesce(const blink::WebGestureEvent& event_to_coalesce,
blink::WebGestureEvent* event);
namespace content { } // namespace internal
template <typename T> template <typename T>
class EventWithLatencyInfo { class EventWithLatencyInfo {
...@@ -33,14 +49,26 @@ class EventWithLatencyInfo { ...@@ -33,14 +49,26 @@ class EventWithLatencyInfo {
bool CanCoalesceWith(const EventWithLatencyInfo& other) bool CanCoalesceWith(const EventWithLatencyInfo& other)
const WARN_UNUSED_RESULT { const WARN_UNUSED_RESULT {
return WebInputEventTraits::CanCoalesce(other.event, event); if (other.event.type != event.type)
return false;
DCHECK_EQ(sizeof(T), event.size);
DCHECK_EQ(sizeof(T), other.event.size);
return internal::CanCoalesce(other.event, event);
} }
void CoalesceWith(const EventWithLatencyInfo& other) { void CoalesceWith(const EventWithLatencyInfo& other) {
// |other| should be a newer event than |this|. // |other| should be a newer event than |this|.
if (other.latency.trace_id() >= 0 && latency.trace_id() >= 0) if (other.latency.trace_id() >= 0 && latency.trace_id() >= 0)
DCHECK_GT(other.latency.trace_id(), latency.trace_id()); DCHECK_GT(other.latency.trace_id(), latency.trace_id());
WebInputEventTraits::Coalesce(other.event, &event);
// New events get coalesced into older events, and the newer timestamp
// should always be preserved.
const double time_stamp_seconds = other.event.timeStampSeconds;
internal::Coalesce(other.event, &event);
event.timeStampSeconds = time_stamp_seconds;
// When coalescing two input events, we keep the oldest LatencyInfo // When coalescing two input events, we keep the oldest LatencyInfo
// for Telemetry latency tests, since it will represent the longest // for Telemetry latency tests, since it will represent the longest
// latency. // latency.
......
...@@ -23,10 +23,6 @@ class CONTENT_EXPORT WebInputEventTraits { ...@@ -23,10 +23,6 @@ class CONTENT_EXPORT WebInputEventTraits {
static size_t GetSize(blink::WebInputEvent::Type type); static size_t GetSize(blink::WebInputEvent::Type type);
static ScopedWebInputEvent Clone(const blink::WebInputEvent& event); static ScopedWebInputEvent Clone(const blink::WebInputEvent& event);
static void Delete(blink::WebInputEvent* event); static void Delete(blink::WebInputEvent* event);
static bool CanCoalesce(const blink::WebInputEvent& event_to_coalesce,
const blink::WebInputEvent& event);
static void Coalesce(const blink::WebInputEvent& event_to_coalesce,
blink::WebInputEvent* event);
static bool ShouldBlockEventStream(const blink::WebInputEvent& event); static bool ShouldBlockEventStream(const blink::WebInputEvent& event);
static bool CanCauseScroll(const blink::WebMouseWheelEvent& event); static bool CanCauseScroll(const blink::WebMouseWheelEvent& event);
......
...@@ -363,6 +363,7 @@ ...@@ -363,6 +363,7 @@
'common/indexed_db/indexed_db_param_traits.h', 'common/indexed_db/indexed_db_param_traits.h',
'common/input/did_overscroll_params.cc', 'common/input/did_overscroll_params.cc',
'common/input/did_overscroll_params.h', 'common/input/did_overscroll_params.h',
'common/input/event_with_latency_info.cc',
'common/input/event_with_latency_info.h', 'common/input/event_with_latency_info.h',
'common/input/gesture_event_stream_validator.cc', 'common/input/gesture_event_stream_validator.cc',
'common/input/gesture_event_stream_validator.h', 'common/input/gesture_event_stream_validator.h',
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "content/common/input/synthetic_web_input_event_builders.h" #include "content/common/input/synthetic_web_input_event_builders.h"
#include "content/common/input/web_input_event_traits.h"
#include "content/common/input_messages.h" #include "content/common/input_messages.h"
#include "content/common/view_messages.h" #include "content/common/view_messages.h"
#include "content/renderer/input/input_event_filter.h" #include "content/renderer/input/input_event_filter.h"
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "content/common/input/input_event_ack_state.h" #include "content/common/input/input_event_ack_state.h"
#include "content/common/input/input_event_dispatch_type.h" #include "content/common/input/input_event_dispatch_type.h"
#include "content/common/input/web_input_event_queue.h" #include "content/common/input/web_input_event_queue.h"
#include "content/common/input/web_input_event_traits.h"
#include "third_party/WebKit/public/web/WebInputEvent.h" #include "third_party/WebKit/public/web/WebInputEvent.h"
#include "ui/events/latency_info.h" #include "ui/events/latency_info.h"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment