Commit f3d7e57b authored by Brian Ho's avatar Brian Ho Committed by Commit Bot

gpu: Add a Dawn representation for Ozone SharedImage

This CL creates a SharedImageRepresentationDawn for Ozone-based
SharedImages. This is mostly adapted from
ExternalVkImageDawnRepresentation and works by passing the dma-buf
handle to Dawn.

Synchronization will be added in a following CL.

Bug: 1023997,996470
Change-Id: I940a0ec9d659d31efaa63fac909d312d3558e675
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1913083
Commit-Queue: Brian Ho <hob@chromium.org>
Reviewed-by: default avatarEric Karl <ericrk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#722672}
parent 4f5dedd4
......@@ -364,6 +364,13 @@ target(link_target_type, "gles2_sources") {
"external_vk_image_dawn_representation.h",
]
}
if (use_ozone && use_dawn) {
sources += [
"shared_image_representation_dawn_ozone.cc",
"shared_image_representation_dawn_ozone.h",
]
}
}
if (use_dawn) {
......
......@@ -11,6 +11,7 @@
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "components/viz/common/gpu/vulkan_context_provider.h"
#include "components/viz/common/resources/resource_format.h"
......@@ -31,9 +32,14 @@
#include "ui/gfx/gpu_fence.h"
#include "ui/gfx/native_pixmap.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gl/buildflags.h"
#include "ui/ozone/public/ozone_platform.h"
#include "ui/ozone/public/surface_factory_ozone.h"
#if BUILDFLAG(USE_DAWN)
#include "gpu/command_buffer/service/shared_image_representation_dawn_ozone.h"
#endif // BUILDFLAG(USE_DAWN)
namespace gpu {
namespace {
......@@ -57,6 +63,7 @@ gfx::BufferUsage GetBufferUsage(uint32_t usage) {
} // namespace
std::unique_ptr<SharedImageBackingOzone> SharedImageBackingOzone::Create(
scoped_refptr<base::RefCountedData<DawnProcTable>> dawn_procs,
SharedContextState* context_state,
const Mailbox& mailbox,
viz::ResourceFormat format,
......@@ -81,9 +88,9 @@ std::unique_ptr<SharedImageBackingOzone> SharedImageBackingOzone::Create(
return nullptr;
}
return base::WrapUnique(
new SharedImageBackingOzone(mailbox, format, size, color_space, usage,
context_state, std::move(pixmap)));
return base::WrapUnique(new SharedImageBackingOzone(
mailbox, format, size, color_space, usage, context_state,
std::move(pixmap), std::move(dawn_procs)));
}
SharedImageBackingOzone::~SharedImageBackingOzone() = default;
......@@ -114,8 +121,16 @@ std::unique_ptr<SharedImageRepresentationDawn>
SharedImageBackingOzone::ProduceDawn(SharedImageManager* manager,
MemoryTypeTracker* tracker,
WGPUDevice device) {
NOTIMPLEMENTED_LOG_ONCE();
#if BUILDFLAG(USE_DAWN)
WGPUTextureFormat webgpu_format = viz::ToWGPUFormat(format());
if (webgpu_format == WGPUTextureFormat_Undefined) {
return nullptr;
}
return std::make_unique<SharedImageRepresentationDawnOzone>(
manager, this, tracker, device, webgpu_format, pixmap_, dawn_procs_);
#else // !BUILDFLAG(USE_DAWN)
return nullptr;
#endif
}
std::unique_ptr<SharedImageRepresentationGLTexture>
......@@ -156,7 +171,8 @@ SharedImageBackingOzone::SharedImageBackingOzone(
const gfx::ColorSpace& color_space,
uint32_t usage,
SharedContextState* context_state,
scoped_refptr<gfx::NativePixmap> pixmap)
scoped_refptr<gfx::NativePixmap> pixmap,
scoped_refptr<base::RefCountedData<DawnProcTable>> dawn_procs)
: SharedImageBacking(mailbox,
format,
size,
......@@ -164,6 +180,7 @@ SharedImageBackingOzone::SharedImageBackingOzone(
usage,
GetPixmapSizeInBytes(*pixmap),
false),
pixmap_(std::move(pixmap)) {}
pixmap_(std::move(pixmap)),
dawn_procs_(std::move(dawn_procs)) {}
} // namespace gpu
......@@ -10,6 +10,7 @@
#include <memory>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "components/viz/common/resources/resource_format.h"
#include "gpu/command_buffer/common/mailbox.h"
......@@ -32,6 +33,7 @@ namespace gpu {
class SharedImageBackingOzone final : public SharedImageBacking {
public:
static std::unique_ptr<SharedImageBackingOzone> Create(
scoped_refptr<base::RefCountedData<DawnProcTable>> dawn_procs,
SharedContextState* context_state,
const Mailbox& mailbox,
viz::ResourceFormat format,
......@@ -67,15 +69,18 @@ class SharedImageBackingOzone final : public SharedImageBacking {
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);
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<base::RefCountedData<DawnProcTable>> dawn_procs);
scoped_refptr<gfx::NativePixmap> pixmap_;
scoped_refptr<base::RefCountedData<DawnProcTable>> dawn_procs_;
DISALLOW_COPY_AND_ASSIGN(SharedImageBackingOzone);
};
......
// 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_representation_dawn_ozone.h"
#include <dawn_native/VulkanBackend.h>
#include <vulkan/vulkan.h>
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "gpu/command_buffer/service/memory_tracking.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/geometry/size.h"
#include "ui/gfx/native_pixmap.h"
namespace gpu {
SharedImageRepresentationDawnOzone::SharedImageRepresentationDawnOzone(
SharedImageManager* manager,
SharedImageBacking* backing,
MemoryTypeTracker* tracker,
WGPUDevice device,
WGPUTextureFormat format,
scoped_refptr<gfx::NativePixmap> pixmap,
scoped_refptr<base::RefCountedData<DawnProcTable>> dawn_procs)
: SharedImageRepresentationDawn(manager, backing, tracker),
device_(device),
format_(format),
pixmap_(pixmap),
dawn_procs_(dawn_procs) {
DCHECK(device_);
// Keep a reference to the device so that it stays valid (it might become
// lost in which case operations will be noops).
dawn_procs_->data.deviceReference(device_);
}
SharedImageRepresentationDawnOzone::~SharedImageRepresentationDawnOzone() {
EndAccess();
dawn_procs_->data.deviceRelease(device_);
}
WGPUTexture SharedImageRepresentationDawnOzone::BeginAccess(
WGPUTextureUsage usage) {
// It doesn't make sense to have two overlapping BeginAccess calls on the same
// representation.
if (texture_) {
return nullptr;
}
DCHECK(pixmap_->GetNumberOfPlanes() == 1)
<< "Multi-plane formats are not supported.";
// TODO(hob): Synchronize access to the dma-buf by waiting on all semaphores
// tracked by SharedImageBackingOzone.
gfx::Size pixmap_size = pixmap_->GetBufferSize();
WGPUTextureDescriptor texture_descriptor = {};
texture_descriptor.nextInChain = nullptr;
texture_descriptor.format = format_;
texture_descriptor.usage = usage;
texture_descriptor.dimension = WGPUTextureDimension_2D;
texture_descriptor.size = {pixmap_size.width(), pixmap_size.height(), 1};
texture_descriptor.arrayLayerCount = 1;
texture_descriptor.mipLevelCount = 1;
texture_descriptor.sampleCount = 1;
dawn_native::vulkan::ExternalImageDescriptorDmaBuf descriptor = {};
descriptor.cTextureDescriptor = &texture_descriptor;
descriptor.isCleared = IsCleared();
// Import the dma-buf into Dawn via the Vulkan backend. As per the Vulkan
// documentation, importing memory from a file descriptor transfers
// ownership of the fd from the application to the Vulkan implementation.
// Thus, we need to dup the fd so the fd corresponding to the dmabuf isn't
// closed twice (once by ScopedFD and once by the Vulkan implementation).
int fd = dup(pixmap_->GetDmaBufFd(0));
descriptor.memoryFD = fd;
descriptor.stride = pixmap_->GetDmaBufPitch(0);
descriptor.drmModifier = pixmap_->GetBufferFormatModifier();
descriptor.waitFDs = {};
texture_ = dawn_native::vulkan::WrapVulkanImage(device_, &descriptor);
if (texture_) {
// Keep a reference to the texture so that it stays valid (its content
// might be destroyed).
dawn_procs_->data.textureReference(texture_);
// Assume that the user of this representation will write to the texture
// so set the cleared flag so that other representations don't overwrite
// the result.
// TODO(cwallez@chromium.org): This is incorrect and allows reading
// uninitialized data. When !IsCleared we should tell dawn_native to
// consider the texture lazy-cleared.
SetCleared();
} else {
close(fd);
}
return texture_;
}
void SharedImageRepresentationDawnOzone::EndAccess() {
if (!texture_) {
return;
}
// TODO(hob): Synchronize access to the dma-buf by exporting the VkSemaphore
// from the WebGPU texture.
dawn_procs_->data.textureDestroy(texture_);
dawn_procs_->data.textureRelease(texture_);
texture_ = nullptr;
}
} // namespace gpu
// 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_REPRESENTATION_DAWN_OZONE_H_
#define GPU_COMMAND_BUFFER_SERVICE_SHARED_IMAGE_REPRESENTATION_DAWN_OZONE_H_
#include <dawn/dawn_proc_table.h>
#include <dawn/webgpu.h>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.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/native_pixmap.h"
namespace gpu {
// SharedImageRepresentation of a Ozone-backed SharedImage to be used by Dawn.
// On access, the pixmap backing the SharedImage is imported into Dawn for
// rendering.
class SharedImageRepresentationDawnOzone
: public SharedImageRepresentationDawn {
public:
SharedImageRepresentationDawnOzone(
SharedImageManager* manager,
SharedImageBacking* backing,
MemoryTypeTracker* tracker,
WGPUDevice device,
WGPUTextureFormat format,
scoped_refptr<gfx::NativePixmap> pixmap,
scoped_refptr<base::RefCountedData<DawnProcTable>> dawn_procs);
~SharedImageRepresentationDawnOzone() override;
WGPUTexture BeginAccess(WGPUTextureUsage usage) override;
void EndAccess() override;
private:
const WGPUDevice device_;
const WGPUTextureFormat format_;
scoped_refptr<gfx::NativePixmap> pixmap_;
WGPUTexture texture_ = nullptr;
scoped_refptr<base::RefCountedData<DawnProcTable>> dawn_procs_;
DISALLOW_COPY_AND_ASSIGN(SharedImageRepresentationDawnOzone);
};
} // namespace gpu
#endif // GPU_COMMAND_BUFFER_SERVICE_SHARED_IMAGE_REPRESENTATION_DAWN_OZONE_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment