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 // A class to Manage a growing transfer buffer.
7 #include "gpu/command_buffer/client/transfer_buffer.h"
10 #include "base/logging.h"
11 #include "base/trace_event/trace_event.h"
12 #include "gpu/command_buffer/client/cmd_buffer_helper.h"
16 TransferBuffer::TransferBuffer(
17 CommandBufferHelper
* helper
)
20 default_buffer_size_(0),
25 bytes_since_last_flush_(0),
28 result_shm_offset_(0),
32 TransferBuffer::~TransferBuffer() {
36 bool TransferBuffer::Initialize(
37 unsigned int default_buffer_size
,
38 unsigned int result_size
,
39 unsigned int min_buffer_size
,
40 unsigned int max_buffer_size
,
41 unsigned int alignment
,
42 unsigned int size_to_flush
) {
43 result_size_
= result_size
;
44 default_buffer_size_
= default_buffer_size
;
45 min_buffer_size_
= min_buffer_size
;
46 max_buffer_size_
= max_buffer_size
;
47 alignment_
= alignment
;
48 size_to_flush_
= size_to_flush
;
49 ReallocateRingBuffer(default_buffer_size_
- result_size
);
53 void TransferBuffer::Free() {
55 TRACE_EVENT0("gpu", "TransferBuffer::Free");
57 helper_
->command_buffer()->DestroyTransferBuffer(buffer_id_
);
60 result_buffer_
= NULL
;
61 result_shm_offset_
= 0;
63 bytes_since_last_flush_
= 0;
67 bool TransferBuffer::HaveBuffer() const {
68 DCHECK(buffer_id_
== -1 || buffer_
.get());
69 return buffer_id_
!= -1;
72 RingBuffer::Offset
TransferBuffer::GetOffset(void* pointer
) const {
73 return ring_buffer_
->GetOffset(pointer
);
76 void TransferBuffer::DiscardBlock(void* p
) {
77 ring_buffer_
->DiscardBlock(p
);
80 void TransferBuffer::FreePendingToken(void* p
, unsigned int token
) {
81 ring_buffer_
->FreePendingToken(p
, token
);
82 if (bytes_since_last_flush_
>= size_to_flush_
&& size_to_flush_
> 0) {
84 bytes_since_last_flush_
= 0;
88 void TransferBuffer::AllocateRingBuffer(unsigned int size
) {
89 for (;size
>= min_buffer_size_
; size
/= 2) {
91 scoped_refptr
<gpu::Buffer
> buffer
=
92 helper_
->command_buffer()->CreateTransferBuffer(size
, &id
);
96 ring_buffer_
.reset(new RingBuffer(
99 buffer_
->size() - result_size_
,
101 static_cast<char*>(buffer_
->memory()) + result_size_
));
103 result_buffer_
= buffer_
->memory();
104 result_shm_offset_
= 0;
107 // we failed so don't try larger than this.
108 max_buffer_size_
= size
/ 2;
113 static unsigned int ComputePOTSize(unsigned int dimension
) {
114 return (dimension
== 0) ? 0 : 1 << base::bits::Log2Ceiling(dimension
);
117 void TransferBuffer::ReallocateRingBuffer(unsigned int size
) {
118 // What size buffer would we ask for if we needed a new one?
119 unsigned int needed_buffer_size
= ComputePOTSize(size
+ result_size_
);
120 needed_buffer_size
= std::max(needed_buffer_size
, min_buffer_size_
);
121 needed_buffer_size
= std::max(needed_buffer_size
, default_buffer_size_
);
122 needed_buffer_size
= std::min(needed_buffer_size
, max_buffer_size_
);
124 if (usable_
&& (!HaveBuffer() || needed_buffer_size
> buffer_
->size())) {
128 AllocateRingBuffer(needed_buffer_size
);
132 void* TransferBuffer::AllocUpTo(
133 unsigned int size
, unsigned int* size_allocated
) {
134 DCHECK(size_allocated
);
136 ReallocateRingBuffer(size
);
142 unsigned int max_size
= ring_buffer_
->GetLargestFreeOrPendingSize();
143 *size_allocated
= std::min(max_size
, size
);
144 bytes_since_last_flush_
+= *size_allocated
;
145 return ring_buffer_
->Alloc(*size_allocated
);
148 void* TransferBuffer::Alloc(unsigned int size
) {
149 ReallocateRingBuffer(size
);
155 unsigned int max_size
= ring_buffer_
->GetLargestFreeOrPendingSize();
156 if (size
> max_size
) {
160 bytes_since_last_flush_
+= size
;
161 return ring_buffer_
->Alloc(size
);
164 void* TransferBuffer::GetResultBuffer() {
165 ReallocateRingBuffer(result_size_
);
166 return result_buffer_
;
169 int TransferBuffer::GetResultOffset() {
170 ReallocateRingBuffer(result_size_
);
171 return result_shm_offset_
;
174 int TransferBuffer::GetShmId() {
175 ReallocateRingBuffer(result_size_
);
179 unsigned int TransferBuffer::GetCurrentMaxAllocationWithoutRealloc() const {
180 return HaveBuffer() ? ring_buffer_
->GetLargestFreeOrPendingSize() : 0;
183 unsigned int TransferBuffer::GetMaxAllocation() const {
184 return HaveBuffer() ? max_buffer_size_
- result_size_
: 0;
187 void ScopedTransferBufferPtr::Release() {
189 transfer_buffer_
->FreePendingToken(buffer_
, helper_
->InsertToken());
195 void ScopedTransferBufferPtr::Discard() {
197 transfer_buffer_
->DiscardBlock(buffer_
);
203 void ScopedTransferBufferPtr::Reset(unsigned int new_size
) {
205 // NOTE: we allocate buffers of size 0 so that HaveBuffer will be true, so
206 // that address will return a pointer just like malloc, and so that GetShmId
207 // will be valid. That has the side effect that we'll insert a token on free.
208 // We could add code skip the token for a zero size buffer but it doesn't seem
209 // worth the complication.
210 buffer_
= transfer_buffer_
->AllocUpTo(new_size
, &size_
);