gl_surface_egl_surface_control.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_EGL_SURFACE_CONTROL_H_
  5. #define UI_GL_GL_SURFACE_EGL_SURFACE_CONTROL_H_
  6. #include <android/native_window.h>
  7. #include <memory>
  8. #include "base/android/scoped_hardware_buffer_handle.h"
  9. #include "base/cancelable_callback.h"
  10. #include "base/containers/flat_map.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/time/time.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. #include "ui/gfx/android/android_surface_control_compat.h"
  16. #include "ui/gl/gl_export.h"
  17. #include "ui/gl/gl_surface_egl.h"
  18. namespace base {
  19. class SingleThreadTaskRunner;
  20. namespace android {
  21. class ScopedHardwareBufferFenceSync;
  22. } // namespace android
  23. } // namespace base
  24. namespace gl {
  25. class GL_EXPORT GLSurfaceEGLSurfaceControl : public GLSurfaceEGL {
  26. public:
  27. GLSurfaceEGLSurfaceControl(
  28. GLDisplayEGL* display,
  29. ANativeWindow* window,
  30. scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  31. // GLSurface implementation.
  32. int GetBufferCount() const override;
  33. bool Initialize(GLSurfaceFormat format) override;
  34. void PrepareToDestroy(bool have_context) override;
  35. void Destroy() override;
  36. bool Resize(const gfx::Size& size,
  37. float scale_factor,
  38. const gfx::ColorSpace& color_space,
  39. bool has_alpha) override;
  40. bool IsOffscreen() override;
  41. gfx::Size GetSize() override;
  42. bool OnMakeCurrent(GLContext* context) override;
  43. bool ScheduleOverlayPlane(
  44. GLImage* image,
  45. std::unique_ptr<gfx::GpuFence> gpu_fence,
  46. const gfx::OverlayPlaneData& overlay_plane_data) override;
  47. bool IsSurfaceless() const override;
  48. void* GetHandle() override;
  49. void PreserveChildSurfaceControls() override;
  50. // Sync versions of frame update, should never be used.
  51. gfx::SwapResult SwapBuffers(PresentationCallback callback) override;
  52. gfx::SwapResult CommitOverlayPlanes(PresentationCallback callback) override;
  53. gfx::SwapResult PostSubBuffer(int x,
  54. int y,
  55. int width,
  56. int height,
  57. PresentationCallback callback) override;
  58. void SwapBuffersAsync(SwapCompletionCallback completion_callback,
  59. PresentationCallback presentation_callback) override;
  60. void CommitOverlayPlanesAsync(
  61. SwapCompletionCallback completion_callback,
  62. PresentationCallback presentation_callback) override;
  63. void PostSubBufferAsync(int x,
  64. int y,
  65. int width,
  66. int height,
  67. SwapCompletionCallback completion_callback,
  68. PresentationCallback presentation_callback) override;
  69. bool SupportsAsyncSwap() override;
  70. bool SupportsPlaneGpuFences() const override;
  71. bool SupportsPostSubBuffer() override;
  72. bool SupportsCommitOverlayPlanes() override;
  73. void SetDisplayTransform(gfx::OverlayTransform transform) override;
  74. gfx::SurfaceOrigin GetOrigin() const override;
  75. void SetFrameRate(float frame_rate) override;
  76. void SetChoreographerVsyncIdForNextFrame(
  77. absl::optional<int64_t> choreographer_vsync_id) override;
  78. private:
  79. ~GLSurfaceEGLSurfaceControl() override;
  80. struct SurfaceState {
  81. SurfaceState();
  82. SurfaceState(const gfx::SurfaceControl::Surface& parent,
  83. const std::string& name);
  84. ~SurfaceState();
  85. SurfaceState(SurfaceState&& other);
  86. SurfaceState& operator=(SurfaceState&& other);
  87. int z_order = 0;
  88. raw_ptr<AHardwareBuffer> hardware_buffer = nullptr;
  89. gfx::Rect dst;
  90. gfx::Rect src;
  91. gfx::OverlayTransform transform = gfx::OVERLAY_TRANSFORM_NONE;
  92. bool opaque = true;
  93. gfx::ColorSpace color_space;
  94. absl::optional<gfx::HDRMetadata> hdr_metadata;
  95. // Indicates whether buffer for this layer was updated in the currently
  96. // pending transaction, or the last transaction submitted if there isn't
  97. // one pending.
  98. bool buffer_updated_in_pending_transaction = true;
  99. // Indicates whether the |surface| will be visible or hidden.
  100. bool visibility = true;
  101. scoped_refptr<gfx::SurfaceControl::Surface> surface;
  102. };
  103. struct ResourceRef {
  104. ResourceRef();
  105. ~ResourceRef();
  106. ResourceRef(ResourceRef&& other);
  107. ResourceRef& operator=(ResourceRef&& other);
  108. scoped_refptr<gfx::SurfaceControl::Surface> surface;
  109. std::unique_ptr<base::android::ScopedHardwareBufferFenceSync> scoped_buffer;
  110. };
  111. using ResourceRefs = base::flat_map<ASurfaceControl*, ResourceRef>;
  112. struct PendingPresentationCallback {
  113. PendingPresentationCallback();
  114. ~PendingPresentationCallback();
  115. PendingPresentationCallback(PendingPresentationCallback&& other);
  116. PendingPresentationCallback& operator=(PendingPresentationCallback&& other);
  117. base::TimeTicks available_time;
  118. base::TimeTicks ready_time;
  119. base::TimeTicks latch_time;
  120. base::ScopedFD present_fence;
  121. PresentationCallback callback;
  122. };
  123. struct PrimaryPlaneFences {
  124. PrimaryPlaneFences();
  125. ~PrimaryPlaneFences();
  126. PrimaryPlaneFences(PrimaryPlaneFences&& other);
  127. PrimaryPlaneFences& operator=(PrimaryPlaneFences&& other);
  128. base::ScopedFD available_fence;
  129. base::ScopedFD ready_fence;
  130. };
  131. using TransactionId = uint64_t;
  132. class TransactionAckTimeoutManager {
  133. public:
  134. TransactionAckTimeoutManager(
  135. scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  136. TransactionAckTimeoutManager(const TransactionAckTimeoutManager&) = delete;
  137. TransactionAckTimeoutManager& operator=(
  138. const TransactionAckTimeoutManager&) = delete;
  139. ~TransactionAckTimeoutManager();
  140. void ScheduleHangDetection();
  141. void OnTransactionAck();
  142. private:
  143. void OnTransactionTimeout(TransactionId transaction_id);
  144. scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner_;
  145. TransactionId current_transaction_id_ = 0;
  146. TransactionId last_acked_transaction_id_ = 0;
  147. base::CancelableOnceClosure hang_detection_cb_;
  148. };
  149. void CommitPendingTransaction(const gfx::Rect& damage_rect,
  150. SwapCompletionCallback completion_callback,
  151. PresentationCallback callback);
  152. // Called on the |gpu_task_runner_| when a transaction is acked by the
  153. // framework.
  154. void OnTransactionAckOnGpuThread(
  155. SwapCompletionCallback completion_callback,
  156. PresentationCallback presentation_callback,
  157. ResourceRefs released_resources,
  158. absl::optional<PrimaryPlaneFences> primary_plane_fences,
  159. gfx::SurfaceControl::TransactionStats transaction_stats);
  160. // Called on the |gpu_task_runner_| when a transaction is committed by the
  161. // framework.
  162. void OnTransactionCommittedOnGpuThread();
  163. void AdvanceTransactionQueue();
  164. void CheckPendingPresentationCallbacks();
  165. gfx::Rect ApplyDisplayInverse(const gfx::Rect& input) const;
  166. const gfx::ColorSpace& GetNearestSupportedColorSpace(
  167. const gfx::ColorSpace& buffer_color_space) const;
  168. const std::string root_surface_name_;
  169. const std::string child_surface_name_;
  170. // The rect of the native window backing this surface.
  171. gfx::Rect window_rect_;
  172. // Holds the surface state changes made since the last call to SwapBuffers.
  173. absl::optional<gfx::SurfaceControl::Transaction> pending_transaction_;
  174. size_t pending_surfaces_count_ = 0u;
  175. // Resources in the pending frame, for which updates are being
  176. // collected in |pending_transaction_|. These are resources for which the
  177. // pending transaction has a ref but they have not been applied and
  178. // transferred to the framework.
  179. ResourceRefs pending_frame_resources_;
  180. // The fences associated with the primary plane (renderer by the display
  181. // compositor) for the pending frame.
  182. absl::optional<PrimaryPlaneFences> primary_plane_fences_;
  183. // Transactions waiting to be applied once the previous transaction is acked.
  184. std::queue<gfx::SurfaceControl::Transaction> pending_transaction_queue_;
  185. // PresentationCallbacks for transactions which have been acked but their
  186. // present fence has not fired yet.
  187. std::queue<PendingPresentationCallback> pending_presentation_callback_queue_;
  188. // The list of Surfaces and the corresponding state based on the most recent
  189. // updates.
  190. std::vector<SurfaceState> surface_list_;
  191. // Resources in the previous transaction sent or queued to be sent to the
  192. // framework. The framework is assumed to retain ownership of these resources
  193. // until the next frame update.
  194. ResourceRefs current_frame_resources_;
  195. // The root surface tied to the ANativeWindow that places the content of this
  196. // GLSurface in the java view tree.
  197. scoped_refptr<gfx::SurfaceControl::Surface> root_surface_;
  198. // The last context made current with this surface.
  199. scoped_refptr<GLContext> context_;
  200. // Set if a transaction was applied and we are waiting for it to be acked.
  201. bool transaction_ack_pending_ = false;
  202. gfx::OverlayTransform display_transform_ = gfx::OVERLAY_TRANSFORM_NONE;
  203. float frame_rate_ = 0;
  204. bool frame_rate_update_pending_ = false;
  205. EGLSurface offscreen_surface_ = nullptr;
  206. base::CancelableOnceClosure check_pending_presentation_callback_queue_task_;
  207. // Set if a swap failed and the surface is no longer usable.
  208. bool surface_lost_ = false;
  209. TransactionAckTimeoutManager transaction_ack_timeout_manager_;
  210. bool preserve_children_ = false;
  211. scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner_;
  212. // Use target deadline API instead of queuing transactions and submitting
  213. // after previous transaction is ack-ed.
  214. const bool use_target_deadline_;
  215. const bool using_on_commit_callback_;
  216. absl::optional<int64_t> choreographer_vsync_id_for_next_frame_;
  217. base::WeakPtrFactory<GLSurfaceEGLSurfaceControl> weak_factory_{this};
  218. };
  219. } // namespace gl
  220. #endif // UI_GL_GL_SURFACE_EGL_SURFACE_CONTROL_H_