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