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 : buffer_(0), |
| 97 enabled_(GL_FALSE), |
| 98 index_(index), |
| 99 size_(-1), |
| 100 type_(-1), |
| 101 normalized_(GL_FALSE), |
| 102 stride_(0), |
| 103 pointer_(0) { |
| 104 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &buffer_); |
| 105 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled_); |
| 106 glEnableVertexAttribArray(index); |
| 107 |
| 108 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_SIZE, &size_); |
| 109 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_TYPE, &type_); |
| 110 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &normalized_); |
| 111 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &stride_); |
| 112 glGetVertexAttribPointerv(index, GL_VERTEX_ATTRIB_ARRAY_POINTER, &pointer_); |
| 113 |
| 114 glVertexAttribPointer(index, size, type, normalized, stride, pointer); |
| 115 } |
| 116 |
| 117 ScopedVertexAttribArray::~ScopedVertexAttribArray() { |
| 118 ScopedBufferBinder buffer_binder(GL_ARRAY_BUFFER, buffer_); |
| 119 glVertexAttribPointer(index_, size_, type_, normalized_, stride_, pointer_); |
| 120 if (enabled_ == GL_FALSE) { |
| 121 glDisableVertexAttribArray(index_); |
| 122 } |
| 123 } |
| 124 |
| 125 ScopedBufferBinder::ScopedBufferBinder(unsigned int target, unsigned int id) |
| 126 : target_(target), old_id_(-1) { |
| 127 GLenum target_getter = 0; |
| 128 switch (target) { |
| 129 case GL_ARRAY_BUFFER: |
| 130 target_getter = GL_ARRAY_BUFFER_BINDING; |
| 131 break; |
| 132 default: |
| 133 NOTIMPLEMENTED() << " Target not supported."; |
| 134 } |
| 135 glGetIntegerv(target_getter, &old_id_); |
| 136 glBindBuffer(target_, id); |
| 137 } |
| 138 |
| 139 ScopedBufferBinder::~ScopedBufferBinder() { |
| 140 glBindBuffer(target_, old_id_); |
| 141 } |
| 142 |
| 143 ScopedViewport::ScopedViewport(int x, int y, int width, int height) { |
| 144 glGetIntegerv(GL_VIEWPORT, data_); |
| 145 glViewport(x, y, width, height); |
| 146 } |
| 147 |
| 148 ScopedViewport::~ScopedViewport() { |
| 149 glViewport(data_[0], data_[1], data_[2], data_[3]); |
| 150 } |
| 151 |
| 152 ScopedColorMask::ScopedColorMask(char red, char green, char blue, char alpha) { |
| 153 glGetBooleanv(GL_COLOR_WRITEMASK, colors_); |
| 154 glColorMask(red, green, blue, alpha); |
| 155 } |
| 156 |
| 157 ScopedColorMask::~ScopedColorMask() { |
| 158 glColorMask(colors_[0], colors_[1], colors_[2], colors_[3]); |
| 159 } |
| 160 |
| 161 ScopedCapability::ScopedCapability(unsigned capability, unsigned char enabled) |
| 162 : capability_(capability) { |
| 163 enabled_ = glIsEnabled(capability_); |
| 164 if (enabled == GL_TRUE) { |
| 165 glEnable(capability); |
| 166 } else { |
| 167 glDisable(capability); |
| 168 } |
| 169 } |
| 170 |
| 171 ScopedCapability::~ScopedCapability() { |
| 172 if (enabled_ == GL_TRUE) { |
| 173 glEnable(capability_); |
| 174 } else { |
| 175 glDisable(capability_); |
| 176 } |
| 177 } |
| 178 |
68 } // namespace gfx | 179 } // namespace gfx |
OLD | NEW |