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