Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(226)

Side by Side Diff: gpu/command_buffer/client/ring_buffer.h

Issue 1796002: Changes the GLES2Implementation to use a RingBuffer... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 // This file contains the definition of the RingBuffer class.
6
7 #ifndef GPU_COMMAND_BUFFER_CLIENT_RING_BUFFER_H_
8 #define GPU_COMMAND_BUFFER_CLIENT_RING_BUFFER_H_
9
10 #include <deque>
11 #include "../common/logging.h"
12 #include "../common/types.h"
13
14 namespace gpu {
15 class CommandBufferHelper;
16
17 // RingBuffer manages a piece of memory as a ring buffer. Memory is allocated
18 // with Alloc and then a is freed pending a token with FreePendingToken. Old
19 // allocations must not be kept past new allocations.
20 class RingBuffer {
21 public:
22 typedef unsigned int Offset;
23
24 // Creates a RingBuffer.
25 // Parameters:
26 // base_offset: The offset of the start of the buffer.
27 // size: The size of the buffer in bytes.
28 // helper: A CommandBufferHelper for dealing with tokens.
29 RingBuffer(
30 Offset base_offset, unsigned int size, CommandBufferHelper* helper);
31
32 ~RingBuffer();
33
34 // Allocates a block of memory. If the buffer is out of directly available
35 // memory, this function may wait until memory that was freed "pending a
36 // token" can be re-used.
37 //
38 // Parameters:
39 // size: the size of the memory block to allocate.
40 //
41 // Returns:
42 // the offset of the allocated memory block.
43 Offset Alloc(unsigned int size);
44
45 // Frees a block of memory, pending the passage of a token. That memory won't
46 // be re-allocated until the token has passed through the command stream.
47 //
48 // Parameters:
49 // offset: the offset of the memory block to free.
50 // token: the token value to wait for before re-using the memory.
51 void FreePendingToken(Offset offset, unsigned int token);
52
53 // Gets the size of the largest free block that is available without waiting.
54 unsigned int GetLargestFreeSizeNoWaiting();
55
56 // Gets the size of the largest free block that can be allocated if the
57 // caller can wait. Allocating a block of this size will succeed, but may
58 // block.
59 unsigned int GetLargestFreeOrPendingSize() {
60 return size_;
61 }
62
63 private:
64 // Book-keeping sturcture that describes a block of memory.
65 struct Block {
66 Block(Offset _offset, unsigned int _size)
67 : offset(_offset),
68 size(_size),
69 token(0),
70 valid(false) {
71 }
72 Offset offset;
73 unsigned int size;
74 unsigned int token; // token to wait for.
75 bool valid; // whether or not token has been set.
76 };
77
78 typedef std::deque<Block> Container;
79 typedef unsigned int BlockIndex;
80
81 void FreeOldestBlock();
82
83 CommandBufferHelper* helper_;
84
85 // Used blocks are added to the end, blocks are freed from the beginning.
86 Container blocks_;
87
88 // The base offset of the ring buffer.
89 Offset base_offset_;
90
91 // The size of the ring buffer.
92 Offset size_;
93
94 // Offset of first free byte.
95 Offset free_offset_;
96
97 // Offset of first used byte.
98 // Range between in_use_mark and free_mark is in use.
99 Offset in_use_offset_;
100
101 DISALLOW_IMPLICIT_CONSTRUCTORS(RingBuffer);
102 };
103
104 // This class functions just like RingBuffer, but its API uses pointers
105 // instead of offsets.
106 class RingBufferWrapper {
107 public:
108 // Parameters:
109 // base_offset: The offset to the start of the buffer
110 // size: The size of the buffer in bytes.
111 // helper: A CommandBufferHelper for dealing with tokens.
112 // base: The physical address that corresponds to base_offset.
113 RingBufferWrapper(RingBuffer::Offset base_offset,
114 unsigned int size,
115 CommandBufferHelper* helper,
116 void* base)
117 : allocator_(base_offset, size, helper),
118 base_(static_cast<int8*>(base) - base_offset) {
119 }
120
121 // Allocates a block of memory. If the buffer is out of directly available
122 // memory, this function may wait until memory that was freed "pending a
123 // token" can be re-used.
124 //
125 // Parameters:
126 // size: the size of the memory block to allocate.
127 //
128 // Returns:
129 // the pointer to the allocated memory block, or NULL if out of
130 // memory.
131 void *Alloc(unsigned int size) {
132 RingBuffer::Offset offset = allocator_.Alloc(size);
133 return GetPointer(offset);
134 }
135
136 // Allocates a block of memory. If the buffer is out of directly available
137 // memory, this function may wait until memory that was freed "pending a
138 // token" can be re-used.
139 // This is a type-safe version of Alloc, returning a typed pointer.
140 //
141 // Parameters:
142 // count: the number of elements to allocate.
143 //
144 // Returns:
145 // the pointer to the allocated memory block, or NULL if out of
146 // memory.
147 template <typename T> T *AllocTyped(unsigned int count) {
148 return static_cast<T *>(Alloc(count * sizeof(T)));
149 }
150
151 // Frees a block of memory, pending the passage of a token. That memory won't
152 // be re-allocated until the token has passed through the command stream.
153 //
154 // Parameters:
155 // pointer: the pointer to the memory block to free.
156 // token: the token value to wait for before re-using the memory.
157 void FreePendingToken(void *pointer, unsigned int token) {
158 DCHECK(pointer);
159 allocator_.FreePendingToken(GetOffset(pointer), token);
160 }
161
162 // Gets a pointer to a memory block given the base memory and the offset.
163 void *GetPointer(RingBuffer::Offset offset) {
164 return static_cast<int8*>(base_) + offset;
165 }
166
167 // Gets the offset to a memory block given the base memory and the address.
168 RingBuffer::Offset GetOffset(void *pointer) {
169 return static_cast<int8*>(pointer) - static_cast<int8*>(base_);
170 }
171
172 // Gets the size of the largest free block that is available without waiting.
173 unsigned int GetLargestFreeSizeNoWaiting() {
174 return allocator_.GetLargestFreeSizeNoWaiting();
175 }
176
177 // Gets the size of the largest free block that can be allocated if the
178 // caller can wait.
179 unsigned int GetLargestFreeOrPendingSize() {
180 return allocator_.GetLargestFreeOrPendingSize();
181 }
182
183 private:
184 RingBuffer allocator_;
185 void *base_;
186 RingBuffer::Offset base_offset_;
187 DISALLOW_IMPLICIT_CONSTRUCTORS(RingBufferWrapper);
188 };
189
190 } // namespace gpu
191
192 #endif // GPU_COMMAND_BUFFER_CLIENT_RING_BUFFER_H_
OLDNEW
« no previous file with comments | « gpu/command_buffer/client/gles2_implementation.cc ('k') | gpu/command_buffer/client/ring_buffer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698