blob: 49443146095a06d984c922c8dc3b4fdf670d0b36 [file] [log] [blame]
khushalsagara693aa72016-08-16 22:18:061// 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
5#ifndef UI_ANDROID_DELEGATED_FRAME_HOST_ANDROID_H_
6#define UI_ANDROID_DELEGATED_FRAME_HOST_ANDROID_H_
7
8#include "base/macros.h"
9#include "base/memory/ref_counted.h"
Peter Kasting809cfe72020-08-04 00:56:1310#include "base/numerics/safe_conversions.h"
Fady Samueldba483c2018-05-30 04:42:3511#include "cc/layers/deadline_policy.h"
Fady Samuelac325ce2018-06-01 15:23:2712#include "components/viz/client/frame_evictor.h"
Eric Karlb6d3d74d2019-07-17 21:43:4713#include "components/viz/common/frame_sinks/begin_frame_args.h"
danakjf20f4502017-09-26 17:13:3114#include "components/viz/common/frame_sinks/copy_output_request.h"
Daniel Libby58389b72019-06-11 20:03:4315#include "components/viz/common/frame_timing_details_map.h"
Fady Samuel4f7f0fb32017-07-28 15:33:3716#include "components/viz/common/resources/returned_resource.h"
Fady Samuel1a21156e2017-07-13 04:57:2917#include "components/viz/common/surfaces/surface_info.h"
Fady Samuelbac0f1a2017-08-02 15:54:0218#include "components/viz/host/host_frame_sink_client.h"
khushalsagara693aa72016-08-16 22:18:0619#include "ui/android/ui_android_export.h"
khushalsagara693aa72016-08-16 22:18:0620
21namespace cc {
khushalsagara693aa72016-08-16 22:18:0622class SurfaceLayer;
khushalsagara693aa72016-08-16 22:18:0623enum class SurfaceDrawStatus;
khushalsagara693aa72016-08-16 22:18:0624} // namespace cc
25
kylechara0900162017-07-14 17:35:2526namespace viz {
kylechara0900162017-07-14 17:35:2527class HostFrameSinkManager;
28} // namespace viz
29
khushalsagara693aa72016-08-16 22:18:0630namespace ui {
31class ViewAndroid;
32class WindowAndroidCompositor;
33
34class UI_ANDROID_EXPORT DelegatedFrameHostAndroid
Eric Karlb6d3d74d2019-07-17 21:43:4735 : public viz::HostFrameSinkClient,
Fady Samuelac325ce2018-06-01 15:23:2736 public viz::FrameEvictorClient {
khushalsagara693aa72016-08-16 22:18:0637 public:
eseckler8c15fc32016-12-20 20:22:2038 class Client {
39 public:
Xu Xing6eeb35a32018-08-25 00:24:1440 virtual ~Client() {}
yiyixae38c332017-11-14 18:58:2741 virtual void OnFrameTokenChanged(uint32_t frame_token) = 0;
Saman Sami4580e6322018-10-21 03:46:5942 virtual void WasEvicted() = 0;
eseckler8c15fc32016-12-20 20:22:2043 };
khushalsagara693aa72016-08-16 22:18:0644
45 DelegatedFrameHostAndroid(ViewAndroid* view,
kylechara0900162017-07-14 17:35:2546 viz::HostFrameSinkManager* host_frame_sink_manager,
xlai9351829f2017-01-27 18:39:1347 Client* client,
Eric Karlb6d3d74d2019-07-17 21:43:4748 const viz::FrameSinkId& frame_sink_id);
khushalsagara693aa72016-08-16 22:18:0649
50 ~DelegatedFrameHostAndroid() override;
51
Bo Liu140aa0c2020-09-14 14:09:5652 static int64_t TimeDeltaToFrames(base::TimeDelta delta) {
53 return base::ClampRound<int64_t>(delta /
54 viz::BeginFrameArgs::DefaultInterval());
55 }
56
Fady Samuel99ec37a2018-07-16 21:43:4757 // Wait up to 5 seconds for the first frame to be produced. Having Android
58 // display a placeholder for a longer period of time is preferable to drawing
59 // nothing, and the first frame can take a while on low-end systems.
60 static constexpr base::TimeDelta FirstFrameTimeout() {
61 return base::TimeDelta::FromSeconds(5);
62 }
Peter Kasting809cfe72020-08-04 00:56:1363 static int64_t FirstFrameTimeoutFrames() {
Bo Liu140aa0c2020-09-14 14:09:5664 return TimeDeltaToFrames(FirstFrameTimeout());
Fady Samuel99ec37a2018-07-16 21:43:4765 }
66
67 // Wait up to 1 second for a frame of the correct size to be produced. Android
68 // OS will only wait 4 seconds, so we limit this to 1 second to make sure we
69 // have always produced a frame before the OS stops waiting.
70 static constexpr base::TimeDelta ResizeTimeout() {
71 return base::TimeDelta::FromSeconds(1);
72 }
Peter Kasting809cfe72020-08-04 00:56:1373 static int64_t ResizeTimeoutFrames() {
Bo Liu140aa0c2020-09-14 14:09:5674 return TimeDeltaToFrames(ResizeTimeout());
Fady Samuel99ec37a2018-07-16 21:43:4775 }
76
akabaf2624f22018-08-21 17:11:0177 // Advances the fallback surface to the first surface after navigation. This
78 // ensures that stale surfaces are not presented to the user for an indefinite
79 // period of time.
80 void ResetFallbackToFirstNavigationSurface();
81
khushalsagara693aa72016-08-16 22:18:0682 bool HasDelegatedContent() const;
83
Fady Samuelbcac6f02018-07-11 02:02:4284 cc::SurfaceLayer* content_layer_for_testing() { return content_layer_.get(); }
85
Fady Samuel0c2f5b82018-07-19 00:33:4686 const viz::FrameSinkId& GetFrameSinkId() const;
khushalsagara693aa72016-08-16 22:18:0687
Yuri Wiitalab9ad27a2017-09-06 19:13:5088 // Should only be called when the host has a content layer. Use this for one-
89 // off screen capture, not for video. Always provides RGBA_BITMAP
90 // CopyOutputResults.
Yuri Wiitala419ed0f2018-02-22 23:21:3891 void CopyFromCompositingSurface(
92 const gfx::Rect& src_subrect,
93 const gfx::Size& output_size,
94 base::OnceCallback<void(const SkBitmap&)> callback);
95 bool CanCopyFromCompositingSurface() const;
khushalsagara693aa72016-08-16 22:18:0696
danakj1120f4c2016-09-15 02:05:3297 void CompositorFrameSinkChanged();
khushalsagara693aa72016-08-16 22:18:0698
ennea487a272016-09-30 19:56:1899 // Called when this DFH is attached/detached from a parent browser compositor
100 // and needs to be attached to the surface hierarchy.
starazaa231112017-02-07 17:53:24101 void AttachToCompositor(WindowAndroidCompositor* compositor);
102 void DetachFromCompositor();
ennea487a272016-09-30 19:56:18103
Fady Samuelac325ce2018-06-01 15:23:27104 bool IsPrimarySurfaceEvicted() const;
105 bool HasSavedFrame() const;
106 void WasHidden();
107 void WasShown(const viz::LocalSurfaceId& local_surface_id,
Vasiliy Telezhnikov03d42b522020-06-29 14:40:59108 const gfx::Size& size_in_pixels,
Vasiliy Telezhnikov2bef50f2020-07-10 19:19:31109 bool is_fullscreen);
Saman Sami1bbd14692018-10-19 22:27:30110 void EmbedSurface(const viz::LocalSurfaceId& new_local_surface_id,
111 const gfx::Size& new_size_in_pixels,
Vasiliy Telezhnikov03d42b522020-06-29 14:40:59112 cc::DeadlinePolicy deadline_policy,
Vasiliy Telezhnikov2bef50f2020-07-10 19:19:31113 bool is_fullscreen);
Eric Karlbaa55ff32018-01-18 00:46:11114
115 // Returns the ID for the current Surface. Returns an invalid ID if no
116 // surface exists (!HasDelegatedContent()).
Fady Samuel120c7e22018-07-20 05:19:28117 viz::SurfaceId SurfaceId() const;
Saman Sami1bbd14692018-10-19 22:27:30118
Saman Samif03a9dba2018-09-24 18:05:07119 bool HasPrimarySurface() const;
Xu Xing9ef5a882018-08-21 00:11:20120 bool HasFallbackSurface() const;
Eric Karlbaa55ff32018-01-18 00:46:11121
Saman Sami26a1fcd2018-04-10 17:12:21122 void TakeFallbackContentFrom(DelegatedFrameHostAndroid* other);
123
Jonathan Ross865a12d2020-12-16 22:33:00124 // Called when navigation has completed, and this DelegatedFrameHost is
125 // visible. A new Surface will have been embedded at this point. If navigation
126 // is done while hidden, this will be called upon becoming visible.
Eric Karldb7eb242018-05-16 17:16:59127 void DidNavigate();
Jonathan Ross865a12d2020-12-16 22:33:00128 // Navigation to a different page than the current one has begun. This is
129 // called regardless of the visibility of the page. Caches the current
130 // LocalSurfaceId information so that old content can be evicted if
131 // navigation fails to complete.
132 void OnNavigateToNewPage();
Eric Karldb7eb242018-05-16 17:16:59133
Sadrul Habib Chowdhury71c7ed802019-11-28 02:24:44134 void SetTopControlsVisibleHeight(float height);
135
khushalsagara693aa72016-08-16 22:18:06136 private:
kylechar055c2102020-07-13 19:58:15137 // FrameEvictorClient implementation.
138 void EvictDelegatedFrame() override;
139
Fady Samuelbac0f1a2017-08-02 15:54:02140 // viz::HostFrameSinkClient implementation.
Fady Samuel5b7fb8e2017-08-08 16:58:22141 void OnFirstSurfaceActivation(const viz::SurfaceInfo& surface_info) override;
yiyixae38c332017-11-14 18:58:27142 void OnFrameTokenChanged(uint32_t frame_token) override;
Fady Samuelbac0f1a2017-08-02 15:54:02143
Eric Karl5479bee2018-08-24 23:10:28144 void ProcessCopyOutputRequest(
145 std::unique_ptr<viz::CopyOutputRequest> request);
146
Fady Samueld5c26182017-07-12 02:43:33147 const viz::FrameSinkId frame_sink_id_;
fsamuelb6acafa2016-10-04 03:21:52148
khushalsagara693aa72016-08-16 22:18:06149 ViewAndroid* view_;
150
kylechara0900162017-07-14 17:35:25151 viz::HostFrameSinkManager* const host_frame_sink_manager_;
starazaa231112017-02-07 17:53:24152 WindowAndroidCompositor* registered_parent_compositor_ = nullptr;
eseckler8c15fc32016-12-20 20:22:20153 Client* client_;
khushalsagara693aa72016-08-16 22:18:06154
Sadrul Habib Chowdhury71c7ed802019-11-28 02:24:44155 float top_controls_visible_height_ = 0.f;
156
khushalsagara693aa72016-08-16 22:18:06157 scoped_refptr<cc::SurfaceLayer> content_layer_;
158
Eric Karldb7eb242018-05-16 17:16:59159 // Whether we've received a frame from the renderer since navigating.
Fady Samueldba483c2018-05-30 04:42:35160 // Only used when surface synchronization is on.
akabaf2624f22018-08-21 17:11:01161 viz::LocalSurfaceId first_local_surface_id_after_navigation_;
Jonathan Ross865a12d2020-12-16 22:33:00162 // While navigating we have no active |local_surface_id_|. Track the one from
163 // before a navigation, because if the navigation fails to complete, we will
164 // need to evict its surface.
165 viz::LocalSurfaceId pre_navigation_local_surface_id_;
Fady Samueldba483c2018-05-30 04:42:35166
Saman Sami1bbd14692018-10-19 22:27:30167 // The LocalSurfaceId of the currently embedded surface. If surface sync is
168 // on, this surface is not necessarily active.
169 viz::LocalSurfaceId local_surface_id_;
Eric Karldb7eb242018-05-16 17:16:59170
Fady Samuelac325ce2018-06-01 15:23:27171 // The size of the above surface (updated at the same time).
Saman Sami1bbd14692018-10-19 22:27:30172 gfx::Size surface_size_in_pixels_;
Fady Samuelac325ce2018-06-01 15:23:27173
174 std::unique_ptr<viz::FrameEvictor> frame_evictor_;
175
khushalsagara693aa72016-08-16 22:18:06176 DISALLOW_COPY_AND_ASSIGN(DelegatedFrameHostAndroid);
177};
178
179} // namespace ui
180
181#endif // UI_ANDROID_DELEGATED_FRAME_HOST_ANDROID_H_