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 | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 8 | #include "ui/gl/gl_bindings.h" |
| 9 | #include "ui/gl/gl_implementation.h" |
gman@chromium.org | f731b946 | 2012-10-30 00:35:22 | [diff] [blame] | 10 | |
gman@chromium.org | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 11 | namespace gpu { |
| 12 | namespace gles2 { |
| 13 | |
gman@chromium.org | f731b946 | 2012-10-30 00:35:22 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | void EnableDisable(GLenum pname, bool enable) { |
| 17 | if (enable) { |
| 18 | glEnable(pname); |
| 19 | } else { |
| 20 | glDisable(pname); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | } // anonymous namespace. |
| 25 | |
gman@chromium.org | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 26 | TextureUnit::TextureUnit() |
| 27 | : bind_target(GL_TEXTURE_2D) { |
| 28 | } |
| 29 | |
| 30 | TextureUnit::~TextureUnit() { |
| 31 | } |
| 32 | |
sievers@chromium.org | b3cbad1 | 2012-12-05 19:56:36 | [diff] [blame] | 33 | ContextState::ContextState(FeatureInfo* feature_info) |
gman@chromium.org | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 34 | : pack_alignment(4), |
| 35 | unpack_alignment(4), |
| 36 | active_texture_unit(0), |
gman@chromium.org | 88a61bf | 2012-10-27 13:00:42 | [diff] [blame] | 37 | hint_generate_mipmap(GL_DONT_CARE), |
| 38 | hint_fragment_shader_derivative(GL_DONT_CARE), |
sievers@chromium.org | b3cbad1 | 2012-12-05 19:56:36 | [diff] [blame] | 39 | pack_reverse_row_order(false), |
| 40 | feature_info_(feature_info) { |
gman@chromium.org | f731b946 | 2012-10-30 00:35:22 | [diff] [blame] | 41 | Initialize(); |
gman@chromium.org | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | ContextState::~ContextState() { |
| 45 | } |
| 46 | |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 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) { |
gman@chromium.org | 16ccec1 | 2013-02-28 03:40:21 | [diff] [blame^] | 73 | Buffer* element_array_buffer = |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 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 { |
gman@chromium.org | ed9f9cd | 2013-02-27 21:12:35 | [diff] [blame] | 99 | const VertexAttrib* info = |
| 100 | vertex_attrib_manager->GetVertexAttrib(attrib); |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 101 | const void* ptr = reinterpret_cast<const void*>(info->offset()); |
gman@chromium.org | 16ccec1 | 2013-02-28 03:40:21 | [diff] [blame^] | 102 | Buffer* buffer_info = info->buffer(); |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 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 { |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 125 | InitCapabilities(); |
| 126 | InitState(); |
| 127 | |
| 128 | glPixelStorei(GL_PACK_ALIGNMENT, pack_alignment); |
| 129 | glPixelStorei(GL_UNPACK_ALIGNMENT, unpack_alignment); |
| 130 | |
| 131 | glHint(GL_GENERATE_MIPMAP_HINT, hint_generate_mipmap); |
| 132 | // TODO: If OES_standard_derivatives is available |
| 133 | // restore GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 134 | } |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 135 | |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 136 | void ContextState::RestoreState() const { |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 137 | // Restore Texture state. |
| 138 | for (size_t ii = 0; ii < texture_units.size(); ++ii) { |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 139 | RestoreTextureUnitBindings(ii); |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 140 | } |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 141 | RestoreActiveTexture(); |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 142 | |
| 143 | // Restore Attrib State |
| 144 | // TODO: This if should not be needed. RestoreState is getting called |
| 145 | // before GLES2Decoder::Initialize which is a bug. |
| 146 | if (vertex_attrib_manager) { |
gman@chromium.org | af638096 | 2012-11-29 23:24:13 | [diff] [blame] | 147 | // TODO(gman): Move this restoration to VertexAttribManager. |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 148 | for (size_t attrib = 0; attrib < vertex_attrib_manager->num_attribs(); |
| 149 | ++attrib) { |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 150 | RestoreAttribute(attrib); |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 151 | } |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 152 | } |
| 153 | |
gman@chromium.org | 15cc23fa | 2013-02-26 21:56:25 | [diff] [blame] | 154 | RestoreBufferBindings(); |
gman@chromium.org | 29a4d90 | 2013-02-26 20:18:06 | [diff] [blame] | 155 | RestoreRenderbufferBindings(); |
| 156 | RestoreProgramBindings(); |
gman@chromium.org | 15cc23fa | 2013-02-26 21:56:25 | [diff] [blame] | 157 | RestoreGlobalState(); |
gman@chromium.org | 1868a34 | 2012-11-07 15:56:02 | [diff] [blame] | 158 | } |
| 159 | |
gman@chromium.org | f731b946 | 2012-10-30 00:35:22 | [diff] [blame] | 160 | // Include the auto-generated part of this file. We split this because it means |
| 161 | // we can easily edit the non-auto generated parts right here in this file |
| 162 | // instead of having to edit some template or the code generator. |
| 163 | #include "gpu/command_buffer/service/context_state_impl_autogen.h" |
| 164 | |
gman@chromium.org | e259eb41 | 2012-10-13 05:47:24 | [diff] [blame] | 165 | } // namespace gles2 |
| 166 | } // namespace gpu |
| 167 | |
| 168 | |