Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / gpu / command_buffer / client / transfer_buffer.h
blobe1f8e91ba43d08a046de8a4fec099f3c08f437d4
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_TRANSFER_BUFFER_H_
6 #define GPU_COMMAND_BUFFER_CLIENT_TRANSFER_BUFFER_H_
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "gpu/command_buffer/client/ring_buffer.h"
11 #include "gpu/command_buffer/common/buffer.h"
12 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
13 #include "gpu/gpu_export.h"
15 namespace gpu {
17 class CommandBufferHelper;
19 // Interface for managing the transfer buffer.
20 class GPU_EXPORT TransferBufferInterface {
21 public:
22 TransferBufferInterface() { }
23 virtual ~TransferBufferInterface() { }
25 virtual bool Initialize(
26 unsigned int buffer_size,
27 unsigned int result_size,
28 unsigned int min_buffer_size,
29 unsigned int max_buffer_size,
30 unsigned int alignment,
31 unsigned int size_to_flush) = 0;
33 virtual int GetShmId() = 0;
34 virtual void* GetResultBuffer() = 0;
35 virtual int GetResultOffset() = 0;
37 virtual void Free() = 0;
39 virtual bool HaveBuffer() const = 0;
41 // Allocates up to size bytes.
42 virtual void* AllocUpTo(unsigned int size, unsigned int* size_allocated) = 0;
44 // Allocates size bytes.
45 // Note: Alloc will fail if it can not return size bytes.
46 virtual void* Alloc(unsigned int size) = 0;
48 virtual RingBuffer::Offset GetOffset(void* pointer) const = 0;
50 virtual void DiscardBlock(void* p) = 0;
52 virtual void FreePendingToken(void* p, unsigned int token) = 0;
55 // Class that manages the transfer buffer.
56 class GPU_EXPORT TransferBuffer : public TransferBufferInterface {
57 public:
58 TransferBuffer(CommandBufferHelper* helper);
59 ~TransferBuffer() override;
61 // Overridden from TransferBufferInterface.
62 bool Initialize(unsigned int default_buffer_size,
63 unsigned int result_size,
64 unsigned int min_buffer_size,
65 unsigned int max_buffer_size,
66 unsigned int alignment,
67 unsigned int size_to_flush) override;
68 int GetShmId() override;
69 void* GetResultBuffer() override;
70 int GetResultOffset() override;
71 void Free() override;
72 bool HaveBuffer() const override;
73 void* AllocUpTo(unsigned int size, unsigned int* size_allocated) override;
74 void* Alloc(unsigned int size) override;
75 RingBuffer::Offset GetOffset(void* pointer) const override;
76 void DiscardBlock(void* p) override;
77 void FreePendingToken(void* p, unsigned int token) override;
79 // These are for testing.
80 unsigned int GetCurrentMaxAllocationWithoutRealloc() const;
81 unsigned int GetMaxAllocation() const;
83 private:
84 // Tries to reallocate the ring buffer if it's not large enough for size.
85 void ReallocateRingBuffer(unsigned int size);
87 void AllocateRingBuffer(unsigned int size);
89 CommandBufferHelper* helper_;
90 scoped_ptr<RingBuffer> ring_buffer_;
92 // size reserved for results
93 unsigned int result_size_;
95 // default size. Size we want when starting or re-allocating
96 unsigned int default_buffer_size_;
98 // min size we'll consider successful
99 unsigned int min_buffer_size_;
101 // max size we'll let the buffer grow
102 unsigned int max_buffer_size_;
104 // alignment for allocations
105 unsigned int alignment_;
107 // Size at which to do an async flush. 0 = never.
108 unsigned int size_to_flush_;
110 // Number of bytes since we last flushed.
111 unsigned int bytes_since_last_flush_;
113 // the current buffer.
114 scoped_refptr<gpu::Buffer> buffer_;
116 // id of buffer. -1 = no buffer
117 int32 buffer_id_;
119 // address of result area
120 void* result_buffer_;
122 // offset to result area
123 uint32 result_shm_offset_;
125 // false if we failed to allocate min_buffer_size
126 bool usable_;
129 // A class that will manage the lifetime of a transferbuffer allocation.
130 class GPU_EXPORT ScopedTransferBufferPtr {
131 public:
132 ScopedTransferBufferPtr(
133 unsigned int size,
134 CommandBufferHelper* helper,
135 TransferBufferInterface* transfer_buffer)
136 : buffer_(NULL),
137 size_(0),
138 helper_(helper),
139 transfer_buffer_(transfer_buffer) {
140 Reset(size);
143 ~ScopedTransferBufferPtr() {
144 Release();
147 bool valid() const {
148 return buffer_ != NULL;
151 unsigned int size() const {
152 return size_;
155 int shm_id() const {
156 return transfer_buffer_->GetShmId();
159 RingBuffer::Offset offset() const {
160 return transfer_buffer_->GetOffset(buffer_);
163 void* address() const {
164 return buffer_;
167 void Release();
169 void Discard();
171 void Reset(unsigned int new_size);
173 private:
174 void* buffer_;
175 unsigned int size_;
176 CommandBufferHelper* helper_;
177 TransferBufferInterface* transfer_buffer_;
178 DISALLOW_COPY_AND_ASSIGN(ScopedTransferBufferPtr);
181 template <typename T>
182 class ScopedTransferBufferArray : public ScopedTransferBufferPtr {
183 public:
184 ScopedTransferBufferArray(
185 unsigned int num_elements,
186 CommandBufferHelper* helper, TransferBufferInterface* transfer_buffer)
187 : ScopedTransferBufferPtr(
188 num_elements * sizeof(T), helper, transfer_buffer) {
191 T* elements() {
192 return static_cast<T*>(address());
195 unsigned int num_elements() const {
196 return size() / sizeof(T);
200 } // namespace gpu
202 #endif // GPU_COMMAND_BUFFER_CLIENT_TRANSFER_BUFFER_H_