adds invalidate method and proper resizing
[platform/framework/web/crosswalk-tizen.git] / atom / browser / osr / osr_render_widget_host_view.h
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.
4
5 #ifndef ATOM_BROWSER_OSR_OSR_RENDER_WIDGET_HOST_VIEW_H_
6 #define ATOM_BROWSER_OSR_OSR_RENDER_WIDGET_HOST_VIEW_H_
7
8 #include <string>
9 #include <vector>
10
11 #if defined(OS_WIN)
12 #include <windows.h>
13 #endif
14
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/output/compositor_frame.h"
21 #include "cc/scheduler/begin_frame_source.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 "third_party/WebKit/public/platform/WebVector.h"
27 #include "ui/base/ime/text_input_client.h"
28 #include "ui/compositor/compositor.h"
29 #include "ui/compositor/layer_delegate.h"
30 #include "ui/compositor/layer_owner.h"
31 #include "ui/gfx/geometry/point.h"
32
33 #if defined(OS_WIN)
34 #include "ui/gfx/win/window_impl.h"
35 #endif
36
37 #if defined(OS_MACOSX)
38 #include "content/browser/renderer_host/browser_compositor_view_mac.h"
39 #endif
40
41 #if defined(OS_MACOSX)
42 #ifdef __OBJC__
43 @class CALayer;
44 @class NSWindow;
45 #else
46 class CALayer;
47 class NSWindow;
48 #endif
49 #endif
50
51 namespace atom {
52
53 class AtomCopyFrameGenerator;
54 class AtomBeginFrameTimer;
55
56 #if defined(OS_MACOSX)
57 class MacHelper;
58 #endif
59
60 class OffScreenRenderWidgetHostView
61     : public content::RenderWidgetHostViewBase,
62       public ui::CompositorDelegate,
63       public content::DelegatedFrameHostClient {
64  public:
65   OffScreenRenderWidgetHostView(bool transparent,
66                                 const OnPaintCallback& callback,
67                                 content::RenderWidgetHost* render_widget_host,
68                                 NativeWindow* native_window);
69   ~OffScreenRenderWidgetHostView() override;
70
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 #if defined(OS_MACOSX)
94   ui::AcceleratedWidgetMac* GetAcceleratedWidgetMac() const override;
95   void SetActive(bool active) override;
96   void ShowDefinitionForSelection() override;
97   bool SupportsSpeech() const override;
98   void SpeakSelection() override;
99   bool IsSpeaking() const override;
100   void StopSpeaking() override;
101 #endif  // defined(OS_MACOSX)
102
103   // content::RenderWidgetHostViewBase:
104   void OnSwapCompositorFrame(uint32_t, cc::CompositorFrame)
105     override;
106   void ClearCompositorFrame(void) override;
107   void InitAsPopup(content::RenderWidgetHostView *rwhv, const gfx::Rect& rect)
108     override;
109   void InitAsFullscreen(content::RenderWidgetHostView *) override;
110   void UpdateCursor(const content::WebCursor &) override;
111   void SetIsLoading(bool is_loading) override;
112   void TextInputStateChanged(const content::TextInputState& params) override;
113   void ImeCancelComposition(void) override;
114   void RenderProcessGone(base::TerminationStatus, int) override;
115   void Destroy(void) override;
116   void SetTooltipText(const base::string16 &) override;
117 #if defined(OS_MACOSX)
118   void SelectionChanged(const base::string16& text,
119                         size_t offset,
120                         const gfx::Range& range) override;
121 #endif
122   void SelectionBoundsChanged(const ViewHostMsg_SelectionBounds_Params &)
123     override;
124   void CopyFromCompositingSurface(const gfx::Rect &,
125     const gfx::Size &,
126     const content::ReadbackRequestCallback &,
127     const SkColorType) override;
128   void CopyFromCompositingSurfaceToVideoFrame(
129     const gfx::Rect &,
130     const scoped_refptr<media::VideoFrame> &,
131     const base::Callback<void(const gfx::Rect &, bool),
132     base::internal::CopyMode::Copyable> &) override;
133   bool CanCopyToVideoFrame(void) const override;
134   void BeginFrameSubscription(
135     std::unique_ptr<content::RenderWidgetHostViewFrameSubscriber>) override;
136   void EndFrameSubscription() override;
137   bool HasAcceleratedSurface(const gfx::Size &) override;
138   void GetScreenInfo(blink::WebScreenInfo *) override;
139   bool GetScreenColorProfile(blink::WebVector<char>*);
140   gfx::Rect GetBoundsInRootWindow(void) override;
141   void LockCompositingSurface(void) override;
142   void UnlockCompositingSurface(void) override;
143   void ImeCompositionRangeChanged(
144     const gfx::Range &, const std::vector<gfx::Rect>&) override;
145   gfx::Size GetPhysicalBackingSize() const override;
146   gfx::Size GetRequestedRendererSize() const override;
147
148   // content::DelegatedFrameHostClient:
149   int DelegatedFrameHostGetGpuMemoryBufferClientId(void) const;
150   ui::Layer *DelegatedFrameHostGetLayer(void) const override;
151   bool DelegatedFrameHostIsVisible(void) const override;
152   SkColor DelegatedFrameHostGetGutterColor(SkColor) const override;
153   gfx::Size DelegatedFrameHostDesiredSizeInDIP(void) const override;
154   bool DelegatedFrameCanCreateResizeLock(void) const override;
155   std::unique_ptr<content::ResizeLock> DelegatedFrameHostCreateResizeLock(
156     bool defer_compositor_lock) override;
157   void DelegatedFrameHostResizeLockWasReleased(void) override;
158   void DelegatedFrameHostSendCompositorSwapAck(
159     int, const cc::CompositorFrameAck &) override;
160   void DelegatedFrameHostSendReclaimCompositorResources(
161     int, const cc::CompositorFrameAck &) override;
162   void DelegatedFrameHostOnLostCompositorResources(void) override;
163   void DelegatedFrameHostUpdateVSyncParameters(
164     const base::TimeTicks &, const base::TimeDelta &) override;
165   void SetBeginFrameSource(cc::BeginFrameSource* source) override;
166
167   // ui::CompositorDelegate:
168   std::unique_ptr<cc::SoftwareOutputDevice> CreateSoftwareOutputDevice(
169       ui::Compositor* compositor) override;
170
171   bool InstallTransparency();
172   bool IsAutoResizeEnabled() const;
173   void OnSetNeedsBeginFrames(bool enabled);
174
175   void OnBeginFrameTimerTick();
176   void SendBeginFrame(base::TimeTicks frame_time,
177                       base::TimeDelta vsync_period);
178
179 #if defined(OS_MACOSX)
180   void CreatePlatformWidget();
181   void DestroyPlatformWidget();
182 #endif
183
184   void OnPaint(const gfx::Rect& damage_rect, const SkBitmap& bitmap);
185
186   void SetPainting(bool painting);
187   bool IsPainting() const;
188
189   void SetFrameRate(int frame_rate);
190   int GetFrameRate() const;
191
192   ui::Compositor* GetCompositor() const;
193   ui::Layer* GetRootLayer() const;
194   content::DelegatedFrameHost* GetDelegatedFrameHost() const;
195
196   void Invalidate();
197
198   content::RenderWidgetHostImpl* render_widget_host() const
199       { return render_widget_host_; }
200   NativeWindow* window() const { return native_window_; }
201
202  private:
203   void SetupFrameRate(bool force);
204   void ResizeRootLayer();
205
206   // Weak ptrs.
207   content::RenderWidgetHostImpl* render_widget_host_;
208   NativeWindow* native_window_;
209   OffScreenOutputDevice* software_output_device_;
210
211   const bool transparent_;
212   OnPaintCallback callback_;
213
214   int frame_rate_;
215   int frame_rate_threshold_ms_;
216
217   base::Time last_time_;
218
219   float scale_factor_;
220   bool is_showing_;
221   gfx::Vector2dF last_scroll_offset_;
222   gfx::Size size_;
223   bool painting_;
224
225   std::unique_ptr<ui::Layer> root_layer_;
226   std::unique_ptr<ui::Compositor> compositor_;
227   std::unique_ptr<content::DelegatedFrameHost> delegated_frame_host_;
228
229   std::unique_ptr<AtomCopyFrameGenerator> copy_frame_generator_;
230   std::unique_ptr<AtomBeginFrameTimer> begin_frame_timer_;
231
232 #if defined(OS_MACOSX)
233   CALayer* background_layer_;
234   std::unique_ptr<content::BrowserCompositorMac> browser_compositor_;
235
236   // Can not be managed by smart pointer because its header can not be included
237   // in the file that has the destructor.
238   MacHelper* mac_helper_;
239
240   // Selected text on the renderer.
241   std::string selected_text_;
242 #endif
243
244   base::WeakPtrFactory<OffScreenRenderWidgetHostView> weak_ptr_factory_;
245
246   DISALLOW_COPY_AND_ASSIGN(OffScreenRenderWidgetHostView);
247 };
248
249 }  // namespace atom
250
251 #endif  // ATOM_BROWSER_OSR_OSR_RENDER_WIDGET_HOST_VIEW_H_