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 // This file contains the ContextState class. 6 7 #ifndef GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_ 8 #define GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_ 9 10 #include <vector> 11 #include "base/logging.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "gpu/command_buffer/service/gl_utils.h" 14 #include "gpu/command_buffer/service/query_manager.h" 15 #include "gpu/command_buffer/service/texture_manager.h" 16 #include "gpu/command_buffer/service/vertex_attrib_manager.h" 17 #include "gpu/command_buffer/service/vertex_array_manager.h" 18 #include "gpu/gpu_export.h" 19 20 namespace gpu { 21 namespace gles2 { 22 23 class Buffer; 24 class ErrorState; 25 class FeatureInfo; 26 class Framebuffer; 27 class Program; 28 class Renderbuffer; 29 30 // State associated with each texture unit. 31 struct GPU_EXPORT TextureUnit { 32 TextureUnit(); 33 ~TextureUnit(); 34 35 // The last target that was bound to this texture unit. 36 GLenum bind_target; 37 38 // texture currently bound to this unit's GL_TEXTURE_2D with glBindTexture 39 scoped_refptr<TextureRef> bound_texture_2d; 40 41 // texture currently bound to this unit's GL_TEXTURE_CUBE_MAP with 42 // glBindTexture 43 scoped_refptr<TextureRef> bound_texture_cube_map; 44 45 // texture currently bound to this unit's GL_TEXTURE_EXTERNAL_OES with 46 // glBindTexture 47 scoped_refptr<TextureRef> bound_texture_external_oes; 48 49 // texture currently bound to this unit's GL_TEXTURE_RECTANGLE_ARB with 50 // glBindTexture 51 scoped_refptr<TextureRef> bound_texture_rectangle_arb; 52 53 scoped_refptr<TextureRef> GetInfoForSamplerType( 54 GLenum type) { 55 DCHECK(type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE || 56 type == GL_SAMPLER_EXTERNAL_OES || type == GL_SAMPLER_2D_RECT_ARB); 57 switch (type) { 58 case GL_SAMPLER_2D: 59 return bound_texture_2d; 60 case GL_SAMPLER_CUBE: 61 return bound_texture_cube_map; 62 case GL_SAMPLER_EXTERNAL_OES: 63 return bound_texture_external_oes; 64 case GL_SAMPLER_2D_RECT_ARB: 65 return bound_texture_rectangle_arb; 66 } 67 68 NOTREACHED(); 69 return NULL; 70 } 71 72 void Unbind(TextureRef* texture) { 73 if (bound_texture_2d.get() == texture) { 74 bound_texture_2d = NULL; 75 } 76 if (bound_texture_cube_map.get() == texture) { 77 bound_texture_cube_map = NULL; 78 } 79 if (bound_texture_external_oes.get() == texture) { 80 bound_texture_external_oes = NULL; 81 } 82 } 83 }; 84 85 struct Vec4 { 86 Vec4() { 87 v[0] = 0.0f; 88 v[1] = 0.0f; 89 v[2] = 0.0f; 90 v[3] = 1.0f; 91 } 92 float v[4]; 93 }; 94 95 struct GPU_EXPORT ContextState { 96 ContextState(FeatureInfo* feature_info, Logger* logger); 97 ~ContextState(); 98 99 void Initialize(); 100 101 void RestoreState() const; 102 void InitCapabilities() const; 103 void InitState() const; 104 105 void RestoreActiveTexture() const; 106 void RestoreAllTextureUnitBindings() const; 107 void RestoreAttribute(GLuint index) const; 108 void RestoreBufferBindings() const; 109 void RestoreGlobalState() const; 110 void RestoreProgramBindings() const; 111 void RestoreRenderbufferBindings() const; 112 void RestoreTextureUnitBindings(GLuint unit) const; 113 114 // Helper for getting cached state. 115 bool GetStateAsGLint( 116 GLenum pname, GLint* params, GLsizei* num_written) const; 117 bool GetStateAsGLfloat( 118 GLenum pname, GLfloat* params, GLsizei* num_written) const; 119 bool GetEnabled(GLenum cap) const; 120 121 ErrorState* GetErrorState(); 122 123 #include "gpu/command_buffer/service/context_state_autogen.h" 124 125 EnableFlags enable_flags; 126 127 // Current active texture by 0 - n index. 128 // In other words, if we call glActiveTexture(GL_TEXTURE2) this value would 129 // be 2. 130 GLuint active_texture_unit; 131 132 // The currently bound array buffer. If this is 0 it is illegal to call 133 // glVertexAttribPointer. 134 scoped_refptr<Buffer> bound_array_buffer; 135 136 // Which textures are bound to texture units through glActiveTexture. 137 std::vector<TextureUnit> texture_units; 138 139 // The values for each attrib. 140 std::vector<Vec4> attrib_values; 141 142 // Class that manages vertex attribs. 143 scoped_refptr<VertexAttribManager> vertex_attrib_manager; 144 145 // The program in use by glUseProgram 146 scoped_refptr<Program> current_program; 147 148 // The currently bound renderbuffer 149 scoped_refptr<Renderbuffer> bound_renderbuffer; 150 151 scoped_refptr<QueryManager::Query> current_query; 152 153 bool pack_reverse_row_order; 154 155 mutable bool fbo_binding_for_scissor_workaround_dirty_; 156 FeatureInfo* feature_info_; 157 158 private: 159 scoped_ptr<ErrorState> error_state_; 160 }; 161 162 } // namespace gles2 163 } // namespace gpu 164 165 #endif // GPU_COMMAND_BUFFER_SERVICE_CONTEXT_STATE_H_ 166 167