2
* Copyright (C) 2011 Google Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
14
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#include "WebCompositorInputHandlerImpl.h"
30
#include "TraceEvent.h"
31
#include "WebCompositorInputHandlerClient.h"
32
#include "WebInputEvent.h"
33
#include <public/Platform.h>
34
#include <public/WebInputHandlerClient.h>
35
#include <wtf/PassOwnPtr.h>
36
#include <wtf/ThreadingPrimitives.h>
38
using namespace WebCore;
42
// These statics may only be accessed from the compositor thread.
43
int WebCompositorInputHandlerImpl::s_nextAvailableIdentifier = 1;
44
HashSet<WebCompositorInputHandlerImpl*>* WebCompositorInputHandlerImpl::s_compositors = 0;
46
WebCompositorInputHandler* WebCompositorInputHandler::fromIdentifier(int identifier)
48
return WebCompositorInputHandlerImpl::fromIdentifier(identifier);
51
WebCompositorInputHandler* WebCompositorInputHandlerImpl::fromIdentifier(int identifier)
57
for (HashSet<WebCompositorInputHandlerImpl*>::iterator it = s_compositors->begin(); it != s_compositors->end(); ++it) {
58
if ((*it)->identifier() == identifier)
64
WebCompositorInputHandlerImpl::WebCompositorInputHandlerImpl()
66
, m_identifier(s_nextAvailableIdentifier++)
67
, m_inputHandlerClient(0)
69
, m_expectScrollUpdateEnd(false)
70
, m_expectPinchUpdateEnd(false)
72
, m_gestureScrollOnImplThread(false)
73
, m_gesturePinchOnImplThread(false)
77
WebCompositorInputHandlerImpl::~WebCompositorInputHandlerImpl()
80
m_client->willShutdown();
82
ASSERT(s_compositors);
83
s_compositors->remove(this);
84
if (!s_compositors->size()) {
90
void WebCompositorInputHandlerImpl::setClient(WebCompositorInputHandlerClient* client)
92
// It's valid to set a new client if we've never had one or to clear the client, but it's not valid to change from having one client to a different one.
93
ASSERT(!m_client || !client);
97
void WebCompositorInputHandlerImpl::handleInputEvent(const WebInputEvent& event)
101
WebCompositorInputHandlerImpl::EventDisposition disposition = handleInputEventInternal(event);
102
switch (disposition) {
104
m_client->didHandleInputEvent();
107
m_client->didNotHandleInputEvent(true /* sendToWidget */);
110
m_client->didNotHandleInputEvent(false /* sendToWidget */);
115
WebCompositorInputHandlerImpl::EventDisposition WebCompositorInputHandlerImpl::handleInputEventInternal(const WebInputEvent& event)
117
if (event.type == WebInputEvent::MouseWheel) {
118
const WebMouseWheelEvent& wheelEvent = *static_cast<const WebMouseWheelEvent*>(&event);
119
WebInputHandlerClient::ScrollStatus scrollStatus = m_inputHandlerClient->scrollBegin(WebPoint(wheelEvent.x, wheelEvent.y), WebInputHandlerClient::ScrollInputTypeWheel);
120
switch (scrollStatus) {
121
case WebInputHandlerClient::ScrollStatusStarted: {
122
TRACE_EVENT_INSTANT2("webkit", "WebCompositorInputHandlerImpl::handleInput wheel scroll", "deltaX", -wheelEvent.deltaX, "deltaY", -wheelEvent.deltaY);
123
bool didScroll = m_inputHandlerClient->scrollByIfPossible(WebPoint(wheelEvent.x, wheelEvent.y), IntSize(-wheelEvent.deltaX, -wheelEvent.deltaY));
124
m_inputHandlerClient->scrollEnd();
125
return didScroll ? DidHandle : DropEvent;
127
case WebInputHandlerClient::ScrollStatusIgnored:
128
// FIXME: This should be DropEvent, but in cases where we fail to properly sync scrollability it's safer to send the
129
// event to the main thread. Change back to DropEvent once we have synchronization bugs sorted out.
131
case WebInputHandlerClient::ScrollStatusOnMainThread:
134
} else if (event.type == WebInputEvent::GestureScrollBegin) {
135
ASSERT(!m_gestureScrollOnImplThread);
136
ASSERT(!m_expectScrollUpdateEnd);
138
m_expectScrollUpdateEnd = true;
140
const WebGestureEvent& gestureEvent = *static_cast<const WebGestureEvent*>(&event);
141
WebInputHandlerClient::ScrollStatus scrollStatus = m_inputHandlerClient->scrollBegin(WebPoint(gestureEvent.x, gestureEvent.y), WebInputHandlerClient::ScrollInputTypeGesture);
142
switch (scrollStatus) {
143
case WebInputHandlerClient::ScrollStatusStarted:
144
m_gestureScrollOnImplThread = true;
146
case WebInputHandlerClient::ScrollStatusOnMainThread:
148
case WebInputHandlerClient::ScrollStatusIgnored:
151
} else if (event.type == WebInputEvent::GestureScrollUpdate) {
152
ASSERT(m_expectScrollUpdateEnd);
154
if (!m_gestureScrollOnImplThread && !m_gesturePinchOnImplThread)
157
const WebGestureEvent& gestureEvent = *static_cast<const WebGestureEvent*>(&event);
158
bool didScroll = m_inputHandlerClient->scrollByIfPossible(WebPoint(gestureEvent.x, gestureEvent.y),
159
IntSize(-gestureEvent.data.scrollUpdate.deltaX, -gestureEvent.data.scrollUpdate.deltaY));
160
return didScroll ? DidHandle : DropEvent;
161
} else if (event.type == WebInputEvent::GestureScrollEnd) {
162
ASSERT(m_expectScrollUpdateEnd);
164
m_expectScrollUpdateEnd = false;
166
if (!m_gestureScrollOnImplThread)
169
m_inputHandlerClient->scrollEnd();
170
m_gestureScrollOnImplThread = false;
172
} else if (event.type == WebInputEvent::GesturePinchBegin) {
173
ASSERT(!m_expectPinchUpdateEnd);
175
m_expectPinchUpdateEnd = true;
177
m_inputHandlerClient->pinchGestureBegin();
178
m_gesturePinchOnImplThread = true;
180
} else if (event.type == WebInputEvent::GesturePinchEnd) {
181
ASSERT(m_expectPinchUpdateEnd);
183
m_expectPinchUpdateEnd = false;
185
m_gesturePinchOnImplThread = false;
186
m_inputHandlerClient->pinchGestureEnd();
188
} else if (event.type == WebInputEvent::GesturePinchUpdate) {
189
ASSERT(m_expectPinchUpdateEnd);
190
const WebGestureEvent& gestureEvent = *static_cast<const WebGestureEvent*>(&event);
191
m_inputHandlerClient->pinchGestureUpdate(gestureEvent.data.pinchUpdate.scale, WebPoint(gestureEvent.x, gestureEvent.y));
193
} else if (event.type == WebInputEvent::GestureFlingStart) {
194
const WebGestureEvent& gestureEvent = *static_cast<const WebGestureEvent*>(&event);
195
return handleGestureFling(gestureEvent);
196
} else if (event.type == WebInputEvent::GestureFlingCancel) {
197
if (cancelCurrentFling())
199
} else if (WebInputEvent::isKeyboardEventType(event.type)) {
200
cancelCurrentFling();
206
WebCompositorInputHandlerImpl::EventDisposition WebCompositorInputHandlerImpl::handleGestureFling(const WebGestureEvent& gestureEvent)
208
WebInputHandlerClient::ScrollStatus scrollStatus = m_inputHandlerClient->scrollBegin(WebPoint(gestureEvent.x, gestureEvent.y), WebInputHandlerClient::ScrollInputTypeGesture);
209
switch (scrollStatus) {
210
case WebInputHandlerClient::ScrollStatusStarted: {
211
if (gestureEvent.data.flingStart.sourceDevice == WebGestureEvent::Touchpad)
212
m_inputHandlerClient->scrollEnd();
213
m_flingCurve = adoptPtr(Platform::current()->createFlingAnimationCurve(gestureEvent.data.flingStart.sourceDevice, WebFloatPoint(gestureEvent.data.flingStart.velocityX, gestureEvent.data.flingStart.velocityY), WebSize()));
214
TRACE_EVENT_ASYNC_BEGIN0("webkit", "WebCompositorInputHandlerImpl::handleGestureFling::started", this);
215
m_flingParameters.delta = WebFloatPoint(gestureEvent.data.flingStart.velocityX, gestureEvent.data.flingStart.velocityY);
216
m_flingParameters.point = WebPoint(gestureEvent.x, gestureEvent.y);
217
m_flingParameters.globalPoint = WebPoint(gestureEvent.globalX, gestureEvent.globalY);
218
m_flingParameters.modifiers = gestureEvent.modifiers;
219
m_flingParameters.sourceDevice = gestureEvent.data.flingStart.sourceDevice;
220
m_inputHandlerClient->scheduleAnimation();
223
case WebInputHandlerClient::ScrollStatusOnMainThread: {
224
TRACE_EVENT_INSTANT0("webkit", "WebCompositorInputHandlerImpl::handleGestureFling::scrollOnMainThread");
227
case WebInputHandlerClient::ScrollStatusIgnored: {
228
TRACE_EVENT_INSTANT0("webkit", "WebCompositorInputHandlerImpl::handleGestureFling::ignored");
229
if (gestureEvent.data.flingStart.sourceDevice == WebGestureEvent::Touchpad) {
230
// We still pass the curve to the main thread if there's nothing scrollable, in case something
231
// registers a handler before the curve is over.
240
void WebCompositorInputHandlerImpl::bindToClient(WebInputHandlerClient* client)
242
ASSERT(!m_inputHandlerClient);
244
TRACE_EVENT_INSTANT0("webkit", "WebCompositorInputHandlerImpl::bindToClient");
246
s_compositors = new HashSet<WebCompositorInputHandlerImpl*>;
247
s_compositors->add(this);
249
m_inputHandlerClient = client;
252
void WebCompositorInputHandlerImpl::animate(double monotonicTime)
257
if (!m_flingParameters.startTime) {
258
m_flingParameters.startTime = monotonicTime;
259
m_inputHandlerClient->scheduleAnimation();
263
if (m_flingCurve->apply(monotonicTime - m_flingParameters.startTime, this))
264
m_inputHandlerClient->scheduleAnimation();
266
TRACE_EVENT_INSTANT0("webkit", "WebCompositorInputHandlerImpl::animate::flingOver");
267
cancelCurrentFling();
271
bool WebCompositorInputHandlerImpl::cancelCurrentFling()
273
bool hadFlingAnimation = m_flingCurve;
274
if (hadFlingAnimation && m_flingParameters.sourceDevice == WebGestureEvent::Touchscreen) {
275
m_inputHandlerClient->scrollEnd();
276
TRACE_EVENT_ASYNC_END0("webkit", "WebCompositorInputHandlerImpl::handleGestureFling::started", this);
279
TRACE_EVENT_INSTANT1("webkit", "WebCompositorInputHandlerImpl::cancelCurrentFling", "hadFlingAnimation", hadFlingAnimation);
280
m_flingCurve.clear();
281
m_flingParameters = WebActiveWheelFlingParameters();
282
return hadFlingAnimation;
285
bool WebCompositorInputHandlerImpl::touchpadFlingScroll(const WebPoint& increment)
287
WebMouseWheelEvent syntheticWheel;
288
syntheticWheel.type = WebInputEvent::MouseWheel;
289
syntheticWheel.deltaX = increment.x;
290
syntheticWheel.deltaY = increment.y;
291
syntheticWheel.hasPreciseScrollingDeltas = true;
292
syntheticWheel.x = m_flingParameters.point.x;
293
syntheticWheel.y = m_flingParameters.point.y;
294
syntheticWheel.globalX = m_flingParameters.globalPoint.x;
295
syntheticWheel.globalY = m_flingParameters.globalPoint.y;
296
syntheticWheel.modifiers = m_flingParameters.modifiers;
298
WebCompositorInputHandlerImpl::EventDisposition disposition = handleInputEventInternal(syntheticWheel);
299
switch (disposition) {
305
TRACE_EVENT_INSTANT0("webkit", "WebCompositorInputHandlerImpl::scrollBy::AbortFling");
306
// If we got a DidNotHandle, that means we need to deliver wheels on the main thread.
307
// In this case we need to schedule a commit and transfer the fling curve over to the main
308
// thread and run the rest of the wheels from there.
309
// This can happen when flinging a page that contains a scrollable subarea that we can't
310
// scroll on the thread if the fling starts outside the subarea but then is flung "under" the
312
m_client->transferActiveWheelFlingAnimation(m_flingParameters);
313
cancelCurrentFling();
320
void WebCompositorInputHandlerImpl::scrollBy(const WebPoint& increment)
322
if (increment == WebPoint())
325
TRACE_EVENT2("webkit", "WebCompositorInputHandlerImpl::scrollBy", "x", increment.x, "y", increment.y);
327
bool didScroll = false;
329
switch (m_flingParameters.sourceDevice) {
330
case WebGestureEvent::Touchpad:
331
didScroll = touchpadFlingScroll(increment);
333
case WebGestureEvent::Touchscreen:
334
didScroll = m_inputHandlerClient->scrollByIfPossible(m_flingParameters.point, IntSize(-increment.x, -increment.y));
339
m_flingParameters.cumulativeScroll.width += increment.x;
340
m_flingParameters.cumulativeScroll.height += increment.y;