1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GPU_COMMAND_BUFFER_CLIENT_MAPPED_MEMORY_H_
6 #define GPU_COMMAND_BUFFER_CLIENT_MAPPED_MEMORY_H_
8 #include "base/memory/scoped_vector.h"
9 #include "gpu/command_buffer/client/fenced_allocator.h"
10 #include "gpu/command_buffer/common/buffer.h"
11 #include "gpu/command_buffer/common/types.h"
12 #include "gpu/gpu_export.h"
16 class CommandBufferHelper;
18 // Manages a shared memory segment.
19 class GPU_EXPORT MemoryChunk {
21 MemoryChunk(int32 shm_id,
22 scoped_refptr<gpu::Buffer> shm,
23 CommandBufferHelper* helper);
26 // Gets the size of the largest free block that is available without waiting.
27 unsigned int GetLargestFreeSizeWithoutWaiting() {
28 return allocator_.GetLargestFreeSize();
31 // Gets the size of the largest free block that can be allocated if the
33 unsigned int GetLargestFreeSizeWithWaiting() {
34 return allocator_.GetLargestFreeOrPendingSize();
37 // Gets the size of the chunk.
38 unsigned int GetSize() const {
39 return static_cast<unsigned int>(shm_->size());
42 // The shared memory id for this chunk.
43 int32 shm_id() const {
47 // Allocates a block of memory. If the buffer is out of directly available
48 // memory, this function may wait until memory that was freed "pending a
49 // token" can be re-used.
52 // size: the size of the memory block to allocate.
55 // the pointer to the allocated memory block, or NULL if out of
57 void* Alloc(unsigned int size) {
58 return allocator_.Alloc(size);
61 // Gets the offset to a memory block given the base memory and the address.
62 // It translates NULL to FencedAllocator::kInvalidOffset.
63 unsigned int GetOffset(void* pointer) {
64 return allocator_.GetOffset(pointer);
67 // Frees a block of memory.
70 // pointer: the pointer to the memory block to free.
71 void Free(void* pointer) {
72 allocator_.Free(pointer);
75 // Frees a block of memory, pending the passage of a token. That memory won't
76 // be re-allocated until the token has passed through the command stream.
79 // pointer: the pointer to the memory block to free.
80 // token: the token value to wait for before re-using the memory.
81 void FreePendingToken(void* pointer, unsigned int token) {
82 allocator_.FreePendingToken(pointer, token);
85 // Frees any blocks whose tokens have passed.
87 allocator_.FreeUnused();
90 // Returns true if pointer is in the range of this block.
91 bool IsInChunk(void* pointer) const {
92 return pointer >= shm_->memory() &&
94 reinterpret_cast<const int8*>(shm_->memory()) + shm_->size();
97 // Returns true of any memory in this chunk is in use.
99 return allocator_.InUse();
102 size_t bytes_in_use() const {
103 return allocator_.bytes_in_use();
108 scoped_refptr<gpu::Buffer> shm_;
109 FencedAllocatorWrapper allocator_;
111 DISALLOW_COPY_AND_ASSIGN(MemoryChunk);
114 // Manages MemoryChunks.
115 class GPU_EXPORT MappedMemoryManager {
121 // |unused_memory_reclaim_limit|: When exceeded this causes pending memory
122 // to be reclaimed before allocating more memory.
123 MappedMemoryManager(CommandBufferHelper* helper,
124 size_t unused_memory_reclaim_limit);
126 ~MappedMemoryManager();
128 unsigned int chunk_size_multiple() const {
129 return chunk_size_multiple_;
132 void set_chunk_size_multiple(unsigned int multiple) {
133 chunk_size_multiple_ = multiple;
136 // Allocates a block of memory
138 // size: size of memory to allocate.
139 // shm_id: pointer to variable to receive the shared memory id.
140 // shm_offset: pointer to variable to receive the shared memory offset.
142 // pointer to allocated block of memory. NULL if failure.
144 unsigned int size, int32* shm_id, unsigned int* shm_offset);
146 // Frees a block of memory.
149 // pointer: the pointer to the memory block to free.
150 void Free(void* pointer);
152 // Frees a block of memory, pending the passage of a token. That memory won't
153 // be re-allocated until the token has passed through the command stream.
156 // pointer: the pointer to the memory block to free.
157 // token: the token value to wait for before re-using the memory.
158 void FreePendingToken(void* pointer, int32 token);
160 // Free Any Shared memory that is not in use.
164 size_t num_chunks() const {
165 return chunks_.size();
169 size_t allocated_memory() const {
170 return allocated_memory_;
174 typedef ScopedVector<MemoryChunk> MemoryChunkVector;
176 // size a chunk is rounded up to.
177 unsigned int chunk_size_multiple_;
178 CommandBufferHelper* helper_;
179 MemoryChunkVector chunks_;
180 size_t allocated_memory_;
181 size_t max_free_bytes_;
183 DISALLOW_COPY_AND_ASSIGN(MappedMemoryManager);
188 #endif // GPU_COMMAND_BUFFER_CLIENT_MAPPED_MEMORY_H_