Fix rendering artifact in edge fades.
Bug #4092053

The problem always existed but was made visible by partial invalidation.
When saving a layer, the renderer would try to postpone glClear()
operations until the next drawing command. This however does not work
since the clip might have changed. The fix is rather simple and
simply gets rid of this "optimization" (that turned out to be
usless anyway given how View issues saveLayer() calls.)

This change also fixes an issue with gradients (color stops where
not properly computed when using a null stops array) and optimizes
display lists rendering (quickly rejects larger portions of the
tree to avoid executing unnecessary code.)

Change-Id: I0f5b5f6e1220d41a09cc2fa84c212b0b4afd9c46
diff --git a/libs/hwui/DisplayListRenderer.cpp b/libs/hwui/DisplayListRenderer.cpp
index 737fa02..868290b 100644
--- a/libs/hwui/DisplayListRenderer.cpp
+++ b/libs/hwui/DisplayListRenderer.cpp
@@ -285,9 +285,12 @@
             break;
             case DrawDisplayList: {
                 DisplayList* displayList = getDisplayList();
-                DISPLAY_LIST_LOGD("%s%s %p, %d", (char*) indent, OP_NAMES[op],
-                    displayList, level + 1);
-                needsInvalidate |= renderer.drawDisplayList(displayList, dirty, level + 1);
+                uint32_t width = getUInt();
+                uint32_t height = getUInt();
+                DISPLAY_LIST_LOGD("%s%s %p, %dx%d, %d", (char*) indent, OP_NAMES[op],
+                    displayList, width, height, level + 1);
+                needsInvalidate |= renderer.drawDisplayList(displayList, width, height,
+                        dirty, level + 1);
             }
             break;
             case DrawLayer: {
@@ -674,11 +677,13 @@
     return OpenGLRenderer::clipRect(left, top, right, bottom, op);
 }
 
-bool DisplayListRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty, uint32_t level) {
+bool DisplayListRenderer::drawDisplayList(DisplayList* displayList,
+        uint32_t width, uint32_t height, Rect& dirty, uint32_t level) {
     // dirty is an out parameter and should not be recorded,
     // it matters only when replaying the display list
     addOp(DisplayList::DrawDisplayList);
     addDisplayList(displayList);
+    addSize(width, height);
     return false;
 }
 
diff --git a/libs/hwui/DisplayListRenderer.h b/libs/hwui/DisplayListRenderer.h
index f24545d..6fc315c 100644
--- a/libs/hwui/DisplayListRenderer.h
+++ b/libs/hwui/DisplayListRenderer.h
@@ -144,6 +144,10 @@
         return mReader.readInt();
     }
 
+    inline uint32_t getUInt() {
+        return mReader.readU32();
+    }
+
     SkMatrix* getMatrix() {
         return (SkMatrix*) getInt();
     }
@@ -238,7 +242,8 @@
 
     bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
 
-    bool drawDisplayList(DisplayList* displayList, Rect& dirty, uint32_t level = 0);
+    bool drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
+            Rect& dirty, uint32_t level = 0);
     void drawLayer(Layer* layer, float x, float y, SkPaint* paint);
     void drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint);
     void drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint);
@@ -323,6 +328,11 @@
         mWriter.writeInt(value);
     }
 
+    inline void addSize(uint32_t w, uint32_t h) {
+        mWriter.writeInt(w);
+        mWriter.writeInt(h);
+    }
+
     void addInts(const int32_t* values, uint32_t count) {
         mWriter.writeInt(count);
         for (uint32_t i = 0; i < count; i++) {
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 1f65201..5d9522e 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -403,10 +403,7 @@
 
     // Window coordinates of the layer
     Rect bounds(left, top, right, bottom);
-    if (fboLayer) {
-        // Clear the previous layer regions before we change the viewport
-        clearLayerRegions();
-    } else {
+    if (!fboLayer) {
         mSnapshot->transform->mapRect(bounds);
 
         // Layers only make sense if they are in the framebuffer's bounds
@@ -464,8 +461,14 @@
                 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
                         snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
             }
-            // Enqueue the buffer coordinates to clear the corresponding region later
-            mLayers.push(new Rect(bounds));
+
+            // Clear the framebuffer where the layer will draw
+            glScissor(bounds.left, mSnapshot->height - bounds.bottom,
+                    bounds.getWidth(), bounds.getHeight());
+            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+            glClear(GL_COLOR_BUFFER_BIT);
+
+            dirtyClip();
         }
     }
 
@@ -760,31 +763,6 @@
 #endif
 }
 
-void OpenGLRenderer::clearLayerRegions() {
-    if (mLayers.size() == 0 || mSnapshot->isIgnored()) return;
-
-    Rect clipRect(*mSnapshot->clipRect);
-    clipRect.snapToPixelBoundaries();
-
-    for (uint32_t i = 0; i < mLayers.size(); i++) {
-        Rect* bounds = mLayers.itemAt(i);
-        if (clipRect.intersects(*bounds)) {
-            // Clear the framebuffer where the layer will draw
-            glScissor(bounds->left, mSnapshot->height - bounds->bottom,
-                    bounds->getWidth(), bounds->getHeight());
-            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
-            glClear(GL_COLOR_BUFFER_BIT);
-
-            // Restore the clip
-            dirtyClip();
-        }
-
-        delete bounds;
-    }
-
-    mLayers.clear();
-}
-
 ///////////////////////////////////////////////////////////////////////////////
 // Transforms
 ///////////////////////////////////////////////////////////////////////////////
@@ -870,7 +848,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 void OpenGLRenderer::setupDraw() {
-    clearLayerRegions();
     if (mDirtyClip) {
         setScissorFromClip();
     }
@@ -1064,12 +1041,18 @@
 // Drawing
 ///////////////////////////////////////////////////////////////////////////////
 
-bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty, uint32_t level) {
+bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
+        Rect& dirty, uint32_t level) {
+    if (quickReject(0.0f, 0.0f, width, height)) {
+        return false;
+    }
+
     // All the usual checks and setup operations (quickReject, setupDraw, etc.)
     // will be performed by the display list itself
     if (displayList) {
         return displayList->replay(*this, dirty, level);
     }
+
     return false;
 }
 
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index 9d86388..7362473 100644
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -96,7 +96,8 @@
     bool quickReject(float left, float top, float right, float bottom);
     virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
 
-    virtual bool drawDisplayList(DisplayList* displayList, Rect& dirty, uint32_t level = 0);
+    virtual bool drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
+            Rect& dirty, uint32_t level = 0);
     virtual void drawLayer(Layer* layer, float x, float y, SkPaint* paint);
     virtual void drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint);
     virtual void drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint);
@@ -247,12 +248,6 @@
     void composeLayerRect(Layer* layer, const Rect& rect, bool swap = false);
 
     /**
-     * Clears all the regions corresponding to the current list of layers.
-     * This method MUST be invoked before any drawing operation.
-     */
-    void clearLayerRegions();
-
-    /**
      * Mark the layer as dirty at the specified coordinates. The coordinates
      * are transformed with the supplied matrix.
      */
@@ -499,9 +494,6 @@
     // Various caches
     Caches& mCaches;
 
-    // List of rectangles to clear due to calls to saveLayer()
-    Vector<Rect*> mLayers;
-
     // Indentity matrix
     const mat4 mIdentity;
 
diff --git a/libs/hwui/ProgramCache.h b/libs/hwui/ProgramCache.h
index 3acd18a..ead5b92 100644
--- a/libs/hwui/ProgramCache.h
+++ b/libs/hwui/ProgramCache.h
@@ -231,9 +231,11 @@
      * Logs the specified message followed by the key identifying this program.
      */
     void log(const char* message) const {
+#if DEBUG_PROGRAMS
         programid k = key();
         PROGRAM_LOGD("%s (key = 0x%.8x%.8x)", message, uint32_t(k >> 32),
                 uint32_t(k & 0xffffffff));
+#endif
     }
 
 private: