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
3 // found in the LICENSE file.
5 #include "ui/gl/scoped_binders.h"
6 #include "ui/gl/gl_bindings.h"
7 #include "ui/gl/gl_context.h"
8 #include "ui/gl/gl_state_restorer.h"
12 ScopedFrameBufferBinder::ScopedFrameBufferBinder(unsigned int fbo
)
13 : state_restorer_(!GLContext::GetCurrent()
15 : GLContext::GetCurrent()->GetGLStateRestorer()),
18 glGetIntegerv(GL_FRAMEBUFFER_BINDING
, &old_fbo_
);
19 glBindFramebufferEXT(GL_FRAMEBUFFER
, fbo
);
22 ScopedFrameBufferBinder::~ScopedFrameBufferBinder() {
23 if (state_restorer_
) {
24 DCHECK(!!GLContext::GetCurrent());
25 DCHECK_EQ(state_restorer_
, GLContext::GetCurrent()->GetGLStateRestorer());
26 state_restorer_
->RestoreFramebufferBindings();
28 glBindFramebufferEXT(GL_FRAMEBUFFER
, old_fbo_
);
32 ScopedTextureBinder::ScopedTextureBinder(unsigned int target
, unsigned int id
)
33 : state_restorer_(!GLContext::GetCurrent()
35 : GLContext::GetCurrent()->GetGLStateRestorer()),
38 if (!state_restorer_
) {
39 GLenum target_getter
= 0;
42 target_getter
= GL_TEXTURE_BINDING_2D
;
44 case GL_TEXTURE_CUBE_MAP
:
45 target_getter
= GL_TEXTURE_BINDING_CUBE_MAP
;
47 case GL_TEXTURE_EXTERNAL_OES
:
48 target_getter
= GL_TEXTURE_BINDING_EXTERNAL_OES
;
51 NOTIMPLEMENTED() << "Target not part of OpenGL ES 2.0 spec.";
53 glGetIntegerv(target_getter
, &old_id_
);
55 glBindTexture(target_
, id
);
58 ScopedTextureBinder::~ScopedTextureBinder() {
59 if (state_restorer_
) {
60 DCHECK(!!GLContext::GetCurrent());
61 DCHECK_EQ(state_restorer_
, GLContext::GetCurrent()->GetGLStateRestorer());
62 state_restorer_
->RestoreAllTextureUnitBindings();
64 glBindTexture(target_
, old_id_
);