Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / compositor / delegated_frame_host.h
blob0b294b61d3ebafba72cdfd7673fa480d70c20fe7
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();
156 cc::SurfaceId SurfaceIdAtPoint(const gfx::Point& point,
157 gfx::Point* transformed_point);
159 // Exposed for tests.
160 cc::DelegatedFrameProvider* FrameProviderForTesting() const {
161 return frame_provider_.get();
163 cc::SurfaceId SurfaceIdForTesting() const { return surface_id_; }
164 void OnCompositingDidCommitForTesting(ui::Compositor* compositor) {
165 OnCompositingDidCommit(compositor);
167 bool ReleasedFrontLockActiveForTesting() const {
168 return !!released_front_lock_.get();
170 void SetRequestCopyOfOutputCallbackForTesting(
171 const base::Callback<void(scoped_ptr<cc::CopyOutputRequest>)>& callback) {
172 request_copy_of_output_callback_for_testing_ = callback;
175 private:
176 friend class DelegatedFrameHostClient;
177 friend class RenderWidgetHostViewAuraCopyRequestTest;
178 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
179 SkippedDelegatedFrames);
180 FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
181 DiscardDelegatedFramesWithLocking);
183 RenderWidgetHostViewFrameSubscriber* frame_subscriber() const {
184 return frame_subscriber_.get();
186 bool ShouldCreateResizeLock();
187 void LockResources();
188 void UnlockResources();
189 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
191 bool ShouldSkipFrame(gfx::Size size_in_dip) const;
193 // Lazily grab a resize lock if the aura window size doesn't match the current
194 // frame size, to give time to the renderer.
195 void MaybeCreateResizeLock();
197 // Checks if the resize lock can be released because we received an new frame.
198 void CheckResizeLock();
200 // Run all on compositing commit callbacks.
201 void RunOnCommitCallbacks();
203 // Add on compositing commit callback.
204 void AddOnCommitCallbackAndDisableLocks(const base::Closure& callback);
206 // Called after async thumbnailer task completes. Scales and crops the result
207 // of the copy.
208 static void CopyFromCompositingSurfaceHasResult(
209 const gfx::Size& dst_size_in_pixel,
210 const SkColorType color_type,
211 ReadbackRequestCallback& callback,
212 scoped_ptr<cc::CopyOutputResult> result);
213 static void PrepareTextureCopyOutputResult(
214 const gfx::Size& dst_size_in_pixel,
215 const SkColorType color_type,
216 ReadbackRequestCallback& callback,
217 scoped_ptr<cc::CopyOutputResult> result);
218 static void PrepareBitmapCopyOutputResult(
219 const gfx::Size& dst_size_in_pixel,
220 const SkColorType color_type,
221 ReadbackRequestCallback& callback,
222 scoped_ptr<cc::CopyOutputResult> result);
223 static void CopyFromCompositingSurfaceHasResultForVideo(
224 base::WeakPtr<DelegatedFrameHost> rwhva,
225 scoped_refptr<OwnedMailbox> subscriber_texture,
226 scoped_refptr<media::VideoFrame> video_frame,
227 const base::Callback<void(bool)>& callback,
228 scoped_ptr<cc::CopyOutputResult> result);
229 static void CopyFromCompositingSurfaceFinishedForVideo(
230 base::WeakPtr<DelegatedFrameHost> rwhva,
231 const base::Callback<void(bool)>& callback,
232 scoped_refptr<OwnedMailbox> subscriber_texture,
233 scoped_ptr<cc::SingleReleaseCallback> release_callback,
234 bool result);
235 static void ReturnSubscriberTexture(
236 base::WeakPtr<DelegatedFrameHost> rwhva,
237 scoped_refptr<OwnedMailbox> subscriber_texture,
238 uint32 sync_point);
240 void SendDelegatedFrameAck(uint32 output_surface_id);
241 void SurfaceDrawn(uint32 output_surface_id, cc::SurfaceDrawStatus drawn);
242 void SendReturnedDelegatedResources(uint32 output_surface_id);
244 // Called to consult the current |frame_subscriber_|, to determine and maybe
245 // initiate a copy-into-video-frame request.
246 void DidReceiveFrameFromRenderer(const gfx::Rect& damage_rect);
248 DelegatedFrameHostClient* const client_;
249 ui::Compositor* compositor_;
251 // True if this renders into a Surface, false if it renders into a delegated
252 // layer.
253 bool use_surfaces_;
255 std::vector<base::Closure> on_compositing_did_commit_callbacks_;
257 // The vsync manager we are observing for changes, if any.
258 scoped_refptr<ui::CompositorVSyncManager> vsync_manager_;
260 // The current VSync timebase and interval. These are zero until the first
261 // call to SetVSyncParameters().
262 base::TimeTicks vsync_timebase_;
263 base::TimeDelta vsync_interval_;
265 // Overridable tick clock used for testing functions using current time.
266 scoped_ptr<base::TickClock> tick_clock_;
268 // With delegated renderer, this is the last output surface, used to
269 // disambiguate resources with the same id coming from different output
270 // surfaces.
271 uint32 last_output_surface_id_;
273 // The number of delegated frame acks that are pending, to delay resource
274 // returns until the acks are sent.
275 int pending_delegated_ack_count_;
277 // True after a delegated frame has been skipped, until a frame is not
278 // skipped.
279 bool skipped_frames_;
280 std::vector<ui::LatencyInfo> skipped_latency_info_list_;
282 // Holds delegated resources that have been given to a DelegatedFrameProvider,
283 // and gives back resources when they are no longer in use for return to the
284 // renderer.
285 scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
287 // Provides delegated frame updates to the cc::DelegatedRendererLayer.
288 scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
290 // State for rendering into a Surface.
291 scoped_ptr<cc::SurfaceIdAllocator> id_allocator_;
292 scoped_ptr<cc::SurfaceFactory> surface_factory_;
293 cc::SurfaceId surface_id_;
294 gfx::Size current_surface_size_;
295 float current_scale_factor_;
296 cc::ReturnedResourceArray surface_returned_resources_;
298 // This lock is the one waiting for a frame of the right size to come back
299 // from the renderer/GPU process. It is set from the moment the aura window
300 // got resized, to the moment we committed the renderer frame of the same
301 // size. It keeps track of the size we expect from the renderer, and locks the
302 // compositor, as well as the UI for a short time to give a chance to the
303 // renderer of producing a frame of the right size.
304 scoped_ptr<ResizeLock> resize_lock_;
306 // Keeps track of the current frame size.
307 gfx::Size current_frame_size_in_dip_;
309 // This lock is for waiting for a front surface to become available to draw.
310 scoped_refptr<ui::CompositorLock> released_front_lock_;
312 enum CanLockCompositorState {
313 YES_CAN_LOCK,
314 // We locked, so at some point we'll need to kick a frame.
315 YES_DID_LOCK,
316 // No. A lock timed out, we need to kick a new frame before locking again.
317 NO_PENDING_RENDERER_FRAME,
318 // No. We've got a frame, but it hasn't been committed.
319 NO_PENDING_COMMIT,
321 CanLockCompositorState can_lock_compositor_;
323 base::TimeTicks last_draw_ended_;
325 // Subscriber that listens to frame presentation events.
326 scoped_ptr<RenderWidgetHostViewFrameSubscriber> frame_subscriber_;
327 std::vector<scoped_refptr<OwnedMailbox> > idle_frame_subscriber_textures_;
329 // Callback used to pass the output request to the layer or to a function
330 // specified by a test.
331 base::Callback<void(scoped_ptr<cc::CopyOutputRequest>)>
332 request_copy_of_output_callback_for_testing_;
334 // YUV readback pipeline.
335 scoped_ptr<content::ReadbackYUVInterface>
336 yuv_readback_pipeline_;
338 scoped_ptr<DelegatedFrameEvictor> delegated_frame_evictor_;
341 } // namespace content
343 #endif // CONTENT_BROWSER_COMPOSITOR_DELEGATED_FRAME_HOST_H_