0

Revert "Improve encapsulation of shmem and dxgi GpuMemoryBufferHandles"

This reverts commit 16a0b272da.

Reason for revert: Tree is closed due to "error: no member named 'set_region' in 'gfx::DXGIHandle'" error in https://ci.chromium.org/ui/p/chromium/builders/ci/win32-archive-rel/49415/overview

Original change's description:
> Improve encapsulation of shmem and dxgi GpuMemoryBufferHandles
>
> This CL attempts to move shared memory and DXGI GpuMemoryBufferHandles
> towards a model that are easier to move into a std::variant. The
> trickiest part of this is updating use of the shared memory region:
> previous refactorings here ignored this by simply dynamically
> dispatching depending on the type of the std::variant. While it is
> possible to continue implementing this behavior, being explicit here
> probably has long-term benefits for code readability...
>
> Summary of changes:
> - Extracting a shared memory regions or DXGI handle from a
>   GpuMemoryBufferHandle is now a consuming operation, i.e. the
>   GpuMemoryBufferHandle becomes an EMPTY_BUFFER.
> - Mutable getters and setters for these two subtypes no longer exist,
>   to make it impossible to have a SHARED_MEMORY_BUFFER with an invalid
>   region.
> - Add overloads for constructing a GpuMemoryBufferHandle from a shared
>   memory region or DXGI handle.
>
> Bug: 40584691
> Change-Id: I580a85be460035e063d1bf65e3c031104cb5410c
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6381717
> Reviewed-by: Frank Liberato <liberato@chromium.org>
> Reviewed-by: Piotr Bialecki <bialpio@chromium.org>
> Reviewed-by: Ilya Nikolaevskiy <ilnik@chromium.org>
> Reviewed-by: Colin Blundell <blundell@chromium.org>
> Reviewed-by: vikas soni <vikassoni@chromium.org>
> Commit-Queue: Daniel Cheng <dcheng@chromium.org>
> Reviewed-by: Min Chen <minch@chromium.org>
> Reviewed-by: Peter McNeeley <petermcneeley@google.com>
> Cr-Commit-Position: refs/heads/main@{#1445133}

Bug: 40584691
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Change-Id: I2c0b29869003b6ebd6f96a24c1d207b0b3546d01
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6438850
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Owners-Override: Nicola Tommasi <tommasin@chromium.org>
Auto-Submit: Nicola Tommasi <tommasin@chromium.org>
Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/heads/main@{#1445159}
This commit is contained in:
Nicola Tommasi
2025-04-10 02:14:07 -07:00
committed by Chromium LUCI CQ
parent 8a7d99bb34
commit acb1ed5b9e
31 changed files with 143 additions and 146 deletions

@ -472,8 +472,7 @@ class WinGpuMemoryBufferHandleHolder : public BufferHandleHolder {
ui::ContextFactory* context_factory)
: context_factory_(context_factory),
gmb_holder_(std::move(buffer_handle), context_factory),
sh_mem_holder_(
gmb_holder_.GetGpuMemoryBufferHandle().dxgi_handle().region()),
sh_mem_holder_(gmb_holder_.GetGpuMemoryBufferHandle().region()),
require_mapped_frame_callback_(
std::move(require_mapped_frame_callback)) {
CHECK_EQ(gmb_holder_.GetGpuMemoryBufferHandle().type,

@ -59,7 +59,9 @@ std::unique_ptr<Buffer> SharedMemory::CreateBuffer(const gfx::Size& size,
return nullptr;
}
gfx::GpuMemoryBufferHandle handle(shared_memory_region_.Duplicate());
gfx::GpuMemoryBufferHandle handle;
handle.type = gfx::SHARED_MEMORY_BUFFER;
handle.set_region(shared_memory_region_.Duplicate());
handle.offset = offset;
handle.stride = stride;

@ -117,8 +117,9 @@ class TestGpuService : public mojom::GpuService {
// size. However, as we don't have the requested format or size here,
// simply set hardcoded parameter values that ensure that this creation
// will succeed for the formats and sizes used in these tests.
handle.type = gfx::SHARED_MEMORY_BUFFER;
constexpr size_t kBufferSizeBytes = 6144;
handle = gfx::GpuMemoryBufferHandle(
handle.set_region(
base::UnsafeSharedMemoryRegion::Create(kBufferSizeBytes));
}
handle.id = req.id;

@ -364,8 +364,11 @@ gfx::GpuMemoryBufferHandle CreateHandle(ID3D11Device* d3d11_device) {
&texture_handle);
EXPECT_HRESULT_SUCCEEDED(hr);
return gfx::GpuMemoryBufferHandle(
gfx::GpuMemoryBufferHandle result;
result.type = gfx::GpuMemoryBufferType::DXGI_SHARED_HANDLE;
result.set_dxgi_handle(
gfx::DXGIHandle(base::win::ScopedHandle(texture_handle)));
return result;
}
} // namespace

@ -114,7 +114,9 @@ class DCOMPTextureWrapperTest : public testing::Test {
void DCOMPTextureWrapperTest::CreateDXBackedVideoFrameTestTask(
std::unique_ptr<media::DCOMPTextureWrapper> dcomp_texture_wrapper,
base::OnceClosure closure) {
gfx::GpuMemoryBufferHandle dx_handle(gfx::DXGIHandle::CreateFakeForTest());
gfx::GpuMemoryBufferHandle dx_handle;
dx_handle.type = gfx::GpuMemoryBufferType::DXGI_SHARED_HANDLE;
dx_handle.set_dxgi_handle(gfx::DXGIHandle::CreateFakeForTest());
gfx::Size frame_size(1920, 1080);
base::WaitableEvent wait_event;

@ -223,8 +223,10 @@ void OpenXrGraphicsBindingD3D11::CreateSharedImages(
}
}
gfx::GpuMemoryBufferHandle gpu_memory_buffer_handle{
gfx::DXGIHandle(base::win::ScopedHandle(shared_handle))};
gfx::GpuMemoryBufferHandle gpu_memory_buffer_handle;
gpu_memory_buffer_handle.type = gfx::DXGI_SHARED_HANDLE;
gpu_memory_buffer_handle.set_dxgi_handle(
gfx::DXGIHandle(base::win::ScopedHandle(shared_handle)));
// TODO(crbug.com/40918787): This size is the size of the texture
// from the OpenXr runtime, which is fine but does not work properly if the
@ -389,8 +391,7 @@ bool OpenXrGraphicsBindingD3D11::SetOverlayTexture(
}
return texture_helper_->SetOverlayTexture(
std::move(texture).dxgi_handle().TakeBufferHandle(), sync_token, left,
right);
texture.dxgi_handle().TakeBufferHandle(), sync_token, left, right);
}
} // namespace device

@ -56,10 +56,11 @@ void SharedImageInterface::CreateSharedMemoryRegionFromSIInfo(
base::TerminateBecauseOutOfMemory(buffer_size);
}
handle = gfx::GpuMemoryBufferHandle(std::move(shared_memory_region));
handle.type = gfx::SHARED_MEMORY_BUFFER;
handle.offset = 0;
handle.stride = static_cast<int32_t>(
gfx::RowSizeForBufferFormat(si_info.meta.size.width(), buffer_format, 0));
handle.set_region(std::move(shared_memory_region));
}
SharedImageInterface::SwapChainSharedImages::SwapChainSharedImages(

@ -74,7 +74,9 @@ class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer {
}
gfx::GpuMemoryBufferId GetId() const override { return id_; }
gfx::GpuMemoryBufferHandle CloneHandle() const override {
gfx::GpuMemoryBufferHandle handle(region_.Duplicate());
gfx::GpuMemoryBufferHandle handle;
handle.type = gfx::SHARED_MEMORY_BUFFER;
handle.set_region(region_.Duplicate());
handle.offset = base::checked_cast<uint32_t>(offset_);
handle.stride = base::checked_cast<uint32_t>(stride_);
return handle;

@ -61,11 +61,13 @@ gfx::GpuMemoryBufferHandle CreateGMBHandle(
base::UnsafeSharedMemoryRegion::Create(buffer_size);
CHECK(shared_memory_region.IsValid());
gfx::GpuMemoryBufferHandle handle(std::move(shared_memory_region));
gfx::GpuMemoryBufferHandle handle;
handle.type = gfx::SHARED_MEMORY_BUFFER;
handle.id = gfx::GpuMemoryBufferId(last_handle_id++);
handle.offset = 0;
handle.stride = static_cast<uint32_t>(
gfx::RowSizeForBufferFormat(size.width(), buffer_format, 0));
handle.set_region(std::move(shared_memory_region));
return handle;
}

@ -639,24 +639,16 @@ std::unique_ptr<SharedImageBacking> D3DImageBackingFactory::CreateSharedImage(
return nullptr;
}
if (handle.type != gfx::DXGI_SHARED_HANDLE) {
if (handle.type != gfx::DXGI_SHARED_HANDLE ||
!handle.dxgi_handle().IsValid()) {
LOG(ERROR) << "Invalid handle with type: " << handle.type;
return nullptr;
}
gfx::DXGIHandle dxgi_handle = std::move(handle).dxgi_handle();
// This shouldn't happen as the GpuMemoryBufferHandle constructor that takes a
// DXGIHandle asserts the handle is valid. However, it is currently possible
// for code to set the type to DXGI_SHARED_HANDLE directly but never actually
// set the handle. Make this an eventual CHECK() but handle this gracefully
// for now just in case.
CHECK(dxgi_handle.IsValid(), base::NotFatalUntil::M138);
if (!dxgi_handle.IsValid()) {
return nullptr;
}
scoped_refptr<DXGISharedHandleState> dxgi_shared_handle_state =
dxgi_shared_handle_manager_->GetOrCreateSharedHandleState(
dxgi_handle.token(), dxgi_handle.TakeBufferHandle(), d3d11_device_);
handle.dxgi_handle().token(), handle.dxgi_handle().TakeBufferHandle(),
d3d11_device_);
if (!dxgi_shared_handle_state) {
LOG(ERROR) << "Failed to retrieve matching DXGI shared handle state";
return nullptr;

@ -1225,8 +1225,10 @@ void D3DImageBackingFactoryTest::RunCreateSharedImageFromHandleTest(
&shared_handle);
ASSERT_EQ(hr, S_OK);
gfx::GpuMemoryBufferHandle gpu_memory_buffer_handle{
gfx::DXGIHandle(base::win::ScopedHandle(shared_handle))};
gfx::GpuMemoryBufferHandle gpu_memory_buffer_handle;
gpu_memory_buffer_handle.type = gfx::DXGI_SHARED_HANDLE;
gpu_memory_buffer_handle.set_dxgi_handle(
gfx::DXGIHandle(base::win::ScopedHandle(shared_handle)));
// Clone before moving the handle in CreateSharedImage.
auto dup_handle = gpu_memory_buffer_handle.Clone();
@ -1547,8 +1549,9 @@ D3DImageBackingFactoryTest::CreateVideoImage(const gfx::Size& size,
const gpu::Mailbox mailbox = gpu::Mailbox::Generate();
std::unique_ptr<SharedImageBacking> shared_image_backing;
if (use_factory) {
gfx::GpuMemoryBufferHandle gmb_handle(
gfx::DXGIHandle(std::move(shared_handle)));
gfx::GpuMemoryBufferHandle gmb_handle;
gmb_handle.type = gfx::DXGI_SHARED_HANDLE;
gmb_handle.set_dxgi_handle(gfx::DXGIHandle(std::move(shared_handle)));
DCHECK(gmb_handle.dxgi_handle().IsValid());
shared_image_backing = shared_image_factory_->CreateSharedImage(
@ -1856,7 +1859,9 @@ void D3DImageBackingFactoryTest::RunCreateFromSharedMemoryMultiplanarTest(
FillNV12(shm_mapping.GetMemoryAs<uint8_t>(), size, 255, 255, 255);
}
gfx::GpuMemoryBufferHandle shm_gmb_handle(shm_region.Duplicate());
gfx::GpuMemoryBufferHandle shm_gmb_handle;
shm_gmb_handle.type = gfx::SHARED_MEMORY_BUFFER;
shm_gmb_handle.set_region(shm_region.Duplicate());
DCHECK(shm_gmb_handle.region().IsValid());
shm_gmb_handle.stride = size.width();

@ -48,7 +48,7 @@ GpuMemoryBufferImplDXGI::CreateFromHandle(
DCHECK(handle.dxgi_handle().IsValid());
return base::WrapUnique(new GpuMemoryBufferImplDXGI(
handle.id, size, format, std::move(callback),
std::move(handle).dxgi_handle(), gpu_memory_buffer_manager,
std::move(handle.dxgi_handle()), gpu_memory_buffer_manager,
std::move(pool), premapped_memory));
}
@ -98,7 +98,8 @@ base::OnceClosure GpuMemoryBufferImplDXGI::AllocateForTesting(
DCHECK(SUCCEEDED(hr));
gfx::GpuMemoryBufferId kBufferId(1);
*handle = gfx::GpuMemoryBufferHandle(
handle->type = gfx::DXGI_SHARED_HANDLE;
handle->set_dxgi_handle(
gfx::DXGIHandle(base::win::ScopedHandle(texture_handle)));
handle->id = kBufferId;
return base::DoNothing();
@ -265,28 +266,16 @@ gfx::GpuMemoryBufferType GpuMemoryBufferImplDXGI::GetType() const {
}
gfx::GpuMemoryBufferHandle GpuMemoryBufferImplDXGI::CloneHandle() const {
gfx::GpuMemoryBufferHandle handle(dxgi_handle_.Clone());
gfx::GpuMemoryBufferHandle handle;
handle.type = gfx::DXGI_SHARED_HANDLE;
handle.id = id_;
handle.offset = 0;
handle.stride = stride(0);
handle.set_dxgi_handle(dxgi_handle_.Clone());
return handle;
}
void GpuMemoryBufferImplDXGI::SetUsePreMappedMemory(bool use_premapped_memory) {
use_premapped_memory_ = use_premapped_memory;
}
gfx::GpuMemoryBufferHandle GpuMemoryBufferImplDXGI::CloneHandleWithRegion(
base::UnsafeSharedMemoryRegion region) const {
gfx::GpuMemoryBufferHandle handle(
dxgi_handle_.CloneWithRegion(std::move(region)));
handle.id = id_;
handle.offset = 0;
handle.stride = stride(0);
return handle;
}
HANDLE GpuMemoryBufferImplDXGI::GetHandle() const {
return dxgi_handle_.buffer_handle();
}
@ -295,6 +284,10 @@ const gfx::DXGIHandleToken& GpuMemoryBufferImplDXGI::GetToken() const {
return dxgi_handle_.token();
}
void GpuMemoryBufferImplDXGI::SetUsePreMappedMemory(bool use_premapped_memory) {
use_premapped_memory_ = use_premapped_memory;
}
GpuMemoryBufferImplDXGI::GpuMemoryBufferImplDXGI(
gfx::GpuMemoryBufferId id,
const gfx::Size& size,

@ -17,7 +17,6 @@
#include "base/memory/raw_span.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/unsafe_shared_memory_pool.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/unguessable_token.h"
#include "base/win/scoped_handle.h"
#include "gpu/gpu_export.h"
@ -75,9 +74,6 @@ class GPU_EXPORT GpuMemoryBufferImplDXGI : public GpuMemoryBufferImpl {
// converted to MappableSI.
void SetUsePreMappedMemory(bool use_premapped_memory) override;
gfx::GpuMemoryBufferHandle CloneHandleWithRegion(
base::UnsafeSharedMemoryRegion region) const;
HANDLE GetHandle() const;
const gfx::DXGIHandleToken& GetToken() const;

@ -90,12 +90,13 @@ GpuMemoryBufferImplSharedMemory::CreateGpuMemoryBuffer(
if (!shared_memory_region.IsValid())
return gfx::GpuMemoryBufferHandle();
gfx::GpuMemoryBufferHandle handle(std::move(shared_memory_region));
gfx::GpuMemoryBufferHandle handle;
handle.type = gfx::SHARED_MEMORY_BUFFER;
handle.id = id;
handle.offset = 0;
handle.stride = static_cast<uint32_t>(
gfx::RowSizeForBufferFormat(size.width(), format, 0));
handle.set_region(std::move(shared_memory_region));
return handle;
}
@ -148,12 +149,10 @@ GpuMemoryBufferImplSharedMemory::CreateFromHandle(
return nullptr;
}
const gfx::GpuMemoryBufferId id = handle.id;
const uint32_t offset = handle.offset;
const uint32_t stride = handle.stride;
return base::WrapUnique(new GpuMemoryBufferImplSharedMemory(
id, size, format, usage, std::move(callback), std::move(handle).region(),
base::WritableSharedMemoryMapping(), offset, stride));
handle.id, size, format, usage, std::move(callback),
std::move(handle.region()), base::WritableSharedMemoryMapping(),
handle.offset, handle.stride));
}
// static
@ -277,10 +276,12 @@ gfx::GpuMemoryBufferType GpuMemoryBufferImplSharedMemory::GetType() const {
gfx::GpuMemoryBufferHandle GpuMemoryBufferImplSharedMemory::CloneHandle()
const {
gfx::GpuMemoryBufferHandle handle(shared_memory_region_.Duplicate());
gfx::GpuMemoryBufferHandle handle;
handle.type = gfx::SHARED_MEMORY_BUFFER;
handle.id = id_;
handle.offset = offset_;
handle.stride = stride_;
handle.set_region(shared_memory_region_.Duplicate());
return handle;
}

@ -212,7 +212,8 @@ gfx::GpuMemoryBufferHandle GpuMemoryBufferFactoryDXGI::CreateGpuMemoryBuffer(
return handle;
}
handle = gfx::GpuMemoryBufferHandle(
handle.type = gfx::DXGI_SHARED_HANDLE;
handle.set_dxgi_handle(
gfx::DXGIHandle(base::win::ScopedHandle(texture_handle)));
handle.id = id;

@ -122,6 +122,8 @@ bool GpuMemoryBufferTrackerWin::Init(const gfx::Size& dimensions,
std::move(dimensions));
}
gfx::GpuMemoryBufferHandle gmb_handle;
gmb_handle.type = gfx::DXGI_SHARED_HANDLE;
base::win::ScopedHandle scoped_handle =
CreateNV12Texture(d3d_device_.Get(), dimensions);
if (!scoped_handle.IsValid()) {
@ -133,7 +135,7 @@ bool GpuMemoryBufferTrackerWin::Init(const gfx::Size& dimensions,
return false;
}
gfx::GpuMemoryBufferHandle gmb_handle(std::move(dxgi_handle));
gmb_handle.set_dxgi_handle(std::move(dxgi_handle));
return CreateBufferInternal(std::move(gmb_handle), std::move(dimensions));
}
@ -228,7 +230,9 @@ GpuMemoryBufferTrackerWin::GetGpuMemoryBufferHandle() {
if (IsD3DDeviceChanged()) {
return gfx::GpuMemoryBufferHandle();
}
return buffer_->CloneHandleWithRegion(region_.Duplicate());
auto handle = buffer_->CloneHandle();
handle.set_region(region_.Duplicate());
return handle;
}
VideoCaptureBufferType GpuMemoryBufferTrackerWin::GetBufferType() {

@ -2392,7 +2392,9 @@ HRESULT VideoCaptureDeviceMFWin::DeliverExternalBufferToClient(
frame_metadata.background_blur = GetBackgroundBlurState();
// Set reused |token| and |share_handle| to gmb handle.
gfx::GpuMemoryBufferHandle gmb_handle(private_data->CloneHandle());
gfx::GpuMemoryBufferHandle gmb_handle;
gmb_handle.type = gfx::GpuMemoryBufferType::DXGI_SHARED_HANDLE;
gmb_handle.set_dxgi_handle(private_data->CloneHandle());
media::CapturedExternalVideoBuffer external_buffer =
media::CapturedExternalVideoBuffer(

@ -1182,7 +1182,9 @@ class MockCaptureHandleProvider
// Clone a |GpuMemoryBufferHandle| for IPC.
gfx::GpuMemoryBufferHandle GetGpuMemoryBufferHandle() override {
gfx::GpuMemoryBufferHandle handle(gfx::DXGIHandle::CreateFakeForTest());
gfx::GpuMemoryBufferHandle handle;
handle.type = gfx::GpuMemoryBufferType::DXGI_SHARED_HANDLE;
handle.set_dxgi_handle(gfx::DXGIHandle::CreateFakeForTest());
return handle;
}
};

@ -143,8 +143,9 @@ class D3D12VideoEncodeAcceleratorTest : public testing::Test {
}
scoped_refptr<VideoFrame> CreateTestVideoFrame() {
gfx::GpuMemoryBufferHandle fake_handle(
gfx::DXGIHandle::CreateFakeForTest());
gfx::GpuMemoryBufferHandle fake_handle;
fake_handle.type = gfx::DXGI_SHARED_HANDLE;
fake_handle.set_dxgi_handle(gfx::DXGIHandle::CreateFakeForTest());
const auto si_usage = gpu::SHARED_IMAGE_USAGE_CPU_WRITE_ONLY |
gpu::SHARED_IMAGE_USAGE_DISPLAY_READ;

@ -117,8 +117,10 @@ class MFVideoProcessorAcceleratorTest : public ::testing::Test {
return nullptr;
}
gfx::GpuMemoryBufferHandle gmb_handle{
gfx::DXGIHandle(base::win::ScopedHandle(shared_handle))};
gfx::GpuMemoryBufferHandle gmb_handle;
gmb_handle.type = gfx::DXGI_SHARED_HANDLE;
gmb_handle.set_dxgi_handle(
gfx::DXGIHandle(base::win::ScopedHandle(shared_handle)));
const auto si_usage = gpu::SHARED_IMAGE_USAGE_CPU_WRITE_ONLY |
gpu::SHARED_IMAGE_USAGE_DISPLAY_READ;

@ -259,7 +259,9 @@ class MediaFoundationRendererClientTest
}
void InitializeFramePool() {
gfx::GpuMemoryBufferHandle gpu_handle(gfx::DXGIHandle::CreateFakeForTest());
gfx::GpuMemoryBufferHandle gpu_handle;
gpu_handle.type = gfx::GpuMemoryBufferType::DXGI_SHARED_HANDLE;
gpu_handle.set_dxgi_handle(gfx::DXGIHandle::CreateFakeForTest());
auto frame_info = media::mojom::FrameTextureInfo::New();
frame_info->token = base::UnguessableToken::Create();

@ -209,8 +209,10 @@ void MediaFoundationRendererWrapper::OnFramePoolInitialized(
auto pool_params = media::mojom::FramePoolInitializationParameters::New();
for (auto& texture : frame_textures) {
auto frame_info = media::mojom::FrameTextureInfo::New();
gfx::GpuMemoryBufferHandle gpu_handle(
gfx::DXGIHandle(std::move(texture.dxgi_handle)));
gfx::GpuMemoryBufferHandle gpu_handle;
gpu_handle.type = gfx::GpuMemoryBufferType::DXGI_SHARED_HANDLE;
gpu_handle.set_dxgi_handle(gfx::DXGIHandle(std::move(texture.dxgi_handle)));
frame_info->token = texture.token;
frame_info->texture_handle = std::move(gpu_handle);

@ -80,7 +80,7 @@ crosapi::mojom::GpuMemoryBufferHandlePtr ToCrosapiGpuMemoryBufferHandle(
if (buffer_handle.type == gfx::GpuMemoryBufferType::SHARED_MEMORY_BUFFER) {
crosapi_gpu_handle->platform_handle =
crosapi::mojom::GpuMemoryBufferPlatformHandle::NewSharedMemoryHandle(
std::move(buffer_handle).region());
std::move(buffer_handle.region()));
} else if (buffer_handle.type == gfx::GpuMemoryBufferType::NATIVE_PIXMAP) {
auto crosapi_native_pixmap_handle =
crosapi::mojom::NativePixmapHandle::New();

@ -189,9 +189,8 @@ bool XRFrameTransport::FrameSubmit(
// passed over IPC.
vr_presentation_provider->SubmitFrameWithTextureHandle(
vr_frame_id,
mojo::PlatformHandle(std::move(gpu_memory_buffer_handle)
.dxgi_handle()
.TakeBufferHandle()),
mojo::PlatformHandle(
gpu_memory_buffer_handle.dxgi_handle().TakeBufferHandle()),
sync_token);
#else
NOTIMPLEMENTED();

@ -411,7 +411,7 @@ bool VideoCaptureImpl::ProcessBuffer(
!buffer_context->data()) {
auto gmb_handle = buffer_context->CloneGpuMemoryBufferHandle();
buffer_context->InitializeFromUnsafeShmemRegion(
std::move(gmb_handle).dxgi_handle().TakeRegion());
std::move(gmb_handle.region()));
DCHECK(buffer_context->data());
}
RequirePremappedFrames();

@ -761,22 +761,24 @@ struct FuzzTraits<gfx::GpuMemoryBufferHandle> {
auto buffer_type = static_cast<gfx::GpuMemoryBufferType>(type);
switch (buffer_type) {
case gfx::SHARED_MEMORY_BUFFER: {
p->type = buffer_type;
base::UnsafeSharedMemoryRegion region;
if (!FuzzParam(&region, fuzzer)) {
return false;
}
*p = gfx::GpuMemoryBufferHandle(std::move(region));
p->set_region(std::move(region));
break;
}
#if BUILDFLAG(IS_WIN)
case gfx::DXGI_SHARED_HANDLE: {
gfx::DXGIHandle dxgi_handle = gfx::DXGIHandle::CreateFakeForTest();
p->type = buffer_type;
base::UnsafeSharedMemoryRegion region;
if (!FuzzParam(&region, fuzzer)) {
return false;
}
gfx::DXGIHandle dxgi_handle = gfx::DXGIHandle::CreateFakeForTest();
dxgi_handle.set_region(std::move(region));
*p = gfx::GpuMemoryBufferHandle(std::move(dxgi_handle));
p->set_dxgi_handle(std::move(dxgi_handle));
break;
}
#endif

@ -69,14 +69,6 @@ DXGIHandle DXGIHandle::Clone() const {
return handle;
}
DXGIHandle DXGIHandle::CloneWithRegion(
base::UnsafeSharedMemoryRegion region) const {
DXGIHandle handle = Clone();
DCHECK(!handle.region_.IsValid());
handle.region_ = std::move(region);
return handle;
}
base::win::ScopedHandle DXGIHandle::TakeBufferHandle() {
DCHECK(buffer_handle_.is_valid());
return std::move(buffer_handle_);
@ -85,19 +77,6 @@ base::win::ScopedHandle DXGIHandle::TakeBufferHandle() {
GpuMemoryBufferHandle::GpuMemoryBufferHandle() = default;
GpuMemoryBufferHandle::GpuMemoryBufferHandle(
base::UnsafeSharedMemoryRegion region)
: type(GpuMemoryBufferType::SHARED_MEMORY_BUFFER),
region_(std::move(region)) {}
#if BUILDFLAG(IS_WIN)
GpuMemoryBufferHandle::GpuMemoryBufferHandle(DXGIHandle handle)
: type(GpuMemoryBufferType::DXGI_SHARED_HANDLE),
dxgi_handle_(std::move(handle)) {
CHECK(dxgi_handle_.IsValid());
}
#endif
#if BUILDFLAG(IS_ANDROID)
GpuMemoryBufferHandle::GpuMemoryBufferHandle(
base::android::ScopedHardwareBufferHandle handle)

@ -37,18 +37,7 @@ class MemoryAllocatorDumpGuid;
} // namespace trace_event
} // namespace base
namespace mojo {
template <typename, typename>
struct StructTraits;
template <typename, typename>
struct UnionTraits;
} // namespace mojo
namespace gfx {
namespace mojom {
class DXGIHandleDataView;
class GpuMemoryBufferPlatformHandleDataView;
} // namespace mojom
class ColorSpace;
@ -120,11 +109,6 @@ class COMPONENT_EXPORT(GFX) DXGIHandle {
// `token()` to check if two `DXGIHandle`s actually refer to the same
// resource.
DXGIHandle Clone() const;
// Similar to above but also associate `region` with the cloned `DXGIHandle`.
//
// Precondition: `this` must not have an associated region, i.e.
// `region().IsValid()` is false.
DXGIHandle CloneWithRegion(base::UnsafeSharedMemoryRegion region) const;
HANDLE buffer_handle() const { return buffer_handle_.Get(); }
base::win::ScopedHandle TakeBufferHandle();
@ -132,11 +116,12 @@ class COMPONENT_EXPORT(GFX) DXGIHandle {
const DXGIHandleToken& token() const { return token_; }
const base::UnsafeSharedMemoryRegion& region() const { return region_; }
base::UnsafeSharedMemoryRegion TakeRegion() { return std::move(region_); }
base::UnsafeSharedMemoryRegion& region() { return region_; }
void set_region(base::UnsafeSharedMemoryRegion region) {
region_ = std::move(region);
}
private:
friend mojo::StructTraits<mojom::DXGIHandleDataView, DXGIHandle>;
base::win::ScopedHandle buffer_handle_;
DXGIHandleToken token_;
base::UnsafeSharedMemoryRegion region_;
@ -150,10 +135,6 @@ struct COMPONENT_EXPORT(GFX) GpuMemoryBufferHandle {
static constexpr GpuMemoryBufferId kInvalidId = GpuMemoryBufferId(-1);
GpuMemoryBufferHandle();
explicit GpuMemoryBufferHandle(base::UnsafeSharedMemoryRegion region);
#if BUILDFLAG(IS_WIN)
explicit GpuMemoryBufferHandle(DXGIHandle handle);
#endif
#if BUILDFLAG(IS_ANDROID)
explicit GpuMemoryBufferHandle(
base::android::ScopedHardwareBufferHandle handle);
@ -171,11 +152,7 @@ struct COMPONENT_EXPORT(GFX) GpuMemoryBufferHandle {
// into memory actually requires an IPC to the GPU process, which then copies
// the contents into the shmem region so it can be accessed from other
// processes.
const base::UnsafeSharedMemoryRegion& region() const& {
// For now, allow this to transparently forward as appropriate for DXGI or
// shmem handles in production. However, this will be a hard CHECK() in the
// future.
CHECK_EQ(type, SHARED_MEMORY_BUFFER, base::NotFatalUntil::M138);
const base::UnsafeSharedMemoryRegion& region() const {
switch (type) {
case SHARED_MEMORY_BUFFER:
return region_;
@ -187,21 +164,46 @@ struct COMPONENT_EXPORT(GFX) GpuMemoryBufferHandle {
NOTREACHED();
}
}
base::UnsafeSharedMemoryRegion region() && {
CHECK_EQ(type, SHARED_MEMORY_BUFFER);
type = EMPTY_BUFFER;
return std::move(region_);
base::UnsafeSharedMemoryRegion& region() {
switch (type) {
case SHARED_MEMORY_BUFFER:
return region_;
#if BUILDFLAG(IS_WIN)
case DXGI_SHARED_HANDLE:
return dxgi_handle_.region();
#endif // BUILDFLAG(IS_WIN)
default:
NOTREACHED();
}
}
void set_region(base::UnsafeSharedMemoryRegion region) {
switch (type) {
case SHARED_MEMORY_BUFFER:
region_ = std::move(region);
return;
#if BUILDFLAG(IS_WIN)
case DXGI_SHARED_HANDLE:
dxgi_handle_.set_region(std::move(region));
return;
#endif // BUILDFLAG(IS_WIN)
default:
NOTREACHED();
}
}
#if BUILDFLAG(IS_WIN)
const DXGIHandle& dxgi_handle() const& {
const DXGIHandle& dxgi_handle() const {
CHECK_EQ(type, DXGI_SHARED_HANDLE);
return dxgi_handle_;
}
DXGIHandle dxgi_handle() && {
DXGIHandle& dxgi_handle() {
CHECK_EQ(type, DXGI_SHARED_HANDLE);
type = EMPTY_BUFFER;
return std::move(dxgi_handle_);
return dxgi_handle_;
}
void set_dxgi_handle(DXGIHandle handle) {
CHECK_EQ(type, DXGI_SHARED_HANDLE);
CHECK(handle.IsValid());
dxgi_handle_ = std::move(handle);
}
#endif // BUILDFLAG(IS_WIN)
@ -219,9 +221,6 @@ struct COMPONENT_EXPORT(GFX) GpuMemoryBufferHandle {
#endif
private:
friend mojo::UnionTraits<mojom::GpuMemoryBufferPlatformHandleDataView,
GpuMemoryBufferHandle>;
// This naming isn't entirely styleguide-compliant, but per the TODO, the end
// goal is to make `this` an encapsulated class.
base::UnsafeSharedMemoryRegion region_;

@ -180,8 +180,10 @@ TEST_F(StructTraitsTest, GpuMemoryBufferHandle) {
ASSERT_TRUE(shared_memory_region.IsValid());
ASSERT_TRUE(shared_memory_region.Map().IsValid());
gfx::GpuMemoryBufferHandle handle(shared_memory_region.Duplicate());
gfx::GpuMemoryBufferHandle handle;
handle.type = gfx::SHARED_MEMORY_BUFFER;
handle.id = kId;
handle.set_region(shared_memory_region.Duplicate());
handle.offset = kOffset;
handle.stride = kStride;
@ -193,7 +195,7 @@ TEST_F(StructTraitsTest, GpuMemoryBufferHandle) {
EXPECT_EQ(kOffset, output.offset);
EXPECT_EQ(kStride, output.stride);
base::UnsafeSharedMemoryRegion output_memory = std::move(output).region();
base::UnsafeSharedMemoryRegion output_memory = std::move(output.region());
EXPECT_TRUE(output_memory.Map().IsValid());
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_OZONE)

@ -233,7 +233,7 @@ bool UnionTraits<gfx::mojom::GpuMemoryBufferPlatformHandleDataView,
switch (data.tag()) {
case Tag::kSharedMemoryHandle:
handle->type = gfx::SHARED_MEMORY_BUFFER;
return data.ReadSharedMemoryHandle(&handle->region_);
return data.ReadSharedMemoryHandle(&handle->region());
#if BUILDFLAG(IS_APPLE)
case Tag::kMachPort:
handle->type = gfx::IO_SURFACE_BUFFER;
@ -259,7 +259,7 @@ bool UnionTraits<gfx::mojom::GpuMemoryBufferPlatformHandleDataView,
#if BUILDFLAG(IS_WIN)
case Tag::kDxgiHandle:
handle->type = gfx::DXGI_SHARED_HANDLE;
return data.ReadDxgiHandle(&handle->dxgi_handle_);
return data.ReadDxgiHandle(&handle->dxgi_handle());
#endif // BUILDFLAG(IS_WIN)
#if BUILDFLAG(IS_ANDROID)
case Tag::kAndroidHardwareBufferHandle:

@ -117,9 +117,9 @@ struct COMPONENT_EXPORT(GFX_NATIVE_HANDLE_TYPES_SHARED_MOJOM_TRAITS)
return handle.token();
}
static base::UnsafeSharedMemoryRegion& shared_memory_handle(
static base::UnsafeSharedMemoryRegion shared_memory_handle(
gfx::DXGIHandle& handle) {
return handle.region_;
return std::move(handle.region());
}
static bool Read(gfx::mojom::DXGIHandleDataView data,
@ -152,7 +152,7 @@ struct COMPONENT_EXPORT(GFX_NATIVE_HANDLE_TYPES_SHARED_MOJOM_TRAITS)
static base::UnsafeSharedMemoryRegion& shared_memory_handle(
gfx::GpuMemoryBufferHandle& handle) {
return handle.region_;
return handle.region();
}
#if BUILDFLAG(IS_APPLE)
@ -168,7 +168,7 @@ struct COMPONENT_EXPORT(GFX_NATIVE_HANDLE_TYPES_SHARED_MOJOM_TRAITS)
#if BUILDFLAG(IS_WIN)
static gfx::DXGIHandle& dxgi_handle(gfx::GpuMemoryBufferHandle& handle) {
return handle.dxgi_handle_;
return handle.dxgi_handle();
}
#endif // BUILDFLAG(IS_WIN)