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