Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "android_webview/browser/scoped_app_gl_state_restore.h" | 5 #include "android_webview/browser/scoped_app_gl_state_restore.h" |
| 6 | 6 |
| 7 #include "base/debug/trace_event.h" | |
| 7 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 8 #include "ui/gl/gl_context.h" | 9 #include "ui/gl/gl_context.h" |
| 9 #include "ui/gl/gl_surface_stub.h" | 10 #include "ui/gl/gl_surface_stub.h" |
| 10 | 11 |
| 11 namespace android_webview { | 12 namespace android_webview { |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 // "App" context is a bit of a stretch. Basically we use this context while | 16 // "App" context is a bit of a stretch. Basically we use this context while |
| 16 // saving and restoring the App GL state. | 17 // saving and restoring the App GL state. |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 35 | 36 |
| 36 // Make the global g_app_context_surface current so that the gl_binding is not | 37 // Make the global g_app_context_surface current so that the gl_binding is not |
| 37 // NULL for making gl* calls. The binding can be null if another GlContext was | 38 // NULL for making gl* calls. The binding can be null if another GlContext was |
| 38 // destroyed immediately before gl* calls here. | 39 // destroyed immediately before gl* calls here. |
| 39 void MakeAppContextCurrent() { | 40 void MakeAppContextCurrent() { |
| 40 g_app_context_surface.Get().MakeCurrent(); | 41 g_app_context_surface.Get().MakeCurrent(); |
| 41 } | 42 } |
| 42 | 43 |
| 43 } // namespace | 44 } // namespace |
| 44 | 45 |
| 45 ScopedAppGLStateRestore::ScopedAppGLStateRestore(CallMode mode) { | 46 ScopedAppGLStateRestore::ScopedAppGLStateRestore(CallMode mode) : mode_(mode) { |
| 47 TRACE_EVENT0("android_webview", "AppGLStateSave"); | |
| 46 MakeAppContextCurrent(); | 48 MakeAppContextCurrent(); |
| 47 | 49 |
| 48 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &vertex_array_buffer_binding_); | 50 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &vertex_array_buffer_binding_); |
| 49 glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &index_array_buffer_binding_); | 51 glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &index_array_buffer_binding_); |
| 50 | 52 |
| 51 switch(mode) { | 53 switch(mode_) { |
| 52 case MODE_DRAW: | 54 case MODE_DRAW: |
| 53 // TODO(boliu): These should always be 0 in draw case. When we have | 55 // TODO(boliu): These should always be 0 in draw case. When we have |
| 54 // guarantee that we are no longer making GL calls outside of draw, DCHECK | 56 // guarantee that we are no longer making GL calls outside of draw, DCHECK |
| 55 // these are 0 here. | 57 // these are 0 here. |
| 56 LOG_IF(ERROR, vertex_array_buffer_binding_) | 58 LOG_IF(ERROR, vertex_array_buffer_binding_) |
| 57 << "GL_ARRAY_BUFFER_BINDING not zero in draw: " | 59 << "GL_ARRAY_BUFFER_BINDING not zero in draw: " |
| 58 << vertex_array_buffer_binding_; | 60 << vertex_array_buffer_binding_; |
| 59 LOG_IF(ERROR, index_array_buffer_binding_) | 61 LOG_IF(ERROR, index_array_buffer_binding_) |
| 60 << "GL_ELEMENT_ARRAY_BUFFER_BINDING not zero in draw: " | 62 << "GL_ELEMENT_ARRAY_BUFFER_BINDING not zero in draw: " |
| 61 << index_array_buffer_binding_; | 63 << index_array_buffer_binding_; |
| 62 | 64 |
| 63 vertex_array_buffer_binding_ = 0; | 65 vertex_array_buffer_binding_ = 0; |
| 64 index_array_buffer_binding_ = 0; | 66 index_array_buffer_binding_ = 0; |
| 65 break; | 67 break; |
| 66 case MODE_DETACH_FROM_WINDOW: | 68 case MODE_DETACH_FROM_WINDOW: |
| 69 glGetBooleanv(GL_BLEND, &blend_enabled_); | |
| 70 glGetIntegerv(GL_BLEND_SRC_RGB, &blend_src_rgb_); | |
| 71 glGetIntegerv(GL_BLEND_SRC_ALPHA, &blend_src_alpha_); | |
| 72 glGetIntegerv(GL_BLEND_DST_RGB, &blend_dest_rgb_); | |
| 73 glGetIntegerv(GL_BLEND_DST_ALPHA, &blend_dest_alpha_); | |
| 74 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture_); | |
| 75 glGetIntegerv(GL_VIEWPORT, viewport_); | |
| 76 glGetBooleanv(GL_SCISSOR_TEST, &scissor_test_); | |
| 77 glGetIntegerv(GL_SCISSOR_BOX, scissor_box_); | |
| 67 break; | 78 break; |
| 68 } | 79 } |
| 69 | 80 |
| 70 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, | 81 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, |
| 71 &texture_external_oes_binding_); | 82 &texture_external_oes_binding_); |
| 72 glGetIntegerv(GL_PACK_ALIGNMENT, &pack_alignment_); | 83 glGetIntegerv(GL_PACK_ALIGNMENT, &pack_alignment_); |
| 73 glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpack_alignment_); | 84 glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpack_alignment_); |
| 74 | 85 |
| 75 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(vertex_attrib_); ++i) { | 86 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(vertex_attrib_); ++i) { |
| 76 glGetVertexAttribiv( | 87 glGetVertexAttribiv( |
| 77 i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &vertex_attrib_[i].enabled); | 88 i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &vertex_attrib_[i].enabled); |
| 78 glGetVertexAttribiv( | 89 glGetVertexAttribiv( |
| 79 i, GL_VERTEX_ATTRIB_ARRAY_SIZE, &vertex_attrib_[i].size); | 90 i, GL_VERTEX_ATTRIB_ARRAY_SIZE, &vertex_attrib_[i].size); |
| 80 glGetVertexAttribiv( | 91 glGetVertexAttribiv( |
| 81 i, GL_VERTEX_ATTRIB_ARRAY_TYPE, &vertex_attrib_[i].type); | 92 i, GL_VERTEX_ATTRIB_ARRAY_TYPE, &vertex_attrib_[i].type); |
| 82 glGetVertexAttribiv( | 93 glGetVertexAttribiv( |
| 83 i, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &vertex_attrib_[i].normalized); | 94 i, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &vertex_attrib_[i].normalized); |
| 84 glGetVertexAttribiv( | 95 glGetVertexAttribiv( |
| 85 i, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &vertex_attrib_[i].stride); | 96 i, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &vertex_attrib_[i].stride); |
| 86 glGetVertexAttribPointerv( | 97 glGetVertexAttribPointerv( |
| 87 i, GL_VERTEX_ATTRIB_ARRAY_POINTER, &vertex_attrib_[i].pointer); | 98 i, GL_VERTEX_ATTRIB_ARRAY_POINTER, &vertex_attrib_[i].pointer); |
| 88 } | 99 } |
| 89 | 100 |
| 90 glGetBooleanv(GL_DEPTH_TEST, &depth_test_); | 101 glGetBooleanv(GL_DEPTH_TEST, &depth_test_); |
| 91 glGetBooleanv(GL_CULL_FACE, &cull_face_); | 102 glGetBooleanv(GL_CULL_FACE, &cull_face_); |
| 103 glGetIntegerv(GL_CULL_FACE_MODE, &cull_face_mode_); | |
| 92 glGetBooleanv(GL_COLOR_WRITEMASK, color_mask_); | 104 glGetBooleanv(GL_COLOR_WRITEMASK, color_mask_); |
| 93 glGetBooleanv(GL_BLEND, &blend_enabled_); | |
| 94 glGetIntegerv(GL_BLEND_SRC_RGB, &blend_src_rgb_); | |
| 95 glGetIntegerv(GL_BLEND_SRC_ALPHA, &blend_src_alpha_); | |
| 96 glGetIntegerv(GL_BLEND_DST_RGB, &blend_dest_rgb_); | |
| 97 glGetIntegerv(GL_BLEND_DST_ALPHA, &blend_dest_alpha_); | |
| 98 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture_); | |
| 99 glGetIntegerv(GL_VIEWPORT, viewport_); | |
| 100 glGetBooleanv(GL_SCISSOR_TEST, &scissor_test_); | |
| 101 glGetIntegerv(GL_SCISSOR_BOX, scissor_box_); | |
| 102 glGetIntegerv(GL_CURRENT_PROGRAM, ¤t_program_); | 105 glGetIntegerv(GL_CURRENT_PROGRAM, ¤t_program_); |
| 106 glGetFloatv(GL_COLOR_CLEAR_VALUE, color_clear_); | |
| 107 glGetFloatv(GL_DEPTH_CLEAR_VALUE, &depth_clear_); | |
| 108 glGetIntegerv(GL_DEPTH_FUNC, &depth_func_); | |
| 109 glGetBooleanv(GL_DEPTH_WRITEMASK, &depth_mask_); | |
| 110 glGetFloatv(GL_DEPTH_RANGE, depth_rage_); | |
| 111 glGetIntegerv(GL_FRONT_FACE, &front_face_); | |
| 112 glGetIntegerv(GL_GENERATE_MIPMAP_HINT, &hint_generate_mipmap_); | |
| 113 glGetFloatv(GL_LINE_WIDTH, &line_width_); | |
| 114 glGetFloatv(GL_POLYGON_OFFSET_FACTOR, &polygon_offset_factor_); | |
| 115 glGetFloatv(GL_POLYGON_OFFSET_UNITS, &polygon_offset_units_); | |
| 116 glGetFloatv(GL_SAMPLE_COVERAGE_VALUE, &sample_coverage_value_); | |
| 117 glGetBooleanv(GL_SAMPLE_COVERAGE_INVERT, &sample_coverage_invert_); | |
| 118 | |
| 119 glGetBooleanv(GL_DITHER, &enable_dither_); | |
| 120 glGetBooleanv(GL_POLYGON_OFFSET_FILL, &enable_polygon_offset_fill_); | |
| 121 glGetBooleanv(GL_SAMPLE_ALPHA_TO_COVERAGE, &enable_sample_alpha_to_coverage_); | |
| 122 glGetBooleanv(GL_SAMPLE_COVERAGE, &enable_sample_coverage_); | |
| 123 | |
| 124 // Intentionally not saving/restoring stencil related state. | |
| 103 } | 125 } |
| 104 | 126 |
| 105 ScopedAppGLStateRestore::~ScopedAppGLStateRestore() { | 127 ScopedAppGLStateRestore::~ScopedAppGLStateRestore() { |
| 128 TRACE_EVENT0("android_webview", "AppGLStateRestore"); | |
| 106 MakeAppContextCurrent(); | 129 MakeAppContextCurrent(); |
| 107 | 130 |
| 108 glBindBuffer(GL_ARRAY_BUFFER, vertex_array_buffer_binding_); | 131 glBindBuffer(GL_ARRAY_BUFFER, vertex_array_buffer_binding_); |
| 109 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_array_buffer_binding_); | 132 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_array_buffer_binding_); |
| 110 | 133 |
| 111 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_external_oes_binding_); | 134 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_external_oes_binding_); |
| 112 glPixelStorei(GL_PACK_ALIGNMENT, pack_alignment_); | 135 glPixelStorei(GL_PACK_ALIGNMENT, pack_alignment_); |
| 113 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack_alignment_); | 136 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack_alignment_); |
| 114 | 137 |
| 115 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(vertex_attrib_); ++i) { | 138 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(vertex_attrib_); ++i) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 131 glEnable(GL_DEPTH_TEST); | 154 glEnable(GL_DEPTH_TEST); |
| 132 } else { | 155 } else { |
| 133 glDisable(GL_DEPTH_TEST); | 156 glDisable(GL_DEPTH_TEST); |
| 134 } | 157 } |
| 135 | 158 |
| 136 if (cull_face_) { | 159 if (cull_face_) { |
| 137 glEnable(GL_CULL_FACE); | 160 glEnable(GL_CULL_FACE); |
| 138 } else { | 161 } else { |
| 139 glDisable(GL_CULL_FACE); | 162 glDisable(GL_CULL_FACE); |
| 140 } | 163 } |
| 164 glCullFace(cull_face_mode_); | |
| 141 | 165 |
| 142 glColorMask(color_mask_[0], color_mask_[1], color_mask_[2], color_mask_[3]); | 166 glColorMask(color_mask_[0], color_mask_[1], color_mask_[2], color_mask_[3]); |
| 143 | 167 |
| 144 if (blend_enabled_) { | 168 glUseProgram(current_program_); |
| 145 glEnable(GL_BLEND); | 169 |
| 146 } else { | 170 glClearColor( |
| 147 glDisable(GL_BLEND); | 171 color_clear_[0], color_clear_[1], color_clear_[2], color_clear_[3]); |
| 172 glClearDepth(depth_clear_); | |
| 173 glDepthFunc(depth_func_); | |
| 174 glDepthMask(depth_mask_); | |
| 175 glDepthRange(depth_rage_[0], depth_rage_[1]); | |
| 176 glFrontFace(front_face_); | |
| 177 glHint(GL_GENERATE_MIPMAP_HINT, hint_generate_mipmap_); | |
| 178 // TODO(boliu): GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES ?? | |
| 179 glLineWidth(line_width_); | |
| 180 glPolygonOffset(polygon_offset_factor_, polygon_offset_units_); | |
| 181 glSampleCoverage(sample_coverage_value_, sample_coverage_invert_); | |
| 182 | |
| 183 if (enable_dither_) | |
| 184 glEnable(GL_DITHER); | |
| 185 else | |
| 186 glDisable(GL_DITHER); | |
|
joth
2013/07/23 22:02:54
Maybe extract a helper for all these:
glEnable
| |
| 187 | |
| 188 if (enable_polygon_offset_fill_) | |
| 189 glEnable(GL_POLYGON_OFFSET_FILL); | |
| 190 else | |
| 191 glDisable(GL_POLYGON_OFFSET_FILL); | |
| 192 | |
| 193 if (enable_sample_alpha_to_coverage_) | |
| 194 glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE); | |
| 195 else | |
| 196 glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE); | |
| 197 | |
| 198 if (enable_sample_coverage_) | |
| 199 glEnable(GL_SAMPLE_COVERAGE); | |
| 200 else | |
| 201 glDisable(GL_SAMPLE_COVERAGE); | |
| 202 | |
| 203 if (mode_ == MODE_DETACH_FROM_WINDOW) { | |
| 204 if (blend_enabled_) | |
| 205 glEnable(GL_BLEND); | |
| 206 else | |
| 207 glDisable(GL_BLEND); | |
| 208 | |
| 209 glBlendFuncSeparate( | |
| 210 blend_src_rgb_, blend_dest_rgb_, blend_src_alpha_, blend_dest_alpha_); | |
| 211 glActiveTexture(active_texture_); | |
| 212 | |
| 213 glViewport(viewport_[0], viewport_[1], viewport_[2], viewport_[3]); | |
| 214 | |
| 215 if (scissor_test_) | |
| 216 glEnable(GL_SCISSOR_TEST); | |
| 217 else | |
| 218 glDisable(GL_SCISSOR_TEST); | |
| 219 | |
| 220 glScissor( | |
| 221 scissor_box_[0], scissor_box_[1], scissor_box_[2], scissor_box_[3]); | |
| 148 } | 222 } |
| 149 | |
| 150 glBlendFuncSeparate( | |
| 151 blend_src_rgb_, blend_dest_rgb_, blend_src_alpha_, blend_dest_alpha_); | |
| 152 glActiveTexture(active_texture_); | |
| 153 | |
| 154 glViewport(viewport_[0], viewport_[1], viewport_[2], viewport_[3]); | |
| 155 | |
| 156 if (scissor_test_) { | |
| 157 glEnable(GL_SCISSOR_TEST); | |
| 158 } else { | |
| 159 glDisable(GL_SCISSOR_TEST); | |
| 160 } | |
| 161 | |
| 162 glScissor( | |
| 163 scissor_box_[0], scissor_box_[1], scissor_box_[2], scissor_box_[3]); | |
| 164 | |
| 165 glUseProgram(current_program_); | |
| 166 } | 223 } |
| 167 | 224 |
| 168 } // namespace android_webview | 225 } // namespace android_webview |
| OLD | NEW |