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.
4
5#ifndef GPU_COMMAND_BUFFER_SERVICE_CONTEXT_GROUP_H_
6#define GPU_COMMAND_BUFFER_SERVICE_CONTEXT_GROUP_H_
7
8#include <string>
9#include <vector>
10#include "base/basictypes.h"
11#include "base/containers/hash_tables.h"
12#include "base/memory/linked_ptr.h"
13#include "base/memory/ref_counted.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/memory/weak_ptr.h"
16#include "gpu/command_buffer/common/gles2_cmd_format.h"
17#include "gpu/command_buffer/service/feature_info.h"
18#include "gpu/command_buffer/service/gles2_cmd_validation.h"
19#include "gpu/command_buffer/service/shader_translator_cache.h"
20#include "gpu/gpu_export.h"
21
22namespace gpu {
23
24class IdAllocatorInterface;
25class TransferBufferManagerInterface;
26
27namespace gles2 {
28
29class ProgramCache;
30class BufferManager;
31class GLES2Decoder;
32class FramebufferManager;
33class MailboxManager;
34class RenderbufferManager;
35class ProgramManager;
36class ShaderManager;
37class TextureManager;
38class MemoryTracker;
39struct DisallowedFeatures;
40
41// A Context Group helps manage multiple GLES2Decoders that share
42// resources.
43class GPU_EXPORT ContextGroup : public base::RefCounted<ContextGroup> {
44 public:
45  ContextGroup(
46      const scoped_refptr<MailboxManager>& mailbox_manager,
47      const scoped_refptr<MemoryTracker>& memory_tracker,
48      const scoped_refptr<ShaderTranslatorCache>& shader_translator_cache,
49      const scoped_refptr<FeatureInfo>& feature_info,
50      bool bind_generates_resource);
51
52  // This should only be called by GLES2Decoder. This must be paired with a
53  // call to destroy if it succeeds.
54  bool Initialize(
55      GLES2Decoder* decoder,
56      const DisallowedFeatures& disallowed_features);
57
58  // Destroys all the resources when called for the last context in the group.
59  // It should only be called by GLES2Decoder.
60  void Destroy(GLES2Decoder* decoder, bool have_context);
61
62  MailboxManager* mailbox_manager() const {
63    return mailbox_manager_.get();
64  }
65
66  MemoryTracker* memory_tracker() const {
67    return memory_tracker_.get();
68  }
69
70  ShaderTranslatorCache* shader_translator_cache() const {
71    return shader_translator_cache_.get();
72  }
73
74  bool bind_generates_resource() {
75    return bind_generates_resource_;
76  }
77
78  uint32 max_vertex_attribs() const {
79    return max_vertex_attribs_;
80  }
81
82  uint32 max_texture_units() const {
83    return max_texture_units_;
84  }
85
86  uint32 max_texture_image_units() const {
87    return max_texture_image_units_;
88  }
89
90  uint32 max_vertex_texture_image_units() const {
91    return max_vertex_texture_image_units_;
92  }
93
94  uint32 max_fragment_uniform_vectors() const {
95    return max_fragment_uniform_vectors_;
96  }
97
98  uint32 max_varying_vectors() const {
99    return max_varying_vectors_;
100  }
101
102  uint32 max_vertex_uniform_vectors() const {
103    return max_vertex_uniform_vectors_;
104  }
105
106  uint32 max_color_attachments() const {
107    return max_color_attachments_;
108  }
109
110  uint32 max_draw_buffers() const {
111    return max_draw_buffers_;
112  }
113
114  FeatureInfo* feature_info() {
115    return feature_info_.get();
116  }
117
118  BufferManager* buffer_manager() const {
119    return buffer_manager_.get();
120  }
121
122  FramebufferManager* framebuffer_manager() const {
123    return framebuffer_manager_.get();
124  }
125
126  RenderbufferManager* renderbuffer_manager() const {
127    return renderbuffer_manager_.get();
128  }
129
130  TextureManager* texture_manager() const {
131    return texture_manager_.get();
132  }
133
134  ProgramManager* program_manager() const {
135    return program_manager_.get();
136  }
137
138  bool has_program_cache() const {
139    return program_cache_ != NULL;
140  }
141
142  void set_program_cache(ProgramCache* program_cache) {
143    program_cache_ = program_cache;
144  }
145
146  ShaderManager* shader_manager() const {
147    return shader_manager_.get();
148  }
149
150  TransferBufferManagerInterface* transfer_buffer_manager() const {
151    return transfer_buffer_manager_.get();
152  }
153
154  IdAllocatorInterface* GetIdAllocator(unsigned namespace_id);
155
156  uint32 GetMemRepresented() const;
157
158  // Loses all the context associated with this group.
159  void LoseContexts(GLenum reset_status);
160
161  // EXT_draw_buffer related states for backbuffer.
162  GLenum draw_buffer() const {
163    return draw_buffer_;
164  }
165  void set_draw_buffer(GLenum buf) {
166    draw_buffer_ = buf;
167  }
168
169 private:
170  friend class base::RefCounted<ContextGroup>;
171  ~ContextGroup();
172
173  bool CheckGLFeature(GLint min_required, GLint* v);
174  bool CheckGLFeatureU(GLint min_required, uint32* v);
175  bool QueryGLFeature(GLenum pname, GLint min_required, GLint* v);
176  bool QueryGLFeatureU(GLenum pname, GLint min_required, uint32* v);
177  bool HaveContexts();
178
179  scoped_refptr<MailboxManager> mailbox_manager_;
180  scoped_refptr<MemoryTracker> memory_tracker_;
181  scoped_refptr<ShaderTranslatorCache> shader_translator_cache_;
182  scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
183
184  bool enforce_gl_minimums_;
185  bool bind_generates_resource_;
186
187  uint32 max_vertex_attribs_;
188  uint32 max_texture_units_;
189  uint32 max_texture_image_units_;
190  uint32 max_vertex_texture_image_units_;
191  uint32 max_fragment_uniform_vectors_;
192  uint32 max_varying_vectors_;
193  uint32 max_vertex_uniform_vectors_;
194  uint32 max_color_attachments_;
195  uint32 max_draw_buffers_;
196
197  ProgramCache* program_cache_;
198
199  scoped_ptr<BufferManager> buffer_manager_;
200
201  scoped_ptr<FramebufferManager> framebuffer_manager_;
202
203  scoped_ptr<RenderbufferManager> renderbuffer_manager_;
204
205  scoped_ptr<TextureManager> texture_manager_;
206
207  scoped_ptr<ProgramManager> program_manager_;
208
209  scoped_ptr<ShaderManager> shader_manager_;
210
211  linked_ptr<IdAllocatorInterface>
212      id_namespaces_[id_namespaces::kNumIdNamespaces];
213
214  scoped_refptr<FeatureInfo> feature_info_;
215
216  std::vector<base::WeakPtr<gles2::GLES2Decoder> > decoders_;
217
218  GLenum draw_buffer_;
219
220  DISALLOW_COPY_AND_ASSIGN(ContextGroup);
221};
222
223}  // namespace gles2
224}  // namespace gpu
225
226#endif  // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_GROUP_H_
227
228
229