Change next_proto member type.
[chromium-blink-merge.git] / content / browser / compositor / delegated_frame_host.h
blob28c282818e0a578e087cf802dcbb713d4411f396
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/rect_conversions.h"
26 namespace cc {
27 class SurfaceFactory;
30 namespace media {
31 class VideoFrame;
34 namespace content {
36 class DelegatedFrameHost;
37 class ReadbackYUVInterface;
38 class RenderWidgetHostViewFrameSubscriber;
39 class RenderWidgetHostImpl;
40 class ResizeLock;
42 // The DelegatedFrameHostClient is the interface from the DelegatedFrameHost,
43 // which manages delegated frames, and the ui::Compositor being used to
44 // display them.
45 class CONTENT_EXPORT DelegatedFrameHostClient {
46 public:
47 virtual ui::Compositor* GetCompositor() const = 0;
48 virtual ui::Layer* GetLayer() = 0;
49 virtual RenderWidgetHostImpl* GetHost() = 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> {
82 public:
83 DelegatedFrameHost(DelegatedFrameHostClient* client);
84 ~DelegatedFrameHost() override;
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);
94 void WasHidden();
95 void WasShown(const ui::LatencyInfo& latency_info);
96 void WasResized();
97 bool HasSavedFrame();
98 gfx::Size GetRequestedRendererSize() const;
99 void AddedToWindow();
100 void RemovingFromWindow();
101 void CopyFromCompositingSurface(const gfx::Rect& src_subrect,
102 const gfx::Size& output_size,
103 ReadbackRequestCallback& callback,
104 const SkColorType color_type);
105 void CopyFromCompositingSurfaceToVideoFrame(
106 const gfx::Rect& src_subrect,
107 const scoped_refptr<media::VideoFrame>& target,
108 const base::Callback<void(bool)>& callback);
109 bool CanCopyToVideoFrame() const;
110 bool CanSubscribeFrame() const;
111 void BeginFrameSubscription(
112 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber);
113 void EndFrameSubscription();
114 bool HasFrameSubscriber() const { return frame_subscriber_; }
116 // Exposed for tests.
117 cc::DelegatedFrameProvider* FrameProviderForTesting() const {
118 return frame_provider_.get();
120 cc::SurfaceId SurfaceIdForTesting() const { return surface_id_; }
121 void OnCompositingDidCommitForTesting(ui::Compositor* compositor) {
122 OnCompositingDidCommit(compositor);
124 bool ShouldCreateResizeLockForTesting() { return ShouldCreateResizeLock(); }
125 bool ReleasedFrontLockActiveForTesting() const {
126 return !!released_front_lock_.get();
129 private:
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 void OnCompositingDidCommit(ui::Compositor* compositor) override;
149 void OnCompositingStarted(ui::Compositor* compositor,
150 base::TimeTicks start_time) override;
151 void OnCompositingEnded(ui::Compositor* compositor) override;
152 void OnCompositingAborted(ui::Compositor* compositor) override;
153 void OnCompositingLockStateChanged(ui::Compositor* compositor) override;
155 // Overridden from ui::CompositorVSyncManager::Observer:
156 void OnUpdateVSyncParameters(base::TimeTicks timebase,
157 base::TimeDelta interval) override;
159 // Overridden from ui::LayerOwnerObserver:
160 void OnLayerRecreated(ui::Layer* old_layer, ui::Layer* new_layer) override;
162 // Overridden from ImageTransportFactoryObserver:
163 void OnLostResources() override;
165 bool ShouldSkipFrame(gfx::Size size_in_dip) const;
167 // Lazily grab a resize lock if the aura window size doesn't match the current
168 // frame size, to give time to the renderer.
169 void MaybeCreateResizeLock();
171 // Checks if the resize lock can be released because we received an new frame.
172 void CheckResizeLock();
174 // Run all on compositing commit callbacks.
175 void RunOnCommitCallbacks();
177 // Add on compositing commit callback.
178 void AddOnCommitCallbackAndDisableLocks(const base::Closure& callback);
180 // Called after async thumbnailer task completes. Scales and crops the result
181 // of the copy.
182 static void CopyFromCompositingSurfaceHasResult(
183 const gfx::Size& dst_size_in_pixel,
184 const SkColorType color_type,
185 ReadbackRequestCallback& callback,
186 scoped_ptr<cc::CopyOutputResult> result);
187 static void PrepareTextureCopyOutputResult(
188 const gfx::Size& dst_size_in_pixel,
189 const SkColorType color_type,
190 ReadbackRequestCallback& callback,
191 scoped_ptr<cc::CopyOutputResult> result);
192 static void PrepareBitmapCopyOutputResult(
193 const gfx::Size& dst_size_in_pixel,
194 const SkColorType color_type,
195 ReadbackRequestCallback& callback,
196 scoped_ptr<cc::CopyOutputResult> result);
197 static void CopyFromCompositingSurfaceHasResultForVideo(
198 base::WeakPtr<DelegatedFrameHost> rwhva,
199 scoped_refptr<OwnedMailbox> subscriber_texture,
200 scoped_refptr<media::VideoFrame> video_frame,
201 const base::Callback<void(bool)>& callback,
202 scoped_ptr<cc::CopyOutputResult> result);
203 static void CopyFromCompositingSurfaceFinishedForVideo(
204 base::WeakPtr<DelegatedFrameHost> rwhva,
205 const base::Callback<void(bool)>& callback,
206 scoped_refptr<OwnedMailbox> subscriber_texture,
207 scoped_ptr<cc::SingleReleaseCallback> release_callback,
208 bool result);
209 static void ReturnSubscriberTexture(
210 base::WeakPtr<DelegatedFrameHost> rwhva,
211 scoped_refptr<OwnedMailbox> subscriber_texture,
212 uint32 sync_point);
214 void SendDelegatedFrameAck(uint32 output_surface_id);
215 void SurfaceDrawn(uint32 output_surface_id, bool drawn);
216 void SendReturnedDelegatedResources(uint32 output_surface_id);
218 // DelegatedFrameEvictorClient implementation.
219 void EvictDelegatedFrame() override;
221 // cc::DelegatedFrameProviderClient implementation.
222 void UnusedResourcesAreAvailable() override;
224 // cc::SurfaceFactoryClient implementation.
225 void ReturnResources(const cc::ReturnedResourceArray& resources) override;
227 void DidReceiveFrameFromRenderer(const gfx::Rect& damage_rect);
229 DelegatedFrameHostClient* client_;
231 // True if this renders into a Surface, false if it renders into a delegated
232 // layer.
233 bool use_surfaces_;
235 std::vector<base::Closure> on_compositing_did_commit_callbacks_;
237 // The vsync manager we are observing for changes, if any.
238 scoped_refptr<ui::CompositorVSyncManager> vsync_manager_;
240 // The current VSync timebase and interval. These are zero until the first
241 // call to OnUpdateVSyncParameters().
242 base::TimeTicks vsync_timebase_;
243 base::TimeDelta vsync_interval_;
245 // With delegated renderer, this is the last output surface, used to
246 // disambiguate resources with the same id coming from different output
247 // surfaces.
248 uint32 last_output_surface_id_;
250 // The number of delegated frame acks that are pending, to delay resource
251 // returns until the acks are sent.
252 int pending_delegated_ack_count_;
254 // True after a delegated frame has been skipped, until a frame is not
255 // skipped.
256 bool skipped_frames_;
257 std::vector<ui::LatencyInfo> skipped_latency_info_list_;
259 // Holds delegated resources that have been given to a DelegatedFrameProvider,
260 // and gives back resources when they are no longer in use for return to the
261 // renderer.
262 scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
264 // Provides delegated frame updates to the cc::DelegatedRendererLayer.
265 scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
267 // State for rendering into a Surface.
268 scoped_ptr<cc::SurfaceIdAllocator> id_allocator_;
269 scoped_ptr<cc::SurfaceFactory> surface_factory_;
270 cc::SurfaceId surface_id_;
271 gfx::Size current_surface_size_;
272 float current_scale_factor_;
273 cc::ReturnedResourceArray surface_returned_resources_;
275 // This lock is the one waiting for a frame of the right size to come back
276 // from the renderer/GPU process. It is set from the moment the aura window
277 // got resized, to the moment we committed the renderer frame of the same
278 // size. It keeps track of the size we expect from the renderer, and locks the
279 // compositor, as well as the UI for a short time to give a chance to the
280 // renderer of producing a frame of the right size.
281 scoped_ptr<ResizeLock> resize_lock_;
283 // Keeps track of the current frame size.
284 gfx::Size current_frame_size_in_dip_;
286 // This lock is for waiting for a front surface to become available to draw.
287 scoped_refptr<ui::CompositorLock> released_front_lock_;
289 enum CanLockCompositorState {
290 YES_CAN_LOCK,
291 // We locked, so at some point we'll need to kick a frame.
292 YES_DID_LOCK,
293 // No. A lock timed out, we need to kick a new frame before locking again.
294 NO_PENDING_RENDERER_FRAME,
295 // No. We've got a frame, but it hasn't been committed.
296 NO_PENDING_COMMIT,
298 CanLockCompositorState can_lock_compositor_;
300 base::TimeTicks last_draw_ended_;
302 // Subscriber that listens to frame presentation events.
303 scoped_ptr<RenderWidgetHostViewFrameSubscriber> frame_subscriber_;
304 std::vector<scoped_refptr<OwnedMailbox> > idle_frame_subscriber_textures_;
306 // YUV readback pipeline.
307 scoped_ptr<content::ReadbackYUVInterface>
308 yuv_readback_pipeline_;
310 scoped_ptr<DelegatedFrameEvictor> delegated_frame_evictor_;
313 } // namespace content
315 #endif // CONTENT_BROWSER_COMPOSITOR_DELEGATED_FRAME_HOST_H_