gpu: Create scaffolding for SharedImageBackingOzone

This CL introduces a NativePixmap-based SharedImageBacking for use by
ChromeOS to support WebGPU. This only creates a skeleton for
SharedImageBackingOzone in an effort to split up code reviews; the
SharedImageBacking methods are neither implemented nor called.

Bug: 1023997,996470
Change-Id: Id4e8ba3079df8af609f883b61ffe6e54975e075b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1913081
Commit-Queue: Brian Ho <hob@chromium.org>
Reviewed-by: Robert Kroeger <rjkroege@chromium.org>
Reviewed-by: Khushal <khushalsagar@chromium.org>
Reviewed-by: Eric Karl <ericrk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721189}
diff --git a/gpu/command_buffer/service/BUILD.gn b/gpu/command_buffer/service/BUILD.gn
index 6d27667..2709ff8 100644
--- a/gpu/command_buffer/service/BUILD.gn
+++ b/gpu/command_buffer/service/BUILD.gn
@@ -326,6 +326,12 @@
     "//ui/gl/init",
   ]
 
+  if (use_ozone) {
+    deps += [
+      "//ui/ozone",
+    ]
+  }
+
   if (enable_vulkan) {
     deps += [
       "//components/viz/common:vulkan_context_provider",
@@ -345,6 +351,13 @@
       ]
     }
 
+    if (use_ozone) {
+      sources += [
+        "shared_image_backing_ozone.cc",
+        "shared_image_backing_ozone.h",
+      ]
+    }
+
     if (is_linux && use_dawn) {
       sources += [
         "external_vk_image_dawn_representation.cc",
diff --git a/gpu/command_buffer/service/shared_image_backing_ozone.cc b/gpu/command_buffer/service/shared_image_backing_ozone.cc
new file mode 100644
index 0000000..84bf3db
--- /dev/null
+++ b/gpu/command_buffer/service/shared_image_backing_ozone.cc
@@ -0,0 +1,169 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "gpu/command_buffer/service/shared_image_backing_ozone.h"
+
+#include <dawn/webgpu.h>
+#include <vulkan/vulkan.h>
+
+#include <memory>
+
+#include "base/logging.h"
+#include "base/memory/ptr_util.h"
+#include "base/memory/scoped_refptr.h"
+#include "components/viz/common/gpu/vulkan_context_provider.h"
+#include "components/viz/common/resources/resource_format.h"
+#include "components/viz/common/resources/resource_format_utils.h"
+#include "gpu/command_buffer/common/mailbox.h"
+#include "gpu/command_buffer/common/shared_image_usage.h"
+#include "gpu/command_buffer/service/mailbox_manager.h"
+#include "gpu/command_buffer/service/memory_tracking.h"
+#include "gpu/command_buffer/service/shared_context_state.h"
+#include "gpu/command_buffer/service/shared_image_manager.h"
+#include "gpu/command_buffer/service/shared_image_representation.h"
+#include "gpu/vulkan/vulkan_device_queue.h"
+#include "ui/gfx/buffer_format_util.h"
+#include "ui/gfx/buffer_types.h"
+#include "ui/gfx/color_space.h"
+#include "ui/gfx/geometry/rect.h"
+#include "ui/gfx/geometry/size.h"
+#include "ui/gfx/gpu_fence.h"
+#include "ui/gfx/native_pixmap.h"
+#include "ui/gfx/native_widget_types.h"
+#include "ui/ozone/public/ozone_platform.h"
+#include "ui/ozone/public/surface_factory_ozone.h"
+
+namespace gpu {
+namespace {
+
+size_t GetPixmapSizeInBytes(const gfx::NativePixmap& pixmap) {
+  return gfx::BufferSizeForBufferFormat(pixmap.GetBufferSize(),
+                                        pixmap.GetBufferFormat());
+}
+
+gfx::BufferUsage GetBufferUsage(uint32_t usage) {
+  if (usage & SHARED_IMAGE_USAGE_WEBGPU) {
+    // Just use SCANOUT for WebGPU since the memory doesn't need to be linear.
+    return gfx::BufferUsage::SCANOUT;
+  } else if (usage & SHARED_IMAGE_USAGE_SCANOUT) {
+    return gfx::BufferUsage::SCANOUT;
+  } else {
+    NOTREACHED() << "Unsupported usage flags.";
+    return gfx::BufferUsage::SCANOUT;
+  }
+}
+
+}  // namespace
+
+std::unique_ptr<SharedImageBackingOzone> SharedImageBackingOzone::Create(
+    SharedContextState* context_state,
+    const Mailbox& mailbox,
+    viz::ResourceFormat format,
+    const gfx::Size& size,
+    const gfx::ColorSpace& color_space,
+    uint32_t usage) {
+  gfx::BufferFormat buffer_format = viz::BufferFormat(format);
+  gfx::BufferUsage buffer_usage = GetBufferUsage(usage);
+  VkDevice vk_device = VK_NULL_HANDLE;
+  DCHECK(context_state);
+  if (context_state->vk_context_provider()) {
+    vk_device = context_state->vk_context_provider()
+                    ->GetDeviceQueue()
+                    ->GetVulkanDevice();
+  }
+  ui::SurfaceFactoryOzone* surface_factory =
+      ui::OzonePlatform::GetInstance()->GetSurfaceFactoryOzone();
+  scoped_refptr<gfx::NativePixmap> pixmap = surface_factory->CreateNativePixmap(
+      gfx::kNullAcceleratedWidget, vk_device, size, buffer_format,
+      buffer_usage);
+  if (!pixmap) {
+    return nullptr;
+  }
+
+  return base::WrapUnique(
+      new SharedImageBackingOzone(mailbox, format, size, color_space, usage,
+                                  context_state, std::move(pixmap)));
+}
+
+SharedImageBackingOzone::~SharedImageBackingOzone() = default;
+
+bool SharedImageBackingOzone::IsCleared() const {
+  NOTIMPLEMENTED_LOG_ONCE();
+  return false;
+}
+
+void SharedImageBackingOzone::SetCleared() {
+  NOTIMPLEMENTED_LOG_ONCE();
+}
+
+void SharedImageBackingOzone::Update(std::unique_ptr<gfx::GpuFence> in_fence) {
+  NOTIMPLEMENTED_LOG_ONCE();
+  return;
+}
+
+void SharedImageBackingOzone::Destroy() {}
+
+bool SharedImageBackingOzone::ProduceLegacyMailbox(
+    MailboxManager* mailbox_manager) {
+  NOTREACHED();
+  return false;
+}
+
+std::unique_ptr<SharedImageRepresentationDawn>
+SharedImageBackingOzone::ProduceDawn(SharedImageManager* manager,
+                                     MemoryTypeTracker* tracker,
+                                     WGPUDevice device) {
+  NOTIMPLEMENTED_LOG_ONCE();
+  return nullptr;
+}
+
+std::unique_ptr<SharedImageRepresentationGLTexture>
+SharedImageBackingOzone::ProduceGLTexture(SharedImageManager* manager,
+                                          MemoryTypeTracker* tracker) {
+  NOTIMPLEMENTED_LOG_ONCE();
+  return nullptr;
+}
+
+std::unique_ptr<SharedImageRepresentationGLTexturePassthrough>
+SharedImageBackingOzone::ProduceGLTexturePassthrough(
+    SharedImageManager* manager,
+    MemoryTypeTracker* tracker) {
+  NOTIMPLEMENTED_LOG_ONCE();
+  return nullptr;
+}
+
+std::unique_ptr<SharedImageRepresentationSkia>
+SharedImageBackingOzone::ProduceSkia(
+    SharedImageManager* manager,
+    MemoryTypeTracker* tracker,
+    scoped_refptr<SharedContextState> context_state) {
+  NOTIMPLEMENTED_LOG_ONCE();
+  return nullptr;
+}
+
+std::unique_ptr<SharedImageRepresentationOverlay>
+SharedImageBackingOzone::ProduceOverlay(SharedImageManager* manager,
+                                        MemoryTypeTracker* tracker) {
+  NOTIMPLEMENTED_LOG_ONCE();
+  return nullptr;
+}
+
+SharedImageBackingOzone::SharedImageBackingOzone(
+    const Mailbox& mailbox,
+    viz::ResourceFormat format,
+    const gfx::Size& size,
+    const gfx::ColorSpace& color_space,
+    uint32_t usage,
+    SharedContextState* context_state,
+    scoped_refptr<gfx::NativePixmap> pixmap)
+    : SharedImageBacking(mailbox,
+                         format,
+                         size,
+                         color_space,
+                         usage,
+                         GetPixmapSizeInBytes(*pixmap),
+                         false),
+      pixmap_(std::move(pixmap)) {}
+
+}  // namespace gpu
diff --git a/gpu/command_buffer/service/shared_image_backing_ozone.h b/gpu/command_buffer/service/shared_image_backing_ozone.h
new file mode 100644
index 0000000..08d9345
--- /dev/null
+++ b/gpu/command_buffer/service/shared_image_backing_ozone.h
@@ -0,0 +1,85 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef GPU_COMMAND_BUFFER_SERVICE_SHARED_IMAGE_BACKING_OZONE_H_
+#define GPU_COMMAND_BUFFER_SERVICE_SHARED_IMAGE_BACKING_OZONE_H_
+
+#include <dawn/webgpu.h>
+
+#include <memory>
+
+#include "base/macros.h"
+#include "base/memory/scoped_refptr.h"
+#include "components/viz/common/resources/resource_format.h"
+#include "gpu/command_buffer/common/mailbox.h"
+#include "gpu/command_buffer/service/mailbox_manager.h"
+#include "gpu/command_buffer/service/memory_tracking.h"
+#include "gpu/command_buffer/service/shared_context_state.h"
+#include "gpu/command_buffer/service/shared_image_backing.h"
+#include "gpu/command_buffer/service/shared_image_manager.h"
+#include "gpu/command_buffer/service/shared_image_representation.h"
+#include "ui/gfx/color_space.h"
+#include "ui/gfx/geometry/size.h"
+#include "ui/gfx/gpu_fence.h"
+#include "ui/gfx/native_pixmap.h"
+
+namespace gpu {
+
+// Implementation of SharedImageBacking that uses a NativePixmap created via
+// an Ozone surface factory. The memory associated with the pixmap can be
+// aliased by both GL and Vulkan for use in rendering or compositing.
+class SharedImageBackingOzone final : public SharedImageBacking {
+ public:
+  static std::unique_ptr<SharedImageBackingOzone> Create(
+      SharedContextState* context_state,
+      const Mailbox& mailbox,
+      viz::ResourceFormat format,
+      const gfx::Size& size,
+      const gfx::ColorSpace& color_space,
+      uint32_t usage);
+  ~SharedImageBackingOzone() override;
+
+  // gpu::SharedImageBacking:
+  bool IsCleared() const override;
+  void SetCleared() override;
+  void Update(std::unique_ptr<gfx::GpuFence> in_fence) override;
+  void Destroy() override;
+  bool ProduceLegacyMailbox(MailboxManager* mailbox_manager) override;
+
+ protected:
+  std::unique_ptr<SharedImageRepresentationDawn> ProduceDawn(
+      SharedImageManager* manager,
+      MemoryTypeTracker* tracker,
+      WGPUDevice device) override;
+  std::unique_ptr<SharedImageRepresentationGLTexture> ProduceGLTexture(
+      SharedImageManager* manager,
+      MemoryTypeTracker* tracker) override;
+  std::unique_ptr<SharedImageRepresentationGLTexturePassthrough>
+  ProduceGLTexturePassthrough(SharedImageManager* manager,
+                              MemoryTypeTracker* tracker) override;
+  std::unique_ptr<SharedImageRepresentationSkia> ProduceSkia(
+      SharedImageManager* manager,
+      MemoryTypeTracker* tracker,
+      scoped_refptr<SharedContextState> context_state) override;
+  std::unique_ptr<SharedImageRepresentationOverlay> ProduceOverlay(
+      SharedImageManager* manager,
+      MemoryTypeTracker* tracker) override;
+
+ private:
+  SharedImageBackingOzone(const Mailbox& mailbox,
+                          viz::ResourceFormat format,
+                          const gfx::Size& size,
+                          const gfx::ColorSpace& color_space,
+                          uint32_t usage,
+                          SharedContextState* context_state,
+                          scoped_refptr<gfx::NativePixmap> pixmap);
+
+  scoped_refptr<gfx::NativePixmap> pixmap_;
+
+  DISALLOW_COPY_AND_ASSIGN(SharedImageBackingOzone);
+};
+
+}  // namespace gpu
+
+#endif  // GPU_COMMAND_BUFFER_SERVICE_SHARED_IMAGE_BACKING_OZONE_H_