| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "gpu/command_buffer/service/context_state.h" | 5 #include "gpu/command_buffer/service/context_state.h" |
| 6 | 6 |
| 7 #include "gpu/command_buffer/common/gles2_cmd_utils.h" | 7 #include "gpu/command_buffer/common/gles2_cmd_utils.h" |
| 8 #include "gpu/command_buffer/service/buffer_manager.h" | 8 #include "gpu/command_buffer/service/buffer_manager.h" |
| 9 #include "gpu/command_buffer/service/error_state.h" | 9 #include "gpu/command_buffer/service/error_state.h" |
| 10 #include "gpu/command_buffer/service/framebuffer_manager.h" | 10 #include "gpu/command_buffer/service/framebuffer_manager.h" |
| 11 #include "gpu/command_buffer/service/program_manager.h" | 11 #include "gpu/command_buffer/service/program_manager.h" |
| 12 #include "gpu/command_buffer/service/renderbuffer_manager.h" | 12 #include "gpu/command_buffer/service/renderbuffer_manager.h" |
| 13 #include "ui/gl/gl_bindings.h" | 13 #include "ui/gl/gl_bindings.h" |
| 14 #include "ui/gl/gl_implementation.h" | 14 #include "ui/gl/gl_implementation.h" |
| 15 | 15 |
| 16 namespace gpu { | 16 namespace gpu { |
| 17 namespace gles2 { | 17 namespace gles2 { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 void EnableDisable(GLenum pname, bool enable) { | 21 void EnableDisable(GLenum pname, bool enable) { |
| 22 if (enable) { | 22 if (enable) { |
| 23 glEnable(pname); | 23 glEnable(pname); |
| 24 } else { | 24 } else { |
| 25 glDisable(pname); | 25 glDisable(pname); |
| 26 } | 26 } |
| 27 } | 27 } |
| 28 | 28 |
| 29 GLuint Get2dServiceId(const TextureUnit& unit) { |
| 30 return unit.bound_texture_2d.get() |
| 31 ? unit.bound_texture_2d->service_id() : 0; |
| 32 } |
| 33 |
| 34 GLuint GetCubeServiceId(const TextureUnit& unit) { |
| 35 return unit.bound_texture_cube_map.get() |
| 36 ? unit.bound_texture_cube_map->service_id() : 0; |
| 37 } |
| 38 |
| 39 GLuint GetOesServiceId(const TextureUnit& unit) { |
| 40 return unit.bound_texture_external_oes.get() |
| 41 ? unit.bound_texture_external_oes->service_id() : 0; |
| 42 } |
| 43 |
| 44 GLuint GetArbServiceId(const TextureUnit& unit) { |
| 45 return unit.bound_texture_rectangle_arb.get() |
| 46 ? unit.bound_texture_rectangle_arb->service_id() : 0; |
| 47 } |
| 48 |
| 29 } // anonymous namespace. | 49 } // anonymous namespace. |
| 30 | 50 |
| 31 TextureUnit::TextureUnit() | 51 TextureUnit::TextureUnit() |
| 32 : bind_target(GL_TEXTURE_2D) { | 52 : bind_target(GL_TEXTURE_2D) { |
| 33 } | 53 } |
| 34 | 54 |
| 35 TextureUnit::~TextureUnit() { | 55 TextureUnit::~TextureUnit() { |
| 36 } | 56 } |
| 37 | 57 |
| 38 ContextState::ContextState(FeatureInfo* feature_info, Logger* logger) | 58 ContextState::ContextState(FeatureInfo* feature_info, Logger* logger) |
| 39 : active_texture_unit(0), | 59 : active_texture_unit(0), |
| 40 pack_reverse_row_order(false), | 60 pack_reverse_row_order(false), |
| 41 fbo_binding_for_scissor_workaround_dirty_(false), | 61 fbo_binding_for_scissor_workaround_dirty_(false), |
| 42 feature_info_(feature_info), | 62 feature_info_(feature_info), |
| 43 error_state_(ErrorState::Create(logger)) { | 63 error_state_(ErrorState::Create(logger)) { |
| 44 Initialize(); | 64 Initialize(); |
| 45 } | 65 } |
| 46 | 66 |
| 47 ContextState::~ContextState() { | 67 ContextState::~ContextState() { |
| 48 } | 68 } |
| 49 | 69 |
| 50 void ContextState::RestoreTextureUnitBindings(GLuint unit) const { | 70 void ContextState::RestoreTextureUnitBindings( |
| 71 GLuint unit, const ContextState* prev_state) const { |
| 51 DCHECK_LT(unit, texture_units.size()); | 72 DCHECK_LT(unit, texture_units.size()); |
| 52 const TextureUnit& texture_unit = texture_units[unit]; | 73 const TextureUnit& texture_unit = texture_units[unit]; |
| 53 glActiveTexture(GL_TEXTURE0 + unit); | 74 GLuint service_id_2d = Get2dServiceId(texture_unit); |
| 54 GLuint service_id = texture_unit.bound_texture_2d.get() | 75 GLuint service_id_cube = GetCubeServiceId(texture_unit); |
| 55 ? texture_unit.bound_texture_2d->service_id() | 76 GLuint service_id_oes = GetOesServiceId(texture_unit); |
| 56 : 0; | 77 GLuint service_id_arb = GetArbServiceId(texture_unit); |
| 57 glBindTexture(GL_TEXTURE_2D, service_id); | |
| 58 service_id = texture_unit.bound_texture_cube_map.get() | |
| 59 ? texture_unit.bound_texture_cube_map->service_id() | |
| 60 : 0; | |
| 61 glBindTexture(GL_TEXTURE_CUBE_MAP, service_id); | |
| 62 | 78 |
| 63 if (feature_info_->feature_flags().oes_egl_image_external) { | 79 bool bind_texture_2d = true; |
| 64 service_id = texture_unit.bound_texture_external_oes.get() | 80 bool bind_texture_cube = true; |
| 65 ? texture_unit.bound_texture_external_oes->service_id() | 81 bool bind_texture_oes = feature_info_->feature_flags().oes_egl_image_external; |
| 66 : 0; | 82 bool bind_texture_arb = feature_info_->feature_flags().arb_texture_rectangle; |
| 67 glBindTexture(GL_TEXTURE_EXTERNAL_OES, service_id); | 83 |
| 84 if (prev_state) { |
| 85 const TextureUnit& prev_unit = prev_state->texture_units[unit]; |
| 86 bind_texture_2d = service_id_2d != Get2dServiceId(prev_unit); |
| 87 bind_texture_cube = service_id_cube != GetCubeServiceId(prev_unit); |
| 88 bind_texture_oes = |
| 89 bind_texture_oes && service_id_oes != GetOesServiceId(prev_unit); |
| 90 bind_texture_arb = |
| 91 bind_texture_arb && service_id_arb != GetArbServiceId(prev_unit); |
| 68 } | 92 } |
| 69 | 93 |
| 70 if (feature_info_->feature_flags().arb_texture_rectangle) { | 94 // Early-out if nothing has changed from the previous state. |
| 71 service_id = texture_unit.bound_texture_rectangle_arb.get() | 95 if (!bind_texture_2d && !bind_texture_cube |
| 72 ? texture_unit.bound_texture_rectangle_arb->service_id() | 96 && !bind_texture_oes && !bind_texture_arb) { |
| 73 : 0; | 97 return; |
| 74 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, service_id); | 98 } |
| 99 |
| 100 glActiveTexture(GL_TEXTURE0 + unit); |
| 101 if (bind_texture_2d) { |
| 102 glBindTexture(GL_TEXTURE_2D, service_id_2d); |
| 103 } |
| 104 if (bind_texture_cube) { |
| 105 glBindTexture(GL_TEXTURE_CUBE_MAP, service_id_cube); |
| 106 } |
| 107 if (bind_texture_oes) { |
| 108 glBindTexture(GL_TEXTURE_EXTERNAL_OES, service_id_oes); |
| 109 } |
| 110 if (bind_texture_arb) { |
| 111 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, service_id_arb); |
| 75 } | 112 } |
| 76 } | 113 } |
| 77 | 114 |
| 78 void ContextState::RestoreBufferBindings() const { | 115 void ContextState::RestoreBufferBindings() const { |
| 79 if (vertex_attrib_manager.get()) { | 116 if (vertex_attrib_manager.get()) { |
| 80 Buffer* element_array_buffer = | 117 Buffer* element_array_buffer = |
| 81 vertex_attrib_manager->element_array_buffer(); | 118 vertex_attrib_manager->element_array_buffer(); |
| 82 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, | 119 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, |
| 83 element_array_buffer ? element_array_buffer->service_id() : 0); | 120 element_array_buffer ? element_array_buffer->service_id() : 0); |
| 84 } | 121 } |
| 85 glBindBuffer(GL_ARRAY_BUFFER, | 122 glBindBuffer(GL_ARRAY_BUFFER, |
| 86 bound_array_buffer.get() ? bound_array_buffer->service_id() : 0); | 123 bound_array_buffer.get() ? bound_array_buffer->service_id() : 0); |
| 87 } | 124 } |
| 88 | 125 |
| 89 void ContextState::RestoreRenderbufferBindings() const { | 126 void ContextState::RestoreRenderbufferBindings() const { |
| 90 // Restore Bindings | 127 // Restore Bindings |
| 91 glBindRenderbufferEXT( | 128 glBindRenderbufferEXT( |
| 92 GL_RENDERBUFFER, | 129 GL_RENDERBUFFER, |
| 93 bound_renderbuffer.get() ? bound_renderbuffer->service_id() : 0); | 130 bound_renderbuffer.get() ? bound_renderbuffer->service_id() : 0); |
| 94 } | 131 } |
| 95 | 132 |
| 96 void ContextState::RestoreProgramBindings() const { | 133 void ContextState::RestoreProgramBindings() const { |
| 97 glUseProgram(current_program.get() ? current_program->service_id() : 0); | 134 glUseProgram(current_program.get() ? current_program->service_id() : 0); |
| 98 } | 135 } |
| 99 | 136 |
| 100 void ContextState::RestoreActiveTexture() const { | 137 void ContextState::RestoreActiveTexture() const { |
| 101 glActiveTexture(GL_TEXTURE0 + active_texture_unit); | 138 glActiveTexture(GL_TEXTURE0 + active_texture_unit); |
| 102 } | 139 } |
| 103 | 140 |
| 104 void ContextState::RestoreAllTextureUnitBindings() const { | 141 void ContextState::RestoreAllTextureUnitBindings( |
| 142 const ContextState* prev_state) const { |
| 105 // Restore Texture state. | 143 // Restore Texture state. |
| 106 for (size_t ii = 0; ii < texture_units.size(); ++ii) { | 144 for (size_t ii = 0; ii < texture_units.size(); ++ii) { |
| 107 RestoreTextureUnitBindings(ii); | 145 RestoreTextureUnitBindings(ii, prev_state); |
| 108 } | 146 } |
| 109 RestoreActiveTexture(); | 147 RestoreActiveTexture(); |
| 110 } | 148 } |
| 111 | 149 |
| 112 void ContextState::RestoreAttribute(GLuint attrib_index) const { | 150 void ContextState::RestoreAttribute(GLuint attrib_index) const { |
| 113 const VertexAttrib* attrib = | 151 const VertexAttrib* attrib = |
| 114 vertex_attrib_manager->GetVertexAttrib(attrib_index); | 152 vertex_attrib_manager->GetVertexAttrib(attrib_index); |
| 115 const void* ptr = reinterpret_cast<const void*>(attrib->offset()); | 153 const void* ptr = reinterpret_cast<const void*>(attrib->offset()); |
| 116 Buffer* buffer = attrib->buffer(); | 154 Buffer* buffer = attrib->buffer(); |
| 117 glBindBuffer(GL_ARRAY_BUFFER, buffer ? buffer->service_id() : 0); | 155 glBindBuffer(GL_ARRAY_BUFFER, buffer ? buffer->service_id() : 0); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 132 } | 170 } |
| 133 } | 171 } |
| 134 glVertexAttrib4fv(attrib_index, attrib_values[attrib_index].v); | 172 glVertexAttrib4fv(attrib_index, attrib_values[attrib_index].v); |
| 135 } | 173 } |
| 136 | 174 |
| 137 void ContextState::RestoreGlobalState() const { | 175 void ContextState::RestoreGlobalState() const { |
| 138 InitCapabilities(); | 176 InitCapabilities(); |
| 139 InitState(); | 177 InitState(); |
| 140 } | 178 } |
| 141 | 179 |
| 142 void ContextState::RestoreState() const { | 180 void ContextState::RestoreState(const ContextState* prev_state) const { |
| 143 RestoreAllTextureUnitBindings(); | 181 RestoreAllTextureUnitBindings(prev_state); |
| 144 | 182 |
| 145 // Restore Attrib State | 183 // Restore Attrib State |
| 146 // TODO: This if should not be needed. RestoreState is getting called | 184 // TODO: This if should not be needed. RestoreState is getting called |
| 147 // before GLES2Decoder::Initialize which is a bug. | 185 // before GLES2Decoder::Initialize which is a bug. |
| 148 if (vertex_attrib_manager.get()) { | 186 if (vertex_attrib_manager.get()) { |
| 149 // TODO(gman): Move this restoration to VertexAttribManager. | 187 // TODO(gman): Move this restoration to VertexAttribManager. |
| 150 for (size_t attrib = 0; attrib < vertex_attrib_manager->num_attribs(); | 188 for (size_t attrib = 0; attrib < vertex_attrib_manager->num_attribs(); |
| 151 ++attrib) { | 189 ++attrib) { |
| 152 RestoreAttribute(attrib); | 190 RestoreAttribute(attrib); |
| 153 } | 191 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 165 | 203 |
| 166 // Include the auto-generated part of this file. We split this because it means | 204 // Include the auto-generated part of this file. We split this because it means |
| 167 // we can easily edit the non-auto generated parts right here in this file | 205 // we can easily edit the non-auto generated parts right here in this file |
| 168 // instead of having to edit some template or the code generator. | 206 // instead of having to edit some template or the code generator. |
| 169 #include "gpu/command_buffer/service/context_state_impl_autogen.h" | 207 #include "gpu/command_buffer/service/context_state_impl_autogen.h" |
| 170 | 208 |
| 171 } // namespace gles2 | 209 } // namespace gles2 |
| 172 } // namespace gpu | 210 } // namespace gpu |
| 173 | 211 |
| 174 | 212 |
| OLD | NEW |