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
gpu / command_buffer / common / buffer.h [blame]
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef GPU_COMMAND_BUFFER_COMMON_BUFFER_H_
#define GPU_COMMAND_BUFFER_COMMON_BUFFER_H_
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <utility>
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/trace_event/memory_allocator_dump.h"
#include "gpu/gpu_export.h"
namespace gpu {
class GPU_EXPORT BufferBacking {
public:
virtual ~BufferBacking() = default;
virtual const base::UnsafeSharedMemoryRegion& shared_memory_region() const;
virtual base::UnguessableToken GetGUID() const;
void* GetMemory() {
return const_cast<void*>(std::as_const(*this).GetMemory());
}
virtual const void* GetMemory() const = 0;
virtual uint32_t GetSize() const = 0;
};
class GPU_EXPORT MemoryBufferBacking : public BufferBacking {
public:
explicit MemoryBufferBacking(uint32_t size, uint32_t alignment = 0);
MemoryBufferBacking(const MemoryBufferBacking&) = delete;
MemoryBufferBacking& operator=(const MemoryBufferBacking&) = delete;
~MemoryBufferBacking() override;
const void* GetMemory() const override;
uint32_t GetSize() const override;
private:
std::unique_ptr<char[]> memory_;
uint32_t size_;
uint32_t alignment_;
};
class GPU_EXPORT SharedMemoryBufferBacking : public BufferBacking {
public:
SharedMemoryBufferBacking(
base::UnsafeSharedMemoryRegion shared_memory_region,
base::WritableSharedMemoryMapping shared_memory_mapping);
SharedMemoryBufferBacking(const SharedMemoryBufferBacking&) = delete;
SharedMemoryBufferBacking& operator=(const SharedMemoryBufferBacking&) =
delete;
~SharedMemoryBufferBacking() override;
const base::UnsafeSharedMemoryRegion& shared_memory_region() const override;
base::UnguessableToken GetGUID() const override;
const void* GetMemory() const override;
uint32_t GetSize() const override;
private:
base::UnsafeSharedMemoryRegion shared_memory_region_;
base::WritableSharedMemoryMapping shared_memory_mapping_;
};
// Buffer owns a piece of shared-memory of a certain size.
class GPU_EXPORT Buffer : public base::RefCountedThreadSafe<Buffer> {
public:
explicit Buffer(std::unique_ptr<BufferBacking> backing);
Buffer(const Buffer&) = delete;
Buffer& operator=(const Buffer&) = delete;
BufferBacking* backing() const { return backing_.get(); }
void* memory() { return memory_; }
const void* memory() const { return memory_; }
uint32_t size() const { return size_; }
// Returns nullptr if the address overflows the memory.
void* GetDataAddress(uint32_t data_offset, uint32_t data_size) const;
// Returns nullptr if the address overflows the memory.
void* GetDataAddressAndSize(uint32_t data_offset, uint32_t* data_size) const;
// Returns the remaining size of the buffer after an offset
uint32_t GetRemainingSize(uint32_t data_offset) const;
private:
friend class base::RefCountedThreadSafe<Buffer>;
~Buffer();
std::unique_ptr<BufferBacking> backing_;
raw_ptr<void> memory_;
uint32_t size_;
};
inline std::unique_ptr<BufferBacking> MakeBackingFromSharedMemory(
base::UnsafeSharedMemoryRegion shared_memory_region,
base::WritableSharedMemoryMapping shared_memory_mapping) {
return std::make_unique<SharedMemoryBufferBacking>(
std::move(shared_memory_region), std::move(shared_memory_mapping));
}
inline scoped_refptr<Buffer> MakeBufferFromSharedMemory(
base::UnsafeSharedMemoryRegion shared_memory_region,
base::WritableSharedMemoryMapping shared_memory_mapping) {
return base::MakeRefCounted<Buffer>(MakeBackingFromSharedMemory(
std::move(shared_memory_region), std::move(shared_memory_mapping)));
}
inline scoped_refptr<Buffer> MakeMemoryBuffer(uint32_t size,
uint32_t alignment = 0) {
return base::MakeRefCounted<Buffer>(
std::make_unique<MemoryBufferBacking>(size, alignment));
}
// Generates a process unique buffer ID which can be safely used with
// GetBufferGUIDForTracing.
GPU_EXPORT int32_t GetNextBufferId();
// Generates GUID which can be used to trace buffer using an Id.
GPU_EXPORT base::trace_event::MemoryAllocatorDumpGuid GetBufferGUIDForTracing(
uint64_t tracing_process_id,
int32_t buffer_id);
} // namespace gpu
#endif // GPU_COMMAND_BUFFER_COMMON_BUFFER_H_