[chromium] Send wheel events to main thread even if we think nothing is scrollable
[WebKit-https.git] / Source / WebKit / chromium / src / WebCompositorInputHandlerImpl.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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.
13  *
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.
24  */
25
26 #include "config.h"
27
28 #include "WebCompositorInputHandlerImpl.h"
29
30 #include "PlatformGestureCurveTarget.h"
31 #include "TouchpadFlingPlatformGestureCurve.h"
32 #include "TraceEvent.h"
33 #include "WebCompositorImpl.h"
34 #include "WebCompositorInputHandlerClient.h"
35 #include "WebInputEvent.h"
36 #include "WebKit.h"
37 #include "cc/CCActiveGestureAnimation.h"
38 #include "cc/CCProxy.h"
39 #include "platform/WebKitPlatformSupport.h"
40 #include <wtf/ThreadingPrimitives.h>
41
42 using namespace WebCore;
43
44 namespace WebCore {
45
46 PassOwnPtr<CCInputHandler> CCInputHandler::create(CCInputHandlerClient* inputHandlerClient)
47 {
48     return WebKit::WebCompositorInputHandlerImpl::create(inputHandlerClient);
49 }
50
51 class PlatformGestureToCCGestureAdapter : public CCGestureCurve, public PlatformGestureCurveTarget {
52 public:
53     static PassOwnPtr<CCGestureCurve> create(PassOwnPtr<PlatformGestureCurve> platformCurve)
54     {
55         return adoptPtr(new PlatformGestureToCCGestureAdapter(platformCurve));
56     }
57
58     virtual const char* debugName() const
59     {
60         return m_curve->debugName();
61     }
62
63     virtual bool apply(double time, CCGestureCurveTarget* target)
64     {
65         ASSERT(target);
66         m_target = target;
67         return m_curve->apply(time, this);
68     }
69
70     virtual void scrollBy(const IntPoint& scrollDelta)
71     {
72         ASSERT(m_target);
73         m_target->scrollBy(scrollDelta);
74     }
75
76 private:
77     PlatformGestureToCCGestureAdapter(PassOwnPtr<PlatformGestureCurve> curve)
78         : m_curve(curve)
79         , m_target(0)
80     {
81     }
82
83     OwnPtr<PlatformGestureCurve> m_curve;
84     CCGestureCurveTarget* m_target;
85 };
86
87 }
88
89 namespace WebKit {
90
91 // These statics may only be accessed from the compositor thread.
92 int WebCompositorInputHandlerImpl::s_nextAvailableIdentifier = 1;
93 HashSet<WebCompositorInputHandlerImpl*>* WebCompositorInputHandlerImpl::s_compositors = 0;
94
95 WebCompositorInputHandler* WebCompositorInputHandler::fromIdentifier(int identifier)
96 {
97     return WebCompositorInputHandlerImpl::fromIdentifier(identifier);
98 }
99
100 PassOwnPtr<WebCompositorInputHandlerImpl> WebCompositorInputHandlerImpl::create(WebCore::CCInputHandlerClient* inputHandlerClient)
101 {
102     return adoptPtr(new WebCompositorInputHandlerImpl(inputHandlerClient));
103 }
104
105 WebCompositorInputHandler* WebCompositorInputHandlerImpl::fromIdentifier(int identifier)
106 {
107     ASSERT(WebCompositorImpl::initialized());
108     ASSERT(CCProxy::isImplThread());
109
110     if (!s_compositors)
111         return 0;
112
113     for (HashSet<WebCompositorInputHandlerImpl*>::iterator it = s_compositors->begin(); it != s_compositors->end(); ++it) {
114         if ((*it)->identifier() == identifier)
115             return *it;
116     }
117     return 0;
118 }
119
120 WebCompositorInputHandlerImpl::WebCompositorInputHandlerImpl(CCInputHandlerClient* inputHandlerClient)
121     : m_client(0)
122     , m_identifier(s_nextAvailableIdentifier++)
123     , m_inputHandlerClient(inputHandlerClient)
124 #ifndef NDEBUG
125     , m_expectScrollUpdateEnd(false)
126     , m_expectPinchUpdateEnd(false)
127 #endif
128     , m_gestureScrollStarted(false)
129 {
130     ASSERT(CCProxy::isImplThread());
131
132     if (!s_compositors)
133         s_compositors = new HashSet<WebCompositorInputHandlerImpl*>;
134     s_compositors->add(this);
135 }
136
137 WebCompositorInputHandlerImpl::~WebCompositorInputHandlerImpl()
138 {
139     ASSERT(CCProxy::isImplThread());
140     if (m_client)
141         m_client->willShutdown();
142
143     ASSERT(s_compositors);
144     s_compositors->remove(this);
145     if (!s_compositors->size()) {
146         delete s_compositors;
147         s_compositors = 0;
148     }
149 }
150
151 void WebCompositorInputHandlerImpl::setClient(WebCompositorInputHandlerClient* client)
152 {
153     ASSERT(CCProxy::isImplThread());
154     // 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.
155     ASSERT(!m_client || !client);
156     m_client = client;
157 }
158
159 void WebCompositorInputHandlerImpl::handleInputEvent(const WebInputEvent& event)
160 {
161     ASSERT(CCProxy::isImplThread());
162     ASSERT(m_client);
163
164     WebCompositorInputHandlerImpl::EventDisposition disposition = handleInputEventInternal(event);
165     switch (disposition) {
166     case DidHandle:
167         m_client->didHandleInputEvent();
168         break;
169     case DidNotHandle:
170         m_client->didNotHandleInputEvent(true /* sendToWidget */);
171         break;
172     case DropEvent:
173         m_client->didNotHandleInputEvent(false /* sendToWidget */);
174         break;
175     }
176 }
177
178 WebCompositorInputHandlerImpl::EventDisposition WebCompositorInputHandlerImpl::handleInputEventInternal(const WebInputEvent& event)
179 {
180     if (event.type == WebInputEvent::MouseWheel) {
181         const WebMouseWheelEvent& wheelEvent = *static_cast<const WebMouseWheelEvent*>(&event);
182         CCInputHandlerClient::ScrollStatus scrollStatus = m_inputHandlerClient->scrollBegin(IntPoint(wheelEvent.x, wheelEvent.y), CCInputHandlerClient::Wheel);
183         switch (scrollStatus) {
184         case CCInputHandlerClient::ScrollStarted: {
185             TRACE_EVENT_INSTANT2("cc", "WebCompositorInputHandlerImpl::handleInput wheel scroll", "deltaX", -wheelEvent.deltaX, "deltaY", -wheelEvent.deltaY);
186             m_inputHandlerClient->scrollBy(IntSize(-wheelEvent.deltaX, -wheelEvent.deltaY));
187             m_inputHandlerClient->scrollEnd();
188             return DidHandle;
189         }
190         case CCInputHandlerClient::ScrollIgnored:
191             // FIXME: This should be DropEvent, but in cases where we fail to properly sync scrollability it's safer to send the
192             // event to the main thread. Change back to DropEvent once we have synchronization bugs sorted out.
193             return DidNotHandle; 
194         case CCInputHandlerClient::ScrollFailed:
195             return DidNotHandle;
196         }
197     } else if (event.type == WebInputEvent::GestureScrollBegin) {
198         ASSERT(!m_gestureScrollStarted);
199         ASSERT(!m_expectScrollUpdateEnd);
200 #ifndef NDEBUG
201         m_expectScrollUpdateEnd = true;
202 #endif
203         const WebGestureEvent& gestureEvent = *static_cast<const WebGestureEvent*>(&event);
204         CCInputHandlerClient::ScrollStatus scrollStatus = m_inputHandlerClient->scrollBegin(IntPoint(gestureEvent.x, gestureEvent.y), CCInputHandlerClient::Gesture);
205         switch (scrollStatus) {
206         case CCInputHandlerClient::ScrollStarted:
207             m_gestureScrollStarted = true;
208             return DidHandle;
209         case CCInputHandlerClient::ScrollFailed:
210             return DidNotHandle;
211         case CCInputHandlerClient::ScrollIgnored:
212             return DropEvent;
213         }
214     } else if (event.type == WebInputEvent::GestureScrollUpdate) {
215         ASSERT(m_expectScrollUpdateEnd);
216
217         if (!m_gestureScrollStarted)
218             return DidNotHandle;
219
220         const WebGestureEvent& gestureEvent = *static_cast<const WebGestureEvent*>(&event);
221         m_inputHandlerClient->scrollBy(IntSize(-gestureEvent.deltaX, -gestureEvent.deltaY));
222         return DidHandle;
223     } else if (event.type == WebInputEvent::GestureScrollEnd) {
224         ASSERT(m_expectScrollUpdateEnd);
225 #ifndef NDEBUG
226         m_expectScrollUpdateEnd = false;
227 #endif
228         if (!m_gestureScrollStarted)
229             return DidNotHandle;
230
231         m_inputHandlerClient->scrollEnd();
232         m_gestureScrollStarted = false;
233         return DidHandle;
234     } else if (event.type == WebInputEvent::GesturePinchBegin) {
235         ASSERT(!m_expectPinchUpdateEnd);
236 #ifndef NDEBUG
237         m_expectPinchUpdateEnd = true;
238 #endif
239         m_inputHandlerClient->pinchGestureBegin();
240         return DidHandle;
241     } else if (event.type == WebInputEvent::GesturePinchEnd) {
242         ASSERT(m_expectPinchUpdateEnd);
243 #ifndef NDEBUG
244         m_expectPinchUpdateEnd = false;
245 #endif
246         m_inputHandlerClient->pinchGestureEnd();
247         return DidHandle;
248     } else if (event.type == WebInputEvent::GesturePinchUpdate) {
249         ASSERT(m_expectPinchUpdateEnd);
250         const WebGestureEvent& gestureEvent = *static_cast<const WebGestureEvent*>(&event);
251         m_inputHandlerClient->pinchGestureUpdate(gestureEvent.deltaX, IntPoint(gestureEvent.x, gestureEvent.y));
252         return DidHandle;
253     } else if (event.type == WebInputEvent::GestureFlingStart) {
254         const WebGestureEvent& gestureEvent = *static_cast<const WebGestureEvent*>(&event);
255         return handleGestureFling(gestureEvent);
256     } else if (event.type == WebInputEvent::GestureFlingCancel) {
257         if (cancelCurrentFling())
258             return DidHandle;
259     } else if (WebInputEvent::isKeyboardEventType(event.type)) {
260          cancelCurrentFling();
261     }
262
263     return DidNotHandle;
264 }
265
266 WebCompositorInputHandlerImpl::EventDisposition WebCompositorInputHandlerImpl::handleGestureFling(const WebGestureEvent& gestureEvent)
267 {
268     CCInputHandlerClient::ScrollStatus scrollStatus = m_inputHandlerClient->scrollBegin(IntPoint(gestureEvent.x, gestureEvent.y), CCInputHandlerClient::Wheel);
269     switch (scrollStatus) {
270     case CCInputHandlerClient::ScrollStarted: {
271         TRACE_EVENT_INSTANT0("cc", "WebCompositorInputHandlerImpl::handleGestureFling::started");
272         OwnPtr<PlatformGestureCurve> flingCurve = TouchpadFlingPlatformGestureCurve::create(FloatPoint(-gestureEvent.deltaX, -gestureEvent.deltaY));
273         m_wheelFlingAnimation = CCActiveGestureAnimation::create(PlatformGestureToCCGestureAdapter::create(flingCurve.release()), this);
274         m_wheelFlingPoint = IntPoint(gestureEvent.x, gestureEvent.y);
275         m_inputHandlerClient->scheduleAnimation();
276         return DidHandle;
277     }
278     case CCInputHandlerClient::ScrollFailed: {
279         TRACE_EVENT_INSTANT0("cc", "WebCompositorInputHandlerImpl::handleGestureFling::failed");
280         return DidNotHandle;
281     }
282     case CCInputHandlerClient::ScrollIgnored: {
283         TRACE_EVENT_INSTANT0("cc", "WebCompositorInputHandlerImpl::handleGestureFling::ignored");
284         // We still pass the curve to the main thread if there's nothing scrollable, in case something
285         // registers a handler before the curve is over.
286         return DidNotHandle;
287     }
288     }
289     return DidNotHandle;
290 }
291
292 int WebCompositorInputHandlerImpl::identifier() const
293 {
294     ASSERT(CCProxy::isImplThread());
295     return m_identifier;
296 }
297
298 void WebCompositorInputHandlerImpl::animate(double monotonicTime)
299 {
300     if (!m_wheelFlingAnimation)
301         return;
302
303     if (m_wheelFlingAnimation->animate(monotonicTime))
304         m_inputHandlerClient->scheduleAnimation();
305     else {
306         TRACE_EVENT_INSTANT0("cc", "WebCompositorInputHandlerImpl::animate::flingOver");
307         m_wheelFlingAnimation.clear();
308     }
309 }
310
311 bool WebCompositorInputHandlerImpl::cancelCurrentFling()
312 {
313     bool hadFlingAnimation = m_wheelFlingAnimation;
314     TRACE_EVENT_INSTANT1("cc", "WebCompositorInputHandlerImpl::cancelCurrentFling", "hadFlingAnimation", hadFlingAnimation);
315     m_wheelFlingAnimation.clear();
316     return hadFlingAnimation;
317 }
318
319 void WebCompositorInputHandlerImpl::scrollBy(const IntPoint& increment)
320 {
321     if (increment == IntPoint::zero())
322         return;
323
324     TRACE_EVENT2("cc", "WebCompositorInputHandlerImpl::scrollBy", "x", increment.x(), "y", increment.y());
325
326     WebMouseWheelEvent event;
327     event.type = WebInputEvent::MouseWheel;
328     event.deltaX = -increment.x();
329     event.deltaY = -increment.y();
330     event.hasPreciseScrollingDeltas = true;
331     event.x = m_wheelFlingPoint.x();
332     event.y = m_wheelFlingPoint.y();
333
334     WebCompositorInputHandlerImpl::EventDisposition disposition = handleInputEventInternal(event);
335     switch (disposition) {
336     case DidHandle:
337     case DropEvent:
338         break;
339     case DidNotHandle:
340         TRACE_EVENT_INSTANT0("cc", "WebCompositorInputHandlerImpl::scrollBy::AbortFling");
341         // FIXME: If we got a DidNotHandle, that means we need to deliver wheels on the main thread.
342         // In this case we need to schedule a commit and transfer the fling curve over to the main
343         // thread and run the rest of the wheels from there.
344         // This can happen when flinging a page that contains a scrollable subarea that we can't
345         // scroll on the thread if the fling starts outside the subarea but then is flung "under" the
346         // pointer.
347         // For now, just abort the fling.
348         cancelCurrentFling();
349     }
350 }
351
352 }