hwc2: Copy UBWC Compression stats from Metadata to layer_buffer

CRs-Fixed: 2073410

Change-Id: Ie7b84686a54ecdfec2fd4a885e1a294b51c6397e
diff --git a/sdm/include/core/layer_buffer.h b/sdm/include/core/layer_buffer.h
index c86e020..c0e3305 100644
--- a/sdm/include/core/layer_buffer.h
+++ b/sdm/include/core/layer_buffer.h
@@ -31,11 +31,16 @@
 
 #include <stdint.h>
 #include <color_metadata.h>
+#include <utility>
+#include <vector>
 
 #include "sdm_types.h"
 
 namespace sdm {
 
+#define NUM_UBWC_CR_STATS_LAYERS 2
+typedef std::vector<std::pair<int, int>> UbwcCrStatsVector;
+
 /*! @brief This enum represents display layer inverse gamma correction (IGC) types.
 
   @sa Layer
@@ -268,6 +273,9 @@
                                 //!< could be modified by both client and SDM.
   uint64_t buffer_id __attribute__((aligned(8))) = 0;
                                 //!< Specifies the buffer id.
+  UbwcCrStatsVector  ubwc_crstats[NUM_UBWC_CR_STATS_LAYERS] = {};
+                                //! < UBWC Compression ratio,stats. Stored as a vector of pair of
+                                //! of (tile size, #of tiles)
 };
 
 // This enum represents buffer layout types.
diff --git a/sdm/libs/hwc2/hwc_layers.cpp b/sdm/libs/hwc2/hwc_layers.cpp
index 015dc75..f485ac6 100644
--- a/sdm/libs/hwc2/hwc_layers.cpp
+++ b/sdm/libs/hwc2/hwc_layers.cpp
@@ -18,6 +18,7 @@
  */
 
 #include <stdint.h>
+#include <utility>
 #include <qdMetaData.h>
 
 #include "hwc_layers.h"
@@ -549,6 +550,27 @@
   return sdm_s3d_format;
 }
 
+void HWCLayer::GetUBWCStatsFromMetaData(UBWCStats *cr_stats, UbwcCrStatsVector *cr_vec) {
+  // TODO(user): Check if we can use UBWCStats directly
+  // in layer_buffer or copy directly to Vector
+  if (cr_stats->bDataValid) {
+    switch (cr_stats->version) {
+      case UBWC_2_0:
+        cr_vec->push_back(std::make_pair(32, cr_stats->ubwc_stats.nCRStatsTile32));
+        cr_vec->push_back(std::make_pair(64, cr_stats->ubwc_stats.nCRStatsTile64));
+        cr_vec->push_back(std::make_pair(96, cr_stats->ubwc_stats.nCRStatsTile96));
+        cr_vec->push_back(std::make_pair(128, cr_stats->ubwc_stats.nCRStatsTile128));
+        cr_vec->push_back(std::make_pair(160, cr_stats->ubwc_stats.nCRStatsTile160));
+        cr_vec->push_back(std::make_pair(192, cr_stats->ubwc_stats.nCRStatsTile192));
+        cr_vec->push_back(std::make_pair(256, cr_stats->ubwc_stats.nCRStatsTile256));
+        break;
+      default:
+        DLOGW("Invalid UBWC Version %d", cr_stats->version);
+        break;
+    }  // switch(cr_stats->version)
+  }  // if (cr_stats->bDatvalid)
+}
+
 DisplayError HWCLayer::SetMetaData(const private_handle_t *pvt_handle, Layer *layer) {
   LayerBuffer *layer_buffer = &layer->input_buffer;
   bool use_color_metadata = true;
@@ -592,6 +614,18 @@
     layer_buffer->s3d_format = GetS3DFormat(s3d);
   }
 
+  // Check if metadata is set
+  struct UBWCStats cr_stats[NUM_UBWC_CR_STATS_LAYERS] = {};
+
+  for (int i = 0; i < NUM_UBWC_CR_STATS_LAYERS; i++) {
+    layer_buffer->ubwc_crstats[i].clear();
+  }
+
+  if (getMetaData(handle, GET_UBWC_CR_STATS_INFO, cr_stats) == 0) {
+  // Only copy top layer for now as only top field for interlaced is used
+    GetUBWCStatsFromMetaData(&cr_stats[0], &(layer_buffer->ubwc_crstats[0]));
+  }  // if (getMetaData)
+
   return kErrorNone;
 }
 
diff --git a/sdm/libs/hwc2/hwc_layers.h b/sdm/libs/hwc2/hwc_layers.h
index b42699d..c566655 100644
--- a/sdm/libs/hwc2/hwc_layers.h
+++ b/sdm/libs/hwc2/hwc_layers.h
@@ -112,6 +112,7 @@
   uint32_t GetUint32Color(const hwc_color_t &source);
   LayerBufferFormat GetSDMFormat(const int32_t &source, const int flags);
   LayerBufferS3DFormat GetS3DFormat(uint32_t s3d_format);
+  void GetUBWCStatsFromMetaData(UBWCStats *cr_stats, UbwcCrStatsVector *cr_vec);
   DisplayError SetMetaData(const private_handle_t *pvt_handle, Layer *layer);
   DisplayError SetIGC(IGC_t source, LayerIGC *target);
   uint32_t RoundToStandardFPS(float fps);