blob: 799494b64217a39a21a507f16cfbdebfd4e19535 [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"
Fady Samueldba483c2018-05-30 04:42:3510#include "cc/layers/deadline_policy.h"
Fady Samuelac325ce2018-06-01 15:23:2711#include "components/viz/client/frame_evictor.h"
danakjf20f4502017-09-26 17:13:3112#include "components/viz/common/frame_sinks/copy_output_request.h"
Fady Samuel4f7f0fb32017-07-28 15:33:3713#include "components/viz/common/resources/returned_resource.h"
Fady Samuel1a21156e2017-07-13 04:57:2914#include "components/viz/common/surfaces/surface_info.h"
Fady Samuelbac0f1a2017-08-02 15:54:0215#include "components/viz/host/host_frame_sink_client.h"
danakjc391f332017-07-12 20:45:5216#include "components/viz/service/frame_sinks/compositor_frame_sink_support.h"
kylechar9075b082017-09-12 16:59:1017#include "services/viz/public/interfaces/compositing/compositor_frame_sink.mojom.h"
khushalsagara693aa72016-08-16 22:18:0618#include "ui/android/ui_android_export.h"
Eric Karl465a5252018-01-10 21:16:2719#include "ui/compositor/compositor_lock.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 {
danakj5e0a12b2017-09-25 17:26:4927class CompositorFrame;
kylechara0900162017-07-14 17:35:2528class HostFrameSinkManager;
29} // namespace viz
30
khushalsagara693aa72016-08-16 22:18:0631namespace ui {
32class ViewAndroid;
33class WindowAndroidCompositor;
34
35class UI_ANDROID_EXPORT DelegatedFrameHostAndroid
kylechar9075b082017-09-12 16:59:1036 : public viz::mojom::CompositorFrameSinkClient,
Fady Samuelbac0f1a2017-08-02 15:54:0237 public viz::ExternalBeginFrameSourceClient,
Eric Karl465a5252018-01-10 21:16:2738 public viz::HostFrameSinkClient,
Fady Samuelac325ce2018-06-01 15:23:2739 public ui::CompositorLockClient,
40 public viz::FrameEvictorClient {
khushalsagara693aa72016-08-16 22:18:0641 public:
eseckler8c15fc32016-12-20 20:22:2042 class Client {
43 public:
44 virtual void SetBeginFrameSource(
Fady Samuelc645ffe2017-07-24 17:28:2045 viz::BeginFrameSource* begin_frame_source) = 0;
Sadrul Habib Chowdhuryb62a97302018-06-06 03:33:4846 virtual void DidPresentCompositorFrame(
47 uint32_t presentation_token,
48 const gfx::PresentationFeedback& feedback) = 0;
Fady Samuelf3982a32018-06-14 20:52:1349 virtual void DidReceiveCompositorFrameAck(
50 const std::vector<viz::ReturnedResource>& resources) = 0;
Fady Samuel4f7f0fb32017-07-28 15:33:3751 virtual void ReclaimResources(
Fady Samuelf3982a32018-06-14 20:52:1352 const std::vector<viz::ReturnedResource>& resources) = 0;
yiyixae38c332017-11-14 18:58:2753 virtual void OnFrameTokenChanged(uint32_t frame_token) = 0;
eseckler8c15fc32016-12-20 20:22:2054 };
khushalsagara693aa72016-08-16 22:18:0655
56 DelegatedFrameHostAndroid(ViewAndroid* view,
kylechara0900162017-07-14 17:35:2557 viz::HostFrameSinkManager* host_frame_sink_manager,
xlai9351829f2017-01-27 18:39:1358 Client* client,
Fady Samuel3b77b8a2018-06-29 06:53:5359 const viz::FrameSinkId& frame_sink_id,
60 bool enable_surface_synchronization);
khushalsagara693aa72016-08-16 22:18:0661
62 ~DelegatedFrameHostAndroid() override;
63
Fady Samuel99ec37a2018-07-16 21:43:4764 // 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 Jiangd89b70552018-02-22 05:36:2684 void SubmitCompositorFrame(
85 const viz::LocalSurfaceId& local_surface_id,
86 viz::CompositorFrame frame,
danakj54af81a2018-05-24 23:59:0287 base::Optional<viz::HitTestRegionList> hit_test_region_list);
Fady Samuelc296f5fb2017-07-21 04:02:1988 void DidNotProduceFrame(const viz::BeginFrameAck& ack);
khushalsagara693aa72016-08-16 22:18:0689
Fady Samuelac325ce2018-06-01 15:23:2790 // FrameEvictorClient implementation.
91 void EvictDelegatedFrame() override;
khushalsagara693aa72016-08-16 22:18:0692
akabaf2624f22018-08-21 17:11:0193 // 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
khushalsagara693aa72016-08-16 22:18:0698 bool HasDelegatedContent() const;
99
Fady Samuelbcac6f02018-07-11 02:02:42100 cc::SurfaceLayer* content_layer_for_testing() { return content_layer_.get(); }
101
Fady Samuel0c2f5b82018-07-19 00:33:46102 const viz::FrameSinkId& GetFrameSinkId() const;
khushalsagara693aa72016-08-16 22:18:06103
Yuri Wiitalab9ad27a2017-09-06 19:13:50104 // 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 Wiitala419ed0f2018-02-22 23:21:38107 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;
khushalsagara693aa72016-08-16 22:18:06112
danakj1120f4c2016-09-15 02:05:32113 void CompositorFrameSinkChanged();
khushalsagara693aa72016-08-16 22:18:06114
ennea487a272016-09-30 19:56:18115 // Called when this DFH is attached/detached from a parent browser compositor
116 // and needs to be attached to the surface hierarchy.
starazaa231112017-02-07 17:53:24117 void AttachToCompositor(WindowAndroidCompositor* compositor);
118 void DetachFromCompositor();
ennea487a272016-09-30 19:56:18119
Fady Samuelac325ce2018-06-01 15:23:27120 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 Samueldba483c2018-05-30 04:42:35125 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 Karlbaa55ff32018-01-18 00:46:11128
Eric Karlb8823c312018-05-09 03:08:39129 // 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 Karlbaa55ff32018-01-18 00:46:11133 // Returns the ID for the current Surface. Returns an invalid ID if no
134 // surface exists (!HasDelegatedContent()).
Fady Samuel120c7e22018-07-20 05:19:28135 viz::SurfaceId SurfaceId() const;
Xu Xing9ef5a882018-08-21 00:11:20136 bool HasFallbackSurface() const;
Eric Karlbaa55ff32018-01-18 00:46:11137
Saman Sami26a1fcd2018-04-10 17:12:21138 void TakeFallbackContentFrom(DelegatedFrameHostAndroid* other);
139
Eric Karldb7eb242018-05-16 17:16:59140 void DidNavigate();
141
khushalsagara693aa72016-08-16 22:18:06142 private:
kylechar9075b082017-09-12 16:59:10143 // viz::mojom::CompositorFrameSinkClient implementation.
samans8d3604f2017-04-11 22:15:30144 void DidReceiveCompositorFrameAck(
Fady Samuel4f7f0fb32017-07-28 15:33:37145 const std::vector<viz::ReturnedResource>& resources) override;
Sadrul Habib Chowdhuryb62a97302018-06-06 03:33:48146 void DidPresentCompositorFrame(
147 uint32_t presentation_token,
148 const gfx::PresentationFeedback& feedback) override;
Fady Samuelc296f5fb2017-07-21 04:02:19149 void OnBeginFrame(const viz::BeginFrameArgs& args) override;
danakj30464ba12017-06-29 21:26:45150 void ReclaimResources(
Fady Samuel4f7f0fb32017-07-28 15:33:37151 const std::vector<viz::ReturnedResource>& resources) override;
Bill Orr372a1262017-07-20 00:18:39152 void OnBeginFramePausedChanged(bool paused) override;
samansb779eca2017-03-02 22:16:37153
Fady Samuelc645ffe2017-07-24 17:28:20154 // viz::ExternalBeginFrameSourceClient implementation.
samansb779eca2017-03-02 22:16:37155 void OnNeedsBeginFrames(bool needs_begin_frames) override;
samansb779eca2017-03-02 22:16:37156
Fady Samuelbac0f1a2017-08-02 15:54:02157 // viz::HostFrameSinkClient implementation.
Fady Samuel5b7fb8e2017-08-08 16:58:22158 void OnFirstSurfaceActivation(const viz::SurfaceInfo& surface_info) override;
yiyixae38c332017-11-14 18:58:27159 void OnFrameTokenChanged(uint32_t frame_token) override;
Fady Samuelbac0f1a2017-08-02 15:54:02160
Eric Karl465a5252018-01-10 21:16:27161 // ui::CompositorLockClient implementation.
162 void CompositorLockTimedOut() override;
163
samansb779eca2017-03-02 22:16:37164 void CreateNewCompositorFrameSinkSupport();
khushalsagara693aa72016-08-16 22:18:06165
Eric Karl5479bee2018-08-24 23:10:28166 void ProcessCopyOutputRequest(
167 std::unique_ptr<viz::CopyOutputRequest> request);
168
Fady Samueld5c26182017-07-12 02:43:33169 const viz::FrameSinkId frame_sink_id_;
fsamuelb6acafa2016-10-04 03:21:52170
khushalsagara693aa72016-08-16 22:18:06171 ViewAndroid* view_;
172
kylechara0900162017-07-14 17:35:25173 viz::HostFrameSinkManager* const host_frame_sink_manager_;
starazaa231112017-02-07 17:53:24174 WindowAndroidCompositor* registered_parent_compositor_ = nullptr;
eseckler8c15fc32016-12-20 20:22:20175 Client* client_;
khushalsagara693aa72016-08-16 22:18:06176
danakjc391f332017-07-12 20:45:52177 std::unique_ptr<viz::CompositorFrameSinkSupport> support_;
Fady Samuelc645ffe2017-07-24 17:28:20178 viz::ExternalBeginFrameSource begin_frame_source_;
khushalsagara693aa72016-08-16 22:18:06179
samanse7398c552017-03-21 21:36:28180 bool has_transparent_background_ = false;
khushalsagara693aa72016-08-16 22:18:06181
182 scoped_refptr<cc::SurfaceLayer> content_layer_;
183
Eric Karlbaa55ff32018-01-18 00:46:11184 const bool enable_surface_synchronization_;
Eric Karldb7eb242018-05-16 17:16:59185 const bool enable_viz_;
Eric Karlbaa55ff32018-01-18 00:46:11186
Eric Karlb8823c312018-05-09 03:08:39187 // 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 Karl465a5252018-01-10 21:16:27191 // 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 Karlb8823c312018-05-09 03:08:39196 // 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 Karldb7eb242018-05-16 17:16:59200 // Whether we've received a frame from the renderer since navigating.
Fady Samueldba483c2018-05-30 04:42:35201 // Only used when surface synchronization is on.
akabaf2624f22018-08-21 17:11:01202 viz::LocalSurfaceId first_local_surface_id_after_navigation_;
Eric Karldb7eb242018-05-16 17:16:59203 bool received_frame_after_navigation_ = false;
Fady Samueldba483c2018-05-30 04:42:35204
Eric Karl5479bee2018-08-24 23:10:28205 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 Samueldba483c2018-05-30 04:42:35214 // 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 Karldb7eb242018-05-16 17:16:59218
Fady Samuelac325ce2018-06-01 15:23:27219 // 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
khushalsagara693aa72016-08-16 22:18:06224 DISALLOW_COPY_AND_ASSIGN(DelegatedFrameHostAndroid);
225};
226
227} // namespace ui
228
229#endif // UI_ANDROID_DELEGATED_FRAME_HOST_ANDROID_H_