scoped_cgl.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2014 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_CGL_H_
  5. #define UI_GL_SCOPED_CGL_H_
  6. #include <OpenGL/OpenGL.h>
  7. #include "base/mac/scoped_typeref.h"
  8. #include "ui/gl/gl_export.h"
  9. namespace base {
  10. template<>
  11. struct ScopedTypeRefTraits<CGLContextObj> {
  12. static CGLContextObj InvalidValue() { return nullptr; }
  13. static CGLContextObj Retain(CGLContextObj object) {
  14. return CGLRetainContext(object);
  15. }
  16. static void Release(CGLContextObj object) {
  17. CGLReleaseContext(object);
  18. }
  19. };
  20. template<>
  21. struct ScopedTypeRefTraits<CGLPixelFormatObj> {
  22. static CGLPixelFormatObj InvalidValue() { return nullptr; }
  23. static CGLPixelFormatObj Retain(CGLPixelFormatObj object) {
  24. return CGLRetainPixelFormat(object);
  25. }
  26. static void Release(CGLPixelFormatObj object) {
  27. CGLReleasePixelFormat(object);
  28. }
  29. };
  30. } // namespace base
  31. namespace gl {
  32. class GL_EXPORT ScopedCGLSetCurrentContext {
  33. public:
  34. explicit ScopedCGLSetCurrentContext(CGLContextObj context);
  35. ScopedCGLSetCurrentContext(const ScopedCGLSetCurrentContext&) = delete;
  36. ScopedCGLSetCurrentContext& operator=(const ScopedCGLSetCurrentContext&) =
  37. delete;
  38. ~ScopedCGLSetCurrentContext();
  39. private:
  40. // Note that if a context is destroyed when it is current, then the current
  41. // context is changed to NULL. Take out a reference on |previous_context_| to
  42. // preserve this behavior (when this falls out of scope, |previous_context_|
  43. // will be made current, then released, so NULL will be current if that
  44. // release destroys the context).
  45. base::ScopedTypeRef<CGLContextObj> previous_context_;
  46. };
  47. } // namespace gl
  48. #endif // UI_GL_SCOPED_CGL_H_