gralloc: Add support for standard metadata type CROP.

CRs-Fixed: 2647933
Change-Id: Icf84b29aaeced4557d336868b8987cc47c9c27c7
diff --git a/gralloc/QtiMapper4.h b/gralloc/QtiMapper4.h
index a8b77bc..8354c36 100644
--- a/gralloc/QtiMapper4.h
+++ b/gralloc/QtiMapper4.h
@@ -249,6 +249,7 @@
       {android::gralloc4::MetadataType_Smpte2086, "", true, true},
       {android::gralloc4::MetadataType_Cta861_3, "", true, true},
       {android::gralloc4::MetadataType_Smpte2094_40, "", true, true},
+      {android::gralloc4::MetadataType_Crop, "", true, true},
       {qtigralloc::MetadataType_VTTimestamp, "VT Timestamp", true, true},
       {qtigralloc::MetadataType_ColorMetadata, "Color metadata", true, true},
       {qtigralloc::MetadataType_PPParamInterlaced, "Interlaced", true, true},
diff --git a/gralloc/gr_buf_mgr.cpp b/gralloc/gr_buf_mgr.cpp
index b6dc2a0..65b3864 100644
--- a/gralloc/gr_buf_mgr.cpp
+++ b/gralloc/gr_buf_mgr.cpp
@@ -330,8 +330,7 @@
   }
 }
 
-static Error getFormatLayout(private_handle_t *handle, Rect crop_rect,
-                             std::vector<PlaneLayout> *out) {
+static Error getFormatLayout(private_handle_t *handle, std::vector<PlaneLayout> *out) {
   std::vector<PlaneLayout> plane_info;
   int plane_count = 0;
   BufferInfo info(handle->unaligned_width, handle->unaligned_height, handle->format, handle->usage);
@@ -358,17 +357,8 @@
     plane_info[i].sampleIncrementInBits = static_cast<int64_t>(plane_layout[i].step * 8);
     plane_info[i].strideInBytes = static_cast<int64_t>(plane_layout[i].stride_bytes);
     plane_info[i].totalSizeInBytes = static_cast<int64_t>(plane_layout[i].size);
-    plane_info[i].widthInSamples =
-        handle->unaligned_width * 8 / plane_info[i].sampleIncrementInBits;
-    plane_info[i].heightInSamples =
-        handle->unaligned_height * 8 / plane_info[i].sampleIncrementInBits;
-#ifdef TARGET_USES_GRALLOC4
-    // TODO(tbalacha): This will be removed when standard metadata type CROP_RECTANGLE is added
-    plane_info[i].crop = crop_rect;
-#else
-    // Avoid unused parameter compiliation error
-    (void)crop_rect;
-#endif
+    plane_info[i].widthInSamples = handle->unaligned_width;
+    plane_info[i].heightInSamples = handle->unaligned_height;
   }
   *out = plane_info;
   return Error::NONE;
@@ -724,6 +714,12 @@
   metadata->name[descriptor.GetName().size()] = '\0';
 
   metadata->reservedRegion.size = static_cast<uint32_t>(descriptor.GetReservedSize());
+
+  metadata->crop.top = 0;
+  metadata->crop.left = 0;
+  metadata->crop.right = hnd->width;
+  metadata->crop.bottom = hnd->height;
+
   unmapAndReset(hnd);
 
   *handle = hnd;
@@ -879,9 +875,7 @@
       break;
     case (int64_t)StandardMetadataType::PLANE_LAYOUTS: {
       std::vector<PlaneLayout> plane_layouts;
-      Rect crop = {0, 0, 0, 0};
-      crop = {metadata->crop.left, metadata->crop.top, metadata->crop.right, metadata->crop.bottom};
-      getFormatLayout(handle, crop, &plane_layouts);
+      getFormatLayout(handle, &plane_layouts);
       android::gralloc4::encodePlaneLayouts(plane_layouts, out);
       break;
     }
@@ -939,6 +933,13 @@
       }
       break;
     }
+    case (int64_t)StandardMetadataType::CROP: {
+      // Crop is the same for all planes
+      std::vector<Rect> out_crop = {{metadata->crop.left, metadata->crop.top, metadata->crop.right,
+                                     metadata->crop.bottom}};
+      android::gralloc4::encodeCrop(out_crop, out);
+      break;
+    }
     case QTI_VT_TIMESTAMP:
       android::gralloc4::encodeUint64(qtigralloc::MetadataType_VTTimestamp, metadata->vtTimeStamp,
                                       out);
@@ -1094,6 +1095,18 @@
       }
       break;
     }
+    case (int64_t)StandardMetadataType::CROP: {
+      std::vector<Rect> in_crop;
+      android::gralloc4::decodeCrop(in, &in_crop);
+      if (in_crop.size() != 1)
+        return Error::UNSUPPORTED;
+
+      metadata->crop.left = in_crop[0].left;
+      metadata->crop.top = in_crop[0].top;
+      metadata->crop.right = in_crop[0].right;
+      metadata->crop.bottom = in_crop[0].bottom;
+      break;
+    }
     case QTI_VT_TIMESTAMP:
       android::gralloc4::decodeUint64(qtigralloc::MetadataType_VTTimestamp, in,
                                       &metadata->vtTimeStamp);