#ifndef GPU_COMMAND_BUFFER_CLIENT_INTERNAL_MAPPABLE_BUFFER_DXGI_H_
#define GPU_COMMAND_BUFFER_CLIENT_INTERNAL_MAPPABLE_BUFFER_DXGI_H_
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <optional>
#include "base/containers/span.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#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 "components/viz/common/resources/shared_image_format.h"
#include "gpu/command_buffer/client/gpu_command_buffer_client_export.h"
#include "gpu/command_buffer/client/internal/mappable_buffer.h"
#include "ui/gfx/color_space.h"
namespace gpu {
class ClientSharedImage;
class GPU_COMMAND_BUFFER_CLIENT_EXPORT MappableBufferDXGI
: public MappableBuffer {
public:
MappableBufferDXGI(const MappableBufferDXGI&) = delete;
MappableBufferDXGI& operator=(const MappableBufferDXGI&) = delete;
~MappableBufferDXGI() override;
static constexpr gfx::GpuMemoryBufferType kBufferType =
gfx::DXGI_SHARED_HANDLE;
static std::unique_ptr<MappableBufferDXGI> CreateFromHandleForTesting(
gfx::GpuMemoryBufferHandle handle,
const gfx::Size& size,
viz::SharedImageFormat format) {
return CreateFromHandle(std::move(handle), size, format);
}
static base::OnceClosure AllocateForTesting(
const gfx::Size& size,
viz::SharedImageFormat format,
gfx::BufferUsage usage,
gfx::GpuMemoryBufferHandle* handle);
bool Map() override;
void MapAsync(base::OnceCallback<void(bool)> result_cb) override;
bool AsyncMappingIsNonBlocking() const override;
void* memory(size_t plane) override;
void Unmap() override;
int stride(size_t plane) const override;
gfx::GpuMemoryBufferType GetType() const override;
gfx::GpuMemoryBufferHandle CloneHandle() const override;
void SetUsePreMappedMemory(bool use_premapped_memory) override;
gfx::GpuMemoryBufferHandle CloneHandleWithRegion(
base::UnsafeSharedMemoryRegion region) const;
HANDLE GetHandle() const;
const gfx::DXGIHandleToken& GetToken() const;
private:
friend ClientSharedImage;
static std::unique_ptr<MappableBufferDXGI> CreateFromHandle(
gfx::GpuMemoryBufferHandle handle,
const gfx::Size& size,
viz::SharedImageFormat format,
CopyNativeBufferToShMemCallback copy_native_buffer_to_shmem_callback =
CopyNativeBufferToShMemCallback(),
scoped_refptr<base::UnsafeSharedMemoryPool> pool = nullptr);
MappableBufferDXGI(
const gfx::Size& size,
viz::SharedImageFormat format,
gfx::DXGIHandle dxgi_handle,
CopyNativeBufferToShMemCallback copy_native_buffer_to_shmem_callback,
scoped_refptr<base::UnsafeSharedMemoryPool> pool);
std::optional<base::OnceCallback<void(void)>> DoMapAsync(
base::OnceCallback<void(bool)>);
void CheckAsyncMapResult(bool result);
void AssertMapped();
const gfx::Size size_;
const viz::SharedImageFormat format_;
bool use_premapped_memory_ = false;
gfx::DXGIHandle dxgi_handle_;
base::WritableSharedMemoryMapping region_mapping_;
CopyNativeBufferToShMemCallback copy_native_buffer_to_shmem_callback_;
base::Lock map_lock_;
uint32_t map_count_ GUARDED_BY(map_lock_) = 0u;
std::vector<base::OnceCallback<void(bool)>> map_callbacks_
GUARDED_BY(map_lock_);
scoped_refptr<base::UnsafeSharedMemoryPool> shared_memory_pool_;
std::unique_ptr<base::UnsafeSharedMemoryPool::Handle> shared_memory_handle_;
base::raw_span<uint8_t> premapped_memory_;
bool async_mapping_in_progress_ GUARDED_BY(map_lock_) = false;
};
}
#endif