1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/gpu_memory_buffer.h"
#include "base/logging.h"
#include "build/build_config.h"
#include "ui/gfx/generic_shared_memory_id.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#include "base/win/scoped_handle.h"
#endif
namespace gfx {
#if BUILDFLAG(IS_WIN)
namespace {
base::win::ScopedHandle CloneDXGIHandle(HANDLE handle) {
HANDLE target_handle = nullptr;
if (!::DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(),
&target_handle, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
DVPLOG(1) << "Error duplicating GMB DXGI handle";
}
return base::win::ScopedHandle(target_handle);
}
} // namespace
DXGIHandle DXGIHandle::CreateFakeForTest() {
// DXGIHandle requires a valid HANDLE, so just create a placeholder event.
base::win::ScopedHandle fake_handle(
::CreateEvent(nullptr, FALSE, FALSE, nullptr));
return DXGIHandle(std::move(fake_handle));
}
DXGIHandle::DXGIHandle() = default;
DXGIHandle::~DXGIHandle() = default;
DXGIHandle::DXGIHandle(base::win::ScopedHandle buffer_handle)
: buffer_handle_(std::move(buffer_handle)) {
DCHECK(buffer_handle_.is_valid());
}
DXGIHandle::DXGIHandle(base::win::ScopedHandle buffer_handle,
const DXGIHandleToken& token,
base::UnsafeSharedMemoryRegion region)
: buffer_handle_(std::move(buffer_handle)),
token_(token),
region_(std::move(region)) {
DCHECK(buffer_handle_.is_valid());
}
DXGIHandle::DXGIHandle(DXGIHandle&&) = default;
DXGIHandle& DXGIHandle::operator=(DXGIHandle&&) = default;
bool DXGIHandle::IsValid() const {
return buffer_handle_.is_valid();
}
DXGIHandle DXGIHandle::Clone() const {
DXGIHandle handle;
if (buffer_handle_.is_valid()) {
handle.buffer_handle_ = CloneDXGIHandle(buffer_handle_.Get());
}
handle.token_ = token_;
handle.region_ = region_.Duplicate();
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_);
}
#endif // BUILDFLAG(IS_WIN)
GpuMemoryBufferHandle::GpuMemoryBufferHandle() = default;
GpuMemoryBufferHandle::GpuMemoryBufferHandle(
base::UnsafeSharedMemoryRegion region)
: type(GpuMemoryBufferType::SHARED_MEMORY_BUFFER),
region_(std::move(region)) {
CHECK(region_.IsValid(), base::NotFatalUntil::M141);
}
#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_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
GpuMemoryBufferHandle::GpuMemoryBufferHandle(
NativePixmapHandle native_pixmap_handle)
: type(GpuMemoryBufferType::NATIVE_PIXMAP),
native_pixmap_handle_(std::move(native_pixmap_handle)) {}
#endif
#if BUILDFLAG(IS_ANDROID)
GpuMemoryBufferHandle::GpuMemoryBufferHandle(
base::android::ScopedHardwareBufferHandle handle)
: type(GpuMemoryBufferType::ANDROID_HARDWARE_BUFFER),
android_hardware_buffer(std::move(handle)) {}
#endif
// TODO(crbug.com/40584691): Reset |type| and possibly the handles on the
// moved-from object.
GpuMemoryBufferHandle::GpuMemoryBufferHandle(GpuMemoryBufferHandle&& other) =
default;
GpuMemoryBufferHandle& GpuMemoryBufferHandle::operator=(
GpuMemoryBufferHandle&& other) = default;
GpuMemoryBufferHandle::~GpuMemoryBufferHandle() = default;
void GpuMemoryBuffer::MapAsync(base::OnceCallback<void(bool)> result_cb) {
std::move(result_cb).Run(Map());
}
bool GpuMemoryBuffer::AsyncMappingIsNonBlocking() const {
return false;
}
GpuMemoryBufferHandle GpuMemoryBufferHandle::Clone() const {
GpuMemoryBufferHandle handle;
handle.type = type;
handle.id = id;
handle.offset = offset;
handle.stride = stride;
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
handle.native_pixmap_handle_ = CloneHandleForIPC(native_pixmap_handle_);
#elif BUILDFLAG(IS_APPLE)
handle.io_surface = io_surface;
#elif BUILDFLAG(IS_WIN)
handle.dxgi_handle_ = dxgi_handle_.Clone();
#elif BUILDFLAG(IS_ANDROID)
NOTIMPLEMENTED();
#endif
handle.region_ = region_.Duplicate();
return handle;
}
} // namespace gfx
|