Unreviewed, rolling out r105366.
[WebKit-https.git] / Source / WebCore / platform / graphics / chromium / TiledLayerChromium.cpp
index 07bab20..713b4e0 100644 (file)
@@ -33,6 +33,7 @@
 #include "LayerRendererChromium.h"
 #include "ManagedTexture.h"
 #include "MathExtras.h"
+#include "Region.h"
 #include "TextStream.h"
 #include "cc/CCLayerImpl.h"
 #include "cc/CCTextureUpdater.h"
@@ -57,12 +58,14 @@ public:
     LayerTextureUpdater::Texture* texture() { return m_texture.get(); }
     ManagedTexture* managedTexture() { return m_texture->texture(); }
 
-    bool isDirty() const { return !m_dirtyLayerRect.isEmpty(); }
-    void clearDirty() { m_dirtyLayerRect = IntRect(); }
+    bool isDirty() const { return !m_dirtyRect.isEmpty(); }
+    void copyAndClearDirty()
+    {
+        m_updateRect = m_dirtyRect;
+        m_dirtyRect = IntRect();
+    }
 
-    // Layer-space dirty rectangle that needs to be repainted.
-    IntRect m_dirtyLayerRect;
-    // Content-space rectangle that needs to be updated.
+    IntRect m_dirtyRect;
     IntRect m_updateRect;
 private:
     OwnPtr<LayerTextureUpdater::Texture> m_texture;
@@ -72,9 +75,11 @@ TiledLayerChromium::TiledLayerChromium(CCLayerDelegate* delegate)
     : LayerChromium(delegate)
     , m_textureFormat(GraphicsContext3D::INVALID_ENUM)
     , m_skipsDraw(false)
+    , m_skipsIdlePaint(false)
     , m_sampledTexelFormat(LayerTextureUpdater::SampledTexelFormatInvalid)
     , m_tilingOption(AutoTile)
 {
+    m_tiler = CCLayerTilingData::create(IntSize(defaultTileSize, defaultTileSize), CCLayerTilingData::HasBorderTexels);
 }
 
 TiledLayerChromium::~TiledLayerChromium()
@@ -90,9 +95,9 @@ void TiledLayerChromium::cleanupResources()
 {
     LayerChromium::cleanupResources();
 
-    m_tiler.clear();
+    m_tiler->reset();
     m_paintRect = IntRect();
-    m_requestedUpdateRect = IntRect();
+    m_requestedUpdateTilesRect = IntRect();
 }
 
 void TiledLayerChromium::updateTileSizeAndTilingOption()
@@ -120,22 +125,38 @@ void TiledLayerChromium::updateTileSizeAndTilingOption()
     setTileSize(clampedSize);
 }
 
-void TiledLayerChromium::setTileSize(const IntSize& size)
+void TiledLayerChromium::updateBounds()
 {
-    if (m_tiler && m_tileSize == size)
+    IntSize oldBounds = m_tiler->bounds();
+    IntSize newBounds = contentBounds();
+    if (oldBounds == newBounds)
         return;
-    m_tileSize = size;
-    m_tiler.clear(); // Changing the tile size invalidates all tiles, so we just throw away the tiling data.
+    m_tiler->setBounds(newBounds);
+
+    // Invalidate any areas that the new bounds exposes.
+    Region oldRegion(IntRect(IntPoint(), oldBounds));
+    Region newRegion(IntRect(IntPoint(), newBounds));
+    newRegion.subtract(oldRegion);
+    Vector<IntRect> rects = newRegion.rects();
+    for (size_t i = 0; i < rects.size(); ++i)
+        invalidateRect(rects[i]);
+}
+
+void TiledLayerChromium::setTileSize(const IntSize& size)
+{
+    m_tiler->setTileSize(size);
+}
+
+void TiledLayerChromium::setBorderTexelOption(CCLayerTilingData::BorderTexelOption borderTexelOption)
+{
+    m_tiler->setBorderTexelOption(borderTexelOption);
 }
 
 bool TiledLayerChromium::drawsContent() const
 {
-    if (!m_delegate)
+    if (!LayerChromium::drawsContent() || !m_delegate)
         return false;
 
-    if (!m_tiler)
-        return true;
-
     if (m_tilingOption == NeverTile && m_tiler->numTiles() > 1)
         return false;
 
@@ -154,34 +175,32 @@ IntSize TiledLayerChromium::contentBounds() const
 
 void TiledLayerChromium::setLayerTreeHost(CCLayerTreeHost* host)
 {
+    if (host == layerTreeHost())
+        return;
+
+    if (layerTreeHost())
+        cleanupResources();
+
     LayerChromium::setLayerTreeHost(host);
 
-    if (m_tiler || !host)
+    if (!host)
         return;
 
     createTextureUpdater(host);
-
     setTextureFormat(host->layerRendererCapabilities().bestTextureFormat);
     m_sampledTexelFormat = textureUpdater()->sampledTexelFormat(m_textureFormat);
 }
 
-void TiledLayerChromium::createTiler(CCLayerTilingData::BorderTexelOption borderTexelOption)
-{
-    m_tiler = CCLayerTilingData::create(m_tileSize, borderTexelOption);
-}
-
 void TiledLayerChromium::updateCompositorResources(GraphicsContext3D*, CCTextureUpdater& updater)
 {
-    // If this assert is hit, it means that paintContentsIfDirty hasn't been
-    // called on this layer. Any layer that is updated should be painted first.
-    ASSERT(m_skipsDraw || m_tiler);
-
     // Painting could cause compositing to get turned off, which may cause the tiler to become invalidated mid-update.
-    if (m_skipsDraw || m_requestedUpdateRect.isEmpty() || !m_tiler || !m_tiler->numTiles())
+    if (m_skipsDraw || m_requestedUpdateTilesRect.isEmpty() || m_tiler->isEmpty())
         return;
 
-    int left, top, right, bottom;
-    m_tiler->contentRectToTileIndices(m_requestedUpdateRect, left, top, right, bottom);
+    int left = m_requestedUpdateTilesRect.x();
+    int top = m_requestedUpdateTilesRect.y();
+    int right = m_requestedUpdateTilesRect.maxX() - 1;
+    int bottom = m_requestedUpdateTilesRect.maxY() - 1;
     for (int j = top; j <= bottom; ++j) {
         for (int i = left; i <= right; ++i) {
             UpdatableTile* tile = tileAt(i, j);
@@ -197,7 +216,7 @@ void TiledLayerChromium::updateCompositorResources(GraphicsContext3D*, CCTexture
                 continue;
 
             ASSERT(tile->managedTexture()->isReserved());
-            const IntPoint anchor = m_tiler->tileContentRect(tile).location();
+            const IntPoint anchor = m_tiler->tileRect(tile).location();
 
             // Calculate tile-space rectangle to upload into.
             IntRect destRect(IntPoint(sourceRect.x() - anchor.x(), sourceRect.y() - anchor.y()), sourceRect.size());
@@ -221,15 +240,12 @@ void TiledLayerChromium::updateCompositorResources(GraphicsContext3D*, CCTexture
         }
     }
 
-    m_updateRect = FloatRect(m_tiler->contentRectToLayerRect(m_paintRect));
+    m_updateRect = FloatRect(m_paintRect);
 }
 
 void TiledLayerChromium::setTilingOption(TilingOption tilingOption)
 {
-    if (m_tiler && m_tilingOption == tilingOption)
-        return;
     m_tilingOption = tilingOption;
-    m_tiler.clear(); // Changing the tiling option invalidates all tiles, so we just throw away the tiling data.
 }
 
 void TiledLayerChromium::setIsMask(bool isMask)
@@ -242,10 +258,6 @@ void TiledLayerChromium::pushPropertiesTo(CCLayerImpl* layer)
     LayerChromium::pushPropertiesTo(layer);
 
     CCTiledLayerImpl* tiledLayer = static_cast<CCTiledLayerImpl*>(layer);
-    if (!m_tiler) {
-        tiledLayer->setSkipsDraw(true);
-        return;
-    }
 
     tiledLayer->setSkipsDraw(m_skipsDraw);
     tiledLayer->setContentsSwizzled(m_sampledTexelFormat != LayerTextureUpdater::SampledTexelFormatRGBA);
@@ -280,7 +292,7 @@ UpdatableTile* TiledLayerChromium::createTile(int i, int j)
 {
     RefPtr<UpdatableTile> tile = adoptRef(new UpdatableTile(textureUpdater()->createTexture(textureManager())));
     m_tiler->addTile(tile, i, j);
-    tile->m_dirtyLayerRect = m_tiler->tileLayerRect(tile.get());
+    tile->m_dirtyRect = m_tiler->tileRect(tile.get());
 
     return tile.get();
 }
@@ -292,25 +304,35 @@ void TiledLayerChromium::setNeedsDisplayRect(const FloatRect& dirtyRect)
     LayerChromium::setNeedsDisplayRect(dirtyRect);
 }
 
-void TiledLayerChromium::invalidateRect(const IntRect& contentRect)
+void TiledLayerChromium::setIsNonCompositedContent(bool isNonCompositedContent)
 {
-    if (!m_tiler || contentRect.isEmpty() || m_skipsDraw)
-        return;
+    LayerChromium::setIsNonCompositedContent(isNonCompositedContent);
+
+    CCLayerTilingData::BorderTexelOption borderTexelOption;
+#if OS(ANDROID)
+    // Always want border texels and GL_LINEAR due to pinch zoom.
+    borderTexelOption = CCLayerTilingData::HasBorderTexels;
+#else
+    borderTexelOption = isNonCompositedContent ? CCLayerTilingData::NoBorderTexels : CCLayerTilingData::HasBorderTexels;
+#endif
+    setBorderTexelOption(borderTexelOption);
+}
 
-    // Dirty rects are always in layer space, as the layer could be repositioned
-    // after invalidation.
-    const IntRect layerRect = m_tiler->contentRectToLayerRect(contentRect);
+void TiledLayerChromium::invalidateRect(const IntRect& layerRect)
+{
+    if (m_tiler->isEmpty() || layerRect.isEmpty() || m_skipsDraw)
+        return;
 
     int left, top, right, bottom;
-    m_tiler->contentRectToTileIndices(contentRect, left, top, right, bottom);
+    m_tiler->layerRectToTileIndices(layerRect, left, top, right, bottom);
     for (int j = top; j <= bottom; ++j) {
         for (int i = left; i <= right; ++i) {
             UpdatableTile* tile = tileAt(i, j);
             if (!tile)
                 continue;
-            IntRect bound = m_tiler->tileLayerRect(tile);
+            IntRect bound = m_tiler->tileRect(tile);
             bound.intersect(layerRect);
-            tile->m_dirtyLayerRect.unite(bound);
+            tile->m_dirtyRect.unite(bound);
         }
     }
 }
@@ -320,13 +342,13 @@ void TiledLayerChromium::protectVisibleTileTextures()
     protectTileTextures(IntRect(IntPoint::zero(), contentBounds()));
 }
 
-void TiledLayerChromium::protectTileTextures(const IntRect& contentRect)
+void TiledLayerChromium::protectTileTextures(const IntRect& layerRect)
 {
-    if (!m_tiler || contentRect.isEmpty())
+    if (m_tiler->isEmpty() || layerRect.isEmpty())
         return;
 
     int left, top, right, bottom;
-    m_tiler->contentRectToTileIndices(contentRect, left, top, right, bottom);
+    m_tiler->layerRectToTileIndices(layerRect, left, top, right, bottom);
 
     for (int j = top; j <= bottom; ++j) {
         for (int i = left; i <= right; ++i) {
@@ -339,38 +361,19 @@ void TiledLayerChromium::protectTileTextures(const IntRect& contentRect)
     }
 }
 
-void TiledLayerChromium::prepareToUpdate(const IntRect& contentRect)
+void TiledLayerChromium::prepareToUpdateTiles(bool idle, int left, int top, int right, int bottom)
 {
-    if (!m_tiler)
-        createTiler(isNonCompositedContent() ? CCLayerTilingData::NoBorderTexels : CCLayerTilingData::HasBorderTexels);
-
-    ASSERT(m_tiler);
-
-    m_skipsDraw = false;
-
     // Reset m_updateRect for all tiles.
     for (CCLayerTilingData::TileMap::const_iterator iter = m_tiler->tiles().begin(); iter != m_tiler->tiles().end(); ++iter) {
         UpdatableTile* tile = static_cast<UpdatableTile*>(iter->second.get());
         tile->m_updateRect = IntRect();
     }
 
-    if (contentRect.isEmpty()) {
-        m_requestedUpdateRect = IntRect();
-        return;
-    }
-
-    m_tiler->growLayerToContain(contentRect);
-
-    if (!m_tiler->numTiles()) {
-        m_requestedUpdateRect = IntRect();
-        return;
-    }
-
     // Create tiles as needed, expanding a dirty rect to contain all
-    // the dirty regions currently being drawn.
+    // the dirty regions currently being drawn. All dirty tiles that are to be painted
+    // get their m_updateRect set to m_dirtyRect and m_dirtyRect cleared. This way if
+    // invalidateRect is invoked during prepareToUpdate we don't lose the request.
     IntRect dirtyLayerRect;
-    int left, top, right, bottom;
-    m_tiler->contentRectToTileIndices(contentRect, left, top, right, bottom);
     for (int j = top; j <= bottom; ++j) {
         for (int i = left; i <= right; ++i) {
             UpdatableTile* tile = tileAt(i, j);
@@ -378,34 +381,39 @@ void TiledLayerChromium::prepareToUpdate(const IntRect& contentRect)
                 tile = createTile(i, j);
 
             if (!tile->managedTexture()->isValid(m_tiler->tileSize(), m_textureFormat))
-                tile->m_dirtyLayerRect = m_tiler->tileLayerRect(tile);
+                tile->m_dirtyRect = m_tiler->tileRect(tile);
 
             if (!tile->managedTexture()->reserve(m_tiler->tileSize(), m_textureFormat)) {
-                m_skipsDraw = true;
-                cleanupResources();
+                m_skipsIdlePaint = true;
+                if (!idle) {
+                    m_skipsDraw = true;
+                    cleanupResources();
+                }
                 return;
             }
 
-            dirtyLayerRect.unite(tile->m_dirtyLayerRect);
+            dirtyLayerRect.unite(tile->m_dirtyRect);
+            tile->copyAndClearDirty();
         }
     }
 
-    // Due to borders, when the paint rect is extended to tile boundaries, it
-    // may end up overlapping more tiles than the original content rect. Record
-    // that original rect so we don't upload more tiles than necessary.
-    m_requestedUpdateRect = contentRect;
-
-    m_paintRect = m_tiler->layerRectToContentRect(dirtyLayerRect);
+    m_paintRect = dirtyLayerRect;
     if (dirtyLayerRect.isEmpty())
         return;
 
+    // Due to borders, when the paint rect is extended to tile boundaries, it
+    // may end up overlapping more tiles than the original content rect. Record
+    // the original tiles so we don't upload more tiles than necessary.
+    if (!m_paintRect.isEmpty())
+        m_requestedUpdateTilesRect = IntRect(left, top, right - left + 1, bottom - top + 1);
+
     // Calling prepareToUpdate() calls into WebKit to paint, which may have the side
     // effect of disabling compositing, which causes our reference to the texture updater to be deleted.
     // However, we can't free the memory backing the GraphicsContext until the paint finishes,
     // so we grab a local reference here to hold the updater alive until the paint completes.
     RefPtr<LayerTextureUpdater> protector(textureUpdater());
-    textureUpdater()->prepareToUpdate(m_paintRect, m_tiler->tileSize(), m_tiler->hasBorderTexels(), contentsScale());
-    m_tiler->contentRectToTileIndices(m_requestedUpdateRect, left, top, right, bottom);
+    IntRect opaqueRect; // FIXME: unused. remove this and store in the layer to pass to impl for draw culling
+    textureUpdater()->prepareToUpdate(m_paintRect, m_tiler->tileSize(), m_tiler->hasBorderTexels(), contentsScale(), &opaqueRect);
     for (int j = top; j <= bottom; ++j) {
         for (int i = left; i <= right; ++i) {
             UpdatableTile* tile = tileAt(i, j);
@@ -414,20 +422,17 @@ void TiledLayerChromium::prepareToUpdate(const IntRect& contentRect)
             if (!tile)
                 CRASH();
 
-            if (!tile->isDirty())
+            // Use m_updateRect as copyAndClearDirty above moved the existing dirty rect to m_updateRect.
+            const IntRect& dirtyRect = tile->m_updateRect;
+            if (dirtyRect.isEmpty())
                 continue;
 
-            // Calculate content-space rectangle to copy from.
-            IntRect sourceRect = m_tiler->tileContentRect(tile);
-            sourceRect.intersect(m_tiler->layerRectToContentRect(tile->m_dirtyLayerRect));
+            IntRect sourceRect = m_tiler->tileRect(tile);
+            sourceRect.intersect(dirtyRect);
             // Paint rect not guaranteed to line up on tile boundaries, so
             // make sure that sourceRect doesn't extend outside of it.
             sourceRect.intersect(m_paintRect);
 
-            // updateCompositorResources() uses m_updateRect to determine
-            // the tiles to update so we can clear the dirty rectangle here.
-            tile->clearDirty();
-
             tile->m_updateRect = sourceRect;
             if (sourceRect.isEmpty())
                 continue;
@@ -437,5 +442,109 @@ void TiledLayerChromium::prepareToUpdate(const IntRect& contentRect)
     }
 }
 
+
+void TiledLayerChromium::prepareToUpdate(const IntRect& layerRect)
+{
+    m_skipsDraw = false;
+    m_skipsIdlePaint = false;
+    m_requestedUpdateTilesRect = IntRect();
+    m_paintRect = IntRect();
+
+    updateBounds();
+
+    if (layerRect.isEmpty() || !m_tiler->numTiles())
+        return;
+
+    int left, top, right, bottom;
+    m_tiler->layerRectToTileIndices(layerRect, left, top, right, bottom);
+
+    prepareToUpdateTiles(false, left, top, right, bottom);
+}
+
+void TiledLayerChromium::prepareToUpdateIdle(const IntRect& layerRect)
+{
+    // Abort if we have already prepared a paint or run out of memory.
+    if (m_skipsIdlePaint || !m_paintRect.isEmpty())
+        return;
+
+    ASSERT(m_tiler);
+
+    updateBounds();
+
+    if (m_tiler->isEmpty())
+        return;
+
+    // Protect any textures in the pre-paint area so we don't end up just
+    // reclaiming them below.
+    IntRect idlePaintLayerRect = idlePaintRect(layerRect);
+    protectTileTextures(idlePaintLayerRect);
+
+    // Expand outwards until we find a dirty row or column to update.
+    int left, top, right, bottom;
+    m_tiler->layerRectToTileIndices(layerRect, left, top, right, bottom);
+    int prepaintLeft, prepaintTop, prepaintRight, prepaintBottom;
+    m_tiler->layerRectToTileIndices(idlePaintLayerRect, prepaintLeft, prepaintTop, prepaintRight, prepaintBottom);
+    while (!m_skipsIdlePaint && (left > prepaintLeft || top > prepaintTop || right < prepaintRight || bottom < prepaintBottom)) {
+        if (bottom < prepaintBottom) {
+            ++bottom;
+            prepareToUpdateTiles(true, left, bottom, right, bottom);
+            if (!m_paintRect.isEmpty() || m_skipsIdlePaint)
+                break;
+        }
+        if (top > prepaintTop) {
+            --top;
+            prepareToUpdateTiles(true, left, top, right, top);
+            if (!m_paintRect.isEmpty() || m_skipsIdlePaint)
+                break;
+        }
+        if (left > prepaintLeft) {
+            --left;
+            prepareToUpdateTiles(true, left, top, left, bottom);
+            if (!m_paintRect.isEmpty() || m_skipsIdlePaint)
+                break;
+        }
+        if (right < prepaintRight) {
+            ++right;
+            prepareToUpdateTiles(true, right, top, right, bottom);
+            if (!m_paintRect.isEmpty() || m_skipsIdlePaint)
+                break;
+        }
+    }
+}
+
+bool TiledLayerChromium::needsIdlePaint(const IntRect& layerRect)
+{
+    if (m_skipsIdlePaint)
+        return false;
+
+    IntRect idlePaintLayerRect = idlePaintRect(layerRect);
+
+    int left, top, right, bottom;
+    m_tiler->layerRectToTileIndices(idlePaintLayerRect, left, top, right, bottom);
+    for (int j = top; j <= bottom; ++j) {
+        for (int i = left; i <= right; ++i) {
+            if (m_requestedUpdateTilesRect.contains(IntPoint(i, j)))
+                continue;
+            UpdatableTile* tile = tileAt(i, j);
+            if (!tile || !tile->managedTexture()->isValid(m_tiler->tileSize(), m_textureFormat) || tile->isDirty())
+                return true;
+        }
+    }
+    return false;
+}
+
+IntRect TiledLayerChromium::idlePaintRect(const IntRect& visibleLayerRect)
+{
+    IntRect prepaintRect = visibleLayerRect;
+    // FIXME: This can be made a lot larger if we can:
+    // - reserve memory at a lower priority than for visible content
+    // - only reserve idle paint tiles up to a memory reclaim threshold and
+    // - insure we play nicely with other layers
+    prepaintRect.inflateX(m_tiler->tileSize().width());
+    prepaintRect.inflateY(m_tiler->tileSize().height());
+    prepaintRect.intersect(IntRect(IntPoint::zero(), contentBounds()));
+    return prepaintRect;
+}
+
 }
 #endif // USE(ACCELERATED_COMPOSITING)