This sets up API to release OutputSurface from LTHClient.
[chromium-blink-merge.git] / content / browser / renderer_host / input / motion_event_web.cc
blob82b6ed7861b6cd2c5c3fbe0fe8cc853b8d7c7772
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
3 // found in the LICENSE file.
5 // MSVC++ requires this to be set before any other includes to get M_PI.
6 #define _USE_MATH_DEFINES
8 #include "content/browser/renderer_host/input/motion_event_web.h"
10 #include <cmath>
12 #include "base/logging.h"
13 #include "content/browser/renderer_host/input/web_input_event_util.h"
14 #include "content/common/input/web_touch_event_traits.h"
16 using blink::WebInputEvent;
17 using blink::WebPointerProperties;
18 using blink::WebTouchEvent;
19 using blink::WebTouchPoint;
21 namespace content {
22 namespace {
24 ui::MotionEvent::Action GetActionFrom(const WebTouchEvent& event) {
25 DCHECK(event.touchesLength);
26 switch (event.type) {
27 case WebInputEvent::TouchStart:
28 if (WebTouchEventTraits::AllTouchPointsHaveState(
29 event, WebTouchPoint::StatePressed))
30 return ui::MotionEvent::ACTION_DOWN;
31 else
32 return ui::MotionEvent::ACTION_POINTER_DOWN;
33 case WebInputEvent::TouchEnd:
34 if (WebTouchEventTraits::AllTouchPointsHaveState(
35 event, WebTouchPoint::StateReleased))
36 return ui::MotionEvent::ACTION_UP;
37 else
38 return ui::MotionEvent::ACTION_POINTER_UP;
39 case WebInputEvent::TouchCancel:
40 DCHECK(WebTouchEventTraits::AllTouchPointsHaveState(
41 event, WebTouchPoint::StateCancelled));
42 return ui::MotionEvent::ACTION_CANCEL;
43 case WebInputEvent::TouchMove:
44 return ui::MotionEvent::ACTION_MOVE;
45 default:
46 break;
48 NOTREACHED()
49 << "Unable to derive a valid MotionEvent::Action from the WebTouchEvent.";
50 return ui::MotionEvent::ACTION_CANCEL;
53 int GetActionIndexFrom(const WebTouchEvent& event) {
54 for (size_t i = 0; i < event.touchesLength; ++i) {
55 if (event.touches[i].state != WebTouchPoint::StateUndefined &&
56 event.touches[i].state != WebTouchPoint::StateStationary)
57 return i;
59 return -1;
62 } // namespace
64 MotionEventWeb::MotionEventWeb(const WebTouchEvent& event)
65 : event_(event),
66 cached_action_(GetActionFrom(event)),
67 cached_action_index_(GetActionIndexFrom(event)),
68 unique_event_id_(event.uniqueTouchEventId) {
69 DCHECK_GT(GetPointerCount(), 0U);
72 MotionEventWeb::~MotionEventWeb() {}
74 uint32 MotionEventWeb::GetUniqueEventId() const {
75 return unique_event_id_;
78 MotionEventWeb::Action MotionEventWeb::GetAction() const {
79 return cached_action_;
82 int MotionEventWeb::GetActionIndex() const {
83 DCHECK(cached_action_ == ACTION_POINTER_UP ||
84 cached_action_ == ACTION_POINTER_DOWN)
85 << "Invalid action for GetActionIndex(): " << cached_action_;
86 DCHECK_GE(cached_action_index_, 0);
87 DCHECK_LT(cached_action_index_, static_cast<int>(event_.touchesLength));
88 return cached_action_index_;
91 size_t MotionEventWeb::GetPointerCount() const { return event_.touchesLength; }
93 int MotionEventWeb::GetPointerId(size_t pointer_index) const {
94 DCHECK_LT(pointer_index, GetPointerCount());
95 return event_.touches[pointer_index].id;
98 float MotionEventWeb::GetX(size_t pointer_index) const {
99 DCHECK_LT(pointer_index, GetPointerCount());
100 return event_.touches[pointer_index].position.x;
103 float MotionEventWeb::GetY(size_t pointer_index) const {
104 DCHECK_LT(pointer_index, GetPointerCount());
105 return event_.touches[pointer_index].position.y;
108 float MotionEventWeb::GetRawX(size_t pointer_index) const {
109 DCHECK_LT(pointer_index, GetPointerCount());
110 return event_.touches[pointer_index].screenPosition.x;
113 float MotionEventWeb::GetRawY(size_t pointer_index) const {
114 DCHECK_LT(pointer_index, GetPointerCount());
115 return event_.touches[pointer_index].screenPosition.y;
118 float MotionEventWeb::GetTouchMajor(size_t pointer_index) const {
119 DCHECK_LT(pointer_index, GetPointerCount());
120 return 2.f * std::max(event_.touches[pointer_index].radiusX,
121 event_.touches[pointer_index].radiusY);
124 float MotionEventWeb::GetTouchMinor(size_t pointer_index) const {
125 DCHECK_LT(pointer_index, GetPointerCount());
126 return 2.f * std::min(event_.touches[pointer_index].radiusX,
127 event_.touches[pointer_index].radiusY);
130 float MotionEventWeb::GetOrientation(size_t pointer_index) const {
131 DCHECK_LT(pointer_index, GetPointerCount());
133 float rotation_angle_rad = event_.touches[pointer_index].rotationAngle
134 * M_PI / 180.f;
135 DCHECK(0 <= rotation_angle_rad && rotation_angle_rad <= M_PI_2)
136 << "Unexpected touch rotation angle";
138 if (event_.touches[pointer_index].radiusX
139 > event_.touches[pointer_index].radiusY) {
140 // The case radiusX == radiusY is omitted from here on purpose: for circles,
141 // we want to pass the angle (which could be any value in such cases but
142 // always seem to be set to zero) unchanged.
143 rotation_angle_rad -= (float) M_PI_2;
146 return rotation_angle_rad;
149 float MotionEventWeb::GetPressure(size_t pointer_index) const {
150 return 0.f;
153 base::TimeTicks MotionEventWeb::GetEventTime() const {
154 return base::TimeTicks() +
155 base::TimeDelta::FromMicroseconds(event_.timeStampSeconds *
156 base::Time::kMicrosecondsPerSecond);
159 ui::MotionEvent::ToolType MotionEventWeb::GetToolType(
160 size_t pointer_index) const {
161 DCHECK_LT(pointer_index, GetPointerCount());
163 const WebPointerProperties& pointer = event_.touches[pointer_index];
165 switch (pointer.pointerType) {
166 case WebPointerProperties::PointerTypeUnknown:
167 return TOOL_TYPE_UNKNOWN;
168 case WebPointerProperties::PointerTypeMouse:
169 return TOOL_TYPE_MOUSE;
170 case WebPointerProperties::PointerTypePen:
171 return TOOL_TYPE_STYLUS;
172 case WebPointerProperties::PointerTypeTouch:
173 return TOOL_TYPE_FINGER;
174 default:
175 NOTREACHED() << "Unhandled pointer type " << pointer.pointerType;
176 return TOOL_TYPE_UNKNOWN;
180 int MotionEventWeb::GetButtonState() const {
181 return 0;
184 int MotionEventWeb::GetFlags() const {
185 return WebEventModifiersToEventFlags(event_.modifiers);
188 } // namespace content