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"
28 enum class SurfaceDrawStatus
;
37 class DelegatedFrameHost
;
38 class ReadbackYUVInterface
;
39 class RenderWidgetHostViewFrameSubscriber
;
40 class RenderWidgetHostImpl
;
43 // The DelegatedFrameHostClient is the interface from the DelegatedFrameHost,
44 // which manages delegated frames, and the ui::Compositor being used to
46 class CONTENT_EXPORT DelegatedFrameHostClient
{
48 virtual ui::Layer
* DelegatedFrameHostGetLayer() const = 0;
49 virtual bool DelegatedFrameHostIsVisible() const = 0;
50 virtual gfx::Size
DelegatedFrameHostDesiredSizeInDIP() const = 0;
52 virtual bool DelegatedFrameCanCreateResizeLock() const = 0;
53 virtual scoped_ptr
<ResizeLock
> DelegatedFrameHostCreateResizeLock(
54 bool defer_compositor_lock
) = 0;
55 virtual void DelegatedFrameHostResizeLockWasReleased() = 0;
57 virtual void DelegatedFrameHostSendCompositorSwapAck(
58 int output_surface_id
,
59 const cc::CompositorFrameAck
& ack
) = 0;
60 virtual void DelegatedFrameHostSendReclaimCompositorResources(
61 int output_surface_id
,
62 const cc::CompositorFrameAck
& ack
) = 0;
63 virtual void DelegatedFrameHostOnLostCompositorResources() = 0;
65 virtual void DelegatedFrameHostUpdateVSyncParameters(
66 const base::TimeTicks
& timebase
,
67 const base::TimeDelta
& interval
) = 0;
70 // The DelegatedFrameHost is used to host all of the RenderWidgetHostView state
71 // and functionality that is associated with delegated frames being sent from
72 // the RenderWidget. The DelegatedFrameHost will push these changes through to
73 // the ui::Compositor associated with its DelegatedFrameHostClient.
74 class CONTENT_EXPORT DelegatedFrameHost
75 : public ui::CompositorObserver
,
76 public ui::CompositorVSyncManager::Observer
,
77 public ui::LayerOwnerDelegate
,
78 public ImageTransportFactoryObserver
,
79 public DelegatedFrameEvictorClient
,
80 public cc::DelegatedFrameResourceCollectionClient
,
81 public cc::SurfaceFactoryClient
,
82 public base::SupportsWeakPtr
<DelegatedFrameHost
> {
84 DelegatedFrameHost(DelegatedFrameHostClient
* client
);
85 ~DelegatedFrameHost() override
;
87 bool CanCopyToBitmap() const;
89 // Public interface exposed to RenderWidgetHostView.
90 void SwapDelegatedFrame(
91 uint32 output_surface_id
,
92 scoped_ptr
<cc::DelegatedFrameData
> frame_data
,
93 float frame_device_scale_factor
,
94 const std::vector
<ui::LatencyInfo
>& latency_info
);
96 void WasShown(const ui::LatencyInfo
& latency_info
);
99 gfx::Size
GetRequestedRendererSize() const;
100 void SetCompositor(ui::Compositor
* compositor
);
101 void ResetCompositor();
102 void CopyFromCompositingSurface(const gfx::Rect
& src_subrect
,
103 const gfx::Size
& output_size
,
104 ReadbackRequestCallback
& callback
,
105 const SkColorType color_type
);
106 void CopyFromCompositingSurfaceToVideoFrame(
107 const gfx::Rect
& src_subrect
,
108 const scoped_refptr
<media::VideoFrame
>& target
,
109 const base::Callback
<void(bool)>& callback
);
110 bool CanCopyToVideoFrame() const;
111 bool CanSubscribeFrame() const;
112 void BeginFrameSubscription(
113 scoped_ptr
<RenderWidgetHostViewFrameSubscriber
> subscriber
);
114 void EndFrameSubscription();
115 bool HasFrameSubscriber() const { return frame_subscriber_
; }
117 // Exposed for tests.
118 cc::DelegatedFrameProvider
* FrameProviderForTesting() const {
119 return frame_provider_
.get();
121 cc::SurfaceId
SurfaceIdForTesting() const { return surface_id_
; }
122 void OnCompositingDidCommitForTesting(ui::Compositor
* compositor
) {
123 OnCompositingDidCommit(compositor
);
125 bool ReleasedFrontLockActiveForTesting() const {
126 return !!released_front_lock_
.get();
128 void SetRequestCopyOfOutputCallbackForTesting(
129 const base::Callback
<void(scoped_ptr
<cc::CopyOutputRequest
>)>& callback
) {
130 request_copy_of_output_callback_for_testing_
= callback
;
134 friend class DelegatedFrameHostClient
;
135 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest
,
136 SkippedDelegatedFrames
);
137 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest
,
138 DiscardDelegatedFramesWithLocking
);
139 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraCopyRequestTest
,
140 DestroyedAfterCopyRequest
);
142 RenderWidgetHostViewFrameSubscriber
* frame_subscriber() const {
143 return frame_subscriber_
.get();
145 bool ShouldCreateResizeLock();
146 void LockResources();
147 void UnlockResources();
148 void RequestCopyOfOutput(scoped_ptr
<cc::CopyOutputRequest
> request
);
150 // Overridden from ui::CompositorObserver:
151 void OnCompositingDidCommit(ui::Compositor
* compositor
) override
;
152 void OnCompositingStarted(ui::Compositor
* compositor
,
153 base::TimeTicks start_time
) override
;
154 void OnCompositingEnded(ui::Compositor
* compositor
) override
;
155 void OnCompositingAborted(ui::Compositor
* compositor
) override
;
156 void OnCompositingLockStateChanged(ui::Compositor
* compositor
) override
;
157 void OnCompositingShuttingDown(ui::Compositor
* compositor
) override
;
159 // Overridden from ui::CompositorVSyncManager::Observer:
160 void OnUpdateVSyncParameters(base::TimeTicks timebase
,
161 base::TimeDelta interval
) override
;
163 // Overridden from ui::LayerOwnerObserver:
164 void OnLayerRecreated(ui::Layer
* old_layer
, ui::Layer
* new_layer
) override
;
166 // Overridden from ImageTransportFactoryObserver:
167 void OnLostResources() override
;
169 bool ShouldSkipFrame(gfx::Size size_in_dip
) const;
171 // Lazily grab a resize lock if the aura window size doesn't match the current
172 // frame size, to give time to the renderer.
173 void MaybeCreateResizeLock();
175 // Checks if the resize lock can be released because we received an new frame.
176 void CheckResizeLock();
178 // Run all on compositing commit callbacks.
179 void RunOnCommitCallbacks();
181 // Add on compositing commit callback.
182 void AddOnCommitCallbackAndDisableLocks(const base::Closure
& callback
);
184 // Called after async thumbnailer task completes. Scales and crops the result
186 static void CopyFromCompositingSurfaceHasResult(
187 const gfx::Size
& dst_size_in_pixel
,
188 const SkColorType color_type
,
189 ReadbackRequestCallback
& callback
,
190 scoped_ptr
<cc::CopyOutputResult
> result
);
191 static void PrepareTextureCopyOutputResult(
192 const gfx::Size
& dst_size_in_pixel
,
193 const SkColorType color_type
,
194 ReadbackRequestCallback
& callback
,
195 scoped_ptr
<cc::CopyOutputResult
> result
);
196 static void PrepareBitmapCopyOutputResult(
197 const gfx::Size
& dst_size_in_pixel
,
198 const SkColorType color_type
,
199 ReadbackRequestCallback
& callback
,
200 scoped_ptr
<cc::CopyOutputResult
> result
);
201 static void CopyFromCompositingSurfaceHasResultForVideo(
202 base::WeakPtr
<DelegatedFrameHost
> rwhva
,
203 scoped_refptr
<OwnedMailbox
> subscriber_texture
,
204 scoped_refptr
<media::VideoFrame
> video_frame
,
205 const base::Callback
<void(bool)>& callback
,
206 scoped_ptr
<cc::CopyOutputResult
> result
);
207 static void CopyFromCompositingSurfaceFinishedForVideo(
208 base::WeakPtr
<DelegatedFrameHost
> rwhva
,
209 const base::Callback
<void(bool)>& callback
,
210 scoped_refptr
<OwnedMailbox
> subscriber_texture
,
211 scoped_ptr
<cc::SingleReleaseCallback
> release_callback
,
213 static void ReturnSubscriberTexture(
214 base::WeakPtr
<DelegatedFrameHost
> rwhva
,
215 scoped_refptr
<OwnedMailbox
> subscriber_texture
,
218 void SendDelegatedFrameAck(uint32 output_surface_id
);
219 void SurfaceDrawn(uint32 output_surface_id
, cc::SurfaceDrawStatus drawn
);
220 void SendReturnedDelegatedResources(uint32 output_surface_id
);
222 // DelegatedFrameEvictorClient implementation.
223 void EvictDelegatedFrame() override
;
225 // cc::DelegatedFrameProviderClient implementation.
226 void UnusedResourcesAreAvailable() override
;
228 // cc::SurfaceFactoryClient implementation.
229 void ReturnResources(const cc::ReturnedResourceArray
& resources
) override
;
231 void DidReceiveFrameFromRenderer(const gfx::Rect
& damage_rect
);
233 DelegatedFrameHostClient
* const client_
;
234 ui::Compositor
* compositor_
;
236 // True if this renders into a Surface, false if it renders into a delegated
240 std::vector
<base::Closure
> on_compositing_did_commit_callbacks_
;
242 // The vsync manager we are observing for changes, if any.
243 scoped_refptr
<ui::CompositorVSyncManager
> vsync_manager_
;
245 // The current VSync timebase and interval. These are zero until the first
246 // call to OnUpdateVSyncParameters().
247 base::TimeTicks vsync_timebase_
;
248 base::TimeDelta vsync_interval_
;
250 // With delegated renderer, this is the last output surface, used to
251 // disambiguate resources with the same id coming from different output
253 uint32 last_output_surface_id_
;
255 // The number of delegated frame acks that are pending, to delay resource
256 // returns until the acks are sent.
257 int pending_delegated_ack_count_
;
259 // True after a delegated frame has been skipped, until a frame is not
261 bool skipped_frames_
;
262 std::vector
<ui::LatencyInfo
> skipped_latency_info_list_
;
264 // Holds delegated resources that have been given to a DelegatedFrameProvider,
265 // and gives back resources when they are no longer in use for return to the
267 scoped_refptr
<cc::DelegatedFrameResourceCollection
> resource_collection_
;
269 // Provides delegated frame updates to the cc::DelegatedRendererLayer.
270 scoped_refptr
<cc::DelegatedFrameProvider
> frame_provider_
;
272 // State for rendering into a Surface.
273 scoped_ptr
<cc::SurfaceIdAllocator
> id_allocator_
;
274 scoped_ptr
<cc::SurfaceFactory
> surface_factory_
;
275 cc::SurfaceId surface_id_
;
276 gfx::Size current_surface_size_
;
277 float current_scale_factor_
;
278 cc::ReturnedResourceArray surface_returned_resources_
;
280 // This lock is the one waiting for a frame of the right size to come back
281 // from the renderer/GPU process. It is set from the moment the aura window
282 // got resized, to the moment we committed the renderer frame of the same
283 // size. It keeps track of the size we expect from the renderer, and locks the
284 // compositor, as well as the UI for a short time to give a chance to the
285 // renderer of producing a frame of the right size.
286 scoped_ptr
<ResizeLock
> resize_lock_
;
288 // Keeps track of the current frame size.
289 gfx::Size current_frame_size_in_dip_
;
291 // This lock is for waiting for a front surface to become available to draw.
292 scoped_refptr
<ui::CompositorLock
> released_front_lock_
;
294 enum CanLockCompositorState
{
296 // We locked, so at some point we'll need to kick a frame.
298 // No. A lock timed out, we need to kick a new frame before locking again.
299 NO_PENDING_RENDERER_FRAME
,
300 // No. We've got a frame, but it hasn't been committed.
303 CanLockCompositorState can_lock_compositor_
;
305 base::TimeTicks last_draw_ended_
;
307 // Subscriber that listens to frame presentation events.
308 scoped_ptr
<RenderWidgetHostViewFrameSubscriber
> frame_subscriber_
;
309 std::vector
<scoped_refptr
<OwnedMailbox
> > idle_frame_subscriber_textures_
;
311 // Callback used to pass the output request to the layer or to a function
312 // specified by a test.
313 base::Callback
<void(scoped_ptr
<cc::CopyOutputRequest
>)>
314 request_copy_of_output_callback_for_testing_
;
316 // YUV readback pipeline.
317 scoped_ptr
<content::ReadbackYUVInterface
>
318 yuv_readback_pipeline_
;
320 scoped_ptr
<DelegatedFrameEvictor
> delegated_frame_evictor_
;
323 } // namespace content
325 #endif // CONTENT_BROWSER_COMPOSITOR_DELEGATED_FRAME_HOST_H_