9a84da6c053a34029e3dad37b76bb82435007f71
[WebKit-https.git] / Source / WebCore / platform / graphics / chromium / cc / CCThreadProxy.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/CCThreadProxy.h"
28
29 #include "GraphicsContext3D.h"
30 #include "TraceEvent.h"
31 #include "cc/CCLayerTreeHost.h"
32 #include "cc/CCMainThreadTask.h"
33 #include "cc/CCThreadTask.h"
34 #include <wtf/MainThread.h>
35
36 using namespace WTF;
37
38 namespace WebCore {
39
40 namespace {
41 CCThread* ccThread;
42 int numProxies = 0;
43 }
44
45 PassOwnPtr<CCProxy> CCThreadProxy::create(CCLayerTreeHost* layerTreeHost)
46 {
47     return adoptPtr(new CCThreadProxy(layerTreeHost));
48 }
49
50 CCThreadProxy::CCThreadProxy(CCLayerTreeHost* layerTreeHost)
51     : m_commitPending(false)
52     , m_layerTreeHost(layerTreeHost)
53 {
54     TRACE_EVENT("CCThreadProxy::CCThreadProxy", this, 0);
55     ASSERT(isMainThread());
56     numProxies++;
57     if (!ccThread) {
58         ccThread = m_layerTreeHost->createCompositorThread().leakPtr();
59 #ifndef NDEBUG
60         CCProxy::setImplThread(ccThread->threadID());
61 #endif
62     }
63 }
64
65 CCThreadProxy::~CCThreadProxy()
66 {
67     TRACE_EVENT("CCThreadProxy::~CCThreadProxy", this, 0);
68     ASSERT(isMainThread());
69     ASSERT(!m_layerTreeHostImpl); // Make sure stop() got called.
70     ASSERT(!m_layerTreeHost); // Make sure stop() got called.
71
72     numProxies--;
73     if (!numProxies) {
74         delete ccThread;
75         ccThread = 0;
76     }
77 }
78
79 bool CCThreadProxy::compositeAndReadback(void *pixels, const IntRect& rect)
80 {
81     ASSERT_NOT_REACHED();
82     return false;
83 }
84
85 GraphicsContext3D* CCThreadProxy::context()
86 {
87     return 0;
88 }
89
90 void CCThreadProxy::finishAllRendering()
91 {
92     ASSERT_NOT_REACHED();
93 }
94
95 bool CCThreadProxy::isStarted() const
96 {
97     return m_layerTreeHostImpl;
98 }
99
100 bool CCThreadProxy::initializeLayerRenderer()
101 {
102     RefPtr<GraphicsContext3D> context = m_layerTreeHost->createLayerTreeHostContext3D();
103     if (!context)
104         return false;
105     ASSERT(context->hasOneRef());
106
107     // Leak the context pointer so we can transfer ownership of it to the other side...
108     GraphicsContext3D* contextPtr = context.release().leakRef();
109     ASSERT(contextPtr->hasOneRef());
110
111     // Make a blocking call to initializeLayerRendererOnCCThread. The results of that call
112     // are pushed into the initializeSucceeded and capabilities local variables.
113     CCCompletionEvent completion;
114     bool initializeSucceeded;
115     LayerRendererCapabilities capabilities;
116     ccThread->postTask(createCCThreadTask(this, &CCThreadProxy::initializeLayerRendererOnCCThread,
117                                           AllowCrossThreadAccess(contextPtr), AllowCrossThreadAccess(&completion), AllowCrossThreadAccess(&initializeSucceeded), AllowCrossThreadAccess(&capabilities)));
118     completion.wait();
119
120     if (initializeSucceeded)
121         m_layerRendererCapabilitiesMainThreadCopy = capabilities;
122     return initializeSucceeded;
123 }
124
125 const LayerRendererCapabilities& CCThreadProxy::layerRendererCapabilities() const
126 {
127     return m_layerRendererCapabilitiesMainThreadCopy;
128 }
129
130 void CCThreadProxy::loseCompositorContext(int numTimes)
131 {
132     ASSERT_NOT_REACHED();
133 }
134
135 void CCThreadProxy::setNeedsCommit()
136 {
137     ASSERT(isMainThread());
138     if (m_commitPending)
139         return;
140
141     TRACE_EVENT("CCThreadProxy::setNeedsCommit", this, 0);
142     m_commitPending = true;
143     ccThread->postTask(createCCThreadTask(this, &CCThreadProxy::setNeedsCommitOnCCThread));
144 }
145
146 void CCThreadProxy::setNeedsCommitAndRedraw()
147 {
148     ASSERT(isMainThread());
149     if (m_commitPending)
150         return;
151
152     TRACE_EVENT("CCThreadProxy::setNeedsCommitAndRedraw", this, 0);
153     m_commitPending = true;
154     ccThread->postTask(createCCThreadTask(this, &CCThreadProxy::setNeedsCommitAndRedrawOnCCThread));
155 }
156
157 void CCThreadProxy::setNeedsRedraw()
158 {
159     ASSERT(isMainThread());
160     ccThread->postTask(createCCThreadTask(this, &CCThreadProxy::setNeedsRedrawOnCCThread));
161 }
162
163 void CCThreadProxy::start()
164 {
165     // Create LayerTreeHostImpl.
166     CCCompletionEvent completion;
167     ccThread->postTask(createCCThreadTask(this, &CCThreadProxy::initializeImplOnCCThread, AllowCrossThreadAccess(&completion)));
168     completion.wait();
169 }
170
171 void CCThreadProxy::stop()
172 {
173     TRACE_EVENT("CCThreadProxy::stop", this, 0);
174     ASSERT(isMainThread());
175     // Synchronously deletes the impl.
176     CCCompletionEvent completion;
177     ccThread->postTask(createCCThreadTask(this, &CCThreadProxy::layerTreeHostClosedOnCCThread, AllowCrossThreadAccess(&completion)));
178     completion.wait();
179
180     ASSERT(!m_layerTreeHostImpl); // verify that the impl deleted.
181     m_layerTreeHost = 0;
182 }
183
184 void CCThreadProxy::beginFrameAndCommitOnCCThread()
185 {
186     TRACE_EVENT("CCThreadProxy::beginFrameAndCommitOnCCThread", this, 0);
187     ASSERT(isImplThread());
188     // TEMP HACK so we can exercise this code in unit tests.
189     CCMainThread::postTask(createMainThreadTask(this, &CCThreadProxy::beginFrameAndCommit, 0.0));
190 }
191
192 void CCThreadProxy::beginFrameAndCommit(double frameBeginTime)
193 {
194     ASSERT(isMainThread());
195     if (!m_layerTreeHost)
196         return;
197
198     TRACE_EVENT("CCThreadProxy::requestFrameAndCommit", this, 0);
199     {
200         TRACE_EVENT("CCLayerTreeHost::animateAndLayout", this, 0);
201         m_layerTreeHost->animateAndLayout(frameBeginTime);
202     }
203
204     m_commitPending = false;
205
206     // Blocking call to CCThreadProxy::performCommit
207     CCCompletionEvent completion;
208     ccThread->postTask(createCCThreadTask(this, &CCThreadProxy::commitOnCCThread, AllowCrossThreadAccess(&completion)));
209     completion.wait();
210 }
211
212 void CCThreadProxy::commitOnCCThread(CCCompletionEvent* completion)
213 {
214     ASSERT(isImplThread());
215     TRACE_EVENT("CCThreadProxy::commitOnCCThread", this, 0);
216     m_layerTreeHostImpl->beginCommit();
217     {
218         TRACE_EVENT("CCLayerTreeHost::commit", this, 0);
219         m_layerTreeHost->commitTo(m_layerTreeHostImpl.get());
220     }
221     completion->signal();
222
223     m_layerTreeHostImpl->commitComplete();
224     setNeedsRedrawOnCCThread();
225 }
226
227 void CCThreadProxy::drawLayersOnCCThread()
228 {
229     TRACE_EVENT("CCThreadProxy::drawLayersOnCCThread", this, 0);
230     ASSERT(isImplThread());
231     if (m_layerTreeHostImpl)
232         m_layerTreeHostImpl->drawLayers();
233 }
234
235 void CCThreadProxy::setNeedsCommitOnCCThread()
236 {
237     TRACE_EVENT("CCThreadProxy::setNeedsCommitOnCCThread", this, 0);
238     ASSERT(isImplThread());
239     ASSERT(m_layerTreeHostImpl);
240     // FIXME: Not yet implemented, see https://bugs.webkit.org/show_bug.cgi?id=67417
241     ASSERT_NOT_REACHED();
242 }
243
244 void CCThreadProxy::setNeedsCommitAndRedrawOnCCThread()
245 {
246     TRACE_EVENT("CCThreadProxy::setNeedsCommitAndRedrawOnCCThread", this, 0);
247     ASSERT(isImplThread());
248     ASSERT(m_layerTreeHostImpl);
249     // TEMP HACK so we can exercise this code in unit tests.
250     CCMainThread::postTask(createMainThreadTask(this, &CCThreadProxy::beginFrameAndCommit, 0.0));
251 }
252
253 void CCThreadProxy::setNeedsRedrawOnCCThread()
254 {
255     TRACE_EVENT("CCThreadProxy::setNeedsRedrawOnCCThread", this, 0);
256     // TEMP HACK so we can exercise this code in unit tests.
257     drawLayersOnCCThread();
258 }
259
260 void CCThreadProxy::initializeImplOnCCThread(CCCompletionEvent* completion)
261 {
262     TRACE_EVENT("CCThreadProxy::initializeImplOnCCThread", this, 0);
263     ASSERT(isImplThread());
264     m_layerTreeHostImpl = m_layerTreeHost->createLayerTreeHostImpl();
265     completion->signal();
266 }
267
268 void CCThreadProxy::initializeLayerRendererOnCCThread(GraphicsContext3D* contextPtr, CCCompletionEvent* completion, bool* initializeSucceeded, LayerRendererCapabilities* capabilities)
269 {
270     TRACE_EVENT("CCThreadProxy::initializeLayerRendererOnCCThread", this, 0);
271     ASSERT(isImplThread());
272     RefPtr<GraphicsContext3D> context(adoptRef(contextPtr));
273     *initializeSucceeded = m_layerTreeHostImpl->initializeLayerRenderer(context);
274     if (*initializeSucceeded)
275         *capabilities = m_layerTreeHostImpl->layerRendererCapabilities();
276     completion->signal();
277 }
278
279 void CCThreadProxy::layerTreeHostClosedOnCCThread(CCCompletionEvent* completion)
280 {
281     TRACE_EVENT("CCThreadProxy::layerTreeHostClosedOnCCThread", this, 0);
282     ASSERT(isImplThread());
283     m_layerTreeHost->deleteContentsTextures(m_layerTreeHostImpl->context());
284     m_layerTreeHostImpl.clear();
285     completion->signal();
286 }
287
288 } // namespace WebCore