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