We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / gpu / command_buffer / client / transfer_buffer.h
blob39e62a6c67d9431574c7ac8ce006a76785861895
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 FreePendingToken(void* p, unsigned int token) = 0;
53 // Class that manages the transfer buffer.
54 class GPU_EXPORT TransferBuffer : public TransferBufferInterface {
55 public:
56 TransferBuffer(CommandBufferHelper* helper);
57 ~TransferBuffer() override;
59 // Overridden from TransferBufferInterface.
60 bool Initialize(unsigned int default_buffer_size,
61 unsigned int result_size,
62 unsigned int min_buffer_size,
63 unsigned int max_buffer_size,
64 unsigned int alignment,
65 unsigned int size_to_flush) override;
66 int GetShmId() override;
67 void* GetResultBuffer() override;
68 int GetResultOffset() override;
69 void Free() override;
70 bool HaveBuffer() const override;
71 void* AllocUpTo(unsigned int size, unsigned int* size_allocated) override;
72 void* Alloc(unsigned int size) override;
73 RingBuffer::Offset GetOffset(void* pointer) const override;
74 void FreePendingToken(void* p, unsigned int token) override;
76 // These are for testing.
77 unsigned int GetCurrentMaxAllocationWithoutRealloc() const;
78 unsigned int GetMaxAllocation() const;
80 private:
81 // Tries to reallocate the ring buffer if it's not large enough for size.
82 void ReallocateRingBuffer(unsigned int size);
84 void AllocateRingBuffer(unsigned int size);
86 CommandBufferHelper* helper_;
87 scoped_ptr<RingBuffer> ring_buffer_;
89 // size reserved for results
90 unsigned int result_size_;
92 // default size. Size we want when starting or re-allocating
93 unsigned int default_buffer_size_;
95 // min size we'll consider successful
96 unsigned int min_buffer_size_;
98 // max size we'll let the buffer grow
99 unsigned int max_buffer_size_;
101 // alignment for allocations
102 unsigned int alignment_;
104 // Size at which to do an async flush. 0 = never.
105 unsigned int size_to_flush_;
107 // Number of bytes since we last flushed.
108 unsigned int bytes_since_last_flush_;
110 // the current buffer.
111 scoped_refptr<gpu::Buffer> buffer_;
113 // id of buffer. -1 = no buffer
114 int32 buffer_id_;
116 // address of result area
117 void* result_buffer_;
119 // offset to result area
120 uint32 result_shm_offset_;
122 // false if we failed to allocate min_buffer_size
123 bool usable_;
126 // A class that will manage the lifetime of a transferbuffer allocation.
127 class GPU_EXPORT ScopedTransferBufferPtr {
128 public:
129 ScopedTransferBufferPtr(
130 unsigned int size,
131 CommandBufferHelper* helper,
132 TransferBufferInterface* transfer_buffer)
133 : buffer_(NULL),
134 size_(0),
135 helper_(helper),
136 transfer_buffer_(transfer_buffer) {
137 Reset(size);
140 ~ScopedTransferBufferPtr() {
141 Release();
144 bool valid() const {
145 return buffer_ != NULL;
148 unsigned int size() const {
149 return size_;
152 int shm_id() const {
153 return transfer_buffer_->GetShmId();
156 RingBuffer::Offset offset() const {
157 return transfer_buffer_->GetOffset(buffer_);
160 void* address() const {
161 return buffer_;
164 void Release();
166 void Reset(unsigned int new_size);
168 private:
169 void* buffer_;
170 unsigned int size_;
171 CommandBufferHelper* helper_;
172 TransferBufferInterface* transfer_buffer_;
173 DISALLOW_COPY_AND_ASSIGN(ScopedTransferBufferPtr);
176 template <typename T>
177 class ScopedTransferBufferArray : public ScopedTransferBufferPtr {
178 public:
179 ScopedTransferBufferArray(
180 unsigned int num_elements,
181 CommandBufferHelper* helper, TransferBufferInterface* transfer_buffer)
182 : ScopedTransferBufferPtr(
183 num_elements * sizeof(T), helper, transfer_buffer) {
186 T* elements() {
187 return static_cast<T*>(address());
190 unsigned int num_elements() const {
191 return size() / sizeof(T);
195 } // namespace gpu
197 #endif // GPU_COMMAND_BUFFER_CLIENT_TRANSFER_BUFFER_H_