Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / gpu / command_buffer / client / ring_buffer.h
blob00de8a59badda7e27dd62eff1fb4b55b6224d563
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 // This file contains the definition of the RingBuffer class.
7 #ifndef GPU_COMMAND_BUFFER_CLIENT_RING_BUFFER_H_
8 #define GPU_COMMAND_BUFFER_CLIENT_RING_BUFFER_H_
10 #include <deque>
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "gpu/gpu_export.h"
16 namespace gpu {
17 class CommandBufferHelper;
19 // RingBuffer manages a piece of memory as a ring buffer. Memory is allocated
20 // with Alloc and then a is freed pending a token with FreePendingToken. Old
21 // allocations must not be kept past new allocations.
22 class GPU_EXPORT RingBuffer {
23 public:
24 typedef unsigned int Offset;
26 // Creates a RingBuffer.
27 // Parameters:
28 // alignment: Alignment for allocations.
29 // base_offset: The offset of the start of the buffer.
30 // size: The size of the buffer in bytes.
31 // helper: A CommandBufferHelper for dealing with tokens.
32 // base: The physical address that corresponds to base_offset.
33 RingBuffer(unsigned int alignment, Offset base_offset,
34 unsigned int size, CommandBufferHelper* helper, void* base);
36 ~RingBuffer();
38 // Allocates a block of memory. If the buffer is out of directly available
39 // memory, this function may wait until memory that was freed "pending a
40 // token" can be re-used.
42 // Parameters:
43 // size: the size of the memory block to allocate.
45 // Returns:
46 // the pointer to the allocated memory block.
47 void* Alloc(unsigned int size);
49 // Frees a block of memory, pending the passage of a token. That memory won't
50 // be re-allocated until the token has passed through the command stream.
52 // Parameters:
53 // pointer: the pointer to the memory block to free.
54 // token: the token value to wait for before re-using the memory.
55 void FreePendingToken(void* pointer, unsigned int token);
57 // Discards a block within the ring buffer.
59 // Parameters:
60 // pointer: the pointer to the memory block to free.
61 void DiscardBlock(void* pointer);
63 // Gets the size of the largest free block that is available without waiting.
64 unsigned int GetLargestFreeSizeNoWaiting();
66 // Gets the size of the largest free block that can be allocated if the
67 // caller can wait. Allocating a block of this size will succeed, but may
68 // block.
69 unsigned int GetLargestFreeOrPendingSize() {
70 return size_;
73 // Gets a pointer to a memory block given the base memory and the offset.
74 void* GetPointer(RingBuffer::Offset offset) const {
75 return static_cast<int8*>(base_) + offset;
78 // Gets the offset to a memory block given the base memory and the address.
79 RingBuffer::Offset GetOffset(void* pointer) const {
80 return static_cast<int8*>(pointer) - static_cast<int8*>(base_);
83 // Rounds the given size to the alignment in use.
84 unsigned int RoundToAlignment(unsigned int size) {
85 return (size + alignment_ - 1) & ~(alignment_ - 1);
89 private:
90 enum State {
91 IN_USE,
92 PADDING,
93 FREE_PENDING_TOKEN
95 // Book-keeping sturcture that describes a block of memory.
96 struct Block {
97 Block(Offset _offset, unsigned int _size, State _state)
98 : offset(_offset),
99 size(_size),
100 token(0),
101 state(_state) {
103 Offset offset;
104 unsigned int size;
105 unsigned int token; // token to wait for.
106 State state;
109 typedef std::deque<Block> Container;
110 typedef unsigned int BlockIndex;
112 void FreeOldestBlock();
114 CommandBufferHelper* helper_;
116 // Used blocks are added to the end, blocks are freed from the beginning.
117 Container blocks_;
119 // The base offset of the ring buffer.
120 Offset base_offset_;
122 // The size of the ring buffer.
123 Offset size_;
125 // Offset of first free byte.
126 Offset free_offset_;
128 // Offset of first used byte.
129 // Range between in_use_mark and free_mark is in use.
130 Offset in_use_offset_;
132 // Alignment for allocations.
133 unsigned int alignment_;
135 // The physical address that corresponds to base_offset.
136 void* base_;
138 DISALLOW_IMPLICIT_CONSTRUCTORS(RingBuffer);
141 } // namespace gpu
143 #endif // GPU_COMMAND_BUFFER_CLIENT_RING_BUFFER_H_