mac: Let IPhotoDataProvider::GetAlbumNames() return albums in a deterministic order.
[chromium-blink-merge.git] / gpu / command_buffer / client / transfer_buffer.h
blob7dbe6021f5075032592fdf1ba1eec781cbd60574
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;
54 virtual unsigned int GetSize() const = 0;
56 virtual unsigned int GetFreeSize() const = 0;
59 // Class that manages the transfer buffer.
60 class GPU_EXPORT TransferBuffer : public TransferBufferInterface {
61 public:
62 TransferBuffer(CommandBufferHelper* helper);
63 ~TransferBuffer() override;
65 // Overridden from TransferBufferInterface.
66 bool Initialize(unsigned int default_buffer_size,
67 unsigned int result_size,
68 unsigned int min_buffer_size,
69 unsigned int max_buffer_size,
70 unsigned int alignment,
71 unsigned int size_to_flush) override;
72 int GetShmId() override;
73 void* GetResultBuffer() override;
74 int GetResultOffset() override;
75 void Free() override;
76 bool HaveBuffer() const override;
77 void* AllocUpTo(unsigned int size, unsigned int* size_allocated) override;
78 void* Alloc(unsigned int size) override;
79 RingBuffer::Offset GetOffset(void* pointer) const override;
80 void DiscardBlock(void* p) override;
81 void FreePendingToken(void* p, unsigned int token) override;
82 unsigned int GetSize() const override;
83 unsigned int GetFreeSize() const override;
85 // These are for testing.
86 unsigned int GetCurrentMaxAllocationWithoutRealloc() const;
87 unsigned int GetMaxAllocation() const;
89 private:
90 // Tries to reallocate the ring buffer if it's not large enough for size.
91 void ReallocateRingBuffer(unsigned int size);
93 void AllocateRingBuffer(unsigned int size);
95 CommandBufferHelper* helper_;
96 scoped_ptr<RingBuffer> ring_buffer_;
98 // size reserved for results
99 unsigned int result_size_;
101 // default size. Size we want when starting or re-allocating
102 unsigned int default_buffer_size_;
104 // min size we'll consider successful
105 unsigned int min_buffer_size_;
107 // max size we'll let the buffer grow
108 unsigned int max_buffer_size_;
110 // alignment for allocations
111 unsigned int alignment_;
113 // Size at which to do an async flush. 0 = never.
114 unsigned int size_to_flush_;
116 // Number of bytes since we last flushed.
117 unsigned int bytes_since_last_flush_;
119 // the current buffer.
120 scoped_refptr<gpu::Buffer> buffer_;
122 // id of buffer. -1 = no buffer
123 int32 buffer_id_;
125 // address of result area
126 void* result_buffer_;
128 // offset to result area
129 uint32 result_shm_offset_;
131 // false if we failed to allocate min_buffer_size
132 bool usable_;
135 // A class that will manage the lifetime of a transferbuffer allocation.
136 class GPU_EXPORT ScopedTransferBufferPtr {
137 public:
138 ScopedTransferBufferPtr(
139 unsigned int size,
140 CommandBufferHelper* helper,
141 TransferBufferInterface* transfer_buffer)
142 : buffer_(NULL),
143 size_(0),
144 helper_(helper),
145 transfer_buffer_(transfer_buffer) {
146 Reset(size);
149 ~ScopedTransferBufferPtr() {
150 Release();
153 bool valid() const {
154 return buffer_ != NULL;
157 unsigned int size() const {
158 return size_;
161 int shm_id() const {
162 return transfer_buffer_->GetShmId();
165 RingBuffer::Offset offset() const {
166 return transfer_buffer_->GetOffset(buffer_);
169 void* address() const {
170 return buffer_;
173 void Release();
175 void Discard();
177 void Reset(unsigned int new_size);
179 private:
180 void* buffer_;
181 unsigned int size_;
182 CommandBufferHelper* helper_;
183 TransferBufferInterface* transfer_buffer_;
184 DISALLOW_COPY_AND_ASSIGN(ScopedTransferBufferPtr);
187 template <typename T>
188 class ScopedTransferBufferArray : public ScopedTransferBufferPtr {
189 public:
190 ScopedTransferBufferArray(
191 unsigned int num_elements,
192 CommandBufferHelper* helper, TransferBufferInterface* transfer_buffer)
193 : ScopedTransferBufferPtr(
194 num_elements * sizeof(T), helper, transfer_buffer) {
197 T* elements() {
198 return static_cast<T*>(address());
201 unsigned int num_elements() const {
202 return size() / sizeof(T);
206 } // namespace gpu
208 #endif // GPU_COMMAND_BUFFER_CLIENT_TRANSFER_BUFFER_H_