Fix API changes in OSR code

This commit is contained in:
Cheng Zhao 2017-04-05 17:31:22 +09:00
parent fc2d62d5cb
commit 9d1b88ed59
6 changed files with 132 additions and 69 deletions

View file

@ -32,8 +32,7 @@ bool FrameSubscriber::ShouldCaptureFrame(
base::TimeTicks present_time,
scoped_refptr<media::VideoFrame>* storage,
DeliverFrameCallback* callback) {
const auto host = view_ ? view_->GetRenderWidgetHost() : nullptr;
if (!view_ || !host)
if (!view_)
return false;
if (dirty_rect.IsEmpty())
@ -54,7 +53,7 @@ bool FrameSubscriber::ShouldCaptureFrame(
rect = gfx::Rect(rect.origin(), bitmap_size);
host->CopyFromBackingStore(
view_->CopyFromSurface(
rect,
rect.size(),
base::Bind(&FrameSubscriber::OnFrameDelivered,

View file

@ -7,9 +7,9 @@
#include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "content/browser/renderer_host/render_widget_host_view_frame_subscriber.h"
#include "content/public/browser/readback_types.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/render_widget_host_view_frame_subscriber.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/size.h"
#include "v8/include/v8.h"

View file

@ -16,10 +16,11 @@
#include "components/display_compositor/gl_helper.h"
#include "content/browser/renderer_host/render_widget_host_delegate.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_view_frame_subscriber.h"
#include "content/common/view_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/context_factory.h"
#include "content/public/browser/render_widget_host_view_frame_subscriber.h"
#include "media/base/video_frame.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_type.h"
@ -337,6 +338,7 @@ OffScreenRenderWidgetHostView::OffScreenRenderWidgetHostView(
bool transparent,
const OnPaintCallback& callback,
content::RenderWidgetHost* host,
bool is_guest_view_hack,
NativeWindow* native_window)
: render_widget_host_(content::RenderWidgetHostImpl::From(host)),
native_window_(native_window),
@ -355,19 +357,23 @@ OffScreenRenderWidgetHostView::OffScreenRenderWidgetHostView(
render_widget_host_->SetView(this);
#if !defined(OS_MACOSX)
content::ImageTransportFactory* factory =
content::ImageTransportFactory::GetInstance();
delegated_frame_host_ = base::MakeUnique<content::DelegatedFrameHost>(
factory->GetContextFactory()->AllocateFrameSinkId(), this);
AllocateFrameSinkId(is_guest_view_hack), this);
root_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR));
#endif
#if defined(OS_MACOSX)
CreatePlatformWidget();
CreatePlatformWidget(is_guest_view_hack);
#else
// On macOS the ui::Compositor is created/owned by the platform view.
content::ImageTransportFactory* factory =
content::ImageTransportFactory::GetInstance();
ui::ContextFactoryPrivate* context_factory_private =
factory->GetContextFactoryPrivate();
compositor_.reset(
new ui::Compositor(content::GetContextFactory(),
new ui::Compositor(context_factory_private->AllocateFrameSinkId(),
content::GetContextFactory(), context_factory_private,
base::ThreadTaskRunnerHandle::Get()));
compositor_->SetAcceleratedWidget(native_window_->GetAcceleratedWidget());
compositor_->SetRootLayer(root_layer_.get());
@ -399,6 +405,17 @@ OffScreenRenderWidgetHostView::~OffScreenRenderWidgetHostView() {
#endif
}
void OffScreenRenderWidgetHostView::OnWindowResize() {
// In offscreen mode call RenderWidgetHostView's SetSize explicitly
auto size = native_window_->GetSize();
SetSize(size);
}
void OffScreenRenderWidgetHostView::OnWindowClosed() {
native_window_->RemoveObserver(this);
native_window_ = nullptr;
}
void OffScreenRenderWidgetHostView::OnBeginFrameTimerTick() {
const base::TimeTicks frame_time = base::TimeTicks::Now();
const base::TimeDelta vsync_period =
@ -416,10 +433,17 @@ void OffScreenRenderWidgetHostView::SendBeginFrame(
base::TimeTicks deadline = display_time - estimated_browser_composite_time;
const cc::BeginFrameArgs& begin_frame_args =
cc::BeginFrameArgs::Create(BEGINFRAME_FROM_HERE,
begin_frame_source_.source_id(),
begin_frame_number_, frame_time, deadline,
vsync_period, cc::BeginFrameArgs::NORMAL);
DCHECK(begin_frame_args.IsValid());
begin_frame_number_++;
render_widget_host_->Send(new ViewMsg_BeginFrame(
render_widget_host_->GetRoutingID(),
cc::BeginFrameArgs::Create(BEGINFRAME_FROM_HERE, frame_time, deadline,
vsync_period, cc::BeginFrameArgs::NORMAL)));
begin_frame_args));
}
bool OffScreenRenderWidgetHostView::OnMessageReceived(
@ -481,7 +505,7 @@ bool OffScreenRenderWidgetHostView::HasFocus() const {
}
bool OffScreenRenderWidgetHostView::IsSurfaceAvailableForCopy() const {
return GetDelegatedFrameHost()->CanCopyToBitmap();
return GetDelegatedFrameHost()->CanCopyFromCompositingSurface();
}
void OffScreenRenderWidgetHostView::Show() {
@ -561,7 +585,7 @@ void OffScreenRenderWidgetHostView::OnSwapCompositorFrame(
last_scroll_offset_ = frame.metadata.root_scroll_offset;
}
if (frame.delegated_frame_data) {
if (!frame.render_pass_list.empty()) {
if (software_output_device_) {
if (!begin_frame_timer_.get()) {
software_output_device_->SetActive(painting_);
@ -569,13 +593,11 @@ void OffScreenRenderWidgetHostView::OnSwapCompositorFrame(
// The compositor will draw directly to the SoftwareOutputDevice which
// then calls OnPaint.
#if defined(OS_MACOSX)
browser_compositor_->SwapCompositorFrame(output_surface_id,
std::move(frame));
#else
delegated_frame_host_->SwapDelegatedFrame(output_surface_id,
std::move(frame));
#endif
// We would normally call BrowserCompositorMac::SwapCompositorFrame on
// macOS, however it contains compositor resize logic that we don't want.
// Consequently we instead call the SwapDelegatedFrame method directly.
GetDelegatedFrameHost()->SwapDelegatedFrame(output_surface_id,
std::move(frame));
} else {
if (!copy_frame_generator_.get()) {
copy_frame_generator_.reset(
@ -584,20 +606,17 @@ void OffScreenRenderWidgetHostView::OnSwapCompositorFrame(
// Determine the damage rectangle for the current frame. This is the same
// calculation that SwapDelegatedFrame uses.
cc::RenderPass* root_pass =
frame.delegated_frame_data->render_pass_list.back().get();
cc::RenderPass* root_pass = frame.render_pass_list.back().get();
gfx::Size frame_size = root_pass->output_rect.size();
gfx::Rect damage_rect =
gfx::ToEnclosingRect(gfx::RectF(root_pass->damage_rect));
damage_rect.Intersect(gfx::Rect(frame_size));
#if defined(OS_MACOSX)
browser_compositor_->SwapCompositorFrame(output_surface_id,
std::move(frame));
#else
delegated_frame_host_->SwapDelegatedFrame(output_surface_id,
std::move(frame));
#endif
// We would normally call BrowserCompositorMac::SwapCompositorFrame on
// macOS, however it contains compositor resize logic that we don't want.
// Consequently we instead call the SwapDelegatedFrame method directly.
GetDelegatedFrameHost()->SwapDelegatedFrame(output_surface_id,
std::move(frame));
// Request a copy of the last compositor frame which will eventually call
// OnPaint asynchronously.
@ -647,7 +666,7 @@ void OffScreenRenderWidgetHostView::SelectionBoundsChanged(
const ViewHostMsg_SelectionBounds_Params &) {
}
void OffScreenRenderWidgetHostView::CopyFromCompositingSurface(
void OffScreenRenderWidgetHostView::CopyFromSurface(
const gfx::Rect& src_subrect,
const gfx::Size& dst_size,
const content::ReadbackRequestCallback& callback,
@ -656,18 +675,14 @@ void OffScreenRenderWidgetHostView::CopyFromCompositingSurface(
src_subrect, dst_size, callback, preferred_color_type);
}
void OffScreenRenderWidgetHostView::CopyFromCompositingSurfaceToVideoFrame(
void OffScreenRenderWidgetHostView::CopyFromSurfaceToVideoFrame(
const gfx::Rect& src_subrect,
const scoped_refptr<media::VideoFrame>& target,
scoped_refptr<media::VideoFrame> target,
const base::Callback<void(const gfx::Rect&, bool)>& callback) {
GetDelegatedFrameHost()->CopyFromCompositingSurfaceToVideoFrame(
src_subrect, target, callback);
}
bool OffScreenRenderWidgetHostView::CanCopyToVideoFrame() const {
return GetDelegatedFrameHost()->CanCopyToVideoFrame();
}
void OffScreenRenderWidgetHostView::BeginFrameSubscription(
std::unique_ptr<content::RenderWidgetHostViewFrameSubscriber> subscriber) {
GetDelegatedFrameHost()->BeginFrameSubscription(std::move(subscriber));
@ -685,12 +700,6 @@ gfx::Rect OffScreenRenderWidgetHostView::GetBoundsInRootWindow() {
return gfx::Rect(size_);
}
void OffScreenRenderWidgetHostView::LockCompositingSurface() {
}
void OffScreenRenderWidgetHostView::UnlockCompositingSurface() {
}
void OffScreenRenderWidgetHostView::ImeCompositionRangeChanged(
const gfx::Range &, const std::vector<gfx::Rect>&) {
}
@ -752,6 +761,40 @@ void OffScreenRenderWidgetHostView::SetBeginFrameSource(
#endif // !defined(OS_MACOSX)
bool OffScreenRenderWidgetHostView::TransformPointToLocalCoordSpace(
const gfx::Point& point,
const cc::SurfaceId& original_surface,
gfx::Point* transformed_point) {
// Transformations use physical pixels rather than DIP, so conversion
// is necessary.
gfx::Point point_in_pixels =
gfx::ConvertPointToPixel(scale_factor_, point);
if (!GetDelegatedFrameHost()->TransformPointToLocalCoordSpace(
point_in_pixels, original_surface, transformed_point)) {
return false;
}
*transformed_point =
gfx::ConvertPointToDIP(scale_factor_, *transformed_point);
return true;
}
bool OffScreenRenderWidgetHostView::TransformPointToCoordSpaceForView(
const gfx::Point& point,
RenderWidgetHostViewBase* target_view,
gfx::Point* transformed_point) {
if (target_view == this) {
*transformed_point = point;
return true;
}
// In TransformPointToLocalCoordSpace() there is a Point-to-Pixel conversion,
// but it is not necessary here because the final target view is responsible
// for converting before computing the final transform.
return GetDelegatedFrameHost()->TransformPointToCoordSpaceForView(
point, target_view, transformed_point);
}
std::unique_ptr<cc::SoftwareOutputDevice>
OffScreenRenderWidgetHostView::CreateSoftwareOutputDevice(
ui::Compositor* compositor) {
@ -894,15 +937,20 @@ void OffScreenRenderWidgetHostView::ResizeRootLayer() {
GetCompositor()->SetScaleAndSize(scale_factor_, size_in_pixels);
}
void OffScreenRenderWidgetHostView::OnWindowResize() {
// In offscreen mode call RenderWidgetHostView's SetSize explicitly
auto size = native_window_->GetSize();
SetSize(size);
}
void OffScreenRenderWidgetHostView::OnWindowClosed() {
native_window_->RemoveObserver(this);
native_window_ = nullptr;
cc::FrameSinkId OffScreenRenderWidgetHostView::AllocateFrameSinkId(
bool is_guest_view_hack) {
// GuestViews have two RenderWidgetHostViews and so we need to make sure
// we don't have FrameSinkId collisions.
// The FrameSinkId generated here must be unique with FrameSinkId allocated
// in ContextFactoryPrivate.
content::ImageTransportFactory* factory =
content::ImageTransportFactory::GetInstance();
return is_guest_view_hack
? factory->GetContextFactoryPrivate()->AllocateFrameSinkId()
: cc::FrameSinkId(base::checked_cast<uint32_t>(
render_widget_host_->GetProcess()->GetID()),
base::checked_cast<uint32_t>(
render_widget_host_->GetRoutingID()));
}
} // namespace atom

View file

@ -69,6 +69,7 @@ class OffScreenRenderWidgetHostView
OffScreenRenderWidgetHostView(bool transparent,
const OnPaintCallback& callback,
content::RenderWidgetHost* render_widget_host,
bool is_guest_view_hack,
NativeWindow* native_window);
~OffScreenRenderWidgetHostView() override;
@ -126,23 +127,20 @@ class OffScreenRenderWidgetHostView
#endif
void SelectionBoundsChanged(const ViewHostMsg_SelectionBounds_Params &)
override;
void CopyFromCompositingSurface(const gfx::Rect &,
const gfx::Size &,
const content::ReadbackRequestCallback &,
const SkColorType) override;
void CopyFromCompositingSurfaceToVideoFrame(
const gfx::Rect &,
const scoped_refptr<media::VideoFrame> &,
const base::Callback<void(const gfx::Rect &, bool),
base::internal::CopyMode::Copyable> &) override;
bool CanCopyToVideoFrame(void) const override;
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;
void BeginFrameSubscription(
std::unique_ptr<content::RenderWidgetHostViewFrameSubscriber>) override;
void EndFrameSubscription() override;
bool HasAcceleratedSurface(const gfx::Size &) override;
gfx::Rect GetBoundsInRootWindow(void) override;
void LockCompositingSurface(void) override;
void UnlockCompositingSurface(void) override;
void ImeCompositionRangeChanged(
const gfx::Range &, const std::vector<gfx::Rect>&) override;
gfx::Size GetPhysicalBackingSize() const override;
@ -166,6 +164,15 @@ class OffScreenRenderWidgetHostView
void SetBeginFrameSource(cc::BeginFrameSource* source) override;
#endif // !defined(OS_MACOSX)
bool TransformPointToLocalCoordSpace(
const gfx::Point& point,
const cc::SurfaceId& original_surface,
gfx::Point* transformed_point) override;
bool TransformPointToCoordSpaceForView(
const gfx::Point& point,
RenderWidgetHostViewBase* target_view,
gfx::Point* transformed_point) override;
// ui::CompositorDelegate:
std::unique_ptr<cc::SoftwareOutputDevice> CreateSoftwareOutputDevice(
ui::Compositor* compositor) override;
@ -182,7 +189,7 @@ class OffScreenRenderWidgetHostView
base::TimeDelta vsync_period);
#if defined(OS_MACOSX)
void CreatePlatformWidget();
void CreatePlatformWidget(bool is_guest_view_hack);
void DestroyPlatformWidget();
#endif
@ -210,6 +217,8 @@ class OffScreenRenderWidgetHostView
void SetupFrameRate(bool force);
void ResizeRootLayer();
cc::FrameSinkId AllocateFrameSinkId(bool is_guest_view_hack);
// Weak ptrs.
content::RenderWidgetHostImpl* render_widget_host_;
NativeWindow* native_window_;
@ -236,6 +245,10 @@ class OffScreenRenderWidgetHostView
std::unique_ptr<AtomCopyFrameGenerator> copy_frame_generator_;
std::unique_ptr<AtomBeginFrameTimer> begin_frame_timer_;
// Provides |source_id| for BeginFrameArgs that we create.
cc::StubBeginFrameSource begin_frame_source_;
uint64_t begin_frame_number_ = cc::BeginFrameArgs::kStartingFrameNumber;
#if defined(OS_MACOSX)
CALayer* background_layer_;
std::unique_ptr<content::BrowserCompositorMac> browser_compositor_;

View file

@ -121,10 +121,12 @@ void OffScreenRenderWidgetHostView::SelectionChanged(
RenderWidgetHostViewBase::SelectionChanged(text, offset, range);
}
void OffScreenRenderWidgetHostView::CreatePlatformWidget() {
void OffScreenRenderWidgetHostView::CreatePlatformWidget(
bool is_guest_view_hack) {
mac_helper_ = new MacHelper(this);
browser_compositor_.reset(new content::BrowserCompositorMac(
mac_helper_, mac_helper_, render_widget_host_->is_hidden(), true));
mac_helper_, mac_helper_, render_widget_host_->is_hidden(), true,
AllocateFrameSinkId(is_guest_view_hack)));
}
void OffScreenRenderWidgetHostView::DestroyPlatformWidget() {

View file

@ -79,7 +79,8 @@ content::RenderWidgetHostViewBase*
content::RenderWidgetHost* render_widget_host, bool is_guest_view_hack) {
auto relay = NativeWindowRelay::FromWebContents(web_contents_);
view_ = new OffScreenRenderWidgetHostView(
transparent_, callback_, render_widget_host, relay->window.get());
transparent_, callback_, render_widget_host,
is_guest_view_hack, relay->window.get());
return view_;
}
@ -88,7 +89,7 @@ content::RenderWidgetHostViewBase*
content::RenderWidgetHost* render_widget_host) {
auto relay = NativeWindowRelay::FromWebContents(web_contents_);
view_ = new OffScreenRenderWidgetHostView(
transparent_, callback_, render_widget_host, relay->window.get());
transparent_, callback_, render_widget_host, false, relay->window.get());
return view_;
}