drm_hwcomposer: Query color_encoding and color_range enums during init

Should slightly reduce CPU load at runtime + improve
atomic commit readability.

Signed-off-by: Roman Stratiienko <roman.o.stratiienko@globallogic.com>
diff --git a/drm/DrmPlane.cpp b/drm/DrmPlane.cpp
index 1de1fce..78013fb 100644
--- a/drm/DrmPlane.cpp
+++ b/drm/DrmPlane.cpp
@@ -37,6 +37,7 @@
 }
 
 int DrmPlane::Init() {
+  uint64_t enum_value = UINT64_MAX;
   DrmProperty p;
 
   int ret = drm_->GetPlaneProperty(*this, "type", &p);
@@ -145,12 +146,41 @@
   if (HasNonRgbFormat()) {
     ret = drm_->GetPlaneProperty(*this, "COLOR_ENCODING",
                                  &color_encoding_propery_);
-    if (ret)
+    if (ret == 0) {
+      std::tie(enum_value, ret) = color_encoding_propery_.GetEnumValueWithName(
+          "ITU-R BT.709 YCbCr");
+      if (ret == 0) {
+        color_encoding_enum_map_[DrmHwcColorSpace::kItuRec709] = enum_value;
+      }
+      std::tie(enum_value, ret) = color_encoding_propery_.GetEnumValueWithName(
+          "ITU-R BT.601 YCbCr");
+      if (ret == 0) {
+        color_encoding_enum_map_[DrmHwcColorSpace::kItuRec601] = enum_value;
+      }
+      std::tie(enum_value, ret) = color_encoding_propery_.GetEnumValueWithName(
+          "ITU-R BT.2020 YCbCr");
+      if (ret == 0) {
+        color_encoding_enum_map_[DrmHwcColorSpace::kItuRec2020] = enum_value;
+      }
+    } else {
       ALOGI("Could not get COLOR_ENCODING property");
+    }
 
     ret = drm_->GetPlaneProperty(*this, "COLOR_RANGE", &color_range_property_);
-    if (ret)
+    if (ret == 0) {
+      std::tie(enum_value, ret) = color_range_property_.GetEnumValueWithName(
+          "YCbCr full range");
+      if (ret == 0) {
+        color_range_enum_map_[DrmHwcSampleRange::kFullRange] = enum_value;
+      }
+      std::tie(enum_value, ret) = color_range_property_.GetEnumValueWithName(
+          "YCbCr limited range");
+      if (ret == 0) {
+        color_range_enum_map_[DrmHwcSampleRange::kLimitedRange] = enum_value;
+      }
+    } else {
       ALOGI("Could not get COLOR_RANGE property");
+    }
   }
 
   return 0;
@@ -267,8 +297,6 @@
   uint64_t rotation = 0;
   uint64_t alpha = 0xFFFF;
   uint64_t blend = UINT64_MAX;
-  uint64_t color_encoding = UINT64_MAX;
-  uint64_t color_range = UINT64_MAX;
 
   if (!layer.FbIdHandle) {
     ALOGE("Expected a valid framebuffer for pset");
@@ -336,41 +364,6 @@
     }
   }
 
-  if (color_encoding_propery_) {
-    switch (layer.color_space) {
-      case DrmHwcColorSpace::kItuRec709:
-        std::tie(color_encoding,
-                 ret) = color_encoding_propery_
-                            .GetEnumValueWithName("ITU-R BT.709 YCbCr");
-        break;
-      case DrmHwcColorSpace::kItuRec601:
-        std::tie(color_encoding,
-                 ret) = color_encoding_propery_
-                            .GetEnumValueWithName("ITU-R BT.601 YCbCr");
-        break;
-      case DrmHwcColorSpace::kItuRec2020:
-        std::tie(color_encoding,
-                 ret) = color_encoding_propery_
-                            .GetEnumValueWithName("ITU-R BT.2020 YCbCr");
-        break;
-      default:
-        break;
-    }
-  }
-
-  switch (layer.sample_range) {
-    case DrmHwcSampleRange::kFullRange:
-      std::tie(color_range, ret) = color_range_property_.GetEnumValueWithName(
-          "YCbCr full range");
-      break;
-    case DrmHwcSampleRange::kLimitedRange:
-      std::tie(color_range, ret) = color_range_property_.GetEnumValueWithName(
-          "YCbCr limited range");
-      break;
-    default:
-      break;
-  }
-
   if (!crtc_property_.AtomicSet(pset, crtc_id) ||
       !fb_property_.AtomicSet(pset, fb_id) ||
       !crtc_x_property_.AtomicSet(pset, display_frame.left) ||
@@ -408,17 +401,18 @@
     }
   }
 
-  if (color_encoding_propery_ && color_encoding != UINT64_MAX) {
-    if (!color_encoding_propery_.AtomicSet(pset, color_encoding)) {
-      return -EINVAL;
-    }
+  if (color_encoding_enum_map_.count(layer.color_space) != 0 &&
+      !color_encoding_propery_
+           .AtomicSet(pset, color_encoding_enum_map_[layer.color_space])) {
+    return -EINVAL;
   }
 
-  if (color_range_property_ && color_range != UINT64_MAX) {
-    if (!color_range_property_.AtomicSet(pset, color_range)) {
-      return -EINVAL;
-    }
+  if (color_range_enum_map_.count(layer.sample_range) != 0 &&
+      !color_range_property_
+           .AtomicSet(pset, color_range_enum_map_[layer.sample_range])) {
+    return -EINVAL;
   }
+
   return 0;
 }
 
diff --git a/drm/DrmPlane.h b/drm/DrmPlane.h
index 6c2d041..ee619b8 100644
--- a/drm/DrmPlane.h
+++ b/drm/DrmPlane.h
@@ -80,6 +80,9 @@
   DrmProperty in_fence_fd_property_;
   DrmProperty color_encoding_propery_;
   DrmProperty color_range_property_;
+
+  std::map<DrmHwcColorSpace, uint64_t> color_encoding_enum_map_;
+  std::map<DrmHwcSampleRange, uint64_t> color_range_enum_map_;
 };
 }  // namespace android