Add getTransportSize/validateBufferSize

Implements the getTransportSize() and validateBufferSize() functions
in Cuttlefish's gralloc0 legacy implementation.

Bug: b/132087346
Test: vts-tradefed run commandAndExit vts -m VtsHalGraphicsMapperV2_1Target
Change-Id: Ib23ba96a6f5ab8c2b21067d939c323aab1c0a9ae
diff --git a/guest/hals/gralloc/legacy/gralloc.cpp b/guest/hals/gralloc/legacy/gralloc.cpp
index 2cdcb12..a100a76 100644
--- a/guest/hals/gralloc/legacy/gralloc.cpp
+++ b/guest/hals/gralloc/legacy/gralloc.cpp
@@ -194,8 +194,8 @@
     .unregisterBuffer = gralloc_unregister_buffer,
     .lock = gralloc_lock,
     .unlock = gralloc_unlock,
-    .validateBufferSize = NULL,
-    .getTransportSize = NULL,
+    .validateBufferSize = gralloc_validate_buffer_size,
+    .getTransportSize = gralloc_get_transport_size,
 #ifdef GRALLOC_MODULE_API_VERSION_0_2
     .perform = NULL,
     .lock_ycbcr = gralloc_lock_ycbcr,
diff --git a/guest/hals/gralloc/legacy/gralloc_vsoc_priv.h b/guest/hals/gralloc/legacy/gralloc_vsoc_priv.h
index 8790321..2544b5a 100644
--- a/guest/hals/gralloc/legacy/gralloc_vsoc_priv.h
+++ b/guest/hals/gralloc/legacy/gralloc_vsoc_priv.h
@@ -320,3 +320,12 @@
     buffer_handle_t handle, int usage,
     int l, int t, int w, int h,
     struct android_ycbcr *ycbcr);
+
+int32_t gralloc_get_transport_size(
+    struct gralloc_module_t const* module, buffer_handle_t handle,
+    uint32_t *outNumFds, uint32_t *outNumInts);
+
+int32_t gralloc_validate_buffer_size(
+    struct gralloc_module_t const* device, buffer_handle_t handle,
+    uint32_t w, uint32_t h, int32_t format, int usage,
+    uint32_t stride);
diff --git a/guest/hals/gralloc/legacy/mapper.cpp b/guest/hals/gralloc/legacy/mapper.cpp
index 597e024..eb0facc 100644
--- a/guest/hals/gralloc/legacy/mapper.cpp
+++ b/guest/hals/gralloc/legacy/mapper.cpp
@@ -122,3 +122,36 @@
   formatToYcbcr(hnd->format, hnd->x_res, hnd->y_res, base, ycbcr);
   return 0;
 }
+
+int32_t gralloc_get_transport_size(struct gralloc_module_t const* /*module*/,
+                                   buffer_handle_t handle,
+                                   uint32_t *outNumFds,
+                                   uint32_t *outNumInts) {
+  if (private_handle_t::validate(handle) < 0) {
+    return 2; // GRALLOC1_ERROR_BAD_HANDLE
+  }
+  private_handle_t* hnd = (private_handle_t*)handle;
+  *outNumFds = hnd->numFds;
+  *outNumInts = hnd->numInts;
+  return 0;
+}
+
+int32_t gralloc_validate_buffer_size(struct gralloc_module_t const* /*device*/,
+                                     buffer_handle_t handle,
+                                     uint32_t w,
+                                     uint32_t h,
+                                     int32_t format,
+                                     int /*usage*/,
+                                     uint32_t stride) {
+  if (private_handle_t::validate(handle) < 0) {
+    return 2; // GRALLOC1_ERROR_BAD_HANDLE
+  }
+  private_handle_t* hnd = (private_handle_t*)handle;
+  if (format != hnd->format ||
+      w > hnd->x_res ||
+      h > hnd->y_res ||
+      stride > hnd->stride_in_pixels) {
+    return 3; // GRALLOC1_ERROR_BAD_VALUE
+  }
+  return 0;
+}