From b5509f3a5cc284582775d363f678b3ca45db2d3d Mon Sep 17 00:00:00 2001
From: Saifuddin Hitawala <hitawala@chromium.org>
Date: Wed, 7 May 2025 06:25:31 -0700
Subject: [PATCH] Reland "[media] Simplify
 VRU::SupportedMultiPlaneChannelFormat"

This reverts commit c20f7a1b8d94eb83565c83dbe14b662f1b7bb8ff.

Reason for revert: Missed that shared_image_caps is set inside,
need to be set outside as they are now used by
SupportedMultiPlaneChannelFormat

Bug: 332564976
Original change's description:
> Revert "[media] Simplify VRU::SupportedMultiPlaneChannelFormat"
>
> This reverts commit 4e2dcd6c34bfc89e678de252071ec245aaf92977.
>
> Reason for revert: Breaks the build and the tree: https://ci.chromium.org/ui/p/chromium/builders/ci/android-desktop-x64-compile-rel/28788/overview
>
> Bug: 332564976
> Original change's description:
> > [media] Simplify VRU::SupportedMultiPlaneChannelFormat
> >
> > Update VideoResourceUpdater::SupportedMultiPlaneChannelFormat to
> > check directly for capabilities instead of going over
> > YuvSharedImageFormat. This allows to remove both YuvSharedImageFormat
> > and GetSingleChannel8BitFormat methods.
> >
> > Also inline IsFormat16BitFloat method where it is needed.
> >
> > Bug: 332564976
> > Change-Id: I8c2f955d2a18c6273d06f2291b1b3d09442a4a97
> > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6512515
> > Commit-Queue: Saifuddin Hitawala <hitawala@chromium.org>
> > Reviewed-by: Vasiliy Telezhnikov <vasilyt@chromium.org>
> > Reviewed-by: Dale Curtis <dalecurtis@chromium.org>
> > Cr-Commit-Position: refs/heads/main@{#1456573}
>
> Bug: 332564976
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Change-Id: I21cf144ab6a19eab3da3b902e810377aab08bf84
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6512529
> Reviewed-by: Sophey Dong <sophey@chromium.org>
> Owners-Override: Anton Maliev <amaliev@chromium.org>
> Commit-Queue: Anton Maliev <amaliev@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#1456579}

Bug: 332564976
Change-Id: I2c5a33136f4c47766cfb74acef5edd6ed8de675e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6516048
Reviewed-by: Vasiliy Telezhnikov <vasilyt@chromium.org>
Commit-Queue: Saifuddin Hitawala <hitawala@chromium.org>
Reviewed-by: Dale Curtis <dalecurtis@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1456949}
---
 media/renderers/video_resource_updater.cc | 79 +++++++----------------
 1 file changed, 25 insertions(+), 54 deletions(-)

diff --git a/media/renderers/video_resource_updater.cc b/media/renderers/video_resource_updater.cc
index 9e8ed53959b8d..bb62b7b7e95f6 100644
--- a/media/renderers/video_resource_updater.cc
+++ b/media/renderers/video_resource_updater.cc
@@ -180,17 +180,6 @@ viz::SharedImageFormat GetRGBSharedImageFormat(VideoPixelFormat format) {
 #endif
 }
 
-viz::SharedImageFormat GetSingleChannel8BitFormat(
-    const gpu::Capabilities& caps,
-    const gpu::SharedImageCapabilities& shared_image_caps) {
-  if (caps.texture_rg && !shared_image_caps.disable_r8_shared_images) {
-    return viz::SinglePlaneFormat::kR_8;
-  }
-
-  DCHECK(shared_image_caps.supports_luminance_shared_images);
-  return viz::SinglePlaneFormat::kLUMINANCE_8;
-}
-
 // Returns true if the input VideoFrame format can be stored directly in the
 // provided output shared image format.
 bool HasCompatibleRGBFormat(VideoPixelFormat input_format,
@@ -214,20 +203,24 @@ bool IsFrameFormat32BitRGB(VideoPixelFormat frame_format) {
          frame_format == PIXEL_FORMAT_ABGR || frame_format == PIXEL_FORMAT_ARGB;
 }
 
-bool IsFormat16BitFloat(viz::SharedImageFormat format) {
-  // Assume multiplanar SharedImageFormat with ChannelFormat::k16F is always
-  // used as LUMINANCEF16.
-  CHECK(format.is_multi_plane());
-  return format.channel_format() == viz::SharedImageFormat::ChannelFormat::k16F;
-}
-
 viz::SharedImageFormat::ChannelFormat SupportedMultiPlaneChannelFormat(
-    viz::SharedImageFormat format) {
-  if (format == viz::SinglePlaneFormat::kR_16) {
+    const gpu::Capabilities& caps,
+    const gpu::SharedImageCapabilities& shared_image_caps,
+    int bits_per_channel) {
+  if (bits_per_channel <= 8) {
+    // Must support texture_rg or 8-bits luminance.
+    DCHECK(shared_image_caps.supports_luminance_shared_images ||
+           caps.texture_rg);
+    return viz::SharedImageFormat::ChannelFormat::k8;
+  }
+  // Can support R_16 formats.
+  if (caps.texture_norm16 && shared_image_caps.supports_r16_shared_images) {
     return viz::SharedImageFormat::ChannelFormat::k16;
   }
-  if (format == viz::SinglePlaneFormat::kLUMINANCE_F16 ||
-      format == viz::SinglePlaneFormat::kR_F16) {
+  // Can support R_F16 or LUMINANCE_F16 formats.
+  if (shared_image_caps.is_r16f_supported ||
+      (caps.texture_half_float_linear &&
+       shared_image_caps.supports_luminance_shared_images)) {
     return viz::SharedImageFormat::ChannelFormat::k16F;
   }
   return viz::SharedImageFormat::ChannelFormat::k8;
@@ -857,30 +850,6 @@ VideoFrameExternalResource VideoResourceUpdater::CreateForHardwareFrame(
   return external_resource;
 }
 
-viz::SharedImageFormat VideoResourceUpdater::YuvSharedImageFormat(
-    int bits_per_channel) {
-  DCHECK(context_provider_);
-  const auto& caps = context_provider_->ContextCapabilities();
-  const auto& shared_image_caps =
-      context_provider_->SharedImageInterface()->GetCapabilities();
-  if (bits_per_channel <= 8) {
-    DCHECK(shared_image_caps.supports_luminance_shared_images ||
-           caps.texture_rg);
-    return GetSingleChannel8BitFormat(caps, shared_image_caps);
-  }
-  if (caps.texture_norm16 && shared_image_caps.supports_r16_shared_images) {
-    return viz::SinglePlaneFormat::kR_16;
-  }
-  if (shared_image_caps.is_r16f_supported) {
-    return viz::SinglePlaneFormat::kR_F16;
-  }
-  if (caps.texture_half_float_linear &&
-      shared_image_caps.supports_luminance_shared_images) {
-    return viz::SinglePlaneFormat::kLUMINANCE_F16;
-  }
-  return GetSingleChannel8BitFormat(caps, shared_image_caps);
-}
-
 viz::SharedImageFormat VideoResourceUpdater::GetSoftwareOutputFormat(
     VideoPixelFormat input_frame_format,
     int bits_per_channel,
@@ -906,6 +875,8 @@ viz::SharedImageFormat VideoResourceUpdater::GetSoftwareOutputFormat(
     return PaintCanvasVideoRenderer::GetRGBPixelsOutputFormat();
   }
 
+  const auto& shared_image_caps =
+      context_provider_->SharedImageInterface()->GetCapabilities();
   // Get the multiplanar shared image format for `input_frame_format`.
   auto yuv_si_format =
       VideoPixelFormatToMultiPlanarSharedImageFormat(input_frame_format);
@@ -914,8 +885,6 @@ viz::SharedImageFormat VideoResourceUpdater::GetSoftwareOutputFormat(
     // Only 8-bit formats are supported with UV planes for software decoding.
     CHECK_EQ(yuv_si_format.channel_format(),
              viz::SharedImageFormat::ChannelFormat::k8);
-    const auto& shared_image_caps =
-        context_provider_->SharedImageInterface()->GetCapabilities();
     // Two channel formats are supported only with texture_rg.
     if (!caps.texture_rg || shared_image_caps.disable_r8_shared_images) {
       texture_needs_rgb_conversion_out = true;
@@ -924,8 +893,8 @@ viz::SharedImageFormat VideoResourceUpdater::GetSoftwareOutputFormat(
   }
 
   // Get the supported channel format for `yuv_si_format`'s first plane.
-  auto channel_format =
-      SupportedMultiPlaneChannelFormat(YuvSharedImageFormat(bits_per_channel));
+  auto channel_format = SupportedMultiPlaneChannelFormat(
+      caps, shared_image_caps, bits_per_channel);
   if (yuv_si_format.channel_format() != channel_format) {
     // If the requested channel format is not supported, use the supported
     // channel format and downsample later if needed.
@@ -1076,12 +1045,14 @@ bool VideoResourceUpdater::WriteYUVPixelsForAllPlanesToTexture(
     const bool needs_bit_upshifting =
         bits_per_channel > 8 && bits_per_channel < resource_bit_depth;
 
+    const bool is_16bit_float = yuv_si_format.channel_format() ==
+                                viz::SharedImageFormat::ChannelFormat::k16F;
+
     // We need to convert the incoming data if we're transferring to half
     // float, if there is need for bit downshift or if the strides need to
     // be reconciled.
-    const bool needs_conversion = IsFormat16BitFloat(yuv_si_format) ||
-                                  needs_bit_downshifting ||
-                                  needs_bit_upshifting;
+    const bool needs_conversion =
+        is_16bit_float || needs_bit_downshifting || needs_bit_upshifting;
     const uint8_t* pixels;
     int pixels_stride_in_bytes;
     if (!needs_conversion) {
@@ -1098,7 +1069,7 @@ bool VideoResourceUpdater::WriteYUVPixelsForAllPlanesToTexture(
         }
       }
 
-      if (IsFormat16BitFloat(yuv_si_format)) {
+      if (is_16bit_float) {
         int max_value = 1 << bits_per_channel;
         // Use 1.0/max_value to be consistent with multiplanar shared images
         // which create TextureDrawQuads and don't take in a multiplier, offset.