| 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 "ui/gl/gl_bindings.h" | 8 #include "ui/gl/gl_bindings.h" |
| 9 #include "ui/gl/gl_implementation.h" | 9 #include "ui/gl/gl_implementation.h" |
| 10 | 10 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 hint_generate_mipmap(GL_DONT_CARE), | 37 hint_generate_mipmap(GL_DONT_CARE), |
| 38 hint_fragment_shader_derivative(GL_DONT_CARE), | 38 hint_fragment_shader_derivative(GL_DONT_CARE), |
| 39 pack_reverse_row_order(false), | 39 pack_reverse_row_order(false), |
| 40 feature_info_(feature_info) { | 40 feature_info_(feature_info) { |
| 41 Initialize(); | 41 Initialize(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 ContextState::~ContextState() { | 44 ContextState::~ContextState() { |
| 45 } | 45 } |
| 46 | 46 |
| 47 void ContextState::RestoreState() const { | 47 void ContextState::RestoreTextureUnitBindings(GLuint unit) const { |
| 48 DCHECK_LT(unit, texture_units.size()); |
| 49 const TextureUnit& texture_unit = texture_units[unit]; |
| 50 glActiveTexture(GL_TEXTURE0 + unit); |
| 51 GLuint service_id = texture_unit.bound_texture_2d ? |
| 52 texture_unit.bound_texture_2d->service_id() : 0; |
| 53 glBindTexture(GL_TEXTURE_2D, service_id); |
| 54 service_id = texture_unit.bound_texture_cube_map ? |
| 55 texture_unit.bound_texture_cube_map->service_id() : 0; |
| 56 glBindTexture(GL_TEXTURE_CUBE_MAP, service_id); |
| 57 |
| 58 if (feature_info_->feature_flags().oes_egl_image_external) { |
| 59 service_id = texture_unit.bound_texture_external_oes ? |
| 60 texture_unit.bound_texture_external_oes->service_id() : 0; |
| 61 glBindTexture(GL_TEXTURE_EXTERNAL_OES, service_id); |
| 62 } |
| 63 |
| 64 if (feature_info_->feature_flags().arb_texture_rectangle) { |
| 65 service_id = texture_unit.bound_texture_rectangle_arb ? |
| 66 texture_unit.bound_texture_rectangle_arb->service_id() : 0; |
| 67 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, service_id); |
| 68 } |
| 69 } |
| 70 |
| 71 void ContextState::RestoreBufferBindings() const { |
| 72 if (vertex_attrib_manager) { |
| 73 BufferManager::BufferInfo* element_array_buffer = |
| 74 vertex_attrib_manager->element_array_buffer(); |
| 75 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, |
| 76 element_array_buffer ? element_array_buffer->service_id() : 0); |
| 77 } |
| 78 glBindBuffer( |
| 79 GL_ARRAY_BUFFER, |
| 80 bound_array_buffer ? bound_array_buffer->service_id() : 0); |
| 81 } |
| 82 |
| 83 void ContextState::RestoreRenderbufferBindings() const { |
| 84 // Restore Bindings |
| 85 glBindRenderbufferEXT( |
| 86 GL_RENDERBUFFER, |
| 87 bound_renderbuffer ? bound_renderbuffer->service_id() : 0); |
| 88 } |
| 89 |
| 90 void ContextState::RestoreProgramBindings() const { |
| 91 glUseProgram(current_program ? current_program->service_id() : 0); |
| 92 } |
| 93 |
| 94 void ContextState::RestoreActiveTexture() const { |
| 95 glActiveTexture(GL_TEXTURE0 + active_texture_unit); |
| 96 } |
| 97 |
| 98 void ContextState::RestoreAttribute(GLuint attrib) const { |
| 99 const VertexAttribManager::VertexAttribInfo* info = |
| 100 vertex_attrib_manager->GetVertexAttribInfo(attrib); |
| 101 const void* ptr = reinterpret_cast<const void*>(info->offset()); |
| 102 BufferManager::BufferInfo* buffer_info = info->buffer(); |
| 103 glBindBuffer( |
| 104 GL_ARRAY_BUFFER, buffer_info ? buffer_info->service_id() : 0); |
| 105 glVertexAttribPointer( |
| 106 attrib, info->size(), info->type(), info->normalized(), |
| 107 info->gl_stride(), ptr); |
| 108 if (info->divisor()) |
| 109 glVertexAttribDivisorANGLE(attrib, info->divisor()); |
| 110 // Never touch vertex attribute 0's state (in particular, never |
| 111 // disable it) when running on desktop GL because it will never be |
| 112 // re-enabled. |
| 113 if (attrib != 0 || |
| 114 gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2) { |
| 115 if (info->enabled()) { |
| 116 glEnableVertexAttribArray(attrib); |
| 117 } else { |
| 118 glDisableVertexAttribArray(attrib); |
| 119 } |
| 120 } |
| 121 glVertexAttrib4fv(attrib, attrib_values[attrib].v); |
| 122 } |
| 123 |
| 124 void ContextState::RestoreGlobalState() const { |
| 48 InitCapabilities(); | 125 InitCapabilities(); |
| 49 InitState(); | 126 InitState(); |
| 50 | 127 |
| 51 glPixelStorei(GL_PACK_ALIGNMENT, pack_alignment); | 128 glPixelStorei(GL_PACK_ALIGNMENT, pack_alignment); |
| 52 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack_alignment); | 129 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack_alignment); |
| 53 | 130 |
| 54 glHint(GL_GENERATE_MIPMAP_HINT, hint_generate_mipmap); | 131 glHint(GL_GENERATE_MIPMAP_HINT, hint_generate_mipmap); |
| 55 // TODO: If OES_standard_derivatives is available | 132 // TODO: If OES_standard_derivatives is available |
| 56 // restore GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES | 133 // restore GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES |
| 134 } |
| 57 | 135 |
| 136 void ContextState::RestoreState() const { |
| 58 // Restore Texture state. | 137 // Restore Texture state. |
| 59 for (size_t ii = 0; ii < texture_units.size(); ++ii) { | 138 for (size_t ii = 0; ii < texture_units.size(); ++ii) { |
| 60 const TextureUnit& texture_unit = texture_units[ii]; | 139 RestoreTextureUnitBindings(ii); |
| 61 glActiveTexture(GL_TEXTURE0 + ii); | |
| 62 GLuint service_id = texture_unit.bound_texture_2d ? | |
| 63 texture_unit.bound_texture_2d->service_id() : 0; | |
| 64 glBindTexture(GL_TEXTURE_2D, service_id); | |
| 65 service_id = texture_unit.bound_texture_cube_map ? | |
| 66 texture_unit.bound_texture_cube_map->service_id() : 0; | |
| 67 glBindTexture(GL_TEXTURE_CUBE_MAP, service_id); | |
| 68 | |
| 69 if (feature_info_->feature_flags().oes_egl_image_external) { | |
| 70 service_id = texture_unit.bound_texture_external_oes ? | |
| 71 texture_unit.bound_texture_external_oes->service_id() : 0; | |
| 72 glBindTexture(GL_TEXTURE_EXTERNAL_OES, service_id); | |
| 73 } | |
| 74 | |
| 75 if (feature_info_->feature_flags().arb_texture_rectangle) { | |
| 76 service_id = texture_unit.bound_texture_rectangle_arb ? | |
| 77 texture_unit.bound_texture_rectangle_arb->service_id() : 0; | |
| 78 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, service_id); | |
| 79 } | |
| 80 } | 140 } |
| 81 glActiveTexture(GL_TEXTURE0 + active_texture_unit); | 141 RestoreActiveTexture(); |
| 82 | 142 |
| 83 // Restore Attrib State | 143 // Restore Attrib State |
| 84 // TODO: This if should not be needed. RestoreState is getting called | 144 // TODO: This if should not be needed. RestoreState is getting called |
| 85 // before GLES2Decoder::Initialize which is a bug. | 145 // before GLES2Decoder::Initialize which is a bug. |
| 86 if (vertex_attrib_manager) { | 146 if (vertex_attrib_manager) { |
| 87 // TODO(gman): Move this restoration to VertexAttribManager. | 147 // TODO(gman): Move this restoration to VertexAttribManager. |
| 88 for (size_t attrib = 0; attrib < vertex_attrib_manager->num_attribs(); | 148 for (size_t attrib = 0; attrib < vertex_attrib_manager->num_attribs(); |
| 89 ++attrib) { | 149 ++attrib) { |
| 90 const VertexAttribManager::VertexAttribInfo* info = | 150 RestoreAttribute(attrib); |
| 91 vertex_attrib_manager->GetVertexAttribInfo(attrib); | |
| 92 const void* ptr = reinterpret_cast<const void*>(info->offset()); | |
| 93 BufferManager::BufferInfo* buffer_info = info->buffer(); | |
| 94 glBindBuffer( | |
| 95 GL_ARRAY_BUFFER, buffer_info ? buffer_info->service_id() : 0); | |
| 96 glVertexAttribPointer( | |
| 97 attrib, info->size(), info->type(), info->normalized(), | |
| 98 info->gl_stride(), ptr); | |
| 99 if (info->divisor()) | |
| 100 glVertexAttribDivisorANGLE(attrib, info->divisor()); | |
| 101 // Never touch vertex attribute 0's state (in particular, never | |
| 102 // disable it) when running on desktop GL because it will never be | |
| 103 // re-enabled. | |
| 104 if (attrib != 0 || | |
| 105 gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2) { | |
| 106 if (info->enabled()) { | |
| 107 glEnableVertexAttribArray(attrib); | |
| 108 } else { | |
| 109 glDisableVertexAttribArray(attrib); | |
| 110 } | |
| 111 } | |
| 112 glVertexAttrib4fv(attrib, attrib_values[attrib].v); | |
| 113 } | 151 } |
| 114 BufferManager::BufferInfo* element_array_buffer = | |
| 115 vertex_attrib_manager->element_array_buffer(); | |
| 116 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, | |
| 117 element_array_buffer ? element_array_buffer->service_id() : 0); | |
| 118 } | 152 } |
| 119 | 153 |
| 120 // Restore Bindings | 154 RestoreRenderbufferBindings(); |
| 121 glBindBuffer( | 155 RestoreProgramBindings(); |
| 122 GL_ARRAY_BUFFER, | |
| 123 bound_array_buffer ? bound_array_buffer->service_id() : 0); | |
| 124 glBindRenderbufferEXT( | |
| 125 GL_RENDERBUFFER, | |
| 126 bound_renderbuffer ? bound_renderbuffer->service_id() : 0); | |
| 127 | |
| 128 glUseProgram(current_program ? current_program->service_id() : 0); | |
| 129 } | 156 } |
| 130 | 157 |
| 131 // Include the auto-generated part of this file. We split this because it means | 158 // Include the auto-generated part of this file. We split this because it means |
| 132 // we can easily edit the non-auto generated parts right here in this file | 159 // we can easily edit the non-auto generated parts right here in this file |
| 133 // instead of having to edit some template or the code generator. | 160 // instead of having to edit some template or the code generator. |
| 134 #include "gpu/command_buffer/service/context_state_impl_autogen.h" | 161 #include "gpu/command_buffer/service/context_state_impl_autogen.h" |
| 135 | 162 |
| 136 } // namespace gles2 | 163 } // namespace gles2 |
| 137 } // namespace gpu | 164 } // namespace gpu |
| 138 | 165 |
| 139 | 166 |
| OLD | NEW |