1 // Copyright (c) 2016 GitHub, Inc.
2 // Use of this source code is governed by the MIT license that can be
3 // found in the LICENSE file.
5 #ifndef ATOM_BROWSER_OSR_OSR_RENDER_WIDGET_HOST_VIEW_H_
6 #define ATOM_BROWSER_OSR_OSR_RENDER_WIDGET_HOST_VIEW_H_
15 #include "atom/browser/native_window.h"
16 #include "atom/browser/osr/osr_output_device.h"
17 #include "base/process/kill.h"
18 #include "base/threading/thread.h"
19 #include "base/time/time.h"
20 #include "cc/scheduler/begin_frame_source.h"
21 #include "cc/output/compositor_frame.h"
22 #include "content/browser/renderer_host/delegated_frame_host.h"
23 #include "content/browser/renderer_host/render_widget_host_impl.h"
24 #include "content/browser/renderer_host/render_widget_host_view_base.h"
25 #include "content/browser/renderer_host/resize_lock.h"
26 #include "ui/compositor/compositor.h"
27 #include "ui/compositor/layer_delegate.h"
28 #include "ui/compositor/layer_owner.h"
29 #include "ui/base/ime/text_input_client.h"
30 #include "ui/gfx/geometry/point.h"
31 #include "third_party/WebKit/public/platform/WebVector.h"
34 #include "ui/gfx/win/window_impl.h"
37 #if defined(OS_MACOSX)
38 #include "content/browser/renderer_host/browser_compositor_view_mac.h"
39 #include "ui/accelerated_widget_mac/accelerated_widget_mac.h"
42 #if defined(OS_MACOSX)
54 class AtomCopyFrameGenerator;
55 class AtomBeginFrameTimer;
57 class OffScreenRenderWidgetHostView
58 : public content::RenderWidgetHostViewBase,
59 #if defined(OS_MACOSX)
60 public ui::AcceleratedWidgetMacNSView,
62 public ui::CompositorDelegate,
63 public content::DelegatedFrameHostClient {
65 OffScreenRenderWidgetHostView(bool transparent,
66 const OnPaintCallback& callback,
67 content::RenderWidgetHost* render_widget_host,
68 NativeWindow* native_window);
69 ~OffScreenRenderWidgetHostView() override;
71 // content::RenderWidgetHostView:
72 bool OnMessageReceived(const IPC::Message&) override;
73 void InitAsChild(gfx::NativeView) override;
74 content::RenderWidgetHost* GetRenderWidgetHost(void) const override;
75 void SetSize(const gfx::Size &) override;
76 void SetBounds(const gfx::Rect &) override;
77 gfx::Vector2dF GetLastScrollOffset(void) const override;
78 gfx::NativeView GetNativeView(void) const override;
79 gfx::NativeViewAccessible GetNativeViewAccessible(void) override;
80 ui::TextInputClient* GetTextInputClient() override;
81 void Focus(void) override;
82 bool HasFocus(void) const override;
83 bool IsSurfaceAvailableForCopy(void) const override;
84 void Show(void) override;
85 void Hide(void) override;
86 bool IsShowing(void) override;
87 gfx::Rect GetViewBounds(void) const override;
88 gfx::Size GetVisibleViewportSize() const override;
89 void SetInsets(const gfx::Insets&) override;
90 void SetBackgroundColor(SkColor color) override;
91 bool LockMouse(void) override;
92 void UnlockMouse(void) override;
93 bool GetScreenColorProfile(std::vector<char>*) override;
94 #if defined(OS_MACOSX)
95 ui::AcceleratedWidgetMac* GetAcceleratedWidgetMac() const override;
96 void SetActive(bool active) override;
97 void ShowDefinitionForSelection() override;
98 bool SupportsSpeech() const override;
99 void SpeakSelection() override;
100 bool IsSpeaking() const override;
101 void StopSpeaking() override;
102 #endif // defined(OS_MACOSX)
104 // content::RenderWidgetHostViewBase:
105 void OnSwapCompositorFrame(uint32_t, std::unique_ptr<cc::CompositorFrame>)
107 void ClearCompositorFrame(void) override;
108 void InitAsPopup(content::RenderWidgetHostView *rwhv, const gfx::Rect& rect)
110 void InitAsFullscreen(content::RenderWidgetHostView *) override;
111 void UpdateCursor(const content::WebCursor &) override;
112 void SetIsLoading(bool is_loading) override;
113 void TextInputStateChanged(const content::TextInputState& params) override;
114 void ImeCancelComposition(void) override;
115 void RenderProcessGone(base::TerminationStatus, int) override;
116 void Destroy(void) override;
117 void SetTooltipText(const base::string16 &) override;
118 #if defined(OS_MACOSX)
119 void SelectionChanged(const base::string16& text,
121 const gfx::Range& range) override;
123 void SelectionBoundsChanged(const ViewHostMsg_SelectionBounds_Params &)
125 void CopyFromCompositingSurface(const gfx::Rect &,
127 const content::ReadbackRequestCallback &,
128 const SkColorType) override;
129 void CopyFromCompositingSurfaceToVideoFrame(
131 const scoped_refptr<media::VideoFrame> &,
132 const base::Callback<void(const gfx::Rect &, bool),
133 base::internal::CopyMode::Copyable> &) override;
134 bool CanCopyToVideoFrame(void) const override;
135 void BeginFrameSubscription(
136 std::unique_ptr<content::RenderWidgetHostViewFrameSubscriber>) override;
137 void EndFrameSubscription() override;
138 bool HasAcceleratedSurface(const gfx::Size &) override;
139 void GetScreenInfo(blink::WebScreenInfo *) override;
140 bool GetScreenColorProfile(blink::WebVector<char>*);
141 gfx::Rect GetBoundsInRootWindow(void) override;
142 void LockCompositingSurface(void) override;
143 void UnlockCompositingSurface(void) override;
144 void ImeCompositionRangeChanged(
145 const gfx::Range &, const std::vector<gfx::Rect>&) override;
146 gfx::Size GetPhysicalBackingSize() const override;
147 gfx::Size GetRequestedRendererSize() const override;
149 // content::DelegatedFrameHostClient:
150 int DelegatedFrameHostGetGpuMemoryBufferClientId(void) const;
151 ui::Layer *DelegatedFrameHostGetLayer(void) const override;
152 bool DelegatedFrameHostIsVisible(void) const override;
153 SkColor DelegatedFrameHostGetGutterColor(SkColor) const override;
154 gfx::Size DelegatedFrameHostDesiredSizeInDIP(void) const override;
155 bool DelegatedFrameCanCreateResizeLock(void) const override;
156 std::unique_ptr<content::ResizeLock> DelegatedFrameHostCreateResizeLock(
157 bool defer_compositor_lock) override;
158 void DelegatedFrameHostResizeLockWasReleased(void) override;
159 void DelegatedFrameHostSendCompositorSwapAck(
160 int, const cc::CompositorFrameAck &) override;
161 void DelegatedFrameHostSendReclaimCompositorResources(
162 int, const cc::CompositorFrameAck &) override;
163 void DelegatedFrameHostOnLostCompositorResources(void) override;
164 void DelegatedFrameHostUpdateVSyncParameters(
165 const base::TimeTicks &, const base::TimeDelta &) override;
166 void SetBeginFrameSource(cc::BeginFrameSource* source) override;
168 // ui::CompositorDelegate:
169 std::unique_ptr<cc::SoftwareOutputDevice> CreateSoftwareOutputDevice(
170 ui::Compositor* compositor) override;
172 bool InstallTransparency();
173 bool IsAutoResizeEnabled() const;
174 void OnSetNeedsBeginFrames(bool enabled);
176 #if defined(OS_MACOSX)
177 // ui::AcceleratedWidgetMacNSView:
178 NSView* AcceleratedWidgetGetNSView() const override;
179 void AcceleratedWidgetGetVSyncParameters(
180 base::TimeTicks* timebase, base::TimeDelta* interval) const override;
181 void AcceleratedWidgetSwapCompleted() override;
182 #endif // defined(OS_MACOSX)
184 void OnBeginFrameTimerTick();
185 void SendBeginFrame(base::TimeTicks frame_time,
186 base::TimeDelta vsync_period);
188 #if defined(OS_MACOSX)
189 void CreatePlatformWidget();
190 void DestroyPlatformWidget();
193 void OnPaint(const gfx::Rect& damage_rect, const SkBitmap& bitmap);
195 void SetPainting(bool painting);
196 bool IsPainting() const;
198 void SetFrameRate(int frame_rate);
199 int GetFrameRate() const;
201 ui::Compositor* compositor() const { return compositor_.get(); }
202 content::RenderWidgetHostImpl* render_widget_host() const
203 { return render_widget_host_; }
206 void SetupFrameRate(bool force);
207 void ResizeRootLayer();
210 content::RenderWidgetHostImpl* render_widget_host_;
211 NativeWindow* native_window_;
212 OffScreenOutputDevice* software_output_device_;
214 const bool transparent_;
215 OnPaintCallback callback_;
218 int frame_rate_threshold_ms_;
220 base::Time last_time_;
224 gfx::Vector2dF last_scroll_offset_;
228 std::unique_ptr<ui::Layer> root_layer_;
229 std::unique_ptr<ui::Compositor> compositor_;
230 std::unique_ptr<content::DelegatedFrameHost> delegated_frame_host_;
232 std::unique_ptr<AtomCopyFrameGenerator> copy_frame_generator_;
233 std::unique_ptr<AtomBeginFrameTimer> begin_frame_timer_;
235 #if defined(OS_MACOSX)
237 CALayer* background_layer_;
238 std::unique_ptr<content::BrowserCompositorMac> browser_compositor_;
240 // Selected text on the renderer.
241 std::string selected_text_;
244 base::WeakPtrFactory<OffScreenRenderWidgetHostView> weak_ptr_factory_;
246 DISALLOW_COPY_AND_ASSIGN(OffScreenRenderWidgetHostView);
251 #endif // ATOM_BROWSER_OSR_OSR_RENDER_WIDGET_HOST_VIEW_H_