[chromium] Hook WebCompositor interface for input events up to the compositor proper
[WebKit-https.git] / Source / WebCore / platform / graphics / chromium / cc / CCLayerTreeHostImpl.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  * 1.  Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "config.h"
26
27 #include "cc/CCLayerTreeHostImpl.h"
28
29 #include "Extensions3D.h"
30 #include "GraphicsContext3D.h"
31 #include "LayerRendererChromium.h"
32 #include "TraceEvent.h"
33 #include "cc/CCLayerTreeHost.h"
34 #include "cc/CCMainThread.h"
35 #include "cc/CCMainThreadTask.h"
36 #include "cc/CCThreadTask.h"
37 #include <wtf/CurrentTime.h>
38
39 namespace WebCore {
40
41 PassOwnPtr<CCLayerTreeHostImpl> CCLayerTreeHostImpl::create(const CCSettings& settings)
42 {
43     return adoptPtr(new CCLayerTreeHostImpl(settings));
44 }
45
46 CCLayerTreeHostImpl::CCLayerTreeHostImpl(const CCSettings& settings)
47     : m_sourceFrameNumber(-1)
48     , m_frameNumber(0)
49     , m_settings(settings)
50 {
51     ASSERT(CCProxy::isImplThread());
52 }
53
54 CCLayerTreeHostImpl::~CCLayerTreeHostImpl()
55 {
56     ASSERT(CCProxy::isImplThread());
57     TRACE_EVENT("CCLayerTreeHostImpl::~CCLayerTreeHostImpl()", this, 0);
58     if (m_layerRenderer)
59         m_layerRenderer->close();
60 }
61
62 void CCLayerTreeHostImpl::beginCommit()
63 {
64 }
65
66 void CCLayerTreeHostImpl::commitComplete()
67 {
68 }
69
70 GraphicsContext3D* CCLayerTreeHostImpl::context()
71 {
72     return m_layerRenderer ? m_layerRenderer->context() : 0;
73 }
74
75 void CCLayerTreeHostImpl::drawLayers()
76 {
77     TRACE_EVENT("CCLayerTreeHostImpl::drawLayers", this, 0);
78     ASSERT(m_layerRenderer);
79     if (m_layerRenderer->rootLayer())
80         m_layerRenderer->drawLayers();
81
82     ++m_frameNumber;
83 }
84
85 void CCLayerTreeHostImpl::finishAllRendering()
86 {
87     m_layerRenderer->finish();
88 }
89
90 bool CCLayerTreeHostImpl::isContextLost()
91 {
92     ASSERT(m_layerRenderer);
93     return m_layerRenderer->isContextLost();
94 }
95
96 const LayerRendererCapabilities& CCLayerTreeHostImpl::layerRendererCapabilities() const
97 {
98     return m_layerRenderer->capabilities();
99 }
100
101 TextureAllocator* CCLayerTreeHostImpl::contentsTextureAllocator() const
102 {
103     return m_layerRenderer ? m_layerRenderer->contentsTextureAllocator() : 0;
104 }
105
106 void CCLayerTreeHostImpl::present()
107 {
108     ASSERT(m_layerRenderer && !isContextLost());
109     m_layerRenderer->present();
110 }
111
112 void CCLayerTreeHostImpl::readback(void* pixels, const IntRect& rect)
113 {
114     ASSERT(m_layerRenderer && !isContextLost());
115     m_layerRenderer->getFramebufferPixels(pixels, rect);
116 }
117
118 void CCLayerTreeHostImpl::setRootLayer(PassRefPtr<CCLayerImpl> layer)
119 {
120     m_rootLayerImpl = layer;
121 }
122
123 void CCLayerTreeHostImpl::setVisible(bool visible)
124 {
125     if (m_layerRenderer && !visible)
126         m_layerRenderer->releaseRenderSurfaceTextures();
127 }
128
129 bool CCLayerTreeHostImpl::initializeLayerRenderer(PassRefPtr<GraphicsContext3D> context)
130 {
131     OwnPtr<LayerRendererChromium> layerRenderer;
132     layerRenderer = LayerRendererChromium::create(this, context);
133
134     // If creation failed, and we had asked for accelerated painting, disable accelerated painting
135     // and try creating the renderer again.
136     if (!layerRenderer && m_settings.acceleratePainting) {
137         m_settings.acceleratePainting = false;
138
139         layerRenderer = LayerRendererChromium::create(this, context);
140     }
141
142     if (m_layerRenderer)
143         m_layerRenderer->close();
144
145     m_layerRenderer = layerRenderer.release();
146     return m_layerRenderer;
147 }
148
149 void CCLayerTreeHostImpl::setViewport(const IntSize& viewportSize)
150 {
151     bool changed = viewportSize != m_viewportSize;
152     m_viewportSize = viewportSize;
153     if (changed)
154         m_layerRenderer->viewportChanged();
155 }
156
157 void CCLayerTreeHostImpl::setZoomAnimatorTransform(const TransformationMatrix& zoom)
158 {
159     m_layerRenderer->setZoomAnimatorTransform(zoom);
160 }
161
162 void CCLayerTreeHostImpl::scrollRootLayer(const IntSize& scrollDelta)
163 {
164     TRACE_EVENT("CCLayerTreeHostImpl::scrollRootLayer", this, 0);
165     if (!m_rootLayerImpl || !m_rootLayerImpl->scrollable())
166         return;
167
168     m_rootLayerImpl->scrollBy(scrollDelta);
169 }
170
171 PassOwnPtr<CCScrollUpdateSet> CCLayerTreeHostImpl::processScrollDeltas()
172 {
173     OwnPtr<CCScrollUpdateSet> scrollInfo = adoptPtr(new CCScrollUpdateSet());
174     // FIXME: track scrolls from layers other than the root
175     if (rootLayer() && !rootLayer()->scrollDelta().isZero()) {
176         CCLayerTreeHostCommon::ScrollUpdateInfo info;
177         info.layerId = rootLayer()->id();
178         info.scrollDelta = rootLayer()->scrollDelta();
179         scrollInfo->append(info);
180
181         rootLayer()->setScrollPosition(rootLayer()->scrollPosition() + rootLayer()->scrollDelta());
182         rootLayer()->setScrollDelta(IntSize());
183     }
184     return scrollInfo.release();
185 }
186
187 }