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/TemporaryChange.h>
54 #if ENABLE(CSS_SHADERS)
55 #include "CustomFilterValidatedProgram.h"
56 #include "ValidatedCustomFilterOperation.h"
59 using namespace WebCore;
63 PassRefPtr<CoordinatedLayerTreeHost> CoordinatedLayerTreeHost::create(WebPage* webPage)
65 return adoptRef(new CoordinatedLayerTreeHost(webPage));
68 CoordinatedLayerTreeHost::~CoordinatedLayerTreeHost()
70 #if ENABLE(CSS_SHADERS)
71 disconnectCustomFilterPrograms();
75 HashSet<WebCore::CoordinatedGraphicsLayer*>::iterator end = m_registeredLayers.end();
76 for (HashSet<WebCore::CoordinatedGraphicsLayer*>::iterator it = m_registeredLayers.begin(); it != end; ++it)
77 (*it)->setCoordinator(0);
80 CoordinatedLayerTreeHost::CoordinatedLayerTreeHost(WebPage* webPage)
81 : LayerTreeHost(webPage)
82 , m_notifyAfterScheduledLayerFlush(false)
85 , m_isFlushingLayerChanges(false)
86 , m_waitingForUIProcess(true)
87 , m_isSuspended(false)
88 , m_shouldSendScrollPositionUpdate(true)
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 // Create a root layer.
98 m_rootLayer = GraphicsLayer::create(this, this);
99 CoordinatedGraphicsLayer* coordinatedRootLayer = toCoordinatedGraphicsLayer(m_rootLayer.get());
100 coordinatedRootLayer->setRootLayer(true);
102 m_rootLayer->setName("CoordinatedLayerTreeHost root layer");
104 m_rootLayer->setDrawsContent(false);
105 m_rootLayer->setSize(m_webPage->size());
106 m_layerTreeContext.coordinatedLayerID = toCoordinatedGraphicsLayer(coordinatedRootLayer)->id();
108 m_nonCompositedContentLayer = GraphicsLayer::create(this, this);
110 m_nonCompositedContentLayer->setName("CoordinatedLayerTreeHost non-composited content");
112 m_nonCompositedContentLayer->setDrawsContent(true);
113 m_nonCompositedContentLayer->setSize(m_webPage->size());
115 m_nonCompositedContentLayer->setShowDebugBorder(m_webPage->corePage()->settings()->showDebugBorders());
116 m_nonCompositedContentLayer->setShowRepaintCounter(m_webPage->corePage()->settings()->showRepaintCounter());
118 m_rootLayer->addChild(m_nonCompositedContentLayer.get());
120 CoordinatedSurface::setFactory(createCoordinatedSurface);
122 if (m_webPage->hasPageOverlay())
123 createPageOverlayLayer();
125 scheduleLayerFlush();
128 void CoordinatedLayerTreeHost::setLayerFlushSchedulingEnabled(bool layerFlushingEnabled)
130 if (m_layerFlushSchedulingEnabled == layerFlushingEnabled)
133 m_layerFlushSchedulingEnabled = layerFlushingEnabled;
135 if (m_layerFlushSchedulingEnabled) {
136 scheduleLayerFlush();
140 cancelPendingLayerFlush();
143 void CoordinatedLayerTreeHost::scheduleLayerFlush()
145 if (!m_layerFlushSchedulingEnabled)
148 if (!m_layerFlushTimer.isActive())
149 m_layerFlushTimer.startOneShot(0);
152 void CoordinatedLayerTreeHost::cancelPendingLayerFlush()
154 m_layerFlushTimer.stop();
157 void CoordinatedLayerTreeHost::setShouldNotifyAfterNextScheduledLayerFlush(bool notifyAfterScheduledLayerFlush)
159 m_notifyAfterScheduledLayerFlush = notifyAfterScheduledLayerFlush;
162 void CoordinatedLayerTreeHost::setRootCompositingLayer(WebCore::GraphicsLayer* graphicsLayer)
164 m_nonCompositedContentLayer->removeAllChildren();
165 m_nonCompositedContentLayer->setContentsOpaque(m_webPage->drawsBackground() && !m_webPage->drawsTransparentBackground());
167 // Add the accelerated layer tree hierarchy.
169 m_nonCompositedContentLayer->addChild(graphicsLayer);
172 void CoordinatedLayerTreeHost::invalidate()
174 cancelPendingLayerFlush();
177 m_rootLayer = nullptr;
181 void CoordinatedLayerTreeHost::setNonCompositedContentsNeedDisplay(const WebCore::IntRect& rect)
183 m_nonCompositedContentLayer->setNeedsDisplayInRect(rect);
184 if (m_pageOverlayLayer)
185 m_pageOverlayLayer->setNeedsDisplayInRect(rect);
187 scheduleLayerFlush();
190 void CoordinatedLayerTreeHost::scrollNonCompositedContents(const WebCore::IntRect& scrollRect, const WebCore::IntSize& /* scrollOffset */)
192 setNonCompositedContentsNeedDisplay(scrollRect);
195 void CoordinatedLayerTreeHost::forceRepaint()
197 // This is necessary for running layout tests. Since in this case we are not waiting for a UIProcess to reply nicely.
198 // Instead we are just triggering forceRepaint. But we still want to have the scripted animation callbacks being executed.
201 // We need to schedule another flush, otherwise the forced paint might cancel a later expected flush.
202 // This is aligned with LayerTreeHostCA.
203 scheduleLayerFlush();
204 flushPendingLayerChanges();
207 bool CoordinatedLayerTreeHost::forceRepaintAsync(uint64_t callbackID)
209 // We expect the UI process to not require a new repaint until the previous one has finished.
210 ASSERT(!m_forceRepaintAsyncCallbackID);
211 m_forceRepaintAsyncCallbackID = callbackID;
212 scheduleLayerFlush();
216 void CoordinatedLayerTreeHost::sizeDidChange(const WebCore::IntSize& newSize)
218 if (m_rootLayer->size() == newSize)
221 m_rootLayer->setSize(newSize);
223 // If the newSize exposes new areas of the non-composited content a setNeedsDisplay is needed
224 // for those newly exposed areas.
225 FloatSize oldSize = m_nonCompositedContentLayer->size();
226 m_nonCompositedContentLayer->setSize(newSize);
228 if (newSize.width() > oldSize.width()) {
229 float height = std::min(static_cast<float>(newSize.height()), oldSize.height());
230 m_nonCompositedContentLayer->setNeedsDisplayInRect(FloatRect(oldSize.width(), 0, newSize.width() - oldSize.width(), height));
233 if (newSize.height() > oldSize.height())
234 m_nonCompositedContentLayer->setNeedsDisplayInRect(FloatRect(0, oldSize.height(), newSize.width(), newSize.height() - oldSize.height()));
236 if (m_pageOverlayLayer)
237 m_pageOverlayLayer->setSize(newSize);
239 scheduleLayerFlush();
242 void CoordinatedLayerTreeHost::didInstallPageOverlay()
244 createPageOverlayLayer();
245 scheduleLayerFlush();
248 void CoordinatedLayerTreeHost::didUninstallPageOverlay()
250 destroyPageOverlayLayer();
251 scheduleLayerFlush();
254 void CoordinatedLayerTreeHost::setPageOverlayNeedsDisplay(const WebCore::IntRect& rect)
256 ASSERT(m_pageOverlayLayer);
257 m_pageOverlayLayer->setNeedsDisplayInRect(rect);
258 scheduleLayerFlush();
261 void CoordinatedLayerTreeHost::setPageOverlayOpacity(float value)
263 ASSERT(m_pageOverlayLayer);
264 m_pageOverlayLayer->setOpacity(value);
265 scheduleLayerFlush();
268 bool CoordinatedLayerTreeHost::flushPendingLayerChanges()
270 if (m_waitingForUIProcess)
273 TemporaryChange<bool> protector(m_isFlushingLayerChanges, true);
275 createCompositingLayers();
277 initializeRootCompositingLayerIfNeeded();
279 m_rootLayer->flushCompositingStateForThisLayerOnly();
280 m_nonCompositedContentLayer->flushCompositingStateForThisLayerOnly();
281 if (m_pageOverlayLayer)
282 m_pageOverlayLayer->flushCompositingStateForThisLayerOnly();
284 bool didSync = m_webPage->corePage()->mainFrame()->view()->flushCompositingStateIncludingSubframes();
286 flushPendingImageBackingChanges();
288 deleteCompositingLayers();
290 if (m_shouldSyncFrame) {
293 IntSize contentsSize = roundedIntSize(m_nonCompositedContentLayer->size());
294 IntRect coveredRect = toCoordinatedGraphicsLayer(m_nonCompositedContentLayer.get())->coverRect();
295 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::DidRenderFrame(contentsSize, coveredRect));
296 m_waitingForUIProcess = true;
297 m_shouldSyncFrame = false;
301 if (m_forceRepaintAsyncCallbackID) {
302 m_webPage->send(Messages::WebPageProxy::VoidCallback(m_forceRepaintAsyncCallbackID));
303 m_forceRepaintAsyncCallbackID = 0;
309 void CoordinatedLayerTreeHost::createCompositingLayers()
311 if (m_layersToCreate.isEmpty())
314 // If a layer gets created and deleted in the same cycle, we can simply remove it from m_layersToCreate and m_layersToDelete.
315 for (int i = m_layersToCreate.size() - 1; i >= 0; --i) {
316 size_t index = m_layersToDelete.find(m_layersToCreate[i]);
317 if (index != notFound) {
318 m_layersToCreate.remove(i);
319 m_layersToDelete.remove(index);
323 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::CreateCompositingLayers(m_layersToCreate));
324 m_layersToCreate.clear();
325 m_shouldSyncFrame = true;
328 void CoordinatedLayerTreeHost::deleteCompositingLayers()
330 if (m_layersToDelete.isEmpty())
334 m_layersToDelete.clear();
338 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::DeleteCompositingLayers(m_layersToDelete));
339 m_layersToDelete.clear();
340 m_shouldSyncFrame = true;
343 void CoordinatedLayerTreeHost::initializeRootCompositingLayerIfNeeded()
345 if (m_didInitializeRootCompositingLayer)
348 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::SetRootCompositingLayer(toCoordinatedGraphicsLayer(m_rootLayer.get())->id()));
349 m_didInitializeRootCompositingLayer = true;
350 m_shouldSyncFrame = true;
353 void CoordinatedLayerTreeHost::syncLayerState(CoordinatedLayerID id, const CoordinatedLayerInfo& info)
355 if (m_shouldSendScrollPositionUpdate) {
356 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::DidChangeScrollPosition(m_visibleContentsRect.location()));
357 m_shouldSendScrollPositionUpdate = false;
360 m_shouldSyncFrame = true;
361 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::SetCompositingLayerState(id, info));
364 void CoordinatedLayerTreeHost::syncLayerChildren(CoordinatedLayerID id, const Vector<CoordinatedLayerID>& children)
366 m_shouldSyncFrame = true;
367 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::SetCompositingLayerChildren(id, children));
370 #if USE(GRAPHICS_SURFACE)
371 void CoordinatedLayerTreeHost::createCanvas(CoordinatedLayerID id, PlatformLayer* canvasPlatformLayer)
373 m_shouldSyncFrame = true;
374 GraphicsSurfaceToken token = canvasPlatformLayer->graphicsSurfaceToken();
375 IntSize canvasSize = canvasPlatformLayer->platformLayerSize();
376 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::CreateCanvas(id, canvasSize, token));
379 void CoordinatedLayerTreeHost::syncCanvas(CoordinatedLayerID id, PlatformLayer* canvasPlatformLayer)
381 m_shouldSyncFrame = true;
382 uint32_t frontBuffer = canvasPlatformLayer->copyToGraphicsSurface();
383 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::SyncCanvas(id, frontBuffer));
386 void CoordinatedLayerTreeHost::destroyCanvas(CoordinatedLayerID id)
391 m_shouldSyncFrame = true;
392 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::DestroyCanvas(id));
396 void CoordinatedLayerTreeHost::setLayerRepaintCount(CoordinatedLayerID id, int value)
398 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::SetLayerRepaintCount(id, value));
401 #if ENABLE(CSS_FILTERS)
402 void CoordinatedLayerTreeHost::syncLayerFilters(CoordinatedLayerID id, const FilterOperations& filters)
404 m_shouldSyncFrame = true;
405 #if ENABLE(CSS_SHADERS)
406 checkCustomFilterProgramProxies(filters);
408 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::SetCompositingLayerFilters(id, filters));
412 #if ENABLE(CSS_SHADERS)
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);
465 void CoordinatedLayerTreeHost::detachLayer(CoordinatedGraphicsLayer* layer)
467 m_registeredLayers.remove(layer);
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(convertSecondsToDOMTimeStamp(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))
582 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::UpdateImageBacking(imageID, handle));
586 void CoordinatedLayerTreeHost::clearImageBackingContents(CoordinatedImageBackingID imageID)
588 m_shouldSyncFrame = true;
589 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::ClearImageBackingContents(imageID));
592 void CoordinatedLayerTreeHost::removeImageBacking(CoordinatedImageBackingID imageID)
597 ASSERT(m_imageBackings.contains(imageID));
598 m_shouldSyncFrame = true;
599 m_imageBackings.remove(imageID);
600 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::RemoveImageBacking(imageID));
603 void CoordinatedLayerTreeHost::flushPendingImageBackingChanges()
605 ImageBackingMap::iterator end = m_imageBackings.end();
606 for (ImageBackingMap::iterator iter = m_imageBackings.begin(); iter != end; ++iter)
607 iter->value->update();
610 void CoordinatedLayerTreeHost::notifyAnimationStarted(const WebCore::GraphicsLayer*, double /* time */)
614 void CoordinatedLayerTreeHost::notifyFlushRequired(const WebCore::GraphicsLayer*)
616 scheduleLayerFlush();
619 void CoordinatedLayerTreeHost::paintContents(const WebCore::GraphicsLayer* graphicsLayer, WebCore::GraphicsContext& graphicsContext, WebCore::GraphicsLayerPaintingPhase, const WebCore::IntRect& clipRect)
621 if (graphicsLayer == m_nonCompositedContentLayer) {
622 m_webPage->drawRect(graphicsContext, clipRect);
626 if (graphicsLayer == m_pageOverlayLayer) {
627 // Overlays contain transparent contents and won't clear the context as part of their rendering, so we do it here.
628 graphicsContext.clearRect(clipRect);
629 m_webPage->drawPageOverlay(graphicsContext, clipRect);
634 PassOwnPtr<GraphicsLayer> CoordinatedLayerTreeHost::createGraphicsLayer(GraphicsLayerClient* client)
636 CoordinatedGraphicsLayer* layer = new CoordinatedGraphicsLayer(client);
637 layer->setCoordinator(this);
638 m_registeredLayers.add(layer);
639 m_layersToCreate.append(layer->id());
640 layer->setNeedsVisibleRectAdjustment();
641 scheduleLayerFlush();
642 return adoptPtr(layer);
645 PassRefPtr<CoordinatedSurface> CoordinatedLayerTreeHost::createCoordinatedSurface(const IntSize& size, CoordinatedSurface::Flags flags)
647 return WebCoordinatedSurface::create(size, flags);
650 float CoordinatedLayerTreeHost::deviceScaleFactor() const
652 return m_webPage->deviceScaleFactor();
655 float CoordinatedLayerTreeHost::pageScaleFactor() const
657 return m_webPage->pageScaleFactor();
660 bool LayerTreeHost::supportsAcceleratedCompositing()
665 void CoordinatedLayerTreeHost::createTile(CoordinatedLayerID layerID, uint32_t tileID, const WebCore::SurfaceUpdateInfo& updateInfo, const WebCore::IntRect& tileRect)
667 m_shouldSyncFrame = true;
668 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::CreateTileForLayer(layerID, tileID, tileRect, updateInfo));
671 void CoordinatedLayerTreeHost::updateTile(CoordinatedLayerID layerID, uint32_t tileID, const WebCore::SurfaceUpdateInfo& updateInfo, const WebCore::IntRect& tileRect)
673 m_shouldSyncFrame = true;
674 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::UpdateTileForLayer(layerID, tileID, tileRect, updateInfo));
677 void CoordinatedLayerTreeHost::removeTile(CoordinatedLayerID layerID, uint32_t tileID)
681 m_shouldSyncFrame = true;
682 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::RemoveTileForLayer(layerID, tileID));
685 bool CoordinatedLayerTreeHost::createUpdateAtlas(uint32_t atlasID, PassRefPtr<CoordinatedSurface> coordinatedSurface)
687 WebCoordinatedSurface* webCoordinatedSurface = static_cast<WebCoordinatedSurface*>(coordinatedSurface.get());
688 WebCoordinatedSurface::Handle handle;
689 if (!webCoordinatedSurface->createHandle(handle))
691 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::CreateUpdateAtlas(atlasID, handle));
695 void CoordinatedLayerTreeHost::removeUpdateAtlas(uint32_t atlasID)
699 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::RemoveUpdateAtlas(atlasID));
702 WebCore::FloatRect CoordinatedLayerTreeHost::visibleContentsRect() const
704 return m_visibleContentsRect;
708 void CoordinatedLayerTreeHost::setLayerAnimations(CoordinatedLayerID layerID, const GraphicsLayerAnimations& animations)
710 m_shouldSyncFrame = true;
711 GraphicsLayerAnimations activeAnimations = animations.getActiveAnimations();
712 #if ENABLE(CSS_SHADERS)
713 for (size_t i = 0; i < activeAnimations.animations().size(); ++i) {
714 const KeyframeValueList& keyframes = animations.animations().at(i).keyframes();
715 if (keyframes.property() != AnimatedPropertyWebkitFilter)
717 for (size_t j = 0; j < keyframes.size(); ++j) {
718 const FilterAnimationValue* filterValue = static_cast<const FilterAnimationValue*>(keyframes.at(i));
719 checkCustomFilterProgramProxies(*filterValue->value());
723 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::SetLayerAnimations(layerID, activeAnimations));
726 void CoordinatedLayerTreeHost::setVisibleContentsRect(const FloatRect& rect, const FloatPoint& trajectoryVector)
728 // A zero trajectoryVector indicates that tiles all around the viewport are requested.
729 toCoordinatedGraphicsLayer(m_nonCompositedContentLayer.get())->setVisibleContentRectTrajectoryVector(trajectoryVector);
731 bool contentsRectDidChange = rect != m_visibleContentsRect;
732 if (contentsRectDidChange) {
733 m_visibleContentsRect = rect;
735 HashSet<WebCore::CoordinatedGraphicsLayer*>::iterator end = m_registeredLayers.end();
736 for (HashSet<WebCore::CoordinatedGraphicsLayer*>::iterator it = m_registeredLayers.begin(); it != end; ++it) {
737 (*it)->setNeedsVisibleRectAdjustment();
741 scheduleLayerFlush();
742 if (m_webPage->useFixedLayout()) {
743 // Round the rect instead of enclosing it to make sure that its size stays
744 // the same while panning. This can have nasty effects on layout.
745 m_webPage->setFixedVisibleContentRect(roundedIntRect(rect));
748 if (contentsRectDidChange)
749 m_shouldSendScrollPositionUpdate = true;
752 void CoordinatedLayerTreeHost::deviceOrPageScaleFactorChanged()
754 m_rootLayer->deviceOrPageScaleFactorChanged();
755 m_nonCompositedContentLayer->deviceOrPageScaleFactorChanged();
756 if (m_pageOverlayLayer)
757 m_pageOverlayLayer->deviceOrPageScaleFactorChanged();
760 GraphicsLayerFactory* CoordinatedLayerTreeHost::graphicsLayerFactory()
765 #if ENABLE(REQUEST_ANIMATION_FRAME)
766 void CoordinatedLayerTreeHost::scheduleAnimation()
768 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::RequestAnimationFrame());
771 void CoordinatedLayerTreeHost::animationFrameReady()
773 scheduleLayerFlush();
777 void CoordinatedLayerTreeHost::renderNextFrame()
779 m_waitingForUIProcess = false;
780 scheduleLayerFlush();
781 for (unsigned i = 0; i < m_updateAtlases.size(); ++i)
782 m_updateAtlases[i]->didSwapBuffers();
785 void CoordinatedLayerTreeHost::purgeBackingStores()
787 TemporaryChange<bool> purgingToggle(m_isPurging, true);
789 HashSet<WebCore::CoordinatedGraphicsLayer*>::iterator end = m_registeredLayers.end();
790 for (HashSet<WebCore::CoordinatedGraphicsLayer*>::iterator it = m_registeredLayers.begin(); it != end; ++it)
791 (*it)->purgeBackingStores();
793 m_imageBackings.clear();
794 m_updateAtlases.clear();
797 PassOwnPtr<GraphicsContext> CoordinatedLayerTreeHost::beginContentUpdate(const IntSize& size, CoordinatedSurface::Flags flags, uint32_t& atlasID, IntPoint& offset)
799 OwnPtr<GraphicsContext> graphicsContext;
800 for (unsigned i = 0; i < m_updateAtlases.size(); ++i) {
801 UpdateAtlas* atlas = m_updateAtlases[i].get();
802 if (atlas->supportsAlpha() == (flags & CoordinatedSurface::SupportsAlpha)) {
803 // This will return null if there is no available buffer space.
804 graphicsContext = atlas->beginPaintingOnAvailableBuffer(atlasID, size, offset);
806 return graphicsContext.release();
810 static const int ScratchBufferDimension = 1024; // Should be a power of two.
811 m_updateAtlases.append(adoptPtr(new UpdateAtlas(this, ScratchBufferDimension, flags)));
812 scheduleReleaseInactiveAtlases();
813 return m_updateAtlases.last()->beginPaintingOnAvailableBuffer(atlasID, size, offset);
816 const double ReleaseInactiveAtlasesTimerInterval = 0.5;
818 void CoordinatedLayerTreeHost::scheduleReleaseInactiveAtlases()
820 if (!m_releaseInactiveAtlasesTimer.isActive())
821 m_releaseInactiveAtlasesTimer.startRepeating(ReleaseInactiveAtlasesTimerInterval);
824 void CoordinatedLayerTreeHost::releaseInactiveAtlasesTimerFired(Timer<CoordinatedLayerTreeHost>*)
826 // We always want to keep one atlas for non-composited content.
827 OwnPtr<UpdateAtlas> atlasToKeepAnyway;
828 bool foundActiveAtlasForNonCompositedContent = false;
829 for (int i = m_updateAtlases.size() - 1; i >= 0; --i) {
830 UpdateAtlas* atlas = m_updateAtlases[i].get();
831 if (!atlas->isInUse())
832 atlas->addTimeInactive(ReleaseInactiveAtlasesTimerInterval);
833 bool usableForNonCompositedContent = !atlas->supportsAlpha();
834 if (atlas->isInactive()) {
835 if (!foundActiveAtlasForNonCompositedContent && !atlasToKeepAnyway && usableForNonCompositedContent)
836 atlasToKeepAnyway = m_updateAtlases[i].release();
837 m_updateAtlases.remove(i);
838 } else if (usableForNonCompositedContent)
839 foundActiveAtlasForNonCompositedContent = true;
842 if (!foundActiveAtlasForNonCompositedContent && atlasToKeepAnyway)
843 m_updateAtlases.append(atlasToKeepAnyway.release());
845 if (m_updateAtlases.size() <= 1)
846 m_releaseInactiveAtlasesTimer.stop();
849 void CoordinatedLayerTreeHost::setBackgroundColor(const WebCore::Color& color)
851 m_webPage->send(Messages::CoordinatedLayerTreeHostProxy::SetBackgroundColor(color));
854 } // namespace WebKit
855 #endif // USE(COORDINATED_GRAPHICS)