khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 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 | |
| 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 Kasting | 809cfe7 | 2020-08-04 00:56:13 | [diff] [blame] | 10 | #include "base/numerics/safe_conversions.h" |
Jonathan Ross | be6a070 | 2021-02-23 01:01:56 | [diff] [blame^] | 11 | #include "base/time/time.h" |
Fady Samuel | dba483c | 2018-05-30 04:42:35 | [diff] [blame] | 12 | #include "cc/layers/deadline_policy.h" |
Fady Samuel | ac325ce | 2018-06-01 15:23:27 | [diff] [blame] | 13 | #include "components/viz/client/frame_evictor.h" |
Eric Karl | b6d3d74d | 2019-07-17 21:43:47 | [diff] [blame] | 14 | #include "components/viz/common/frame_sinks/begin_frame_args.h" |
danakj | f20f450 | 2017-09-26 17:13:31 | [diff] [blame] | 15 | #include "components/viz/common/frame_sinks/copy_output_request.h" |
Daniel Libby | 58389b7 | 2019-06-11 20:03:43 | [diff] [blame] | 16 | #include "components/viz/common/frame_timing_details_map.h" |
Fady Samuel | 4f7f0fb3 | 2017-07-28 15:33:37 | [diff] [blame] | 17 | #include "components/viz/common/resources/returned_resource.h" |
Fady Samuel | 1a21156e | 2017-07-13 04:57:29 | [diff] [blame] | 18 | #include "components/viz/common/surfaces/surface_info.h" |
Fady Samuel | bac0f1a | 2017-08-02 15:54:02 | [diff] [blame] | 19 | #include "components/viz/host/host_frame_sink_client.h" |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 20 | #include "ui/android/ui_android_export.h" |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 21 | |
| 22 | namespace cc { |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 23 | class SurfaceLayer; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 24 | enum class SurfaceDrawStatus; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 25 | } // namespace cc |
| 26 | |
kylechar | a090016 | 2017-07-14 17:35:25 | [diff] [blame] | 27 | namespace viz { |
kylechar | a090016 | 2017-07-14 17:35:25 | [diff] [blame] | 28 | class HostFrameSinkManager; |
| 29 | } // namespace viz |
| 30 | |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 31 | namespace ui { |
| 32 | class ViewAndroid; |
| 33 | class WindowAndroidCompositor; |
| 34 | |
| 35 | class UI_ANDROID_EXPORT DelegatedFrameHostAndroid |
Eric Karl | b6d3d74d | 2019-07-17 21:43:47 | [diff] [blame] | 36 | : public viz::HostFrameSinkClient, |
Fady Samuel | ac325ce | 2018-06-01 15:23:27 | [diff] [blame] | 37 | public viz::FrameEvictorClient { |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 38 | public: |
eseckler | 8c15fc3 | 2016-12-20 20:22:20 | [diff] [blame] | 39 | class Client { |
| 40 | public: |
Xu Xing | 6eeb35a3 | 2018-08-25 00:24:14 | [diff] [blame] | 41 | virtual ~Client() {} |
Jonathan Ross | be6a070 | 2021-02-23 01:01:56 | [diff] [blame^] | 42 | virtual void OnFrameTokenChanged(uint32_t frame_token, |
| 43 | base::TimeTicks activation_time) = 0; |
Saman Sami | 4580e632 | 2018-10-21 03:46:59 | [diff] [blame] | 44 | virtual void WasEvicted() = 0; |
eseckler | 8c15fc3 | 2016-12-20 20:22:20 | [diff] [blame] | 45 | }; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 46 | |
| 47 | DelegatedFrameHostAndroid(ViewAndroid* view, |
kylechar | a090016 | 2017-07-14 17:35:25 | [diff] [blame] | 48 | viz::HostFrameSinkManager* host_frame_sink_manager, |
xlai | 9351829f | 2017-01-27 18:39:13 | [diff] [blame] | 49 | Client* client, |
Eric Karl | b6d3d74d | 2019-07-17 21:43:47 | [diff] [blame] | 50 | const viz::FrameSinkId& frame_sink_id); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 51 | |
| 52 | ~DelegatedFrameHostAndroid() override; |
| 53 | |
Bo Liu | 140aa0c | 2020-09-14 14:09:56 | [diff] [blame] | 54 | static int64_t TimeDeltaToFrames(base::TimeDelta delta) { |
| 55 | return base::ClampRound<int64_t>(delta / |
| 56 | viz::BeginFrameArgs::DefaultInterval()); |
| 57 | } |
| 58 | |
Fady Samuel | 99ec37a | 2018-07-16 21:43:47 | [diff] [blame] | 59 | // Wait up to 5 seconds for the first frame to be produced. Having Android |
| 60 | // display a placeholder for a longer period of time is preferable to drawing |
| 61 | // nothing, and the first frame can take a while on low-end systems. |
| 62 | static constexpr base::TimeDelta FirstFrameTimeout() { |
| 63 | return base::TimeDelta::FromSeconds(5); |
| 64 | } |
Peter Kasting | 809cfe7 | 2020-08-04 00:56:13 | [diff] [blame] | 65 | static int64_t FirstFrameTimeoutFrames() { |
Bo Liu | 140aa0c | 2020-09-14 14:09:56 | [diff] [blame] | 66 | return TimeDeltaToFrames(FirstFrameTimeout()); |
Fady Samuel | 99ec37a | 2018-07-16 21:43:47 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Wait up to 1 second for a frame of the correct size to be produced. Android |
| 70 | // OS will only wait 4 seconds, so we limit this to 1 second to make sure we |
| 71 | // have always produced a frame before the OS stops waiting. |
| 72 | static constexpr base::TimeDelta ResizeTimeout() { |
| 73 | return base::TimeDelta::FromSeconds(1); |
| 74 | } |
Peter Kasting | 809cfe7 | 2020-08-04 00:56:13 | [diff] [blame] | 75 | static int64_t ResizeTimeoutFrames() { |
Bo Liu | 140aa0c | 2020-09-14 14:09:56 | [diff] [blame] | 76 | return TimeDeltaToFrames(ResizeTimeout()); |
Fady Samuel | 99ec37a | 2018-07-16 21:43:47 | [diff] [blame] | 77 | } |
| 78 | |
akaba | f2624f2 | 2018-08-21 17:11:01 | [diff] [blame] | 79 | // Advances the fallback surface to the first surface after navigation. This |
| 80 | // ensures that stale surfaces are not presented to the user for an indefinite |
| 81 | // period of time. |
| 82 | void ResetFallbackToFirstNavigationSurface(); |
| 83 | |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 84 | bool HasDelegatedContent() const; |
| 85 | |
Fady Samuel | bcac6f0 | 2018-07-11 02:02:42 | [diff] [blame] | 86 | cc::SurfaceLayer* content_layer_for_testing() { return content_layer_.get(); } |
| 87 | |
Fady Samuel | 0c2f5b8 | 2018-07-19 00:33:46 | [diff] [blame] | 88 | const viz::FrameSinkId& GetFrameSinkId() const; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 89 | |
Yuri Wiitala | b9ad27a | 2017-09-06 19:13:50 | [diff] [blame] | 90 | // Should only be called when the host has a content layer. Use this for one- |
| 91 | // off screen capture, not for video. Always provides RGBA_BITMAP |
| 92 | // CopyOutputResults. |
Yuri Wiitala | 419ed0f | 2018-02-22 23:21:38 | [diff] [blame] | 93 | void CopyFromCompositingSurface( |
| 94 | const gfx::Rect& src_subrect, |
| 95 | const gfx::Size& output_size, |
| 96 | base::OnceCallback<void(const SkBitmap&)> callback); |
| 97 | bool CanCopyFromCompositingSurface() const; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 98 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 99 | void CompositorFrameSinkChanged(); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 100 | |
enne | a487a27 | 2016-09-30 19:56:18 | [diff] [blame] | 101 | // Called when this DFH is attached/detached from a parent browser compositor |
| 102 | // and needs to be attached to the surface hierarchy. |
staraz | aa23111 | 2017-02-07 17:53:24 | [diff] [blame] | 103 | void AttachToCompositor(WindowAndroidCompositor* compositor); |
| 104 | void DetachFromCompositor(); |
enne | a487a27 | 2016-09-30 19:56:18 | [diff] [blame] | 105 | |
Fady Samuel | ac325ce | 2018-06-01 15:23:27 | [diff] [blame] | 106 | bool IsPrimarySurfaceEvicted() const; |
| 107 | bool HasSavedFrame() const; |
| 108 | void WasHidden(); |
| 109 | void WasShown(const viz::LocalSurfaceId& local_surface_id, |
Vasiliy Telezhnikov | 03d42b52 | 2020-06-29 14:40:59 | [diff] [blame] | 110 | const gfx::Size& size_in_pixels, |
Vasiliy Telezhnikov | 2bef50f | 2020-07-10 19:19:31 | [diff] [blame] | 111 | bool is_fullscreen); |
Saman Sami | 1bbd1469 | 2018-10-19 22:27:30 | [diff] [blame] | 112 | void EmbedSurface(const viz::LocalSurfaceId& new_local_surface_id, |
| 113 | const gfx::Size& new_size_in_pixels, |
Vasiliy Telezhnikov | 03d42b52 | 2020-06-29 14:40:59 | [diff] [blame] | 114 | cc::DeadlinePolicy deadline_policy, |
Vasiliy Telezhnikov | 2bef50f | 2020-07-10 19:19:31 | [diff] [blame] | 115 | bool is_fullscreen); |
Eric Karl | baa55ff3 | 2018-01-18 00:46:11 | [diff] [blame] | 116 | |
| 117 | // Returns the ID for the current Surface. Returns an invalid ID if no |
| 118 | // surface exists (!HasDelegatedContent()). |
Fady Samuel | 120c7e2 | 2018-07-20 05:19:28 | [diff] [blame] | 119 | viz::SurfaceId SurfaceId() const; |
Saman Sami | 1bbd1469 | 2018-10-19 22:27:30 | [diff] [blame] | 120 | |
Saman Sami | f03a9dba | 2018-09-24 18:05:07 | [diff] [blame] | 121 | bool HasPrimarySurface() const; |
Xu Xing | 9ef5a88 | 2018-08-21 00:11:20 | [diff] [blame] | 122 | bool HasFallbackSurface() const; |
Eric Karl | baa55ff3 | 2018-01-18 00:46:11 | [diff] [blame] | 123 | |
Saman Sami | 26a1fcd | 2018-04-10 17:12:21 | [diff] [blame] | 124 | void TakeFallbackContentFrom(DelegatedFrameHostAndroid* other); |
| 125 | |
Jonathan Ross | 865a12d | 2020-12-16 22:33:00 | [diff] [blame] | 126 | // Called when navigation has completed, and this DelegatedFrameHost is |
| 127 | // visible. A new Surface will have been embedded at this point. If navigation |
| 128 | // is done while hidden, this will be called upon becoming visible. |
Eric Karl | db7eb24 | 2018-05-16 17:16:59 | [diff] [blame] | 129 | void DidNavigate(); |
Jonathan Ross | 865a12d | 2020-12-16 22:33:00 | [diff] [blame] | 130 | // Navigation to a different page than the current one has begun. This is |
| 131 | // called regardless of the visibility of the page. Caches the current |
| 132 | // LocalSurfaceId information so that old content can be evicted if |
| 133 | // navigation fails to complete. |
| 134 | void OnNavigateToNewPage(); |
Eric Karl | db7eb24 | 2018-05-16 17:16:59 | [diff] [blame] | 135 | |
Sadrul Habib Chowdhury | 71c7ed80 | 2019-11-28 02:24:44 | [diff] [blame] | 136 | void SetTopControlsVisibleHeight(float height); |
| 137 | |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 138 | private: |
kylechar | 055c210 | 2020-07-13 19:58:15 | [diff] [blame] | 139 | // FrameEvictorClient implementation. |
| 140 | void EvictDelegatedFrame() override; |
| 141 | |
Fady Samuel | bac0f1a | 2017-08-02 15:54:02 | [diff] [blame] | 142 | // viz::HostFrameSinkClient implementation. |
Fady Samuel | 5b7fb8e | 2017-08-08 16:58:22 | [diff] [blame] | 143 | void OnFirstSurfaceActivation(const viz::SurfaceInfo& surface_info) override; |
Jonathan Ross | be6a070 | 2021-02-23 01:01:56 | [diff] [blame^] | 144 | void OnFrameTokenChanged(uint32_t frame_token, |
| 145 | base::TimeTicks activation_time) override; |
Fady Samuel | bac0f1a | 2017-08-02 15:54:02 | [diff] [blame] | 146 | |
Eric Karl | 5479bee | 2018-08-24 23:10:28 | [diff] [blame] | 147 | void ProcessCopyOutputRequest( |
| 148 | std::unique_ptr<viz::CopyOutputRequest> request); |
| 149 | |
Fady Samuel | d5c2618 | 2017-07-12 02:43:33 | [diff] [blame] | 150 | const viz::FrameSinkId frame_sink_id_; |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 151 | |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 152 | ViewAndroid* view_; |
| 153 | |
kylechar | a090016 | 2017-07-14 17:35:25 | [diff] [blame] | 154 | viz::HostFrameSinkManager* const host_frame_sink_manager_; |
staraz | aa23111 | 2017-02-07 17:53:24 | [diff] [blame] | 155 | WindowAndroidCompositor* registered_parent_compositor_ = nullptr; |
eseckler | 8c15fc3 | 2016-12-20 20:22:20 | [diff] [blame] | 156 | Client* client_; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 157 | |
Sadrul Habib Chowdhury | 71c7ed80 | 2019-11-28 02:24:44 | [diff] [blame] | 158 | float top_controls_visible_height_ = 0.f; |
| 159 | |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 160 | scoped_refptr<cc::SurfaceLayer> content_layer_; |
| 161 | |
Eric Karl | db7eb24 | 2018-05-16 17:16:59 | [diff] [blame] | 162 | // Whether we've received a frame from the renderer since navigating. |
Fady Samuel | dba483c | 2018-05-30 04:42:35 | [diff] [blame] | 163 | // Only used when surface synchronization is on. |
akaba | f2624f2 | 2018-08-21 17:11:01 | [diff] [blame] | 164 | viz::LocalSurfaceId first_local_surface_id_after_navigation_; |
Jonathan Ross | 865a12d | 2020-12-16 22:33:00 | [diff] [blame] | 165 | // While navigating we have no active |local_surface_id_|. Track the one from |
| 166 | // before a navigation, because if the navigation fails to complete, we will |
| 167 | // need to evict its surface. |
| 168 | viz::LocalSurfaceId pre_navigation_local_surface_id_; |
Fady Samuel | dba483c | 2018-05-30 04:42:35 | [diff] [blame] | 169 | |
Saman Sami | 1bbd1469 | 2018-10-19 22:27:30 | [diff] [blame] | 170 | // The LocalSurfaceId of the currently embedded surface. If surface sync is |
| 171 | // on, this surface is not necessarily active. |
| 172 | viz::LocalSurfaceId local_surface_id_; |
Eric Karl | db7eb24 | 2018-05-16 17:16:59 | [diff] [blame] | 173 | |
Fady Samuel | ac325ce | 2018-06-01 15:23:27 | [diff] [blame] | 174 | // The size of the above surface (updated at the same time). |
Saman Sami | 1bbd1469 | 2018-10-19 22:27:30 | [diff] [blame] | 175 | gfx::Size surface_size_in_pixels_; |
Fady Samuel | ac325ce | 2018-06-01 15:23:27 | [diff] [blame] | 176 | |
| 177 | std::unique_ptr<viz::FrameEvictor> frame_evictor_; |
| 178 | |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 179 | DISALLOW_COPY_AND_ASSIGN(DelegatedFrameHostAndroid); |
| 180 | }; |
| 181 | |
| 182 | } // namespace ui |
| 183 | |
| 184 | #endif // UI_ANDROID_DELEGATED_FRAME_HOST_ANDROID_H_ |