Unreviewed, rolling out r105366.
[WebKit-https.git] / Source / WebCore / platform / graphics / chromium / TiledLayerChromium.cpp
index a0e9102..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;
@@ -76,6 +79,7 @@ TiledLayerChromium::TiledLayerChromium(CCLayerDelegate* delegate)
     , m_sampledTexelFormat(LayerTextureUpdater::SampledTexelFormatInvalid)
     , m_tilingOption(AutoTile)
 {
+    m_tiler = CCLayerTilingData::create(IntSize(defaultTileSize, defaultTileSize), CCLayerTilingData::HasBorderTexels);
 }
 
 TiledLayerChromium::~TiledLayerChromium()
@@ -91,7 +95,7 @@ void TiledLayerChromium::cleanupResources()
 {
     LayerChromium::cleanupResources();
 
-    m_tiler.clear();
+    m_tiler->reset();
     m_paintRect = IntRect();
     m_requestedUpdateTilesRect = IntRect();
 }
@@ -121,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;
 
@@ -171,19 +191,10 @@ void TiledLayerChromium::setLayerTreeHost(CCLayerTreeHost* host)
     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_requestedUpdateTilesRect.isEmpty() || !m_tiler || !m_tiler->numTiles())
+    if (m_skipsDraw || m_requestedUpdateTilesRect.isEmpty() || m_tiler->isEmpty())
         return;
 
     int left = m_requestedUpdateTilesRect.x();
@@ -205,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());
@@ -229,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)
@@ -250,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);
@@ -288,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();
 }
@@ -300,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);
         }
     }
 }
@@ -328,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) {
@@ -356,7 +370,9 @@ void TiledLayerChromium::prepareToUpdateTiles(bool idle, int left, int top, int
     }
 
     // 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;
     for (int j = top; j <= bottom; ++j) {
         for (int i = left; i <= right; ++i) {
@@ -365,7 +381,7 @@ void TiledLayerChromium::prepareToUpdateTiles(bool idle, int left, int top, int
                 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_skipsIdlePaint = true;
@@ -376,11 +392,12 @@ void TiledLayerChromium::prepareToUpdateTiles(bool idle, int left, int top, int
                 return;
             }
 
-            dirtyLayerRect.unite(tile->m_dirtyLayerRect);
+            dirtyLayerRect.unite(tile->m_dirtyRect);
+            tile->copyAndClearDirty();
         }
     }
 
-    m_paintRect = m_tiler->layerRectToContentRect(dirtyLayerRect);
+    m_paintRect = dirtyLayerRect;
     if (dirtyLayerRect.isEmpty())
         return;
 
@@ -395,7 +412,8 @@ void TiledLayerChromium::prepareToUpdateTiles(bool idle, int left, int top, int
     // 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());
+    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);
@@ -404,20 +422,17 @@ void TiledLayerChromium::prepareToUpdateTiles(bool idle, int left, int top, int
             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;
@@ -428,30 +443,25 @@ void TiledLayerChromium::prepareToUpdateTiles(bool idle, int left, int top, int
 }
 
 
-void TiledLayerChromium::prepareToUpdate(const IntRect& contentRect)
+void TiledLayerChromium::prepareToUpdate(const IntRect& layerRect)
 {
-    if (!m_tiler)
-        createTiler(isNonCompositedContent() ? CCLayerTilingData::NoBorderTexels : CCLayerTilingData::HasBorderTexels);
-
-    ASSERT(m_tiler);
-
     m_skipsDraw = false;
     m_skipsIdlePaint = false;
     m_requestedUpdateTilesRect = IntRect();
     m_paintRect = IntRect();
 
-    m_tiler->growLayerToContain(IntRect(IntPoint::zero(), contentBounds()));
+    updateBounds();
 
-    if (contentRect.isEmpty() || !m_tiler->numTiles())
+    if (layerRect.isEmpty() || !m_tiler->numTiles())
         return;
 
     int left, top, right, bottom;
-    m_tiler->contentRectToTileIndices(contentRect, left, top, right, bottom);
+    m_tiler->layerRectToTileIndices(layerRect, left, top, right, bottom);
 
     prepareToUpdateTiles(false, left, top, right, bottom);
 }
 
-void TiledLayerChromium::prepareToUpdateIdle(const IntRect& contentRect)
+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())
@@ -459,21 +469,21 @@ void TiledLayerChromium::prepareToUpdateIdle(const IntRect& contentRect)
 
     ASSERT(m_tiler);
 
-    m_tiler->growLayerToContain(IntRect(IntPoint::zero(), contentBounds()));
+    updateBounds();
 
-    if (!m_tiler->numTiles())
+    if (m_tiler->isEmpty())
         return;
 
     // Protect any textures in the pre-paint area so we don't end up just
     // reclaiming them below.
-    IntRect idlePaintContentRect = idlePaintRect(contentRect);
-    protectTileTextures(idlePaintContentRect);
+    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->contentRectToTileIndices(contentRect, left, top, right, bottom);
+    m_tiler->layerRectToTileIndices(layerRect, left, top, right, bottom);
     int prepaintLeft, prepaintTop, prepaintRight, prepaintBottom;
-    m_tiler->contentRectToTileIndices(idlePaintContentRect, 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;
@@ -502,17 +512,15 @@ void TiledLayerChromium::prepareToUpdateIdle(const IntRect& contentRect)
     }
 }
 
-bool TiledLayerChromium::needsIdlePaint(const IntRect& contentRect)
+bool TiledLayerChromium::needsIdlePaint(const IntRect& layerRect)
 {
     if (m_skipsIdlePaint)
         return false;
 
-    ASSERT(m_tiler);
-
-    IntRect idlePaintContentRect = idlePaintRect(contentRect);
+    IntRect idlePaintLayerRect = idlePaintRect(layerRect);
 
     int left, top, right, bottom;
-    m_tiler->contentRectToTileIndices(idlePaintContentRect, 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)))
@@ -525,10 +533,9 @@ bool TiledLayerChromium::needsIdlePaint(const IntRect& contentRect)
     return false;
 }
 
-IntRect TiledLayerChromium::idlePaintRect(const IntRect& visibleContentRect)
+IntRect TiledLayerChromium::idlePaintRect(const IntRect& visibleLayerRect)
 {
-    ASSERT(m_tiler);
-    IntRect prepaintRect = visibleContentRect;
+    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