scoped_binders.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include "ui/gl/scoped_binders.h"
  5. #include "ui/gl/gl_bindings.h"
  6. #include "ui/gl/gl_context.h"
  7. #include "ui/gl/gl_state_restorer.h"
  8. namespace gl {
  9. ScopedFramebufferBinder::ScopedFramebufferBinder(unsigned int fbo)
  10. : state_restorer_(!GLContext::GetCurrent()
  11. ? nullptr
  12. : GLContext::GetCurrent()->GetGLStateRestorer()),
  13. old_fbo_(-1) {
  14. if (!state_restorer_)
  15. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_fbo_);
  16. glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
  17. }
  18. ScopedFramebufferBinder::~ScopedFramebufferBinder() {
  19. if (state_restorer_) {
  20. DCHECK(!!GLContext::GetCurrent());
  21. DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer());
  22. state_restorer_->RestoreFramebufferBindings();
  23. } else {
  24. glBindFramebufferEXT(GL_FRAMEBUFFER, old_fbo_);
  25. }
  26. }
  27. ScopedActiveTexture::ScopedActiveTexture(unsigned int texture)
  28. : state_restorer_(!GLContext::GetCurrent()
  29. ? nullptr
  30. : GLContext::GetCurrent()->GetGLStateRestorer()),
  31. old_texture_(-1) {
  32. if (!state_restorer_)
  33. glGetIntegerv(GL_ACTIVE_TEXTURE, &old_texture_);
  34. glActiveTexture(texture);
  35. }
  36. ScopedActiveTexture::~ScopedActiveTexture() {
  37. if (state_restorer_) {
  38. DCHECK(!!GLContext::GetCurrent());
  39. DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer());
  40. state_restorer_->RestoreActiveTexture();
  41. } else {
  42. glActiveTexture(old_texture_);
  43. }
  44. }
  45. ScopedTextureBinder::ScopedTextureBinder(unsigned int target, unsigned int id)
  46. : state_restorer_(!GLContext::GetCurrent()
  47. ? nullptr
  48. : GLContext::GetCurrent()->GetGLStateRestorer()),
  49. target_(target),
  50. old_id_(-1) {
  51. if (!state_restorer_) {
  52. GLenum target_getter = 0;
  53. switch (target) {
  54. case GL_TEXTURE_2D:
  55. target_getter = GL_TEXTURE_BINDING_2D;
  56. break;
  57. case GL_TEXTURE_CUBE_MAP:
  58. target_getter = GL_TEXTURE_BINDING_CUBE_MAP;
  59. break;
  60. case GL_TEXTURE_EXTERNAL_OES:
  61. target_getter = GL_TEXTURE_BINDING_EXTERNAL_OES;
  62. break;
  63. case GL_TEXTURE_RECTANGLE_ARB:
  64. target_getter = GL_TEXTURE_BINDING_RECTANGLE_ARB;
  65. break;
  66. default:
  67. NOTIMPLEMENTED() << " Target not supported.";
  68. }
  69. glGetIntegerv(target_getter, &old_id_);
  70. }
  71. glBindTexture(target_, id);
  72. }
  73. ScopedTextureBinder::~ScopedTextureBinder() {
  74. if (state_restorer_) {
  75. DCHECK(!!GLContext::GetCurrent());
  76. DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer());
  77. state_restorer_->RestoreActiveTextureUnitBinding(target_);
  78. } else {
  79. glBindTexture(target_, old_id_);
  80. }
  81. }
  82. ScopedUseProgram::ScopedUseProgram(unsigned int program)
  83. : state_restorer_(!GLContext::GetCurrent()
  84. ? nullptr
  85. : GLContext::GetCurrent()->GetGLStateRestorer()),
  86. old_program_(-1) {
  87. if (!state_restorer_)
  88. glGetIntegerv(GL_CURRENT_PROGRAM, &old_program_);
  89. glUseProgram(program);
  90. }
  91. ScopedUseProgram::~ScopedUseProgram() {
  92. if (state_restorer_) {
  93. DCHECK(!!GLContext::GetCurrent());
  94. DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer());
  95. state_restorer_->RestoreProgramBindings();
  96. } else {
  97. glUseProgram(old_program_);
  98. }
  99. }
  100. ScopedVertexAttribArray::ScopedVertexAttribArray(unsigned int index,
  101. int size,
  102. unsigned int type,
  103. char normalized,
  104. int stride,
  105. const void* pointer)
  106. : state_restorer_(!GLContext::GetCurrent()
  107. ? nullptr
  108. : GLContext::GetCurrent()->GetGLStateRestorer()),
  109. buffer_(0),
  110. enabled_(GL_FALSE),
  111. index_(index),
  112. size_(-1),
  113. type_(-1),
  114. normalized_(GL_FALSE),
  115. stride_(0),
  116. pointer_(nullptr) {
  117. if (!state_restorer_) {
  118. glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &buffer_);
  119. glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled_);
  120. glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_SIZE, &size_);
  121. glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_TYPE, &type_);
  122. glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &normalized_);
  123. glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &stride_);
  124. glGetVertexAttribPointerv(index, GL_VERTEX_ATTRIB_ARRAY_POINTER, &pointer_);
  125. }
  126. glEnableVertexAttribArray(index);
  127. glVertexAttribPointer(index, size, type, normalized, stride, pointer);
  128. }
  129. ScopedVertexAttribArray::~ScopedVertexAttribArray() {
  130. if (state_restorer_) {
  131. DCHECK(!!GLContext::GetCurrent());
  132. DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer());
  133. state_restorer_->RestoreVertexAttribArray(index_);
  134. } else {
  135. ScopedBufferBinder buffer_binder(GL_ARRAY_BUFFER, buffer_);
  136. glVertexAttribPointer(index_, size_, type_, normalized_, stride_, pointer_);
  137. if (enabled_ == GL_FALSE)
  138. glDisableVertexAttribArray(index_);
  139. }
  140. }
  141. ScopedBufferBinder::ScopedBufferBinder(unsigned int target, unsigned int id)
  142. : state_restorer_(!GLContext::GetCurrent()
  143. ? nullptr
  144. : GLContext::GetCurrent()->GetGLStateRestorer()),
  145. target_(target),
  146. old_id_(-1) {
  147. if (!state_restorer_) {
  148. GLenum target_getter = 0;
  149. switch (target) {
  150. case GL_ARRAY_BUFFER:
  151. target_getter = GL_ARRAY_BUFFER_BINDING;
  152. break;
  153. case GL_PIXEL_UNPACK_BUFFER:
  154. target_getter = GL_PIXEL_UNPACK_BUFFER_BINDING;
  155. break;
  156. default:
  157. NOTIMPLEMENTED() << " Target not supported.";
  158. }
  159. glGetIntegerv(target_getter, &old_id_);
  160. }
  161. glBindBuffer(target_, id);
  162. }
  163. ScopedBufferBinder::~ScopedBufferBinder() {
  164. if (state_restorer_) {
  165. DCHECK(!!GLContext::GetCurrent());
  166. DCHECK_EQ(state_restorer_, GLContext::GetCurrent()->GetGLStateRestorer());
  167. state_restorer_->RestoreBufferBinding(target_);
  168. } else {
  169. glBindBuffer(target_, old_id_);
  170. }
  171. }
  172. ScopedViewport::ScopedViewport(int x, int y, int width, int height) {
  173. glGetIntegerv(GL_VIEWPORT, data_);
  174. glViewport(x, y, width, height);
  175. }
  176. ScopedViewport::~ScopedViewport() {
  177. glViewport(data_[0], data_[1], data_[2], data_[3]);
  178. }
  179. ScopedColorMask::ScopedColorMask(char red, char green, char blue, char alpha) {
  180. glGetBooleanv(GL_COLOR_WRITEMASK, colors_);
  181. glColorMask(red, green, blue, alpha);
  182. }
  183. ScopedColorMask::~ScopedColorMask() {
  184. glColorMask(colors_[0], colors_[1], colors_[2], colors_[3]);
  185. }
  186. ScopedCapability::ScopedCapability(unsigned capability, unsigned char enabled)
  187. : capability_(capability) {
  188. enabled_ = glIsEnabled(capability_);
  189. if (enabled == GL_TRUE) {
  190. glEnable(capability);
  191. } else {
  192. glDisable(capability);
  193. }
  194. }
  195. ScopedCapability::~ScopedCapability() {
  196. if (enabled_ == GL_TRUE) {
  197. glEnable(capability_);
  198. } else {
  199. glDisable(capability_);
  200. }
  201. }
  202. } // namespace gl