Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / gpu / command_buffer / service / context_group.h
blob504ae7e6196dd87dba7acad86240c70139cf468b
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_CONTEXT_GROUP_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_CONTEXT_GROUP_H_
8 #include <vector>
9 #include "base/basictypes.h"
10 #include "base/containers/hash_tables.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "gpu/command_buffer/common/constants.h"
15 #include "gpu/command_buffer/common/gles2_cmd_format.h"
16 #include "gpu/command_buffer/service/feature_info.h"
17 #include "gpu/command_buffer/service/framebuffer_completeness_cache.h"
18 #include "gpu/command_buffer/service/shader_translator_cache.h"
19 #include "gpu/gpu_export.h"
21 namespace gpu {
23 class TransferBufferManager;
24 class ValueStateMap;
26 namespace gles2 {
28 class ProgramCache;
29 class BufferManager;
30 class GLES2Decoder;
31 class FramebufferManager;
32 class MailboxManager;
33 class RenderbufferManager;
34 class PathManager;
35 class ProgramManager;
36 class ShaderManager;
37 class TextureManager;
38 class SubscriptionRefSet;
39 class ValuebufferManager;
40 class MemoryTracker;
41 struct DisallowedFeatures;
43 // A Context Group helps manage multiple GLES2Decoders that share
44 // resources.
45 class GPU_EXPORT ContextGroup : public base::RefCounted<ContextGroup> {
46 public:
47 enum ContextType {
48 CONTEXT_TYPE_WEBGL1,
49 CONTEXT_TYPE_WEBGL2,
50 CONTEXT_TYPE_OTHER,
51 CONTEXT_TYPE_UNDEFINED
54 static ContextType GetContextType(unsigned webgl_version);
56 ContextGroup(
57 const scoped_refptr<MailboxManager>& mailbox_manager,
58 const scoped_refptr<MemoryTracker>& memory_tracker,
59 const scoped_refptr<ShaderTranslatorCache>& shader_translator_cache,
60 const scoped_refptr<FramebufferCompletenessCache>&
61 framebuffer_completeness_cache,
62 const scoped_refptr<FeatureInfo>& feature_info,
63 const scoped_refptr<SubscriptionRefSet>& subscription_ref_set,
64 const scoped_refptr<ValueStateMap>& pending_valuebuffer_state,
65 bool bind_generates_resource);
67 // This should only be called by GLES2Decoder. This must be paired with a
68 // call to destroy if it succeeds.
69 bool Initialize(
70 GLES2Decoder* decoder,
71 ContextType context_type,
72 const DisallowedFeatures& disallowed_features);
74 // Destroys all the resources when called for the last context in the group.
75 // It should only be called by GLES2Decoder.
76 void Destroy(GLES2Decoder* decoder, bool have_context);
78 MailboxManager* mailbox_manager() const {
79 return mailbox_manager_.get();
82 MemoryTracker* memory_tracker() const {
83 return memory_tracker_.get();
86 ShaderTranslatorCache* shader_translator_cache() const {
87 return shader_translator_cache_.get();
90 FramebufferCompletenessCache* framebuffer_completeness_cache() const {
91 return framebuffer_completeness_cache_.get();
94 bool bind_generates_resource() {
95 return bind_generates_resource_;
98 uint32 max_vertex_attribs() const {
99 return max_vertex_attribs_;
102 uint32 max_texture_units() const {
103 return max_texture_units_;
106 uint32 max_texture_image_units() const {
107 return max_texture_image_units_;
110 uint32 max_vertex_texture_image_units() const {
111 return max_vertex_texture_image_units_;
114 uint32 max_fragment_uniform_vectors() const {
115 return max_fragment_uniform_vectors_;
118 uint32 max_varying_vectors() const {
119 return max_varying_vectors_;
122 uint32 max_vertex_uniform_vectors() const {
123 return max_vertex_uniform_vectors_;
126 uint32 max_color_attachments() const {
127 return max_color_attachments_;
130 uint32 max_draw_buffers() const {
131 return max_draw_buffers_;
134 FeatureInfo* feature_info() {
135 return feature_info_.get();
138 BufferManager* buffer_manager() const {
139 return buffer_manager_.get();
142 FramebufferManager* framebuffer_manager() const {
143 return framebuffer_manager_.get();
146 RenderbufferManager* renderbuffer_manager() const {
147 return renderbuffer_manager_.get();
150 ValuebufferManager* valuebuffer_manager() const {
151 return valuebuffer_manager_.get();
154 ValueStateMap* pending_valuebuffer_state() const {
155 return pending_valuebuffer_state_.get();
158 TextureManager* texture_manager() const {
159 return texture_manager_.get();
162 PathManager* path_manager() const { return path_manager_.get(); }
164 ProgramManager* program_manager() const {
165 return program_manager_.get();
168 bool has_program_cache() const {
169 return program_cache_ != NULL;
172 void set_program_cache(ProgramCache* program_cache) {
173 program_cache_ = program_cache;
176 ShaderManager* shader_manager() const {
177 return shader_manager_.get();
180 TransferBufferManager* transfer_buffer_manager() const {
181 return transfer_buffer_manager_.get();
184 uint32 GetMemRepresented() const;
186 // Loses all the context associated with this group.
187 void LoseContexts(error::ContextLostReason reason);
189 bool GetBufferServiceId(GLuint client_id, GLuint* service_id) const;
191 void AddSamplerId(GLuint client_id, GLuint service_id) {
192 samplers_id_map_[client_id] = service_id;
195 bool GetSamplerServiceId(GLuint client_id, GLuint* service_id) const {
196 base::hash_map<GLuint, GLuint>::const_iterator iter =
197 samplers_id_map_.find(client_id);
198 if (iter == samplers_id_map_.end())
199 return false;
200 if (service_id)
201 *service_id = iter->second;
202 return true;
205 void RemoveSamplerId(GLuint client_id) {
206 samplers_id_map_.erase(client_id);
209 void AddTransformFeedbackId(GLuint client_id, GLuint service_id) {
210 transformfeedbacks_id_map_[client_id] = service_id;
213 bool GetTransformFeedbackServiceId(
214 GLuint client_id, GLuint* service_id) const {
215 if (client_id == 0) {
216 // Default one.
217 if (service_id)
218 *service_id = 0;
219 return true;
221 base::hash_map<GLuint, GLuint>::const_iterator iter =
222 transformfeedbacks_id_map_.find(client_id);
223 if (iter == transformfeedbacks_id_map_.end())
224 return false;
225 if (service_id)
226 *service_id = iter->second;
227 return true;
230 void RemoveTransformFeedbackId(GLuint client_id) {
231 transformfeedbacks_id_map_.erase(client_id);
234 void AddSyncId(GLuint client_id, GLsync service_id) {
235 syncs_id_map_[client_id] = service_id;
238 bool GetSyncServiceId(GLuint client_id, GLsync* service_id) const {
239 base::hash_map<GLuint, GLsync>::const_iterator iter =
240 syncs_id_map_.find(client_id);
241 if (iter == syncs_id_map_.end())
242 return false;
243 if (service_id)
244 *service_id = iter->second;
245 return true;
248 void RemoveSyncId(GLuint client_id) {
249 syncs_id_map_.erase(client_id);
252 private:
253 friend class base::RefCounted<ContextGroup>;
254 ~ContextGroup();
256 bool CheckGLFeature(GLint min_required, GLint* v);
257 bool CheckGLFeatureU(GLint min_required, uint32* v);
258 bool QueryGLFeature(GLenum pname, GLint min_required, GLint* v);
259 bool QueryGLFeatureU(GLenum pname, GLint min_required, uint32* v);
260 bool HaveContexts();
262 ContextType context_type_;
264 scoped_refptr<MailboxManager> mailbox_manager_;
265 scoped_refptr<MemoryTracker> memory_tracker_;
266 scoped_refptr<ShaderTranslatorCache> shader_translator_cache_;
267 scoped_refptr<FramebufferCompletenessCache> framebuffer_completeness_cache_;
268 scoped_refptr<TransferBufferManager> transfer_buffer_manager_;
269 scoped_refptr<SubscriptionRefSet> subscription_ref_set_;
270 scoped_refptr<ValueStateMap> pending_valuebuffer_state_;
272 bool enforce_gl_minimums_;
273 bool bind_generates_resource_;
275 uint32 max_vertex_attribs_;
276 uint32 max_texture_units_;
277 uint32 max_texture_image_units_;
278 uint32 max_vertex_texture_image_units_;
279 uint32 max_fragment_uniform_vectors_;
280 uint32 max_varying_vectors_;
281 uint32 max_vertex_uniform_vectors_;
282 uint32 max_color_attachments_;
283 uint32 max_draw_buffers_;
285 ProgramCache* program_cache_;
287 scoped_ptr<BufferManager> buffer_manager_;
289 scoped_ptr<FramebufferManager> framebuffer_manager_;
291 scoped_ptr<RenderbufferManager> renderbuffer_manager_;
293 scoped_ptr<TextureManager> texture_manager_;
295 scoped_ptr<PathManager> path_manager_;
297 scoped_ptr<ProgramManager> program_manager_;
299 scoped_ptr<ShaderManager> shader_manager_;
301 scoped_ptr<ValuebufferManager> valuebuffer_manager_;
303 scoped_refptr<FeatureInfo> feature_info_;
305 std::vector<base::WeakPtr<gles2::GLES2Decoder> > decoders_;
307 // Mappings from client side IDs to service side IDs.
308 base::hash_map<GLuint, GLuint> samplers_id_map_;
309 base::hash_map<GLuint, GLuint> transformfeedbacks_id_map_;
310 base::hash_map<GLuint, GLsync> syncs_id_map_;
312 DISALLOW_COPY_AND_ASSIGN(ContextGroup);
315 } // namespace gles2
316 } // namespace gpu
318 #endif // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_GROUP_H_