OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_BUFFER_MANAGER_H_ | 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_BUFFER_MANAGER_H_ |
6 #define GPU_COMMAND_BUFFER_SERVICE_BUFFER_MANAGER_H_ | 6 #define GPU_COMMAND_BUFFER_SERVICE_BUFFER_MANAGER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/hash_tables.h" | 10 #include "base/hash_tables.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "gpu/command_buffer/service/gl_utils.h" | 14 #include "gpu/command_buffer/service/gl_utils.h" |
15 #include "gpu/command_buffer/service/memory_tracking.h" | 15 #include "gpu/command_buffer/service/memory_tracking.h" |
16 #include "gpu/gpu_export.h" | 16 #include "gpu/gpu_export.h" |
17 | 17 |
18 namespace gpu { | 18 namespace gpu { |
19 namespace gles2 { | 19 namespace gles2 { |
20 | 20 |
21 // This class keeps track of the buffers and their sizes so we can do | 21 // This class keeps track of the buffers and their sizes so we can do |
22 // bounds checking. | 22 // bounds checking. |
23 // | 23 // |
24 // NOTE: To support shared resources an instance of this class will need to be | 24 // NOTE: To support shared resources an instance of this class will need to be |
25 // shared by multiple GLES2Decoders. | 25 // shared by multiple GLES2Decoders. |
26 class GPU_EXPORT BufferManager { | 26 class GPU_EXPORT BufferManager { |
27 public: | 27 public: |
28 // Info about Buffers currently in the system. | 28 // Info about Buffers currently in the system. |
29 class GPU_EXPORT BufferInfo : public base::RefCounted<BufferInfo> { | 29 class GPU_EXPORT Buffer : public base::RefCounted<Buffer> { |
30 public: | 30 public: |
31 typedef scoped_refptr<BufferInfo> Ref; | 31 Buffer(BufferManager* manager, GLuint service_id); |
32 | |
33 BufferInfo(BufferManager* manager, GLuint service_id); | |
34 | 32 |
35 GLuint service_id() const { | 33 GLuint service_id() const { |
36 return service_id_; | 34 return service_id_; |
37 } | 35 } |
38 | 36 |
39 GLsizeiptr size() const { | 37 GLsizeiptr size() const { |
40 return size_; | 38 return size_; |
41 } | 39 } |
42 | 40 |
43 GLenum usage() const { | 41 GLenum usage() const { |
(...skipping 19 matching lines...) Expand all Loading... |
63 return deleted_; | 61 return deleted_; |
64 } | 62 } |
65 | 63 |
66 bool IsValid() const { | 64 bool IsValid() const { |
67 return target() && !IsDeleted(); | 65 return target() && !IsDeleted(); |
68 } | 66 } |
69 | 67 |
70 private: | 68 private: |
71 friend class BufferManager; | 69 friend class BufferManager; |
72 friend class BufferManagerTestBase; | 70 friend class BufferManagerTestBase; |
73 friend class base::RefCounted<BufferInfo>; | 71 friend class base::RefCounted<Buffer>; |
74 | 72 |
75 // Represents a range in a buffer. | 73 // Represents a range in a buffer. |
76 class Range { | 74 class Range { |
77 public: | 75 public: |
78 Range(GLuint offset, GLsizei count, GLenum type) | 76 Range(GLuint offset, GLsizei count, GLenum type) |
79 : offset_(offset), | 77 : offset_(offset), |
80 count_(count), | 78 count_(count), |
81 type_(type) { | 79 type_(type) { |
82 } | 80 } |
83 | 81 |
84 // A less functor provided for std::map so it can find ranges. | 82 // A less functor provided for std::map so it can find ranges. |
85 struct Less { | 83 struct Less { |
86 bool operator() (const Range& lhs, const Range& rhs) const { | 84 bool operator() (const Range& lhs, const Range& rhs) const { |
87 if (lhs.offset_ != rhs.offset_) { | 85 if (lhs.offset_ != rhs.offset_) { |
88 return lhs.offset_ < rhs.offset_; | 86 return lhs.offset_ < rhs.offset_; |
89 } | 87 } |
90 if (lhs.count_ != rhs.count_) { | 88 if (lhs.count_ != rhs.count_) { |
91 return lhs.count_ < rhs.count_; | 89 return lhs.count_ < rhs.count_; |
92 } | 90 } |
93 return lhs.type_ < rhs.type_; | 91 return lhs.type_ < rhs.type_; |
94 } | 92 } |
95 }; | 93 }; |
96 | 94 |
97 private: | 95 private: |
98 GLuint offset_; | 96 GLuint offset_; |
99 GLsizei count_; | 97 GLsizei count_; |
100 GLenum type_; | 98 GLenum type_; |
101 }; | 99 }; |
102 | 100 |
103 ~BufferInfo(); | 101 ~Buffer(); |
104 | 102 |
105 GLenum target() const { | 103 GLenum target() const { |
106 return target_; | 104 return target_; |
107 } | 105 } |
108 | 106 |
109 void set_target(GLenum target) { | 107 void set_target(GLenum target) { |
110 DCHECK_EQ(target_, 0u); // you can only set this once. | 108 DCHECK_EQ(target_, 0u); // you can only set this once. |
111 target_ = target; | 109 target_ = target; |
112 } | 110 } |
113 | 111 |
114 bool shadowed() const { | 112 bool shadowed() const { |
115 return shadowed_; | 113 return shadowed_; |
116 } | 114 } |
117 | 115 |
118 void MarkAsDeleted() { | 116 void MarkAsDeleted() { |
119 deleted_ = true; | 117 deleted_ = true; |
120 } | 118 } |
121 | 119 |
122 void SetInfo(GLsizeiptr size, GLenum usage, bool shadow); | 120 void SetInfo(GLsizeiptr size, GLenum usage, bool shadow); |
123 | 121 |
124 // Clears any cache of index ranges. | 122 // Clears any cache of index ranges. |
125 void ClearCache(); | 123 void ClearCache(); |
126 | 124 |
127 // Check if an offset, size range is valid for the current buffer. | 125 // Check if an offset, size range is valid for the current buffer. |
128 bool CheckRange(GLintptr offset, GLsizeiptr size) const; | 126 bool CheckRange(GLintptr offset, GLsizeiptr size) const; |
129 | 127 |
130 // The manager that owns this BufferInfo. | 128 // The manager that owns this Buffer. |
131 BufferManager* manager_; | 129 BufferManager* manager_; |
132 | 130 |
133 // True if deleted. | 131 // True if deleted. |
134 bool deleted_; | 132 bool deleted_; |
135 | 133 |
136 // Service side buffer id. | 134 // Service side buffer id. |
137 GLuint service_id_; | 135 GLuint service_id_; |
138 | 136 |
139 // The type of buffer. 0 = unset, GL_BUFFER_ARRAY = vertex data, | 137 // The type of buffer. 0 = unset, GL_BUFFER_ARRAY = vertex data, |
140 // GL_ELEMENT_BUFFER_ARRAY = index data. | 138 // GL_ELEMENT_BUFFER_ARRAY = index data. |
(...skipping 17 matching lines...) Expand all Loading... |
158 typedef std::map<Range, GLuint, Range::Less> RangeToMaxValueMap; | 156 typedef std::map<Range, GLuint, Range::Less> RangeToMaxValueMap; |
159 RangeToMaxValueMap range_set_; | 157 RangeToMaxValueMap range_set_; |
160 }; | 158 }; |
161 | 159 |
162 BufferManager(MemoryTracker* memory_tracker); | 160 BufferManager(MemoryTracker* memory_tracker); |
163 ~BufferManager(); | 161 ~BufferManager(); |
164 | 162 |
165 // Must call before destruction. | 163 // Must call before destruction. |
166 void Destroy(bool have_context); | 164 void Destroy(bool have_context); |
167 | 165 |
168 // Creates a BufferInfo for the given buffer. | 166 // Creates a Buffer for the given buffer. |
169 void CreateBufferInfo(GLuint client_id, GLuint service_id); | 167 void CreateBuffer(GLuint client_id, GLuint service_id); |
170 | 168 |
171 // Gets the buffer info for the given buffer. | 169 // Gets the buffer info for the given buffer. |
172 BufferInfo* GetBufferInfo(GLuint client_id); | 170 Buffer* GetBuffer(GLuint client_id); |
173 | 171 |
174 // Removes a buffer info for the given buffer. | 172 // Removes a buffer info for the given buffer. |
175 void RemoveBufferInfo(GLuint client_id); | 173 void RemoveBuffer(GLuint client_id); |
176 | 174 |
177 // Gets a client id for a given service id. | 175 // Gets a client id for a given service id. |
178 bool GetClientId(GLuint service_id, GLuint* client_id) const; | 176 bool GetClientId(GLuint service_id, GLuint* client_id) const; |
179 | 177 |
180 // Sets the size and usage of a buffer. | 178 // Sets the size and usage of a buffer. |
181 void SetInfo(BufferInfo* info, GLsizeiptr size, GLenum usage); | 179 void SetInfo(Buffer* info, GLsizeiptr size, GLenum usage); |
182 | 180 |
183 // Sets the target of a buffer. Returns false if the target can not be set. | 181 // Sets the target of a buffer. Returns false if the target can not be set. |
184 bool SetTarget(BufferInfo* info, GLenum target); | 182 bool SetTarget(Buffer* info, GLenum target); |
185 | 183 |
186 void set_allow_buffers_on_multiple_targets(bool allow) { | 184 void set_allow_buffers_on_multiple_targets(bool allow) { |
187 allow_buffers_on_multiple_targets_ = allow; | 185 allow_buffers_on_multiple_targets_ = allow; |
188 } | 186 } |
189 | 187 |
190 size_t mem_represented() const { | 188 size_t mem_represented() const { |
191 return memory_tracker_->GetMemRepresented(); | 189 return memory_tracker_->GetMemRepresented(); |
192 } | 190 } |
193 | 191 |
194 private: | 192 private: |
195 void StartTracking(BufferInfo* info); | 193 void StartTracking(Buffer* info); |
196 void StopTracking(BufferInfo* info); | 194 void StopTracking(Buffer* info); |
197 | 195 |
198 scoped_ptr<MemoryTypeTracker> memory_tracker_; | 196 scoped_ptr<MemoryTypeTracker> memory_tracker_; |
199 | 197 |
200 // Info for each buffer in the system. | 198 // Info for each buffer in the system. |
201 typedef base::hash_map<GLuint, BufferInfo::Ref> BufferInfoMap; | 199 typedef base::hash_map<GLuint, scoped_refptr<Buffer> > BufferInfoMap; |
202 BufferInfoMap buffer_infos_; | 200 BufferInfoMap buffer_infos_; |
203 | 201 |
204 // Whether or not buffers can be bound to multiple targets. | 202 // Whether or not buffers can be bound to multiple targets. |
205 bool allow_buffers_on_multiple_targets_; | 203 bool allow_buffers_on_multiple_targets_; |
206 | 204 |
207 // Counts the number of BufferInfo allocated with 'this' as its manager. | 205 // Counts the number of Buffer allocated with 'this' as its manager. |
208 // Allows to check no BufferInfo will outlive this. | 206 // Allows to check no Buffer will outlive this. |
209 unsigned int buffer_info_count_; | 207 unsigned int buffer_info_count_; |
210 | 208 |
211 bool have_context_; | 209 bool have_context_; |
212 | 210 |
213 DISALLOW_COPY_AND_ASSIGN(BufferManager); | 211 DISALLOW_COPY_AND_ASSIGN(BufferManager); |
214 }; | 212 }; |
215 | 213 |
216 } // namespace gles2 | 214 } // namespace gles2 |
217 } // namespace gpu | 215 } // namespace gpu |
218 | 216 |
219 #endif // GPU_COMMAND_BUFFER_SERVICE_BUFFER_MANAGER_H_ | 217 #endif // GPU_COMMAND_BUFFER_SERVICE_BUFFER_MANAGER_H_ |
OLD | NEW |