Chromium Code Reviews| 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 "ui/gl/scoped_binders.h" | 5 #include "ui/gl/scoped_binders.h" |
| 6 #include "ui/gl/gl_bindings.h" | 6 #include "ui/gl/gl_bindings.h" |
| 7 #include "ui/gl/gl_context.h" | 7 #include "ui/gl/gl_context.h" |
| 8 #include "ui/gl/gl_state_restorer.h" | 8 #include "ui/gl/gl_state_restorer.h" |
| 9 | 9 |
| 10 namespace gl { | 10 namespace gl { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 if (state_restorer_) { | 23 if (state_restorer_) { |
| 24 DCHECK(!!GLContext::GetCurrent()); | 24 DCHECK(!!GLContext::GetCurrent()); |
| 25 DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer()); | 25 DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer()); |
| 26 state_restorer_->RestoreFramebufferBindings(); | 26 state_restorer_->RestoreFramebufferBindings(); |
| 27 } else { | 27 } else { |
| 28 glBindFramebufferEXT(GL_FRAMEBUFFER, old_fbo_); | 28 glBindFramebufferEXT(GL_FRAMEBUFFER, old_fbo_); |
| 29 } | 29 } |
| 30 } | 30 } |
| 31 | 31 |
| 32 ScopedActiveTexture::ScopedActiveTexture(unsigned int texture) | 32 ScopedActiveTexture::ScopedActiveTexture(unsigned int texture) |
| 33 : old_texture_(-1) { | 33 : state_restorer_(!GLContext::GetCurrent() |
| 34 glGetIntegerv(GL_ACTIVE_TEXTURE, &old_texture_); | 34 ? NULL |
| 35 : GLContext::GetCurrent()->GetGLStateRestorer()), | |
| 36 old_texture_(-1) { | |
| 37 if (!state_restorer_) | |
| 38 glGetIntegerv(GL_ACTIVE_TEXTURE, &old_texture_); | |
| 35 glActiveTexture(texture); | 39 glActiveTexture(texture); |
| 36 } | 40 } |
| 37 | 41 |
| 38 ScopedActiveTexture::~ScopedActiveTexture() { | 42 ScopedActiveTexture::~ScopedActiveTexture() { |
| 39 glActiveTexture(old_texture_); | 43 if (state_restorer_) { |
| 44 DCHECK(!!GLContext::GetCurrent()); | |
| 45 DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer()); | |
| 46 state_restorer_->RestoreActiveTexture(); | |
| 47 } else { | |
| 48 glActiveTexture(old_texture_); | |
| 49 } | |
| 40 } | 50 } |
| 41 | 51 |
| 42 ScopedTextureBinder::ScopedTextureBinder(unsigned int target, unsigned int id) | 52 ScopedTextureBinder::ScopedTextureBinder(unsigned int target, unsigned int id) |
| 43 : state_restorer_(!GLContext::GetCurrent() | 53 : state_restorer_(!GLContext::GetCurrent() |
| 44 ? NULL | 54 ? NULL |
| 45 : GLContext::GetCurrent()->GetGLStateRestorer()), | 55 : GLContext::GetCurrent()->GetGLStateRestorer()), |
| 46 target_(target), | 56 target_(target), |
| 47 old_id_(-1) { | 57 old_id_(-1) { |
| 48 if (!state_restorer_) { | 58 if (!state_restorer_) { |
| 49 GLenum target_getter = 0; | 59 GLenum target_getter = 0; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 71 ScopedTextureBinder::~ScopedTextureBinder() { | 81 ScopedTextureBinder::~ScopedTextureBinder() { |
| 72 if (state_restorer_) { | 82 if (state_restorer_) { |
| 73 DCHECK(!!GLContext::GetCurrent()); | 83 DCHECK(!!GLContext::GetCurrent()); |
| 74 DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer()); | 84 DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer()); |
| 75 state_restorer_->RestoreActiveTextureUnitBinding(target_); | 85 state_restorer_->RestoreActiveTextureUnitBinding(target_); |
| 76 } else { | 86 } else { |
| 77 glBindTexture(target_, old_id_); | 87 glBindTexture(target_, old_id_); |
| 78 } | 88 } |
| 79 } | 89 } |
| 80 | 90 |
| 81 ScopedUseProgram::ScopedUseProgram(unsigned int program) : old_program_(-1) { | 91 ScopedUseProgram::ScopedUseProgram(unsigned int program) |
| 82 glGetIntegerv(GL_CURRENT_PROGRAM, &old_program_); | 92 : state_restorer_(!GLContext::GetCurrent() |
| 93 ? NULL | |
| 94 : GLContext::GetCurrent()->GetGLStateRestorer()), | |
| 95 old_program_(-1) { | |
| 96 if (!state_restorer_) | |
| 97 glGetIntegerv(GL_CURRENT_PROGRAM, &old_program_); | |
| 83 glUseProgram(program); | 98 glUseProgram(program); |
| 84 } | 99 } |
| 85 | 100 |
| 86 ScopedUseProgram::~ScopedUseProgram() { | 101 ScopedUseProgram::~ScopedUseProgram() { |
| 87 glUseProgram(old_program_); | 102 if (state_restorer_) { |
| 103 DCHECK(!!GLContext::GetCurrent()); | |
| 104 DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer()); | |
| 105 state_restorer_->RestoreProgramBindings(); | |
| 106 } else { | |
| 107 glUseProgram(old_program_); | |
| 108 } | |
| 88 } | 109 } |
| 89 | 110 |
| 90 ScopedVertexAttribArray::ScopedVertexAttribArray(unsigned int index, | 111 ScopedVertexAttribArray::ScopedVertexAttribArray(unsigned int index, |
| 91 int size, | 112 int size, |
| 92 unsigned int type, | 113 unsigned int type, |
| 93 char normalized, | 114 char normalized, |
| 94 int stride, | 115 int stride, |
| 95 const void* pointer) | 116 const void* pointer) |
| 96 : buffer_(0), | 117 : state_restorer_(!GLContext::GetCurrent() |
| 118 ? NULL | |
| 119 : GLContext::GetCurrent()->GetGLStateRestorer()), | |
| 120 buffer_(0), | |
| 97 enabled_(GL_FALSE), | 121 enabled_(GL_FALSE), |
| 98 index_(index), | 122 index_(index), |
| 99 size_(-1), | 123 size_(-1), |
| 100 type_(-1), | 124 type_(-1), |
| 101 normalized_(GL_FALSE), | 125 normalized_(GL_FALSE), |
| 102 stride_(0), | 126 stride_(0), |
| 103 pointer_(0) { | 127 pointer_(0) { |
| 104 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &buffer_); | 128 if (!state_restorer_) { |
| 105 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled_); | 129 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &buffer_); |
| 106 glEnableVertexAttribArray(index); | 130 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled_); |
| 131 glEnableVertexAttribArray(index); | |
| 107 | 132 |
| 108 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_SIZE, &size_); | 133 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_SIZE, &size_); |
| 109 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_TYPE, &type_); | 134 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_TYPE, &type_); |
| 110 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &normalized_); | 135 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &normalized_); |
| 111 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &stride_); | 136 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &stride_); |
| 112 glGetVertexAttribPointerv(index, GL_VERTEX_ATTRIB_ARRAY_POINTER, &pointer_); | 137 glGetVertexAttribPointerv(index, GL_VERTEX_ATTRIB_ARRAY_POINTER, &pointer_); |
| 138 } | |
| 113 | 139 |
| 114 glVertexAttribPointer(index, size, type, normalized, stride, pointer); | 140 glVertexAttribPointer(index, size, type, normalized, stride, pointer); |
| 115 } | 141 } |
| 116 | 142 |
| 117 ScopedVertexAttribArray::~ScopedVertexAttribArray() { | 143 ScopedVertexAttribArray::~ScopedVertexAttribArray() { |
| 118 ScopedBufferBinder buffer_binder(GL_ARRAY_BUFFER, buffer_); | 144 if (state_restorer_) { |
| 119 glVertexAttribPointer(index_, size_, type_, normalized_, stride_, pointer_); | 145 DCHECK(!!GLContext::GetCurrent()); |
| 120 if (enabled_ == GL_FALSE) { | 146 DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer()); |
| 121 glDisableVertexAttribArray(index_); | 147 state_restorer_->RestoreVertexAttribArray(index_); |
|
piman
2017/03/22 21:28:26
RestoreVertexAttribArray has a side effect of bind
Chandan
2017/03/23 09:19:39
Thanks for pointing this out. Made necessary chang
| |
| 148 } else { | |
| 149 ScopedBufferBinder buffer_binder(GL_ARRAY_BUFFER, buffer_); | |
| 150 glVertexAttribPointer(index_, size_, type_, normalized_, stride_, pointer_); | |
| 151 if (enabled_ == GL_FALSE) | |
| 152 glDisableVertexAttribArray(index_); | |
| 122 } | 153 } |
| 123 } | 154 } |
| 124 | 155 |
| 125 ScopedBufferBinder::ScopedBufferBinder(unsigned int target, unsigned int id) | 156 ScopedBufferBinder::ScopedBufferBinder(unsigned int target, unsigned int id) |
| 126 : target_(target), old_id_(-1) { | 157 : state_restorer_(!GLContext::GetCurrent() |
| 127 GLenum target_getter = 0; | 158 ? NULL |
| 128 switch (target) { | 159 : GLContext::GetCurrent()->GetGLStateRestorer()), |
| 129 case GL_ARRAY_BUFFER: | 160 target_(target), |
| 130 target_getter = GL_ARRAY_BUFFER_BINDING; | 161 old_id_(-1) { |
| 131 break; | 162 if (!state_restorer_) { |
| 132 default: | 163 GLenum target_getter = 0; |
| 133 NOTIMPLEMENTED() << " Target not supported."; | 164 switch (target) { |
| 165 case GL_ARRAY_BUFFER: | |
| 166 target_getter = GL_ARRAY_BUFFER_BINDING; | |
| 167 break; | |
| 168 default: | |
| 169 NOTIMPLEMENTED() << " Target not supported."; | |
| 170 } | |
| 171 glGetIntegerv(target_getter, &old_id_); | |
| 134 } | 172 } |
| 135 glGetIntegerv(target_getter, &old_id_); | |
| 136 glBindBuffer(target_, id); | 173 glBindBuffer(target_, id); |
| 137 } | 174 } |
| 138 | 175 |
| 139 ScopedBufferBinder::~ScopedBufferBinder() { | 176 ScopedBufferBinder::~ScopedBufferBinder() { |
| 140 glBindBuffer(target_, old_id_); | 177 if (state_restorer_) { |
| 178 DCHECK(!!GLContext::GetCurrent()); | |
| 179 DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer()); | |
| 180 state_restorer_->RestoreBufferBindings(); | |
| 181 } else { | |
| 182 glBindBuffer(target_, old_id_); | |
| 183 } | |
| 141 } | 184 } |
| 142 | 185 |
| 143 ScopedViewport::ScopedViewport(int x, int y, int width, int height) { | 186 ScopedViewport::ScopedViewport(int x, int y, int width, int height) { |
| 144 glGetIntegerv(GL_VIEWPORT, data_); | 187 glGetIntegerv(GL_VIEWPORT, data_); |
| 145 glViewport(x, y, width, height); | 188 glViewport(x, y, width, height); |
| 146 } | 189 } |
| 147 | 190 |
| 148 ScopedViewport::~ScopedViewport() { | 191 ScopedViewport::~ScopedViewport() { |
| 149 glViewport(data_[0], data_[1], data_[2], data_[3]); | 192 glViewport(data_[0], data_[1], data_[2], data_[3]); |
| 150 } | 193 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 170 | 213 |
| 171 ScopedCapability::~ScopedCapability() { | 214 ScopedCapability::~ScopedCapability() { |
| 172 if (enabled_ == GL_TRUE) { | 215 if (enabled_ == GL_TRUE) { |
| 173 glEnable(capability_); | 216 glEnable(capability_); |
| 174 } else { | 217 } else { |
| 175 glDisable(capability_); | 218 glDisable(capability_); |
| 176 } | 219 } |
| 177 } | 220 } |
| 178 | 221 |
| 179 } // namespace gl | 222 } // namespace gl |
| OLD | NEW |