delegated_frame_host_android.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 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 UI_ANDROID_DELEGATED_FRAME_HOST_ANDROID_H_
  5. #define UI_ANDROID_DELEGATED_FRAME_HOST_ANDROID_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/memory/ref_counted.h"
  8. #include "base/numerics/safe_conversions.h"
  9. #include "base/time/time.h"
  10. #include "cc/layers/deadline_policy.h"
  11. #include "components/viz/client/frame_evictor.h"
  12. #include "components/viz/common/frame_sinks/begin_frame_args.h"
  13. #include "components/viz/common/frame_sinks/copy_output_request.h"
  14. #include "components/viz/common/frame_timing_details_map.h"
  15. #include "components/viz/common/resources/returned_resource.h"
  16. #include "components/viz/common/surfaces/surface_info.h"
  17. #include "components/viz/host/host_frame_sink_client.h"
  18. #include "ui/android/ui_android_export.h"
  19. namespace cc {
  20. class SurfaceLayer;
  21. enum class SurfaceDrawStatus;
  22. } // namespace cc
  23. namespace viz {
  24. class HostFrameSinkManager;
  25. } // namespace viz
  26. namespace ui {
  27. class ViewAndroid;
  28. class WindowAndroidCompositor;
  29. class UI_ANDROID_EXPORT DelegatedFrameHostAndroid
  30. : public viz::HostFrameSinkClient,
  31. public viz::FrameEvictorClient {
  32. public:
  33. class Client {
  34. public:
  35. virtual ~Client() {}
  36. virtual void OnFrameTokenChanged(uint32_t frame_token,
  37. base::TimeTicks activation_time) = 0;
  38. virtual void WasEvicted() = 0;
  39. virtual void OnSurfaceIdChanged() = 0;
  40. };
  41. DelegatedFrameHostAndroid(ViewAndroid* view,
  42. viz::HostFrameSinkManager* host_frame_sink_manager,
  43. Client* client,
  44. const viz::FrameSinkId& frame_sink_id);
  45. DelegatedFrameHostAndroid(const DelegatedFrameHostAndroid&) = delete;
  46. DelegatedFrameHostAndroid& operator=(const DelegatedFrameHostAndroid&) =
  47. delete;
  48. ~DelegatedFrameHostAndroid() override;
  49. static int64_t TimeDeltaToFrames(base::TimeDelta delta) {
  50. return base::ClampRound<int64_t>(delta /
  51. viz::BeginFrameArgs::DefaultInterval());
  52. }
  53. // Wait up to 5 seconds for the first frame to be produced. Having Android
  54. // display a placeholder for a longer period of time is preferable to drawing
  55. // nothing, and the first frame can take a while on low-end systems.
  56. static constexpr base::TimeDelta FirstFrameTimeout() {
  57. return base::Seconds(5);
  58. }
  59. static int64_t FirstFrameTimeoutFrames() {
  60. return TimeDeltaToFrames(FirstFrameTimeout());
  61. }
  62. // Wait up to 175 milliseconds for a frame of the correct size to be produced.
  63. // Android OS will only wait 200 milliseconds, so we limit this to make sure
  64. // that Viz is able to produce the latest frame from the Browser before the OS
  65. // stops waiting. Otherwise a rotated version of the previous frame will be
  66. // displayed with a large black region where there is no content yet.
  67. static constexpr base::TimeDelta ResizeTimeout() {
  68. return base::Milliseconds(175);
  69. }
  70. static int64_t ResizeTimeoutFrames() {
  71. return TimeDeltaToFrames(ResizeTimeout());
  72. }
  73. void ClearFallbackSurfaceForCommitPending();
  74. // Advances the fallback surface to the first surface after navigation. This
  75. // ensures that stale surfaces are not presented to the user for an indefinite
  76. // period of time.
  77. void ResetFallbackToFirstNavigationSurface();
  78. bool HasDelegatedContent() const;
  79. cc::SurfaceLayer* content_layer_for_testing() { return content_layer_.get(); }
  80. const viz::FrameSinkId& GetFrameSinkId() const;
  81. // Should only be called when the host has a content layer. Use this for one-
  82. // off screen capture, not for video. Always provides ResultFormat::RGBA,
  83. // ResultDestination::kSystemMemory CopyOutputResults.
  84. void CopyFromCompositingSurface(
  85. const gfx::Rect& src_subrect,
  86. const gfx::Size& output_size,
  87. base::OnceCallback<void(const SkBitmap&)> callback);
  88. bool CanCopyFromCompositingSurface() const;
  89. void CompositorFrameSinkChanged();
  90. // Called when this DFH is attached/detached from a parent browser compositor
  91. // and needs to be attached to the surface hierarchy.
  92. void AttachToCompositor(WindowAndroidCompositor* compositor);
  93. void DetachFromCompositor();
  94. bool IsPrimarySurfaceEvicted() const;
  95. bool HasSavedFrame() const;
  96. void WasHidden();
  97. void WasShown(const viz::LocalSurfaceId& local_surface_id,
  98. const gfx::Size& size_in_pixels,
  99. bool is_fullscreen);
  100. void EmbedSurface(const viz::LocalSurfaceId& new_local_surface_id,
  101. const gfx::Size& new_size_in_pixels,
  102. cc::DeadlinePolicy deadline_policy,
  103. bool is_fullscreen);
  104. // Returns the ID for the current Surface. Returns an invalid ID if no
  105. // surface exists (!HasDelegatedContent()).
  106. viz::SurfaceId SurfaceId() const;
  107. bool HasPrimarySurface() const;
  108. bool HasFallbackSurface() const;
  109. void TakeFallbackContentFrom(DelegatedFrameHostAndroid* other);
  110. // Called when navigation has completed, and this DelegatedFrameHost is
  111. // visible. A new Surface will have been embedded at this point. If navigation
  112. // is done while hidden, this will be called upon becoming visible.
  113. void DidNavigate();
  114. // Navigation to a different page than the current one has begun. This is
  115. // called regardless of the visibility of the page. Caches the current
  116. // LocalSurfaceId information so that old content can be evicted if
  117. // navigation fails to complete.
  118. void OnNavigateToNewPage();
  119. void SetTopControlsVisibleHeight(float height);
  120. private:
  121. // FrameEvictorClient implementation.
  122. void EvictDelegatedFrame() override;
  123. // viz::HostFrameSinkClient implementation.
  124. void OnFirstSurfaceActivation(const viz::SurfaceInfo& surface_info) override;
  125. void OnFrameTokenChanged(uint32_t frame_token,
  126. base::TimeTicks activation_time) override;
  127. void ProcessCopyOutputRequest(
  128. std::unique_ptr<viz::CopyOutputRequest> request);
  129. void SetLocalSurfaceId(const viz::LocalSurfaceId& local_surface_id);
  130. const viz::FrameSinkId frame_sink_id_;
  131. raw_ptr<ViewAndroid> view_;
  132. const raw_ptr<viz::HostFrameSinkManager> host_frame_sink_manager_;
  133. raw_ptr<WindowAndroidCompositor> registered_parent_compositor_ = nullptr;
  134. raw_ptr<Client> client_;
  135. float top_controls_visible_height_ = 0.f;
  136. scoped_refptr<cc::SurfaceLayer> content_layer_;
  137. // Whether we've received a frame from the renderer since navigating.
  138. // Only used when surface synchronization is on.
  139. viz::LocalSurfaceId first_local_surface_id_after_navigation_;
  140. // While navigating we have no active |local_surface_id_|. Track the one from
  141. // before a navigation, because if the navigation fails to complete, we will
  142. // need to evict its surface.
  143. viz::LocalSurfaceId pre_navigation_local_surface_id_;
  144. // The LocalSurfaceId of the currently embedded surface. If surface sync is
  145. // on, this surface is not necessarily active.
  146. viz::LocalSurfaceId local_surface_id_;
  147. // The size of the above surface (updated at the same time).
  148. gfx::Size surface_size_in_pixels_;
  149. std::unique_ptr<viz::FrameEvictor> frame_evictor_;
  150. };
  151. } // namespace ui
  152. #endif // UI_ANDROID_DELEGATED_FRAME_HOST_ANDROID_H_