sdm: Fix layer stack caching

1. Reset the layer stack cache when the frame cannot be cached
   during GPU fallback scenarios.

2. Update checks for framebuffer refreshing to only consider whether
   animation is present on the current layer stack.

Change-Id: I3f945d432b3d387dd711e17c5ea207410646473f
diff --git a/sdm/libs/hwc/hwc_display.cpp b/sdm/libs/hwc/hwc_display.cpp
index 3416d1b..002b32c 100644
--- a/sdm/libs/hwc/hwc_display.cpp
+++ b/sdm/libs/hwc/hwc_display.cpp
@@ -744,8 +744,8 @@
   // 2. Any layer is added/removed/layer properties changes in the current layer stack.
   // 3. Any layer handle is changed and it is marked for GPU composition
   // 4. Any layer's current composition is different from previous composition.
-  if (((layer_stack_cache_.layer_count != layer_count) || layer_stack_.flags.skip_present ||
-      layer_stack_.flags.geometry_changed) && !layer_stack_cache_.animating) {
+  if ((layer_stack_cache_.layer_count != layer_count) || layer_stack_.flags.skip_present ||
+      layer_stack_.flags.geometry_changed || layer_stack_.flags.animating) {
     return true;
   }
 
@@ -762,8 +762,7 @@
       return true;
     }
 
-    if ((layer.composition == kCompositionGPU) && (layer_cache.handle != hwc_layer.handle) &&
-        !layer_stack_cache_.animating) {
+    if ((layer.composition == kCompositionGPU) && (layer_cache.handle != hwc_layer.handle)) {
       return true;
     }
   }
@@ -775,6 +774,7 @@
   uint32_t layer_count = layer_stack_.layer_count;
 
   if (layer_count > kMaxLayerCount) {
+    ResetLayerCacheStack();
     return;
   }
 
@@ -1209,5 +1209,16 @@
   return ret;
 }
 
+void HWCDisplay::ResetLayerCacheStack() {
+  uint32_t layer_count = layer_stack_cache_.layer_count;
+  for (uint32_t i = 0; i < layer_count; i++) {
+    LayerCache &layer_cache = layer_stack_cache_.layer_cache[i];
+    layer_cache.handle = NULL;
+    layer_cache.composition = kCompositionGPU;
+  }
+  layer_stack_cache_.animating = false;
+  layer_stack_cache_.layer_count = 0;
+}
+
 }  // namespace sdm
 
diff --git a/sdm/libs/hwc/hwc_display.h b/sdm/libs/hwc/hwc_display.h
index 87f3c6a..d87c184 100644
--- a/sdm/libs/hwc/hwc_display.h
+++ b/sdm/libs/hwc/hwc_display.h
@@ -157,6 +157,7 @@
   bool NeedsFrameBufferRefresh(hwc_display_contents_1_t *content_list);
   int PrepareLayerParams(hwc_layer_1_t *hwc_layer, Layer *layer, uint32_t fps);
   void CommitLayerParams(hwc_layer_1_t *hwc_layer, Layer *layer);
+  void ResetLayerCacheStack();
   bool use_blit_comp_;
   BlitEngine *blit_engine_;
 };