hwc2: Fix reading HDR10+ from gralloc metadata

- One variable per_frame_hdr_metadata_ was used to track whether to
  read or avoid from gralloc color metadatas. This variable was set from
  both SetLayerPerFrameMetadata and SetLayerPerFrameMetadataBlobs.
- Since SF calls only SetLayerPerFrameMetadata (w/o support for HDR10+
  thru FW), it avoided reading HDR10+ data from gralloc metadata and
  hence HDR10+ dynamic metadata was not updatedi.
- HDR10+ blob data from FW API has higher priority over gralloc
  metadata, once the FW API sets HDR10+ avoid reading it from gralloc
  metadata as Video HAL doesn't set both.
- Composer must read color metadata from gralloc metadata unless SF sets
  all PerFrameMetaDataBlobs for HDR10+.

Change-Id: I3e288f2947778cad5f95264dd4926ed596d24b96
Crs-fixed: 2581361
diff --git a/sdm/libs/hwc2/hwc_layers.cpp b/sdm/libs/hwc2/hwc_layers.cpp
index a8c1926..aed84b1 100644
--- a/sdm/libs/hwc2/hwc_layers.cpp
+++ b/sdm/libs/hwc2/hwc_layers.cpp
@@ -601,7 +601,6 @@
   }
   if ((!SameConfig(&old_mastering_display, &mastering_display, UINT32(sizeof(MasteringDisplay)))) ||
        (!SameConfig(&old_content_light, &content_light, UINT32(sizeof(ContentLightLevel))))) {
-    per_frame_hdr_metadata_ = true;
     layer_->update_mask.set(kMetadataUpdate);
     geometry_changes_ |= kDataspace;
   }
@@ -625,7 +624,6 @@
           DLOGE("Size of HDR10_PLUS_SEI = %d", sizes[i]);
           return HWC2::Error::BadParameter;
         }
-        per_frame_hdr_metadata_ = false;
         // if dynamic metadata changes, store and set needs validate
         if (!SameConfig(static_cast<const uint8_t*>(color_metadata.dynamicMetaDataPayload),
                         metadata, sizes[i])) {
@@ -633,9 +631,11 @@
           color_metadata.dynamicMetaDataValid = true;
           color_metadata.dynamicMetaDataLen = sizes[i];
           std::memcpy(color_metadata.dynamicMetaDataPayload, metadata, sizes[i]);
-          per_frame_hdr_metadata_ = true;
           layer_->update_mask.set(kMetadataUpdate);
         }
+        // once blob data is set by FW, we should not read from gralloc metadata as video HAL
+        // is setting thru gralloc metadata OR native window API (TODO(user): Revisit).
+        per_frame_hdr_metadata_blob_ = true;
         break;
       default:
         DLOGW("Invalid key = %d", keys[i]);
@@ -963,7 +963,9 @@
      use_color_metadata = true;
   }
 
-  if (use_color_metadata && !per_frame_hdr_metadata_) {
+  // Since client has set PerFrameMetadatablobs its dataspace and hdr10+ data will be updated
+  // so we can skip reading from ColorMetaData.
+  if (use_color_metadata && !per_frame_hdr_metadata_blob_) {
     ColorMetaData new_metadata = {};
     if (sdm::SetCSC(handle, &new_metadata) == kErrorNone) {
       // If dataspace is KNOWN, overwrite the gralloc metadata CSC using the previously derived CSC
@@ -978,6 +980,9 @@
           (layer_buffer->color_metadata.range != new_metadata.range)) {
         layer_->update_mask.set(kMetadataUpdate);
       }
+      DLOGV_IF(kTagClient, "Layer id =%lld Dynamic Metadata valid = %d size = %d", id_,
+               new_metadata.dynamicMetaDataValid,
+               new_metadata.dynamicMetaDataLen);
       if (new_metadata.dynamicMetaDataValid &&
           !SameConfig(layer_buffer->color_metadata.dynamicMetaDataPayload,
           new_metadata.dynamicMetaDataPayload, HDR_DYNAMIC_META_DATA_SZ)) {
diff --git a/sdm/libs/hwc2/hwc_layers.h b/sdm/libs/hwc2/hwc_layers.h
index aa77e60..8fc4afd 100644
--- a/sdm/libs/hwc2/hwc_layers.h
+++ b/sdm/libs/hwc2/hwc_layers.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014-2019, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2014-2020, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright 2015 The Android Open Source Project
@@ -139,7 +139,7 @@
   bool non_integral_source_crop_ = false;
   bool has_metadata_refresh_rate_ = false;
   bool buffer_flipped_ = false;
-  bool per_frame_hdr_metadata_ = false;  // used to track if perframe metadata and blob is set.
+  bool per_frame_hdr_metadata_blob_ = false;  // used to track if perframe metadata blob is set
 
   // Composition requested by client(SF) Original
   HWC2::Composition client_requested_orig_ = HWC2::Composition::Device;