gralloc: Apply format check in getComponentSizeAndOffset

In getComponentSizeAndOffset, we apply the check on format to prevent
unexpected plane layout Components from being added into the component
vector.

Bug: 152506121
Test: VtsHalGraphicsMapperV4_0TargetTest
Change-Id: I58c51b7e304ba18cefaa7b2949a9127002ba1c33
diff --git a/gralloc/gr_buf_mgr.cpp b/gralloc/gr_buf_mgr.cpp
index 31c39db..5e9d2eb 100644
--- a/gralloc/gr_buf_mgr.cpp
+++ b/gralloc/gr_buf_mgr.cpp
@@ -280,7 +280,7 @@
   return Error::NONE;
 }
 
-static void getComponentSizeAndOffset(int32_t format, PlaneLayoutComponent &comp) {
+static Error getComponentSizeAndOffset(int32_t format, PlaneLayoutComponent &comp) {
   switch (format) {
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGBA_8888):
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGBX_8888):
@@ -292,8 +292,11 @@
         comp.offsetInBits = 8;
       } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
         comp.offsetInBits = 16;
-      } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
+      } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value &&
+                 format != HAL_PIXEL_FORMAT_RGB_888) {
         comp.offsetInBits = 24;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGB_565):
@@ -306,6 +309,8 @@
       } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
         comp.offsetInBits = 11;
         comp.sizeInBits = 5;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_BGR_565):
@@ -318,6 +323,8 @@
       } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
         comp.offsetInBits = 0;
         comp.sizeInBits = 5;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_BGRA_8888):
@@ -330,8 +337,11 @@
         comp.offsetInBits = 8;
       } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_B.value) {
         comp.offsetInBits = 0;
-      } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
+      } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value &&
+                 format != HAL_PIXEL_FORMAT_BGR_888) {
         comp.offsetInBits = 24;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGBA_5551):
@@ -347,6 +357,8 @@
       } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
         comp.sizeInBits = 1;
         comp.offsetInBits = 15;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGBA_4444):
@@ -362,6 +374,8 @@
       } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
         comp.sizeInBits = 4;
         comp.offsetInBits = 12;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_R_8):
@@ -369,8 +383,11 @@
       comp.sizeInBits = 8;
       if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_R.value) {
         comp.offsetInBits = 0;
-      } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_G.value) {
+      } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_G.value &&
+                 format != HAL_PIXEL_FORMAT_R_8) {
         comp.offsetInBits = 8;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGBA_1010102):
@@ -387,6 +404,8 @@
       } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
         comp.sizeInBits = 2;
         comp.offsetInBits = 30;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_ARGB_2101010):
@@ -403,6 +422,8 @@
       } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
         comp.sizeInBits = 2;
         comp.offsetInBits = 0;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_BGRA_1010102):
@@ -419,6 +440,8 @@
       } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
         comp.sizeInBits = 2;
         comp.offsetInBits = 30;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_ABGR_2101010):
@@ -435,6 +458,8 @@
       } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
         comp.sizeInBits = 2;
         comp.offsetInBits = 0;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_RGBA_FP16):
@@ -450,6 +475,8 @@
       } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_A.value) {
         comp.sizeInBits = 16;
         comp.offsetInBits = 48;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_YCbCr_420_SP):
@@ -462,6 +489,8 @@
         comp.offsetInBits = 0;
       } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_CR.value) {
         comp.offsetInBits = 8;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_YCrCb_420_SP):
@@ -475,12 +504,16 @@
         comp.offsetInBits = 0;
       } else if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_CB.value) {
         comp.offsetInBits = 8;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_Y16):
       if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_Y.value) {
         comp.offsetInBits = 0;
         comp.sizeInBits = 16;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_YV12):
@@ -489,12 +522,16 @@
           comp.type.value == android::gralloc4::PlaneLayoutComponentType_CR.value) {
         comp.offsetInBits = 0;
         comp.sizeInBits = 8;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_Y8):
       if (comp.type.value == android::gralloc4::PlaneLayoutComponentType_Y.value) {
         comp.offsetInBits = 0;
         comp.sizeInBits = 8;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     case static_cast<int32_t>(HAL_PIXEL_FORMAT_YCbCr_420_P010):
@@ -503,12 +540,15 @@
           comp.type.value == android::gralloc4::PlaneLayoutComponentType_CR.value) {
         comp.offsetInBits = 0;
         comp.sizeInBits = 10;
+      } else {
+        return Error::BAD_VALUE;
       }
       break;
     default:
       ALOGE("Offset and size in bits unknown for format %d", format);
-      break;
+      return Error::UNSUPPORTED;
   }
+  return Error::NONE;
 }
 
 static void grallocToStandardPlaneLayoutComponentType(uint32_t in,
@@ -520,44 +560,44 @@
 
   if (in & PLANE_COMPONENT_Y) {
     comp.type = android::gralloc4::PlaneLayoutComponentType_Y;
-    getComponentSizeAndOffset(format, comp);
-    components->push_back(comp);
+    if (getComponentSizeAndOffset(format, comp) == Error::NONE)
+      components->push_back(comp);
   }
 
   if (in & PLANE_COMPONENT_Cb) {
     comp.type = android::gralloc4::PlaneLayoutComponentType_CB;
-    getComponentSizeAndOffset(format, comp);
-    components->push_back(comp);
+    if (getComponentSizeAndOffset(format, comp) == Error::NONE)
+      components->push_back(comp);
   }
 
   if (in & PLANE_COMPONENT_Cr) {
     comp.type = android::gralloc4::PlaneLayoutComponentType_CR;
-    getComponentSizeAndOffset(format, comp);
-    components->push_back(comp);
+    if (getComponentSizeAndOffset(format, comp) == Error::NONE)
+      components->push_back(comp);
   }
 
   if (in & PLANE_COMPONENT_R) {
     comp.type = android::gralloc4::PlaneLayoutComponentType_R;
-    getComponentSizeAndOffset(format, comp);
-    components->push_back(comp);
+    if (getComponentSizeAndOffset(format, comp) == Error::NONE)
+      components->push_back(comp);
   }
 
   if (in & PLANE_COMPONENT_G) {
     comp.type = android::gralloc4::PlaneLayoutComponentType_G;
-    getComponentSizeAndOffset(format, comp);
-    components->push_back(comp);
+    if (getComponentSizeAndOffset(format, comp) == Error::NONE)
+      components->push_back(comp);
   }
 
   if (in & PLANE_COMPONENT_B) {
     comp.type = android::gralloc4::PlaneLayoutComponentType_B;
-    getComponentSizeAndOffset(format, comp);
-    components->push_back(comp);
+    if (getComponentSizeAndOffset(format, comp) == Error::NONE)
+      components->push_back(comp);
   }
 
   if (in & PLANE_COMPONENT_A) {
     comp.type = android::gralloc4::PlaneLayoutComponentType_A;
-    getComponentSizeAndOffset(format, comp);
-    components->push_back(comp);
+    if (getComponentSizeAndOffset(format, comp) == Error::NONE)
+      components->push_back(comp);
   }
 
   if (in & PLANE_COMPONENT_RAW) {