Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / compositor / delegated_frame_host.h
blob5b2588d34a80eb14ac0d316452cb094bfb7309bf
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 "content/browser/compositor/image_transport_factory.h"
12 #include "content/browser/compositor/owned_mailbox.h"
13 #include "content/browser/renderer_host/delegated_frame_evictor.h"
14 #include "content/browser/renderer_host/dip_util.h"
15 #include "content/browser/renderer_host/render_widget_host_impl.h"
16 #include "content/common/gpu/client/gl_helper.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"
25 namespace media {
26 class VideoFrame;
29 namespace content {
31 class DelegatedFrameHost;
32 class RenderWidgetHostViewFrameSubscriber;
33 class RenderWidgetHostImpl;
34 class ResizeLock;
36 // The DelegatedFrameHostClient is the interface from the DelegatedFrameHost,
37 // which manages delegated frames, and the ui::Compositor being used to
38 // display them.
39 class CONTENT_EXPORT DelegatedFrameHostClient {
40 public:
41 virtual ui::Compositor* GetCompositor() const = 0;
42 virtual ui::Layer* GetLayer() = 0;
43 virtual RenderWidgetHostImpl* GetHost() = 0;
44 virtual void SchedulePaintInRect(const gfx::Rect& damage_rect_in_dip) = 0;
45 virtual bool IsVisible() = 0;
46 virtual scoped_ptr<ResizeLock> CreateResizeLock(
47 bool defer_compositor_lock) = 0;
48 virtual gfx::Size DesiredFrameSize() = 0;
50 // TODO(ccameron): It is likely that at least one of these two functions is
51 // redundant. Find which one, and delete it.
52 virtual float CurrentDeviceScaleFactor() = 0;
53 virtual gfx::Size ConvertViewSizeToPixel(const gfx::Size& size) = 0;
55 // These are to be overridden for testing only.
56 // TODO(ccameron): This is convoluted. Make the tests that need to override
57 // these functions test DelegatedFrameHost directly (rather than do it
58 // through RenderWidgetHostViewAura).
59 virtual DelegatedFrameHost* GetDelegatedFrameHost() const = 0;
60 virtual bool ShouldCreateResizeLock();
61 virtual void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
64 // The DelegatedFrameHost is used to host all of the RenderWidgetHostView state
65 // and functionality that is associated with delegated frames being sent from
66 // the RenderWidget. The DelegatedFrameHost will push these changes through to
67 // the ui::Compositor associated with its DelegatedFrameHostClient.
68 class CONTENT_EXPORT DelegatedFrameHost
69 : public ui::CompositorObserver,
70 public ui::CompositorVSyncManager::Observer,
71 public ui::LayerOwnerDelegate,
72 public ImageTransportFactoryObserver,
73 public DelegatedFrameEvictorClient,
74 public cc::DelegatedFrameResourceCollectionClient,
75 public base::SupportsWeakPtr<DelegatedFrameHost> {
76 public:
77 DelegatedFrameHost(DelegatedFrameHostClient* client);
78 virtual ~DelegatedFrameHost();
80 gfx::Rect GetViewBoundsWithResizeLock(const gfx::Rect& bounds) const;
81 bool CanCopyToBitmap() const;
83 // Public interface exposed to RenderWidgetHostView.
84 void SwapDelegatedFrame(
85 uint32 output_surface_id,
86 scoped_ptr<cc::DelegatedFrameData> frame_data,
87 float frame_device_scale_factor,
88 const std::vector<ui::LatencyInfo>& latency_info);
89 void WasHidden();
90 void WasShown();
91 void WasResized();
92 void AddedToWindow();
93 void RemovingFromWindow();
94 void CopyFromCompositingSurface(
95 const gfx::Rect& src_subrect,
96 const gfx::Size& dst_size,
97 const base::Callback<void(bool, const SkBitmap&)>& callback,
98 const SkBitmap::Config config);
99 void CopyFromCompositingSurfaceToVideoFrame(
100 const gfx::Rect& src_subrect,
101 const scoped_refptr<media::VideoFrame>& target,
102 const base::Callback<void(bool)>& callback);
103 bool CanCopyToVideoFrame() const;
104 bool CanSubscribeFrame() const;
105 void BeginFrameSubscription(
106 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber);
107 void EndFrameSubscription();
109 // Exposed for tests.
110 cc::DelegatedFrameProvider* FrameProviderForTesting() const {
111 return frame_provider_.get();
113 gfx::Size CurrentFrameSizeInDIPForTesting() const {
114 return current_frame_size_in_dip_;
116 void OnCompositingDidCommitForTesting(ui::Compositor* compositor) {
117 OnCompositingDidCommit(compositor);
120 private:
121 friend class DelegatedFrameHostClient;
122 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
123 SkippedDelegatedFrames);
124 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
125 DiscardDelegatedFramesWithLocking);
126 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraCopyRequestTest,
127 DestroyedAfterCopyRequest);
129 RenderWidgetHostViewFrameSubscriber* frame_subscriber() const {
130 return frame_subscriber_.get();
132 bool ShouldCreateResizeLock();
133 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
135 void LockResources();
136 void UnlockResources();
138 // Overridden from ui::CompositorObserver:
139 virtual void OnCompositingDidCommit(ui::Compositor* compositor) OVERRIDE;
140 virtual void OnCompositingStarted(ui::Compositor* compositor,
141 base::TimeTicks start_time) OVERRIDE;
142 virtual void OnCompositingEnded(ui::Compositor* compositor) OVERRIDE;
143 virtual void OnCompositingAborted(ui::Compositor* compositor) OVERRIDE;
144 virtual void OnCompositingLockStateChanged(
145 ui::Compositor* compositor) OVERRIDE;
147 // Overridden from ui::CompositorVSyncManager::Observer:
148 virtual void OnUpdateVSyncParameters(base::TimeTicks timebase,
149 base::TimeDelta interval) OVERRIDE;
151 // Overridden from ui::LayerOwnerObserver:
152 virtual void OnLayerRecreated(ui::Layer* old_layer,
153 ui::Layer* new_layer) OVERRIDE;
155 // Overridden from ImageTransportFactoryObserver:
156 virtual void OnLostResources() OVERRIDE;
158 bool ShouldSkipFrame(gfx::Size size_in_dip) const;
160 // Lazily grab a resize lock if the aura window size doesn't match the current
161 // frame size, to give time to the renderer.
162 void MaybeCreateResizeLock();
164 // Checks if the resize lock can be released because we received an new frame.
165 void CheckResizeLock();
167 // Run all on compositing commit callbacks.
168 void RunOnCommitCallbacks();
170 // Add on compositing commit callback.
171 void AddOnCommitCallbackAndDisableLocks(const base::Closure& callback);
173 // Called after async thumbnailer task completes. Scales and crops the result
174 // of the copy.
175 static void CopyFromCompositingSurfaceHasResult(
176 const gfx::Size& dst_size_in_pixel,
177 const SkBitmap::Config config,
178 const base::Callback<void(bool, const SkBitmap&)>& callback,
179 scoped_ptr<cc::CopyOutputResult> result);
180 static void PrepareTextureCopyOutputResult(
181 const gfx::Size& dst_size_in_pixel,
182 const SkBitmap::Config config,
183 const base::Callback<void(bool, const SkBitmap&)>& callback,
184 scoped_ptr<cc::CopyOutputResult> result);
185 static void PrepareBitmapCopyOutputResult(
186 const gfx::Size& dst_size_in_pixel,
187 const SkBitmap::Config config,
188 const base::Callback<void(bool, const SkBitmap&)>& callback,
189 scoped_ptr<cc::CopyOutputResult> result);
190 static void CopyFromCompositingSurfaceHasResultForVideo(
191 base::WeakPtr<DelegatedFrameHost> rwhva,
192 scoped_refptr<OwnedMailbox> subscriber_texture,
193 scoped_refptr<media::VideoFrame> video_frame,
194 const base::Callback<void(bool)>& callback,
195 scoped_ptr<cc::CopyOutputResult> result);
196 static void CopyFromCompositingSurfaceFinishedForVideo(
197 base::WeakPtr<DelegatedFrameHost> rwhva,
198 const base::Callback<void(bool)>& callback,
199 scoped_refptr<OwnedMailbox> subscriber_texture,
200 scoped_ptr<cc::SingleReleaseCallback> release_callback,
201 bool result);
202 static void ReturnSubscriberTexture(
203 base::WeakPtr<DelegatedFrameHost> rwhva,
204 scoped_refptr<OwnedMailbox> subscriber_texture,
205 uint32 sync_point);
207 void SendDelegatedFrameAck(uint32 output_surface_id);
208 void SendReturnedDelegatedResources(uint32 output_surface_id);
210 // DelegatedFrameEvictorClient implementation.
211 virtual void EvictDelegatedFrame() OVERRIDE;
213 // cc::DelegatedFrameProviderClient implementation.
214 virtual void UnusedResourcesAreAvailable() OVERRIDE;
216 void DidReceiveFrameFromRenderer();
218 DelegatedFrameHostClient* client_;
220 std::vector<base::Closure> on_compositing_did_commit_callbacks_;
222 // The vsync manager we are observing for changes, if any.
223 scoped_refptr<ui::CompositorVSyncManager> vsync_manager_;
225 // With delegated renderer, this is the last output surface, used to
226 // disambiguate resources with the same id coming from different output
227 // surfaces.
228 uint32 last_output_surface_id_;
230 // The number of delegated frame acks that are pending, to delay resource
231 // returns until the acks are sent.
232 int pending_delegated_ack_count_;
234 // True after a delegated frame has been skipped, until a frame is not
235 // skipped.
236 bool skipped_frames_;
238 // Holds delegated resources that have been given to a DelegatedFrameProvider,
239 // and gives back resources when they are no longer in use for return to the
240 // renderer.
241 scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
243 // Provides delegated frame updates to the cc::DelegatedRendererLayer.
244 scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
246 // This lock is the one waiting for a frame of the right size to come back
247 // from the renderer/GPU process. It is set from the moment the aura window
248 // got resized, to the moment we committed the renderer frame of the same
249 // size. It keeps track of the size we expect from the renderer, and locks the
250 // compositor, as well as the UI for a short time to give a chance to the
251 // renderer of producing a frame of the right size.
252 scoped_ptr<ResizeLock> resize_lock_;
254 // Keeps track of the current frame size.
255 gfx::Size current_frame_size_in_dip_;
257 // This lock is for waiting for a front surface to become available to draw.
258 scoped_refptr<ui::CompositorLock> released_front_lock_;
260 enum CanLockCompositorState {
261 YES,
262 // We locked, so at some point we'll need to kick a frame.
263 YES_DID_LOCK,
264 // No. A lock timed out, we need to kick a new frame before locking again.
265 NO_PENDING_RENDERER_FRAME,
266 // No. We've got a frame, but it hasn't been committed.
267 NO_PENDING_COMMIT,
269 CanLockCompositorState can_lock_compositor_;
271 base::TimeTicks last_draw_ended_;
273 // Subscriber that listens to frame presentation events.
274 scoped_ptr<RenderWidgetHostViewFrameSubscriber> frame_subscriber_;
275 std::vector<scoped_refptr<OwnedMailbox> > idle_frame_subscriber_textures_;
276 std::set<OwnedMailbox*> active_frame_subscriber_textures_;
278 // YUV readback pipeline.
279 scoped_ptr<content::ReadbackYUVInterface>
280 yuv_readback_pipeline_;
282 scoped_ptr<DelegatedFrameEvictor> delegated_frame_evictor_;
285 } // namespace content
287 #endif // CONTENT_BROWSER_COMPOSITOR_DELEGATED_FRAME_HOST_H_