[viz] Add needed test support for manipulating MappableSI

An upcoming conversion [1] will need support for the following:
- Failing to create a MappableSharedImage.
- Creating a MappableSharedImage backed by a test GMB.

This CL adds this support. We separate it out from the actual conversion
so that the test support sticks in case the conversion gets reverted.

[1] https://chromium-review.googlesource.com/c/chromium/src/+/5119550

Bug: 1431497
Change-Id: I47e76798942ae4ff3c5cb9e141471f8524c02cbb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5123393
Reviewed-by: Vasiliy Telezhnikov <vasilyt@chromium.org>
Commit-Queue: Colin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1238544}
diff --git a/components/viz/test/test_context_provider.cc b/components/viz/test/test_context_provider.cc
index c9362f6..70ff478 100644
--- a/components/viz/test/test_context_provider.cc
+++ b/components/viz/test/test_context_provider.cc
@@ -198,12 +198,25 @@
                                             base::StringPiece debug_label,
                                             gpu::SurfaceHandle surface_handle,
                                             gfx::BufferUsage buffer_usage) {
+  if (fail_shared_image_creation_with_buffer_usage_) {
+    return nullptr;
+  }
+
   // Create a ClientSharedImage with a GMB.
   auto client_shared_image =
       CreateSharedImage(format, size, color_space, surface_origin, alpha_type,
                         usage, std::move(debug_label), surface_handle);
   CHECK(client_shared_image);
   auto mailbox = client_shared_image->mailbox();
+
+  if (test_gmb_manager_) {
+    auto gpu_memory_buffer = test_gmb_manager_->CreateGpuMemoryBuffer(
+        size, SinglePlaneSharedImageFormatToBufferFormat(format), buffer_usage,
+        surface_handle, nullptr);
+    return gpu::ClientSharedImage::CreateForTesting(
+        mailbox, std::move(gpu_memory_buffer));
+  }
+
   auto gmb_handle = CreateGMBHandle(format, size, buffer_usage);
 
   return base::MakeRefCounted<gpu::ClientSharedImage>(
diff --git a/components/viz/test/test_context_provider.h b/components/viz/test/test_context_provider.h
index 926d7c3..7056c5ae 100644
--- a/components/viz/test/test_context_provider.h
+++ b/components/viz/test/test_context_provider.h
@@ -22,6 +22,7 @@
 #include "components/viz/common/gpu/context_provider.h"
 #include "components/viz/common/gpu/raster_context_provider.h"
 #include "components/viz/test/test_context_support.h"
+#include "components/viz/test/test_gpu_memory_buffer_manager.h"
 #include "gpu/command_buffer/client/gles2_interface_stub.h"
 #include "gpu/command_buffer/client/shared_image_interface.h"
 #include "gpu/command_buffer/common/shared_image_capabilities.h"
@@ -161,6 +162,14 @@
   const gpu::SharedImageCapabilities& GetCapabilities() override;
   void SetCapabilities(const gpu::SharedImageCapabilities& caps);
 
+  void SetFailSharedImageCreationWithBufferUsage(bool value) {
+    fail_shared_image_creation_with_buffer_usage_ = value;
+  }
+
+  void UseTestGMBInSharedImageCreationWithBufferUsage() {
+    test_gmb_manager_ = std::make_unique<TestGpuMemoryBufferManager>();
+  }
+
  private:
   mutable base::Lock lock_;
 
@@ -171,6 +180,11 @@
   base::flat_set<gpu::Mailbox> shared_images_;
 
   gpu::SharedImageCapabilities shared_image_capabilities_;
+  bool fail_shared_image_creation_with_buffer_usage_ = false;
+
+  // If non-null, this will be used to back mappable SharedImages with test
+  // GpuMemoryBuffers.
+  std::unique_ptr<TestGpuMemoryBufferManager> test_gmb_manager_;
 };
 
 class TestContextProvider
diff --git a/gpu/command_buffer/client/client_shared_image.h b/gpu/command_buffer/client/client_shared_image.h
index 8116b2a..1da54e7b 100644
--- a/gpu/command_buffer/client/client_shared_image.h
+++ b/gpu/command_buffer/client/client_shared_image.h
@@ -106,6 +106,14 @@
         Mailbox::GenerateForSharedImage());
   }
 
+  static scoped_refptr<ClientSharedImage> CreateForTesting(
+      const Mailbox& mailbox,
+      std::unique_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer) {
+    auto client_si = base::MakeRefCounted<ClientSharedImage>(mailbox);
+    client_si->gpu_memory_buffer_ = std::move(gpu_memory_buffer);
+    return client_si;
+  }
+
  private:
   friend class base::RefCountedThreadSafe<ClientSharedImage>;
   ~ClientSharedImage();