sf: Override presentation layer with ext_only layer

Do not add presentation layer to the external list when external only
layer is present.

Change-Id: I46c8a85a572f88a2d4026c4026d99fd037b928be
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 29cb41d..fd22039 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -1038,13 +1038,11 @@
                 for (size_t i=0 ; i<count ; i++) {
                     const sp<Layer>& layer(layers[i]);
                     const Layer::State& s(layer->getDrawingState());
-                    if (s.layerStack == hw->getLayerStack()) {
-                        Region drawRegion(tr.transform(
-                                layer->visibleNonTransparentRegion));
-                        drawRegion.andSelf(bounds);
-                        if (!drawRegion.isEmpty()) {
-                            layersSortedByZ.add(layer);
-                        }
+                    Region drawRegion(tr.transform(
+                            layer->visibleNonTransparentRegion));
+                    drawRegion.andSelf(bounds);
+                    if (!drawRegion.isEmpty()) {
+                        layersSortedByZ.add(layer);
                     }
                 }
             }
@@ -1758,7 +1756,21 @@
 
     outDirtyRegion.clear();
     bool bIgnoreLayers = false;
+    int extOnlyLayerIndex = -1;
     size_t i = currentLayers.size();
+#ifdef QCOM_BSP
+    while (i--) {
+        const sp<Layer>& layer = currentLayers[i];
+        // iterate through the layer list to find ext_only layers and store
+        // the index
+        if ((dpy && layer->isExtOnly())) {
+            bIgnoreLayers = true;
+            extOnlyLayerIndex = i;
+            break;
+        }
+    }
+    i = currentLayers.size();
+#endif
     while (i--) {
         const sp<Layer>& layer = currentLayers[i];
 
@@ -1766,7 +1778,12 @@
         const Layer::State& s(layer->getDrawingState());
 
 #ifdef QCOM_BSP
-        if(bIgnoreLayers) {
+        // Only add the layer marked as "external_only" to external list and
+        // only remove the layer marked as "external_only" from primary list
+        // and do not add the layer marked as "internal_only" to external list
+        if((bIgnoreLayers && extOnlyLayerIndex != (int)i) ||
+           (!dpy && layer->isExtOnly()) ||
+           (dpy && layer->isIntOnly())) {
             // Ignore all other layers except the layers marked as ext_only
             // by setting visible non transparent region empty.
             Region visibleNonTransRegion;
@@ -1774,27 +1791,18 @@
             layer->setVisibleNonTransparentRegion(visibleNonTransRegion);
             continue;
         }
-        // Only add the layer marked as "external_only" to external list and
-        // only remove the layer marked as "external_only" from primary list
-        // and do not add the layer marked as "internal_only" to external list
-        if ((dpy && layer->isExtOnly())) {
-            bIgnoreLayers = true;
-        } else if(layer->isExtOnly() || (dpy && layer->isIntOnly())) {
-            // Ignore only ext_only layers for primary by setting
-            // visible non transparent region empty.
+#endif
+        // only consider the layers on the given later stack
+        // Override layers created using presentation class by the layers having
+        // ext_only flag enabled
+        if(s.layerStack != layerStack && !bIgnoreLayers) {
+            // set the visible region as empty since we have removed the
+            // layerstack check in rebuildLayerStack() function.
             Region visibleNonTransRegion;
             visibleNonTransRegion.set(Rect(0,0));
             layer->setVisibleNonTransparentRegion(visibleNonTransRegion);
             continue;
         }
-#endif
-
-        // only consider the layers on the given later stack
-        // Override layers created using presentation class by the layers having
-        // ext_only flag enabled
-        if (s.layerStack != layerStack && !bIgnoreLayers)
-            continue;
-
         /*
          * opaqueRegion: area of a surface that is fully opaque.
          */