Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / client / mapped_memory.h
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.
4
5 #ifndef GPU_COMMAND_BUFFER_CLIENT_MAPPED_MEMORY_H_
6 #define GPU_COMMAND_BUFFER_CLIENT_MAPPED_MEMORY_H_
7
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"
13
14 namespace gpu {
15
16 class CommandBufferHelper;
17
18 // Manages a shared memory segment.
19 class GPU_EXPORT MemoryChunk {
20  public:
21   MemoryChunk(int32 shm_id,
22               scoped_refptr<gpu::Buffer> shm,
23               CommandBufferHelper* helper);
24   ~MemoryChunk();
25
26   // Gets the size of the largest free block that is available without waiting.
27   unsigned int GetLargestFreeSizeWithoutWaiting() {
28     return allocator_.GetLargestFreeSize();
29   }
30
31   // Gets the size of the largest free block that can be allocated if the
32   // caller can wait.
33   unsigned int GetLargestFreeSizeWithWaiting() {
34     return allocator_.GetLargestFreeOrPendingSize();
35   }
36
37   // Gets the size of the chunk.
38   unsigned int GetSize() const {
39     return static_cast<unsigned int>(shm_->size());
40   }
41
42   // The shared memory id for this chunk.
43   int32 shm_id() const {
44     return shm_id_;
45   }
46
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.
50   //
51   // Parameters:
52   //   size: the size of the memory block to allocate.
53   //
54   // Returns:
55   //   the pointer to the allocated memory block, or NULL if out of
56   //   memory.
57   void* Alloc(unsigned int size) {
58     return allocator_.Alloc(size);
59   }
60
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);
65   }
66
67   // Frees a block of memory.
68   //
69   // Parameters:
70   //   pointer: the pointer to the memory block to free.
71   void Free(void* pointer) {
72     allocator_.Free(pointer);
73   }
74
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.
77   //
78   // Parameters:
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);
83   }
84
85   // Frees any blocks whose tokens have passed.
86   void FreeUnused() {
87     allocator_.FreeUnused();
88   }
89
90   // Returns true if pointer is in the range of this block.
91   bool IsInChunk(void* pointer) const {
92     return pointer >= shm_->memory() &&
93            pointer <
94                reinterpret_cast<const int8*>(shm_->memory()) + shm_->size();
95   }
96
97   // Returns true of any memory in this chunk is in use.
98   bool InUse() {
99     return allocator_.InUse();
100   }
101
102   size_t bytes_in_use() const {
103     return allocator_.bytes_in_use();
104   }
105
106  private:
107   int32 shm_id_;
108   scoped_refptr<gpu::Buffer> shm_;
109   FencedAllocatorWrapper allocator_;
110
111   DISALLOW_COPY_AND_ASSIGN(MemoryChunk);
112 };
113
114 // Manages MemoryChunks.
115 class GPU_EXPORT MappedMemoryManager {
116  public:
117   enum MemoryLimit {
118     kNoLimit = 0,
119   };
120
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);
125
126   ~MappedMemoryManager();
127
128   unsigned int chunk_size_multiple() const {
129     return chunk_size_multiple_;
130   }
131
132   void set_chunk_size_multiple(unsigned int multiple) {
133     chunk_size_multiple_ = multiple;
134   }
135
136   // Allocates a block of memory
137   // Parameters:
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.
141   // Returns:
142   //   pointer to allocated block of memory. NULL if failure.
143   void* Alloc(
144       unsigned int size, int32* shm_id, unsigned int* shm_offset);
145
146   // Frees a block of memory.
147   //
148   // Parameters:
149   //   pointer: the pointer to the memory block to free.
150   void Free(void* pointer);
151
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.
154   //
155   // Parameters:
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);
159
160   // Free Any Shared memory that is not in use.
161   void FreeUnused();
162
163   // Used for testing
164   size_t num_chunks() const {
165     return chunks_.size();
166   }
167
168   // Used for testing
169   size_t allocated_memory() const {
170     return allocated_memory_;
171   }
172
173  private:
174   typedef ScopedVector<MemoryChunk> MemoryChunkVector;
175
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_;
182
183   DISALLOW_COPY_AND_ASSIGN(MappedMemoryManager);
184 };
185
186 }  // namespace gpu
187
188 #endif  // GPU_COMMAND_BUFFER_CLIENT_MAPPED_MEMORY_H_
189