2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 * Copyright (C) 2012 Company 100, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 * THE POSSIBILITY OF SUCH DAMAGE.
30 #if USE(COORDINATED_GRAPHICS)
31 #include "CoordinatedLayerTreeHost.h"
33 #include "CoordinatedGraphicsArgumentCoders.h"
34 #include "CoordinatedLayerTreeHostProxyMessages.h"
35 #include "DrawingAreaImpl.h"
36 #include "GraphicsContext.h"
37 #include "WebCoordinatedSurface.h"
38 #include "WebCoreArgumentCoders.h"
40 #include "WebPageProxyMessages.h"
41 #include <WebCore/Frame.h>
42 #include <WebCore/FrameView.h>
43 #include <WebCore/GraphicsSurface.h>
44 #include <WebCore/Page.h>
45 #include <WebCore/RenderLayer.h>
46 #include <WebCore/RenderLayerBacking.h>
47 #include <WebCore/RenderLayerCompositor.h>
48 #include <WebCore/RenderView.h>
49 #include <WebCore/Settings.h>
50 #include <WebCore/SurfaceUpdateInfo.h>
51 #include <WebCore/TextureMapperPlatformLayer.h>
52 #include <wtf/CurrentTime.h>
53 #include <wtf/TemporaryChange.h>
55 #if ENABLE(CSS_SHADERS)
56 #include "CustomFilterValidatedProgram.h"
57 #include "ValidatedCustomFilterOperation.h"
60 using namespace WebCore;
64 PassRefPtr<CoordinatedLayerTreeHost> CoordinatedLayerTreeHost::create(WebPage* webPage)
66 return adoptRef(new CoordinatedLayerTreeHost(webPage));
69 CoordinatedLayerTreeHost::~CoordinatedLayerTreeHost()
71 #if ENABLE(CSS_SHADERS)
72 disconnectCustomFilterPrograms();
76 LayerMap::iterator end = m_registeredLayers.end();
77 for (LayerMap::iterator it = m_registeredLayers.begin(); it != end; ++it)
78 it->value->setCoordinator(0);
81 CoordinatedLayerTreeHost::CoordinatedLayerTreeHost(WebPage* webPage)
82 : LayerTreeHost(webPage)
83 , m_notifyAfterScheduledLayerFlush(false)
86 , m_isFlushingLayerChanges(false)
87 , m_waitingForUIProcess(true)
88 , m_isSuspended(false)
89 , m_shouldSyncFrame(false)
90 , m_didInitializeRootCompositingLayer(false)
91 , m_layerFlushTimer(this, &CoordinatedLayerTreeHost::layerFlushTimerFired)
92 , m_releaseInactiveAtlasesTimer(this, &CoordinatedLayerTreeHost::releaseInactiveAtlasesTimerFired)
93 , m_layerFlushSchedulingEnabled(true)
94 , m_forceRepaintAsyncCallbackID(0)
95 , m_animationsLocked(false)
97 m_webPage->corePage()->settings()->setScrollingCoordinatorEnabled(true);
98 m_webPage->corePage()->settings()->setApplyDeviceScaleFactorInCompositor(true);
100 // Create a root layer.
101 m_rootLayer = GraphicsLayer::create(this, this);
102 CoordinatedGraphicsLayer* coordinatedRootLayer = toCoordinatedGraphicsLayer(m_rootLayer.get());
104 m_rootLayer->setName("CoordinatedLayerTreeHost root layer");
106 m_rootLayer->setDrawsContent(false);
107 m_rootLayer->setSize(m_webPage->size());
108 m_layerTreeContext.coordinatedLayerID = toCoordinatedGraphicsLayer(coordinatedRootLayer)->id();
110 m_nonCompositedContentLayer = GraphicsLayer::create(this, this);
112 m_nonCompositedContentLayer->setName("CoordinatedLayerTreeHost non-composited content");
114 m_nonCompositedContentLayer->setDrawsContent(true);
115 m_nonCompositedContentLayer->setSize(m_webPage->size());
117 m_nonCompositedContentLayer->setShowDebugBorder(m_webPage->corePage()->settings()->showDebugBorders());
118 m_nonCompositedContentLayer->setShowRepaintCounter(m_webPage->corePage()->settings()->showRepaintCounter());
120 m_rootLayer->addChild(m_nonCompositedContentLayer.get());
122 CoordinatedSurface::setFactory(createCoordinatedSurface);
124 // This is a temporary way to enable this only in the GL case, until TextureMapperImageBuffer is removed.
125 // See https://bugs.webkit.org/show_bug.cgi?id=114869
126 CoordinatedGraphicsLayer::setShouldSupportContentsTiling(true);
128 if (m_webPage->hasPageOverlay())
129 createPageOverlayLayer();
131 scheduleLayerFlush();
134 void CoordinatedLayerTreeHost::setLayerFlushSchedulingEnabled(bool layerFlushingEnabled)
136 if (m_layerFlushSchedulingEnabled == layerFlushingEnabled)
139 m_layerFlushSchedulingEnabled = layerFlushingEnabled;
141 if (m_layerFlushSchedulingEnabled) {
142 scheduleLayerFlush();
146 cancelPendingLayerFlush();
149 void CoordinatedLayerTreeHost::scheduleLayerFlush()
151 if (!m_layerFlushSchedulingEnabled)
154 if (!m_layerFlushTimer.isActive())
155 m_layerFlushTimer.startOneShot(0);
158 void CoordinatedLayerTreeHost::cancelPendingLayerFlush()
160 m_layerFlushTimer.stop();
163 void CoordinatedLayerTreeHost::setShouldNotifyAfterNextScheduledLayerFlush(bool notifyAfterScheduledLayerFlush)
165 m_notifyAfterScheduledLayerFlush = notifyAfterScheduledLayerFlush;
168 void CoordinatedLayerTreeHost::setRootCompositingLayer(WebCore::GraphicsLayer* graphicsLayer)
170 m_nonCompositedContentLayer->removeAllChildren();
171 m_nonCompositedContentLayer->setContentsOpaque(m_webPage->drawsBackground() && !m_webPage->drawsTransparentBackground());
173 // Add the accelerated layer tree hierarchy.
175 m_nonCompositedContentLayer->addChild(graphicsLayer);
178 void CoordinatedLayerTreeHost::invalidate()
180 cancelPendingLayerFlush();
183 m_rootLayer = nullptr;
187 void CoordinatedLayerTreeHost::setNonCompositedContentsNeedDisplay()
189 m_nonCompositedContentLayer->setNeedsDisplay();
190 if (m_pageOverlayLayer)
191 m_pageOverlayLayer->setNeedsDisplay();
193 scheduleLayerFlush();
196 void CoordinatedLayerTreeHost::setNonCompositedContentsNeedDisplayInRect(const WebCore::IntRect& rect)
198 m_nonCompositedContentLayer->setNeedsDisplayInRect(rect);
199 if (m_pageOverlayLayer)
200 m_pageOverlayLayer->setNeedsDisplayInRect(rect);
202 scheduleLayerFlush();
205 void CoordinatedLayerTreeHost::scrollNonCompositedContents(const WebCore::IntRect&)
207 if (!m_webPage->useFixedLayout())
208 setNonCompositedContentsNeedDisplay();
210 scheduleLayerFlush();
213 void CoordinatedLayerTreeHost::forceRepaint()
215 // This is necessary for running layout tests. Since in this case we are not waiting for a UIProcess to reply nicely.
216 // Instead we are just triggering forceRepaint. But we still want to have the scripted animation callbacks being executed.
219 // We need to schedule another flush, otherwise the forced paint might cancel a later expected flush.
220 // This is aligned with LayerTreeHostCA.
221 scheduleLayerFlush();
222 flushPendingLayerChanges();
225 bool CoordinatedLayerTreeHost::forceRepaintAsync(uint64_t callbackID)
227 // We expect the UI process to not require a new repaint until the previous one has finished.
228 ASSERT(!m_forceRepaintAsyncCallbackID);
229 m_forceRepaintAsyncCallbackID = callbackID;
230 scheduleLayerFlush();
234 void CoordinatedLayerTreeHost::sizeDidChange(const WebCore::IntSize& newSize)
236 if (m_rootLayer->size() == newSize)
239 m_rootLayer->setSize(newSize);
241 // If the newSize exposes new areas of the non-composited content a setNeedsDisplay is needed
242 // for those newly exposed areas.
243 FloatSize oldSize = m_nonCompositedContentLayer->size();
244 m_nonCompositedContentLayer->setSize(newSize);
246 if (newSize.width() > oldSize.width()) {
247 float height = std::min(static_cast<float>(newSize.height()), oldSize.height());
248 m_nonCompositedContentLayer->setNeedsDisplayInRect(FloatRect(oldSize.width(), 0, newSize.width() - oldSize.width(), height));
251 if (newSize.height() > oldSize.height())
252 m_nonCompositedContentLayer->setNeedsDisplayInRect(FloatRect(0, oldSize.height(), newSize.width(), newSize.height() - oldSize.height()));
254 if (m_pageOverlayLayer)
255 m_pageOverlayLayer->setSize(newSize);
257 scheduleLayerFlush();
260 void CoordinatedLayerTreeHost::didInstallPageOverlay(PageOverlay* pageOverlay)
262 ASSERT(!m_pageOverlay);
263 m_pageOverlay = pageOverlay;
265 createPageOverlayLayer();
266 scheduleLayerFlush();
269 void CoordinatedLayerTreeHost::didUninstallPageOverlay(PageOverlay*)
273 destroyPageOverlayLayer();
274 scheduleLayerFlush();
277 void CoordinatedLayerTreeHost::setPageOverlayNeedsDisplay(PageOverlay*, const WebCore::IntRect& rect)
279 ASSERT(m_pageOverlayLayer);
280 m_pageOverlayLayer->setNeedsDisplayInRect(rect);
281 scheduleLayerFlush();
284 void CoordinatedLayerTreeHost::setPageOverlayOpacity(PageOverlay*, float value)
286 ASSERT(m_pageOverlayLayer);
287 m_pageOverlayLayer->setOpacity(value);
288 scheduleLayerFlush();
291 bool CoordinatedLayerTreeHost::flushPendingLayerChanges()
293 if (m_waitingForUIProcess)
296 TemporaryChange<bool> protector(m_isFlushingLayerChanges, true);
298 createCompositingLayers();
300 initializeRootCompositingLayerIfNeeded();
302 m_rootLayer->flushCompositingStateForThisLayerOnly();
303 m_nonCompositedContentLayer->flushCompositingStateForThisLayerOnly();
304 if (m_pageOverlayLayer)
305 m_pageOverlayLayer->flushCompositingStateForThisLayerOnly();
307 bool didSync = m_webPage->corePage()->mainFrame()->view()->flushCompositingStateIncludingSubframes();
309 flushPendingImageBackingChanges();
311 deleteCompositingLayers();
313 if (m_shouldSyncFrame) {
316 m_state.contentsSize = roundedIntSize(m_nonCompositedContentLayer->size());
317 m_state.coveredRect = toCoordinatedGraphicsLayer(m_nonCompositedContentLayer.get())->coverRect();
318 m_state.scrollPosition = m_visibleContentsRect.location();
320 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::CommitCoordinatedGraphicsState(m_state));
322 m_state.layersToUpdate.clear();
323 m_state.imagesToUpdate.clear();
325 m_waitingForUIProcess = true;
326 m_shouldSyncFrame = false;
330 if (m_forceRepaintAsyncCallbackID) {
331 m_webPage->send(Messages::WebPageProxy::VoidCallback(m_forceRepaintAsyncCallbackID));
332 m_forceRepaintAsyncCallbackID = 0;
338 void CoordinatedLayerTreeHost::createCompositingLayers()
340 if (m_layersToCreate.isEmpty())
343 // If a layer gets created and deleted in the same cycle, we can simply remove it from m_layersToCreate and m_layersToDelete.
344 for (int i = m_layersToCreate.size() - 1; i >= 0; --i) {
345 size_t index = m_layersToDelete.find(m_layersToCreate[i]);
346 if (index != notFound) {
347 m_layersToCreate.remove(i);
348 m_layersToDelete.remove(index);
352 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::CreateCompositingLayers(m_layersToCreate));
353 m_layersToCreate.clear();
354 m_shouldSyncFrame = true;
357 void CoordinatedLayerTreeHost::deleteCompositingLayers()
359 if (m_layersToDelete.isEmpty())
363 m_layersToDelete.clear();
367 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::DeleteCompositingLayers(m_layersToDelete));
368 m_layersToDelete.clear();
369 m_shouldSyncFrame = true;
372 void CoordinatedLayerTreeHost::initializeRootCompositingLayerIfNeeded()
374 if (m_didInitializeRootCompositingLayer)
377 m_state.rootCompositingLayer = toCoordinatedGraphicsLayer(m_rootLayer.get())->id();
378 m_didInitializeRootCompositingLayer = true;
379 m_shouldSyncFrame = true;
382 void CoordinatedLayerTreeHost::syncLayerState(CoordinatedLayerID id, CoordinatedGraphicsLayerState& state)
384 m_shouldSyncFrame = true;
386 #if ENABLE(CSS_SHADERS)
387 prepareCustomFilterProxiesIfNeeded(state);
390 m_state.layersToUpdate.append(std::make_pair(id, state));
393 #if ENABLE(CSS_SHADERS)
394 void CoordinatedLayerTreeHost::prepareCustomFilterProxiesIfNeeded(CoordinatedGraphicsLayerState& state)
396 if (state.animationsChanged) {
397 GraphicsLayerAnimations& activeAnimations = state.animations;
398 for (size_t i = 0; i < activeAnimations.animations().size(); ++i) {
399 const KeyframeValueList& keyframes = activeAnimations.animations().at(i).keyframes();
400 if (keyframes.property() != AnimatedPropertyWebkitFilter)
402 for (size_t j = 0; j < keyframes.size(); ++j) {
403 const FilterAnimationValue* filterValue = static_cast<const FilterAnimationValue*>(keyframes.at(i));
404 checkCustomFilterProgramProxies(*filterValue->value());
409 if (state.filtersChanged)
410 checkCustomFilterProgramProxies(state.filters);
413 void CoordinatedLayerTreeHost::checkCustomFilterProgramProxies(const FilterOperations& filters)
415 // We need to create the WebCustomFilterProgramProxy objects before we get to serialize the
416 // custom filters to the other process. That's because WebCustomFilterProgramProxy needs
417 // to link back to the coordinator, so that we can send a message to the UI process when
418 // the program is not needed anymore.
419 // Note that the serialization will only happen at a later time in ArgumentCoder<WebCore::FilterOperations>::encode.
420 // At that point the program will only be serialized once. All the other times it will only use the ID of the program.
421 for (size_t i = 0; i < filters.size(); ++i) {
422 const FilterOperation* operation = filters.at(i);
423 if (operation->getOperationType() != FilterOperation::VALIDATED_CUSTOM)
425 const ValidatedCustomFilterOperation* customOperation = static_cast<const ValidatedCustomFilterOperation*>(operation);
426 ASSERT(customOperation->validatedProgram()->isInitialized());
427 TextureMapperPlatformCompiledProgram* program = customOperation->validatedProgram()->platformCompiledProgram();
429 RefPtr<WebCustomFilterProgramProxy> customFilterProgramProxy;
430 if (program->client())
431 customFilterProgramProxy = static_cast<WebCustomFilterProgramProxy*>(program->client());
433 customFilterProgramProxy = WebCustomFilterProgramProxy::create();
434 program->setClient(customFilterProgramProxy);
437 if (!customFilterProgramProxy->client()) {
438 customFilterProgramProxy->setClient(this);
439 m_customFilterPrograms.add(customFilterProgramProxy.get());
440 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::CreateCustomFilterProgram(customFilterProgramProxy->id(), customOperation->validatedProgram()->validatedProgramInfo()));
442 // If the client was not disconnected then this coordinator must be the client for it.
443 ASSERT(customFilterProgramProxy->client() == this);
448 void CoordinatedLayerTreeHost::removeCustomFilterProgramProxy(WebCustomFilterProgramProxy* customFilterProgramProxy)
450 // At this time the shader is not needed anymore, so we remove it from our set and
451 // send a message to the other process to delete it.
452 m_customFilterPrograms.remove(customFilterProgramProxy);
453 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::RemoveCustomFilterProgram(customFilterProgramProxy->id()));
456 void CoordinatedLayerTreeHost::disconnectCustomFilterPrograms()
458 // Make sure that WebCore will not call into this coordinator anymore.
459 HashSet<WebCustomFilterProgramProxy*>::iterator iter = m_customFilterPrograms.begin();
460 for (; iter != m_customFilterPrograms.end(); ++iter)
461 (*iter)->setClient(0);
463 #endif // ENABLE(CSS_SHADERS)
465 void CoordinatedLayerTreeHost::detachLayer(CoordinatedGraphicsLayer* layer)
467 m_registeredLayers.remove(layer->id());
468 m_layersToDelete.append(layer->id());
469 scheduleLayerFlush();
472 void CoordinatedLayerTreeHost::lockAnimations()
474 m_animationsLocked = true;
475 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::SetAnimationsLocked(true));
478 void CoordinatedLayerTreeHost::unlockAnimations()
480 if (!m_animationsLocked)
483 m_animationsLocked = false;
484 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::SetAnimationsLocked(false));
487 void CoordinatedLayerTreeHost::performScheduledLayerFlush()
489 if (m_isSuspended || m_waitingForUIProcess)
492 // We lock the animations while performing layout, to avoid flickers caused by animations continuing in the UI process while
493 // the web process layout wants to cancel them.
497 // We can unlock the animations before flushing if there are no visible changes, for example if there are content updates
498 // in a layer with opacity 0.
499 bool canUnlockBeforeFlush = !m_isValid || !toCoordinatedGraphicsLayer(m_rootLayer.get())->hasPendingVisibleChanges();
500 if (canUnlockBeforeFlush)
506 if (flushPendingLayerChanges())
507 didPerformScheduledLayerFlush();
510 void CoordinatedLayerTreeHost::syncDisplayState()
512 #if ENABLE(REQUEST_ANIMATION_FRAME) && !USE(REQUEST_ANIMATION_FRAME_TIMER) && !USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
513 // Make sure that any previously registered animation callbacks are being executed before we flush the layers.
514 m_webPage->corePage()->mainFrame()->view()->serviceScriptedAnimations(currentTime());
517 m_webPage->layoutIfNeeded();
520 void CoordinatedLayerTreeHost::didPerformScheduledLayerFlush()
522 if (m_notifyAfterScheduledLayerFlush) {
523 static_cast<DrawingAreaImpl*>(m_webPage->drawingArea())->layerHostDidFlushLayers();
524 m_notifyAfterScheduledLayerFlush = false;
528 void CoordinatedLayerTreeHost::layerFlushTimerFired(Timer<CoordinatedLayerTreeHost>*)
530 performScheduledLayerFlush();
533 void CoordinatedLayerTreeHost::createPageOverlayLayer()
535 ASSERT(!m_pageOverlayLayer);
537 m_pageOverlayLayer = GraphicsLayer::create(this, this);
539 m_pageOverlayLayer->setName("CoordinatedLayerTreeHost page overlay content");
542 m_pageOverlayLayer->setDrawsContent(true);
543 m_pageOverlayLayer->setSize(m_webPage->size());
545 m_rootLayer->addChild(m_pageOverlayLayer.get());
548 void CoordinatedLayerTreeHost::destroyPageOverlayLayer()
550 ASSERT(m_pageOverlayLayer);
551 m_pageOverlayLayer->removeFromParent();
552 m_pageOverlayLayer = nullptr;
555 PassRefPtr<CoordinatedImageBacking> CoordinatedLayerTreeHost::createImageBackingIfNeeded(Image* image)
557 CoordinatedImageBackingID imageID = CoordinatedImageBacking::getCoordinatedImageBackingID(image);
558 ImageBackingMap::iterator it = m_imageBackings.find(imageID);
559 RefPtr<CoordinatedImageBacking> imageBacking;
560 if (it == m_imageBackings.end()) {
561 imageBacking = CoordinatedImageBacking::create(this, image);
562 m_imageBackings.add(imageID, imageBacking);
564 imageBacking = it->value;
569 void CoordinatedLayerTreeHost::createImageBacking(CoordinatedImageBackingID imageID)
571 m_shouldSyncFrame = true;
572 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::CreateImageBacking(imageID));
575 bool CoordinatedLayerTreeHost::updateImageBacking(CoordinatedImageBackingID imageID, PassRefPtr<CoordinatedSurface> coordinatedSurface)
577 m_shouldSyncFrame = true;
578 WebCoordinatedSurface* webCoordinatedSurface = static_cast<WebCoordinatedSurface*>(coordinatedSurface.get());
579 WebCoordinatedSurface::Handle handle;
580 if (!webCoordinatedSurface->createHandle(handle))
583 m_state.imagesToUpdate.append(std::make_pair(imageID, coordinatedSurface));
587 void CoordinatedLayerTreeHost::clearImageBackingContents(CoordinatedImageBackingID imageID)
589 m_shouldSyncFrame = true;
590 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::ClearImageBackingContents(imageID));
593 void CoordinatedLayerTreeHost::removeImageBacking(CoordinatedImageBackingID imageID)
598 ASSERT(m_imageBackings.contains(imageID));
599 m_shouldSyncFrame = true;
600 m_imageBackings.remove(imageID);
601 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::RemoveImageBacking(imageID));
604 void CoordinatedLayerTreeHost::flushPendingImageBackingChanges()
606 ImageBackingMap::iterator end = m_imageBackings.end();
607 for (ImageBackingMap::iterator iter = m_imageBackings.begin(); iter != end; ++iter)
608 iter->value->update();
611 void CoordinatedLayerTreeHost::notifyAnimationStarted(const WebCore::GraphicsLayer*, double /* time */)
615 void CoordinatedLayerTreeHost::notifyFlushRequired(const WebCore::GraphicsLayer*)
617 scheduleLayerFlush();
620 void CoordinatedLayerTreeHost::paintContents(const WebCore::GraphicsLayer* graphicsLayer, WebCore::GraphicsContext& graphicsContext, WebCore::GraphicsLayerPaintingPhase, const WebCore::IntRect& clipRect)
622 if (graphicsLayer == m_nonCompositedContentLayer) {
623 m_webPage->drawRect(graphicsContext, clipRect);
627 if (graphicsLayer == m_pageOverlayLayer) {
628 // Overlays contain transparent contents and won't clear the context as part of their rendering, so we do it here.
629 graphicsContext.clearRect(clipRect);
630 m_webPage->drawPageOverlay(m_pageOverlay.get(), graphicsContext, clipRect);
635 PassOwnPtr<GraphicsLayer> CoordinatedLayerTreeHost::createGraphicsLayer(GraphicsLayerClient* client)
637 CoordinatedGraphicsLayer* layer = new CoordinatedGraphicsLayer(client);
638 layer->setCoordinator(this);
639 m_registeredLayers.add(layer->id(), layer);
640 m_layersToCreate.append(layer->id());
641 layer->setNeedsVisibleRectAdjustment();
642 scheduleLayerFlush();
643 return adoptPtr(layer);
646 PassRefPtr<CoordinatedSurface> CoordinatedLayerTreeHost::createCoordinatedSurface(const IntSize& size, CoordinatedSurface::Flags flags)
648 return WebCoordinatedSurface::create(size, flags);
651 float CoordinatedLayerTreeHost::deviceScaleFactor() const
653 return m_webPage->deviceScaleFactor();
656 float CoordinatedLayerTreeHost::pageScaleFactor() const
658 return m_webPage->pageScaleFactor();
661 bool LayerTreeHost::supportsAcceleratedCompositing()
666 bool CoordinatedLayerTreeHost::createUpdateAtlas(uint32_t atlasID, PassRefPtr<CoordinatedSurface> coordinatedSurface)
668 WebCoordinatedSurface* webCoordinatedSurface = static_cast<WebCoordinatedSurface*>(coordinatedSurface.get());
669 WebCoordinatedSurface::Handle handle;
670 if (!webCoordinatedSurface->createHandle(handle))
672 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::CreateUpdateAtlas(atlasID, handle));
676 void CoordinatedLayerTreeHost::removeUpdateAtlas(uint32_t atlasID)
680 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::RemoveUpdateAtlas(atlasID));
683 WebCore::FloatRect CoordinatedLayerTreeHost::visibleContentsRect() const
685 return m_visibleContentsRect;
688 void CoordinatedLayerTreeHost::setVisibleContentsRect(const FloatRect& rect, const FloatPoint& trajectoryVector)
690 // A zero trajectoryVector indicates that tiles all around the viewport are requested.
691 toCoordinatedGraphicsLayer(m_nonCompositedContentLayer.get())->setVisibleContentRectTrajectoryVector(trajectoryVector);
693 bool contentsRectDidChange = rect != m_visibleContentsRect;
694 if (contentsRectDidChange) {
695 m_visibleContentsRect = rect;
697 LayerMap::iterator end = m_registeredLayers.end();
698 for (LayerMap::iterator it = m_registeredLayers.begin(); it != end; ++it) {
699 it->value->setNeedsVisibleRectAdjustment();
703 scheduleLayerFlush();
704 if (m_webPage->useFixedLayout()) {
705 // Round the rect instead of enclosing it to make sure that its size stays
706 // the same while panning. This can have nasty effects on layout.
707 m_webPage->setFixedVisibleContentRect(roundedIntRect(rect));
711 void CoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged()
713 m_rootLayer->deviceOrPageScaleFactorChanged();
714 m_nonCompositedContentLayer->deviceOrPageScaleFactorChanged();
715 if (m_pageOverlayLayer)
716 m_pageOverlayLayer->deviceOrPageScaleFactorChanged();
719 GraphicsLayerFactory* CoordinatedLayerTreeHost::graphicsLayerFactory()
724 #if ENABLE(REQUEST_ANIMATION_FRAME)
725 void CoordinatedLayerTreeHost::scheduleAnimation()
727 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::RequestAnimationFrame());
730 void CoordinatedLayerTreeHost::animationFrameReady()
732 scheduleLayerFlush();
736 void CoordinatedLayerTreeHost::renderNextFrame()
738 m_waitingForUIProcess = false;
739 scheduleLayerFlush();
740 for (unsigned i = 0; i < m_updateAtlases.size(); ++i)
741 m_updateAtlases[i]->didSwapBuffers();
744 void CoordinatedLayerTreeHost::purgeBackingStores()
746 TemporaryChange<bool> purgingToggle(m_isPurging, true);
748 LayerMap::iterator end = m_registeredLayers.end();
749 for (LayerMap::iterator it = m_registeredLayers.begin(); it != end; ++it)
750 it->value->purgeBackingStores();
752 m_imageBackings.clear();
753 m_updateAtlases.clear();
756 PassOwnPtr<GraphicsContext> CoordinatedLayerTreeHost::beginContentUpdate(const IntSize& size, CoordinatedSurface::Flags flags, uint32_t& atlasID, IntPoint& offset)
758 OwnPtr<GraphicsContext> graphicsContext;
759 for (unsigned i = 0; i < m_updateAtlases.size(); ++i) {
760 UpdateAtlas* atlas = m_updateAtlases[i].get();
761 if (atlas->supportsAlpha() == (flags & CoordinatedSurface::SupportsAlpha)) {
762 // This will return null if there is no available buffer space.
763 graphicsContext = atlas->beginPaintingOnAvailableBuffer(atlasID, size, offset);
765 return graphicsContext.release();
769 static const int ScratchBufferDimension = 1024; // Should be a power of two.
770 m_updateAtlases.append(adoptPtr(new UpdateAtlas(this, ScratchBufferDimension, flags)));
771 scheduleReleaseInactiveAtlases();
772 return m_updateAtlases.last()->beginPaintingOnAvailableBuffer(atlasID, size, offset);
775 const double ReleaseInactiveAtlasesTimerInterval = 0.5;
777 void CoordinatedLayerTreeHost::scheduleReleaseInactiveAtlases()
779 if (!m_releaseInactiveAtlasesTimer.isActive())
780 m_releaseInactiveAtlasesTimer.startRepeating(ReleaseInactiveAtlasesTimerInterval);
783 void CoordinatedLayerTreeHost::releaseInactiveAtlasesTimerFired(Timer<CoordinatedLayerTreeHost>*)
785 // We always want to keep one atlas for non-composited content.
786 OwnPtr<UpdateAtlas> atlasToKeepAnyway;
787 bool foundActiveAtlasForNonCompositedContent = false;
788 for (int i = m_updateAtlases.size() - 1; i >= 0; --i) {
789 UpdateAtlas* atlas = m_updateAtlases[i].get();
790 if (!atlas->isInUse())
791 atlas->addTimeInactive(ReleaseInactiveAtlasesTimerInterval);
792 bool usableForNonCompositedContent = !atlas->supportsAlpha();
793 if (atlas->isInactive()) {
794 if (!foundActiveAtlasForNonCompositedContent && !atlasToKeepAnyway && usableForNonCompositedContent)
795 atlasToKeepAnyway = m_updateAtlases[i].release();
796 m_updateAtlases.remove(i);
797 } else if (usableForNonCompositedContent)
798 foundActiveAtlasForNonCompositedContent = true;
801 if (!foundActiveAtlasForNonCompositedContent && atlasToKeepAnyway)
802 m_updateAtlases.append(atlasToKeepAnyway.release());
804 if (m_updateAtlases.size() <= 1)
805 m_releaseInactiveAtlasesTimer.stop();
808 void CoordinatedLayerTreeHost::setBackgroundColor(const WebCore::Color& color)
810 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::SetBackgroundColor(color));
813 void CoordinatedLayerTreeHost::commitScrollOffset(uint32_t layerID, const WebCore::IntSize& offset)
815 LayerMap::iterator i = m_registeredLayers.find(layerID);
816 if (i == m_registeredLayers.end())
819 i->value->commitScrollOffset(offset);
822 } // namespace WebKit
823 #endif // USE(COORDINATED_GRAPHICS)