Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / content / browser / compositor / delegated_frame_host.h
blob5af6da420c037ab8ad9139ec5a2f0d97f3589ebd
1 // Copyright 2014 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.
5 #ifndef CONTENT_BROWSER_COMPOSITOR_DELEGATED_FRAME_HOST_H_
6 #define CONTENT_BROWSER_COMPOSITOR_DELEGATED_FRAME_HOST_H_
8 #include "cc/layers/delegated_frame_provider.h"
9 #include "cc/layers/delegated_frame_resource_collection.h"
10 #include "cc/output/copy_output_result.h"
11 #include "cc/surfaces/surface_factory_client.h"
12 #include "content/browser/compositor/image_transport_factory.h"
13 #include "content/browser/compositor/owned_mailbox.h"
14 #include "content/browser/renderer_host/delegated_frame_evictor.h"
15 #include "content/browser/renderer_host/dip_util.h"
16 #include "content/browser/renderer_host/render_widget_host_impl.h"
17 #include "content/browser/renderer_host/render_widget_host_view_base.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "ui/compositor/compositor.h"
20 #include "ui/compositor/compositor_observer.h"
21 #include "ui/compositor/compositor_vsync_manager.h"
22 #include "ui/compositor/layer.h"
23 #include "ui/compositor/layer_owner_delegate.h"
24 #include "ui/gfx/geometry/rect_conversions.h"
26 namespace base {
27 class TickClock;
30 namespace cc {
31 class SurfaceFactory;
32 enum class SurfaceDrawStatus;
35 namespace media {
36 class VideoFrame;
39 namespace content {
41 class DelegatedFrameHost;
42 class ReadbackYUVInterface;
43 class RenderWidgetHostViewFrameSubscriber;
44 class RenderWidgetHostImpl;
45 class ResizeLock;
47 // The DelegatedFrameHostClient is the interface from the DelegatedFrameHost,
48 // which manages delegated frames, and the ui::Compositor being used to
49 // display them.
50 class CONTENT_EXPORT DelegatedFrameHostClient {
51 public:
52 virtual ui::Layer* DelegatedFrameHostGetLayer() const = 0;
53 virtual bool DelegatedFrameHostIsVisible() const = 0;
54 virtual gfx::Size DelegatedFrameHostDesiredSizeInDIP() const = 0;
56 virtual bool DelegatedFrameCanCreateResizeLock() const = 0;
57 virtual scoped_ptr<ResizeLock> DelegatedFrameHostCreateResizeLock(
58 bool defer_compositor_lock) = 0;
59 virtual void DelegatedFrameHostResizeLockWasReleased() = 0;
61 virtual void DelegatedFrameHostSendCompositorSwapAck(
62 int output_surface_id,
63 const cc::CompositorFrameAck& ack) = 0;
64 virtual void DelegatedFrameHostSendReclaimCompositorResources(
65 int output_surface_id,
66 const cc::CompositorFrameAck& ack) = 0;
67 virtual void DelegatedFrameHostOnLostCompositorResources() = 0;
69 virtual void DelegatedFrameHostUpdateVSyncParameters(
70 const base::TimeTicks& timebase,
71 const base::TimeDelta& interval) = 0;
74 // The DelegatedFrameHost is used to host all of the RenderWidgetHostView state
75 // and functionality that is associated with delegated frames being sent from
76 // the RenderWidget. The DelegatedFrameHost will push these changes through to
77 // the ui::Compositor associated with its DelegatedFrameHostClient.
78 class CONTENT_EXPORT DelegatedFrameHost
79 : public ui::CompositorObserver,
80 public ui::CompositorVSyncManager::Observer,
81 public ui::LayerOwnerDelegate,
82 public ImageTransportFactoryObserver,
83 public DelegatedFrameEvictorClient,
84 public cc::DelegatedFrameResourceCollectionClient,
85 public cc::SurfaceFactoryClient,
86 public base::SupportsWeakPtr<DelegatedFrameHost> {
87 public:
88 DelegatedFrameHost(DelegatedFrameHostClient* client);
89 ~DelegatedFrameHost() override;
91 // ui::CompositorObserver implementation.
92 void OnCompositingDidCommit(ui::Compositor* compositor) override;
93 void OnCompositingStarted(ui::Compositor* compositor,
94 base::TimeTicks start_time) override;
95 void OnCompositingEnded(ui::Compositor* compositor) override;
96 void OnCompositingAborted(ui::Compositor* compositor) override;
97 void OnCompositingLockStateChanged(ui::Compositor* compositor) override;
98 void OnCompositingShuttingDown(ui::Compositor* compositor) override;
100 // ui::CompositorVSyncManager::Observer implementation.
101 void OnUpdateVSyncParameters(base::TimeTicks timebase,
102 base::TimeDelta interval) override;
104 // ui::LayerOwnerObserver implementation.
105 void OnLayerRecreated(ui::Layer* old_layer, ui::Layer* new_layer) override;
107 // ImageTransportFactoryObserver implementation.
108 void OnLostResources() override;
110 // DelegatedFrameEvictorClient implementation.
111 void EvictDelegatedFrame() override;
113 // cc::DelegatedFrameProviderClient implementation.
114 void UnusedResourcesAreAvailable() override;
116 // cc::SurfaceFactoryClient implementation.
117 void ReturnResources(const cc::ReturnedResourceArray& resources) override;
119 bool CanCopyToBitmap() const;
121 // Public interface exposed to RenderWidgetHostView.
123 // Note: |satisfies_sequences| is cleared in calls to this function.
124 void SwapDelegatedFrame(
125 uint32 output_surface_id,
126 scoped_ptr<cc::DelegatedFrameData> frame_data,
127 float frame_device_scale_factor,
128 const std::vector<ui::LatencyInfo>& latency_info,
129 std::vector<uint32_t>* satisfies_sequences);
130 void ClearDelegatedFrame();
131 void WasHidden();
132 void WasShown(const ui::LatencyInfo& latency_info);
133 void WasResized();
134 bool HasSavedFrame();
135 gfx::Size GetRequestedRendererSize() const;
136 void SetCompositor(ui::Compositor* compositor);
137 void ResetCompositor();
138 void SetVSyncParameters(const base::TimeTicks& timebase,
139 const base::TimeDelta& interval);
140 // Note: |src_subset| is specified in DIP dimensions while |output_size|
141 // expects pixels.
142 void CopyFromCompositingSurface(const gfx::Rect& src_subrect,
143 const gfx::Size& output_size,
144 ReadbackRequestCallback& callback,
145 const SkColorType preferred_color_type);
146 void CopyFromCompositingSurfaceToVideoFrame(
147 const gfx::Rect& src_subrect,
148 const scoped_refptr<media::VideoFrame>& target,
149 const base::Callback<void(bool)>& callback);
150 bool CanCopyToVideoFrame() const;
151 void BeginFrameSubscription(
152 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber);
153 void EndFrameSubscription();
154 bool HasFrameSubscriber() const { return frame_subscriber_; }
155 uint32_t GetSurfaceIdNamespace();
156 // Returns a null SurfaceId if this DelegatedFrameHost has not yet created
157 // a compositor Surface.
158 cc::SurfaceId SurfaceIdAtPoint(const gfx::Point& point,
159 gfx::Point* transformed_point);
161 // Exposed for tests.
162 cc::DelegatedFrameProvider* FrameProviderForTesting() const {
163 return frame_provider_.get();
165 cc::SurfaceId SurfaceIdForTesting() const { return surface_id_; }
166 void OnCompositingDidCommitForTesting(ui::Compositor* compositor) {
167 OnCompositingDidCommit(compositor);
169 bool ReleasedFrontLockActiveForTesting() const {
170 return !!released_front_lock_.get();
172 void SetRequestCopyOfOutputCallbackForTesting(
173 const base::Callback<void(scoped_ptr<cc::CopyOutputRequest>)>& callback) {
174 request_copy_of_output_callback_for_testing_ = callback;
177 private:
178 friend class DelegatedFrameHostClient;
179 friend class RenderWidgetHostViewAuraCopyRequestTest;
180 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
181 SkippedDelegatedFrames);
182 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
183 DiscardDelegatedFramesWithLocking);
185 RenderWidgetHostViewFrameSubscriber* frame_subscriber() const {
186 return frame_subscriber_.get();
188 bool ShouldCreateResizeLock();
189 void LockResources();
190 void UnlockResources();
191 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
193 bool ShouldSkipFrame(gfx::Size size_in_dip) const;
195 // Lazily grab a resize lock if the aura window size doesn't match the current
196 // frame size, to give time to the renderer.
197 void MaybeCreateResizeLock();
199 // Checks if the resize lock can be released because we received an new frame.
200 void CheckResizeLock();
202 // Run all on compositing commit callbacks.
203 void RunOnCommitCallbacks();
205 // Add on compositing commit callback.
206 void AddOnCommitCallbackAndDisableLocks(const base::Closure& callback);
208 // Called after async thumbnailer task completes. Scales and crops the result
209 // of the copy.
210 static void CopyFromCompositingSurfaceHasResult(
211 const gfx::Size& dst_size_in_pixel,
212 const SkColorType color_type,
213 ReadbackRequestCallback& callback,
214 scoped_ptr<cc::CopyOutputResult> result);
215 static void PrepareTextureCopyOutputResult(
216 const gfx::Size& dst_size_in_pixel,
217 const SkColorType color_type,
218 ReadbackRequestCallback& callback,
219 scoped_ptr<cc::CopyOutputResult> result);
220 static void PrepareBitmapCopyOutputResult(
221 const gfx::Size& dst_size_in_pixel,
222 const SkColorType color_type,
223 ReadbackRequestCallback& callback,
224 scoped_ptr<cc::CopyOutputResult> result);
225 static void CopyFromCompositingSurfaceHasResultForVideo(
226 base::WeakPtr<DelegatedFrameHost> rwhva,
227 scoped_refptr<OwnedMailbox> subscriber_texture,
228 scoped_refptr<media::VideoFrame> video_frame,
229 const base::Callback<void(bool)>& callback,
230 scoped_ptr<cc::CopyOutputResult> result);
231 static void CopyFromCompositingSurfaceFinishedForVideo(
232 base::WeakPtr<DelegatedFrameHost> rwhva,
233 const base::Callback<void(bool)>& callback,
234 scoped_refptr<OwnedMailbox> subscriber_texture,
235 scoped_ptr<cc::SingleReleaseCallback> release_callback,
236 bool result);
237 static void ReturnSubscriberTexture(
238 base::WeakPtr<DelegatedFrameHost> rwhva,
239 scoped_refptr<OwnedMailbox> subscriber_texture,
240 uint32 sync_point);
242 void SendDelegatedFrameAck(uint32 output_surface_id);
243 void SurfaceDrawn(uint32 output_surface_id, cc::SurfaceDrawStatus drawn);
244 void SendReturnedDelegatedResources(uint32 output_surface_id);
246 // Called to consult the current |frame_subscriber_|, to determine and maybe
247 // initiate a copy-into-video-frame request.
248 void DidReceiveFrameFromRenderer(const gfx::Rect& damage_rect);
250 DelegatedFrameHostClient* const client_;
251 ui::Compositor* compositor_;
253 // True if this renders into a Surface, false if it renders into a delegated
254 // layer.
255 bool use_surfaces_;
257 std::vector<base::Closure> on_compositing_did_commit_callbacks_;
259 // The vsync manager we are observing for changes, if any.
260 scoped_refptr<ui::CompositorVSyncManager> vsync_manager_;
262 // The current VSync timebase and interval. These are zero until the first
263 // call to SetVSyncParameters().
264 base::TimeTicks vsync_timebase_;
265 base::TimeDelta vsync_interval_;
267 // Overridable tick clock used for testing functions using current time.
268 scoped_ptr<base::TickClock> tick_clock_;
270 // With delegated renderer, this is the last output surface, used to
271 // disambiguate resources with the same id coming from different output
272 // surfaces.
273 uint32 last_output_surface_id_;
275 // The number of delegated frame acks that are pending, to delay resource
276 // returns until the acks are sent.
277 int pending_delegated_ack_count_;
279 // True after a delegated frame has been skipped, until a frame is not
280 // skipped.
281 bool skipped_frames_;
282 std::vector<ui::LatencyInfo> skipped_latency_info_list_;
284 // Holds delegated resources that have been given to a DelegatedFrameProvider,
285 // and gives back resources when they are no longer in use for return to the
286 // renderer.
287 scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
289 // Provides delegated frame updates to the cc::DelegatedRendererLayer.
290 scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
292 // State for rendering into a Surface.
293 scoped_ptr<cc::SurfaceIdAllocator> id_allocator_;
294 scoped_ptr<cc::SurfaceFactory> surface_factory_;
295 cc::SurfaceId surface_id_;
296 gfx::Size current_surface_size_;
297 float current_scale_factor_;
298 cc::ReturnedResourceArray surface_returned_resources_;
300 // This lock is the one waiting for a frame of the right size to come back
301 // from the renderer/GPU process. It is set from the moment the aura window
302 // got resized, to the moment we committed the renderer frame of the same
303 // size. It keeps track of the size we expect from the renderer, and locks the
304 // compositor, as well as the UI for a short time to give a chance to the
305 // renderer of producing a frame of the right size.
306 scoped_ptr<ResizeLock> resize_lock_;
308 // Keeps track of the current frame size.
309 gfx::Size current_frame_size_in_dip_;
311 // This lock is for waiting for a front surface to become available to draw.
312 scoped_refptr<ui::CompositorLock> released_front_lock_;
314 enum CanLockCompositorState {
315 YES_CAN_LOCK,
316 // We locked, so at some point we'll need to kick a frame.
317 YES_DID_LOCK,
318 // No. A lock timed out, we need to kick a new frame before locking again.
319 NO_PENDING_RENDERER_FRAME,
320 // No. We've got a frame, but it hasn't been committed.
321 NO_PENDING_COMMIT,
323 CanLockCompositorState can_lock_compositor_;
325 base::TimeTicks last_draw_ended_;
327 // Subscriber that listens to frame presentation events.
328 scoped_ptr<RenderWidgetHostViewFrameSubscriber> frame_subscriber_;
329 std::vector<scoped_refptr<OwnedMailbox> > idle_frame_subscriber_textures_;
331 // Callback used to pass the output request to the layer or to a function
332 // specified by a test.
333 base::Callback<void(scoped_ptr<cc::CopyOutputRequest>)>
334 request_copy_of_output_callback_for_testing_;
336 // YUV readback pipeline.
337 scoped_ptr<content::ReadbackYUVInterface>
338 yuv_readback_pipeline_;
340 scoped_ptr<DelegatedFrameEvictor> delegated_frame_evictor_;
343 } // namespace content
345 #endif // CONTENT_BROWSER_COMPOSITOR_DELEGATED_FRAME_HOST_H_