OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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. |
| 4 |
| 5 #ifndef GPU_COMMAND_BUFFER_CLIENT_MAPPED_MEMORY_H_ |
| 6 #define GPU_COMMAND_BUFFER_CLIENT_MAPPED_MEMORY_H_ |
| 7 |
| 8 #include <vector> |
| 9 #include "base/basictypes.h" |
| 10 #include "base/ref_counted.h" |
| 11 #include "gpu/command_buffer/client/fenced_allocator.h" |
| 12 #include "gpu/command_buffer/common/buffer.h" |
| 13 |
| 14 namespace gpu { |
| 15 |
| 16 class CommandBufferHelper; |
| 17 |
| 18 // Manages a shared memory segment. |
| 19 class MemoryChunk : public base::RefCounted<MemoryChunk> { |
| 20 public: |
| 21 typedef scoped_refptr<MemoryChunk> Ref; |
| 22 |
| 23 MemoryChunk(int32 shm_id, gpu::Buffer shm, CommandBufferHelper* helper); |
| 24 |
| 25 // Gets the size of the largest free block that is available without waiting. |
| 26 unsigned int GetLargestFreeSizeWithoutWaiting() { |
| 27 return allocator_.GetLargestFreeSize(); |
| 28 } |
| 29 |
| 30 // Gets the size of the largest free block that can be allocated if the |
| 31 // caller can wait. |
| 32 unsigned int GetLargestFreeSizeWithWaiting() { |
| 33 return allocator_.GetLargestFreeOrPendingSize(); |
| 34 } |
| 35 |
| 36 // Gets the size of the chunk. |
| 37 unsigned int GetSize() const { |
| 38 return shm_.size; |
| 39 } |
| 40 |
| 41 // The shared memory id for this chunk. |
| 42 int32 shm_id() const { |
| 43 return shm_id_; |
| 44 } |
| 45 |
| 46 // Allocates a block of memory. If the buffer is out of directly available |
| 47 // memory, this function may wait until memory that was freed "pending a |
| 48 // token" can be re-used. |
| 49 // |
| 50 // Parameters: |
| 51 // size: the size of the memory block to allocate. |
| 52 // |
| 53 // Returns: |
| 54 // the pointer to the allocated memory block, or NULL if out of |
| 55 // memory. |
| 56 void* Alloc(unsigned int size) { |
| 57 return allocator_.Alloc(size); |
| 58 } |
| 59 |
| 60 // Gets the offset to a memory block given the base memory and the address. |
| 61 // It translates NULL to FencedAllocator::kInvalidOffset. |
| 62 unsigned int GetOffset(void* pointer) { |
| 63 return allocator_.GetOffset(pointer); |
| 64 } |
| 65 |
| 66 // Frees a block of memory. |
| 67 // |
| 68 // Parameters: |
| 69 // pointer: the pointer to the memory block to free. |
| 70 void Free(void* pointer) { |
| 71 allocator_.Free(pointer); |
| 72 } |
| 73 |
| 74 // Frees a block of memory, pending the passage of a token. That memory won't |
| 75 // be re-allocated until the token has passed through the command stream. |
| 76 // |
| 77 // Parameters: |
| 78 // pointer: the pointer to the memory block to free. |
| 79 // token: the token value to wait for before re-using the memory. |
| 80 void FreePendingToken(void* pointer, unsigned int token) { |
| 81 allocator_.FreePendingToken(pointer, token); |
| 82 } |
| 83 |
| 84 // Frees any blocks who's tokens have passed. |
| 85 void FreeUnused() { |
| 86 allocator_.FreeUnused(); |
| 87 } |
| 88 |
| 89 // Returns true if pointer is in the range of this block. |
| 90 bool IsInChunk(void* pointer) const { |
| 91 return pointer >= shm_.ptr && |
| 92 pointer < reinterpret_cast<const int8*>(shm_.ptr) + shm_.size; |
| 93 } |
| 94 |
| 95 private: |
| 96 int32 shm_id_; |
| 97 gpu::Buffer shm_; |
| 98 FencedAllocatorWrapper allocator_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(MemoryChunk); |
| 101 }; |
| 102 |
| 103 // Manages MemoryChucks. |
| 104 class MappedMemoryManager { |
| 105 public: |
| 106 explicit MappedMemoryManager(CommandBufferHelper* helper) |
| 107 : helper_(helper) { |
| 108 } |
| 109 |
| 110 // Allocates a block of memory |
| 111 // Parameters: |
| 112 // size: size of memory to allocate. |
| 113 // shm_id: pointer to variable to receive the shared memory id. |
| 114 // shm_offset: pointer to variable to receive the shared memory offset. |
| 115 // Returns: |
| 116 // pointer to allocated block of memory. NULL if failure. |
| 117 void* Alloc( |
| 118 unsigned int size, int32* shm_id, unsigned int* shm_offset); |
| 119 |
| 120 // Frees a block of memory. |
| 121 // |
| 122 // Parameters: |
| 123 // pointer: the pointer to the memory block to free. |
| 124 void Free(void* pointer); |
| 125 |
| 126 // Frees a block of memory, pending the passage of a token. That memory won't |
| 127 // be re-allocated until the token has passed through the command stream. |
| 128 // |
| 129 // Parameters: |
| 130 // pointer: the pointer to the memory block to free. |
| 131 // token: the token value to wait for before re-using the memory. |
| 132 void FreePendingToken(void* pointer, int32 token); |
| 133 |
| 134 private: |
| 135 typedef std::vector<MemoryChunk::Ref> MemoryChunkVector; |
| 136 |
| 137 CommandBufferHelper* helper_; |
| 138 MemoryChunkVector chunks_; |
| 139 |
| 140 DISALLOW_COPY_AND_ASSIGN(MappedMemoryManager); |
| 141 }; |
| 142 |
| 143 } // namespace gpu |
| 144 |
| 145 #endif // GPU_COMMAND_BUFFER_CLIENT_MAPPED_MEMORY_H_ |
| 146 |
OLD | NEW |