display: Add reserved size and status check to MetaData_t

* Move from fixed array reserved region to variable reserved
  region size
* Add bool arrays to indicate whether metadata has been set using
  Gralloc4 indices
* Replace metadata->operation with bool arrays for
  gralloc4/qdMetaData compatibility

CRs-Fixed: 2730848
Change-Id: Id52aac70c23ee4b39db185861f951bb909831afb
diff --git a/gralloc/Android.bp b/gralloc/Android.bp
index 4882e79..c7339c2 100644
--- a/gralloc/Android.bp
+++ b/gralloc/Android.bp
@@ -13,7 +13,7 @@
          "libgralloctypes",
          "libhidlbase",
          "libhardware",
-         "android.hardware.graphics.mapper@4.0"
+         "android.hardware.graphics.mapper@4.0",
     ],
 
     srcs: ["QtiGralloc.cpp"],
diff --git a/gralloc/QtiGralloc.cpp b/gralloc/QtiGralloc.cpp
index e999063..aab5754 100644
--- a/gralloc/QtiGralloc.cpp
+++ b/gralloc/QtiGralloc.cpp
@@ -38,6 +38,24 @@
   static sp<IMapper> mapper = IMapper::getService();
   return mapper;
 }
+
+Error decodeMetadataState(hidl_vec<uint8_t> &in, bool *out) {
+  if (!in.size() || !out) {
+    return Error::BAD_VALUE;
+  }
+  memcpy(out, in.data(), METADATA_SET_SIZE);
+  return Error::NONE;
+}
+
+Error encodeMetadataState(bool *in, hidl_vec<uint8_t> *out) {
+  if (!in || !out) {
+    return Error::BAD_VALUE;
+  }
+  out->resize(sizeof(bool) * METADATA_SET_SIZE);
+  memcpy(out->data(), in, sizeof(bool) * METADATA_SET_SIZE);
+  return Error::NONE;
+}
+
 Error decodeColorMetadata(hidl_vec<uint8_t> &in, ColorMetaData *out) {
   if (!in.size() || !out) {
     return Error::BAD_VALUE;
@@ -180,6 +198,10 @@
       return MetadataType_AlignedWidthInPixels;
     case QTI_ALIGNED_HEIGHT_IN_PIXELS:
       return MetadataType_AlignedHeightInPixels;
+    case QTI_STANDARD_METADATA_STATUS:
+      return MetadataType_StandardMetadataStatus;
+    case QTI_VENDOR_METADATA_STATUS:
+      return MetadataType_VendorMetadataStatus;
     default:
       return MetadataType_Invalid;
   }
@@ -265,6 +287,10 @@
       err = static_cast<Error>(android::gralloc4::decodeUint32(
           qtigralloc::MetadataType_AlignedHeightInPixels, bytestream, (uint32_t *)param));
       break;
+    case QTI_STANDARD_METADATA_STATUS:
+    case QTI_VENDOR_METADATA_STATUS:
+      err = decodeMetadataState(bytestream, (bool *)param);
+      break;
     default:
       param = nullptr;
       return Error::UNSUPPORTED;
@@ -339,4 +365,25 @@
   return mapper->set((void *)buffer, metadata_type, bytestream);
 }
 
+int getMetadataState(void *buffer, uint32_t type) {
+  bool metadata_set[METADATA_SET_SIZE];
+  Error err;
+  if (IS_VENDOR_METADATA_TYPE(type)) {
+    err = get(buffer, QTI_VENDOR_METADATA_STATUS, &metadata_set);
+  } else {
+    err = get(buffer, QTI_STANDARD_METADATA_STATUS, &metadata_set);
+  }
+
+  if (err != Error::NONE) {
+    ALOGE("Unable to get metadata state");
+    return -1;
+  }
+
+  if (IS_VENDOR_METADATA_TYPE(type)) {
+    return metadata_set[GET_VENDOR_METADATA_STATUS_INDEX(type)];
+  } else {
+    return metadata_set[GET_STANDARD_METADATA_STATUS_INDEX(type)];
+  }
+}
+
 }  // namespace qtigralloc
diff --git a/gralloc/QtiGralloc.h b/gralloc/QtiGralloc.h
index f0392f9..4be74e2 100644
--- a/gralloc/QtiGralloc.h
+++ b/gralloc/QtiGralloc.h
@@ -67,6 +67,7 @@
 Error get(void *buffer, uint32_t type, void *param);
 Error set(void *buffer, uint32_t type, void *param);
 MetadataType getMetadataType(uint32_t in);
+int getMetadataState(void *buffer, uint32_t type);
 
 static const MetadataType MetadataType_VTTimestamp = {VENDOR_QTI, QTI_VT_TIMESTAMP};
 
@@ -102,6 +103,12 @@
 static const MetadataType MetadataType_AlignedHeightInPixels = {VENDOR_QTI,
                                                                 QTI_ALIGNED_HEIGHT_IN_PIXELS};
 
+static const MetadataType MetadataType_StandardMetadataStatus = {VENDOR_QTI,
+                                                                 QTI_STANDARD_METADATA_STATUS};
+
+static const MetadataType MetadataType_VendorMetadataStatus = {VENDOR_QTI,
+                                                               QTI_VENDOR_METADATA_STATUS};
+
 // 0 is also used as invalid value in standard metadata
 static const MetadataType MetadataType_Invalid = {VENDOR_QTI, 0};
 
@@ -115,6 +122,8 @@
 static const aidl::android::hardware::graphics::common::ExtendableType
     PlaneLayoutComponentType_Meta = {VENDOR_QTI, PLANE_COMPONENT_TYPE_META};
 
+Error decodeMetadataState(hidl_vec<uint8_t> &in, bool *out);
+Error encodeMetadataState(bool *in, hidl_vec<uint8_t> *out);
 Error decodeColorMetadata(hidl_vec<uint8_t> &in, ColorMetaData *out);
 Error encodeColorMetadata(ColorMetaData &in, hidl_vec<uint8_t> *out);
 Error decodeGraphicsMetadata(hidl_vec<uint8_t> &in, GraphicsMetadata *out);
@@ -127,7 +136,6 @@
 Error encodeCVPMetadata(CVPMetadata &in, hidl_vec<uint8_t> *out);
 Error decodeVideoHistogramMetadata(hidl_vec<uint8_t> &in, VideoHistogramMetadata *out);
 Error encodeVideoHistogramMetadata(VideoHistogramMetadata &in, hidl_vec<uint8_t> *out);
-
 }  // namespace qtigralloc
 
 #endif  //__QTIGRALLOC_H__
diff --git a/gralloc/QtiGrallocMetadata.h b/gralloc/QtiGrallocMetadata.h
index b8e779b..cc134a8 100644
--- a/gralloc/QtiGrallocMetadata.h
+++ b/gralloc/QtiGrallocMetadata.h
@@ -52,6 +52,9 @@
 #define QTI_ALIGNED_WIDTH_IN_PIXELS 10014
 // Height of the allocated buffer in pixels
 #define QTI_ALIGNED_HEIGHT_IN_PIXELS 10015
+// Indicates whether metadata is using default value or has been set
+#define QTI_STANDARD_METADATA_STATUS 10016
+#define QTI_VENDOR_METADATA_STATUS 10017
 
 // Used to indicate to framework that internal definitions are used instead
 #define COMPRESSION_QTI_UBWC 20001
@@ -155,4 +158,11 @@
   uint8_t data[RESERVED_REGION_SIZE];
 } ReservedRegion;
 
+#define METADATA_SET_SIZE 512
+
+#define IS_VENDOR_METADATA_TYPE(x) (x >= QTI_VT_TIMESTAMP)
+
+#define GET_STANDARD_METADATA_STATUS_INDEX(x) x
+#define GET_VENDOR_METADATA_STATUS_INDEX(x) x - QTI_VT_TIMESTAMP
+
 #endif  //__QTIGRALLOCMETADATA_H__
diff --git a/gralloc/QtiGrallocPriv.h b/gralloc/QtiGrallocPriv.h
index ee0c6a4..3874ecd 100644
--- a/gralloc/QtiGrallocPriv.h
+++ b/gralloc/QtiGrallocPriv.h
@@ -52,6 +52,8 @@
  *
  */
 
+#define METADATA_V2
+
 // TODO: MetaData_t should be in qtigralloc namespace
 struct MetaData_t {
   int32_t operation;
@@ -95,6 +97,9 @@
   int32_t blendMode;
   char name[MAX_NAME_LEN];
   ReservedRegion reservedRegion;
+  bool isStandardMetadataSet[METADATA_SET_SIZE];
+  bool isVendorMetadataSet[METADATA_SET_SIZE];
+  uint64_t reservedSize;
 };
 
 namespace qtigralloc {