tizen beta release
[framework/web/webkit-efl.git] / Source / WebKit / chromium / src / WebCompositorImpl.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 "WebCompositorImpl.h"
29
30 #include "CCThreadImpl.h"
31 #include "WebCompositorClient.h"
32 #include "WebInputEvent.h"
33 #include "cc/CCInputHandler.h"
34 #include "cc/CCThreadProxy.h"
35 #include <wtf/ThreadingPrimitives.h>
36
37 using namespace WebCore;
38
39 namespace WebCore {
40
41 PassOwnPtr<CCInputHandler> CCInputHandler::create(CCInputHandlerClient* inputHandlerClient)
42 {
43     return WebKit::WebCompositorImpl::create(inputHandlerClient);
44 }
45
46 }
47
48 namespace WebKit {
49
50 void WebCompositor::setThread(WebThread* compositorThread)
51 {
52     ASSERT(compositorThread);
53     CCThreadProxy::setImplThread(CCThreadImpl::create(compositorThread).leakPtr());
54 }
55
56
57 // These statics may only be accessed from the compositor thread.
58 int WebCompositorImpl::s_nextAvailableIdentifier = 1;
59 HashSet<WebCompositorImpl*>* WebCompositorImpl::s_compositors = 0;
60
61 WebCompositor* WebCompositor::fromIdentifier(int identifier)
62 {
63     ASSERT(CCProxy::isImplThread());
64     return WebCompositorImpl::fromIdentifier(identifier);
65 }
66
67 WebCompositor* WebCompositorImpl::fromIdentifier(int identifier)
68 {
69     ASSERT(CCProxy::isImplThread());
70
71     if (!s_compositors)
72         return 0;
73
74     for (HashSet<WebCompositorImpl*>::iterator it = s_compositors->begin(); it != s_compositors->end(); ++it) {
75         if ((*it)->identifier() == identifier)
76             return *it;
77     }
78     return 0;
79 }
80
81 WebCompositorImpl::WebCompositorImpl(CCInputHandlerClient* inputHandlerClient)
82     : m_client(0)
83     , m_identifier(s_nextAvailableIdentifier++)
84     , m_inputHandlerClient(inputHandlerClient)
85 {
86     ASSERT(CCProxy::isImplThread());
87
88     if (!s_compositors)
89         s_compositors = new HashSet<WebCompositorImpl*>;
90     s_compositors->add(this);
91 }
92
93 WebCompositorImpl::~WebCompositorImpl()
94 {
95     ASSERT(CCProxy::isImplThread());
96     if (m_client)
97         m_client->willShutdown();
98
99     ASSERT(s_compositors);
100     s_compositors->remove(this);
101     if (!s_compositors->size()) {
102         delete s_compositors;
103         s_compositors = 0;
104     }
105 }
106
107 void WebCompositorImpl::setClient(WebCompositorClient* client)
108 {
109     ASSERT(CCProxy::isImplThread());
110     // 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.
111     ASSERT(!m_client || !client);
112     m_client = client;
113 }
114
115 void WebCompositorImpl::handleInputEvent(const WebInputEvent& event)
116 {
117     ASSERT(CCProxy::isImplThread());
118     ASSERT(m_client);
119
120     if (event.type == WebInputEvent::MouseWheel && !m_inputHandlerClient->haveWheelEventHandlers()) {
121         const WebMouseWheelEvent& wheelEvent = *static_cast<const WebMouseWheelEvent*>(&event);
122         m_inputHandlerClient->scrollRootLayer(IntSize(-wheelEvent.deltaX, -wheelEvent.deltaY));
123         m_client->didHandleInputEvent();
124         return;
125     }
126     m_client->didNotHandleInputEvent(true /* sendToWidget */);
127 }
128
129 int WebCompositorImpl::identifier() const
130 {
131     ASSERT(CCProxy::isImplThread());
132     return m_identifier;
133 }
134
135 void WebCompositorImpl::willDraw(double frameBeginTimeMs)
136 {
137 }
138
139 }