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 gfx { | 10 namespace gfx { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 ScopedFrameBufferBinder::~ScopedFrameBufferBinder() { | 22 ScopedFrameBufferBinder::~ScopedFrameBufferBinder() { |
| 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) | |
| 33 : old_texture_(-1) { | |
| 34 glGetIntegerv(GL_ACTIVE_TEXTURE, &old_texture_); | |
| 35 glActiveTexture(texture); | |
| 36 } | |
| 37 | |
| 38 ScopedActiveTexture::~ScopedActiveTexture() { | |
| 39 glActiveTexture(old_texture_); | |
| 40 } | |
| 41 | |
| 32 ScopedTextureBinder::ScopedTextureBinder(unsigned int target, unsigned int id) | 42 ScopedTextureBinder::ScopedTextureBinder(unsigned int target, unsigned int id) |
| 33 : state_restorer_(!GLContext::GetCurrent() | 43 : state_restorer_(!GLContext::GetCurrent() |
| 34 ? NULL | 44 ? NULL |
| 35 : GLContext::GetCurrent()->GetGLStateRestorer()), | 45 : GLContext::GetCurrent()->GetGLStateRestorer()), |
| 36 target_(target), | 46 target_(target), |
| 37 old_id_(-1) { | 47 old_id_(-1) { |
| 38 if (!state_restorer_) { | 48 if (!state_restorer_) { |
| 39 GLenum target_getter = 0; | 49 GLenum target_getter = 0; |
| 40 switch (target) { | 50 switch (target) { |
| 41 case GL_TEXTURE_2D: | 51 case GL_TEXTURE_2D: |
| 42 target_getter = GL_TEXTURE_BINDING_2D; | 52 target_getter = GL_TEXTURE_BINDING_2D; |
| 43 break; | 53 break; |
| 44 case GL_TEXTURE_CUBE_MAP: | 54 case GL_TEXTURE_CUBE_MAP: |
| 45 target_getter = GL_TEXTURE_BINDING_CUBE_MAP; | 55 target_getter = GL_TEXTURE_BINDING_CUBE_MAP; |
| 46 break; | 56 break; |
| 47 case GL_TEXTURE_EXTERNAL_OES: | 57 case GL_TEXTURE_EXTERNAL_OES: |
| 48 target_getter = GL_TEXTURE_BINDING_EXTERNAL_OES; | 58 target_getter = GL_TEXTURE_BINDING_EXTERNAL_OES; |
| 49 break; | 59 break; |
| 60 case GL_TEXTURE_RECTANGLE_ARB: | |
| 61 target_getter = GL_TEXTURE_BINDING_RECTANGLE_ARB; | |
| 62 break; | |
| 50 default: | 63 default: |
| 51 NOTIMPLEMENTED() << "Target not part of OpenGL ES 2.0 spec."; | 64 NOTIMPLEMENTED() << " Target not supported."; |
| 52 } | 65 } |
| 53 glGetIntegerv(target_getter, &old_id_); | 66 glGetIntegerv(target_getter, &old_id_); |
| 54 } | 67 } |
| 55 glBindTexture(target_, id); | 68 glBindTexture(target_, id); |
| 56 } | 69 } |
| 57 | 70 |
| 58 ScopedTextureBinder::~ScopedTextureBinder() { | 71 ScopedTextureBinder::~ScopedTextureBinder() { |
| 59 if (state_restorer_) { | 72 if (state_restorer_) { |
| 60 DCHECK(!!GLContext::GetCurrent()); | 73 DCHECK(!!GLContext::GetCurrent()); |
| 61 DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer()); | 74 DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer()); |
| 62 state_restorer_->RestoreActiveTextureUnitBinding(target_); | 75 state_restorer_->RestoreActiveTextureUnitBinding(target_); |
| 63 } else { | 76 } else { |
| 64 glBindTexture(target_, old_id_); | 77 glBindTexture(target_, old_id_); |
| 65 } | 78 } |
| 66 } | 79 } |
| 67 | 80 |
| 81 ScopedUseProgram::ScopedUseProgram(unsigned int program) : old_program_(-1) { | |
| 82 glGetIntegerv(GL_CURRENT_PROGRAM, &old_program_); | |
| 83 glUseProgram(program); | |
| 84 } | |
| 85 | |
| 86 ScopedUseProgram::~ScopedUseProgram() { | |
| 87 glUseProgram(old_program_); | |
| 88 } | |
| 89 | |
| 90 ScopedVertexAttribArray::ScopedVertexAttribArray(unsigned int index, | |
| 91 int size, | |
| 92 unsigned int type, | |
| 93 char normalized, | |
| 94 int stride, | |
| 95 const void* pointer) | |
| 96 : enabled_(GL_FALSE), | |
| 97 index_(index), | |
| 98 size_(-1), | |
| 99 type_(-1), | |
| 100 normalized_(GL_FALSE), | |
| 101 stride_(0), | |
| 102 pointer_(0) { | |
| 103 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled_); | |
| 104 glEnableVertexAttribArray(index); | |
| 105 | |
| 106 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_SIZE, &size_); | |
| 107 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_TYPE, &type_); | |
| 108 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &normalized_); | |
| 109 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &stride_); | |
| 110 glGetVertexAttribPointerv(index, GL_VERTEX_ATTRIB_ARRAY_POINTER, &pointer_); | |
| 111 | |
| 112 glVertexAttribPointer(index, size, type, normalized, stride, pointer); | |
| 113 } | |
| 114 | |
| 115 ScopedVertexAttribArray::~ScopedVertexAttribArray() { | |
| 116 glVertexAttribPointer(index_, size_, type_, normalized_, stride_, pointer_); | |
|
piman
2015/11/04 22:49:45
So this one is a little tricky, because there's an
Daniele Castagna
2015/12/03 23:12:08
Done.
| |
| 117 if (enabled_ == GL_FALSE) { | |
| 118 glDisableVertexAttribArray(index_); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 ScopedBufferBinder::ScopedBufferBinder(unsigned int target, unsigned int id) | |
| 123 : target_(target), old_id_(-1) { | |
| 124 GLenum target_getter = 0; | |
| 125 switch (target) { | |
| 126 case GL_ARRAY_BUFFER: | |
| 127 target_getter = GL_ARRAY_BUFFER_BINDING; | |
| 128 break; | |
| 129 default: | |
| 130 NOTIMPLEMENTED() << " Target not supported."; | |
| 131 } | |
| 132 glGetIntegerv(target_getter, &old_id_); | |
| 133 glBindBuffer(target_, id); | |
| 134 } | |
| 135 | |
| 136 ScopedBufferBinder::~ScopedBufferBinder() { | |
| 137 glBindBuffer(target_, old_id_); | |
| 138 } | |
| 139 | |
| 140 ScopedViewport::ScopedViewport(int x, int y, int width, int height) { | |
| 141 glGetIntegerv(GL_VIEWPORT, data_); | |
| 142 glViewport(x, y, width, height); | |
| 143 } | |
| 144 | |
| 145 ScopedViewport::~ScopedViewport() { | |
| 146 glViewport(data_[0], data_[1], data_[2], data_[3]); | |
| 147 } | |
| 148 | |
| 149 ScopedColorMask::ScopedColorMask(char red, char green, char blue, char alpha) { | |
| 150 glGetBooleanv(GL_COLOR_WRITEMASK, colors_); | |
| 151 glColorMask(red, green, blue, alpha); | |
| 152 } | |
| 153 | |
| 154 ScopedColorMask::~ScopedColorMask() { | |
| 155 glColorMask(colors_[0], colors_[1], colors_[2], colors_[3]); | |
| 156 } | |
| 157 | |
| 158 // static | |
| 159 GLenum ScopedCapabilitiesRestorer::kCapabilities[] = { | |
| 160 GL_BLEND, GL_CULL_FACE, GL_DEPTH_TEST, | |
| 161 GL_DITHER, GL_POLYGON_OFFSET_FILL, GL_SAMPLE_ALPHA_TO_COVERAGE, | |
|
piman
2015/11/04 22:49:45
- polygon offset shouldn't matter since you disabl
Daniele Castagna
2015/12/03 23:12:08
Ack.
This ScopedCapabilitiesRestorer could be used
piman
2015/12/04 00:02:16
Sure, but why these ones and not others too? E.g.
Daniele Castagna
2015/12/04 00:40:18
Not sure why I left GL_SAMPLE_COVERAGE out. My int
| |
| 162 GL_SCISSOR_TEST, GL_STENCIL_TEST}; | |
| 163 | |
| 164 ScopedCapabilitiesRestorer::ScopedCapabilitiesRestorer() { | |
| 165 static_assert(arraysize(kCapabilities) <= arraysize(enabled_), | |
| 166 "enabled_ array size should be at least kCapabilities size. "); | |
| 167 for (size_t i = 0; i < arraysize(kCapabilities); ++i) { | |
| 168 enabled_[i] = glIsEnabled(kCapabilities[i]); | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 ScopedCapabilitiesRestorer::~ScopedCapabilitiesRestorer() { | |
| 173 for (size_t i = 0; i < arraysize(kCapabilities); ++i) { | |
| 174 if (enabled_[i] == GL_TRUE) { | |
| 175 glEnable(kCapabilities[i]); | |
| 176 } else { | |
| 177 glDisable(kCapabilities[i]); | |
| 178 } | |
| 179 } | |
| 180 } | |
| 181 | |
| 68 } // namespace gfx | 182 } // namespace gfx |
| OLD | NEW |