scoped_binders.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifndef UI_GL_SCOPED_BINDERS_H_
  5. #define UI_GL_SCOPED_BINDERS_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "ui/gl/gl_export.h"
  8. namespace gl {
  9. class GLStateRestorer;
  10. class GL_EXPORT ScopedFramebufferBinder {
  11. public:
  12. explicit ScopedFramebufferBinder(unsigned int fbo);
  13. ScopedFramebufferBinder(const ScopedFramebufferBinder&) = delete;
  14. ScopedFramebufferBinder& operator=(const ScopedFramebufferBinder&) = delete;
  15. ~ScopedFramebufferBinder();
  16. private:
  17. // Whenever possible we prefer to use the current GLContext's
  18. // GLStateRestorer to maximize driver compabitility.
  19. raw_ptr<GLStateRestorer> state_restorer_;
  20. // Failing that we use GL calls to save and restore state.
  21. int old_fbo_;
  22. };
  23. class GL_EXPORT ScopedActiveTexture {
  24. public:
  25. ScopedActiveTexture(unsigned int texture);
  26. ScopedActiveTexture(const ScopedActiveTexture&) = delete;
  27. ScopedActiveTexture& operator=(const ScopedActiveTexture&) = delete;
  28. ~ScopedActiveTexture();
  29. private:
  30. // Whenever possible we prefer to use the current GLContext's
  31. // GLStateRestorer to maximize driver compabitility.
  32. raw_ptr<GLStateRestorer> state_restorer_;
  33. // Failing that we use GL calls to save and restore state.
  34. int old_texture_;
  35. };
  36. class GL_EXPORT ScopedTextureBinder {
  37. public:
  38. ScopedTextureBinder(unsigned int target, unsigned int id);
  39. ScopedTextureBinder(const ScopedTextureBinder&) = delete;
  40. ScopedTextureBinder& operator=(const ScopedTextureBinder&) = delete;
  41. ~ScopedTextureBinder();
  42. private:
  43. // Whenever possible we prefer to use the current GLContext's
  44. // GLStateRestorer to maximize driver compabitility.
  45. raw_ptr<GLStateRestorer> state_restorer_;
  46. // Failing that we use GL calls to save and restore state.
  47. int target_;
  48. int old_id_;
  49. };
  50. class GL_EXPORT ScopedUseProgram {
  51. public:
  52. ScopedUseProgram(unsigned int program);
  53. ScopedUseProgram(const ScopedUseProgram&) = delete;
  54. ScopedUseProgram& operator=(const ScopedUseProgram&) = delete;
  55. ~ScopedUseProgram();
  56. private:
  57. // Whenever possible we prefer to use the current GLContext's
  58. // GLStateRestorer to maximize driver compabitility.
  59. raw_ptr<GLStateRestorer> state_restorer_;
  60. // Failing that we use GL calls to save and restore state.
  61. int old_program_;
  62. };
  63. class GL_EXPORT ScopedVertexAttribArray {
  64. public:
  65. ScopedVertexAttribArray(unsigned int index,
  66. int size,
  67. unsigned int type,
  68. char normalized,
  69. int stride,
  70. const void* pointer);
  71. ScopedVertexAttribArray(const ScopedVertexAttribArray&) = delete;
  72. ScopedVertexAttribArray& operator=(const ScopedVertexAttribArray&) = delete;
  73. ~ScopedVertexAttribArray();
  74. private:
  75. // Whenever possible we prefer to use the current GLContext's
  76. // GLStateRestorer to maximize driver compabitility.
  77. raw_ptr<GLStateRestorer> state_restorer_;
  78. // Failing that we use GL calls to save and restore state.
  79. int buffer_;
  80. int enabled_;
  81. int index_;
  82. int size_;
  83. int type_;
  84. int normalized_;
  85. int stride_;
  86. void* pointer_;
  87. };
  88. class GL_EXPORT ScopedBufferBinder {
  89. public:
  90. ScopedBufferBinder(unsigned int target, unsigned int index);
  91. ScopedBufferBinder(const ScopedBufferBinder&) = delete;
  92. ScopedBufferBinder& operator=(const ScopedBufferBinder&) = delete;
  93. ~ScopedBufferBinder();
  94. private:
  95. // Whenever possible we prefer to use the current GLContext's
  96. // GLStateRestorer to maximize driver compabitility.
  97. raw_ptr<GLStateRestorer> state_restorer_;
  98. // Failing that we use GL calls to save and restore state.
  99. int target_;
  100. int old_id_;
  101. };
  102. class GL_EXPORT ScopedViewport {
  103. public:
  104. ScopedViewport(int x, int y, int width, int height);
  105. ScopedViewport(const ScopedViewport&) = delete;
  106. ScopedViewport& operator=(const ScopedViewport&) = delete;
  107. ~ScopedViewport();
  108. private:
  109. int data_[4] = {};
  110. };
  111. class GL_EXPORT ScopedVertexAttribPointer {
  112. public:
  113. ScopedVertexAttribPointer(unsigned index,
  114. int size,
  115. unsigned type,
  116. char normalized,
  117. int stride,
  118. const void* pointer);
  119. ScopedVertexAttribPointer(const ScopedVertexAttribPointer&) = delete;
  120. ScopedVertexAttribPointer& operator=(const ScopedVertexAttribPointer&) =
  121. delete;
  122. ~ScopedVertexAttribPointer();
  123. };
  124. class GL_EXPORT ScopedColorMask {
  125. public:
  126. ScopedColorMask(char red, char green, char blue, char alpha);
  127. ScopedColorMask(const ScopedColorMask&) = delete;
  128. ScopedColorMask& operator=(const ScopedColorMask&) = delete;
  129. ~ScopedColorMask();
  130. private:
  131. unsigned char colors_[4] = {};
  132. };
  133. class GL_EXPORT ScopedCapability {
  134. public:
  135. ScopedCapability(unsigned capability, unsigned char enabled);
  136. ScopedCapability(const ScopedCapability&) = delete;
  137. ScopedCapability& operator=(const ScopedCapability&) = delete;
  138. ~ScopedCapability();
  139. private:
  140. unsigned capability_;
  141. unsigned char enabled_;
  142. };
  143. } // namespace gl
  144. #endif // UI_GL_SCOPED_BINDERS_H_