gl_surface_presentation_helper.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2018 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_GL_SURFACE_PRESENTATION_HELPER_H_
  5. #define UI_GL_GL_SURFACE_PRESENTATION_HELPER_H_
  6. #include "base/containers/circular_deque.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/time/time.h"
  10. #include "ui/gfx/swap_result.h"
  11. #include "ui/gl/gl_export.h"
  12. #include "ui/gl/gl_surface.h"
  13. namespace gfx {
  14. class VSyncProvider;
  15. }
  16. namespace gl {
  17. class GLContext;
  18. class GLFence;
  19. class GPUTimingClient;
  20. class GPUTimer;
  21. // Helper class for managering and invoke presentation callbacks for GLSurface
  22. // implementations.
  23. class GL_EXPORT GLSurfacePresentationHelper {
  24. public:
  25. class GL_EXPORT ScopedSwapBuffers {
  26. public:
  27. ScopedSwapBuffers(GLSurfacePresentationHelper* helper,
  28. GLSurface::PresentationCallback callback);
  29. ScopedSwapBuffers(GLSurfacePresentationHelper* helper,
  30. GLSurface::PresentationCallback callback,
  31. int frame_id);
  32. ScopedSwapBuffers(const ScopedSwapBuffers&) = delete;
  33. ScopedSwapBuffers& operator=(const ScopedSwapBuffers&) = delete;
  34. ~ScopedSwapBuffers();
  35. void set_result(gfx::SwapResult result) { result_ = result; }
  36. gfx::SwapResult result() const { return result_; }
  37. private:
  38. const raw_ptr<GLSurfacePresentationHelper> helper_;
  39. gfx::SwapResult result_ = gfx::SwapResult::SWAP_ACK;
  40. };
  41. explicit GLSurfacePresentationHelper(gfx::VSyncProvider* vsync_provider);
  42. // For using fixed VSync provider.
  43. GLSurfacePresentationHelper(const base::TimeTicks timebase,
  44. const base::TimeDelta interval);
  45. GLSurfacePresentationHelper(const GLSurfacePresentationHelper&) = delete;
  46. GLSurfacePresentationHelper& operator=(const GLSurfacePresentationHelper&) =
  47. delete;
  48. ~GLSurfacePresentationHelper();
  49. void OnMakeCurrent(GLContext* context, GLSurface* surface);
  50. void PreSwapBuffers(GLSurface::PresentationCallback callback, int frame_id);
  51. void PostSwapBuffers(gfx::SwapResult result);
  52. private:
  53. struct Frame {
  54. Frame(Frame&& other);
  55. Frame(int frame_id, GLSurface::PresentationCallback callback);
  56. Frame(std::unique_ptr<GPUTimer>&& timer,
  57. GLSurface::PresentationCallback callback);
  58. Frame(std::unique_ptr<GLFence>&& fence,
  59. GLSurface::PresentationCallback callback);
  60. explicit Frame(GLSurface::PresentationCallback callback);
  61. ~Frame();
  62. Frame& operator=(Frame&& other);
  63. void Destroy(bool has_context = false);
  64. std::unique_ptr<GPUTimer> timer;
  65. // GLFence is used only if gpu timers are not available.
  66. std::unique_ptr<GLFence> fence;
  67. int frame_id = -1;
  68. GLSurface::PresentationCallback callback;
  69. gfx::SwapResult result = gfx::SwapResult::SWAP_ACK;
  70. };
  71. bool GetFrameTimestampInfoIfAvailable(const Frame& frame,
  72. base::TimeTicks* timestamp,
  73. base::TimeDelta* interval,
  74. base::TimeTicks* writes_done,
  75. uint32_t* flags);
  76. // Check |pending_frames_| and run presentation callbacks.
  77. void CheckPendingFrames();
  78. // Callback used by PostDelayedTask for running CheckPendingFrames().
  79. void CheckPendingFramesCallback();
  80. void UpdateVSyncCallback(bool should_check_pending_frames,
  81. const base::TimeTicks timebase,
  82. const base::TimeDelta interval);
  83. void ScheduleCheckPendingFrames(bool align_with_next_vsync);
  84. const raw_ptr<gfx::VSyncProvider> vsync_provider_;
  85. scoped_refptr<GLContext> gl_context_;
  86. raw_ptr<GLSurface> surface_ = nullptr;
  87. scoped_refptr<GPUTimingClient> gpu_timing_client_;
  88. base::circular_deque<Frame> pending_frames_;
  89. base::TimeTicks vsync_timebase_;
  90. base::TimeDelta vsync_interval_;
  91. bool check_pending_frame_scheduled_ = false;
  92. bool gl_fence_supported_ = false;
  93. raw_ptr<EGLTimestampClient> egl_timestamp_client_ = nullptr;
  94. bool update_vsync_pending_ = false;
  95. base::WeakPtrFactory<GLSurfacePresentationHelper> weak_ptr_factory_{this};
  96. };
  97. } // namespace gl
  98. #endif // UI_GL_GL_SURFACE_PRESENTATION_HELPER_H_