Title: [235165] trunk
Revision
235165
Author
zandober...@gmail.com
Date
2018-08-22 05:14:14 -0700 (Wed, 22 Aug 2018)

Log Message

[CoordGraphics] Switch to Nicosia::CompositionLayer state tracking
https://bugs.webkit.org/show_bug.cgi?id=188693

Reviewed by Carlos Garcia Campos.

.:

* Source/cmake/OptionsGTK.cmake: Enable USE_NICOSIA alongside
USE_COORDINATED_GRAPHICS and USE_COORDINATED_GRAPHICS_THREADED.
* Source/cmake/OptionsWPE.cmake: Ditto.

Source/WebCore:

Populate Nicosia::CompositionLayer with additional LayerState member
objects. For now we're using pending, staging and committed states,
though it might be possible to narrow down these to just two.

Pending state contains state that will be moved to staging during the
final steps of the next layer flush. flushState() method accumulates all
state changes in the staging state, and also allows the caller to
additionally perform flushing operations that are specific to backing
store, image backing or content layer containers.

commitState() method moves staging state over into the committed state,
again allowing user to pass a functor that receives the just-committed
state and apply it to their composition engine.

Changes in state objects are done under a thread-safe lock. This might
not be completely necessary at this point, but will be useful when
additonal layer state updates will be coming from e.g. the scrolling
thread. It might also make sense to tie in this lock use with the
Nicosia::Scene lock in the future.

Nicosia::ContentLayerTextureMapperImpl is modified slightly to allow
determining during flushes whether an update is pending. This is
necessary for a special case in ThreadedCompositor where content (i.e.
platform) layers like WebGL or video require an additional level of
scene update coordination. This complete special case has to go through
another review to see whether it's still necessary. Ideally we would be
able to remove it.

CoordinatedGraphicsLayer is finally switched over to using
Nicosia layer objects for state updates of any kind. This patch only
adds all the necessary bits, but doesn't yet remove any of the existing
code (but rather disables it temporarily, before it's removed).

Updating of simple state values is already in place. For backing stores,
the flushCompositingStateForThisLayerOnly() method now takes care of
preparing the backing store object if necessary as per layer state,
while the updateContentBuffers() method is switched to operate with
TiledBackingStore objects now kept on the BackingStoreTextureMapperImpl
instance associated with that backing store. Helper methods like
adjustContentsScale() and createBackingStore(), only called from the
updateContentBuffers() method, are removed and the code there inlined.

For image-backed layers, the update is now done directly in
the flushCompositingStateForThisLayerOnly() method, if necessary. The
helper syncImageBacking() method is commented out in order to prevent
double-painting of image buffers for now, but all this (along with the
CoordinatedImageBacking logic in CompositingCoordinator) will be removed
later.

For layers backed by platform layer objects, integration is relatively
simple. setContentsToPlatformLayer() is changed to properly handle any
passed-in platform layer object, and updatePlatformLayer() invokes the
swapBuffersIfNeeded() method on the ContentLayerTextureMapperImpl object
during each flush, if necessary.

In order to ensure any Nicosia-specific state update properly triggers
a composition update, m_nicosia.performLayerUpdate is added and flipped
to true during the flush in case of any state change. This then triggers
a layer sync in the CompositingCoordinator object when the
syncPendingStateChangesIncludingSubLayers() method is called. While no
old-style layer state update is provided, it causes the necessary
synchronization step that properly picks up the Nicosia-provided state
changes. Once the old-style layer state tracking is removed, this method
of update triggering will have to be updated as well.

* platform/graphics/nicosia/NicosiaPlatformLayer.h:
(Nicosia::CompositionLayer::flushState):
(Nicosia::CompositionLayer::commitState):
(Nicosia::CompositionLayer::accessCommitted):
* platform/graphics/nicosia/NicosiaScene.h:
* platform/graphics/nicosia/texmap/NicosiaContentLayerTextureMapperImpl.cpp:
(Nicosia::ContentLayerTextureMapperImpl::flushUpdate):
* platform/graphics/nicosia/texmap/NicosiaContentLayerTextureMapperImpl.h:
* platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.cpp:
(WebCore::CoordinatedGraphicsLayer::~CoordinatedGraphicsLayer):
(WebCore::CoordinatedGraphicsLayer::setContentsNeedsDisplay):
(WebCore::CoordinatedGraphicsLayer::setContentsToPlatformLayer):
(WebCore::CoordinatedGraphicsLayer::updatePlatformLayer):
(WebCore::CoordinatedGraphicsLayer::flushCompositingStateForThisLayerOnly):
(WebCore::CoordinatedGraphicsLayer::syncPendingStateChangesIncludingSubLayers):
(WebCore::CoordinatedGraphicsLayer::updateContentBuffers):
(WebCore::CoordinatedGraphicsLayer::purgeBackingStores):
(WebCore::CoordinatedGraphicsLayer::adjustContentsScale): Deleted.
(WebCore::CoordinatedGraphicsLayer::createBackingStore): Deleted.
* platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.h:

Source/WebKit:

Switch CoordinatedGraphicsScene to utilizing Nicosia::CompositionLayer
objects for state updates of the TextureMapper layer tree.

CoordinatedGraphicsScene::paintToCurrentGLContext() now calls
updateSceneState() at the beginning. This is a new method that manages
all updates for a given Nicosia::Scene instance. Any removed layers
have their composition-side state cleaned up, and the current set of
layers is then iterated to ensure and update the corresponding
TextureMapperLayer objects.

Layers with any backing (painted backing store, platform-layer or image
content) are temporarly stored for updating outside of mutex-controlled
scene update. Performing all other state updates outside of this mutex
area will be investigated at a later point.

We then iterate over vectors for each layer backing, gathering any
affected CoordinatedBackingStore or TextureMapperPlatformLayerProxy
objects that we have to update.

This drops a bunch of member variables and helper methods off the
CoordinatedGraphicsScene class. The applyStateChanges() method will be
further simplified in the future. coordinateUpdateCompletionWithClient
logic in ThreadedCompositor should be checked to see whether it still
addresses any real-life problem, because at the moment it imposes a few
additional operations in terms of scene updates that we could really do
without. This will be checked later and removed if possible.

* Shared/CoordinatedGraphics/CoordinatedGraphicsScene.cpp:
(WebKit::CoordinatedGraphicsScene::applyStateChanges):
(WebKit::CoordinatedGraphicsScene::paintToCurrentGLContext):
(WebKit::compositionLayerImpl):
(WebKit::contentLayerImpl):
(WebKit::backingStoreImpl):
(WebKit::imageBackingImpl):
(WebKit::texmapLayer):
(WebKit::updateBackingStore):
(WebKit::updateImageBacking):
(WebKit::removeLayer):
(WebKit::CoordinatedGraphicsScene::commitSceneState):
(WebKit::CoordinatedGraphicsScene::updateSceneState):
(WebKit::CoordinatedGraphicsScene::purgeGLResources):
(WebKit::CoordinatedGraphicsScene::syncPlatformLayerIfNeeded): Deleted.
(WebKit::CoordinatedGraphicsScene::setLayerChildrenIfNeeded): Deleted.
(WebKit::CoordinatedGraphicsScene::setLayerFiltersIfNeeded): Deleted.
(WebKit::CoordinatedGraphicsScene::setLayerState): Deleted.
(WebKit::CoordinatedGraphicsScene::getLayerByIDIfExists): Deleted.
(WebKit::CoordinatedGraphicsScene::createLayers): Deleted.
(WebKit::CoordinatedGraphicsScene::createLayer): Deleted.
(WebKit::CoordinatedGraphicsScene::deleteLayers): Deleted.
(WebKit::CoordinatedGraphicsScene::deleteLayer): Deleted.
(WebKit::CoordinatedGraphicsScene::setRootLayerID): Deleted.
(WebKit::CoordinatedGraphicsScene::prepareContentBackingStore): Deleted.
(WebKit::CoordinatedGraphicsScene::createBackingStoreIfNeeded): Deleted.
(WebKit::CoordinatedGraphicsScene::removeBackingStoreIfNeeded): Deleted.
(WebKit::CoordinatedGraphicsScene::resetBackingStoreSizeToLayerSize): Deleted.
(WebKit::CoordinatedGraphicsScene::createTilesIfNeeded): Deleted.
(WebKit::CoordinatedGraphicsScene::removeTilesIfNeeded): Deleted.
(WebKit::CoordinatedGraphicsScene::updateTilesIfNeeded): Deleted.
(WebKit::CoordinatedGraphicsScene::syncImageBackings): Deleted.
(WebKit::CoordinatedGraphicsScene::createImageBacking): Deleted.
(WebKit::CoordinatedGraphicsScene::updateImageBacking): Deleted.
(WebKit::CoordinatedGraphicsScene::clearImageBackingContents): Deleted.
(WebKit::CoordinatedGraphicsScene::removeImageBacking): Deleted.
(WebKit::CoordinatedGraphicsScene::assignImageBackingToLayer): Deleted.
(WebKit::CoordinatedGraphicsScene::setLayerAnimationsIfNeeded): Deleted.
* Shared/CoordinatedGraphics/CoordinatedGraphicsScene.h:
(WebKit::CoordinatedGraphicsScene::layerByID): Deleted.
* Shared/CoordinatedGraphics/threadedcompositor/ThreadedCompositor.cpp:
(WebKit::ThreadedCompositor::renderLayerTree):
* WebProcess/WebPage/CoordinatedGraphics/CompositingCoordinator.cpp:
(WebKit::CompositingCoordinator::flushPendingLayerChanges):

Modified Paths

Diff

Modified: trunk/ChangeLog (235164 => 235165)


--- trunk/ChangeLog	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/ChangeLog	2018-08-22 12:14:14 UTC (rev 235165)
@@ -1,3 +1,14 @@
+2018-08-22  Zan Dobersek  <zdober...@igalia.com>
+
+        [CoordGraphics] Switch to Nicosia::CompositionLayer state tracking
+        https://bugs.webkit.org/show_bug.cgi?id=188693
+
+        Reviewed by Carlos Garcia Campos.
+
+        * Source/cmake/OptionsGTK.cmake: Enable USE_NICOSIA alongside
+        USE_COORDINATED_GRAPHICS and USE_COORDINATED_GRAPHICS_THREADED.
+        * Source/cmake/OptionsWPE.cmake: Ditto.
+
 2018-08-21  Adrian Perez de Castro  <ape...@igalia.com>
 
         Unreviewed. Update OptionsWPE.cmake and NEWS for 2.21.91 release.

Modified: trunk/Source/WebCore/ChangeLog (235164 => 235165)


--- trunk/Source/WebCore/ChangeLog	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/Source/WebCore/ChangeLog	2018-08-22 12:14:14 UTC (rev 235165)
@@ -1,3 +1,96 @@
+2018-08-22  Zan Dobersek  <zdober...@igalia.com>
+
+        [CoordGraphics] Switch to Nicosia::CompositionLayer state tracking
+        https://bugs.webkit.org/show_bug.cgi?id=188693
+
+        Reviewed by Carlos Garcia Campos.
+
+        Populate Nicosia::CompositionLayer with additional LayerState member
+        objects. For now we're using pending, staging and committed states,
+        though it might be possible to narrow down these to just two.
+
+        Pending state contains state that will be moved to staging during the
+        final steps of the next layer flush. flushState() method accumulates all
+        state changes in the staging state, and also allows the caller to
+        additionally perform flushing operations that are specific to backing
+        store, image backing or content layer containers.
+
+        commitState() method moves staging state over into the committed state,
+        again allowing user to pass a functor that receives the just-committed
+        state and apply it to their composition engine.
+
+        Changes in state objects are done under a thread-safe lock. This might
+        not be completely necessary at this point, but will be useful when
+        additonal layer state updates will be coming from e.g. the scrolling
+        thread. It might also make sense to tie in this lock use with the
+        Nicosia::Scene lock in the future.
+
+        Nicosia::ContentLayerTextureMapperImpl is modified slightly to allow
+        determining during flushes whether an update is pending. This is
+        necessary for a special case in ThreadedCompositor where content (i.e.
+        platform) layers like WebGL or video require an additional level of
+        scene update coordination. This complete special case has to go through
+        another review to see whether it's still necessary. Ideally we would be
+        able to remove it.
+
+        CoordinatedGraphicsLayer is finally switched over to using
+        Nicosia layer objects for state updates of any kind. This patch only
+        adds all the necessary bits, but doesn't yet remove any of the existing
+        code (but rather disables it temporarily, before it's removed).
+
+        Updating of simple state values is already in place. For backing stores,
+        the flushCompositingStateForThisLayerOnly() method now takes care of
+        preparing the backing store object if necessary as per layer state,
+        while the updateContentBuffers() method is switched to operate with
+        TiledBackingStore objects now kept on the BackingStoreTextureMapperImpl
+        instance associated with that backing store. Helper methods like
+        adjustContentsScale() and createBackingStore(), only called from the
+        updateContentBuffers() method, are removed and the code there inlined.
+
+        For image-backed layers, the update is now done directly in
+        the flushCompositingStateForThisLayerOnly() method, if necessary. The
+        helper syncImageBacking() method is commented out in order to prevent
+        double-painting of image buffers for now, but all this (along with the
+        CoordinatedImageBacking logic in CompositingCoordinator) will be removed
+        later.
+
+        For layers backed by platform layer objects, integration is relatively
+        simple. setContentsToPlatformLayer() is changed to properly handle any
+        passed-in platform layer object, and updatePlatformLayer() invokes the
+        swapBuffersIfNeeded() method on the ContentLayerTextureMapperImpl object
+        during each flush, if necessary.
+
+        In order to ensure any Nicosia-specific state update properly triggers
+        a composition update, m_nicosia.performLayerUpdate is added and flipped
+        to true during the flush in case of any state change. This then triggers
+        a layer sync in the CompositingCoordinator object when the
+        syncPendingStateChangesIncludingSubLayers() method is called. While no
+        old-style layer state update is provided, it causes the necessary
+        synchronization step that properly picks up the Nicosia-provided state
+        changes. Once the old-style layer state tracking is removed, this method
+        of update triggering will have to be updated as well.
+
+        * platform/graphics/nicosia/NicosiaPlatformLayer.h:
+        (Nicosia::CompositionLayer::flushState):
+        (Nicosia::CompositionLayer::commitState):
+        (Nicosia::CompositionLayer::accessCommitted):
+        * platform/graphics/nicosia/NicosiaScene.h:
+        * platform/graphics/nicosia/texmap/NicosiaContentLayerTextureMapperImpl.cpp:
+        (Nicosia::ContentLayerTextureMapperImpl::flushUpdate):
+        * platform/graphics/nicosia/texmap/NicosiaContentLayerTextureMapperImpl.h:
+        * platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.cpp:
+        (WebCore::CoordinatedGraphicsLayer::~CoordinatedGraphicsLayer):
+        (WebCore::CoordinatedGraphicsLayer::setContentsNeedsDisplay):
+        (WebCore::CoordinatedGraphicsLayer::setContentsToPlatformLayer):
+        (WebCore::CoordinatedGraphicsLayer::updatePlatformLayer):
+        (WebCore::CoordinatedGraphicsLayer::flushCompositingStateForThisLayerOnly):
+        (WebCore::CoordinatedGraphicsLayer::syncPendingStateChangesIncludingSubLayers):
+        (WebCore::CoordinatedGraphicsLayer::updateContentBuffers):
+        (WebCore::CoordinatedGraphicsLayer::purgeBackingStores):
+        (WebCore::CoordinatedGraphicsLayer::adjustContentsScale): Deleted.
+        (WebCore::CoordinatedGraphicsLayer::createBackingStore): Deleted.
+        * platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.h:
+
 2018-08-22  David Kilzer  <ddkil...@apple.com>
 
         Remove stale file references in WebCore Xcode project

Modified: trunk/Source/WebCore/platform/graphics/nicosia/NicosiaPlatformLayer.h (235164 => 235165)


--- trunk/Source/WebCore/platform/graphics/nicosia/NicosiaPlatformLayer.h	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/Source/WebCore/platform/graphics/nicosia/NicosiaPlatformLayer.h	2018-08-22 12:14:14 UTC (rev 235165)
@@ -180,6 +180,88 @@
         functor(m_state.pending);
     }
 
+    template<typename T>
+    void flushState(const T& functor)
+    {
+        LockHolder locker(PlatformLayer::m_state.lock);
+        auto& pending = m_state.pending;
+        auto& staging = m_state.staging;
+
+        staging.delta.value |= pending.delta.value;
+
+        if (pending.delta.positionChanged)
+            staging.position = pending.position;
+        if (pending.delta.anchorPointChanged)
+            staging.anchorPoint = pending.anchorPoint;
+        if (pending.delta.sizeChanged)
+            staging.size = pending.size;
+
+        if (pending.delta.transformChanged)
+            staging.transform = pending.transform;
+        if (pending.delta.childrenTransformChanged)
+            staging.childrenTransform = pending.childrenTransform;
+
+        if (pending.delta.contentsRectChanged)
+            staging.contentsRect = pending.contentsRect;
+        if (pending.delta.contentsTilingChanged) {
+            staging.contentsTilePhase = pending.contentsTilePhase;
+            staging.contentsTileSize = pending.contentsTileSize;
+        }
+
+        if (pending.delta.opacityChanged)
+            staging.opacity = pending.opacity;
+        if (pending.delta.solidColorChanged)
+            staging.solidColor = pending.solidColor;
+
+        if (pending.delta.filtersChanged)
+            staging.filters = pending.filters;
+        if (pending.delta.animationsChanged)
+            staging.animations = pending.animations;
+
+        if (pending.delta.childrenChanged)
+            staging.children = pending.children;
+        if (pending.delta.maskChanged)
+            staging.mask = pending.mask;
+        if (pending.delta.replicaChanged)
+            staging.replica = pending.replica;
+
+        if (pending.delta.flagsChanged)
+            staging.flags.value = pending.flags.value;
+
+        if (pending.delta.repaintCounterChanged)
+            staging.repaintCounter = pending.repaintCounter;
+        if (pending.delta.debugBorderChanged)
+            staging.debugBorder = pending.debugBorder;
+
+        if (pending.delta.backingStoreChanged)
+            staging.backingStore = pending.backingStore;
+        if (pending.delta.contentLayerChanged)
+            staging.contentLayer = pending.contentLayer;
+        if (pending.delta.imageBackingChanged)
+            staging.imageBacking = pending.imageBacking;
+
+        pending.delta = { };
+
+        functor(staging);
+    }
+
+    template<typename T>
+    void commitState(const T& functor)
+    {
+        LockHolder locker(PlatformLayer::m_state.lock);
+        m_state.committed = m_state.staging;
+        m_state.staging.delta = { };
+
+        functor(m_state.committed);
+    }
+
+    template<typename T>
+    void accessCommitted(const T& functor)
+    {
+        LockHolder locker(PlatformLayer::m_state.lock);
+        functor(m_state.committed);
+    }
+
 private:
     CompositionLayer(uint64_t, const Impl::Factory&);
 
@@ -187,6 +269,8 @@
 
     struct {
         LayerState pending;
+        LayerState staging;
+        LayerState committed;
     } m_state;
 };
 

Modified: trunk/Source/WebCore/platform/graphics/nicosia/NicosiaScene.h (235164 => 235165)


--- trunk/Source/WebCore/platform/graphics/nicosia/NicosiaScene.h	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/Source/WebCore/platform/graphics/nicosia/NicosiaScene.h	2018-08-22 12:14:14 UTC (rev 235165)
@@ -51,6 +51,10 @@
         ~State();
 
         uint32_t id { 0 };
+        // FIXME: This is needed for a ThreadedCompositor oddity that might not even be
+        // necessary anymore. It that has to be checked and ideally removed.
+        // https://bugs.webkit.org/show_bug.cgi?id=188839
+        bool platformLayerUpdated { false };
         HashSet<RefPtr<Nicosia::CompositionLayer>> layers;
         RefPtr<Nicosia::CompositionLayer> rootLayer;
     };

Modified: trunk/Source/WebCore/platform/graphics/nicosia/texmap/NicosiaContentLayerTextureMapperImpl.cpp (235164 => 235165)


--- trunk/Source/WebCore/platform/graphics/nicosia/texmap/NicosiaContentLayerTextureMapperImpl.cpp	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/Source/WebCore/platform/graphics/nicosia/texmap/NicosiaContentLayerTextureMapperImpl.cpp	2018-08-22 12:14:14 UTC (rev 235165)
@@ -63,6 +63,12 @@
     m_client.client = nullptr;
 }
 
+bool ContentLayerTextureMapperImpl::flushUpdate()
+{
+    LockHolder locker(m_client.lock);
+    return std::exchange(m_client.pendingUpdate, false);
+}
+
 void ContentLayerTextureMapperImpl::swapBuffersIfNeeded()
 {
     LockHolder locker(m_client.lock);

Modified: trunk/Source/WebCore/platform/graphics/nicosia/texmap/NicosiaContentLayerTextureMapperImpl.h (235164 => 235165)


--- trunk/Source/WebCore/platform/graphics/nicosia/texmap/NicosiaContentLayerTextureMapperImpl.h	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/Source/WebCore/platform/graphics/nicosia/texmap/NicosiaContentLayerTextureMapperImpl.h	2018-08-22 12:14:14 UTC (rev 235165)
@@ -56,6 +56,8 @@
 
     void invalidateClient();
 
+    bool flushUpdate();
+
     WebCore::TextureMapperPlatformLayerProxy& proxy() const { return m_proxy; }
     void swapBuffersIfNeeded();
 
@@ -64,6 +66,7 @@
     struct {
         Lock lock;
         Client* client { nullptr };
+        bool pendingUpdate { true }; // Starts off with a pending update.
     } m_client;
 };
 

Modified: trunk/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.cpp (235164 => 235165)


--- trunk/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.cpp	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.cpp	2018-08-22 12:14:14 UTC (rev 235165)
@@ -30,7 +30,11 @@
 #include "GraphicsContext.h"
 #include "GraphicsLayer.h"
 #include "GraphicsLayerFactory.h"
+#include "NicosiaBackingStoreTextureMapperImpl.h"
 #include "NicosiaCompositionLayerTextureMapperImpl.h"
+#include "NicosiaContentLayerTextureMapperImpl.h"
+#include "NicosiaImageBackingTextureMapperImpl.h"
+#include "NicosiaPaintingContext.h"
 #include "NicosiaPaintingEngine.h"
 #include "ScrollableArea.h"
 #include "TextureMapperPlatformLayerProxyProvider.h"
@@ -160,6 +164,8 @@
     }
     ASSERT(!m_coordinatedImageBacking);
     ASSERT(!m_mainBackingStore);
+    ASSERT(!m_nicosia.imageBacking);
+    ASSERT(!m_nicosia.backingStore);
     willBeDestroyed();
 }
 
@@ -421,9 +427,14 @@
 void CoordinatedGraphicsLayer::setContentsNeedsDisplay()
 {
 #if USE(COORDINATED_GRAPHICS_THREADED)
+#if USE(NICOSIA)
+    if (m_nicosia.contentLayer)
+        m_shouldUpdatePlatformLayer = true;
+#else
     if (m_platformLayer)
         m_shouldUpdatePlatformLayer = true;
 #endif
+#endif
 
     notifyFlushRequired();
     addRepaintRect(contentsRect());
@@ -433,6 +444,12 @@
 {
 #if USE(COORDINATED_GRAPHICS_THREADED)
 #if USE(NICOSIA)
+    auto* contentLayer = downcast<Nicosia::ContentLayer>(platformLayer);
+    if (m_nicosia.contentLayer != contentLayer) {
+        m_shouldSyncPlatformLayer = true;
+        m_nicosia.contentLayer = contentLayer;
+        m_nicosia.delta.contentLayerChanged = true;
+    }
 #else
     if (m_platformLayer != platformLayer)
         m_shouldSyncPlatformLayer = true;
@@ -767,6 +784,8 @@
     m_shouldUpdatePlatformLayer = false;
 #if USE(COORDINATED_GRAPHICS_THREADED)
 #if USE(NICOSIA)
+    if (m_nicosia.contentLayer)
+        downcast<Nicosia::ContentLayerTextureMapperImpl>(m_nicosia.contentLayer->impl()).swapBuffersIfNeeded();
 #else
     m_layerState.platformLayerUpdated = true;
     if (m_platformLayer)
@@ -785,7 +804,10 @@
     // Sets the values.
     computePixelAlignment(m_adjustedPosition, m_adjustedSize, m_adjustedAnchorPoint, m_pixelAlignmentOffset);
 
-    syncImageBacking();
+    // FIXME: this is commented out to immediately disable any old-way image updates.
+    // It will soon be removed in a larger cleanup.
+    // syncImageBacking();
+
     syncLayerState();
     syncAnimations();
     computeTransformedVisibleRect();
@@ -798,6 +820,67 @@
     if (!hasActiveTransformAnimation)
         m_movingVisibleRect = false;
 
+    // Determine the backing store presence. Content is painted later, in the updateContentBuffers() traversal.
+    if (shouldHaveBackingStore()) {
+        if (!m_nicosia.backingStore) {
+            m_nicosia.backingStore = Nicosia::BackingStore::create(Nicosia::BackingStoreTextureMapperImpl::createFactory());
+            m_nicosia.delta.backingStoreChanged = true;
+        }
+    } else if (m_nicosia.backingStore) {
+        auto& layerState = downcast<Nicosia::BackingStoreTextureMapperImpl>(m_nicosia.backingStore->impl()).layerState();
+        layerState.isPurging = true;
+        layerState.mainBackingStore = nullptr;
+        layerState.previousBackingStore = nullptr;
+
+        m_nicosia.backingStore = nullptr;
+        m_nicosia.delta.backingStoreChanged = true;
+    }
+
+    // Determine image backing presence according to the composited image source.
+    if (m_compositedNativeImagePtr) {
+        ASSERT(m_compositedImage);
+        auto& image = *m_compositedImage;
+        uintptr_t imageID = reinterpret_cast<uintptr_t>(&image);
+        uintptr_t nativeImageID = reinterpret_cast<uintptr_t>(m_compositedNativeImagePtr.get());
+
+        // Respawn the ImageBacking object if the underlying image changed.
+        if (m_nicosia.imageBacking) {
+            auto& impl = downcast<Nicosia::ImageBackingTextureMapperImpl>(m_nicosia.imageBacking->impl());
+            if (impl.layerState().imageID != imageID) {
+                impl.layerState().update = Nicosia::ImageBackingTextureMapperImpl::Update { };
+                m_nicosia.imageBacking = nullptr;
+            }
+        }
+        if (!m_nicosia.imageBacking) {
+            m_nicosia.imageBacking = Nicosia::ImageBacking::create(Nicosia::ImageBackingTextureMapperImpl::createFactory());
+            m_nicosia.delta.imageBackingChanged = true;
+        }
+
+        // Update the image contents only when the image layer is visible and the native image changed.
+        auto& impl = downcast<Nicosia::ImageBackingTextureMapperImpl>(m_nicosia.imageBacking->impl());
+        auto& layerState = impl.layerState();
+        layerState.imageID = imageID;
+        layerState.update.isVisible = transformedVisibleRect().intersects(IntRect(contentsRect()));
+        if (layerState.update.isVisible && layerState.nativeImageID != nativeImageID) {
+            auto buffer = Nicosia::Buffer::create(IntSize(image.size()),
+                !image.currentFrameKnownToBeOpaque() ? Nicosia::Buffer::SupportsAlpha : Nicosia::Buffer::NoFlags);
+            Nicosia::PaintingContext::paint(buffer,
+                [&image](GraphicsContext& context)
+                {
+                    IntRect rect { { }, IntSize { image.size() } };
+                    context.drawImage(image, rect, rect, ImagePaintingOptions(CompositeCopy));
+                });
+            layerState.nativeImageID = nativeImageID;
+            layerState.update.buffer = WTFMove(buffer);
+            m_nicosia.delta.imageBackingChanged = true;
+        }
+    } else if (m_nicosia.imageBacking) {
+        auto& layerState = downcast<Nicosia::ImageBackingTextureMapperImpl>(m_nicosia.imageBacking->impl()).layerState();
+        layerState.update = Nicosia::ImageBackingTextureMapperImpl::Update { };
+        m_nicosia.imageBacking = nullptr;
+        m_nicosia.delta.imageBackingChanged = true;
+    }
+
     {
         m_nicosia.layer->updateState(
             [this](Nicosia::CompositionLayer::LayerState& state)
@@ -867,7 +950,15 @@
                     state.repaintCounter = m_nicosia.repaintCounter;
                 if (localDelta.debugBorderChanged)
                     state.debugBorder = m_nicosia.debugBorder;
+
+                if (localDelta.backingStoreChanged)
+                    state.backingStore = m_nicosia.backingStore;
+                if (localDelta.contentLayerChanged)
+                    state.contentLayer = m_nicosia.contentLayer;
+                if (localDelta.imageBackingChanged)
+                    state.imageBacking = m_nicosia.imageBacking;
             });
+        m_nicosia.performLayerSync = !!m_nicosia.delta.value;
         m_nicosia.delta = { };
     }
 }
@@ -874,10 +965,14 @@
 
 void CoordinatedGraphicsLayer::syncPendingStateChangesIncludingSubLayers()
 {
-    if (m_layerState.hasPendingChanges()) {
+    // FIXME: For now the best way for layer state changes to trigger an update is to use
+    // the CoordinatedGraphicsLayerClient::syncLayerState() method. This should be simplified
+    // once the m_layerState member object is removed.
+    if (m_nicosia.performLayerSync || m_layerState.hasPendingChanges()) {
         m_coordinator->syncLayerState(m_id, m_layerState);
         resetLayerState();
     }
+    m_nicosia.performLayerSync = false;
 
     if (maskLayer())
         downcast<CoordinatedGraphicsLayer>(*maskLayer()).syncPendingStateChangesIncludingSubLayers();
@@ -923,27 +1018,6 @@
     return selfOrAncestorHaveNonAffineTransforms() ? 1 : deviceScaleFactor() * pageScaleFactor();
 }
 
-void CoordinatedGraphicsLayer::adjustContentsScale()
-{
-    ASSERT(shouldHaveBackingStore());
-    if (!m_mainBackingStore || m_mainBackingStore->contentsScale() == effectiveContentsScale())
-        return;
-
-    // Between creating the new backing store and painting the content,
-    // we do not want to drop the previous one as that might result in
-    // briefly seeing flickering as the old tiles may be dropped before
-    // something replaces them.
-    m_previousBackingStore = WTFMove(m_mainBackingStore);
-
-    // No reason to save the previous backing store for non-visible areas.
-    m_previousBackingStore->removeAllNonVisibleTiles(transformedVisibleRect(), IntRect(0, 0, size().width(), size().height()));
-}
-
-void CoordinatedGraphicsLayer::createBackingStore()
-{
-    m_mainBackingStore = std::make_unique<TiledBackingStore>(*this, effectiveContentsScale());
-}
-
 void CoordinatedGraphicsLayer::tiledBackingStoreHasPendingTileCreation()
 {
     setNeedsVisibleRectAdjustment();
@@ -1024,32 +1098,57 @@
 
 void CoordinatedGraphicsLayer::updateContentBuffers()
 {
-    if (!shouldHaveBackingStore()) {
-        m_mainBackingStore = nullptr;
-        m_previousBackingStore = nullptr;
+    if (!m_nicosia.backingStore)
         return;
-    }
 
+    // Prepare for painting on the impl-contained backing store. isFlushing is used there
+    // for internal sanity checks.
+    auto& impl = downcast<Nicosia::BackingStoreTextureMapperImpl>(m_nicosia.backingStore->impl());
+    auto& layerState = impl.layerState();
+    layerState.isFlushing = true;
+
+    // Helper lambda that finished the flush update and determines layer sync necessity.
+    auto finishUpdate =
+        [this, &layerState] {
+            auto& update = layerState.update;
+            m_nicosia.performLayerSync = !update.tilesToCreate.isEmpty()
+                || !update.tilesToRemove.isEmpty() || !update.tilesToUpdate.isEmpty();
+            layerState.isFlushing = false;
+        };
+
+    // Address the content scale adjustment.
+    // FIXME: the previousBackingStore logic is likely possible to remove.
+    // https://bugs.webkit.org/show_bug.cgi?id=188693
     if (m_pendingContentsScaleAdjustment) {
-        adjustContentsScale();
+        if (layerState.mainBackingStore && layerState.mainBackingStore->contentsScale() != effectiveContentsScale()) {
+            // Between creating the new backing store and painting the content, we do not
+            // want to drop the previous one as that might result in briefly seeing flickering
+            // as the old tiles may be dropped before something replaces them.
+            layerState.previousBackingStore = WTFMove(layerState.mainBackingStore);
+
+            // No reason to save the previous backing store for non-visible areas.
+            layerState.previousBackingStore->removeAllNonVisibleTiles(transformedVisibleRect(), IntRect(0, 0, size().width(), size().height()));
+        }
         m_pendingContentsScaleAdjustment = false;
     }
 
-    // This is the only place we (re)create the main tiled backing store, once we
-    // have a remote client and we are ready to send our data to the UI process.
-    if (!m_mainBackingStore) {
-        createBackingStore();
+    // Ensure the TiledBackingStore object, and enforce a complete repaint if it's not been present yet.
+    if (!layerState.mainBackingStore) {
+        layerState.mainBackingStore = std::make_unique<TiledBackingStore>(impl, effectiveContentsScale());
         m_pendingVisibleRectAdjustment = true;
     }
 
-    if (!m_pendingVisibleRectAdjustment && !m_needsDisplay.completeLayer && m_needsDisplay.rects.isEmpty())
+    // Bail if there's no painting recorded or enforced.
+    if (!m_pendingVisibleRectAdjustment && !m_needsDisplay.completeLayer && m_needsDisplay.rects.isEmpty()) {
+        finishUpdate();
         return;
+    }
 
     if (!m_needsDisplay.completeLayer) {
         for (auto& rect : m_needsDisplay.rects)
-            m_mainBackingStore->invalidate(IntRect { rect });
+            layerState.mainBackingStore->invalidate(IntRect { rect });
     } else
-        m_mainBackingStore->invalidate({ { }, IntSize { m_size } });
+        layerState.mainBackingStore->invalidate({ { }, IntSize { m_size } });
 
     m_needsDisplay.completeLayer = false;
     m_needsDisplay.rects.clear();
@@ -1056,12 +1155,13 @@
 
     if (m_pendingVisibleRectAdjustment) {
         m_pendingVisibleRectAdjustment = false;
-        m_mainBackingStore->createTilesIfNeeded(transformedVisibleRect(), IntRect(0, 0, size().width(), size().height()));
+        layerState.mainBackingStore->createTilesIfNeeded(transformedVisibleRect(), IntRect(0, 0, m_size.width(), m_size.height()));
     }
 
     ASSERT(m_coordinator && m_coordinator->isFlushingLayerChanges());
 
-    auto dirtyTiles = m_mainBackingStore->dirtyTiles();
+    // With all the affected tiles created and/or invalidated, we can finally paint them.
+    auto dirtyTiles = layerState.mainBackingStore->dirtyTiles();
     if (!dirtyTiles.isEmpty()) {
         bool didUpdateTiles = false;
 
@@ -1079,11 +1179,11 @@
             updateInfo.buffer = coordinatedBuffer.copyRef();
 
             if (!m_coordinator->paintingEngine().paint(*this, WTFMove(coordinatedBuffer),
-                dirtyRect, m_mainBackingStore->mapToContents(dirtyRect),
-                IntRect { { 0, 0 }, dirtyRect.size() }, m_mainBackingStore->contentsScale()))
+                dirtyRect, layerState.mainBackingStore->mapToContents(dirtyRect),
+                IntRect { { 0, 0 }, dirtyRect.size() }, layerState.mainBackingStore->contentsScale()))
                 continue;
 
-            updateTile(tile.tileID(), updateInfo, tileRect);
+            impl.updateTile(tile.tileID(), updateInfo, tileRect);
 
             tile.markClean();
             didUpdateTiles |= true;
@@ -1096,8 +1196,16 @@
     // The previous backing store is kept around to avoid flickering between
     // removing the existing tiles and painting the new ones. The first time
     // the visibleRect is full painted we remove the previous backing store.
-    if (m_previousBackingStore && m_mainBackingStore->visibleAreaIsCovered())
-        m_previousBackingStore = nullptr;
+    if (layerState.previousBackingStore && layerState.mainBackingStore->visibleAreaIsCovered())
+        layerState.previousBackingStore = nullptr;
+
+    // Request a second update immediately if some tiles are still pending creation.
+    if (layerState.hasPendingTileCreation) {
+        setNeedsVisibleRectAdjustment();
+        notifyFlushRequired();
+    }
+
+    finishUpdate();
 }
 
 void CoordinatedGraphicsLayer::purgeBackingStores()
@@ -1107,7 +1215,15 @@
 #endif
     m_mainBackingStore = nullptr;
     m_previousBackingStore = nullptr;
+    if (m_nicosia.backingStore) {
+        auto& layerState = downcast<Nicosia::BackingStoreTextureMapperImpl>(m_nicosia.backingStore->impl()).layerState();
+        layerState.isPurging = true;
+        layerState.mainBackingStore = nullptr;
+        layerState.previousBackingStore = nullptr;
 
+        m_nicosia.backingStore = nullptr;
+    }
+
     releaseImageBackingIfNeeded();
 
     didChangeLayerState();

Modified: trunk/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.h (235164 => 235165)


--- trunk/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.h	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.h	2018-08-22 12:14:14 UTC (rev 235165)
@@ -110,7 +110,7 @@
     void removeAnimation(const String&) override;
     void suspendAnimations(MonotonicTime) override;
     void resumeAnimations() override;
-    bool usesContentsLayer() const override { return m_platformLayer || m_compositedImage; }
+    bool usesContentsLayer() const override { return m_platformLayer || m_nicosia.contentLayer || m_compositedImage; }
 
     void syncPendingStateChangesIncludingSubLayers();
     void updateContentBuffersIncludingSubLayers();
@@ -161,7 +161,6 @@
     void computeTransformedVisibleRect();
     void updateContentBuffers();
 
-    void createBackingStore();
     void releaseImageBackingIfNeeded();
 
     void notifyFlushRequired();
@@ -171,7 +170,6 @@
     bool shouldHaveBackingStore() const;
     bool selfOrAncestorHasActiveTransformAnimation() const;
     bool selfOrAncestorHaveNonAffineTransforms();
-    void adjustContentsScale();
 
     void setShouldUpdateVisibleRect();
     float effectiveContentsScale();
@@ -231,6 +229,11 @@
         Nicosia::CompositionLayer::LayerState::Delta delta;
         Nicosia::CompositionLayer::LayerState::RepaintCounter repaintCounter;
         Nicosia::CompositionLayer::LayerState::DebugBorder debugBorder;
+        bool performLayerSync { false };
+
+        RefPtr<Nicosia::BackingStore> backingStore;
+        RefPtr<Nicosia::ContentLayer> contentLayer;
+        RefPtr<Nicosia::ImageBacking> imageBacking;
     } m_nicosia;
 };
 

Modified: trunk/Source/WebKit/ChangeLog (235164 => 235165)


--- trunk/Source/WebKit/ChangeLog	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/Source/WebKit/ChangeLog	2018-08-22 12:14:14 UTC (rev 235165)
@@ -1,3 +1,82 @@
+2018-08-22  Zan Dobersek  <zdober...@igalia.com>
+
+        [CoordGraphics] Switch to Nicosia::CompositionLayer state tracking
+        https://bugs.webkit.org/show_bug.cgi?id=188693
+
+        Reviewed by Carlos Garcia Campos.
+
+        Switch CoordinatedGraphicsScene to utilizing Nicosia::CompositionLayer
+        objects for state updates of the TextureMapper layer tree.
+
+        CoordinatedGraphicsScene::paintToCurrentGLContext() now calls
+        updateSceneState() at the beginning. This is a new method that manages
+        all updates for a given Nicosia::Scene instance. Any removed layers
+        have their composition-side state cleaned up, and the current set of
+        layers is then iterated to ensure and update the corresponding
+        TextureMapperLayer objects.
+
+        Layers with any backing (painted backing store, platform-layer or image
+        content) are temporarly stored for updating outside of mutex-controlled
+        scene update. Performing all other state updates outside of this mutex
+        area will be investigated at a later point.
+
+        We then iterate over vectors for each layer backing, gathering any
+        affected CoordinatedBackingStore or TextureMapperPlatformLayerProxy
+        objects that we have to update.
+
+        This drops a bunch of member variables and helper methods off the
+        CoordinatedGraphicsScene class. The applyStateChanges() method will be
+        further simplified in the future. coordinateUpdateCompletionWithClient
+        logic in ThreadedCompositor should be checked to see whether it still
+        addresses any real-life problem, because at the moment it imposes a few
+        additional operations in terms of scene updates that we could really do
+        without. This will be checked later and removed if possible.
+
+        * Shared/CoordinatedGraphics/CoordinatedGraphicsScene.cpp:
+        (WebKit::CoordinatedGraphicsScene::applyStateChanges):
+        (WebKit::CoordinatedGraphicsScene::paintToCurrentGLContext):
+        (WebKit::compositionLayerImpl):
+        (WebKit::contentLayerImpl):
+        (WebKit::backingStoreImpl):
+        (WebKit::imageBackingImpl):
+        (WebKit::texmapLayer):
+        (WebKit::updateBackingStore):
+        (WebKit::updateImageBacking):
+        (WebKit::removeLayer):
+        (WebKit::CoordinatedGraphicsScene::commitSceneState):
+        (WebKit::CoordinatedGraphicsScene::updateSceneState):
+        (WebKit::CoordinatedGraphicsScene::purgeGLResources):
+        (WebKit::CoordinatedGraphicsScene::syncPlatformLayerIfNeeded): Deleted.
+        (WebKit::CoordinatedGraphicsScene::setLayerChildrenIfNeeded): Deleted.
+        (WebKit::CoordinatedGraphicsScene::setLayerFiltersIfNeeded): Deleted.
+        (WebKit::CoordinatedGraphicsScene::setLayerState): Deleted.
+        (WebKit::CoordinatedGraphicsScene::getLayerByIDIfExists): Deleted.
+        (WebKit::CoordinatedGraphicsScene::createLayers): Deleted.
+        (WebKit::CoordinatedGraphicsScene::createLayer): Deleted.
+        (WebKit::CoordinatedGraphicsScene::deleteLayers): Deleted.
+        (WebKit::CoordinatedGraphicsScene::deleteLayer): Deleted.
+        (WebKit::CoordinatedGraphicsScene::setRootLayerID): Deleted.
+        (WebKit::CoordinatedGraphicsScene::prepareContentBackingStore): Deleted.
+        (WebKit::CoordinatedGraphicsScene::createBackingStoreIfNeeded): Deleted.
+        (WebKit::CoordinatedGraphicsScene::removeBackingStoreIfNeeded): Deleted.
+        (WebKit::CoordinatedGraphicsScene::resetBackingStoreSizeToLayerSize): Deleted.
+        (WebKit::CoordinatedGraphicsScene::createTilesIfNeeded): Deleted.
+        (WebKit::CoordinatedGraphicsScene::removeTilesIfNeeded): Deleted.
+        (WebKit::CoordinatedGraphicsScene::updateTilesIfNeeded): Deleted.
+        (WebKit::CoordinatedGraphicsScene::syncImageBackings): Deleted.
+        (WebKit::CoordinatedGraphicsScene::createImageBacking): Deleted.
+        (WebKit::CoordinatedGraphicsScene::updateImageBacking): Deleted.
+        (WebKit::CoordinatedGraphicsScene::clearImageBackingContents): Deleted.
+        (WebKit::CoordinatedGraphicsScene::removeImageBacking): Deleted.
+        (WebKit::CoordinatedGraphicsScene::assignImageBackingToLayer): Deleted.
+        (WebKit::CoordinatedGraphicsScene::setLayerAnimationsIfNeeded): Deleted.
+        * Shared/CoordinatedGraphics/CoordinatedGraphicsScene.h:
+        (WebKit::CoordinatedGraphicsScene::layerByID): Deleted.
+        * Shared/CoordinatedGraphics/threadedcompositor/ThreadedCompositor.cpp:
+        (WebKit::ThreadedCompositor::renderLayerTree):
+        * WebProcess/WebPage/CoordinatedGraphics/CompositingCoordinator.cpp:
+        (WebKit::CompositingCoordinator::flushPendingLayerChanges):
+
 2018-08-21  Ryosuke Niwa  <rn...@webkit.org>
 
         Replace booleans for modifier keys in UIEventWithKeyState with OptionSet<Modifier>

Modified: trunk/Source/WebKit/Shared/CoordinatedGraphics/CoordinatedGraphicsScene.cpp (235164 => 235165)


--- trunk/Source/WebKit/Shared/CoordinatedGraphics/CoordinatedGraphicsScene.cpp	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/Source/WebKit/Shared/CoordinatedGraphics/CoordinatedGraphicsScene.cpp	2018-08-22 12:14:14 UTC (rev 235165)
@@ -25,7 +25,12 @@
 #if USE(COORDINATED_GRAPHICS)
 
 #include <WebCore/CoordinatedBackingStore.h>
+#include <WebCore/NicosiaBackingStoreTextureMapperImpl.h>
 #include <WebCore/NicosiaBuffer.h>
+#include <WebCore/NicosiaCompositionLayerTextureMapperImpl.h>
+#include <WebCore/NicosiaContentLayerTextureMapperImpl.h>
+#include <WebCore/NicosiaImageBackingTextureMapperImpl.h>
+#include <WebCore/NicosiaScene.h>
 #include <WebCore/TextureMapper.h>
 #include <WebCore/TextureMapperBackingStore.h>
 #include <WebCore/TextureMapperGL.h>
@@ -57,20 +62,17 @@
     ensureRootLayer();
 
     for (auto& state : states)
-        commitSceneState(state);
+        commitSceneState(state.nicosia);
 }
 
 void CoordinatedGraphicsScene::paintToCurrentGLContext(const TransformationMatrix& matrix, float opacity, const FloatRect& clipRect, const Color& backgroundColor, bool drawsBackground, TextureMapper::PaintFlags PaintFlags)
 {
+    updateSceneState();
+
     TextureMapperLayer* currentRootLayer = rootLayer();
     if (!currentRootLayer)
         return;
 
-#if USE(COORDINATED_GRAPHICS_THREADED)
-    for (auto& proxy : m_platformLayerProxies.values())
-        proxy->swapBuffer();
-#endif
-
     currentRootLayer->setTextureMapper(m_textureMapper.get());
     bool sceneHasRunningAnimations = currentRootLayer->applyAnimationsRecursively(MonotonicTime::now());
     m_textureMapper->beginPainting(PaintFlags);
@@ -104,24 +106,7 @@
         m_client->updateViewport();
 }
 
-void CoordinatedGraphicsScene::syncPlatformLayerIfNeeded(TextureMapperLayer* layer, const CoordinatedGraphicsLayerState& state)
-{
 #if USE(COORDINATED_GRAPHICS_THREADED)
-    if (!state.platformLayerChanged)
-        return;
-
-    if (state.platformLayerProxy) {
-        m_platformLayerProxies.set(layer, state.platformLayerProxy);
-        state.platformLayerProxy->activateOnCompositingThread(this, layer);
-    } else
-        m_platformLayerProxies.remove(layer);
-#else
-    UNUSED_PARAM(layer);
-    UNUSED_PARAM(state);
-#endif
-}
-
-#if USE(COORDINATED_GRAPHICS_THREADED)
 void CoordinatedGraphicsScene::onNewBufferAvailable()
 {
     updateViewport();
@@ -128,320 +113,305 @@
 }
 #endif
 
-void CoordinatedGraphicsScene::setLayerChildrenIfNeeded(TextureMapperLayer* layer, const CoordinatedGraphicsLayerState& state)
+Nicosia::CompositionLayerTextureMapperImpl& compositionLayerImpl(Nicosia::CompositionLayer& compositionLayer)
 {
-    if (!state.childrenChanged)
-        return;
-
-    Vector<TextureMapperLayer*> children;
-    children.reserveCapacity(state.children.size());
-    for (auto& child : state.children)
-        children.append(layerByID(child));
-
-    layer->setChildren(children);
+    return downcast<Nicosia::CompositionLayerTextureMapperImpl>(compositionLayer.impl());
 }
 
-void CoordinatedGraphicsScene::setLayerFiltersIfNeeded(TextureMapperLayer* layer, const CoordinatedGraphicsLayerState& state)
+Nicosia::ContentLayerTextureMapperImpl& contentLayerImpl(Nicosia::ContentLayer& contentLayer)
 {
-    if (!state.filtersChanged)
-        return;
-
-    layer->setFilters(state.filters);
+    return downcast<Nicosia::ContentLayerTextureMapperImpl>(contentLayer.impl());
 }
 
-void CoordinatedGraphicsScene::setLayerState(CoordinatedLayerID id, const CoordinatedGraphicsLayerState& layerState, CommitScope& commitScope)
+Nicosia::BackingStoreTextureMapperImpl& backingStoreImpl(Nicosia::BackingStore& backingStore)
 {
-    ASSERT(m_rootLayerID != InvalidCoordinatedLayerID);
-    TextureMapperLayer* layer = layerByID(id);
-
-    if (layerState.positionChanged)
-        layer->setPosition(layerState.pos);
-
-    if (layerState.anchorPointChanged)
-        layer->setAnchorPoint(layerState.anchorPoint);
-
-    if (layerState.sizeChanged)
-        layer->setSize(layerState.size);
-
-    if (layerState.transformChanged)
-        layer->setTransform(layerState.transform);
-
-    if (layerState.childrenTransformChanged)
-        layer->setChildrenTransform(layerState.childrenTransform);
-
-    if (layerState.contentsRectChanged)
-        layer->setContentsRect(layerState.contentsRect);
-
-    if (layerState.contentsTilingChanged) {
-        layer->setContentsTilePhase(layerState.contentsTilePhase);
-        layer->setContentsTileSize(layerState.contentsTileSize);
-    }
-
-    if (layerState.opacityChanged)
-        layer->setOpacity(layerState.opacity);
-
-    if (layerState.solidColorChanged)
-        layer->setSolidColor(layerState.solidColor);
-
-    if (layerState.debugVisualsChanged)
-        layer->setDebugVisuals(layerState.debugVisuals.showDebugBorders, layerState.debugVisuals.debugBorderColor, layerState.debugVisuals.debugBorderWidth);
-    if (layerState.repaintCountChanged)
-        layer->setRepaintCounter(layerState.repaintCount.showRepaintCounter, layerState.repaintCount.count);
-
-    if (layerState.replicaChanged)
-        layer->setReplicaLayer(getLayerByIDIfExists(layerState.replica));
-
-    if (layerState.maskChanged)
-        layer->setMaskLayer(getLayerByIDIfExists(layerState.mask));
-
-    if (layerState.imageChanged)
-        assignImageBackingToLayer(layer, layerState.imageID);
-
-    if (layerState.flagsChanged) {
-        layer->setContentsOpaque(layerState.contentsOpaque);
-        layer->setDrawsContent(layerState.drawsContent);
-        layer->setContentsVisible(layerState.contentsVisible);
-        layer->setBackfaceVisibility(layerState.backfaceVisible);
-
-        // Never clip the root layer.
-        layer->setMasksToBounds(id == m_rootLayerID ? false : layerState.masksToBounds);
-        layer->setPreserves3D(layerState.preserves3D);
-    }
-
-    // Apply Operations.
-    setLayerChildrenIfNeeded(layer, layerState);
-    setLayerFiltersIfNeeded(layer, layerState);
-    setLayerAnimationsIfNeeded(layer, layerState);
-    prepareContentBackingStore(layer, commitScope);
-    createTilesIfNeeded(layer, layerState);
-    removeTilesIfNeeded(layer, layerState, commitScope);
-    updateTilesIfNeeded(layer, layerState, commitScope);
-    syncPlatformLayerIfNeeded(layer, layerState);
+    return downcast<Nicosia::BackingStoreTextureMapperImpl>(backingStore.impl());
 }
 
-TextureMapperLayer* CoordinatedGraphicsScene::getLayerByIDIfExists(CoordinatedLayerID id)
+Nicosia::ImageBackingTextureMapperImpl& imageBackingImpl(Nicosia::ImageBacking& imageBacking)
 {
-    return (id != InvalidCoordinatedLayerID) ? layerByID(id) : 0;
+    return downcast<Nicosia::ImageBackingTextureMapperImpl>(imageBacking.impl());
 }
 
-void CoordinatedGraphicsScene::createLayers(const Vector<CoordinatedLayerID>& layerIDs)
+TextureMapperLayer& texmapLayer(Nicosia::CompositionLayer& compositionLayer)
 {
-    for (auto& layerID : layerIDs)
-        createLayer(layerID);
+    auto& compositionState = compositionLayerImpl(compositionLayer).compositionState();
+    if (!compositionState.layer) {
+        compositionState.layer = std::make_unique<TextureMapperLayer>();
+        compositionState.layer->setID(compositionLayer.id());
+    }
+    return *compositionState.layer;
 }
 
-void CoordinatedGraphicsScene::createLayer(CoordinatedLayerID id)
+void updateBackingStore(TextureMapperLayer& layer,
+    Nicosia::BackingStoreTextureMapperImpl::CompositionState& compositionState,
+    const Nicosia::BackingStoreTextureMapperImpl::TileUpdate& update)
 {
-    std::unique_ptr<TextureMapperLayer> newLayer = std::make_unique<TextureMapperLayer>();
-    newLayer->setID(id);
-    m_layers.add(id, WTFMove(newLayer));
-}
+    if (!layerShouldHaveBackingStore(&layer)) {
+        layer.setBackingStore(nullptr);
+        compositionState.backingStore = nullptr;
+        return;
+    }
 
-void CoordinatedGraphicsScene::deleteLayers(const Vector<CoordinatedLayerID>& layerIDs)
-{
-    for (auto& layerID : layerIDs)
-        deleteLayer(layerID);
-}
+    if (!compositionState.backingStore)
+        compositionState.backingStore = CoordinatedBackingStore::create();
+    auto& backingStore = *compositionState.backingStore;
 
-void CoordinatedGraphicsScene::deleteLayer(CoordinatedLayerID layerID)
-{
-    std::unique_ptr<TextureMapperLayer> layer = m_layers.take(layerID);
-    ASSERT(layer);
+    layer.setBackingStore(&backingStore);
+    backingStore.setSize(layer.size());
 
-    m_backingStores.remove(layer.get());
-#if USE(COORDINATED_GRAPHICS_THREADED)
-    if (auto platformLayerProxy = m_platformLayerProxies.take(layer.get()))
-        platformLayerProxy->invalidate();
-#endif
+    for (auto& tile : update.tilesToCreate)
+        backingStore.createTile(tile.tileID, tile.scale);
+    for (auto& tile : update.tilesToRemove)
+        backingStore.removeTile(tile.tileID);
+    for (auto& tile : update.tilesToUpdate) {
+        backingStore.updateTile(tile.tileID, tile.updateInfo.updateRect,
+            tile.tileRect, tile.updateInfo.buffer.copyRef(), { 0, 0 });
+    }
 }
 
-void CoordinatedGraphicsScene::setRootLayerID(CoordinatedLayerID layerID)
+void updateImageBacking(TextureMapperLayer& layer,
+    Nicosia::ImageBackingTextureMapperImpl::CompositionState& compositionState,
+    Nicosia::ImageBackingTextureMapperImpl::Update& update)
 {
-    ASSERT(layerID != InvalidCoordinatedLayerID);
-    ASSERT(m_rootLayerID == InvalidCoordinatedLayerID);
-
-    m_rootLayerID = layerID;
-
-    TextureMapperLayer* layer = layerByID(layerID);
-    ASSERT(m_rootLayer->children().isEmpty());
-    m_rootLayer->addChild(layer);
-}
-
-void CoordinatedGraphicsScene::prepareContentBackingStore(TextureMapperLayer* layer, CommitScope& commitScope)
-{
-    if (!layerShouldHaveBackingStore(layer)) {
-        removeBackingStoreIfNeeded(layer);
+    if (!update.isVisible) {
+        layer.setBackingStore(nullptr);
         return;
     }
 
-    createBackingStoreIfNeeded(layer);
-    resetBackingStoreSizeToLayerSize(layer, commitScope);
-}
+    if (!compositionState.backingStore)
+        compositionState.backingStore = CoordinatedBackingStore::create();
+    auto& backingStore = *compositionState.backingStore;
+    layer.setContentsLayer(&backingStore);
 
-void CoordinatedGraphicsScene::createBackingStoreIfNeeded(TextureMapperLayer* layer)
-{
-    if (m_backingStores.contains(layer))
+    if (!update.buffer)
         return;
 
-    auto backingStore = CoordinatedBackingStore::create();
-    m_backingStores.add(layer, backingStore.copyRef());
-    layer->setBackingStore(backingStore.ptr());
+    backingStore.createTile(1, 1.0);
+    WebCore::IntRect rect { { }, update.buffer->size() };
+    ASSERT(2000 >= std::max(rect.width(), rect.height()));
+    backingStore.setSize(rect.size());
+    backingStore.updateTile(1, rect, rect, WTFMove(update.buffer), rect.location());
 }
 
-void CoordinatedGraphicsScene::removeBackingStoreIfNeeded(TextureMapperLayer* layer)
+void removeLayer(Nicosia::CompositionLayer& layer)
 {
-    RefPtr<CoordinatedBackingStore> backingStore = m_backingStores.take(layer);
-    if (!backingStore)
-        return;
+    layer.accessCommitted(
+        [](const Nicosia::CompositionLayer::LayerState& committed)
+        {
+            if (committed.backingStore) {
+                auto& compositionState = backingStoreImpl(*committed.backingStore).compositionState();
+                compositionState.backingStore = nullptr;
+            }
 
-    layer->setBackingStore(nullptr);
-}
+            if (committed.contentLayer)
+                contentLayerImpl(*committed.contentLayer).proxy().invalidate();
 
-void CoordinatedGraphicsScene::resetBackingStoreSizeToLayerSize(TextureMapperLayer* layer, CommitScope& commitScope)
-{
-    RefPtr<CoordinatedBackingStore> backingStore = m_backingStores.get(layer);
-    ASSERT(backingStore);
-    backingStore->setSize(layer->size());
-    commitScope.backingStoresWithPendingBuffers.add(backingStore);
+            if (committed.imageBacking) {
+                auto& compositionState = imageBackingImpl(*committed.imageBacking).compositionState();
+                compositionState.backingStore = nullptr;
+            }
+        });
+
+    auto& compositionState = compositionLayerImpl(layer).compositionState();
+    compositionState.layer = nullptr;
 }
 
-void CoordinatedGraphicsScene::createTilesIfNeeded(TextureMapperLayer* layer, const CoordinatedGraphicsLayerState& state)
+void CoordinatedGraphicsScene::commitSceneState(const CoordinatedGraphicsState::NicosiaState& state)
 {
-    if (state.tilesToCreate.isEmpty())
+    if (!m_client)
         return;
 
-    RefPtr<CoordinatedBackingStore> backingStore = m_backingStores.get(layer);
-    ASSERT(backingStore || !layerShouldHaveBackingStore(layer));
-    if (!backingStore)
-        return;
-
-    for (auto& tile : state.tilesToCreate)
-        backingStore->createTile(tile.tileID, tile.scale);
+    m_nicosia.scene = state.scene;
 }
 
-void CoordinatedGraphicsScene::removeTilesIfNeeded(TextureMapperLayer* layer, const CoordinatedGraphicsLayerState& state, CommitScope& commitScope)
+void CoordinatedGraphicsScene::updateSceneState()
 {
-    if (state.tilesToRemove.isEmpty())
+    if (!m_nicosia.scene)
         return;
 
-    RefPtr<CoordinatedBackingStore> backingStore = m_backingStores.get(layer);
-    if (!backingStore)
-        return;
+    // Store layer and impl references along with the corresponding update
+    // for each type of possible layer backing.
+    struct {
+        struct BackingStore {
+            std::reference_wrapper<TextureMapperLayer> layer;
+            std::reference_wrapper<Nicosia::BackingStoreTextureMapperImpl> backingStore;
+            Nicosia::BackingStoreTextureMapperImpl::TileUpdate update;
+        };
+        Vector<BackingStore> backingStore;
 
-    for (auto& tile : state.tilesToRemove)
-        backingStore->removeTile(tile);
+        struct ContentLayer {
+            std::reference_wrapper<TextureMapperLayer> layer;
+            std::reference_wrapper<TextureMapperPlatformLayerProxy> proxy;
+            bool needsActivation { false };
+        };
+        Vector<ContentLayer> contentLayer;
 
-    commitScope.backingStoresWithPendingBuffers.add(backingStore);
-}
+        struct ImageBacking {
+            std::reference_wrapper<TextureMapperLayer> layer;
+            std::reference_wrapper<Nicosia::ImageBackingTextureMapperImpl> imageBacking;
+            Nicosia::ImageBackingTextureMapperImpl::Update update;
+        };
+        Vector<ImageBacking> imageBacking;
+    } layersByBacking;
 
-void CoordinatedGraphicsScene::updateTilesIfNeeded(TextureMapperLayer* layer, const CoordinatedGraphicsLayerState& state, CommitScope& commitScope)
-{
-    if (state.tilesToUpdate.isEmpty())
-        return;
+    // Access the scene state and perform state update for each layer.
+    m_nicosia.scene->accessState(
+        [this, &layersByBacking](Nicosia::Scene::State& state)
+        {
+            // Bail if the scene didn't change.
+            if (state.id == m_nicosia.state.id)
+                return;
 
-    RefPtr<CoordinatedBackingStore> backingStore = m_backingStores.get(layer);
-    ASSERT(backingStore || !layerShouldHaveBackingStore(layer));
-    if (!backingStore)
-        return;
+            // Handle the root layer, adding it to the TextureMapperLayer tree
+            // on the first update. No such change is expected later.
+            {
+                auto& rootLayer = texmapLayer(*state.rootLayer);
+                if (rootLayer.id() != m_rootLayerID) {
+                    m_rootLayerID = rootLayer.id();
+                    RELEASE_ASSERT(m_rootLayer->children().isEmpty());
+                    m_rootLayer->addChild(&rootLayer);
+                }
+            }
 
-    for (auto& tile : state.tilesToUpdate) {
-        const SurfaceUpdateInfo& surfaceUpdateInfo = tile.updateInfo;
+            // Gather all the to-be-removed layers so that composition-side state
+            // can be properly purged after the current state's set of layers is adopted.
+            HashSet<RefPtr<Nicosia::CompositionLayer>> removedLayers;
+            for (auto& layer : m_nicosia.state.layers) {
+                if (!state.layers.contains(layer))
+                    removedLayers.add(layer);
+            }
 
-        backingStore->updateTile(tile.tileID, surfaceUpdateInfo.updateRect, tile.tileRect, surfaceUpdateInfo.buffer.copyRef(), { 0, 0 });
-        commitScope.backingStoresWithPendingBuffers.add(backingStore);
-    }
-}
+            m_nicosia.state = state;
 
-void CoordinatedGraphicsScene::syncImageBackings(const CoordinatedGraphicsState& state, CommitScope& commitScope)
-{
-    for (auto& image : state.imagesToRemove)
-        removeImageBacking(image, commitScope);
+            for (auto& layer : removedLayers)
+                removeLayer(*layer);
+            removedLayers = { };
 
-    for (auto& image : state.imagesToCreate)
-        createImageBacking(image);
+            // Iterate the current state's set of layers, updating state values according to
+            // the incoming state changes. Layer backings are stored so that the updates
+            // (possibly time-consuming) can be done outside of this scene update.
+            for (auto& compositionLayer : m_nicosia.state.layers) {
+                auto& layer = texmapLayer(*compositionLayer);
+                compositionLayer->commitState(
+                    [this, &layer, &compositionLayer, &layersByBacking]
+                    (const Nicosia::CompositionLayer::LayerState& layerState)
+                    {
+                        if (layerState.delta.positionChanged)
+                            layer.setPosition(layerState.position);
+                        if (layerState.delta.anchorPointChanged)
+                            layer.setAnchorPoint(layerState.anchorPoint);
+                        if (layerState.delta.sizeChanged)
+                            layer.setSize(layerState.size);
 
-    for (auto& image : state.imagesToUpdate)
-        updateImageBacking(image.first, image.second.copyRef(), commitScope);
+                        if (layerState.delta.transformChanged)
+                            layer.setTransform(layerState.transform);
+                        if (layerState.delta.childrenTransformChanged)
+                            layer.setChildrenTransform(layerState.childrenTransform);
 
-    for (auto& image : state.imagesToClear)
-        clearImageBackingContents(image, commitScope);
-}
+                        if (layerState.delta.contentsRectChanged)
+                            layer.setContentsRect(layerState.contentsRect);
+                        if (layerState.delta.contentsTilingChanged) {
+                            layer.setContentsTilePhase(layerState.contentsTilePhase);
+                            layer.setContentsTileSize(layerState.contentsTileSize);
+                        }
 
-void CoordinatedGraphicsScene::createImageBacking(CoordinatedImageBackingID imageID)
-{
-    ASSERT(!m_imageBackings.contains(imageID));
-    m_imageBackings.add(imageID, CoordinatedBackingStore::create());
-}
+                        if (layerState.delta.opacityChanged)
+                            layer.setOpacity(layerState.opacity);
+                        if (layerState.delta.solidColorChanged)
+                            layer.setSolidColor(layerState.solidColor);
 
-void CoordinatedGraphicsScene::updateImageBacking(CoordinatedImageBackingID imageID, RefPtr<Nicosia::Buffer>&& buffer, CommitScope& commitScope)
-{
-    ASSERT(m_imageBackings.contains(imageID));
-    auto it = m_imageBackings.find(imageID);
-    RefPtr<CoordinatedBackingStore> backingStore = it->value;
+                        if (layerState.delta.filtersChanged)
+                            layer.setFilters(layerState.filters);
+                        if (layerState.delta.animationsChanged)
+                            layer.setAnimations(layerState.animations);
 
-    // CoordinatedImageBacking is realized to CoordinatedBackingStore with only one tile in UI Process.
-    backingStore->createTile(1 /* id */, 1 /* scale */);
-    IntRect rect(IntPoint::zero(), buffer->size());
-    // See CoordinatedGraphicsLayer::shouldDirectlyCompositeImage()
-    ASSERT(2000 >= std::max(rect.width(), rect.height()));
-    backingStore->setSize(rect.size());
-    backingStore->updateTile(1 /* id */, rect, rect, WTFMove(buffer), rect.location());
+                        if (layerState.delta.childrenChanged) {
+                            layer.setChildren(WTF::map(layerState.children,
+                                [](auto& child) { return &texmapLayer(*child); }));
+                        }
 
-    commitScope.backingStoresWithPendingBuffers.add(backingStore);
-}
+                        if (layerState.delta.maskChanged)
+                            layer.setMaskLayer(layerState.mask ? &texmapLayer(*layerState.mask) : nullptr);
+                        if (layerState.delta.replicaChanged)
+                            layer.setReplicaLayer(layerState.replica ? &texmapLayer(*layerState.replica) : nullptr);
 
-void CoordinatedGraphicsScene::clearImageBackingContents(CoordinatedImageBackingID imageID, CommitScope& commitScope)
-{
-    ASSERT(m_imageBackings.contains(imageID));
-    auto it = m_imageBackings.find(imageID);
-    RefPtr<CoordinatedBackingStore> backingStore = it->value;
-    backingStore->removeAllTiles();
-    commitScope.backingStoresWithPendingBuffers.add(backingStore);
-}
+                        if (layerState.delta.flagsChanged) {
+                            layer.setContentsOpaque(layerState.flags.contentsOpaque);
+                            layer.setDrawsContent(layerState.flags.drawsContent);
+                            layer.setContentsVisible(layerState.flags.contentsVisible);
+                            layer.setBackfaceVisibility(layerState.flags.backfaceVisible);
+                            layer.setMasksToBounds(layerState.flags.masksToBounds);
+                            layer.setPreserves3D(layerState.flags.preserves3D);
+                        }
 
-void CoordinatedGraphicsScene::removeImageBacking(CoordinatedImageBackingID imageID, CommitScope& commitScope)
-{
-    ASSERT(m_imageBackings.contains(imageID));
+                        if (layerState.backingStore) {
+                            auto& impl = backingStoreImpl(*layerState.backingStore);
+                            layersByBacking.backingStore.append(
+                                { std::ref(layer), std::ref(impl), impl.takeUpdate() });
+                        } else
+                            layer.setBackingStore(nullptr);
 
-    // We don't want TextureMapperLayer refers a dangling pointer.
-    commitScope.releasedImageBackings.append(m_imageBackings.take(imageID));
-}
+                        if (layerState.contentLayer) {
+                            auto& impl = contentLayerImpl(*layerState.contentLayer);
+                            layersByBacking.contentLayer.append(
+                                { std::ref(layer), std::ref(impl.proxy()), layerState.delta.contentLayerChanged });
+                        } else if (layerState.imageBacking) {
+                            auto& impl = imageBackingImpl(*layerState.imageBacking);
+                            layersByBacking.imageBacking.append(
+                                { std::ref(layer), std::ref(impl), impl.takeUpdate() });
+                        } else
+                            layer.setContentsLayer(nullptr);
+                    });
+            }
+        });
 
-void CoordinatedGraphicsScene::assignImageBackingToLayer(TextureMapperLayer* layer, CoordinatedImageBackingID imageID)
-{
-    if (imageID == InvalidCoordinatedImageBackingID) {
-        layer->setContentsLayer(0);
-        return;
-    }
+    // Iterate through each backing type of layers and gather backing store
+    // or proxy objects that need an update.
+    // FIXME: HashSet<std::reference_wrapper<>> would be ideal, but doesn't work (yet).
+    HashSet<Ref<WebCore::CoordinatedBackingStore>> backingStoresWithPendingBuffers;
+    HashSet<Ref<WebCore::TextureMapperPlatformLayerProxy>> proxiesForSwapping;
 
-    auto it = m_imageBackings.find(imageID);
-    ASSERT(it != m_imageBackings.end());
-    layer->setContentsLayer(it->value.get());
-}
+    {
+        for (auto& entry : layersByBacking.backingStore) {
+            auto& compositionState = entry.backingStore.get().compositionState();
+            updateBackingStore(entry.layer.get(), compositionState, entry.update);
 
-void CoordinatedGraphicsScene::commitSceneState(const CoordinatedGraphicsState& state)
-{
-    if (!m_client)
-        return;
+            if (compositionState.backingStore)
+                backingStoresWithPendingBuffers.add(makeRef(*compositionState.backingStore));
+        }
 
-    m_nicosia = state.nicosia;
-    // FIXME: Start using the Nicosia layer state for updates.
+        layersByBacking.backingStore = { };
+    }
 
-    CommitScope commitScope;
+    {
+        for (auto& entry : layersByBacking.contentLayer) {
+            auto& proxy = entry.proxy.get();
+            if (entry.needsActivation)
+                proxy.activateOnCompositingThread(this, &entry.layer.get());
+            proxiesForSwapping.add(makeRef(proxy));
+        }
 
-    createLayers(state.layersToCreate);
-    deleteLayers(state.layersToRemove);
+        layersByBacking.contentLayer = { };
+    }
 
-    if (state.rootCompositingLayer != m_rootLayerID)
-        setRootLayerID(state.rootCompositingLayer);
+    {
+        for (auto& entry : layersByBacking.imageBacking) {
+            auto& compositionState = entry.imageBacking.get().compositionState();
+            updateImageBacking(entry.layer.get(), compositionState, entry.update);
 
-    syncImageBackings(state, commitScope);
+            if (compositionState.backingStore)
+                backingStoresWithPendingBuffers.add(makeRef(*compositionState.backingStore));
+        }
 
-    for (auto& layer : state.layersToUpdate)
-        setLayerState(layer.first, layer.second, commitScope);
+        layersByBacking.imageBacking = { };
+    }
 
-    for (auto& backingStore : commitScope.backingStoresWithPendingBuffers)
+    for (auto& backingStore : backingStoresWithPendingBuffers)
         backingStore->commitTileOperations(*m_textureMapper);
+
+    for (auto& proxy : proxiesForSwapping)
+        proxy->swapBuffer();
 }
 
 void CoordinatedGraphicsScene::ensureRootLayer()
@@ -465,28 +435,24 @@
 {
     ASSERT(!m_client);
 
-    m_imageBackings.clear();
-#if USE(COORDINATED_GRAPHICS_THREADED)
-    for (auto& proxy : m_platformLayerProxies.values())
-        proxy->invalidate();
-    m_platformLayerProxies.clear();
-#endif
+    if (m_nicosia.scene) {
+        m_nicosia.scene->accessState(
+            [this](const Nicosia::Scene::State& state)
+            {
+                ASSERT(state.id == m_nicosia.state.id);
+                for (auto& layer : m_nicosia.state.layers)
+                    removeLayer(*layer);
+                m_nicosia.state.layers = { };
+                m_nicosia.state.rootLayer = nullptr;
+            });
+        m_nicosia.scene = nullptr;
+    }
 
     m_rootLayer = nullptr;
     m_rootLayerID = InvalidCoordinatedLayerID;
-    m_layers.clear();
     m_textureMapper = nullptr;
-    m_backingStores.clear();
 }
 
-void CoordinatedGraphicsScene::setLayerAnimationsIfNeeded(TextureMapperLayer* layer, const CoordinatedGraphicsLayerState& state)
-{
-    if (!state.animationsChanged)
-        return;
-
-    layer->setAnimations(state.animations);
-}
-
 void CoordinatedGraphicsScene::detach()
 {
     ASSERT(RunLoop::isMain());

Modified: trunk/Source/WebKit/Shared/CoordinatedGraphics/CoordinatedGraphicsScene.h (235164 => 235165)


--- trunk/Source/WebKit/Shared/CoordinatedGraphics/CoordinatedGraphicsScene.h	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/Source/WebKit/Shared/CoordinatedGraphics/CoordinatedGraphicsScene.h	2018-08-22 12:14:14 UTC (rev 235165)
@@ -81,76 +81,30 @@
     bool isActive() const { return m_isActive; }
     void setActive(bool active) { m_isActive = active; }
 
-    void commitSceneState(const WebCore::CoordinatedGraphicsState&);
-
     void setViewBackgroundColor(const WebCore::Color& color) { m_viewBackgroundColor = color; }
     WebCore::Color viewBackgroundColor() const { return m_viewBackgroundColor; }
 
 private:
-    struct CommitScope {
-        CommitScope() = default;
-        CommitScope(CommitScope&) = delete;
-        CommitScope& operator=(const CommitScope&) = delete;
+    void commitSceneState(const WebCore::CoordinatedGraphicsState::NicosiaState&);
+    void updateSceneState();
 
-        Vector<RefPtr<WebCore::CoordinatedBackingStore>> releasedImageBackings;
-        HashSet<RefPtr<WebCore::CoordinatedBackingStore>> backingStoresWithPendingBuffers;
-    };
-
-    void setRootLayerID(WebCore::CoordinatedLayerID);
-    void createLayers(const Vector<WebCore::CoordinatedLayerID>&);
-    void deleteLayers(const Vector<WebCore::CoordinatedLayerID>&);
-    void setLayerState(WebCore::CoordinatedLayerID, const WebCore::CoordinatedGraphicsLayerState&, CommitScope&);
-    void setLayerChildrenIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
-    void updateTilesIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&, CommitScope&);
-    void createTilesIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
-    void removeTilesIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&, CommitScope&);
-    void setLayerFiltersIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
-    void setLayerAnimationsIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
-    void syncPlatformLayerIfNeeded(WebCore::TextureMapperLayer*, const WebCore::CoordinatedGraphicsLayerState&);
-
-    void syncImageBackings(const WebCore::CoordinatedGraphicsState&, CommitScope&);
-    void createImageBacking(WebCore::CoordinatedImageBackingID);
-    void updateImageBacking(WebCore::CoordinatedImageBackingID, RefPtr<Nicosia::Buffer>&&, CommitScope&);
-    void clearImageBackingContents(WebCore::CoordinatedImageBackingID, CommitScope&);
-    void removeImageBacking(WebCore::CoordinatedImageBackingID, CommitScope&);
-
-    WebCore::TextureMapperLayer* layerByID(WebCore::CoordinatedLayerID id)
-    {
-        ASSERT(m_layers.contains(id));
-        ASSERT(id != WebCore::InvalidCoordinatedLayerID);
-        return m_layers.get(id);
-    }
-    WebCore::TextureMapperLayer* getLayerByIDIfExists(WebCore::CoordinatedLayerID);
     WebCore::TextureMapperLayer* rootLayer() { return m_rootLayer.get(); }
 
     void updateViewport();
 
-    void createLayer(WebCore::CoordinatedLayerID);
-    void deleteLayer(WebCore::CoordinatedLayerID);
-
-    void assignImageBackingToLayer(WebCore::TextureMapperLayer*, WebCore::CoordinatedImageBackingID);
     void ensureRootLayer();
 
-    void prepareContentBackingStore(WebCore::TextureMapperLayer*, CommitScope&);
-    void createBackingStoreIfNeeded(WebCore::TextureMapperLayer*);
-    void removeBackingStoreIfNeeded(WebCore::TextureMapperLayer*);
-    void resetBackingStoreSizeToLayerSize(WebCore::TextureMapperLayer*, CommitScope&);
-
 #if USE(COORDINATED_GRAPHICS_THREADED)
     void onNewBufferAvailable() override;
 #endif
 
-    WebCore::CoordinatedGraphicsState::NicosiaState m_nicosia;
+    struct {
+        RefPtr<Nicosia::Scene> scene;
+        Nicosia::Scene::State state;
+    } m_nicosia;
 
     std::unique_ptr<WebCore::TextureMapper> m_textureMapper;
 
-    HashMap<WebCore::CoordinatedImageBackingID, RefPtr<WebCore::CoordinatedBackingStore>> m_imageBackings;
-    HashMap<WebCore::TextureMapperLayer*, RefPtr<WebCore::CoordinatedBackingStore>> m_backingStores;
-
-#if USE(COORDINATED_GRAPHICS_THREADED)
-    HashMap<WebCore::TextureMapperLayer*, RefPtr<WebCore::TextureMapperPlatformLayerProxy>> m_platformLayerProxies;
-#endif
-
     // Below two members are accessed by only the main thread. The painting thread must lock the main thread to access both members.
     CoordinatedGraphicsSceneClient* m_client;
     bool m_isActive { false };
@@ -157,7 +111,6 @@
 
     std::unique_ptr<WebCore::TextureMapperLayer> m_rootLayer;
 
-    HashMap<WebCore::CoordinatedLayerID, std::unique_ptr<WebCore::TextureMapperLayer>> m_layers;
     WebCore::CoordinatedLayerID m_rootLayerID { WebCore::InvalidCoordinatedLayerID };
     WebCore::Color m_viewBackgroundColor { WebCore::Color::white };
 

Modified: trunk/Source/WebKit/Shared/CoordinatedGraphics/threadedcompositor/ThreadedCompositor.cpp (235164 => 235165)


--- trunk/Source/WebKit/Shared/CoordinatedGraphics/threadedcompositor/ThreadedCompositor.cpp	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/Source/WebKit/Shared/CoordinatedGraphics/threadedcompositor/ThreadedCompositor.cpp	2018-08-22 12:14:14 UTC (rev 235165)
@@ -215,11 +215,21 @@
             // Coordinate scene update completion with the client in case of changed or updated platform layers.
             // But do not change coordinateUpdateCompletionWithClient while in force repaint because that
             // demands immediate scene update completion regardless of platform layers.
+            // FIXME: Check whether we still need all this coordinateUpdateCompletionWithClient logic.
+            // https://bugs.webkit.org/show_bug.cgi?id=188839
+            // Relatedly, we should only ever operate with a single Nicosia::Scene object, not with a Vector
+            // of CoordinatedGraphicsState instances (which at this time will all contain RefPtr to the same
+            // Nicosia::State object anyway).
             if (!m_inForceRepaint) {
                 bool coordinateUpdate = false;
-                for (auto& state : states)
-                    coordinateUpdate |= std::any_of(state.layersToUpdate.begin(), state.layersToUpdate.end(),
-                        [](auto& it) { return it.second.platformLayerChanged || it.second.platformLayerUpdated; });
+                for (auto& state : states) {
+                    state.nicosia.scene->accessState(
+                        [&coordinateUpdate](Nicosia::Scene::State& state)
+                        {
+                            coordinateUpdate |= state.platformLayerUpdated;
+                            state.platformLayerUpdated = false;
+                        });
+                }
                 m_attributes.coordinateUpdateCompletionWithClient = coordinateUpdate;
             }
         }

Modified: trunk/Source/WebKit/WebProcess/WebPage/CoordinatedGraphics/CompositingCoordinator.cpp (235164 => 235165)


--- trunk/Source/WebKit/WebProcess/WebPage/CoordinatedGraphics/CompositingCoordinator.cpp	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/Source/WebKit/WebProcess/WebPage/CoordinatedGraphics/CompositingCoordinator.cpp	2018-08-22 12:14:14 UTC (rev 235165)
@@ -35,6 +35,9 @@
 #include <WebCore/FrameView.h>
 #include <WebCore/GraphicsContext.h>
 #include <WebCore/InspectorController.h>
+#include <WebCore/NicosiaBackingStoreTextureMapperImpl.h>
+#include <WebCore/NicosiaContentLayerTextureMapperImpl.h>
+#include <WebCore/NicosiaImageBackingTextureMapperImpl.h>
 #include <WebCore/NicosiaPaintingEngine.h>
 #include <WebCore/Page.h>
 #include <wtf/MemoryPressureHandler.h>
@@ -130,7 +133,31 @@
         m_state.nicosia.scene->accessState(
             [this](Nicosia::Scene::State& state)
             {
+                bool platformLayerUpdated = false;
+                for (auto& compositionLayer : m_nicosia.state.layers) {
+                    compositionLayer->flushState(
+                        [&platformLayerUpdated]
+                        (const Nicosia::CompositionLayer::LayerState& state)
+                        {
+                            if (state.backingStore) {
+                                auto& impl = downcast<Nicosia::BackingStoreTextureMapperImpl>(state.backingStore->impl());
+                                impl.flushUpdate();
+                            }
+
+                            if (state.imageBacking) {
+                                auto& impl = downcast<Nicosia::ImageBackingTextureMapperImpl>(state.imageBacking->impl());
+                                impl.flushUpdate();
+                            }
+
+                            if (state.contentLayer) {
+                                auto& impl = downcast<Nicosia::ContentLayerTextureMapperImpl>(state.contentLayer->impl());
+                                platformLayerUpdated |= impl.flushUpdate();
+                            }
+                        });
+                }
+
                 ++state.id;
+                state.platformLayerUpdated = platformLayerUpdated;
                 state.layers = m_nicosia.state.layers;
                 state.rootLayer = m_nicosia.state.rootLayer;
             });

Modified: trunk/Source/cmake/OptionsGTK.cmake (235164 => 235165)


--- trunk/Source/cmake/OptionsGTK.cmake	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/Source/cmake/OptionsGTK.cmake	2018-08-22 12:14:14 UTC (rev 235165)
@@ -277,6 +277,7 @@
 
     SET_AND_EXPOSE_TO_BUILD(USE_COORDINATED_GRAPHICS TRUE)
     SET_AND_EXPOSE_TO_BUILD(USE_COORDINATED_GRAPHICS_THREADED TRUE)
+    SET_AND_EXPOSE_TO_BUILD(USE_NICOSIA TRUE)
 endif ()
 
 if (ENABLE_PLUGIN_PROCESS_GTK2)

Modified: trunk/Source/cmake/OptionsWPE.cmake (235164 => 235165)


--- trunk/Source/cmake/OptionsWPE.cmake	2018-08-22 10:37:12 UTC (rev 235164)
+++ trunk/Source/cmake/OptionsWPE.cmake	2018-08-22 12:14:14 UTC (rev 235165)
@@ -134,6 +134,7 @@
 SET_AND_EXPOSE_TO_BUILD(USE_TILED_BACKING_STORE TRUE)
 SET_AND_EXPOSE_TO_BUILD(USE_COORDINATED_GRAPHICS TRUE)
 SET_AND_EXPOSE_TO_BUILD(USE_COORDINATED_GRAPHICS_THREADED TRUE)
+SET_AND_EXPOSE_TO_BUILD(USE_NICOSIA TRUE)
 
 set(FORWARDING_HEADERS_DIR ${DERIVED_SOURCES_DIR}/ForwardingHeaders)
 set(FORWARDING_HEADERS_WPE_DIR ${FORWARDING_HEADERS_DIR}/wpe)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to