scoped_binders.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
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.
4
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"
9
10namespace gfx {
11
12ScopedFrameBufferBinder::ScopedFrameBufferBinder(unsigned int fbo)
13    : state_restorer_(!GLContext::GetCurrent()
14                          ? NULL
15                          : GLContext::GetCurrent()->GetGLStateRestorer()),
16      old_fbo_(-1) {
17  if (!state_restorer_)
18    glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_fbo_);
19  glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
20}
21
22ScopedFrameBufferBinder::~ScopedFrameBufferBinder() {
23  if (state_restorer_) {
24    DCHECK(!!GLContext::GetCurrent());
25    DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer());
26    state_restorer_->RestoreFramebufferBindings();
27  } else {
28    glBindFramebufferEXT(GL_FRAMEBUFFER, old_fbo_);
29  }
30}
31
32ScopedTextureBinder::ScopedTextureBinder(unsigned int target, unsigned int id)
33    : state_restorer_(!GLContext::GetCurrent()
34                          ? NULL
35                          : GLContext::GetCurrent()->GetGLStateRestorer()),
36      target_(target),
37      old_id_(-1) {
38  if (!state_restorer_) {
39    GLenum target_getter = 0;
40    switch (target) {
41      case GL_TEXTURE_2D:
42        target_getter = GL_TEXTURE_BINDING_2D;
43        break;
44      case GL_TEXTURE_CUBE_MAP:
45        target_getter = GL_TEXTURE_BINDING_CUBE_MAP;
46        break;
47      case GL_TEXTURE_EXTERNAL_OES:
48        target_getter = GL_TEXTURE_BINDING_EXTERNAL_OES;
49        break;
50      default:
51        NOTIMPLEMENTED() << "Target not part of OpenGL ES 2.0 spec.";
52    }
53    glGetIntegerv(target_getter, &old_id_);
54  }
55  glBindTexture(target_, id);
56}
57
58ScopedTextureBinder::~ScopedTextureBinder() {
59  if (state_restorer_) {
60    DCHECK(!!GLContext::GetCurrent());
61    DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer());
62    state_restorer_->RestoreActiveTextureUnitBinding(target_);
63  } else {
64    glBindTexture(target_, old_id_);
65  }
66}
67
68}  // namespace gfx
69