9a14e8e111f47d83038e3b0d6dcffdbe017b1285
[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             return DropEvent;
192         case CCInputHandlerClient::ScrollFailed:
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::ScrollFailed:
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(IntSize(-gestureEvent.deltaX, -gestureEvent.deltaY));
220         return DidHandle;
221     } else if (event.type == WebInputEvent::GestureScrollEnd) {
222         ASSERT(m_expectScrollUpdateEnd);
223 #ifndef NDEBUG
224         m_expectScrollUpdateEnd = false;
225 #endif
226         if (!m_gestureScrollStarted)
227             return DidNotHandle;
228
229         m_inputHandlerClient->scrollEnd();
230         m_gestureScrollStarted = false;
231         return DidHandle;
232     } else if (event.type == WebInputEvent::GesturePinchBegin) {
233         ASSERT(!m_expectPinchUpdateEnd);
234 #ifndef NDEBUG
235         m_expectPinchUpdateEnd = true;
236 #endif
237         m_inputHandlerClient->pinchGestureBegin();
238         return DidHandle;
239     } else if (event.type == WebInputEvent::GesturePinchEnd) {
240         ASSERT(m_expectPinchUpdateEnd);
241 #ifndef NDEBUG
242         m_expectPinchUpdateEnd = false;
243 #endif
244         m_inputHandlerClient->pinchGestureEnd();
245         return DidHandle;
246     } else if (event.type == WebInputEvent::GesturePinchUpdate) {
247         ASSERT(m_expectPinchUpdateEnd);
248         const WebGestureEvent& gestureEvent = *static_cast<const WebGestureEvent*>(&event);
249         m_inputHandlerClient->pinchGestureUpdate(gestureEvent.deltaX, IntPoint(gestureEvent.x, gestureEvent.y));
250         return DidHandle;
251     } else if (event.type == WebInputEvent::GestureFlingStart) {
252         const WebGestureEvent& gestureEvent = *static_cast<const WebGestureEvent*>(&event);
253         return handleGestureFling(gestureEvent);
254     } else if (event.type == WebInputEvent::GestureFlingCancel) {
255         if (cancelCurrentFling())
256             return DidHandle;
257     } else if (WebInputEvent::isKeyboardEventType(event.type)) {
258          cancelCurrentFling();
259     }
260
261     return DidNotHandle;
262 }
263
264 WebCompositorInputHandlerImpl::EventDisposition WebCompositorInputHandlerImpl::handleGestureFling(const WebGestureEvent& gestureEvent)
265 {
266     CCInputHandlerClient::ScrollStatus scrollStatus = m_inputHandlerClient->scrollBegin(IntPoint(gestureEvent.x, gestureEvent.y), CCInputHandlerClient::Wheel);
267     switch (scrollStatus) {
268     case CCInputHandlerClient::ScrollStarted: {
269         TRACE_EVENT_INSTANT0("cc", "WebCompositorInputHandlerImpl::handleGestureFling::started");
270         OwnPtr<PlatformGestureCurve> flingCurve = TouchpadFlingPlatformGestureCurve::create(FloatPoint(-gestureEvent.deltaX, -gestureEvent.deltaY));
271         m_wheelFlingAnimation = CCActiveGestureAnimation::create(PlatformGestureToCCGestureAdapter::create(flingCurve.release()), this);
272         m_wheelFlingPoint = IntPoint(gestureEvent.x, gestureEvent.y);
273         m_inputHandlerClient->scheduleAnimation();
274         return DidHandle;
275     }
276     case CCInputHandlerClient::ScrollFailed: {
277         TRACE_EVENT_INSTANT0("cc", "WebCompositorInputHandlerImpl::handleGestureFling::failed");
278         return DidNotHandle;
279     }
280     case CCInputHandlerClient::ScrollIgnored: {
281         TRACE_EVENT_INSTANT0("cc", "WebCompositorInputHandlerImpl::handleGestureFling::ignored");
282         // We still pass the curve to the main thread if there's nothing scrollable, in case something
283         // registers a handler before the curve is over.
284         return DidNotHandle;
285     }
286     }
287     return DidNotHandle;
288 }
289
290 int WebCompositorInputHandlerImpl::identifier() const
291 {
292     ASSERT(CCProxy::isImplThread());
293     return m_identifier;
294 }
295
296 void WebCompositorInputHandlerImpl::animate(double monotonicTime)
297 {
298     if (!m_wheelFlingAnimation)
299         return;
300
301     if (m_wheelFlingAnimation->animate(monotonicTime))
302         m_inputHandlerClient->scheduleAnimation();
303     else {
304         TRACE_EVENT_INSTANT0("cc", "WebCompositorInputHandlerImpl::animate::flingOver");
305         m_wheelFlingAnimation.clear();
306     }
307 }
308
309 bool WebCompositorInputHandlerImpl::cancelCurrentFling()
310 {
311     bool hadFlingAnimation = m_wheelFlingAnimation;
312     TRACE_EVENT_INSTANT1("cc", "WebCompositorInputHandlerImpl::cancelCurrentFling", "hadFlingAnimation", hadFlingAnimation);
313     m_wheelFlingAnimation.clear();
314     return hadFlingAnimation;
315 }
316
317 void WebCompositorInputHandlerImpl::scrollBy(const IntPoint& increment)
318 {
319     if (increment == IntPoint::zero())
320         return;
321
322     TRACE_EVENT2("cc", "WebCompositorInputHandlerImpl::scrollBy", "x", increment.x(), "y", increment.y());
323
324     WebMouseWheelEvent event;
325     event.type = WebInputEvent::MouseWheel;
326     event.deltaX = -increment.x();
327     event.deltaY = -increment.y();
328     event.hasPreciseScrollingDeltas = true;
329     event.x = m_wheelFlingPoint.x();
330     event.y = m_wheelFlingPoint.y();
331
332     WebCompositorInputHandlerImpl::EventDisposition disposition = handleInputEventInternal(event);
333     switch (disposition) {
334     case DidHandle:
335     case DropEvent:
336         break;
337     case DidNotHandle:
338         TRACE_EVENT_INSTANT0("cc", "WebCompositorInputHandlerImpl::scrollBy::AbortFling");
339         // FIXME: If we got a DidNotHandle, that means we need to deliver wheels on the main thread.
340         // In this case we need to schedule a commit and transfer the fling curve over to the main
341         // thread and run the rest of the wheels from there.
342         // This can happen when flinging a page that contains a scrollable subarea that we can't
343         // scroll on the thread if the fling starts outside the subarea but then is flung "under" the
344         // pointer.
345         // For now, just abort the fling.
346         cancelCurrentFling();
347     }
348 }
349
350 }