electron/atom/browser/osr/osr_render_widget_host_view.h

358 lines
12 KiB
C
Raw Normal View History

2016-07-30 19:35:14 +00:00
// Copyright (c) 2016 GitHub, Inc.
2016-07-05 19:33:22 +00:00
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_OSR_OSR_RENDER_WIDGET_HOST_VIEW_H_
#define ATOM_BROWSER_OSR_OSR_RENDER_WIDGET_HOST_VIEW_H_
2016-07-05 19:33:22 +00:00
2017-03-05 15:18:57 +00:00
#include <set>
2016-07-31 10:19:56 +00:00
#include <string>
#include <vector>
#if defined(OS_WIN)
#include <windows.h>
#endif
2016-07-27 12:36:22 +00:00
#include "atom/browser/native_window.h"
#include "atom/browser/native_window_observer.h"
#include "atom/browser/osr/osr_output_device.h"
#include "atom/browser/osr/osr_view_proxy.h"
2016-07-29 22:51:19 +00:00
#include "base/process/kill.h"
2016-07-05 19:33:22 +00:00
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "cc/output/compositor_frame.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"
#include "components/viz/common/frame_sinks/begin_frame_source.h"
#include "content/browser/frame_host/render_widget_host_view_guest.h"
#include "content/browser/renderer_host/compositor_resize_lock.h"
#include "content/browser/renderer_host/delegated_frame_host.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_view_base.h"
#include "content/browser/web_contents/web_contents_view.h"
#include "third_party/skia/include/core/SkBitmap.h"
2016-08-26 22:30:02 +00:00
#include "third_party/WebKit/public/platform/WebVector.h"
#include "ui/base/ime/text_input_client.h"
2016-07-05 19:33:22 +00:00
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer_delegate.h"
#include "ui/compositor/layer_owner.h"
#include "ui/gfx/geometry/point.h"
2016-07-05 19:33:22 +00:00
2016-07-18 14:16:23 +00:00
#if defined(OS_WIN)
2016-07-05 19:33:22 +00:00
#include "ui/gfx/win/window_impl.h"
2016-07-18 14:16:23 +00:00
#endif
#if defined(OS_MACOSX)
#include "content/browser/renderer_host/browser_compositor_view_mac.h"
#endif
#if defined(OS_MACOSX)
#ifdef __OBJC__
@class CALayer;
@class NSWindow;
#else
class CALayer;
class NSWindow;
#endif
#endif
2016-07-05 19:33:22 +00:00
namespace atom {
2016-07-27 18:31:03 +00:00
class AtomCopyFrameGenerator;
class AtomBeginFrameTimer;
2016-07-18 14:16:23 +00:00
#if defined(OS_MACOSX)
2016-09-06 08:24:37 +00:00
class MacHelper;
#endif
2016-09-06 08:24:37 +00:00
class OffScreenRenderWidgetHostView
: public content::RenderWidgetHostViewBase,
2016-08-03 04:46:34 +00:00
public ui::CompositorDelegate,
2016-11-30 07:30:03 +00:00
#if !defined(OS_MACOSX)
public content::DelegatedFrameHostClient,
public content::CompositorResizeLockClient,
2016-11-30 07:30:03 +00:00
#endif
public NativeWindowObserver,
public OffscreenViewProxyObserver {
2016-07-31 10:19:56 +00:00
public:
2016-08-03 04:46:34 +00:00
OffScreenRenderWidgetHostView(bool transparent,
bool painting,
int frame_rate,
const OnPaintCallback& callback,
2016-08-03 04:46:34 +00:00
content::RenderWidgetHost* render_widget_host,
OffScreenRenderWidgetHostView* parent_host_view,
2016-08-03 04:46:34 +00:00
NativeWindow* native_window);
~OffScreenRenderWidgetHostView() override;
2016-07-05 19:33:22 +00:00
2016-08-03 04:46:34 +00:00
// content::RenderWidgetHostView:
2016-07-05 19:33:22 +00:00
bool OnMessageReceived(const IPC::Message&) override;
2016-07-31 10:19:56 +00:00
void InitAsChild(gfx::NativeView) override;
content::RenderWidgetHost* GetRenderWidgetHost(void) const override;
void SetSize(const gfx::Size &) override;
void SetBounds(const gfx::Rect &) override;
gfx::Vector2dF GetLastScrollOffset(void) const override;
gfx::NativeView GetNativeView(void) const override;
gfx::NativeViewAccessible GetNativeViewAccessible(void) override;
ui::TextInputClient* GetTextInputClient() override;
void Focus(void) override;
bool HasFocus(void) const override;
bool IsSurfaceAvailableForCopy(void) const override;
void Show(void) override;
void Hide(void) override;
bool IsShowing(void) override;
gfx::Rect GetViewBounds(void) const override;
gfx::Size GetVisibleViewportSize() const override;
void SetInsets(const gfx::Insets&) override;
2016-08-01 11:27:39 +00:00
void SetBackgroundColor(SkColor color) override;
2017-07-08 19:25:15 +00:00
SkColor background_color() const override;
2016-07-31 10:19:56 +00:00
bool LockMouse(void) override;
void UnlockMouse(void) override;
2016-11-30 07:30:03 +00:00
void SetNeedsBeginFrames(bool needs_begin_frames) override;
2016-07-31 10:19:56 +00:00
#if defined(OS_MACOSX)
ui::AcceleratedWidgetMac* GetAcceleratedWidgetMac() const override;
void SetActive(bool active) override;
void ShowDefinitionForSelection() override;
bool SupportsSpeech() const override;
void SpeakSelection() override;
bool IsSpeaking() const override;
void StopSpeaking() override;
#endif // defined(OS_MACOSX)
2016-08-03 04:46:34 +00:00
// content::RenderWidgetHostViewBase:
2017-07-08 19:25:15 +00:00
void DidCreateNewRendererCompositorFrameSink(
viz::mojom::CompositorFrameSinkClient* renderer_compositor_frame_sink)
2017-07-08 19:25:15 +00:00
override;
void SubmitCompositorFrame(const viz::LocalSurfaceId& local_surface_id,
2017-07-08 19:25:15 +00:00
cc::CompositorFrame frame) override;
2016-07-31 10:19:56 +00:00
void ClearCompositorFrame(void) override;
void InitAsPopup(content::RenderWidgetHostView *rwhv, const gfx::Rect& rect)
override;
void InitAsFullscreen(content::RenderWidgetHostView *) override;
void UpdateCursor(const content::WebCursor &) override;
void SetIsLoading(bool is_loading) override;
void TextInputStateChanged(const content::TextInputState& params) override;
void ImeCancelComposition(void) override;
void RenderProcessGone(base::TerminationStatus, int) override;
void Destroy(void) override;
void SetTooltipText(const base::string16 &) override;
2016-07-27 17:44:41 +00:00
#if defined(OS_MACOSX)
2016-07-31 10:19:56 +00:00
void SelectionChanged(const base::string16& text,
size_t offset,
const gfx::Range& range) override;
#endif
void SelectionBoundsChanged(const ViewHostMsg_SelectionBounds_Params &)
override;
2017-04-05 08:31:22 +00:00
void CopyFromSurface(
const gfx::Rect& src_subrect,
const gfx::Size& dst_size,
const content::ReadbackRequestCallback& callback,
const SkColorType color_type) override;
void CopyFromSurfaceToVideoFrame(
const gfx::Rect& src_subrect,
scoped_refptr<media::VideoFrame> target,
const base::Callback<void(const gfx::Rect&, bool)>& callback) override;
2016-07-31 10:19:56 +00:00
void BeginFrameSubscription(
std::unique_ptr<content::RenderWidgetHostViewFrameSubscriber>) override;
void EndFrameSubscription() override;
void InitAsGuest(
2017-03-05 15:18:57 +00:00
content::RenderWidgetHostView*,
content::RenderWidgetHostViewGuest*) override;
2016-07-31 10:19:56 +00:00
bool HasAcceleratedSurface(const gfx::Size &) override;
gfx::Rect GetBoundsInRootWindow(void) override;
2016-07-05 19:33:22 +00:00
void ImeCompositionRangeChanged(
2016-07-18 14:16:23 +00:00
const gfx::Range &, const std::vector<gfx::Rect>&) override;
2016-07-13 15:43:00 +00:00
gfx::Size GetPhysicalBackingSize() const override;
gfx::Size GetRequestedRendererSize() const override;
2016-07-05 19:33:22 +00:00
content::RenderWidgetHostViewBase* CreateViewForWidget(
content::RenderWidgetHost*,
content::RenderWidgetHost*,
content::WebContentsView*) override;
2016-11-30 07:30:03 +00:00
#if !defined(OS_MACOSX)
2016-08-03 04:46:34 +00:00
// content::DelegatedFrameHostClient:
2016-07-05 19:33:22 +00:00
int DelegatedFrameHostGetGpuMemoryBufferClientId(void) const;
2016-07-18 14:16:23 +00:00
ui::Layer *DelegatedFrameHostGetLayer(void) const override;
bool DelegatedFrameHostIsVisible(void) const override;
SkColor DelegatedFrameHostGetGutterColor(SkColor) const override;
gfx::Size DelegatedFrameHostDesiredSizeInDIP(void) const override;
bool DelegatedFrameCanCreateResizeLock() const override;
std::unique_ptr<content::CompositorResizeLock>
DelegatedFrameHostCreateResizeLock() override;
void OnBeginFrame() override;
// CompositorResizeLockClient implementation.
std::unique_ptr<ui::CompositorLock> GetCompositorLock(
ui::CompositorLockClient* client) override;
void CompositorResizeLockEnded() override;
2016-11-30 07:30:03 +00:00
#endif // !defined(OS_MACOSX)
2017-04-05 08:31:22 +00:00
bool TransformPointToLocalCoordSpace(
const gfx::Point& point,
const viz::SurfaceId& original_surface,
2017-04-05 08:31:22 +00:00
gfx::Point* transformed_point) override;
bool TransformPointToCoordSpaceForView(
const gfx::Point& point,
RenderWidgetHostViewBase* target_view,
gfx::Point* transformed_point) override;
2016-08-03 04:46:34 +00:00
// ui::CompositorDelegate:
std::unique_ptr<viz::SoftwareOutputDevice> CreateSoftwareOutputDevice(
2016-07-27 16:24:58 +00:00
ui::Compositor* compositor) override;
2016-08-03 04:46:34 +00:00
bool InstallTransparency();
bool IsAutoResizeEnabled() const;
2016-07-18 14:16:23 +00:00
// NativeWindowObserver:
void OnWindowResize() override;
void OnWindowClosed() override;
2016-07-18 14:16:23 +00:00
void OnBeginFrameTimerTick();
void SendBeginFrame(base::TimeTicks frame_time,
base::TimeDelta vsync_period);
2016-08-03 04:46:34 +00:00
#if defined(OS_MACOSX)
2017-04-05 08:31:22 +00:00
void CreatePlatformWidget(bool is_guest_view_hack);
void DestroyPlatformWidget();
2016-08-03 04:46:34 +00:00
#endif
void CancelWidget();
void AddGuestHostView(OffScreenRenderWidgetHostView* guest_host);
void RemoveGuestHostView(OffScreenRenderWidgetHostView* guest_host);
void AddViewProxy(OffscreenViewProxy* proxy);
void RemoveViewProxy(OffscreenViewProxy* proxy);
void ProxyViewDestroyed(OffscreenViewProxy* proxy);
void RegisterGuestViewFrameSwappedCallback(
content::RenderWidgetHostViewGuest* guest_host_view);
void OnGuestViewFrameSwapped(
content::RenderWidgetHostViewGuest* guest_host_view);
2016-08-04 04:22:19 +00:00
void OnPaint(const gfx::Rect& damage_rect, const SkBitmap& bitmap);
void OnPopupPaint(const gfx::Rect& damage_rect, const SkBitmap& bitmap);
void OnProxyViewPaint(const gfx::Rect& damage_rect);
2017-03-05 15:18:57 +00:00
bool IsPopupWidget() const {
2017-06-16 20:42:33 +00:00
return popup_type_ != blink::kWebPopupTypeNone;
}
2017-03-05 15:18:57 +00:00
void HoldResize();
void ReleaseResize();
void WasResized();
2017-03-05 15:18:57 +00:00
void ProcessKeyboardEvent(
const content::NativeWebKeyboardEvent& event,
const ui::LatencyInfo& latency) override;
void ProcessMouseEvent(const blink::WebMouseEvent& event,
const ui::LatencyInfo& latency) override;
void ProcessMouseWheelEvent(const blink::WebMouseWheelEvent& event,
const ui::LatencyInfo& latency) override;
2016-07-27 17:44:41 +00:00
2016-07-31 10:19:56 +00:00
void SetPainting(bool painting);
bool IsPainting() const;
2016-07-31 10:19:56 +00:00
void SetFrameRate(int frame_rate);
int GetFrameRate() const;
2016-08-03 04:46:34 +00:00
2016-09-07 08:33:09 +00:00
ui::Compositor* GetCompositor() const;
ui::Layer* GetRootLayer() const;
content::DelegatedFrameHost* GetDelegatedFrameHost() const;
void Invalidate();
void InvalidateBounds(const gfx::Rect&);
2016-08-03 04:46:34 +00:00
content::RenderWidgetHostImpl* render_widget_host() const
{ return render_widget_host_; }
2016-09-06 08:24:37 +00:00
NativeWindow* window() const { return native_window_; }
2016-12-20 01:23:41 +00:00
gfx::Size size() const { return size_; }
float scale_factor() const { return scale_factor_; }
2016-08-03 04:46:34 +00:00
void set_popup_host_view(OffScreenRenderWidgetHostView* popup_view) {
popup_host_view_ = popup_view;
}
2017-03-05 15:18:57 +00:00
void set_child_host_view(OffScreenRenderWidgetHostView* child_view) {
child_host_view_ = child_view;
}
2016-08-03 04:46:34 +00:00
private:
2016-07-31 10:19:56 +00:00
void SetupFrameRate(bool force);
2016-07-22 11:55:58 +00:00
void ResizeRootLayer();
viz::FrameSinkId AllocateFrameSinkId(bool is_guest_view_hack);
2017-04-05 08:31:22 +00:00
2017-07-08 19:25:15 +00:00
// Applies background color without notifying the RenderWidget about
// opaqueness changes.
void UpdateBackgroundColorFromRenderer(SkColor color);
2016-08-03 04:46:34 +00:00
// Weak ptrs.
2016-07-13 15:43:00 +00:00
content::RenderWidgetHostImpl* render_widget_host_;
2017-03-05 15:18:57 +00:00
OffScreenRenderWidgetHostView* parent_host_view_;
OffScreenRenderWidgetHostView* popup_host_view_;
std::unique_ptr<SkBitmap> popup_bitmap_;
OffScreenRenderWidgetHostView* child_host_view_;
std::set<OffScreenRenderWidgetHostView*> guest_host_views_;
std::set<OffscreenViewProxy*> proxy_views_;
2017-03-05 15:18:57 +00:00
2016-07-27 12:36:22 +00:00
NativeWindow* native_window_;
2016-07-22 11:55:58 +00:00
OffScreenOutputDevice* software_output_device_;
const bool transparent_;
2016-08-03 04:04:36 +00:00
OnPaintCallback callback_;
OnPaintCallback parent_callback_;
2016-07-29 12:50:27 +00:00
int frame_rate_;
int frame_rate_threshold_us_;
2016-07-05 19:33:22 +00:00
base::Time last_time_;
2016-07-13 15:43:00 +00:00
float scale_factor_;
gfx::Vector2dF last_scroll_offset_;
gfx::Size size_;
bool painting_;
2017-03-05 15:18:57 +00:00
bool is_showing_;
bool is_destroyed_;
gfx::Rect popup_position_;
bool hold_resize_;
bool pending_resize_;
2016-07-05 19:33:22 +00:00
bool paint_callback_running_;
2016-07-18 14:16:23 +00:00
std::unique_ptr<ui::Layer> root_layer_;
2016-08-03 04:46:34 +00:00
std::unique_ptr<ui::Compositor> compositor_;
std::unique_ptr<content::DelegatedFrameHost> delegated_frame_host_;
std::unique_ptr<AtomCopyFrameGenerator> copy_frame_generator_;
std::unique_ptr<AtomBeginFrameTimer> begin_frame_timer_;
2016-07-18 14:16:23 +00:00
2017-04-05 08:31:22 +00:00
// Provides |source_id| for BeginFrameArgs that we create.
viz::StubBeginFrameSource begin_frame_source_;
uint64_t begin_frame_number_ = viz::BeginFrameArgs::kStartingFrameNumber;
2017-04-05 08:31:22 +00:00
2016-08-02 10:56:03 +00:00
#if defined(OS_MACOSX)
CALayer* background_layer_;
2016-07-18 14:16:23 +00:00
std::unique_ptr<content::BrowserCompositorMac> browser_compositor_;
2016-09-06 08:24:37 +00:00
// Can not be managed by smart pointer because its header can not be included
// in the file that has the destructor.
2016-09-07 08:33:09 +00:00
MacHelper* mac_helper_;
2016-09-06 08:24:37 +00:00
2016-07-18 14:16:23 +00:00
// Selected text on the renderer.
std::string selected_text_;
2016-07-13 15:43:00 +00:00
#endif
2016-07-05 19:33:22 +00:00
viz::mojom::CompositorFrameSinkClient* renderer_compositor_frame_sink_;
2017-07-08 19:25:15 +00:00
SkColor background_color_;
2016-07-29 12:50:27 +00:00
base::WeakPtrFactory<OffScreenRenderWidgetHostView> weak_ptr_factory_;
2016-08-03 04:46:34 +00:00
2016-07-29 12:50:27 +00:00
DISALLOW_COPY_AND_ASSIGN(OffScreenRenderWidgetHostView);
2016-07-05 19:33:22 +00:00
};
} // namespace atom
#endif // ATOM_BROWSER_OSR_OSR_RENDER_WIDGET_HOST_VIEW_H_