| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_BROWSER_COMPOSITOR_VIEW_MAC_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_BROWSER_COMPOSITOR_VIEW_MAC_H_ |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_BROWSER_COMPOSITOR_VIEW_MAC_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_BROWSER_COMPOSITOR_VIEW_MAC_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "content/browser/renderer_host/delegated_frame_host.h" |
| 11 #include "ui/compositor/compositor.h" | 12 #include "ui/compositor/compositor.h" |
| 12 #include "ui/compositor/compositor_observer.h" | 13 #include "ui/compositor/compositor_observer.h" |
| 13 | 14 |
| 14 namespace ui { | 15 namespace ui { |
| 15 class AcceleratedWidgetMac; | 16 class AcceleratedWidgetMac; |
| 17 class AcceleratedWidgetMacNSView; |
| 16 } | 18 } |
| 17 | 19 |
| 18 namespace content { | 20 namespace content { |
| 19 | 21 |
| 20 // A ui::Compositor and a gfx::AcceleratedWidget (and helper) that it draws | 22 class RecyclableCompositorMac; |
| 21 // into. This structure is used to efficiently recycle these structures across | 23 |
| 22 // tabs (because creating a new ui::Compositor for each tab would be expensive | 24 |
| 23 // in terms of time and resources). | 25 // This class owns a DelegatedFrameHost, and will dynamically attach and |
| 24 class BrowserCompositorMac : public ui::CompositorObserver { | 26 // detach it from a ui::Compositor as needed. The ui::Compositor will be |
| 27 // detached from the DelegatedFrameHost when the following conditions are |
| 28 // all met: |
| 29 // - There are no outstanding copy requests |
| 30 // - The RenderWidgetHostImpl providing frames to the DelegatedFrameHost |
| 31 // is visible. |
| 32 // - The RenderWidgetHostViewMac that is used to display these frames is |
| 33 // attached to the NSView hierarchy of an NSWindow. |
| 34 class BrowserCompositorMac { |
| 25 public: | 35 public: |
| 26 virtual ~BrowserCompositorMac(); | 36 BrowserCompositorMac( |
| 37 ui::AcceleratedWidgetMacNSView* accelerated_widget_mac_ns_view, |
| 38 DelegatedFrameHostClient* delegated_frame_host_client, |
| 39 bool render_widget_host_is_hidden, |
| 40 bool ns_view_attached_to_window); |
| 41 ~BrowserCompositorMac(); |
| 27 | 42 |
| 28 // Create a compositor, or recycle a preexisting one. | 43 // This must be called before the destructor. |
| 29 static std::unique_ptr<BrowserCompositorMac> Create(); | 44 // TODO(ccameron): This is because the RWHVMac is still the |
| 45 // DelegatedFrameHostClient. When that is cleaned up, this can be rolled |
| 46 // into the destructor. |
| 47 void Destroy(); |
| 30 | 48 |
| 31 // Delete a compositor, or allow it to be recycled. | 49 // These will not return nullptr until Destroy is called. |
| 32 static void Recycle(std::unique_ptr<BrowserCompositorMac> compositor); | 50 ui::Layer* GetRootLayer(); |
| 51 DelegatedFrameHost* GetDelegatedFrameHost(); |
| 52 |
| 53 // This may return nullptr, if this has detached itself from its |
| 54 // ui::Compositor. |
| 55 ui::AcceleratedWidgetMac* GetAcceleratedWidgetMac(); |
| 56 |
| 57 void SwapCompositorFrame(uint32_t output_surface_id, |
| 58 cc::CompositorFrame frame); |
| 59 void SetHasTransparentBackground(bool transparent); |
| 60 void UpdateVSyncParameters(const base::TimeTicks& timebase, |
| 61 const base::TimeDelta& interval); |
| 62 |
| 63 // This is used to ensure that the ui::Compositor be attached to the |
| 64 // DelegatedFrameHost while the RWHImpl is visible. |
| 65 // Note: This should be called before the RWHImpl is made visible and after |
| 66 // it has been hidden, in order to ensure that thumbnailer notifications to |
| 67 // initiate copies occur before the ui::Compositor be detached. |
| 68 void SetRenderWidgetHostIsHidden(bool hidden); |
| 69 |
| 70 // This is used to ensure that the ui::Compositor be attached to this |
| 71 // NSView while its contents may be visible on-screen, even if the RWHImpl is |
| 72 // hidden (e.g, because it is occluded by another window). |
| 73 void SetNSViewAttachedToWindow(bool attached); |
| 74 |
| 75 // These functions will track the number of outstanding copy requests, and |
| 76 // will not allow the ui::Compositor to be detached until all outstanding |
| 77 // copies have returned. |
| 78 void CopyFromCompositingSurface(const gfx::Rect& src_subrect, |
| 79 const gfx::Size& dst_size, |
| 80 const ReadbackRequestCallback& callback, |
| 81 SkColorType preferred_color_type); |
| 82 void CopyFromCompositingSurfaceToVideoFrame( |
| 83 const gfx::Rect& src_subrect, |
| 84 const scoped_refptr<media::VideoFrame>& target, |
| 85 const base::Callback<void(const gfx::Rect&, bool)>& callback); |
| 33 | 86 |
| 34 // Indicate that the recyclable compositor should be destroyed, and no future | 87 // Indicate that the recyclable compositor should be destroyed, and no future |
| 35 // compositors should be recycled. | 88 // compositors should be recycled. |
| 36 static void DisableRecyclingForShutdown(); | 89 static void DisableRecyclingForShutdown(); |
| 37 | 90 |
| 38 ui::Compositor* compositor() { return &compositor_; } | 91 private: |
| 39 ui::AcceleratedWidgetMac* accelerated_widget_mac() { | 92 // The state of |delegated_frame_host_| and |recyclable_compositor_| to |
| 40 return accelerated_widget_mac_.get(); | 93 // manage being visible, hidden, or occluded. |
| 41 } | 94 enum State { |
| 95 // Effects: |
| 96 // - |recyclable_compositor_| exists and is attached to |
| 97 // |delegated_frame_host_|. |
| 98 // Happens when: |
| 99 // - |render_widet_host_| is in the visible state, or there are |
| 100 // outstanding copy requests. |
| 101 HasAttachedCompositor, |
| 102 // Effects: |
| 103 // - |recyclable_compositor_| exists, but |delegated_frame_host_| is |
| 104 // hidden and detached from it. |
| 105 // Happens when: |
| 106 // - The |render_widget_host_| is hidden, but |cocoa_view_| is still in the |
| 107 // NSWindow hierarchy (e.g, when the window is occluded or offscreen). |
| 108 // - Note: In this state, |recyclable_compositor_| and its CALayers are kept |
| 109 // around so that we will have content to show when we are un-occluded. If |
| 110 // we had a way to keep the CALayers attached to the NSView while |
| 111 // detaching the ui::Compositor, then there would be no need for this |
| 112 HasDetachedCompositor, |
| 113 // Effects: |
| 114 // - |recyclable_compositor_| has been recycled and |delegated_frame_host_| |
| 115 // is hidden and detached from it. |
| 116 // Happens when: |
| 117 // - The |render_widget_host_| hidden or gone, and |cocoa_view_| is not |
| 118 // attached to an NSWindow. |
| 119 // - This happens for backgrounded tabs. |
| 120 HasNoCompositor, |
| 121 }; |
| 122 State state_ = HasNoCompositor; |
| 123 void UpdateState(); |
| 124 void TransitionToState(State new_state); |
| 42 | 125 |
| 43 // Suspend will prevent the compositor from producing new frames. This should | 126 static void CopyCompleted( |
| 44 // be called to avoid creating spurious frames while changing state. | 127 base::WeakPtr<BrowserCompositorMac> browser_compositor, |
| 45 // Compositors are created as suspended. | 128 const ReadbackRequestCallback& callback, |
| 46 void Suspend(); | 129 const SkBitmap& bitmap, |
| 47 void Unsuspend(); | 130 ReadbackResponse response); |
| 131 static void CopyToVideoFrameCompleted( |
| 132 base::WeakPtr<BrowserCompositorMac> browser_compositor, |
| 133 const base::Callback<void(const gfx::Rect&, bool)>& callback, |
| 134 const gfx::Rect& rect, |
| 135 bool result); |
| 136 uint64_t outstanding_copy_count_ = 0; |
| 48 | 137 |
| 49 private: | 138 bool render_widget_host_is_hidden_ = true; |
| 50 BrowserCompositorMac(); | 139 bool ns_view_attached_to_window_ = false; |
| 51 | 140 |
| 52 // ui::CompositorObserver implementation: | 141 ui::AcceleratedWidgetMacNSView* accelerated_widget_mac_ns_view_ = nullptr; |
| 53 void OnCompositingDidCommit(ui::Compositor* compositor) override; | 142 std::unique_ptr<RecyclableCompositorMac> recyclable_compositor_; |
| 54 void OnCompositingStarted(ui::Compositor* compositor, | |
| 55 base::TimeTicks start_time) override {} | |
| 56 void OnCompositingEnded(ui::Compositor* compositor) override {} | |
| 57 void OnCompositingAborted(ui::Compositor* compositor) override {} | |
| 58 void OnCompositingLockStateChanged(ui::Compositor* compositor) override {} | |
| 59 void OnCompositingShuttingDown(ui::Compositor* compositor) override {} | |
| 60 | 143 |
| 61 std::unique_ptr<ui::AcceleratedWidgetMac> accelerated_widget_mac_; | 144 std::unique_ptr<DelegatedFrameHost> delegated_frame_host_; |
| 62 ui::Compositor compositor_; | 145 std::unique_ptr<ui::Layer> root_layer_; |
| 63 scoped_refptr<ui::CompositorLock> compositor_suspended_lock_; | |
| 64 | 146 |
| 65 DISALLOW_COPY_AND_ASSIGN(BrowserCompositorMac); | 147 bool has_transparent_background_ = false; |
| 66 }; | |
| 67 | 148 |
| 68 // A class to keep around whenever a BrowserCompositorMac may be created. | 149 bool has_been_destroyed_ = false; |
| 69 // While at least one instance of this class exists, a spare | |
| 70 // BrowserCompositorViewCocoa will be kept around to be recycled so that the | |
| 71 // next BrowserCompositorMac to be created will be be created quickly. | |
| 72 class BrowserCompositorMacPlaceholder { | |
| 73 public: | |
| 74 BrowserCompositorMacPlaceholder(); | |
| 75 ~BrowserCompositorMacPlaceholder(); | |
| 76 | 150 |
| 77 private: | 151 base::WeakPtrFactory<BrowserCompositorMac> weak_factory_; |
| 78 DISALLOW_COPY_AND_ASSIGN(BrowserCompositorMacPlaceholder); | |
| 79 }; | 152 }; |
| 80 | 153 |
| 81 } // namespace content | 154 } // namespace content |
| 82 | 155 |
| 83 #endif // CONTENT_BROWSER_RENDERER_HOST_BROWSER_COMPOSITOR_VIEW_MAC_H_ | 156 #endif // CONTENT_BROWSER_RENDERER_HOST_BROWSER_COMPOSITOR_VIEW_MAC_H_ |
| OLD | NEW |