Add long running gmail memory benchmark for background tab.
[chromium-blink-merge.git] / content / browser / compositor / delegated_frame_host.h
blob7972bfa29101ba9daa97288200024cce0e2eed2a
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 WasHidden();
131 void WasShown(const ui::LatencyInfo& latency_info);
132 void WasResized();
133 bool HasSavedFrame();
134 gfx::Size GetRequestedRendererSize() const;
135 void SetCompositor(ui::Compositor* compositor);
136 void ResetCompositor();
137 void SetVSyncParameters(const base::TimeTicks& timebase,
138 const base::TimeDelta& interval);
139 // Note: |src_subset| is specified in DIP dimensions while |output_size|
140 // expects pixels.
141 void CopyFromCompositingSurface(const gfx::Rect& src_subrect,
142 const gfx::Size& output_size,
143 ReadbackRequestCallback& callback,
144 const SkColorType preferred_color_type);
145 void CopyFromCompositingSurfaceToVideoFrame(
146 const gfx::Rect& src_subrect,
147 const scoped_refptr<media::VideoFrame>& target,
148 const base::Callback<void(bool)>& callback);
149 bool CanCopyToVideoFrame() const;
150 bool CanSubscribeFrame() const;
151 void BeginFrameSubscription(
152 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber);
153 void EndFrameSubscription();
154 bool HasFrameSubscriber() const { return frame_subscriber_; }
155 uint32_t GetSurfaceIdNamespace();
157 // Exposed for tests.
158 cc::DelegatedFrameProvider* FrameProviderForTesting() const {
159 return frame_provider_.get();
161 cc::SurfaceId SurfaceIdForTesting() const { return surface_id_; }
162 void OnCompositingDidCommitForTesting(ui::Compositor* compositor) {
163 OnCompositingDidCommit(compositor);
165 bool ReleasedFrontLockActiveForTesting() const {
166 return !!released_front_lock_.get();
168 void SetRequestCopyOfOutputCallbackForTesting(
169 const base::Callback<void(scoped_ptr<cc::CopyOutputRequest>)>& callback) {
170 request_copy_of_output_callback_for_testing_ = callback;
173 private:
174 friend class DelegatedFrameHostClient;
175 friend class RenderWidgetHostViewAuraCopyRequestTest;
176 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
177 SkippedDelegatedFrames);
178 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
179 DiscardDelegatedFramesWithLocking);
181 RenderWidgetHostViewFrameSubscriber* frame_subscriber() const {
182 return frame_subscriber_.get();
184 bool ShouldCreateResizeLock();
185 void LockResources();
186 void UnlockResources();
187 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
189 bool ShouldSkipFrame(gfx::Size size_in_dip) const;
191 // Lazily grab a resize lock if the aura window size doesn't match the current
192 // frame size, to give time to the renderer.
193 void MaybeCreateResizeLock();
195 // Checks if the resize lock can be released because we received an new frame.
196 void CheckResizeLock();
198 // Run all on compositing commit callbacks.
199 void RunOnCommitCallbacks();
201 // Add on compositing commit callback.
202 void AddOnCommitCallbackAndDisableLocks(const base::Closure& callback);
204 // Called after async thumbnailer task completes. Scales and crops the result
205 // of the copy.
206 static void CopyFromCompositingSurfaceHasResult(
207 const gfx::Size& dst_size_in_pixel,
208 const SkColorType color_type,
209 ReadbackRequestCallback& callback,
210 scoped_ptr<cc::CopyOutputResult> result);
211 static void PrepareTextureCopyOutputResult(
212 const gfx::Size& dst_size_in_pixel,
213 const SkColorType color_type,
214 ReadbackRequestCallback& callback,
215 scoped_ptr<cc::CopyOutputResult> result);
216 static void PrepareBitmapCopyOutputResult(
217 const gfx::Size& dst_size_in_pixel,
218 const SkColorType color_type,
219 ReadbackRequestCallback& callback,
220 scoped_ptr<cc::CopyOutputResult> result);
221 static void CopyFromCompositingSurfaceHasResultForVideo(
222 base::WeakPtr<DelegatedFrameHost> rwhva,
223 scoped_refptr<OwnedMailbox> subscriber_texture,
224 scoped_refptr<media::VideoFrame> video_frame,
225 const base::Callback<void(bool)>& callback,
226 scoped_ptr<cc::CopyOutputResult> result);
227 static void CopyFromCompositingSurfaceFinishedForVideo(
228 base::WeakPtr<DelegatedFrameHost> rwhva,
229 const base::Callback<void(bool)>& callback,
230 scoped_refptr<OwnedMailbox> subscriber_texture,
231 scoped_ptr<cc::SingleReleaseCallback> release_callback,
232 bool result);
233 static void ReturnSubscriberTexture(
234 base::WeakPtr<DelegatedFrameHost> rwhva,
235 scoped_refptr<OwnedMailbox> subscriber_texture,
236 uint32 sync_point);
238 void SendDelegatedFrameAck(uint32 output_surface_id);
239 void SurfaceDrawn(uint32 output_surface_id, cc::SurfaceDrawStatus drawn);
240 void SendReturnedDelegatedResources(uint32 output_surface_id);
242 // Called to consult the current |frame_subscriber_|, to determine and maybe
243 // initiate a copy-into-video-frame request.
244 void DidReceiveFrameFromRenderer(const gfx::Rect& damage_rect);
246 DelegatedFrameHostClient* const client_;
247 ui::Compositor* compositor_;
249 // True if this renders into a Surface, false if it renders into a delegated
250 // layer.
251 bool use_surfaces_;
253 std::vector<base::Closure> on_compositing_did_commit_callbacks_;
255 // The vsync manager we are observing for changes, if any.
256 scoped_refptr<ui::CompositorVSyncManager> vsync_manager_;
258 // The current VSync timebase and interval. These are zero until the first
259 // call to SetVSyncParameters().
260 base::TimeTicks vsync_timebase_;
261 base::TimeDelta vsync_interval_;
263 // Overridable tick clock used for testing functions using current time.
264 scoped_ptr<base::TickClock> tick_clock_;
266 // With delegated renderer, this is the last output surface, used to
267 // disambiguate resources with the same id coming from different output
268 // surfaces.
269 uint32 last_output_surface_id_;
271 // The number of delegated frame acks that are pending, to delay resource
272 // returns until the acks are sent.
273 int pending_delegated_ack_count_;
275 // True after a delegated frame has been skipped, until a frame is not
276 // skipped.
277 bool skipped_frames_;
278 std::vector<ui::LatencyInfo> skipped_latency_info_list_;
280 // Holds delegated resources that have been given to a DelegatedFrameProvider,
281 // and gives back resources when they are no longer in use for return to the
282 // renderer.
283 scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
285 // Provides delegated frame updates to the cc::DelegatedRendererLayer.
286 scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
288 // State for rendering into a Surface.
289 scoped_ptr<cc::SurfaceIdAllocator> id_allocator_;
290 scoped_ptr<cc::SurfaceFactory> surface_factory_;
291 cc::SurfaceId surface_id_;
292 gfx::Size current_surface_size_;
293 float current_scale_factor_;
294 cc::ReturnedResourceArray surface_returned_resources_;
296 // This lock is the one waiting for a frame of the right size to come back
297 // from the renderer/GPU process. It is set from the moment the aura window
298 // got resized, to the moment we committed the renderer frame of the same
299 // size. It keeps track of the size we expect from the renderer, and locks the
300 // compositor, as well as the UI for a short time to give a chance to the
301 // renderer of producing a frame of the right size.
302 scoped_ptr<ResizeLock> resize_lock_;
304 // Keeps track of the current frame size.
305 gfx::Size current_frame_size_in_dip_;
307 // This lock is for waiting for a front surface to become available to draw.
308 scoped_refptr<ui::CompositorLock> released_front_lock_;
310 enum CanLockCompositorState {
311 YES_CAN_LOCK,
312 // We locked, so at some point we'll need to kick a frame.
313 YES_DID_LOCK,
314 // No. A lock timed out, we need to kick a new frame before locking again.
315 NO_PENDING_RENDERER_FRAME,
316 // No. We've got a frame, but it hasn't been committed.
317 NO_PENDING_COMMIT,
319 CanLockCompositorState can_lock_compositor_;
321 base::TimeTicks last_draw_ended_;
323 // Subscriber that listens to frame presentation events.
324 scoped_ptr<RenderWidgetHostViewFrameSubscriber> frame_subscriber_;
325 std::vector<scoped_refptr<OwnedMailbox> > idle_frame_subscriber_textures_;
327 // Callback used to pass the output request to the layer or to a function
328 // specified by a test.
329 base::Callback<void(scoped_ptr<cc::CopyOutputRequest>)>
330 request_copy_of_output_callback_for_testing_;
332 // YUV readback pipeline.
333 scoped_ptr<content::ReadbackYUVInterface>
334 yuv_readback_pipeline_;
336 scoped_ptr<DelegatedFrameEvictor> delegated_frame_evictor_;
339 } // namespace content
341 #endif // CONTENT_BROWSER_COMPOSITOR_DELEGATED_FRAME_HOST_H_