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/public/browser/render_process_host.h"
18 #include "ui/compositor/compositor.h"
19 #include "ui/compositor/compositor_observer.h"
20 #include "ui/compositor/compositor_vsync_manager.h"
21 #include "ui/compositor/layer.h"
22 #include "ui/compositor/layer_owner_delegate.h"
23 #include "ui/gfx/rect_conversions.h"
35 class DelegatedFrameHost;
36 class ReadbackYUVInterface;
37 class RenderWidgetHostViewFrameSubscriber;
38 class RenderWidgetHostImpl;
41 // The DelegatedFrameHostClient is the interface from the DelegatedFrameHost,
42 // which manages delegated frames, and the ui::Compositor being used to
44 class CONTENT_EXPORT DelegatedFrameHostClient {
46 virtual ui::Compositor* GetCompositor() const = 0;
47 virtual ui::Layer* GetLayer() = 0;
48 virtual RenderWidgetHostImpl* GetHost() = 0;
49 virtual void SchedulePaintInRect(const gfx::Rect& damage_rect_in_dip) = 0;
50 virtual bool IsVisible() = 0;
51 virtual scoped_ptr<ResizeLock> CreateResizeLock(
52 bool defer_compositor_lock) = 0;
53 virtual gfx::Size DesiredFrameSize() = 0;
55 // TODO(ccameron): It is likely that at least one of these two functions is
56 // redundant. Find which one, and delete it.
57 virtual float CurrentDeviceScaleFactor() = 0;
58 virtual gfx::Size ConvertViewSizeToPixel(const gfx::Size& size) = 0;
60 // These are to be overridden for testing only.
61 // TODO(ccameron): This is convoluted. Make the tests that need to override
62 // these functions test DelegatedFrameHost directly (rather than do it
63 // through RenderWidgetHostViewAura).
64 virtual DelegatedFrameHost* GetDelegatedFrameHost() const = 0;
65 virtual bool ShouldCreateResizeLock();
66 virtual void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
69 // The DelegatedFrameHost is used to host all of the RenderWidgetHostView state
70 // and functionality that is associated with delegated frames being sent from
71 // the RenderWidget. The DelegatedFrameHost will push these changes through to
72 // the ui::Compositor associated with its DelegatedFrameHostClient.
73 class CONTENT_EXPORT DelegatedFrameHost
74 : public ui::CompositorObserver,
75 public ui::CompositorVSyncManager::Observer,
76 public ui::LayerOwnerDelegate,
77 public ImageTransportFactoryObserver,
78 public DelegatedFrameEvictorClient,
79 public cc::DelegatedFrameResourceCollectionClient,
80 public cc::SurfaceFactoryClient,
81 public base::SupportsWeakPtr<DelegatedFrameHost> {
83 DelegatedFrameHost(DelegatedFrameHostClient* client);
84 virtual ~DelegatedFrameHost();
86 bool CanCopyToBitmap() const;
88 // Public interface exposed to RenderWidgetHostView.
89 void SwapDelegatedFrame(
90 uint32 output_surface_id,
91 scoped_ptr<cc::DelegatedFrameData> frame_data,
92 float frame_device_scale_factor,
93 const std::vector<ui::LatencyInfo>& latency_info);
95 void WasShown(const ui::LatencyInfo& latency_info);
98 gfx::Size GetRequestedRendererSize() const;
100 void RemovingFromWindow();
101 void CopyFromCompositingSurface(
102 const gfx::Rect& src_subrect,
103 const gfx::Size& output_size,
104 const base::Callback<void(bool, const SkBitmap&)>& 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 void OnCompositingDidCommitForTesting(ui::Compositor* compositor) {
122 OnCompositingDidCommit(compositor);
124 bool ShouldCreateResizeLockForTesting() { return ShouldCreateResizeLock(); }
125 bool ReleasedFrontLockActiveForTesting() const {
126 return !!released_front_lock_;
130 friend class DelegatedFrameHostClient;
131 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
132 SkippedDelegatedFrames);
133 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
134 DiscardDelegatedFramesWithLocking);
135 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraCopyRequestTest,
136 DestroyedAfterCopyRequest);
138 RenderWidgetHostViewFrameSubscriber* frame_subscriber() const {
139 return frame_subscriber_.get();
141 bool ShouldCreateResizeLock();
142 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
144 void LockResources();
145 void UnlockResources();
147 // Overridden from ui::CompositorObserver:
148 virtual void OnCompositingDidCommit(ui::Compositor* compositor) OVERRIDE;
149 virtual void OnCompositingStarted(ui::Compositor* compositor,
150 base::TimeTicks start_time) OVERRIDE;
151 virtual void OnCompositingEnded(ui::Compositor* compositor) OVERRIDE;
152 virtual void OnCompositingAborted(ui::Compositor* compositor) OVERRIDE;
153 virtual void OnCompositingLockStateChanged(
154 ui::Compositor* compositor) OVERRIDE;
156 // Overridden from ui::CompositorVSyncManager::Observer:
157 virtual void OnUpdateVSyncParameters(base::TimeTicks timebase,
158 base::TimeDelta interval) OVERRIDE;
160 // Overridden from ui::LayerOwnerObserver:
161 virtual void OnLayerRecreated(ui::Layer* old_layer,
162 ui::Layer* new_layer) OVERRIDE;
164 // Overridden from ImageTransportFactoryObserver:
165 virtual void OnLostResources() OVERRIDE;
167 bool ShouldSkipFrame(gfx::Size size_in_dip) const;
169 // Lazily grab a resize lock if the aura window size doesn't match the current
170 // frame size, to give time to the renderer.
171 void MaybeCreateResizeLock();
173 // Checks if the resize lock can be released because we received an new frame.
174 void CheckResizeLock();
176 // Run all on compositing commit callbacks.
177 void RunOnCommitCallbacks();
179 // Add on compositing commit callback.
180 void AddOnCommitCallbackAndDisableLocks(const base::Closure& callback);
182 // Called after async thumbnailer task completes. Scales and crops the result
184 static void CopyFromCompositingSurfaceHasResult(
185 const gfx::Size& dst_size_in_pixel,
186 const SkColorType color_type,
187 const base::Callback<void(bool, const SkBitmap&)>& callback,
188 scoped_ptr<cc::CopyOutputResult> result);
189 static void PrepareTextureCopyOutputResult(
190 const gfx::Size& dst_size_in_pixel,
191 const SkColorType color_type,
192 const base::Callback<void(bool, const SkBitmap&)>& callback,
193 scoped_ptr<cc::CopyOutputResult> result);
194 static void PrepareBitmapCopyOutputResult(
195 const gfx::Size& dst_size_in_pixel,
196 const SkColorType color_type,
197 const base::Callback<void(bool, const SkBitmap&)>& callback,
198 scoped_ptr<cc::CopyOutputResult> result);
199 static void CopyFromCompositingSurfaceHasResultForVideo(
200 base::WeakPtr<DelegatedFrameHost> rwhva,
201 scoped_refptr<OwnedMailbox> subscriber_texture,
202 scoped_refptr<media::VideoFrame> video_frame,
203 const base::Callback<void(bool)>& callback,
204 scoped_ptr<cc::CopyOutputResult> result);
205 static void CopyFromCompositingSurfaceFinishedForVideo(
206 base::WeakPtr<DelegatedFrameHost> rwhva,
207 const base::Callback<void(bool)>& callback,
208 scoped_refptr<OwnedMailbox> subscriber_texture,
209 scoped_ptr<cc::SingleReleaseCallback> release_callback,
211 static void ReturnSubscriberTexture(
212 base::WeakPtr<DelegatedFrameHost> rwhva,
213 scoped_refptr<OwnedMailbox> subscriber_texture,
216 void SendDelegatedFrameAck(uint32 output_surface_id);
217 void SendReturnedDelegatedResources(uint32 output_surface_id);
219 // DelegatedFrameEvictorClient implementation.
220 virtual void EvictDelegatedFrame() OVERRIDE;
222 // cc::DelegatedFrameProviderClient implementation.
223 virtual void UnusedResourcesAreAvailable() OVERRIDE;
225 // cc::SurfaceFactoryClient implementation.
226 virtual void ReturnResources(
227 const cc::ReturnedResourceArray& resources) OVERRIDE;
229 void DidReceiveFrameFromRenderer(const gfx::Rect& damage_rect);
231 DelegatedFrameHostClient* client_;
233 // True if this renders into a Surface, false if it renders into a delegated
237 std::vector<base::Closure> on_compositing_did_commit_callbacks_;
239 // The vsync manager we are observing for changes, if any.
240 scoped_refptr<ui::CompositorVSyncManager> vsync_manager_;
242 // The current VSync timebase and interval. These are zero until the first
243 // call to OnUpdateVSyncParameters().
244 base::TimeTicks vsync_timebase_;
245 base::TimeDelta vsync_interval_;
247 // With delegated renderer, this is the last output surface, used to
248 // disambiguate resources with the same id coming from different output
250 uint32 last_output_surface_id_;
252 // The number of delegated frame acks that are pending, to delay resource
253 // returns until the acks are sent.
254 int pending_delegated_ack_count_;
256 // True after a delegated frame has been skipped, until a frame is not
258 bool skipped_frames_;
259 std::vector<ui::LatencyInfo> skipped_latency_info_list_;
261 // Holds delegated resources that have been given to a DelegatedFrameProvider,
262 // and gives back resources when they are no longer in use for return to the
264 scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
266 // Provides delegated frame updates to the cc::DelegatedRendererLayer.
267 scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
269 // State for rendering into a Surface.
270 scoped_ptr<cc::SurfaceIdAllocator> id_allocator_;
271 scoped_ptr<cc::SurfaceFactory> surface_factory_;
272 cc::SurfaceId surface_id_;
273 gfx::Size current_surface_size_;
274 cc::ReturnedResourceArray surface_returned_resources_;
276 // This lock is the one waiting for a frame of the right size to come back
277 // from the renderer/GPU process. It is set from the moment the aura window
278 // got resized, to the moment we committed the renderer frame of the same
279 // size. It keeps track of the size we expect from the renderer, and locks the
280 // compositor, as well as the UI for a short time to give a chance to the
281 // renderer of producing a frame of the right size.
282 scoped_ptr<ResizeLock> resize_lock_;
284 // Keeps track of the current frame size.
285 gfx::Size current_frame_size_in_dip_;
287 // This lock is for waiting for a front surface to become available to draw.
288 scoped_refptr<ui::CompositorLock> released_front_lock_;
290 enum CanLockCompositorState {
292 // We locked, so at some point we'll need to kick a frame.
294 // No. A lock timed out, we need to kick a new frame before locking again.
295 NO_PENDING_RENDERER_FRAME,
296 // No. We've got a frame, but it hasn't been committed.
299 CanLockCompositorState can_lock_compositor_;
301 base::TimeTicks last_draw_ended_;
303 // Subscriber that listens to frame presentation events.
304 scoped_ptr<RenderWidgetHostViewFrameSubscriber> frame_subscriber_;
305 std::vector<scoped_refptr<OwnedMailbox> > idle_frame_subscriber_textures_;
307 // YUV readback pipeline.
308 scoped_ptr<content::ReadbackYUVInterface>
309 yuv_readback_pipeline_;
311 scoped_ptr<DelegatedFrameEvictor> delegated_frame_evictor_;
314 } // namespace content
316 #endif // CONTENT_BROWSER_COMPOSITOR_DELEGATED_FRAME_HOST_H_