Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / gpu / command_buffer / service / buffer_manager.h
blob9f69cf060b08f8f97ba02a0f10c9f6547c9bf312
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 GLsizeiptr size() const {
51 return size_;
54 GLenum usage() const {
55 return usage_;
58 // Gets the maximum value in the buffer for the given range interpreted as
59 // the given type. Returns false if offset and count are out of range.
60 // offset is in bytes.
61 // count is in elements of type.
62 bool GetMaxValueForRange(GLuint offset, GLsizei count, GLenum type,
63 GLuint* max_value);
65 // Returns a pointer to shadowed data.
66 const void* GetRange(GLintptr offset, GLsizeiptr size) const;
68 bool IsDeleted() const {
69 return deleted_;
72 bool IsValid() const {
73 return initial_target() && !IsDeleted();
76 bool IsClientSideArray() const {
77 return is_client_side_array_;
80 void SetMappedRange(GLintptr offset, GLsizeiptr size, GLenum access,
81 void* pointer, scoped_refptr<gpu::Buffer> shm) {
82 mapped_range_.reset(new MappedRange(offset, size, access, pointer, shm));
85 void RemoveMappedRange() {
86 mapped_range_.reset(nullptr);
89 const MappedRange* GetMappedRange() const {
90 return mapped_range_.get();
93 private:
94 friend class BufferManager;
95 friend class BufferManagerTestBase;
96 friend class base::RefCounted<Buffer>;
98 // Represents a range in a buffer.
99 class Range {
100 public:
101 Range(GLuint offset, GLsizei count, GLenum type)
102 : offset_(offset),
103 count_(count),
104 type_(type) {
107 // A less functor provided for std::map so it can find ranges.
108 struct Less {
109 bool operator() (const Range& lhs, const Range& rhs) const {
110 if (lhs.offset_ != rhs.offset_) {
111 return lhs.offset_ < rhs.offset_;
113 if (lhs.count_ != rhs.count_) {
114 return lhs.count_ < rhs.count_;
116 return lhs.type_ < rhs.type_;
120 private:
121 GLuint offset_;
122 GLsizei count_;
123 GLenum type_;
126 ~Buffer();
128 GLenum initial_target() const {
129 return initial_target_;
132 void set_initial_target(GLenum target) {
133 DCHECK_EQ(0u, initial_target_);
134 initial_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 first target of buffer. 0 = unset.
186 // It is set the first time bindBuffer() is called and cannot be changed.
187 GLenum initial_target_;
189 // Usage of buffer.
190 GLenum usage_;
192 // Data cached from last glMapBufferRange call.
193 scoped_ptr<MappedRange> mapped_range_;
195 // A map of ranges to the highest value in that range of a certain type.
196 typedef std::map<Range, GLuint, Range::Less> RangeToMaxValueMap;
197 RangeToMaxValueMap range_set_;
200 // This class keeps track of the buffers and their sizes so we can do
201 // bounds checking.
203 // NOTE: To support shared resources an instance of this class will need to be
204 // shared by multiple GLES2Decoders.
205 class GPU_EXPORT BufferManager {
206 public:
207 BufferManager(MemoryTracker* memory_tracker, FeatureInfo* feature_info);
208 ~BufferManager();
210 // Must call before destruction.
211 void Destroy(bool have_context);
213 // Creates a Buffer for the given buffer.
214 void CreateBuffer(GLuint client_id, GLuint service_id);
216 // Gets the buffer info for the given buffer.
217 Buffer* GetBuffer(GLuint client_id);
219 // Removes a buffer info for the given buffer.
220 void RemoveBuffer(GLuint client_id);
222 // Gets a client id for a given service id.
223 bool GetClientId(GLuint service_id, GLuint* client_id) const;
225 // Validates a glBufferSubData, and then calls DoBufferData if validation was
226 // successful.
227 void ValidateAndDoBufferSubData(
228 ContextState* context_state, GLenum target, GLintptr offset,
229 GLsizeiptr size, const GLvoid * data);
231 // Validates a glBufferData, and then calls DoBufferData if validation was
232 // successful.
233 void ValidateAndDoBufferData(
234 ContextState* context_state, GLenum target, GLsizeiptr size,
235 const GLvoid * data, GLenum usage);
237 // Validates a glGetBufferParameteriv, and then calls GetBufferParameteriv if
238 // validation was successful.
239 void ValidateAndDoGetBufferParameteriv(
240 ContextState* context_state, GLenum target, GLenum pname, GLint* params);
242 // Sets the target of a buffer. Returns false if the target can not be set.
243 bool SetTarget(Buffer* buffer, GLenum target);
245 void set_allow_buffers_on_multiple_targets(bool allow) {
246 allow_buffers_on_multiple_targets_ = allow;
249 void set_allow_fixed_attribs(bool allow) {
250 allow_fixed_attribs_ = allow;
253 size_t mem_represented() const {
254 return memory_tracker_->GetMemRepresented();
257 // Tells for a given usage if this would be a client side array.
258 bool IsUsageClientSideArray(GLenum usage);
260 // Tells whether a buffer that is emulated using client-side arrays should be
261 // set to a non-zero size.
262 bool UseNonZeroSizeForClientSideArrayBuffer();
264 Buffer* GetBufferInfoForTarget(ContextState* state, GLenum target) const;
266 private:
267 friend class Buffer;
268 friend class TestHelper; // Needs access to DoBufferData.
269 friend class BufferManagerTestBase; // Needs access to DoBufferSubData.
271 void StartTracking(Buffer* buffer);
272 void StopTracking(Buffer* buffer);
274 // Does a glBufferSubData and updates the approriate accounting.
275 // Assumes the values have already been validated.
276 void DoBufferSubData(
277 ErrorState* error_state,
278 Buffer* buffer,
279 GLenum target,
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 GLenum target,
290 GLsizeiptr size,
291 GLenum usage,
292 const GLvoid* data);
294 // Sets the size, usage and initial data of a buffer.
295 // If data is NULL buffer will be initialized to 0 if shadowed.
296 void SetInfo(Buffer* buffer, GLenum target, GLsizeiptr size, GLenum usage,
297 const GLvoid* data);
299 scoped_ptr<MemoryTypeTracker> memory_tracker_;
300 scoped_refptr<FeatureInfo> feature_info_;
302 // Info for each buffer in the system.
303 typedef base::hash_map<GLuint, scoped_refptr<Buffer> > BufferMap;
304 BufferMap buffers_;
306 // Whether or not buffers can be bound to multiple targets.
307 bool allow_buffers_on_multiple_targets_;
309 // Whether or not allow using GL_FIXED type for vertex attribs.
310 bool allow_fixed_attribs_;
312 // Counts the number of Buffer allocated with 'this' as its manager.
313 // Allows to check no Buffer will outlive this.
314 unsigned int buffer_count_;
316 bool have_context_;
317 bool use_client_side_arrays_for_stream_buffers_;
319 DISALLOW_COPY_AND_ASSIGN(BufferManager);
322 } // namespace gles2
323 } // namespace gpu
325 #endif // GPU_COMMAND_BUFFER_SERVICE_BUFFER_MANAGER_H_