thread_state.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (c) 2016 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 GPU_GLES2_CONFORM_SUPPORT_EGL_THREAD_STATE_H_
  5. #define GPU_GLES2_CONFORM_SUPPORT_EGL_THREAD_STATE_H_
  6. #include <EGL/egl.h>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/ref_counted.h"
  9. namespace gles2_conform_support {
  10. namespace egl {
  11. class Context;
  12. class Display;
  13. class Surface;
  14. // Thread-local API state of EGL.
  15. class ThreadState {
  16. public:
  17. // Factory getter for the class. Should only be called by the API layer, and
  18. // then passed through Display in order to avoid lock issues.
  19. static ThreadState* Get();
  20. ThreadState(const ThreadState&) = delete;
  21. ThreadState& operator=(const ThreadState&) = delete;
  22. static void ReleaseThread();
  23. Surface* current_surface() const { return current_surface_.get(); }
  24. Context* current_context() const { return current_context_.get(); }
  25. template <typename T>
  26. T ReturnError(EGLint error, T return_value) {
  27. error_code_ = error;
  28. return return_value;
  29. }
  30. template <typename T>
  31. T ReturnSuccess(T return_value) {
  32. error_code_ = EGL_SUCCESS;
  33. return return_value;
  34. }
  35. EGLint ConsumeErrorCode();
  36. Display* GetDefaultDisplay();
  37. Display* GetDisplay(EGLDisplay);
  38. // RAII class for ensuring that ThreadState current context
  39. // is reflected in the gfx:: and gles:: global variables.
  40. class AutoCurrentContextRestore {
  41. public:
  42. AutoCurrentContextRestore(ThreadState*);
  43. AutoCurrentContextRestore(const AutoCurrentContextRestore&) = delete;
  44. AutoCurrentContextRestore& operator=(const AutoCurrentContextRestore&) =
  45. delete;
  46. ~AutoCurrentContextRestore();
  47. void SetCurrent(Surface*, Context*);
  48. private:
  49. raw_ptr<ThreadState> thread_state_;
  50. };
  51. private:
  52. ThreadState();
  53. ~ThreadState();
  54. void SetCurrent(Surface*, Context*);
  55. EGLint error_code_;
  56. scoped_refptr<Surface> current_surface_;
  57. scoped_refptr<Context> current_context_;
  58. };
  59. } // namespace egl
  60. } // namespace gles2_conform_support
  61. #endif // GPU_GLES2_CONFORM_SUPPORT_EGL_THREAD_STATE_H_