sdm: Set Color Space value for MDP to configure CSC matrix

Pass the Color Space value of the source to MDP driver, to configure
CSC matrix correctly.

Change-Id: I7ac7b9ef64f20908ceb63dd6148cf2d200f96572
diff --git a/sdm/include/core/layer_stack.h b/sdm/include/core/layer_stack.h
index 17e866a..37182cc 100644
--- a/sdm/include/core/layer_stack.h
+++ b/sdm/include/core/layer_stack.h
@@ -74,6 +74,12 @@
                           //!< Only one layer shall be marked as target buffer by the caller.
 };
 
+enum LayerColorSpace {
+  kLimitedRange601,       //!< 601 limited range color space
+  kFullRange601,          //!< 601 full range color space
+  kLimitedRange709,       //!< 709 limited range color space
+};
+
 /*! @brief This structure defines rotation and flip values for a display layer.
 
   @sa Layer
@@ -213,8 +219,10 @@
 
   uint32_t frame_rate;              //!< Rate at which frames are being updated for this layer.
 
+  LayerColorSpace color_space;      //!< Color Space of the layer
+
   Layer() : input_buffer(NULL), composition(kCompositionGPU), blending(kBlendingOpaque),
-            plane_alpha(0), frame_rate(0) { }
+            plane_alpha(0), frame_rate(0), color_space(kLimitedRange601) { }
 };
 
 /*! @brief This structure defines a layer stack that contains layers which need to be composed and
diff --git a/sdm/libs/core/fb/hw_device.cpp b/sdm/libs/core/fb/hw_device.cpp
index 31b3da9..380a058 100644
--- a/sdm/libs/core/fb/hw_device.cpp
+++ b/sdm/libs/core/fb/hw_device.cpp
@@ -250,6 +250,7 @@
         SetRect(pipe_info->src_roi, &mdp_layer.src_rect);
         SetRect(pipe_info->dst_roi, &mdp_layer.dst_rect);
         SetMDPFlags(layer, is_rotator_used, &mdp_layer.flags);
+        SetColorSpace(layer.color_space, &mdp_layer.color_space);
 
         if (pipe_info->scale_data.enable_pixel_ext) {
           if ((mdp_layer.flags & MDP_LAYER_DEINTERLACE) && (layer.transform.rotation == 90.0f)) {
@@ -929,5 +930,13 @@
   }
 }
 
+void HWDevice::SetColorSpace(LayerColorSpace source, mdp_color_space *color_space) {
+  switch (source) {
+  case kLimitedRange601:    *color_space = MDP_CSC_ITU_R_601;      break;
+  case kFullRange601:       *color_space = MDP_CSC_ITU_R_601_FR;   break;
+  case kLimitedRange709:    *color_space = MDP_CSC_ITU_R_709;      break;
+  }
+}
+
 }  // namespace sdm
 
diff --git a/sdm/libs/core/fb/hw_device.h b/sdm/libs/core/fb/hw_device.h
index 7d617f0..35a1c1d 100644
--- a/sdm/libs/core/fb/hw_device.h
+++ b/sdm/libs/core/fb/hw_device.h
@@ -92,6 +92,7 @@
   mdp_scale_data* GetScaleDataRef(uint32_t index) { return &scale_data_[index]; }
   void SetHWScaleData(const ScaleData &scale, uint32_t index);
   void ResetDisplayParams();
+  void SetColorSpace(LayerColorSpace source, mdp_color_space *color_space);
 
   bool EnableHotPlugDetection(int enable);
 
diff --git a/sdm/libs/hwc/hwc_display.cpp b/sdm/libs/hwc/hwc_display.cpp
index 24bfcaa..193e9b2 100644
--- a/sdm/libs/hwc/hwc_display.cpp
+++ b/sdm/libs/hwc/hwc_display.cpp
@@ -32,7 +32,6 @@
 #include <gralloc_priv.h>
 #include <gr.h>
 #include <utils/constants.h>
-#include <qdMetaData.h>
 #include <sync/sync.h>
 #include <cutils/properties.h>
 
@@ -350,13 +349,9 @@
     }
 
     layer->frame_rate = fps;
-    MetaData_t *meta_data = reinterpret_cast<MetaData_t *>(pvt_handle->base_metadata);
-    if (meta_data && meta_data->operation & UPDATE_REFRESH_RATE) {
-      layer->frame_rate = RoundToStandardFPS(meta_data->refreshrate);
-    }
-
-    if (meta_data && meta_data->operation == PP_PARAM_INTERLACED && meta_data->interlaced) {
-      layer_buffer->flags.interlace = true;
+    const MetaData_t *meta_data = reinterpret_cast<MetaData_t *>(pvt_handle->base_metadata);
+    if (meta_data && (SetMetaData(*meta_data, layer) != kErrorNone)) {
+      return -EINVAL;
     }
 
     if (pvt_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY) {
@@ -1051,5 +1046,36 @@
 void HWCDisplay::ApplyScanAdjustment(hwc_rect_t *display_frame) {
 }
 
+DisplayError HWCDisplay::SetColorSpace(const ColorSpace_t source, LayerColorSpace *target) {
+  switch (source) {
+  case ITU_R_601:      *target = kLimitedRange601;   break;
+  case ITU_R_601_FR:   *target = kFullRange601;      break;
+  case ITU_R_709:      *target = kLimitedRange709;   break;
+  default:
+    DLOGE("Unsupported Color Space: %d", source);
+    return kErrorNotSupported;
+  }
+
+  return kErrorNone;
+}
+
+DisplayError HWCDisplay::SetMetaData(const MetaData_t &meta_data, Layer *layer) {
+  if (meta_data.operation & UPDATE_REFRESH_RATE) {
+    layer->frame_rate = RoundToStandardFPS(meta_data.refreshrate);
+  }
+
+  if ((meta_data.operation & PP_PARAM_INTERLACED) && meta_data.interlaced) {
+    layer->input_buffer->flags.interlace = true;
+  }
+
+  if (meta_data.operation & UPDATE_COLOR_SPACE) {
+    if (SetColorSpace(meta_data.colorSpace, &layer->color_space) != kErrorNone) {
+      return kErrorNotSupported;
+    }
+  }
+
+  return kErrorNone;
+}
+
 }  // namespace sdm
 
diff --git a/sdm/libs/hwc/hwc_display.h b/sdm/libs/hwc/hwc_display.h
index b20b00b..d69c490 100644
--- a/sdm/libs/hwc/hwc_display.h
+++ b/sdm/libs/hwc/hwc_display.h
@@ -27,6 +27,7 @@
 
 #include <hardware/hwcomposer.h>
 #include <core/core_interface.h>
+#include <qdMetaData.h>
 
 namespace sdm {
 
@@ -113,6 +114,8 @@
   void CloseAcquireFences(hwc_display_contents_1_t *content_list);
   uint32_t RoundToStandardFPS(uint32_t fps);
   virtual void ApplyScanAdjustment(hwc_rect_t *display_frame);
+  DisplayError SetColorSpace(const ColorSpace_t source, LayerColorSpace *target);
+  DisplayError SetMetaData(const MetaData_t &meta_data, Layer *layer);
 
   enum {
     INPUT_LAYER_DUMP,