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 ScopedEnableVertexAttribArray::ScopedEnableVertexAttribArray(unsigned int index) |
| 91 : enabled_(GL_FALSE), index_(index) { |
| 92 glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled_); |
| 93 glEnableVertexAttribArray(index); |
| 94 } |
| 95 |
| 96 ScopedEnableVertexAttribArray::~ScopedEnableVertexAttribArray() { |
| 97 if (enabled_ == GL_FALSE) { |
| 98 glDisableVertexAttribArray(index_); |
| 99 } |
| 100 } |
| 101 |
| 102 ScopedBufferBinder::ScopedBufferBinder(unsigned int target, unsigned int id) |
| 103 : target_(target), old_id_(-1) { |
| 104 GLenum target_getter = 0; |
| 105 switch (target) { |
| 106 case GL_ARRAY_BUFFER: |
| 107 target_getter = GL_ARRAY_BUFFER_BINDING; |
| 108 break; |
| 109 default: |
| 110 NOTIMPLEMENTED() << " Target not supported."; |
| 111 } |
| 112 glGetIntegerv(target_getter, &old_id_); |
| 113 glBindBuffer(target_, id); |
| 114 } |
| 115 |
| 116 ScopedBufferBinder::~ScopedBufferBinder() { |
| 117 glBindBuffer(target_, old_id_); |
| 118 } |
| 119 |
| 120 ScopedViewport::ScopedViewport(int x, int y, int width, int height) { |
| 121 glGetIntegerv(GL_VIEWPORT, data_); |
| 122 glViewport(x, y, width, height); |
| 123 } |
| 124 |
| 125 ScopedViewport::~ScopedViewport() { |
| 126 glViewport(data_[0], data_[1], data_[2], data_[3]); |
| 127 } |
| 128 |
68 } // namespace gfx | 129 } // namespace gfx |
OLD | NEW |