[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / content / browser / compositor / delegated_frame_host.h
blobf208c267e3f2eab3da530a6a29402c33a83a6d03
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 cc {
27 class SurfaceFactory;
28 enum class SurfaceDrawStatus;
31 namespace media {
32 class VideoFrame;
35 namespace content {
37 class DelegatedFrameHost;
38 class ReadbackYUVInterface;
39 class RenderWidgetHostViewFrameSubscriber;
40 class RenderWidgetHostImpl;
41 class ResizeLock;
43 // The DelegatedFrameHostClient is the interface from the DelegatedFrameHost,
44 // which manages delegated frames, and the ui::Compositor being used to
45 // display them.
46 class CONTENT_EXPORT DelegatedFrameHostClient {
47 public:
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> {
83 public:
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);
95 void WasHidden();
96 void WasShown(const ui::LatencyInfo& latency_info);
97 void WasResized();
98 bool HasSavedFrame();
99 gfx::Size GetRequestedRendererSize() const;
100 void SetCompositor(ui::Compositor* compositor);
101 void ResetCompositor();
102 // Note: |src_subset| is specified in DIP dimensions while |output_size|
103 // expects pixels.
104 void CopyFromCompositingSurface(const gfx::Rect& src_subrect,
105 const gfx::Size& output_size,
106 ReadbackRequestCallback& callback,
107 const SkColorType color_type);
108 void CopyFromCompositingSurfaceToVideoFrame(
109 const gfx::Rect& src_subrect,
110 const scoped_refptr<media::VideoFrame>& target,
111 const base::Callback<void(bool)>& callback);
112 bool CanCopyToVideoFrame() const;
113 bool CanSubscribeFrame() const;
114 void BeginFrameSubscription(
115 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber);
116 void EndFrameSubscription();
117 bool HasFrameSubscriber() const { return frame_subscriber_; }
119 // Exposed for tests.
120 cc::DelegatedFrameProvider* FrameProviderForTesting() const {
121 return frame_provider_.get();
123 cc::SurfaceId SurfaceIdForTesting() const { return surface_id_; }
124 void OnCompositingDidCommitForTesting(ui::Compositor* compositor) {
125 OnCompositingDidCommit(compositor);
127 bool ReleasedFrontLockActiveForTesting() const {
128 return !!released_front_lock_.get();
130 void SetRequestCopyOfOutputCallbackForTesting(
131 const base::Callback<void(scoped_ptr<cc::CopyOutputRequest>)>& callback) {
132 request_copy_of_output_callback_for_testing_ = callback;
135 private:
136 friend class DelegatedFrameHostClient;
137 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
138 SkippedDelegatedFrames);
139 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
140 DiscardDelegatedFramesWithLocking);
141 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraCopyRequestTest,
142 DestroyedAfterCopyRequest);
144 RenderWidgetHostViewFrameSubscriber* frame_subscriber() const {
145 return frame_subscriber_.get();
147 bool ShouldCreateResizeLock();
148 void LockResources();
149 void UnlockResources();
150 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
152 // Overridden from ui::CompositorObserver:
153 void OnCompositingDidCommit(ui::Compositor* compositor) override;
154 void OnCompositingStarted(ui::Compositor* compositor,
155 base::TimeTicks start_time) override;
156 void OnCompositingEnded(ui::Compositor* compositor) override;
157 void OnCompositingAborted(ui::Compositor* compositor) override;
158 void OnCompositingLockStateChanged(ui::Compositor* compositor) override;
159 void OnCompositingShuttingDown(ui::Compositor* compositor) override;
161 // Overridden from ui::CompositorVSyncManager::Observer:
162 void OnUpdateVSyncParameters(base::TimeTicks timebase,
163 base::TimeDelta interval) override;
165 // Overridden from ui::LayerOwnerObserver:
166 void OnLayerRecreated(ui::Layer* old_layer, ui::Layer* new_layer) override;
168 // Overridden from ImageTransportFactoryObserver:
169 void OnLostResources() override;
171 bool ShouldSkipFrame(gfx::Size size_in_dip) const;
173 // Lazily grab a resize lock if the aura window size doesn't match the current
174 // frame size, to give time to the renderer.
175 void MaybeCreateResizeLock();
177 // Checks if the resize lock can be released because we received an new frame.
178 void CheckResizeLock();
180 // Run all on compositing commit callbacks.
181 void RunOnCommitCallbacks();
183 // Add on compositing commit callback.
184 void AddOnCommitCallbackAndDisableLocks(const base::Closure& callback);
186 // Called after async thumbnailer task completes. Scales and crops the result
187 // of the copy.
188 static void CopyFromCompositingSurfaceHasResult(
189 const gfx::Size& dst_size_in_pixel,
190 const SkColorType color_type,
191 ReadbackRequestCallback& callback,
192 scoped_ptr<cc::CopyOutputResult> result);
193 static void PrepareTextureCopyOutputResult(
194 const gfx::Size& dst_size_in_pixel,
195 const SkColorType color_type,
196 ReadbackRequestCallback& callback,
197 scoped_ptr<cc::CopyOutputResult> result);
198 static void PrepareBitmapCopyOutputResult(
199 const gfx::Size& dst_size_in_pixel,
200 const SkColorType color_type,
201 ReadbackRequestCallback& callback,
202 scoped_ptr<cc::CopyOutputResult> result);
203 static void CopyFromCompositingSurfaceHasResultForVideo(
204 base::WeakPtr<DelegatedFrameHost> rwhva,
205 scoped_refptr<OwnedMailbox> subscriber_texture,
206 scoped_refptr<media::VideoFrame> video_frame,
207 const base::Callback<void(bool)>& callback,
208 scoped_ptr<cc::CopyOutputResult> result);
209 static void CopyFromCompositingSurfaceFinishedForVideo(
210 base::WeakPtr<DelegatedFrameHost> rwhva,
211 const base::Callback<void(bool)>& callback,
212 scoped_refptr<OwnedMailbox> subscriber_texture,
213 scoped_ptr<cc::SingleReleaseCallback> release_callback,
214 bool result);
215 static void ReturnSubscriberTexture(
216 base::WeakPtr<DelegatedFrameHost> rwhva,
217 scoped_refptr<OwnedMailbox> subscriber_texture,
218 uint32 sync_point);
220 void SendDelegatedFrameAck(uint32 output_surface_id);
221 void SurfaceDrawn(uint32 output_surface_id, cc::SurfaceDrawStatus drawn);
222 void SendReturnedDelegatedResources(uint32 output_surface_id);
224 // DelegatedFrameEvictorClient implementation.
225 void EvictDelegatedFrame() override;
227 // cc::DelegatedFrameProviderClient implementation.
228 void UnusedResourcesAreAvailable() override;
230 // cc::SurfaceFactoryClient implementation.
231 void ReturnResources(const cc::ReturnedResourceArray& resources) override;
233 // Called to consult the current |frame_subscriber_|, to determine and maybe
234 // initiate a copy-into-video-frame request.
235 void DidReceiveFrameFromRenderer(const gfx::Rect& damage_rect);
237 DelegatedFrameHostClient* const client_;
238 ui::Compositor* compositor_;
240 // True if this renders into a Surface, false if it renders into a delegated
241 // layer.
242 bool use_surfaces_;
244 std::vector<base::Closure> on_compositing_did_commit_callbacks_;
246 // The vsync manager we are observing for changes, if any.
247 scoped_refptr<ui::CompositorVSyncManager> vsync_manager_;
249 // The current VSync timebase and interval. These are zero until the first
250 // call to OnUpdateVSyncParameters().
251 base::TimeTicks vsync_timebase_;
252 base::TimeDelta vsync_interval_;
254 // With delegated renderer, this is the last output surface, used to
255 // disambiguate resources with the same id coming from different output
256 // surfaces.
257 uint32 last_output_surface_id_;
259 // The number of delegated frame acks that are pending, to delay resource
260 // returns until the acks are sent.
261 int pending_delegated_ack_count_;
263 // True after a delegated frame has been skipped, until a frame is not
264 // skipped.
265 bool skipped_frames_;
266 std::vector<ui::LatencyInfo> skipped_latency_info_list_;
268 // Holds delegated resources that have been given to a DelegatedFrameProvider,
269 // and gives back resources when they are no longer in use for return to the
270 // renderer.
271 scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
273 // Provides delegated frame updates to the cc::DelegatedRendererLayer.
274 scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
276 // State for rendering into a Surface.
277 scoped_ptr<cc::SurfaceIdAllocator> id_allocator_;
278 scoped_ptr<cc::SurfaceFactory> surface_factory_;
279 cc::SurfaceId surface_id_;
280 gfx::Size current_surface_size_;
281 float current_scale_factor_;
282 cc::ReturnedResourceArray surface_returned_resources_;
284 // This lock is the one waiting for a frame of the right size to come back
285 // from the renderer/GPU process. It is set from the moment the aura window
286 // got resized, to the moment we committed the renderer frame of the same
287 // size. It keeps track of the size we expect from the renderer, and locks the
288 // compositor, as well as the UI for a short time to give a chance to the
289 // renderer of producing a frame of the right size.
290 scoped_ptr<ResizeLock> resize_lock_;
292 // Keeps track of the current frame size.
293 gfx::Size current_frame_size_in_dip_;
295 // This lock is for waiting for a front surface to become available to draw.
296 scoped_refptr<ui::CompositorLock> released_front_lock_;
298 enum CanLockCompositorState {
299 YES_CAN_LOCK,
300 // We locked, so at some point we'll need to kick a frame.
301 YES_DID_LOCK,
302 // No. A lock timed out, we need to kick a new frame before locking again.
303 NO_PENDING_RENDERER_FRAME,
304 // No. We've got a frame, but it hasn't been committed.
305 NO_PENDING_COMMIT,
307 CanLockCompositorState can_lock_compositor_;
309 base::TimeTicks last_draw_ended_;
311 // Subscriber that listens to frame presentation events.
312 scoped_ptr<RenderWidgetHostViewFrameSubscriber> frame_subscriber_;
313 std::vector<scoped_refptr<OwnedMailbox> > idle_frame_subscriber_textures_;
315 // Callback used to pass the output request to the layer or to a function
316 // specified by a test.
317 base::Callback<void(scoped_ptr<cc::CopyOutputRequest>)>
318 request_copy_of_output_callback_for_testing_;
320 // YUV readback pipeline.
321 scoped_ptr<content::ReadbackYUVInterface>
322 yuv_readback_pipeline_;
324 scoped_ptr<DelegatedFrameEvictor> delegated_frame_evictor_;
327 } // namespace content
329 #endif // CONTENT_BROWSER_COMPOSITOR_DELEGATED_FRAME_HOST_H_