Isolate tiling clip state from snapshot

bug:8409891

Snapshots frequently have their clip overwritten due to applying
deferred state - now, store tiling clip information in a separate
rect, outside of the snapshot so it isn't overwritten.

Change-Id: I21ca4c45dcd802eae99e8de86f11525196777ccb
diff --git a/libs/hwui/DisplayList.cpp b/libs/hwui/DisplayList.cpp
index 55ddd17..d985ad0 100644
--- a/libs/hwui/DisplayList.cpp
+++ b/libs/hwui/DisplayList.cpp
@@ -351,9 +351,9 @@
                     level * 2, "", mTransformMatrix, MATRIX_ARGS(mTransformMatrix));
         }
     }
-    if (mAlpha < 1 && !mCaching) {
-        if (!mHasOverlappingRendering) {
-            ALOGD("%*sSetAlpha %.2f", level * 2, "", mAlpha);
+    if (mAlpha < 1) {
+        if (mCaching || !mHasOverlappingRendering) {
+            ALOGD("%*sScaleAlpha %.2f", level * 2, "", mAlpha);
         } else {
             int flags = SkCanvas::kHasAlphaLayer_SaveFlag;
             if (mClipChildren) {
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index bc28d65..2cf7183 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -193,6 +193,7 @@
     mSaveCount = 1;
 
     mSnapshot->setClip(left, top, right, bottom);
+    mTilingClip.set(left, top, right, bottom);
     mDirtyClip = true;
 
     updateLayers();
@@ -206,8 +207,7 @@
     // invoked during the frame
     mSuppressTiling = mCaches.hasRegisteredFunctors();
 
-    mTilingSnapshot = mSnapshot;
-    startTiling(mTilingSnapshot, true);
+    startTiling(mSnapshot, true);
 
     debugOverdraw(true, true);
 
@@ -252,9 +252,9 @@
 
 void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
     if (!mSuppressTiling) {
-        Rect* clip = mTilingSnapshot->clipRect;
+        Rect* clip = &mTilingClip;
         if (s->flags & Snapshot::kFlagFboTarget) {
-            clip = &s->layer->clipRect;
+            clip = &(s->layer->clipRect);
         }
 
         startTiling(*clip, s->height, opaque);
@@ -480,10 +480,10 @@
 
 void OpenGLRenderer::renderOverdraw() {
     if (mCaches.debugOverdraw && getTargetFbo() == 0) {
-        const Rect* clip = mTilingSnapshot->clipRect;
+        const Rect* clip = &mTilingClip;
 
         mCaches.enableScissor();
-        mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
+        mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
                 clip->right - clip->left, clip->bottom - clip->top);
 
         mCaches.stencil.enableDebugTest(2);
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index 71bd6bb..7bb9395 100644
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -947,7 +947,7 @@
     // Current state
     sp<Snapshot> mSnapshot;
     // State used to define the clipping region
-    sp<Snapshot> mTilingSnapshot;
+    Rect mTilingClip;
 
     // Used to draw textured quads
     TextureVertex mMeshVertices[4];
diff --git a/libs/hwui/Snapshot.cpp b/libs/hwui/Snapshot.cpp
index 923913e..d26ee38 100644
--- a/libs/hwui/Snapshot.cpp
+++ b/libs/hwui/Snapshot.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "OpenGLRenderer"
+
 #include "Snapshot.h"
 
 #include <SkCanvas.h>
@@ -199,5 +201,14 @@
     return invisible || empty;
 }
 
+void Snapshot::dump() const {
+    ALOGD("Snapshot %p, flags %x, prev %p, height %d, ignored %d, hasComplexClip %d",
+            this, flags, previous.get(), height, isIgnored(), clipRegion && !clipRegion->isEmpty());
+    ALOGD("  ClipRect (at %p) %.1f %.1f %.1f %.1f",
+            clipRect, clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
+    ALOGD("  Transform (at %p):", transform);
+    transform->dump();
+}
+
 }; // namespace uirenderer
 }; // namespace android
diff --git a/libs/hwui/Snapshot.h b/libs/hwui/Snapshot.h
index ffd4729..cc6d0cd 100644
--- a/libs/hwui/Snapshot.h
+++ b/libs/hwui/Snapshot.h
@@ -228,6 +228,8 @@
      */
     float alpha;
 
+    void dump() const;
+
 private:
     void ensureClipRegion();
     void copyClipRectFromRegion();