hwc: rgz: Fix dirty region not working when blitting only one layer

The dirty region handling doesn't work properly when only one
layer is being blitted. The problem was the screen dirty state was
only set to zero when one layer reaches the dirty count to zero. In
case only one layer was being blitted the dirty region handling
is never triggered making the empty areas to be cleared on every
frame even if it was not needed.

This patch removes the screen dirty flag and instead introduces
a dirty count for the background and layers with the clear fb hint
as well, since we don't care about the handle, the dirty count
is decreased on every frame, in this way the dirty region handling
sees the background and layers with the clear fb hint as any other
layer.

Rebased from p-ics-mr1:
11e5f7e hwc: rgz: Fix dirty region not working when blitting only one layer

Change-Id: Ifbb111ce7738cd097e74ac01e2c48f3994857416
Signed-off-by: Gustavo Diaz Prado <a0273371@ti.com>

Conflicts:

	hwc/rgz_2d.c

Signed-off-by: Tony Lofthouse <a0741364@ti.com>
(cherry picked from commit a79d46bbd0438140fb6500d6243b5e3aa392068b)
diff --git a/hwc/rgz_2d.c b/hwc/rgz_2d.c
index 9e2dcea..abf2722 100644
--- a/hwc/rgz_2d.c
+++ b/hwc/rgz_2d.c
@@ -624,10 +624,18 @@
     }
 
     int possible_blit = 0, candidates = 0;
-    rgz->screen_isdirty = 1;
 
-    /* Insert the background layer at the beginning of the list */
-    rgz->rgz_layers[0].hwc_layer = &bg_layer;
+    /*
+     * Insert the background layer at the beginning of the list, maintain a
+     * state for dirty region handling
+     */
+    rgz_layer_t *rgz_layer = &rgz->rgz_layers[0];
+    rgz_layer->hwc_layer = &bg_layer;
+    if (!rgz_layer->dirty_hndl) {
+        rgz_layer->dirty_hndl = (void*)0x1;
+        rgz_layer->dirty_count = RGZ_NUM_FB;
+    } else
+        rgz_layer->dirty_count -= rgz_layer->dirty_count ? 1 : 0;
 
     for (l = 0; l < layerno; l++) {
         if (layers[l].compositionType == HWC_FRAMEBUFFER) {
@@ -640,11 +648,9 @@
                 if (rgz_layer->hwc_layer->handle != rgz_layer->dirty_hndl) {
                     rgz_layer->dirty_count = RGZ_NUM_FB;
                     rgz_layer->dirty_hndl = (void*)rgz_layer->hwc_layer->handle;
-                } else {
+                } else
                      rgz_layer->dirty_count -= rgz_layer->dirty_count ? 1 : 0;
-                     if (rgz_layer->dirty_count == 0)
-                         rgz->screen_isdirty = 0; /* Just update dirty regions from now on */
-                }
+
                 possible_blit++;
             }
             continue;
@@ -659,9 +665,16 @@
                  */
                 rgz_layer_t *rgz_layer = &rgz->rgz_layers[possible_blit+1];
                 rgz_layer->buffidx = -1;
-                rgz_layer->dirty_count = 0;
-                rgz_layer->dirty_hndl = NULL;
                 rgz_layer->hwc_layer = &layers[l];
+                /*
+                 * We don't care about the handle but we want to maintain a layer state for
+                 * dirty region handling
+                 */
+                if (!rgz_layer->dirty_hndl) {
+                    rgz_layer->dirty_hndl = (void*)0x1;
+                    rgz_layer->dirty_count = RGZ_NUM_FB;
+                } else
+                    rgz_layer->dirty_count -= rgz_layer->dirty_count ? 1 : 0;
                 possible_blit++;
             }
         }
@@ -1263,7 +1276,7 @@
     e->bp.batchflags |= set;
 }
 
-static int rgz_hwc_subregion_blit(blit_hregion_t *hregion, int sidx, rgz_out_params_t *params, int screen_isdirty)
+static int rgz_hwc_subregion_blit(blit_hregion_t *hregion, int sidx, rgz_out_params_t *params)
 {
     static int loaded = 0;
     if (!loaded)
@@ -1293,12 +1306,26 @@
         return -1;
     }
 
+    /* Determine if this region is dirty */
+    int dirty = 0, dirtylix = lix;
+    while (dirtylix != -1) {
+        rgz_layer_t *rgz_layer = hregion->rgz_layers[dirtylix];
+        if (rgz_layer->dirty_count){
+            /* One of the layers is dirty, we need to generate blits for this subregion */
+            dirty = 1;
+            break;
+        }
+        dirtylix = get_layer_ops_next(hregion, sidx, dirtylix);
+    }
+
+    if (!dirty)
+        return 0;
+
     /* Check if the bottom layer is the background */
     if (hregion->rgz_layers[lix]->hwc_layer == &bg_layer) {
         if (ldepth == 1) {
             /* Background layer is the only operation, clear subregion */
-            if (screen_isdirty)
-                rgz_out_clrdst(params, &hregion->blitrects[lix][sidx]);
+            rgz_out_clrdst(params, &hregion->blitrects[lix][sidx]);
             return 0;
         } else {
             /* No need to generate blits with background layer if there is
@@ -1316,28 +1343,12 @@
     if (hregion->rgz_layers[lix]->buffidx == -1) {
         ldepth--;
         if (!ldepth) {
-            if (screen_isdirty)
-                rgz_out_clrdst(params, &hregion->blitrects[lix][sidx]);
+            rgz_out_clrdst(params, &hregion->blitrects[lix][sidx]);
             return 0;
         }
         lix = get_layer_ops_next(hregion, sidx, lix);
     }
 
-    /* Determine if this region is dirty */
-    int dirty = 0, dirtylix = lix;
-    while (dirtylix != -1) {
-        rgz_layer_t *rgz_layer = hregion->rgz_layers[dirtylix];
-        if (rgz_layer->dirty_count){
-            /* One of the layers is dirty, we need to generate blits for this subregion */
-            dirty = 1;
-            break;
-        }
-        dirtylix = get_layer_ops_next(hregion, sidx, dirtylix);
-    }
-
-    if (!dirty)
-        return 0;
-
     if (!noblend && ldepth > 1) { /* BLEND */
         blit_rect_t *rect = &hregion->blitrects[lix][sidx];
         struct rgz_blt_entry* e = rgz_blts_get(&blts, params);
@@ -1489,7 +1500,7 @@
         }
         for (s = 0; s < hregion->nsubregions; s++) {
             ALOGD_IF(debug, "h[%d] -> [%d]", i, s);
-            rgz_hwc_subregion_blit(hregion, s, params, rgz->screen_isdirty);
+            rgz_hwc_subregion_blit(hregion, s, params);
         }
     }
 
diff --git a/hwc/rgz_2d.h b/hwc/rgz_2d.h
index 4b4d3fb..44867b8 100644
--- a/hwc/rgz_2d.h
+++ b/hwc/rgz_2d.h
@@ -285,7 +285,6 @@
     int state;
     unsigned int rgz_layerno;
     rgz_layer_t rgz_layers[RGZ_MAXLAYERS];
-    int screen_isdirty;
 };
 
 #endif /* __RGZ_2D__ */