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_SERVICE_BUFFER_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_BUFFER_MANAGER_H_
9 #include "base/basictypes.h"
10 #include "base/containers/hash_tables.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "gpu/command_buffer/common/buffer.h"
15 #include "gpu/command_buffer/service/gl_utils.h"
16 #include "gpu/command_buffer/service/memory_tracking.h"
17 #include "gpu/gpu_export.h"
28 // Info about Buffers currently in the system.
29 class GPU_EXPORT Buffer
: public base::RefCounted
<Buffer
> {
35 void* pointer
; // Pointer returned by driver.
36 scoped_refptr
<gpu::Buffer
> shm
; // Client side mem.
38 MappedRange(GLintptr offset
, GLsizeiptr size
, GLenum access
,
39 void* pointer
, scoped_refptr
<gpu::Buffer
> shm
);
41 void* GetShmPointer() const;
44 Buffer(BufferManager
* manager
, GLuint service_id
);
46 GLuint
service_id() const {
50 GLenum
target() const {
54 GLsizeiptr
size() const {
58 GLenum
usage() const {
62 // Gets the maximum value in the buffer for the given range interpreted as
63 // the given type. Returns false if offset and count are out of range.
64 // offset is in bytes.
65 // count is in elements of type.
66 bool GetMaxValueForRange(GLuint offset
, GLsizei count
, GLenum type
,
69 // Returns a pointer to shadowed data.
70 const void* GetRange(GLintptr offset
, GLsizeiptr size
) const;
72 bool IsDeleted() const {
76 bool IsValid() const {
77 return target() && !IsDeleted();
80 bool IsClientSideArray() const {
81 return is_client_side_array_
;
84 void SetMappedRange(GLintptr offset
, GLsizeiptr size
, GLenum access
,
85 void* pointer
, scoped_refptr
<gpu::Buffer
> shm
) {
86 mapped_range_
.reset(new MappedRange(offset
, size
, access
, pointer
, shm
));
89 void RemoveMappedRange() {
90 mapped_range_
.reset(nullptr);
93 const MappedRange
* GetMappedRange() const {
94 return mapped_range_
.get();
98 friend class BufferManager
;
99 friend class BufferManagerTestBase
;
100 friend class base::RefCounted
<Buffer
>;
102 // Represents a range in a buffer.
105 Range(GLuint offset
, GLsizei count
, GLenum type
)
111 // A less functor provided for std::map so it can find ranges.
113 bool operator() (const Range
& lhs
, const Range
& rhs
) const {
114 if (lhs
.offset_
!= rhs
.offset_
) {
115 return lhs
.offset_
< rhs
.offset_
;
117 if (lhs
.count_
!= rhs
.count_
) {
118 return lhs
.count_
< rhs
.count_
;
120 return lhs
.type_
< rhs
.type_
;
132 void set_target(GLenum target
) {
133 DCHECK_EQ(target_
, 0u); // you can only set this once.
137 bool shadowed() const {
141 void MarkAsDeleted() {
145 // Sets the size, usage and initial data of a buffer.
146 // If shadow is true then if data is NULL buffer will be initialized to 0.
148 GLsizeiptr size
, GLenum usage
, bool shadow
, const GLvoid
* data
,
149 bool is_client_side_array
);
151 // Sets a range of data for this buffer. Returns false if the offset or size
154 GLintptr offset
, GLsizeiptr size
, const GLvoid
* data
);
156 // Clears any cache of index ranges.
159 // Check if an offset, size range is valid for the current buffer.
160 bool CheckRange(GLintptr offset
, GLsizeiptr size
) const;
162 // The manager that owns this Buffer.
163 BufferManager
* manager_
;
165 // A copy of the data in the buffer. This data is only kept if the target
166 // is backed_ = true.
167 scoped_ptr
<int8
[]> shadow_
;
175 // Whether or not the data is shadowed.
178 // Whether or not this Buffer is not uploaded to the GPU but just
179 // sitting in local memory.
180 bool is_client_side_array_
;
182 // Service side buffer id.
185 // The type of buffer. 0 = unset, GL_BUFFER_ARRAY = vertex data,
186 // GL_ELEMENT_BUFFER_ARRAY = index data.
187 // Once set a buffer can not be used for something else.
193 // Data cached from last glMapBufferRange call.
194 scoped_ptr
<MappedRange
> mapped_range_
;
196 // A map of ranges to the highest value in that range of a certain type.
197 typedef std::map
<Range
, GLuint
, Range::Less
> RangeToMaxValueMap
;
198 RangeToMaxValueMap range_set_
;
201 // This class keeps track of the buffers and their sizes so we can do
204 // NOTE: To support shared resources an instance of this class will need to be
205 // shared by multiple GLES2Decoders.
206 class GPU_EXPORT BufferManager
{
208 BufferManager(MemoryTracker
* memory_tracker
, FeatureInfo
* feature_info
);
211 // Must call before destruction.
212 void Destroy(bool have_context
);
214 // Creates a Buffer for the given buffer.
215 void CreateBuffer(GLuint client_id
, GLuint service_id
);
217 // Gets the buffer info for the given buffer.
218 Buffer
* GetBuffer(GLuint client_id
);
220 // Removes a buffer info for the given buffer.
221 void RemoveBuffer(GLuint client_id
);
223 // Gets a client id for a given service id.
224 bool GetClientId(GLuint service_id
, GLuint
* client_id
) const;
226 // Validates a glBufferSubData, and then calls DoBufferData if validation was
228 void ValidateAndDoBufferSubData(
229 ContextState
* context_state
, GLenum target
, GLintptr offset
,
230 GLsizeiptr size
, const GLvoid
* data
);
232 // Validates a glBufferData, and then calls DoBufferData if validation was
234 void ValidateAndDoBufferData(
235 ContextState
* context_state
, GLenum target
, GLsizeiptr size
,
236 const GLvoid
* data
, GLenum usage
);
238 // Validates a glGetBufferParameteriv, and then calls GetBufferParameteriv if
239 // validation was successful.
240 void ValidateAndDoGetBufferParameteriv(
241 ContextState
* context_state
, GLenum target
, GLenum pname
, GLint
* params
);
243 // Sets the target of a buffer. Returns false if the target can not be set.
244 bool SetTarget(Buffer
* buffer
, GLenum target
);
246 void set_allow_buffers_on_multiple_targets(bool allow
) {
247 allow_buffers_on_multiple_targets_
= allow
;
250 void set_allow_fixed_attribs(bool allow
) {
251 allow_fixed_attribs_
= allow
;
254 size_t mem_represented() const {
255 return memory_tracker_
->GetMemRepresented();
258 // Tells for a given usage if this would be a client side array.
259 bool IsUsageClientSideArray(GLenum usage
);
261 // Tells whether a buffer that is emulated using client-side arrays should be
262 // set to a non-zero size.
263 bool UseNonZeroSizeForClientSideArrayBuffer();
265 Buffer
* GetBufferInfoForTarget(ContextState
* state
, GLenum target
) const;
269 friend class TestHelper
; // Needs access to DoBufferData.
270 friend class BufferManagerTestBase
; // Needs access to DoBufferSubData.
272 void StartTracking(Buffer
* buffer
);
273 void StopTracking(Buffer
* buffer
);
275 // Does a glBufferSubData and updates the approriate accounting.
276 // Assumes the values have already been validated.
277 void DoBufferSubData(
278 ErrorState
* error_state
,
284 // Does a glBufferData and updates the approprate accounting. Currently
285 // Assumes the values have already been validated.
287 ErrorState
* error_state
,
293 // Sets the size, usage and initial data of a buffer.
294 // If data is NULL buffer will be initialized to 0 if shadowed.
296 Buffer
* buffer
, GLsizeiptr size
, GLenum usage
, const GLvoid
* data
);
298 scoped_ptr
<MemoryTypeTracker
> memory_tracker_
;
299 scoped_refptr
<FeatureInfo
> feature_info_
;
301 // Info for each buffer in the system.
302 typedef base::hash_map
<GLuint
, scoped_refptr
<Buffer
> > BufferMap
;
305 // Whether or not buffers can be bound to multiple targets.
306 bool allow_buffers_on_multiple_targets_
;
308 // Whether or not allow using GL_FIXED type for vertex attribs.
309 bool allow_fixed_attribs_
;
311 // Counts the number of Buffer allocated with 'this' as its manager.
312 // Allows to check no Buffer will outlive this.
313 unsigned int buffer_count_
;
316 bool use_client_side_arrays_for_stream_buffers_
;
318 DISALLOW_COPY_AND_ASSIGN(BufferManager
);
324 #endif // GPU_COMMAND_BUFFER_SERVICE_BUFFER_MANAGER_H_