Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / gpu / command_buffer / service / context_group.cc
blobd918526ee9043aee8100e5d24e9fab11d982904f
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 #include "gpu/command_buffer/service/context_group.h"
7 #include <algorithm>
8 #include <string>
10 #include "base/command_line.h"
11 #include "base/strings/string_util.h"
12 #include "base/sys_info.h"
13 #include "gpu/command_buffer/service/buffer_manager.h"
14 #include "gpu/command_buffer/service/framebuffer_manager.h"
15 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
16 #include "gpu/command_buffer/service/gpu_switches.h"
17 #include "gpu/command_buffer/service/mailbox_manager_impl.h"
18 #include "gpu/command_buffer/service/memory_tracking.h"
19 #include "gpu/command_buffer/service/program_manager.h"
20 #include "gpu/command_buffer/service/renderbuffer_manager.h"
21 #include "gpu/command_buffer/service/shader_manager.h"
22 #include "gpu/command_buffer/service/texture_manager.h"
23 #include "gpu/command_buffer/service/transfer_buffer_manager.h"
24 #include "gpu/command_buffer/service/valuebuffer_manager.h"
25 #include "ui/gl/gl_implementation.h"
27 namespace gpu {
28 namespace gles2 {
30 ContextGroup::ContextGroup(
31 const scoped_refptr<MailboxManager>& mailbox_manager,
32 const scoped_refptr<MemoryTracker>& memory_tracker,
33 const scoped_refptr<ShaderTranslatorCache>& shader_translator_cache,
34 const scoped_refptr<FeatureInfo>& feature_info,
35 const scoped_refptr<SubscriptionRefSet>& subscription_ref_set,
36 const scoped_refptr<ValueStateMap>& pending_valuebuffer_state,
37 bool bind_generates_resource)
38 : mailbox_manager_(mailbox_manager),
39 memory_tracker_(memory_tracker),
40 shader_translator_cache_(shader_translator_cache),
41 subscription_ref_set_(subscription_ref_set),
42 pending_valuebuffer_state_(pending_valuebuffer_state),
43 enforce_gl_minimums_(base::CommandLine::ForCurrentProcess()->HasSwitch(
44 switches::kEnforceGLMinimums)),
45 bind_generates_resource_(bind_generates_resource),
46 max_vertex_attribs_(0u),
47 max_texture_units_(0u),
48 max_texture_image_units_(0u),
49 max_vertex_texture_image_units_(0u),
50 max_fragment_uniform_vectors_(0u),
51 max_varying_vectors_(0u),
52 max_vertex_uniform_vectors_(0u),
53 max_color_attachments_(1u),
54 max_draw_buffers_(1u),
55 program_cache_(NULL),
56 feature_info_(feature_info),
57 draw_buffer_(GL_BACK) {
59 if (!mailbox_manager_.get())
60 mailbox_manager_ = new MailboxManagerImpl;
61 if (!subscription_ref_set_.get())
62 subscription_ref_set_ = new SubscriptionRefSet();
63 if (!pending_valuebuffer_state_.get())
64 pending_valuebuffer_state_ = new ValueStateMap();
65 if (!feature_info.get())
66 feature_info_ = new FeatureInfo;
67 TransferBufferManager* manager = new TransferBufferManager();
68 transfer_buffer_manager_.reset(manager);
69 manager->Initialize();
73 static void GetIntegerv(GLenum pname, uint32* var) {
74 GLint value = 0;
75 glGetIntegerv(pname, &value);
76 *var = value;
79 bool ContextGroup::Initialize(
80 GLES2Decoder* decoder,
81 const DisallowedFeatures& disallowed_features) {
82 // If we've already initialized the group just add the context.
83 if (HaveContexts()) {
84 decoders_.push_back(base::AsWeakPtr<GLES2Decoder>(decoder));
85 return true;
88 if (!feature_info_->Initialize(disallowed_features)) {
89 LOG(ERROR) << "ContextGroup::Initialize failed because FeatureInfo "
90 << "initialization failed.";
91 return false;
94 const GLint kMinRenderbufferSize = 512; // GL says 1 pixel!
95 GLint max_renderbuffer_size = 0;
96 if (!QueryGLFeature(
97 GL_MAX_RENDERBUFFER_SIZE, kMinRenderbufferSize,
98 &max_renderbuffer_size)) {
99 LOG(ERROR) << "ContextGroup::Initialize failed because maximum "
100 << "renderbuffer size too small.";
101 return false;
103 GLint max_samples = 0;
104 if (feature_info_->feature_flags().chromium_framebuffer_multisample ||
105 feature_info_->feature_flags().multisampled_render_to_texture) {
106 if (feature_info_->feature_flags(
107 ).use_img_for_multisampled_render_to_texture) {
108 glGetIntegerv(GL_MAX_SAMPLES_IMG, &max_samples);
109 } else {
110 glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
114 if (feature_info_->feature_flags().ext_draw_buffers) {
115 GetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &max_color_attachments_);
116 if (max_color_attachments_ < 1)
117 max_color_attachments_ = 1;
118 GetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &max_draw_buffers_);
119 if (max_draw_buffers_ < 1)
120 max_draw_buffers_ = 1;
121 draw_buffer_ = GL_BACK;
124 const bool depth24_supported = feature_info_->feature_flags().oes_depth24;
126 buffer_manager_.reset(
127 new BufferManager(memory_tracker_.get(), feature_info_.get()));
128 framebuffer_manager_.reset(
129 new FramebufferManager(max_draw_buffers_, max_color_attachments_));
130 renderbuffer_manager_.reset(new RenderbufferManager(
131 memory_tracker_.get(), max_renderbuffer_size, max_samples,
132 depth24_supported));
133 shader_manager_.reset(new ShaderManager());
134 valuebuffer_manager_.reset(
135 new ValuebufferManager(subscription_ref_set_.get(),
136 pending_valuebuffer_state_.get()));
138 // Lookup GL things we need to know.
139 const GLint kGLES2RequiredMinimumVertexAttribs = 8u;
140 if (!QueryGLFeatureU(
141 GL_MAX_VERTEX_ATTRIBS, kGLES2RequiredMinimumVertexAttribs,
142 &max_vertex_attribs_)) {
143 LOG(ERROR) << "ContextGroup::Initialize failed because too few "
144 << "vertex attributes supported.";
145 return false;
148 const GLuint kGLES2RequiredMinimumTextureUnits = 8u;
149 if (!QueryGLFeatureU(
150 GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, kGLES2RequiredMinimumTextureUnits,
151 &max_texture_units_)) {
152 LOG(ERROR) << "ContextGroup::Initialize failed because too few "
153 << "texture units supported.";
154 return false;
157 GLint max_texture_size = 0;
158 GLint max_cube_map_texture_size = 0;
159 GLint max_rectangle_texture_size = 0;
160 const GLint kMinTextureSize = 2048; // GL actually says 64!?!?
161 const GLint kMinCubeMapSize = 256; // GL actually says 16!?!?
162 const GLint kMinRectangleTextureSize = 64;
163 if (!QueryGLFeature(GL_MAX_TEXTURE_SIZE, kMinTextureSize,
164 &max_texture_size) ||
165 !QueryGLFeature(GL_MAX_CUBE_MAP_TEXTURE_SIZE, kMinCubeMapSize,
166 &max_cube_map_texture_size)) {
167 LOG(ERROR) << "ContextGroup::Initialize failed because maximum "
168 << " texture size is too small.";
169 return false;
171 if (feature_info_->feature_flags().arb_texture_rectangle) {
172 if (!QueryGLFeature(GL_MAX_RECTANGLE_TEXTURE_SIZE_ARB,
173 kMinRectangleTextureSize,
174 &max_rectangle_texture_size)) {
175 LOG(ERROR) << "ContextGroup::Initialize failed because maximum "
176 << "rectangle texture size is too small.";
177 return false;
181 if (feature_info_->workarounds().max_texture_size) {
182 max_texture_size = std::min(
183 max_texture_size,
184 feature_info_->workarounds().max_texture_size);
185 max_rectangle_texture_size = std::min(
186 max_rectangle_texture_size,
187 feature_info_->workarounds().max_texture_size);
189 if (feature_info_->workarounds().max_cube_map_texture_size) {
190 max_cube_map_texture_size = std::min(
191 max_cube_map_texture_size,
192 feature_info_->workarounds().max_cube_map_texture_size);
195 texture_manager_.reset(new TextureManager(memory_tracker_.get(),
196 feature_info_.get(),
197 max_texture_size,
198 max_cube_map_texture_size,
199 max_rectangle_texture_size,
200 bind_generates_resource_));
201 texture_manager_->set_framebuffer_manager(framebuffer_manager_.get());
203 const GLint kMinTextureImageUnits = 8;
204 const GLint kMinVertexTextureImageUnits = 0;
205 if (!QueryGLFeatureU(
206 GL_MAX_TEXTURE_IMAGE_UNITS, kMinTextureImageUnits,
207 &max_texture_image_units_) ||
208 !QueryGLFeatureU(
209 GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, kMinVertexTextureImageUnits,
210 &max_vertex_texture_image_units_)) {
211 LOG(ERROR) << "ContextGroup::Initialize failed because too few "
212 << "texture units.";
213 return false;
216 if (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2) {
217 GetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS,
218 &max_fragment_uniform_vectors_);
219 GetIntegerv(GL_MAX_VARYING_VECTORS, &max_varying_vectors_);
220 GetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &max_vertex_uniform_vectors_);
221 } else {
222 GetIntegerv(
223 GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max_fragment_uniform_vectors_);
224 max_fragment_uniform_vectors_ /= 4;
225 GetIntegerv(GL_MAX_VARYING_FLOATS, &max_varying_vectors_);
226 max_varying_vectors_ /= 4;
227 GetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS, &max_vertex_uniform_vectors_);
228 max_vertex_uniform_vectors_ /= 4;
231 const GLint kMinFragmentUniformVectors = 16;
232 const GLint kMinVaryingVectors = 8;
233 const GLint kMinVertexUniformVectors = 128;
234 if (!CheckGLFeatureU(
235 kMinFragmentUniformVectors, &max_fragment_uniform_vectors_) ||
236 !CheckGLFeatureU(kMinVaryingVectors, &max_varying_vectors_) ||
237 !CheckGLFeatureU(
238 kMinVertexUniformVectors, &max_vertex_uniform_vectors_)) {
239 LOG(ERROR) << "ContextGroup::Initialize failed because too few "
240 << "uniforms or varyings supported.";
241 return false;
244 // Some shaders in Skia need more than the min available vertex and
245 // fragment shader uniform vectors in case of OSMesa GL Implementation
246 if (feature_info_->workarounds().max_fragment_uniform_vectors) {
247 max_fragment_uniform_vectors_ = std::min(
248 max_fragment_uniform_vectors_,
249 static_cast<uint32>(
250 feature_info_->workarounds().max_fragment_uniform_vectors));
252 if (feature_info_->workarounds().max_varying_vectors) {
253 max_varying_vectors_ = std::min(
254 max_varying_vectors_,
255 static_cast<uint32>(feature_info_->workarounds().max_varying_vectors));
257 if (feature_info_->workarounds().max_vertex_uniform_vectors) {
258 max_vertex_uniform_vectors_ =
259 std::min(max_vertex_uniform_vectors_,
260 static_cast<uint32>(
261 feature_info_->workarounds().max_vertex_uniform_vectors));
264 program_manager_.reset(new ProgramManager(
265 program_cache_, max_varying_vectors_));
267 if (!texture_manager_->Initialize()) {
268 LOG(ERROR) << "Context::Group::Initialize failed because texture manager "
269 << "failed to initialize.";
270 return false;
273 decoders_.push_back(base::AsWeakPtr<GLES2Decoder>(decoder));
274 return true;
277 namespace {
279 bool IsNull(const base::WeakPtr<gles2::GLES2Decoder>& decoder) {
280 return !decoder.get();
283 template <typename T>
284 class WeakPtrEquals {
285 public:
286 explicit WeakPtrEquals(T* t) : t_(t) {}
288 bool operator()(const base::WeakPtr<T>& t) {
289 return t.get() == t_;
292 private:
293 T* const t_;
296 } // namespace anonymous
298 bool ContextGroup::HaveContexts() {
299 decoders_.erase(std::remove_if(decoders_.begin(), decoders_.end(), IsNull),
300 decoders_.end());
301 return !decoders_.empty();
304 void ContextGroup::Destroy(GLES2Decoder* decoder, bool have_context) {
305 decoders_.erase(std::remove_if(decoders_.begin(), decoders_.end(),
306 WeakPtrEquals<gles2::GLES2Decoder>(decoder)),
307 decoders_.end());
308 // If we still have contexts do nothing.
309 if (HaveContexts()) {
310 return;
313 if (buffer_manager_ != NULL) {
314 buffer_manager_->Destroy(have_context);
315 buffer_manager_.reset();
318 if (framebuffer_manager_ != NULL) {
319 framebuffer_manager_->Destroy(have_context);
320 if (texture_manager_)
321 texture_manager_->set_framebuffer_manager(NULL);
322 framebuffer_manager_.reset();
325 if (renderbuffer_manager_ != NULL) {
326 renderbuffer_manager_->Destroy(have_context);
327 renderbuffer_manager_.reset();
330 if (texture_manager_ != NULL) {
331 texture_manager_->Destroy(have_context);
332 texture_manager_.reset();
335 if (program_manager_ != NULL) {
336 program_manager_->Destroy(have_context);
337 program_manager_.reset();
340 if (shader_manager_ != NULL) {
341 shader_manager_->Destroy(have_context);
342 shader_manager_.reset();
345 if (valuebuffer_manager_ != NULL) {
346 valuebuffer_manager_->Destroy();
347 valuebuffer_manager_.reset();
350 memory_tracker_ = NULL;
353 uint32 ContextGroup::GetMemRepresented() const {
354 uint32 total = 0;
355 if (buffer_manager_.get())
356 total += buffer_manager_->mem_represented();
357 if (renderbuffer_manager_.get())
358 total += renderbuffer_manager_->mem_represented();
359 if (texture_manager_.get())
360 total += texture_manager_->mem_represented();
361 return total;
364 void ContextGroup::LoseContexts(GLenum reset_status) {
365 for (size_t ii = 0; ii < decoders_.size(); ++ii) {
366 if (decoders_[ii].get()) {
367 decoders_[ii]->LoseContext(reset_status);
372 ContextGroup::~ContextGroup() {
373 CHECK(!HaveContexts());
376 bool ContextGroup::CheckGLFeature(GLint min_required, GLint* v) {
377 GLint value = *v;
378 if (enforce_gl_minimums_) {
379 value = std::min(min_required, value);
381 *v = value;
382 return value >= min_required;
385 bool ContextGroup::CheckGLFeatureU(GLint min_required, uint32* v) {
386 GLint value = *v;
387 if (enforce_gl_minimums_) {
388 value = std::min(min_required, value);
390 *v = value;
391 return value >= min_required;
394 bool ContextGroup::QueryGLFeature(
395 GLenum pname, GLint min_required, GLint* v) {
396 GLint value = 0;
397 glGetIntegerv(pname, &value);
398 *v = value;
399 return CheckGLFeature(min_required, v);
402 bool ContextGroup::QueryGLFeatureU(
403 GLenum pname, GLint min_required, uint32* v) {
404 uint32 value = 0;
405 GetIntegerv(pname, &value);
406 bool result = CheckGLFeatureU(min_required, &value);
407 *v = value;
408 return result;
411 bool ContextGroup::GetBufferServiceId(
412 GLuint client_id, GLuint* service_id) const {
413 Buffer* buffer = buffer_manager_->GetBuffer(client_id);
414 if (!buffer)
415 return false;
416 *service_id = buffer->service_id();
417 return true;
420 } // namespace gles2
421 } // namespace gpu