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 {
diff --git a/libqdmetadata/Android.bp b/libqdmetadata/Android.bp
index 7621800..3cd036b 100644
--- a/libqdmetadata/Android.bp
+++ b/libqdmetadata/Android.bp
@@ -13,6 +13,7 @@
         "libutils",
         "libhidlbase",
         "libgralloc.qti",
+        "libgralloctypes",
     ],
     header_libs: ["libhardware_headers", "display_intf_headers"],
     srcs: ["qdMetaData.cpp", "qd_utils.cpp"],
diff --git a/libqdmetadata/qdMetaData.cpp b/libqdmetadata/qdMetaData.cpp
index a3141f0..0fa24ca 100644
--- a/libqdmetadata/qdMetaData.cpp
+++ b/libqdmetadata/qdMetaData.cpp
@@ -32,6 +32,7 @@
 #include <QtiGrallocPriv.h>
 #include <errno.h>
 #include <gralloc_priv.h>
+#include <gralloctypes/Gralloc4.h>
 #include <log/log.h>
 #include <string.h>
 #include <sys/mman.h>
@@ -96,18 +97,121 @@
 
   return 0;
 }
+
+static bool getGralloc4Array(MetaData_t *metadata, int32_t paramType) {
+  switch (paramType) {
+    case SET_VT_TIMESTAMP:
+      return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_VT_TIMESTAMP)];
+    case COLOR_METADATA:
+      return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_COLOR_METADATA)];
+    case PP_PARAM_INTERLACED:
+      return metadata
+          ->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_PP_PARAM_INTERLACED)];
+    case SET_VIDEO_PERF_MODE:
+      return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_VIDEO_PERF_MODE)];
+    case SET_GRAPHICS_METADATA:
+      return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_GRAPHICS_METADATA)];
+    case SET_UBWC_CR_STATS_INFO:
+      return metadata
+          ->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_UBWC_CR_STATS_INFO)];
+    case UPDATE_BUFFER_GEOMETRY:
+      return metadata->isStandardMetadataSet[GET_STANDARD_METADATA_STATUS_INDEX(
+          ::android::gralloc4::MetadataType_Crop.value)];
+    case UPDATE_REFRESH_RATE:
+      return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_REFRESH_RATE)];
+    case UPDATE_COLOR_SPACE:
+      return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_COLOR_METADATA)];
+    case MAP_SECURE_BUFFER:
+      return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_MAP_SECURE_BUFFER)];
+    case LINEAR_FORMAT:
+      return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_LINEAR_FORMAT)];
+    case SET_SINGLE_BUFFER_MODE:
+      return metadata
+          ->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_SINGLE_BUFFER_MODE)];
+    case SET_CVP_METADATA:
+      return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_CVP_METADATA)];
+    case SET_VIDEO_HISTOGRAM_STATS:
+      return metadata
+          ->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_VIDEO_HISTOGRAM_STATS)];
+    default:
+      ALOGE("paramType %d not supported", paramType);
+      return false;
+  }
+}
+
+static void setGralloc4Array(MetaData_t *metadata, int32_t paramType, bool isSet) {
+  switch (paramType) {
+    case SET_VT_TIMESTAMP:
+      metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_VT_TIMESTAMP)] = isSet;
+      break;
+    case COLOR_METADATA:
+      metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_COLOR_METADATA)] = isSet;
+      break;
+    case PP_PARAM_INTERLACED:
+      metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_PP_PARAM_INTERLACED)] =
+          isSet;
+      break;
+    case SET_VIDEO_PERF_MODE:
+      metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_VIDEO_PERF_MODE)] = isSet;
+      break;
+    case SET_GRAPHICS_METADATA:
+      metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_GRAPHICS_METADATA)] =
+          isSet;
+      break;
+    case SET_UBWC_CR_STATS_INFO:
+      metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_UBWC_CR_STATS_INFO)] =
+          isSet;
+      break;
+    case UPDATE_BUFFER_GEOMETRY:
+      metadata->isStandardMetadataSet[GET_STANDARD_METADATA_STATUS_INDEX(
+          ::android::gralloc4::MetadataType_Crop.value)] = isSet;
+      break;
+    case UPDATE_REFRESH_RATE:
+      metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_REFRESH_RATE)] = isSet;
+      break;
+    case UPDATE_COLOR_SPACE:
+      metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_COLOR_METADATA)] = isSet;
+      break;
+    case MAP_SECURE_BUFFER:
+      metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_MAP_SECURE_BUFFER)] =
+          isSet;
+      break;
+    case LINEAR_FORMAT:
+      metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_LINEAR_FORMAT)] = isSet;
+      break;
+    case SET_SINGLE_BUFFER_MODE:
+      metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_SINGLE_BUFFER_MODE)] =
+          isSet;
+      break;
+    case SET_CVP_METADATA:
+      metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_CVP_METADATA)] = isSet;
+      break;
+    case SET_VIDEO_HISTOGRAM_STATS:
+      metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_VIDEO_HISTOGRAM_STATS)] =
+          isSet;
+      break;
+    default:
+      ALOGE("paramType %d not supported in Gralloc4", paramType);
+  }
+}
+
 unsigned long getMetaDataSize() {
     return static_cast<unsigned long>(ROUND_UP_PAGESIZE(sizeof(MetaData_t)));
 }
 
+// Cannot add default argument to existing function
+unsigned long getMetaDataSizeWithReservedRegion(uint64_t reserved_size) {
+  return static_cast<unsigned long>(ROUND_UP_PAGESIZE(sizeof(MetaData_t) + reserved_size));
+}
+
 static int validateAndMap(private_handle_t* handle) {
     if (private_handle_t::validate(handle)) {
         ALOGE("%s: Private handle is invalid - handle:%p", __func__, handle);
         return -1;
     }
     if (handle->fd_metadata < 0) {
-        // Silently return, metadata cannot be used
-        return -1;
+      // Metadata cannot be used
+      return -1;
     }
 
     if (!handle->base_metadata) {
@@ -117,18 +221,35 @@
         if (base == reinterpret_cast<void*>(MAP_FAILED)) {
             ALOGE("%s: metadata mmap failed - handle:%p fd: %d err: %s",
                 __func__, handle, handle->fd_metadata, strerror(errno));
-
             return -1;
         }
         handle->base_metadata = (uintptr_t) base;
+        auto metadata = reinterpret_cast<MetaData_t *>(handle->base_metadata);
+        if (metadata->reservedSize) {
+          auto reserved_size = metadata->reservedSize;
+          munmap(reinterpret_cast<void *>(handle->base_metadata), getMetaDataSize());
+          handle->base_metadata = 0;
+          size = getMetaDataSizeWithReservedRegion(reserved_size);
+          void *new_base =
+              mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd_metadata, 0);
+          if (new_base == reinterpret_cast<void *>(MAP_FAILED)) {
+            ALOGE("%s: metadata mmap failed - handle:%p fd: %d err: %s", __func__, handle,
+                  handle->fd_metadata, strerror(errno));
+            return -1;
+          }
+          handle->base_metadata = (uintptr_t)new_base;
+        }
     }
     return 0;
 }
 
 static void unmapAndReset(private_handle_t *handle) {
     if (private_handle_t::validate(handle) == 0 && handle->base_metadata) {
-        munmap(reinterpret_cast<void *>(handle->base_metadata), getMetaDataSize());
-        handle->base_metadata = 0;
+      // If reservedSize is 0, the return value will be the same as getMetaDataSize
+      auto metadata = reinterpret_cast<MetaData_t *>(handle->base_metadata);
+      auto size = getMetaDataSizeWithReservedRegion(metadata->reservedSize);
+      munmap(reinterpret_cast<void *>(handle->base_metadata), size);
+      handle->base_metadata = 0;
     }
 }
 
@@ -147,26 +268,26 @@
         return -EINVAL;
     // If parameter is NULL reset the specific MetaData Key
     if (!param) {
-       data->operation &= ~paramType;
-       switch (paramType) {
-          case SET_VIDEO_PERF_MODE:
-             data->isVideoPerfMode = 0;
-             break;
-          case SET_CVP_METADATA:
-             data->cvpMetadata.size = 0;
-             break;
-          case SET_VIDEO_HISTOGRAM_STATS:
-             data->video_histogram_stats.stat_len = 0;
-             break;
-          default:
-             ALOGE("Unknown paramType %d", paramType);
-             break;
-       }
+      setGralloc4Array(data, paramType, false);
+      switch (paramType) {
+        case SET_VIDEO_PERF_MODE:
+          data->isVideoPerfMode = 0;
+          break;
+        case SET_CVP_METADATA:
+          data->cvpMetadata.size = 0;
+          break;
+        case SET_VIDEO_HISTOGRAM_STATS:
+          data->video_histogram_stats.stat_len = 0;
+          break;
+        default:
+          ALOGE("Unknown paramType %d", paramType);
+          break;
+      }
        // param unset
        return 0;
     }
 
-    data->operation |= paramType;
+    setGralloc4Array(data, paramType, true);
     switch (paramType) {
         case PP_PARAM_INTERLACED:
             data->interlaced = *((int32_t *)param);
@@ -206,12 +327,12 @@
             data->color = *((ColorMetaData *)param);
             break;
         case SET_UBWC_CR_STATS_INFO: {
-             struct UBWCStats* stats = (struct UBWCStats*)param;
-             int numelems = sizeof(data->ubwcCRStats) / sizeof(struct UBWCStats);
-             for (int i = 0; i < numelems; i++) {
-                  data->ubwcCRStats[i] = stats[i];
-             }
-              break;
+          struct UBWCStats *stats = (struct UBWCStats *)param;
+          int numelems = sizeof(data->ubwcCRStats) / sizeof(struct UBWCStats);
+          for (int i = 0; i < numelems; i++) {
+            data->ubwcCRStats[i] = stats[i];
+          }
+          break;
           }
         case SET_VIDEO_PERF_MODE:
             data->isVideoPerfMode = *((uint32_t *)param);
@@ -235,10 +356,10 @@
                  memcpy(data->cvpMetadata.reserved, cvpMetadata->reserved,
                         (8 * sizeof(uint32_t)));
              } else {
-                 data->operation &= ~(paramType);
-                 ALOGE("%s: cvp metadata length %d is more than max size %d",
-                     __func__, cvpMetadata->size, CVP_METADATA_SIZE);
-                 return -EINVAL;
+               setGralloc4Array(data, paramType, false);
+               ALOGE("%s: cvp metadata length %d is more than max size %d", __func__,
+                     cvpMetadata->size, CVP_METADATA_SIZE);
+               return -EINVAL;
              }
              break;
         }
@@ -254,10 +375,10 @@
                 data->video_histogram_stats.decode_width = vidstats->decode_width;
                 data->video_histogram_stats.decode_height = vidstats->decode_height;
             } else {
-                 data->operation &= ~(paramType);
-                 ALOGE("%s: video stats length %u is more than max size %u",
-                     __func__, vidstats->stat_len, VIDEO_HISTOGRAM_STATS_SIZE);
-                 return -EINVAL;
+              setGralloc4Array(data, paramType, false);
+              ALOGE("%s: video stats length %u is more than max size %u", __func__,
+                    vidstats->stat_len, VIDEO_HISTOGRAM_STATS_SIZE);
+              return -EINVAL;
             }
             break;
          }
@@ -315,128 +436,98 @@
     if (param == nullptr)
         return ret;
 
+    if (!getGralloc4Array(data, paramType)) {
+      return ret;
+    }
+
+    ret = 0;
+
     switch (paramType) {
         case GET_PP_PARAM_INTERLACED:
-            if (data->operation & PP_PARAM_INTERLACED) {
-                *((int32_t *)param) = data->interlaced;
-                ret = 0;
-            }
-            break;
+          *((int32_t *)param) = data->interlaced;
+          break;
         case GET_BUFFER_GEOMETRY:
-            if (data->operation & UPDATE_BUFFER_GEOMETRY) {
-              *((BufferDim_t *)param) = {data->crop.right, data->crop.bottom};
-              ret = 0;
-            }
-            break;
+          *((BufferDim_t *)param) = {data->crop.right, data->crop.bottom};
+          break;
         case GET_REFRESH_RATE:
-            if (data->operation & UPDATE_REFRESH_RATE) {
-                *((float *)param) = data->refreshrate;
-                ret = 0;
-            }
-            break;
-        case GET_COLOR_SPACE:
-            if (data->operation & UPDATE_COLOR_SPACE) {
-              ColorSpace_t color_space;
-              if (!colorMetaDataToColorSpace(data->color, &color_space)) {
-                *((ColorSpace_t *)param) = color_space;
-                ret = 0;
-              }
-            }
-            break;
+          *((float *)param) = data->refreshrate;
+          break;
+        case GET_COLOR_SPACE: {
+          ColorSpace_t color_space;
+          if (!colorMetaDataToColorSpace(data->color, &color_space)) {
+            *((ColorSpace_t *)param) = color_space;
+          } else {
+            ret = -EINVAL;
+          }
+          break;
+        }
         case GET_MAP_SECURE_BUFFER:
-            if (data->operation & MAP_SECURE_BUFFER) {
-                *((int32_t *)param) = data->mapSecureBuffer;
-                ret = 0;
-            }
-            break;
+          *((int32_t *)param) = data->mapSecureBuffer;
+          break;
         case GET_S3D_FORMAT:
-            if (data->operation & S3D_FORMAT) {
-                *((uint32_t *)param) = data->s3dFormat;
-                ret = 0;
-            }
-            break;
+          *((uint32_t *)param) = data->s3dFormat;
+          break;
         case GET_LINEAR_FORMAT:
-            if (data->operation & LINEAR_FORMAT) {
-                *((uint32_t *)param) = data->linearFormat;
-                ret = 0;
-            }
-            break;
+          *((uint32_t *)param) = data->linearFormat;
+          break;
         case GET_SINGLE_BUFFER_MODE:
-            if (data->operation & SET_SINGLE_BUFFER_MODE) {
-                *((uint32_t *)param) = data->isSingleBufferMode;
-                ret = 0;
-            }
-            break;
+          *((uint32_t *)param) = data->isSingleBufferMode;
+          break;
         case GET_VT_TIMESTAMP:
-            if (data->operation & SET_VT_TIMESTAMP) {
-                *((uint64_t *)param) = data->vtTimeStamp;
-                ret = 0;
-            }
-            break;
+          *((uint64_t *)param) = data->vtTimeStamp;
+          break;
         case GET_COLOR_METADATA:
-            if (data->operation & COLOR_METADATA) {
-                *((ColorMetaData *)param) = data->color;
-                ret = 0;
-            }
-            break;
-        case GET_UBWC_CR_STATS_INFO:
-            if (data->operation & SET_UBWC_CR_STATS_INFO) {
-                struct UBWCStats* stats = (struct UBWCStats*)param;
-                int numelems = sizeof(data->ubwcCRStats) / sizeof(struct UBWCStats);
-                for (int i = 0; i < numelems; i++) {
-                    stats[i] = data->ubwcCRStats[i];
-                }
-                ret = 0;
-            }
-            break;
+          *((ColorMetaData *)param) = data->color;
+          break;
+        case GET_UBWC_CR_STATS_INFO: {
+          struct UBWCStats *stats = (struct UBWCStats *)param;
+          int numelems = sizeof(data->ubwcCRStats) / sizeof(struct UBWCStats);
+          for (int i = 0; i < numelems; i++) {
+            stats[i] = data->ubwcCRStats[i];
+          }
+          break;
+        }
         case GET_VIDEO_PERF_MODE:
-            if (data->operation & SET_VIDEO_PERF_MODE) {
-                *((uint32_t *)param) = data->isVideoPerfMode;
-                ret = 0;
-            }
-            break;
+          *((uint32_t *)param) = data->isVideoPerfMode;
+          break;
         case GET_GRAPHICS_METADATA:
-            if (data->operation & SET_GRAPHICS_METADATA) {
-                memcpy(param, data->graphics_metadata.data, sizeof(data->graphics_metadata.data));
-                ret = 0;
-            }
-            break;
-        case GET_CVP_METADATA:
-            if (data->operation & SET_CVP_METADATA) {
-                struct CVPMetadata *cvpMetadata = (struct CVPMetadata *)param;
-                cvpMetadata->size = 0;
-                if (data->cvpMetadata.size <= CVP_METADATA_SIZE) {
-                    cvpMetadata->size = data->cvpMetadata.size;
-                    memcpy(cvpMetadata->payload, data->cvpMetadata.payload,
-                           data->cvpMetadata.size);
-                    cvpMetadata->capture_frame_rate = data->cvpMetadata.capture_frame_rate;
-                    cvpMetadata->cvp_frame_rate = data->cvpMetadata.cvp_frame_rate;
-                    cvpMetadata->flags = data->cvpMetadata.flags;
-                    memcpy(cvpMetadata->reserved, data->cvpMetadata.reserved,
-                           (8 * sizeof(uint32_t)));
-                    ret = 0;
-                }
-            }
-            break;
-        case GET_VIDEO_HISTOGRAM_STATS:
-            if (data->operation & SET_VIDEO_HISTOGRAM_STATS) {
-                struct VideoHistogramMetadata *vidstats = (struct VideoHistogramMetadata *)param;
-                vidstats->stat_len = 0;
-                if (data->video_histogram_stats.stat_len <= VIDEO_HISTOGRAM_STATS_SIZE) {
-                    memcpy(vidstats->stats_info,
-                        data->video_histogram_stats.stats_info, VIDEO_HISTOGRAM_STATS_SIZE);
-                    vidstats->stat_len = data->video_histogram_stats.stat_len;
-                    vidstats->frame_type = data->video_histogram_stats.frame_type;
-                    vidstats->display_width = data->video_histogram_stats.display_width;
-                    vidstats->display_height = data->video_histogram_stats.display_height;
-                    vidstats->decode_width = data->video_histogram_stats.decode_width;
-                    vidstats->decode_height = data->video_histogram_stats.decode_height;
-                    ret = 0;
-                }
-            }
-            break;
+          memcpy(param, data->graphics_metadata.data, sizeof(data->graphics_metadata.data));
+          break;
+        case GET_CVP_METADATA: {
+          struct CVPMetadata *cvpMetadata = (struct CVPMetadata *)param;
+          cvpMetadata->size = 0;
+          if (data->cvpMetadata.size <= CVP_METADATA_SIZE) {
+            cvpMetadata->size = data->cvpMetadata.size;
+            memcpy(cvpMetadata->payload, data->cvpMetadata.payload, data->cvpMetadata.size);
+            cvpMetadata->capture_frame_rate = data->cvpMetadata.capture_frame_rate;
+            cvpMetadata->cvp_frame_rate = data->cvpMetadata.cvp_frame_rate;
+            cvpMetadata->flags = data->cvpMetadata.flags;
+            memcpy(cvpMetadata->reserved, data->cvpMetadata.reserved, (8 * sizeof(uint32_t)));
+          } else {
+            ret = -EINVAL;
+          }
+          break;
+        }
+        case GET_VIDEO_HISTOGRAM_STATS: {
+          struct VideoHistogramMetadata *vidstats = (struct VideoHistogramMetadata *)param;
+          vidstats->stat_len = 0;
+          if (data->video_histogram_stats.stat_len <= VIDEO_HISTOGRAM_STATS_SIZE) {
+            memcpy(vidstats->stats_info, data->video_histogram_stats.stats_info,
+                   VIDEO_HISTOGRAM_STATS_SIZE);
+            vidstats->stat_len = data->video_histogram_stats.stat_len;
+            vidstats->frame_type = data->video_histogram_stats.frame_type;
+            vidstats->display_width = data->video_histogram_stats.display_width;
+            vidstats->display_height = data->video_histogram_stats.display_height;
+            vidstats->decode_width = data->video_histogram_stats.decode_width;
+            vidstats->decode_height = data->video_histogram_stats.decode_height;
+          } else {
+            ret = -EINVAL;
+          }
+          break;
+        }
         default:
             ALOGE("Unknown paramType %d", paramType);
+            ret = -EINVAL;
             break;
     }
     return ret;