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