From a72ed981ab5fc425127e8319c6773b934f3b4b51 Mon Sep 17 00:00:00 2001
From: Qiu Jianlin <jianlin.qiu@intel.com>
Date: Tue, 17 Dec 2024 17:42:10 -0800
Subject: [PATCH] [MFVEA] Re-enable back D3DCopy/Scaling for texture on
 rendering device

A previous change in MF VEA disabled downscaling copy. Re-enable it back. Also restore a previous no-scaling copy that was removed to avoid jitter during encoding.

Bug: 384567067
Change-Id: I5fdd098e687b691bfd75305d11e8f2fe22172ef0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6096777
Commit-Queue: Jianlin Qiu <jianlin.qiu@intel.com>
Reviewed-by: Eugene Zemtsov <eugene@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1397682}
---
 ...media_foundation_video_encode_accelerator_win.cc | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/media/gpu/windows/media_foundation_video_encode_accelerator_win.cc b/media/gpu/windows/media_foundation_video_encode_accelerator_win.cc
index aaaf4d65cfd16..00d43c746fe3d 100644
--- a/media/gpu/windows/media_foundation_video_encode_accelerator_win.cc
+++ b/media/gpu/windows/media_foundation_video_encode_accelerator_win.cc
@@ -2071,21 +2071,18 @@ HRESULT MediaFoundationVideoEncodeAccelerator::PopulateInputSampleBufferGpu(
     hr = PerformD3DScaling(input_texture.Get(), frame->visible_rect());
     RETURN_ON_HR_FAILURE(hr, "Failed to perform D3D video processing", hr);
     sample_texture = scaled_d3d11_texture_;
+  } else if (frame->HasSharedImage()) {
+    // The texture is already a copied version so we can directly use it.
+    sample_texture = input_texture;
   } else {
     // Even though no scaling is needed we still need to copy the texture to
     // avoid concurrent usage causing glitches (https://crbug.com/1462315). This
     // is preferred over holding a keyed mutex for the duration of the encode
     // operation since that can take a significant amount of time and mutex
     // acquisitions (necessary even for read-only operations) are blocking.
-    ComD3D11Device texture_device;
-    input_texture->GetDevice(&texture_device);
-    if (texture_device == dxgi_device_manager_->GetDevice()) {
-      sample_texture = input_texture;
-    } else {
       hr = PerformD3DCopy(input_texture.Get(), frame->visible_rect());
       RETURN_ON_HR_FAILURE(hr, "Failed to perform D3D texture copy", hr);
       sample_texture = copied_d3d11_texture_;
-    }
   }
 
   ComMFMediaBuffer input_buffer;
@@ -2540,9 +2537,7 @@ HRESULT MediaFoundationVideoEncodeAccelerator::PerformD3DScaling(
   video_context_->VideoProcessorSetOutputColorSpace(video_processor_.Get(),
                                                     &output_d3d11_color_space);
 
-  ComD3D11Device texture_device;
-  input_texture->GetDevice(&texture_device);
-  if (texture_device != dxgi_device_manager_->GetDevice()) {
+  {
     std::optional<gpu::DXGIScopedReleaseKeyedMutex> release_keyed_mutex;
     ComDXGIKeyedMutex keyed_mutex;
     hr = input_texture->QueryInterface(IID_PPV_ARGS(&keyed_mutex));