[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "config.h" |
| 6 | |
| 7 | #include "CCSingleThreadProxy.h" |
| 8 | |
| 9 | #include "CCDrawQuad.h" |
| 10 | #include "CCGraphicsContext.h" |
| 11 | #include "CCLayerTreeHost.h" |
| 12 | #include "CCTextureUpdateController.h" |
| 13 | #include "CCTimer.h" |
| 14 | #include "TraceEvent.h" |
| 15 | #include <wtf/CurrentTime.h> |
| 16 | |
| 17 | using namespace WTF; |
| 18 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 19 | namespace cc { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 20 | |
| 21 | PassOwnPtr<CCProxy> CCSingleThreadProxy::create(CCLayerTreeHost* layerTreeHost) |
| 22 | { |
| 23 | return adoptPtr(new CCSingleThreadProxy(layerTreeHost)); |
| 24 | } |
| 25 | |
| 26 | CCSingleThreadProxy::CCSingleThreadProxy(CCLayerTreeHost* layerTreeHost) |
| 27 | : m_layerTreeHost(layerTreeHost) |
| 28 | , m_contextLost(false) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 29 | , m_rendererInitialized(false) |
| 30 | , m_nextFrameIsNewlyCommittedFrame(false) |
| 31 | { |
| 32 | TRACE_EVENT0("cc", "CCSingleThreadProxy::CCSingleThreadProxy"); |
| 33 | ASSERT(CCProxy::isMainThread()); |
| 34 | } |
| 35 | |
| 36 | void CCSingleThreadProxy::start() |
| 37 | { |
| 38 | DebugScopedSetImplThread impl; |
| 39 | m_layerTreeHostImpl = m_layerTreeHost->createLayerTreeHostImpl(this); |
| 40 | } |
| 41 | |
| 42 | CCSingleThreadProxy::~CCSingleThreadProxy() |
| 43 | { |
| 44 | TRACE_EVENT0("cc", "CCSingleThreadProxy::~CCSingleThreadProxy"); |
| 45 | ASSERT(CCProxy::isMainThread()); |
| 46 | ASSERT(!m_layerTreeHostImpl && !m_layerTreeHost); // make sure stop() got called. |
| 47 | } |
| 48 | |
| 49 | bool CCSingleThreadProxy::compositeAndReadback(void *pixels, const IntRect& rect) |
| 50 | { |
| 51 | TRACE_EVENT0("cc", "CCSingleThreadProxy::compositeAndReadback"); |
| 52 | ASSERT(CCProxy::isMainThread()); |
| 53 | |
| 54 | if (!commitAndComposite()) |
| 55 | return false; |
| 56 | |
| 57 | m_layerTreeHostImpl->readback(pixels, rect); |
| 58 | |
| 59 | if (m_layerTreeHostImpl->isContextLost()) |
| 60 | return false; |
| 61 | |
| 62 | m_layerTreeHostImpl->swapBuffers(); |
| 63 | didSwapFrame(); |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | void CCSingleThreadProxy::startPageScaleAnimation(const IntSize& targetPosition, bool useAnchor, float scale, double duration) |
| 69 | { |
| 70 | m_layerTreeHostImpl->startPageScaleAnimation(targetPosition, useAnchor, scale, monotonicallyIncreasingTime(), duration); |
| 71 | } |
| 72 | |
| 73 | void CCSingleThreadProxy::finishAllRendering() |
| 74 | { |
| 75 | ASSERT(CCProxy::isMainThread()); |
| 76 | { |
| 77 | DebugScopedSetImplThread impl; |
| 78 | m_layerTreeHostImpl->finishAllRendering(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | bool CCSingleThreadProxy::isStarted() const |
| 83 | { |
| 84 | ASSERT(CCProxy::isMainThread()); |
| 85 | return m_layerTreeHostImpl; |
| 86 | } |
| 87 | |
| 88 | bool CCSingleThreadProxy::initializeContext() |
| 89 | { |
| 90 | ASSERT(CCProxy::isMainThread()); |
| 91 | OwnPtr<CCGraphicsContext> context = m_layerTreeHost->createContext(); |
| 92 | if (!context) |
| 93 | return false; |
| 94 | m_contextBeforeInitialization = context.release(); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | void CCSingleThreadProxy::setSurfaceReady() |
| 99 | { |
| 100 | // Scheduling is controlled by the embedder in the single thread case, so nothing to do. |
| 101 | } |
| 102 | |
| 103 | void CCSingleThreadProxy::setVisible(bool visible) |
| 104 | { |
| 105 | DebugScopedSetImplThread impl; |
| 106 | m_layerTreeHostImpl->setVisible(visible); |
| 107 | } |
| 108 | |
| 109 | bool CCSingleThreadProxy::initializeRenderer() |
| 110 | { |
| 111 | ASSERT(CCProxy::isMainThread()); |
| 112 | ASSERT(m_contextBeforeInitialization); |
| 113 | { |
| 114 | DebugScopedSetImplThread impl; |
| 115 | bool ok = m_layerTreeHostImpl->initializeRenderer(m_contextBeforeInitialization.release(), UnthrottledUploader); |
| 116 | if (ok) { |
| 117 | m_rendererInitialized = true; |
| 118 | m_RendererCapabilitiesForMainThread = m_layerTreeHostImpl->rendererCapabilities(); |
| 119 | } |
| 120 | |
| 121 | return ok; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | bool CCSingleThreadProxy::recreateContext() |
| 126 | { |
| 127 | TRACE_EVENT0("cc", "CCSingleThreadProxy::recreateContext"); |
| 128 | ASSERT(CCProxy::isMainThread()); |
| 129 | ASSERT(m_contextLost); |
| 130 | |
| 131 | OwnPtr<CCGraphicsContext> context = m_layerTreeHost->createContext(); |
| 132 | if (!context) |
| 133 | return false; |
| 134 | |
| 135 | bool initialized; |
| 136 | { |
[email protected] | d74ca3b | 2012-09-12 23:11:12 | [diff] [blame] | 137 | DebugScopedSetMainThreadBlocked mainThreadBlocked; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 138 | DebugScopedSetImplThread impl; |
| 139 | if (!m_layerTreeHostImpl->contentsTexturesPurged()) |
| 140 | m_layerTreeHost->deleteContentsTexturesOnImplThread(m_layerTreeHostImpl->resourceProvider()); |
| 141 | initialized = m_layerTreeHostImpl->initializeRenderer(context.release(), UnthrottledUploader); |
| 142 | if (initialized) { |
| 143 | m_RendererCapabilitiesForMainThread = m_layerTreeHostImpl->rendererCapabilities(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (initialized) |
| 148 | m_contextLost = false; |
| 149 | |
| 150 | return initialized; |
| 151 | } |
| 152 | |
| 153 | void CCSingleThreadProxy::implSideRenderingStats(CCRenderingStats& stats) |
| 154 | { |
| 155 | m_layerTreeHostImpl->renderingStats(stats); |
| 156 | } |
| 157 | |
| 158 | const RendererCapabilities& CCSingleThreadProxy::rendererCapabilities() const |
| 159 | { |
| 160 | ASSERT(m_rendererInitialized); |
| 161 | // Note: this gets called during the commit by the "impl" thread |
| 162 | return m_RendererCapabilitiesForMainThread; |
| 163 | } |
| 164 | |
| 165 | void CCSingleThreadProxy::loseContext() |
| 166 | { |
| 167 | ASSERT(CCProxy::isMainThread()); |
| 168 | m_layerTreeHost->didLoseContext(); |
| 169 | m_contextLost = true; |
| 170 | } |
| 171 | |
| 172 | void CCSingleThreadProxy::setNeedsAnimate() |
| 173 | { |
| 174 | // CCThread-only feature |
| 175 | ASSERT_NOT_REACHED(); |
| 176 | } |
| 177 | |
| 178 | void CCSingleThreadProxy::doCommit(CCTextureUpdateQueue& queue) |
| 179 | { |
| 180 | ASSERT(CCProxy::isMainThread()); |
| 181 | // Commit immediately |
| 182 | { |
| 183 | DebugScopedSetMainThreadBlocked mainThreadBlocked; |
| 184 | DebugScopedSetImplThread impl; |
| 185 | |
| 186 | m_layerTreeHostImpl->beginCommit(); |
| 187 | |
| 188 | m_layerTreeHost->beginCommitOnImplThread(m_layerTreeHostImpl.get()); |
| 189 | |
[email protected] | cc11ff3 | 2012-09-18 14:46:15 | [diff] [blame^] | 190 | CCTextureUpdateController::updateTextures(m_layerTreeHostImpl->resourceProvider(), m_layerTreeHostImpl->resourceProvider()->textureUploader(), &queue); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 191 | |
| 192 | m_layerTreeHost->finishCommitOnImplThread(m_layerTreeHostImpl.get()); |
| 193 | |
| 194 | m_layerTreeHostImpl->commitComplete(); |
| 195 | |
| 196 | #if !ASSERT_DISABLED |
| 197 | // In the single-threaded case, the scroll deltas should never be |
| 198 | // touched on the impl layer tree. |
| 199 | OwnPtr<CCScrollAndScaleSet> scrollInfo = m_layerTreeHostImpl->processScrollDeltas(); |
| 200 | ASSERT(!scrollInfo->scrolls.size()); |
| 201 | #endif |
| 202 | } |
| 203 | m_layerTreeHost->commitComplete(); |
| 204 | m_nextFrameIsNewlyCommittedFrame = true; |
| 205 | } |
| 206 | |
| 207 | void CCSingleThreadProxy::setNeedsCommit() |
| 208 | { |
| 209 | ASSERT(CCProxy::isMainThread()); |
| 210 | m_layerTreeHost->scheduleComposite(); |
| 211 | } |
| 212 | |
| 213 | void CCSingleThreadProxy::setNeedsRedraw() |
| 214 | { |
| 215 | // FIXME: Once we move render_widget scheduling into this class, we can |
| 216 | // treat redraw requests more efficiently than commitAndRedraw requests. |
| 217 | m_layerTreeHostImpl->setFullRootLayerDamage(); |
| 218 | setNeedsCommit(); |
| 219 | } |
| 220 | |
| 221 | bool CCSingleThreadProxy::commitRequested() const |
| 222 | { |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | void CCSingleThreadProxy::didAddAnimation() |
| 227 | { |
| 228 | } |
| 229 | |
| 230 | void CCSingleThreadProxy::stop() |
| 231 | { |
| 232 | TRACE_EVENT0("cc", "CCSingleThreadProxy::stop"); |
| 233 | ASSERT(CCProxy::isMainThread()); |
| 234 | { |
| 235 | DebugScopedSetMainThreadBlocked mainThreadBlocked; |
| 236 | DebugScopedSetImplThread impl; |
| 237 | |
| 238 | if (!m_layerTreeHostImpl->contentsTexturesPurged()) |
| 239 | m_layerTreeHost->deleteContentsTexturesOnImplThread(m_layerTreeHostImpl->resourceProvider()); |
| 240 | m_layerTreeHostImpl.clear(); |
| 241 | } |
| 242 | m_layerTreeHost = 0; |
| 243 | } |
| 244 | |
| 245 | void CCSingleThreadProxy::postAnimationEventsToMainThreadOnImplThread(PassOwnPtr<CCAnimationEventsVector> events, double wallClockTime) |
| 246 | { |
| 247 | ASSERT(CCProxy::isImplThread()); |
| 248 | DebugScopedSetMainThread main; |
| 249 | m_layerTreeHost->setAnimationEvents(events, wallClockTime); |
| 250 | } |
| 251 | |
| 252 | // Called by the legacy scheduling path (e.g. where render_widget does the scheduling) |
| 253 | void CCSingleThreadProxy::compositeImmediately() |
| 254 | { |
| 255 | if (commitAndComposite()) { |
| 256 | m_layerTreeHostImpl->swapBuffers(); |
| 257 | didSwapFrame(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void CCSingleThreadProxy::forceSerializeOnSwapBuffers() |
| 262 | { |
| 263 | { |
| 264 | DebugScopedSetImplThread impl; |
| 265 | if (m_rendererInitialized) |
| 266 | m_layerTreeHostImpl->renderer()->doNoOp(); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | bool CCSingleThreadProxy::commitAndComposite() |
| 271 | { |
| 272 | ASSERT(CCProxy::isMainThread()); |
| 273 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 274 | if (!m_layerTreeHost->initializeRendererIfNeeded()) |
| 275 | return false; |
| 276 | |
[email protected] | d74ca3b | 2012-09-12 23:11:12 | [diff] [blame] | 277 | if (m_layerTreeHostImpl->contentsTexturesPurged()) { |
| 278 | m_layerTreeHost->unlinkAllContentTextures(); |
| 279 | DebugScopedSetImplThreadAndMainThreadBlocked implAndMainBlocked; |
| 280 | m_layerTreeHost->deleteUnlinkedTextures(); |
| 281 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 282 | |
| 283 | CCTextureUpdateQueue queue; |
| 284 | m_layerTreeHost->updateLayers(queue, m_layerTreeHostImpl->memoryAllocationLimitBytes()); |
| 285 | m_layerTreeHostImpl->resetContentsTexturesPurged(); |
| 286 | |
| 287 | m_layerTreeHost->willCommit(); |
| 288 | doCommit(queue); |
| 289 | bool result = doComposite(); |
| 290 | m_layerTreeHost->didBeginFrame(); |
| 291 | return result; |
| 292 | } |
| 293 | |
| 294 | bool CCSingleThreadProxy::doComposite() |
| 295 | { |
| 296 | ASSERT(!m_contextLost); |
| 297 | { |
| 298 | DebugScopedSetImplThread impl; |
| 299 | |
| 300 | if (!m_layerTreeHostImpl->visible()) |
| 301 | return false; |
| 302 | |
| 303 | double monotonicTime = monotonicallyIncreasingTime(); |
| 304 | double wallClockTime = currentTime(); |
| 305 | m_layerTreeHostImpl->animate(monotonicTime, wallClockTime); |
| 306 | |
| 307 | // We guard prepareToDraw() with canDraw() because it always returns a valid frame, so can only |
| 308 | // be used when such a frame is possible. Since drawLayers() depends on the result of |
| 309 | // prepareToDraw(), it is guarded on canDraw() as well. |
| 310 | if (!m_layerTreeHostImpl->canDraw()) |
| 311 | return false; |
| 312 | |
| 313 | CCLayerTreeHostImpl::FrameData frame; |
| 314 | m_layerTreeHostImpl->prepareToDraw(frame); |
| 315 | m_layerTreeHostImpl->drawLayers(frame); |
| 316 | m_layerTreeHostImpl->didDrawAllLayers(frame); |
| 317 | } |
| 318 | |
| 319 | if (m_layerTreeHostImpl->isContextLost()) { |
| 320 | m_contextLost = true; |
| 321 | m_layerTreeHost->didLoseContext(); |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | void CCSingleThreadProxy::didSwapFrame() |
| 329 | { |
| 330 | if (m_nextFrameIsNewlyCommittedFrame) { |
| 331 | m_nextFrameIsNewlyCommittedFrame = false; |
| 332 | m_layerTreeHost->didCommitAndDrawFrame(); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | } |