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" |
danakj | f20f450 | 2017-09-26 17:13:31 | [diff] [blame] | 12 | #include "components/viz/common/frame_sinks/copy_output_request.h" |
Fady Samuel | 4f7f0fb3 | 2017-07-28 15:33:37 | [diff] [blame] | 13 | #include "components/viz/common/resources/returned_resource.h" |
Fady Samuel | 1a21156e | 2017-07-13 04:57:29 | [diff] [blame] | 14 | #include "components/viz/common/surfaces/surface_info.h" |
Fady Samuel | bac0f1a | 2017-08-02 15:54:02 | [diff] [blame] | 15 | #include "components/viz/host/host_frame_sink_client.h" |
danakj | c391f33 | 2017-07-12 20:45:52 | [diff] [blame] | 16 | #include "components/viz/service/frame_sinks/compositor_frame_sink_support.h" |
kylechar | 9075b08 | 2017-09-12 16:59:10 | [diff] [blame] | 17 | #include "services/viz/public/interfaces/compositing/compositor_frame_sink.mojom.h" |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 18 | #include "ui/android/ui_android_export.h" |
Eric Karl | 465a525 | 2018-01-10 21:16:27 | [diff] [blame] | 19 | #include "ui/compositor/compositor_lock.h" |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 20 | |
| 21 | namespace cc { |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 22 | class SurfaceLayer; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 23 | enum class SurfaceDrawStatus; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 24 | } // namespace cc |
| 25 | |
kylechar | a090016 | 2017-07-14 17:35:25 | [diff] [blame] | 26 | namespace viz { |
danakj | 5e0a12b | 2017-09-25 17:26:49 | [diff] [blame] | 27 | class CompositorFrame; |
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 |
kylechar | 9075b08 | 2017-09-12 16:59:10 | [diff] [blame] | 36 | : public viz::mojom::CompositorFrameSinkClient, |
Fady Samuel | bac0f1a | 2017-08-02 15:54:02 | [diff] [blame] | 37 | public viz::ExternalBeginFrameSourceClient, |
Eric Karl | 465a525 | 2018-01-10 21:16:27 | [diff] [blame] | 38 | public viz::HostFrameSinkClient, |
Fady Samuel | ac325ce | 2018-06-01 15:23:27 | [diff] [blame] | 39 | public ui::CompositorLockClient, |
| 40 | public viz::FrameEvictorClient { |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 41 | public: |
eseckler | 8c15fc3 | 2016-12-20 20:22:20 | [diff] [blame] | 42 | class Client { |
| 43 | public: |
| 44 | virtual void SetBeginFrameSource( |
Fady Samuel | c645ffe | 2017-07-24 17:28:20 | [diff] [blame] | 45 | viz::BeginFrameSource* begin_frame_source) = 0; |
Sadrul Habib Chowdhury | b62a9730 | 2018-06-06 03:33:48 | [diff] [blame] | 46 | virtual void DidPresentCompositorFrame( |
| 47 | uint32_t presentation_token, |
| 48 | const gfx::PresentationFeedback& feedback) = 0; |
Fady Samuel | f3982a3 | 2018-06-14 20:52:13 | [diff] [blame] | 49 | virtual void DidReceiveCompositorFrameAck( |
| 50 | const std::vector<viz::ReturnedResource>& resources) = 0; |
Fady Samuel | 4f7f0fb3 | 2017-07-28 15:33:37 | [diff] [blame] | 51 | virtual void ReclaimResources( |
Fady Samuel | f3982a3 | 2018-06-14 20:52:13 | [diff] [blame] | 52 | const std::vector<viz::ReturnedResource>& resources) = 0; |
yiyix | ae38c33 | 2017-11-14 18:58:27 | [diff] [blame] | 53 | virtual void OnFrameTokenChanged(uint32_t frame_token) = 0; |
eseckler | 8c15fc3 | 2016-12-20 20:22:20 | [diff] [blame] | 54 | }; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 55 | |
| 56 | DelegatedFrameHostAndroid(ViewAndroid* view, |
kylechar | a090016 | 2017-07-14 17:35:25 | [diff] [blame] | 57 | viz::HostFrameSinkManager* host_frame_sink_manager, |
xlai | 9351829f | 2017-01-27 18:39:13 | [diff] [blame] | 58 | Client* client, |
Fady Samuel | 3b77b8a | 2018-06-29 06:53:53 | [diff] [blame] | 59 | const viz::FrameSinkId& frame_sink_id, |
| 60 | bool enable_surface_synchronization); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 61 | |
| 62 | ~DelegatedFrameHostAndroid() override; |
| 63 | |
Fady Samuel | 99ec37a | 2018-07-16 21:43:47 | [diff] [blame] | 64 | // Wait up to 5 seconds for the first frame to be produced. Having Android |
| 65 | // display a placeholder for a longer period of time is preferable to drawing |
| 66 | // nothing, and the first frame can take a while on low-end systems. |
| 67 | static constexpr base::TimeDelta FirstFrameTimeout() { |
| 68 | return base::TimeDelta::FromSeconds(5); |
| 69 | } |
| 70 | static constexpr int64_t FirstFrameTimeoutFrames() { |
| 71 | return FirstFrameTimeout() / viz::BeginFrameArgs::DefaultInterval(); |
| 72 | } |
| 73 | |
| 74 | // Wait up to 1 second for a frame of the correct size to be produced. Android |
| 75 | // OS will only wait 4 seconds, so we limit this to 1 second to make sure we |
| 76 | // have always produced a frame before the OS stops waiting. |
| 77 | static constexpr base::TimeDelta ResizeTimeout() { |
| 78 | return base::TimeDelta::FromSeconds(1); |
| 79 | } |
| 80 | static constexpr int64_t ResizeTimeoutFrames() { |
| 81 | return ResizeTimeout() / viz::BeginFrameArgs::DefaultInterval(); |
| 82 | } |
| 83 | |
Ria Jiang | d89b7055 | 2018-02-22 05:36:26 | [diff] [blame] | 84 | void SubmitCompositorFrame( |
| 85 | const viz::LocalSurfaceId& local_surface_id, |
| 86 | viz::CompositorFrame frame, |
danakj | 54af81a | 2018-05-24 23:59:02 | [diff] [blame] | 87 | base::Optional<viz::HitTestRegionList> hit_test_region_list); |
Fady Samuel | c296f5fb | 2017-07-21 04:02:19 | [diff] [blame] | 88 | void DidNotProduceFrame(const viz::BeginFrameAck& ack); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 89 | |
Fady Samuel | ac325ce | 2018-06-01 15:23:27 | [diff] [blame] | 90 | // FrameEvictorClient implementation. |
| 91 | void EvictDelegatedFrame() override; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 92 | |
akaba | f2624f2 | 2018-08-21 17:11:01 | [diff] [blame] | 93 | // Advances the fallback surface to the first surface after navigation. This |
| 94 | // ensures that stale surfaces are not presented to the user for an indefinite |
| 95 | // period of time. |
| 96 | void ResetFallbackToFirstNavigationSurface(); |
| 97 | |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 98 | bool HasDelegatedContent() const; |
| 99 | |
Fady Samuel | bcac6f0 | 2018-07-11 02:02:42 | [diff] [blame] | 100 | cc::SurfaceLayer* content_layer_for_testing() { return content_layer_.get(); } |
| 101 | |
Fady Samuel | 0c2f5b8 | 2018-07-19 00:33:46 | [diff] [blame] | 102 | const viz::FrameSinkId& GetFrameSinkId() const; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 103 | |
Yuri Wiitala | b9ad27a | 2017-09-06 19:13:50 | [diff] [blame] | 104 | // Should only be called when the host has a content layer. Use this for one- |
| 105 | // off screen capture, not for video. Always provides RGBA_BITMAP |
| 106 | // CopyOutputResults. |
Yuri Wiitala | 419ed0f | 2018-02-22 23:21:38 | [diff] [blame] | 107 | void CopyFromCompositingSurface( |
| 108 | const gfx::Rect& src_subrect, |
| 109 | const gfx::Size& output_size, |
| 110 | base::OnceCallback<void(const SkBitmap&)> callback); |
| 111 | bool CanCopyFromCompositingSurface() const; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 112 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 113 | void CompositorFrameSinkChanged(); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 114 | |
enne | a487a27 | 2016-09-30 19:56:18 | [diff] [blame] | 115 | // Called when this DFH is attached/detached from a parent browser compositor |
| 116 | // and needs to be attached to the surface hierarchy. |
staraz | aa23111 | 2017-02-07 17:53:24 | [diff] [blame] | 117 | void AttachToCompositor(WindowAndroidCompositor* compositor); |
| 118 | void DetachFromCompositor(); |
enne | a487a27 | 2016-09-30 19:56:18 | [diff] [blame] | 119 | |
Fady Samuel | ac325ce | 2018-06-01 15:23:27 | [diff] [blame] | 120 | bool IsPrimarySurfaceEvicted() const; |
| 121 | bool HasSavedFrame() const; |
| 122 | void WasHidden(); |
| 123 | void WasShown(const viz::LocalSurfaceId& local_surface_id, |
| 124 | const gfx::Size& size_in_pixels); |
Fady Samuel | dba483c | 2018-05-30 04:42:35 | [diff] [blame] | 125 | void EmbedSurface(const viz::LocalSurfaceId& new_pending_local_surface_id, |
| 126 | const gfx::Size& new_pending_size_in_pixels, |
| 127 | cc::DeadlinePolicy deadline_policy); |
Eric Karl | baa55ff3 | 2018-01-18 00:46:11 | [diff] [blame] | 128 | |
Eric Karl | b8823c31 | 2018-05-09 03:08:39 | [diff] [blame] | 129 | // Called when we begin a resize operation. Takes the compositor lock until we |
| 130 | // receive a frame of the expected size. |
| 131 | void PixelSizeWillChange(const gfx::Size& pixel_size); |
| 132 | |
Eric Karl | baa55ff3 | 2018-01-18 00:46:11 | [diff] [blame] | 133 | // Returns the ID for the current Surface. Returns an invalid ID if no |
| 134 | // surface exists (!HasDelegatedContent()). |
Fady Samuel | 120c7e2 | 2018-07-20 05:19:28 | [diff] [blame] | 135 | viz::SurfaceId SurfaceId() const; |
Xu Xing | 9ef5a88 | 2018-08-21 00:11:20 | [diff] [blame] | 136 | bool HasFallbackSurface() const; |
Eric Karl | baa55ff3 | 2018-01-18 00:46:11 | [diff] [blame] | 137 | |
Saman Sami | 26a1fcd | 2018-04-10 17:12:21 | [diff] [blame] | 138 | void TakeFallbackContentFrom(DelegatedFrameHostAndroid* other); |
| 139 | |
Eric Karl | db7eb24 | 2018-05-16 17:16:59 | [diff] [blame] | 140 | void DidNavigate(); |
| 141 | |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 142 | private: |
kylechar | 9075b08 | 2017-09-12 16:59:10 | [diff] [blame] | 143 | // viz::mojom::CompositorFrameSinkClient implementation. |
samans | 8d3604f | 2017-04-11 22:15:30 | [diff] [blame] | 144 | void DidReceiveCompositorFrameAck( |
Fady Samuel | 4f7f0fb3 | 2017-07-28 15:33:37 | [diff] [blame] | 145 | const std::vector<viz::ReturnedResource>& resources) override; |
Sadrul Habib Chowdhury | b62a9730 | 2018-06-06 03:33:48 | [diff] [blame] | 146 | void DidPresentCompositorFrame( |
| 147 | uint32_t presentation_token, |
| 148 | const gfx::PresentationFeedback& feedback) override; |
Fady Samuel | c296f5fb | 2017-07-21 04:02:19 | [diff] [blame] | 149 | void OnBeginFrame(const viz::BeginFrameArgs& args) override; |
danakj | 30464ba1 | 2017-06-29 21:26:45 | [diff] [blame] | 150 | void ReclaimResources( |
Fady Samuel | 4f7f0fb3 | 2017-07-28 15:33:37 | [diff] [blame] | 151 | const std::vector<viz::ReturnedResource>& resources) override; |
Bill Orr | 372a126 | 2017-07-20 00:18:39 | [diff] [blame] | 152 | void OnBeginFramePausedChanged(bool paused) override; |
samans | b779eca | 2017-03-02 22:16:37 | [diff] [blame] | 153 | |
Fady Samuel | c645ffe | 2017-07-24 17:28:20 | [diff] [blame] | 154 | // viz::ExternalBeginFrameSourceClient implementation. |
samans | b779eca | 2017-03-02 22:16:37 | [diff] [blame] | 155 | void OnNeedsBeginFrames(bool needs_begin_frames) override; |
samans | b779eca | 2017-03-02 22:16:37 | [diff] [blame] | 156 | |
Fady Samuel | bac0f1a | 2017-08-02 15:54:02 | [diff] [blame] | 157 | // viz::HostFrameSinkClient implementation. |
Fady Samuel | 5b7fb8e | 2017-08-08 16:58:22 | [diff] [blame] | 158 | void OnFirstSurfaceActivation(const viz::SurfaceInfo& surface_info) override; |
yiyix | ae38c33 | 2017-11-14 18:58:27 | [diff] [blame] | 159 | void OnFrameTokenChanged(uint32_t frame_token) override; |
Fady Samuel | bac0f1a | 2017-08-02 15:54:02 | [diff] [blame] | 160 | |
Eric Karl | 465a525 | 2018-01-10 21:16:27 | [diff] [blame] | 161 | // ui::CompositorLockClient implementation. |
| 162 | void CompositorLockTimedOut() override; |
| 163 | |
samans | b779eca | 2017-03-02 22:16:37 | [diff] [blame] | 164 | void CreateNewCompositorFrameSinkSupport(); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 165 | |
Eric Karl | 5479bee | 2018-08-24 23:10:28 | [diff] [blame^] | 166 | void ProcessCopyOutputRequest( |
| 167 | std::unique_ptr<viz::CopyOutputRequest> request); |
| 168 | |
Fady Samuel | d5c2618 | 2017-07-12 02:43:33 | [diff] [blame] | 169 | const viz::FrameSinkId frame_sink_id_; |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 170 | |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 171 | ViewAndroid* view_; |
| 172 | |
kylechar | a090016 | 2017-07-14 17:35:25 | [diff] [blame] | 173 | viz::HostFrameSinkManager* const host_frame_sink_manager_; |
staraz | aa23111 | 2017-02-07 17:53:24 | [diff] [blame] | 174 | WindowAndroidCompositor* registered_parent_compositor_ = nullptr; |
eseckler | 8c15fc3 | 2016-12-20 20:22:20 | [diff] [blame] | 175 | Client* client_; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 176 | |
danakj | c391f33 | 2017-07-12 20:45:52 | [diff] [blame] | 177 | std::unique_ptr<viz::CompositorFrameSinkSupport> support_; |
Fady Samuel | c645ffe | 2017-07-24 17:28:20 | [diff] [blame] | 178 | viz::ExternalBeginFrameSource begin_frame_source_; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 179 | |
samans | e7398c55 | 2017-03-21 21:36:28 | [diff] [blame] | 180 | bool has_transparent_background_ = false; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 181 | |
| 182 | scoped_refptr<cc::SurfaceLayer> content_layer_; |
| 183 | |
Eric Karl | baa55ff3 | 2018-01-18 00:46:11 | [diff] [blame] | 184 | const bool enable_surface_synchronization_; |
Eric Karl | db7eb24 | 2018-05-16 17:16:59 | [diff] [blame] | 185 | const bool enable_viz_; |
Eric Karl | baa55ff3 | 2018-01-18 00:46:11 | [diff] [blame] | 186 | |
Eric Karl | b8823c31 | 2018-05-09 03:08:39 | [diff] [blame] | 187 | // The size we are resizing to. Once we receive a frame of this size we can |
| 188 | // release any resize compositor lock. |
| 189 | gfx::Size expected_pixel_size_; |
| 190 | |
Eric Karl | 465a525 | 2018-01-10 21:16:27 | [diff] [blame] | 191 | // A lock that is held from the point at which we attach to the compositor to |
| 192 | // the point at which we submit our first frame to the compositor. This |
| 193 | // ensures that the compositor doesn't swap without a frame available. |
| 194 | std::unique_ptr<ui::CompositorLock> compositor_attach_until_frame_lock_; |
| 195 | |
Eric Karl | b8823c31 | 2018-05-09 03:08:39 | [diff] [blame] | 196 | // A lock that is held from the point we begin resizing this frame to the |
| 197 | // point at which we receive a frame of the correct size. |
| 198 | std::unique_ptr<ui::CompositorLock> compositor_pending_resize_lock_; |
| 199 | |
Eric Karl | db7eb24 | 2018-05-16 17:16:59 | [diff] [blame] | 200 | // Whether we've received a frame from the renderer since navigating. |
Fady Samuel | dba483c | 2018-05-30 04:42:35 | [diff] [blame] | 201 | // Only used when surface synchronization is on. |
akaba | f2624f2 | 2018-08-21 17:11:01 | [diff] [blame] | 202 | viz::LocalSurfaceId first_local_surface_id_after_navigation_; |
Eric Karl | db7eb24 | 2018-05-16 17:16:59 | [diff] [blame] | 203 | bool received_frame_after_navigation_ = false; |
Fady Samuel | dba483c | 2018-05-30 04:42:35 | [diff] [blame] | 204 | |
Eric Karl | 5479bee | 2018-08-24 23:10:28 | [diff] [blame^] | 205 | std::vector<std::unique_ptr<viz::CopyOutputRequest>> |
| 206 | pending_first_frame_requests_; |
| 207 | |
| 208 | // The surface id that was most recently activated by |
| 209 | // OnFirstSurfaceActivation. |
| 210 | viz::LocalSurfaceId active_local_surface_id_; |
| 211 | // The scale factor of the above surface. |
| 212 | float active_device_scale_factor_ = 0.f; |
| 213 | |
Fady Samuel | dba483c | 2018-05-30 04:42:35 | [diff] [blame] | 214 | // The local surface id as of the most recent call to |
| 215 | // EmbedSurface. This is the surface that we expect future frames to |
| 216 | // reference. This will eventually equal the active surface. |
| 217 | viz::LocalSurfaceId pending_local_surface_id_; |
Eric Karl | db7eb24 | 2018-05-16 17:16:59 | [diff] [blame] | 218 | |
Fady Samuel | ac325ce | 2018-06-01 15:23:27 | [diff] [blame] | 219 | // The size of the above surface (updated at the same time). |
| 220 | gfx::Size pending_surface_size_in_pixels_; |
| 221 | |
| 222 | std::unique_ptr<viz::FrameEvictor> frame_evictor_; |
| 223 | |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 224 | DISALLOW_COPY_AND_ASSIGN(DelegatedFrameHostAndroid); |
| 225 | }; |
| 226 | |
| 227 | } // namespace ui |
| 228 | |
| 229 | #endif // UI_ANDROID_DELEGATED_FRAME_HOST_ANDROID_H_ |