drm_hwcomposer: Set zpos relative to the minimum possible value

Current implementation doesn't handle properly the cases where zpos
range starts from 1.
See https://gitlab.freedesktop.org/drm-hwcomposer/drm-hwcomposer/issues/19#note_100622

Fixes: ea1c5e5a ("drm_hwcomposer: Add z order support")

Signed-off-by: Alexandru Gheorghe <alexc.g1.ro@gmail.com>
[seanpaul converted to std::tuple return type]
Signed-off-by: Sean Paul <seanpaul@chromium.org>

Change-Id: I35dc2c1cfd0e38ca3a47cf4e668eeb5f3c470ddb
diff --git a/drmdisplaycompositor.cpp b/drmdisplaycompositor.cpp
index 8599363..e6f6922 100644
--- a/drmdisplaycompositor.cpp
+++ b/drmdisplaycompositor.cpp
@@ -361,9 +361,14 @@
 
       if (plane->zpos_property().id() &&
           !plane->zpos_property().is_immutable()) {
+        uint64_t min_zpos = 0;
+
+        // Ignore ret and use min_zpos as 0 by default
+        std::tie(std::ignore, min_zpos) = plane->zpos_property().range_min();
+
         ret = drmModeAtomicAddProperty(pset, plane->id(),
                                        plane->zpos_property().id(),
-                                       source_layers.front()) < 0;
+                                       source_layers.front() + min_zpos) < 0;
         if (ret) {
           ALOGE("Failed to add zpos property %d to plane %d",
                 plane->zpos_property().id(), plane->id());
diff --git a/drmproperty.cpp b/drmproperty.cpp
index e276f56..3aeed13 100644
--- a/drmproperty.cpp
+++ b/drmproperty.cpp
@@ -99,6 +99,28 @@
   return id_ && (flags_ & DRM_MODE_PROP_IMMUTABLE);
 }
 
+bool DrmProperty::is_range() const {
+  return id_ && (flags_ & DRM_MODE_PROP_RANGE);
+}
+
+std::tuple<int, uint64_t> DrmProperty::range_min() const {
+  if (!is_range())
+    return std::make_tuple(-EINVAL, 0);
+  if (values_.size() < 1)
+    return std::make_tuple(-ENOENT, 0);
+
+  return std::make_tuple(0, values_[0]);
+}
+
+std::tuple<int, uint64_t> DrmProperty::range_max() const {
+  if (!is_range())
+    return std::make_tuple(-EINVAL, 0);
+  if (values_.size() < 2)
+    return std::make_tuple(-ENOENT, 0);
+
+  return std::make_tuple(0, values_[1]);
+}
+
 std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName(
     std::string name) const {
   for (auto it : enums_) {
diff --git a/drmproperty.h b/drmproperty.h
index 5b40c4c..2d92ca1 100644
--- a/drmproperty.h
+++ b/drmproperty.h
@@ -48,6 +48,10 @@
   std::tuple<int, uint64_t> value() const;
   bool is_immutable() const;
 
+  bool is_range() const;
+  std::tuple<int, uint64_t> range_min() const;
+  std::tuple<int, uint64_t> range_max() const;
+
  private:
   class DrmPropertyEnum {
    public: