Home | History | Annotate | Download | only in compositor
      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.
      4 
      5 #ifndef CONTENT_BROWSER_COMPOSITOR_DELEGATED_FRAME_HOST_H_
      6 #define CONTENT_BROWSER_COMPOSITOR_DELEGATED_FRAME_HOST_H_
      7 
      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/public/browser/render_process_host.h"
     17 #include "ui/compositor/compositor.h"
     18 #include "ui/compositor/compositor_observer.h"
     19 #include "ui/compositor/compositor_vsync_manager.h"
     20 #include "ui/compositor/layer.h"
     21 #include "ui/compositor/layer_owner_delegate.h"
     22 #include "ui/gfx/rect_conversions.h"
     23 
     24 namespace media {
     25 class VideoFrame;
     26 }
     27 
     28 namespace content {
     29 
     30 class DelegatedFrameHost;
     31 class ReadbackYUVInterface;
     32 class RenderWidgetHostViewFrameSubscriber;
     33 class RenderWidgetHostImpl;
     34 class ResizeLock;
     35 
     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;
     49 
     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;
     54 
     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);
     62 };
     63 
     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();
     79 
     80   bool CanCopyToBitmap() const;
     81 
     82   // Public interface exposed to RenderWidgetHostView.
     83   void SwapDelegatedFrame(
     84       uint32 output_surface_id,
     85       scoped_ptr<cc::DelegatedFrameData> frame_data,
     86       float frame_device_scale_factor,
     87       const std::vector<ui::LatencyInfo>& latency_info);
     88   void WasHidden();
     89   void WasShown();
     90   void WasResized();
     91   gfx::Size GetRequestedRendererSize() const;
     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();
    108 
    109   // Exposed for tests.
    110   cc::DelegatedFrameProvider* FrameProviderForTesting() const {
    111     return frame_provider_.get();
    112   }
    113   void OnCompositingDidCommitForTesting(ui::Compositor* compositor) {
    114     OnCompositingDidCommit(compositor);
    115   }
    116   bool ShouldCreateResizeLockForTesting() { return ShouldCreateResizeLock(); }
    117 
    118  private:
    119   friend class DelegatedFrameHostClient;
    120   FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
    121                            SkippedDelegatedFrames);
    122   FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
    123                            DiscardDelegatedFramesWithLocking);
    124   FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraCopyRequestTest,
    125                            DestroyedAfterCopyRequest);
    126 
    127   RenderWidgetHostViewFrameSubscriber* frame_subscriber() const {
    128     return frame_subscriber_.get();
    129   }
    130   bool ShouldCreateResizeLock();
    131   void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
    132 
    133   void LockResources();
    134   void UnlockResources();
    135 
    136   // Overridden from ui::CompositorObserver:
    137   virtual void OnCompositingDidCommit(ui::Compositor* compositor) OVERRIDE;
    138   virtual void OnCompositingStarted(ui::Compositor* compositor,
    139                                     base::TimeTicks start_time) OVERRIDE;
    140   virtual void OnCompositingEnded(ui::Compositor* compositor) OVERRIDE;
    141   virtual void OnCompositingAborted(ui::Compositor* compositor) OVERRIDE;
    142   virtual void OnCompositingLockStateChanged(
    143       ui::Compositor* compositor) OVERRIDE;
    144 
    145   // Overridden from ui::CompositorVSyncManager::Observer:
    146   virtual void OnUpdateVSyncParameters(base::TimeTicks timebase,
    147                                        base::TimeDelta interval) OVERRIDE;
    148 
    149   // Overridden from ui::LayerOwnerObserver:
    150   virtual void OnLayerRecreated(ui::Layer* old_layer,
    151                                 ui::Layer* new_layer) OVERRIDE;
    152 
    153   // Overridden from ImageTransportFactoryObserver:
    154   virtual void OnLostResources() OVERRIDE;
    155 
    156   bool ShouldSkipFrame(gfx::Size size_in_dip) const;
    157 
    158   // Lazily grab a resize lock if the aura window size doesn't match the current
    159   // frame size, to give time to the renderer.
    160   void MaybeCreateResizeLock();
    161 
    162   // Checks if the resize lock can be released because we received an new frame.
    163   void CheckResizeLock();
    164 
    165   // Run all on compositing commit callbacks.
    166   void RunOnCommitCallbacks();
    167 
    168   // Add on compositing commit callback.
    169   void AddOnCommitCallbackAndDisableLocks(const base::Closure& callback);
    170 
    171   // Called after async thumbnailer task completes.  Scales and crops the result
    172   // of the copy.
    173   static void CopyFromCompositingSurfaceHasResult(
    174       const gfx::Size& dst_size_in_pixel,
    175       const SkBitmap::Config config,
    176       const base::Callback<void(bool, const SkBitmap&)>& callback,
    177       scoped_ptr<cc::CopyOutputResult> result);
    178   static void PrepareTextureCopyOutputResult(
    179       const gfx::Size& dst_size_in_pixel,
    180       const SkBitmap::Config config,
    181       const base::Callback<void(bool, const SkBitmap&)>& callback,
    182       scoped_ptr<cc::CopyOutputResult> result);
    183   static void PrepareBitmapCopyOutputResult(
    184       const gfx::Size& dst_size_in_pixel,
    185       const SkBitmap::Config config,
    186       const base::Callback<void(bool, const SkBitmap&)>& callback,
    187       scoped_ptr<cc::CopyOutputResult> result);
    188   static void CopyFromCompositingSurfaceHasResultForVideo(
    189       base::WeakPtr<DelegatedFrameHost> rwhva,
    190       scoped_refptr<OwnedMailbox> subscriber_texture,
    191       scoped_refptr<media::VideoFrame> video_frame,
    192       const base::Callback<void(bool)>& callback,
    193       scoped_ptr<cc::CopyOutputResult> result);
    194   static void CopyFromCompositingSurfaceFinishedForVideo(
    195       base::WeakPtr<DelegatedFrameHost> rwhva,
    196       const base::Callback<void(bool)>& callback,
    197       scoped_refptr<OwnedMailbox> subscriber_texture,
    198       scoped_ptr<cc::SingleReleaseCallback> release_callback,
    199       bool result);
    200   static void ReturnSubscriberTexture(
    201       base::WeakPtr<DelegatedFrameHost> rwhva,
    202       scoped_refptr<OwnedMailbox> subscriber_texture,
    203       uint32 sync_point);
    204 
    205   void SendDelegatedFrameAck(uint32 output_surface_id);
    206   void SendReturnedDelegatedResources(uint32 output_surface_id);
    207 
    208   // DelegatedFrameEvictorClient implementation.
    209   virtual void EvictDelegatedFrame() OVERRIDE;
    210 
    211   // cc::DelegatedFrameProviderClient implementation.
    212   virtual void UnusedResourcesAreAvailable() OVERRIDE;
    213 
    214   void DidReceiveFrameFromRenderer();
    215 
    216   DelegatedFrameHostClient* client_;
    217 
    218   std::vector<base::Closure> on_compositing_did_commit_callbacks_;
    219 
    220   // The vsync manager we are observing for changes, if any.
    221   scoped_refptr<ui::CompositorVSyncManager> vsync_manager_;
    222 
    223   // With delegated renderer, this is the last output surface, used to
    224   // disambiguate resources with the same id coming from different output
    225   // surfaces.
    226   uint32 last_output_surface_id_;
    227 
    228   // The number of delegated frame acks that are pending, to delay resource
    229   // returns until the acks are sent.
    230   int pending_delegated_ack_count_;
    231 
    232   // True after a delegated frame has been skipped, until a frame is not
    233   // skipped.
    234   bool skipped_frames_;
    235   std::vector<ui::LatencyInfo> skipped_latency_info_list_;
    236 
    237   // Holds delegated resources that have been given to a DelegatedFrameProvider,
    238   // and gives back resources when they are no longer in use for return to the
    239   // renderer.
    240   scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
    241 
    242   // Provides delegated frame updates to the cc::DelegatedRendererLayer.
    243   scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
    244 
    245   // This lock is the one waiting for a frame of the right size to come back
    246   // from the renderer/GPU process. It is set from the moment the aura window
    247   // got resized, to the moment we committed the renderer frame of the same
    248   // size. It keeps track of the size we expect from the renderer, and locks the
    249   // compositor, as well as the UI for a short time to give a chance to the
    250   // renderer of producing a frame of the right size.
    251   scoped_ptr<ResizeLock> resize_lock_;
    252 
    253   // Keeps track of the current frame size.
    254   gfx::Size current_frame_size_in_dip_;
    255 
    256   // This lock is for waiting for a front surface to become available to draw.
    257   scoped_refptr<ui::CompositorLock> released_front_lock_;
    258 
    259   enum CanLockCompositorState {
    260     YES_CAN_LOCK,
    261     // We locked, so at some point we'll need to kick a frame.
    262     YES_DID_LOCK,
    263     // No. A lock timed out, we need to kick a new frame before locking again.
    264     NO_PENDING_RENDERER_FRAME,
    265     // No. We've got a frame, but it hasn't been committed.
    266     NO_PENDING_COMMIT,
    267   };
    268   CanLockCompositorState can_lock_compositor_;
    269 
    270   base::TimeTicks last_draw_ended_;
    271 
    272   // Subscriber that listens to frame presentation events.
    273   scoped_ptr<RenderWidgetHostViewFrameSubscriber> frame_subscriber_;
    274   std::vector<scoped_refptr<OwnedMailbox> > idle_frame_subscriber_textures_;
    275 
    276   // YUV readback pipeline.
    277   scoped_ptr<content::ReadbackYUVInterface>
    278       yuv_readback_pipeline_;
    279 
    280   scoped_ptr<DelegatedFrameEvictor> delegated_frame_evictor_;
    281 };
    282 
    283 }  // namespace content
    284 
    285 #endif  // CONTENT_BROWSER_COMPOSITOR_DELEGATED_FRAME_HOST_H_
    286