gman@chromium.org | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 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 | #include "gpu/command_buffer/service/context_state.h" |
| 6 | |
gman@chromium.org | f731b946 | 2012-10-30 00:35:22 | [diff] [blame] | 7 | #include "gpu/command_buffer/common/gles2_cmd_utils.h" |
gman@chromium.org | 31494b8 | 2013-02-28 10:10:26 | [diff] [blame] | 8 | #include "gpu/command_buffer/service/buffer_manager.h" |
kloveless@chromium.org | d3eba34 | 2013-04-18 21:11:50 | [diff] [blame] | 9 | #include "gpu/command_buffer/service/error_state.h" |
gman@chromium.org | 31494b8 | 2013-02-28 10:10:26 | [diff] [blame] | 10 | #include "gpu/command_buffer/service/framebuffer_manager.h" |
| 11 | #include "gpu/command_buffer/service/program_manager.h" |
| 12 | #include "gpu/command_buffer/service/renderbuffer_manager.h" |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 13 | #include "ui/gl/gl_bindings.h" |
| 14 | #include "ui/gl/gl_implementation.h" |
gman@chromium.org | f731b946 | 2012-10-30 00:35:22 | [diff] [blame] | 15 | |
gman@chromium.org | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 16 | namespace gpu { |
| 17 | namespace gles2 { |
| 18 | |
gman@chromium.org | f731b946 | 2012-10-30 00:35:22 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | void EnableDisable(GLenum pname, bool enable) { |
| 22 | if (enable) { |
| 23 | glEnable(pname); |
| 24 | } else { |
| 25 | glDisable(pname); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | } // anonymous namespace. |
| 30 | |
gman@chromium.org | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 31 | TextureUnit::TextureUnit() |
| 32 | : bind_target(GL_TEXTURE_2D) { |
| 33 | } |
| 34 | |
| 35 | TextureUnit::~TextureUnit() { |
| 36 | } |
| 37 | |
kloveless@chromium.org | d3eba34 | 2013-04-18 21:11:50 | [diff] [blame] | 38 | ContextState::ContextState(FeatureInfo* feature_info, Logger* logger) |
gman@chromium.org | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 39 | : pack_alignment(4), |
| 40 | unpack_alignment(4), |
| 41 | active_texture_unit(0), |
gman@chromium.org | 88a61bf | 2012-10-27 13:00:42 | [diff] [blame] | 42 | hint_generate_mipmap(GL_DONT_CARE), |
| 43 | hint_fragment_shader_derivative(GL_DONT_CARE), |
sievers@chromium.org | b3cbad1 | 2012-12-05 19:56:36 | [diff] [blame] | 44 | pack_reverse_row_order(false), |
sievers@chromium.org | 28718a9 | 2013-04-04 12:12:51 | [diff] [blame] | 45 | fbo_binding_for_scissor_workaround_dirty_(false), |
kloveless@chromium.org | d3eba34 | 2013-04-18 21:11:50 | [diff] [blame] | 46 | feature_info_(feature_info), |
| 47 | error_state_(ErrorState::Create(logger)) { |
gman@chromium.org | f731b946 | 2012-10-30 00:35:22 | [diff] [blame] | 48 | Initialize(); |
gman@chromium.org | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | ContextState::~ContextState() { |
| 52 | } |
| 53 | |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 54 | void ContextState::RestoreTextureUnitBindings(GLuint unit) const { |
| 55 | DCHECK_LT(unit, texture_units.size()); |
| 56 | const TextureUnit& texture_unit = texture_units[unit]; |
| 57 | glActiveTexture(GL_TEXTURE0 + unit); |
| 58 | GLuint service_id = texture_unit.bound_texture_2d ? |
| 59 | texture_unit.bound_texture_2d->service_id() : 0; |
| 60 | glBindTexture(GL_TEXTURE_2D, service_id); |
| 61 | service_id = texture_unit.bound_texture_cube_map ? |
| 62 | texture_unit.bound_texture_cube_map->service_id() : 0; |
| 63 | glBindTexture(GL_TEXTURE_CUBE_MAP, service_id); |
| 64 | |
| 65 | if (feature_info_->feature_flags().oes_egl_image_external) { |
| 66 | service_id = texture_unit.bound_texture_external_oes ? |
| 67 | texture_unit.bound_texture_external_oes->service_id() : 0; |
| 68 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, service_id); |
| 69 | } |
| 70 | |
| 71 | if (feature_info_->feature_flags().arb_texture_rectangle) { |
| 72 | service_id = texture_unit.bound_texture_rectangle_arb ? |
| 73 | texture_unit.bound_texture_rectangle_arb->service_id() : 0; |
| 74 | glBindTexture(GL_TEXTURE_RECTANGLE_ARB, service_id); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void ContextState::RestoreBufferBindings() const { |
| 79 | if (vertex_attrib_manager) { |
gman@chromium.org | 16ccec1 | 2013-02-28 03:40:21 | [diff] [blame] | 80 | Buffer* element_array_buffer = |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 81 | vertex_attrib_manager->element_array_buffer(); |
| 82 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, |
| 83 | element_array_buffer ? element_array_buffer->service_id() : 0); |
| 84 | } |
| 85 | glBindBuffer( |
| 86 | GL_ARRAY_BUFFER, |
| 87 | bound_array_buffer ? bound_array_buffer->service_id() : 0); |
| 88 | } |
| 89 | |
| 90 | void ContextState::RestoreRenderbufferBindings() const { |
| 91 | // Restore Bindings |
| 92 | glBindRenderbufferEXT( |
| 93 | GL_RENDERBUFFER, |
| 94 | bound_renderbuffer ? bound_renderbuffer->service_id() : 0); |
| 95 | } |
| 96 | |
| 97 | void ContextState::RestoreProgramBindings() const { |
| 98 | glUseProgram(current_program ? current_program->service_id() : 0); |
| 99 | } |
| 100 | |
| 101 | void ContextState::RestoreActiveTexture() const { |
| 102 | glActiveTexture(GL_TEXTURE0 + active_texture_unit); |
| 103 | } |
| 104 | |
backer@chromium.org | 21700451 | 2013-05-10 21:25:55 | [diff] [blame^] | 105 | void ContextState::RestoreAllTextureUnitBindings() const { |
| 106 | // Restore Texture state. |
| 107 | for (size_t ii = 0; ii < texture_units.size(); ++ii) { |
| 108 | RestoreTextureUnitBindings(ii); |
| 109 | } |
| 110 | RestoreActiveTexture(); |
| 111 | } |
| 112 | |
gman@chromium.org | ac77603c7 | 2013-03-08 13:52:06 | [diff] [blame] | 113 | void ContextState::RestoreAttribute(GLuint attrib_index) const { |
| 114 | const VertexAttrib* attrib = |
| 115 | vertex_attrib_manager->GetVertexAttrib(attrib_index); |
| 116 | const void* ptr = reinterpret_cast<const void*>(attrib->offset()); |
| 117 | Buffer* buffer = attrib->buffer(); |
gman@chromium.org | b10492f | 2013-03-08 05:24:07 | [diff] [blame] | 118 | glBindBuffer(GL_ARRAY_BUFFER, buffer ? buffer->service_id() : 0); |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 119 | glVertexAttribPointer( |
gman@chromium.org | ac77603c7 | 2013-03-08 13:52:06 | [diff] [blame] | 120 | attrib_index, attrib->size(), attrib->type(), attrib->normalized(), |
| 121 | attrib->gl_stride(), ptr); |
| 122 | if (attrib->divisor()) |
| 123 | glVertexAttribDivisorANGLE(attrib_index, attrib->divisor()); |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 124 | // Never touch vertex attribute 0's state (in particular, never |
| 125 | // disable it) when running on desktop GL because it will never be |
| 126 | // re-enabled. |
gman@chromium.org | ac77603c7 | 2013-03-08 13:52:06 | [diff] [blame] | 127 | if (attrib_index != 0 || |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 128 | gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2) { |
gman@chromium.org | ac77603c7 | 2013-03-08 13:52:06 | [diff] [blame] | 129 | if (attrib->enabled()) { |
| 130 | glEnableVertexAttribArray(attrib_index); |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 131 | } else { |
gman@chromium.org | ac77603c7 | 2013-03-08 13:52:06 | [diff] [blame] | 132 | glDisableVertexAttribArray(attrib_index); |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 133 | } |
| 134 | } |
gman@chromium.org | ac77603c7 | 2013-03-08 13:52:06 | [diff] [blame] | 135 | glVertexAttrib4fv(attrib_index, attrib_values[attrib_index].v); |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void ContextState::RestoreGlobalState() const { |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 139 | InitCapabilities(); |
| 140 | InitState(); |
| 141 | |
| 142 | glPixelStorei(GL_PACK_ALIGNMENT, pack_alignment); |
| 143 | glPixelStorei(GL_UNPACK_ALIGNMENT, unpack_alignment); |
| 144 | |
| 145 | glHint(GL_GENERATE_MIPMAP_HINT, hint_generate_mipmap); |
| 146 | // TODO: If OES_standard_derivatives is available |
| 147 | // restore GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 148 | } |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 149 | |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 150 | void ContextState::RestoreState() const { |
backer@chromium.org | 21700451 | 2013-05-10 21:25:55 | [diff] [blame^] | 151 | RestoreAllTextureUnitBindings(); |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 152 | |
| 153 | // Restore Attrib State |
| 154 | // TODO: This if should not be needed. RestoreState is getting called |
| 155 | // before GLES2Decoder::Initialize which is a bug. |
| 156 | if (vertex_attrib_manager) { |
gman@chromium.org | af638096 | 2012-11-29 23:24:13 | [diff] [blame] | 157 | // TODO(gman): Move this restoration to VertexAttribManager. |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 158 | for (size_t attrib = 0; attrib < vertex_attrib_manager->num_attribs(); |
| 159 | ++attrib) { |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 160 | RestoreAttribute(attrib); |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 161 | } |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 162 | } |
| 163 | |
gman@chromium.org | 15cc23fa | 2013-02-26 21:56:25 | [diff] [blame] | 164 | RestoreBufferBindings(); |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 165 | RestoreRenderbufferBindings(); |
| 166 | RestoreProgramBindings(); |
gman@chromium.org | 15cc23fa | 2013-02-26 21:56:25 | [diff] [blame] | 167 | RestoreGlobalState(); |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 168 | } |
| 169 | |
kloveless@chromium.org | d3eba34 | 2013-04-18 21:11:50 | [diff] [blame] | 170 | ErrorState* ContextState::GetErrorState() { |
| 171 | return error_state_.get(); |
| 172 | } |
| 173 | |
gman@chromium.org | f731b946 | 2012-10-30 00:35:22 | [diff] [blame] | 174 | // Include the auto-generated part of this file. We split this because it means |
| 175 | // we can easily edit the non-auto generated parts right here in this file |
| 176 | // instead of having to edit some template or the code generator. |
| 177 | #include "gpu/command_buffer/service/context_state_impl_autogen.h" |
| 178 | |
gman@chromium.org | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 179 | } // namespace gles2 |
| 180 | } // namespace gpu |
| 181 | |
| 182 | |