drm_hwcomposer: Fix ValidateDisplay() when lowest z-order is nonzero

ValidateDisplay()'s algorithm for achieving minimal GPU load assumes
that the lowest z-order is zero and that layers have sequential z-orders.
CalcPixOps() and MarkValidated() are also written with the same assumption.
However, there is no such guarantee provided by SurfaceFlinger and VTS
tests like PRESENT_DISPLAY_NO_LAYER_STATE_CHANGES fail as they only have
one layer with z-order of 10.

Normalise the mapping between layers and z-order so that the algorithm works
as intended.

Signed-off-by: Liviu Dudau <liviu.dudau@arm.com>
Fixes commit b7b81cfba252 ("drm_hwcomposer: Choose client layer range to
achieve minimal GPU load")
Signed-off-by: John Stultz <john.stultz@linaro.org>
Change-Id: I71b76b9d151bf506ad6026f5b1f9de6b6c0dc7c1
diff --git a/drmhwctwo.cpp b/drmhwctwo.cpp
index 8743b14..8338f59 100644
--- a/drmhwctwo.cpp
+++ b/drmhwctwo.cpp
@@ -910,9 +910,14 @@
   if (avail_planes < layers_.size())
     avail_planes--;
 
-  std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
+  std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map, z_map_tmp;
+  uint32_t z_index = 0;
+  // First create a map of layers and z_order values
   for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
-    z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
+    z_map_tmp.emplace(std::make_pair(l.second.z_order(), &l.second));
+  // normalise the map so that the lowest z_order layer has key 0
+  for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map_tmp)
+    z_map.emplace(std::make_pair(z_index++, l.second));
 
   uint32_t total_pixops = CalcPixOps(z_map, 0, z_map.size()), gpu_pixops = 0;