scoped_make_current.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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_MAKE_CURRENT_H_
  5. #define UI_GL_SCOPED_MAKE_CURRENT_H_
  6. #include "base/memory/ref_counted.h"
  7. #include "ui/gl/gl_export.h"
  8. namespace gl {
  9. class GLContext;
  10. class GLSurface;
  11. }
  12. namespace ui {
  13. // ScopedMakeCurrent makes given context current with the given surface. If this
  14. // fails, Succeeded() returns false. It also restores the previous context on
  15. // destruction and asserts that this succeeds. Users can optionally use
  16. // Restore() to check if restoring the previous context succeeds. This prevents
  17. // the assert during destruction.
  18. class GL_EXPORT ScopedMakeCurrent {
  19. public:
  20. ScopedMakeCurrent(gl::GLContext* context, gl::GLSurface* surface);
  21. ScopedMakeCurrent(const ScopedMakeCurrent&) = delete;
  22. ScopedMakeCurrent& operator=(const ScopedMakeCurrent&) = delete;
  23. ~ScopedMakeCurrent();
  24. // Returns whether the |context_| is current.
  25. bool IsContextCurrent() { return is_context_current_; }
  26. private:
  27. scoped_refptr<gl::GLContext> previous_context_;
  28. scoped_refptr<gl::GLSurface> previous_surface_;
  29. scoped_refptr<gl::GLContext> context_;
  30. scoped_refptr<gl::GLSurface> surface_;
  31. bool is_context_current_ = false;
  32. };
  33. } // namespace ui
  34. #endif // UI_GL_SCOPED_MAKE_CURRENT_H_