Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / gpu / command_buffer / service / buffer_manager.h
blob29bfaf4d431cf0809a6e1ecc920ace2df5fdedf4
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_
8 #include <map>
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"
19 namespace gpu {
20 namespace gles2 {
22 class BufferManager;
23 struct ContextState;
24 class ErrorState;
25 class FeatureInfo;
26 class TestHelper;
28 // Info about Buffers currently in the system.
29 class GPU_EXPORT Buffer : public base::RefCounted<Buffer> {
30 public:
31 struct MappedRange {
32 GLintptr offset;
33 GLsizeiptr size;
34 GLenum access;
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);
40 ~MappedRange();
41 void* GetShmPointer() const;
44 Buffer(BufferManager* manager, GLuint service_id);
46 GLuint service_id() const {
47 return service_id_;
50 GLenum target() const {
51 return target_;
54 GLsizeiptr size() const {
55 return size_;
58 GLenum usage() const {
59 return usage_;
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,
67 GLuint* max_value);
69 // Returns a pointer to shadowed data.
70 const void* GetRange(GLintptr offset, GLsizeiptr size) const;
72 bool IsDeleted() const {
73 return deleted_;
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();
97 private:
98 friend class BufferManager;
99 friend class BufferManagerTestBase;
100 friend class base::RefCounted<Buffer>;
102 // Represents a range in a buffer.
103 class Range {
104 public:
105 Range(GLuint offset, GLsizei count, GLenum type)
106 : offset_(offset),
107 count_(count),
108 type_(type) {
111 // A less functor provided for std::map so it can find ranges.
112 struct Less {
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_;
124 private:
125 GLuint offset_;
126 GLsizei count_;
127 GLenum type_;
130 ~Buffer();
132 void set_target(GLenum target) {
133 DCHECK_EQ(target_, 0u); // you can only set this once.
134 target_ = target;
137 bool shadowed() const {
138 return shadowed_;
141 void MarkAsDeleted() {
142 deleted_ = true;
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.
147 void SetInfo(
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
152 // is out of range.
153 bool SetRange(
154 GLintptr offset, GLsizeiptr size, const GLvoid * data);
156 // Clears any cache of index ranges.
157 void ClearCache();
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_;
169 // Size of buffer.
170 GLsizeiptr size_;
172 // True if deleted.
173 bool deleted_;
175 // Whether or not the data is shadowed.
176 bool 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.
183 GLuint service_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.
188 GLenum target_;
190 // Usage of buffer.
191 GLenum usage_;
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
202 // bounds checking.
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 {
207 public:
208 BufferManager(MemoryTracker* memory_tracker, FeatureInfo* feature_info);
209 ~BufferManager();
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
227 // successful.
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
233 // successful.
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;
267 private:
268 friend class Buffer;
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,
279 Buffer* buffer,
280 GLintptr offset,
281 GLsizeiptr size,
282 const GLvoid* data);
284 // Does a glBufferData and updates the approprate accounting. Currently
285 // Assumes the values have already been validated.
286 void DoBufferData(
287 ErrorState* error_state,
288 Buffer* buffer,
289 GLsizeiptr size,
290 GLenum usage,
291 const GLvoid* data);
293 // Sets the size, usage and initial data of a buffer.
294 // If data is NULL buffer will be initialized to 0 if shadowed.
295 void SetInfo(
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;
303 BufferMap buffers_;
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_;
315 bool have_context_;
316 bool use_client_side_arrays_for_stream_buffers_;
318 DISALLOW_COPY_AND_ASSIGN(BufferManager);
321 } // namespace gles2
322 } // namespace gpu
324 #endif // GPU_COMMAND_BUFFER_SERVICE_BUFFER_MANAGER_H_