mediatek: ensure total size satisfy luma padding for Sigurd based GPU

BUG=b:239243515
TEST=testGLViewLargerWidthDecodeAccuracy[40]

Change-Id: I3c172a93a85c878beb10ecc6f65ed90438c85d89
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/minigbm/+/3919606
Reviewed-by: Hsin-Yi Wang <hsinyi@chromium.org>
Commit-Queue: Fei Shao <fshao@chromium.org>
Reviewed-by: Fei Shao <fshao@chromium.org>
Auto-Submit: Yiwei Zhang <zzyiwei@chromium.org>
Tested-by: Yiwei Zhang <zzyiwei@chromium.org>
diff --git a/mediatek.c b/mediatek.c
index becf540..7f780f0 100644
--- a/mediatek.c
+++ b/mediatek.c
@@ -223,13 +223,31 @@
 		drv_bo_from_format(bo, stride, height, format);
 
 #ifdef USE_EXTRA_PADDING_FOR_YVU420
-		/* Do the fix after drv_bo_from_format to avoid extra calculations. */
+		/*
+		 * Apply extra padding for YV12 if the height does not meet round up requirement and
+		 * the image is to be sampled by gpu.
+		 */
+		static const uint32_t required_round_up = 4;
+		const uint32_t height_mod = height % required_round_up;
 		if ((format == DRM_FORMAT_YVU420 || format == DRM_FORMAT_YVU420_ANDROID) &&
-		    (bo->meta.use_flags & BO_USE_TEXTURE)) {
-			const size_t last_plane = bo->meta.num_planes - 1;
-			uint32_t padded_last_plane_size = drv_size_from_format(
-			    format, bo->meta.strides[last_plane], ALIGN(height, 4), last_plane);
-			bo->meta.total_size += padded_last_plane_size - bo->meta.sizes[last_plane];
+		    (bo->meta.use_flags & BO_USE_TEXTURE) && height_mod) {
+			const uint32_t height_padding = required_round_up - height_mod;
+			const uint32_t u_padding =
+			    drv_size_from_format(format, bo->meta.strides[2], height_padding, 2);
+
+			bo->meta.total_size += u_padding;
+
+			/*
+			 * Since we are not aligning Y, we must make sure that its padding fits
+			 * inside the rest of the space allocated for the V/U planes.
+			 */
+			const uint32_t y_padding =
+			    drv_size_from_format(format, bo->meta.strides[0], height_padding, 0);
+			const uint32_t vu_size = drv_bo_get_plane_size(bo, 2) * 2;
+			if (y_padding > vu_size) {
+				/* Align with mali workaround to pad all 3 planes. */
+				bo->meta.total_size += y_padding + u_padding;
+			}
 		}
 #endif
 	}