electron/atom/browser/osr/osr_render_widget_host_view.cc

916 lines
27 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.
#include "atom/browser/osr/osr_render_widget_host_view.h"
2016-07-29 12:50:27 +00:00
2016-07-31 10:19:56 +00:00
#include <vector>
2016-08-03 04:46:34 +00:00
#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/single_thread_task_runner.h"
#include "base/time/time.h"
#include "cc/output/copy_output_request.h"
#include "cc/scheduler/delay_based_time_source.h"
#include "components/display_compositor/gl_helper.h"
2016-07-05 19:33:22 +00:00
#include "content/browser/renderer_host/render_widget_host_delegate.h"
2016-08-26 22:30:02 +00:00
#include "content/browser/renderer_host/render_widget_host_impl.h"
2016-07-05 19:33:22 +00:00
#include "content/common/view_messages.h"
2016-08-03 04:46:34 +00:00
#include "content/public/browser/browser_thread.h"
2016-07-05 19:33:22 +00:00
#include "content/public/browser/context_factory.h"
2016-08-03 04:46:34 +00:00
#include "content/public/browser/render_widget_host_view_frame_subscriber.h"
2016-07-05 19:33:22 +00:00
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_type.h"
2016-08-03 04:46:34 +00:00
#include "ui/events/latency_info.h"
#include "ui/gfx/geometry/dip_util.h"
2016-07-05 19:33:22 +00:00
#include "ui/gfx/native_widget_types.h"
2016-07-29 22:51:19 +00:00
2016-08-03 05:09:38 +00:00
namespace atom {
namespace {
2016-07-18 14:16:23 +00:00
2016-08-03 05:09:38 +00:00
const float kDefaultScaleFactor = 1.0;
2016-07-18 14:16:23 +00:00
const int kFrameRetryLimit = 2;
2016-08-03 05:09:38 +00:00
} // namespace
2016-07-05 19:33:22 +00:00
2016-07-27 18:31:03 +00:00
class AtomCopyFrameGenerator {
2016-07-18 14:16:23 +00:00
public:
2016-07-27 18:31:03 +00:00
AtomCopyFrameGenerator(int frame_rate_threshold_ms,
2016-07-29 12:50:27 +00:00
OffScreenRenderWidgetHostView* view)
2016-07-18 14:16:23 +00:00
: frame_rate_threshold_ms_(frame_rate_threshold_ms),
view_(view),
frame_pending_(false),
frame_in_progress_(false),
frame_retry_count_(0),
weak_ptr_factory_(this) {
last_time_ = base::Time::Now();
2016-07-18 14:16:23 +00:00
}
void GenerateCopyFrame(
bool force_frame,
const gfx::Rect& damage_rect) {
if (force_frame && !frame_pending_)
frame_pending_ = true;
if (!frame_pending_)
return;
if (!damage_rect.IsEmpty())
pending_damage_rect_.Union(damage_rect);
if (frame_in_progress_)
2016-07-18 14:16:23 +00:00
return;
2016-07-18 14:16:23 +00:00
frame_in_progress_ = true;
const int64_t frame_rate_delta =
(base::TimeTicks::Now() - frame_start_time_).InMilliseconds();
if (frame_rate_delta < frame_rate_threshold_ms_) {
2016-07-29 22:51:19 +00:00
content::BrowserThread::PostDelayedTask(content::BrowserThread::UI,
FROM_HERE,
base::Bind(&AtomCopyFrameGenerator::InternalGenerateCopyFrame,
2016-09-07 08:33:09 +00:00
weak_ptr_factory_.GetWeakPtr()),
base::TimeDelta::FromMilliseconds(
frame_rate_threshold_ms_ - frame_rate_delta));
2016-07-18 14:16:23 +00:00
return;
}
InternalGenerateCopyFrame();
}
bool frame_pending() const { return frame_pending_; }
void set_frame_rate_threshold_ms(int frame_rate_threshold_ms) {
frame_rate_threshold_ms_ = frame_rate_threshold_ms;
}
private:
void InternalGenerateCopyFrame() {
frame_pending_ = false;
frame_start_time_ = base::TimeTicks::Now();
if (!view_->render_widget_host())
return;
const gfx::Rect damage_rect = pending_damage_rect_;
pending_damage_rect_.SetRect(0, 0, 0, 0);
std::unique_ptr<cc::CopyOutputRequest> request =
cc::CopyOutputRequest::CreateRequest(base::Bind(
2016-07-27 18:31:03 +00:00
&AtomCopyFrameGenerator::CopyFromCompositingSurfaceHasResult,
2016-07-18 14:16:23 +00:00
weak_ptr_factory_.GetWeakPtr(),
2016-07-19 21:50:49 +00:00
damage_rect));
2016-07-18 14:16:23 +00:00
2016-07-22 11:55:58 +00:00
request->set_area(gfx::Rect(view_->GetPhysicalBackingSize()));
2016-09-07 08:33:09 +00:00
view_->GetRootLayer()->RequestCopyOfOutput(std::move(request));
2016-07-18 14:16:23 +00:00
}
void CopyFromCompositingSurfaceHasResult(
const gfx::Rect& damage_rect,
std::unique_ptr<cc::CopyOutputResult> result) {
if (result->IsEmpty() || result->size().IsEmpty() ||
!view_->render_widget_host()) {
OnCopyFrameCaptureFailure(damage_rect);
return;
}
if (result->HasTexture()) {
PrepareTextureCopyOutputResult(damage_rect, std::move(result));
return;
}
DCHECK(result->HasBitmap());
PrepareBitmapCopyOutputResult(damage_rect, std::move(result));
}
void PrepareTextureCopyOutputResult(
const gfx::Rect& damage_rect,
std::unique_ptr<cc::CopyOutputResult> result) {
DCHECK(result->HasTexture());
base::ScopedClosureRunner scoped_callback_runner(
2016-07-27 18:31:03 +00:00
base::Bind(&AtomCopyFrameGenerator::OnCopyFrameCaptureFailure,
2016-07-18 14:16:23 +00:00
weak_ptr_factory_.GetWeakPtr(),
damage_rect));
const gfx::Size& result_size = result->size();
SkIRect bitmap_size;
if (bitmap_)
bitmap_->getBounds(&bitmap_size);
if (!bitmap_ ||
bitmap_size.width() != result_size.width() ||
bitmap_size.height() != result_size.height()) {
bitmap_.reset(new SkBitmap);
bitmap_->allocN32Pixels(result_size.width(),
result_size.height(),
true);
if (bitmap_->drawsNothing())
return;
}
content::ImageTransportFactory* factory =
content::ImageTransportFactory::GetInstance();
display_compositor::GLHelper* gl_helper = factory->GetGLHelper();
2016-07-18 14:16:23 +00:00
if (!gl_helper)
return;
std::unique_ptr<SkAutoLockPixels> bitmap_pixels_lock(
new SkAutoLockPixels(*bitmap_));
uint8_t* pixels = static_cast<uint8_t*>(bitmap_->getPixels());
cc::TextureMailbox texture_mailbox;
std::unique_ptr<cc::SingleReleaseCallback> release_callback;
result->TakeTexture(&texture_mailbox, &release_callback);
DCHECK(texture_mailbox.IsTexture());
if (!texture_mailbox.IsTexture())
return;
ignore_result(scoped_callback_runner.Release());
gl_helper->CropScaleReadbackAndCleanMailbox(
texture_mailbox.mailbox(),
texture_mailbox.sync_token(),
result_size,
gfx::Rect(result_size),
result_size,
pixels,
kN32_SkColorType,
base::Bind(
2016-07-27 18:31:03 +00:00
&AtomCopyFrameGenerator::CopyFromCompositingSurfaceFinishedProxy,
2016-07-18 14:16:23 +00:00
weak_ptr_factory_.GetWeakPtr(),
base::Passed(&release_callback),
damage_rect,
base::Passed(&bitmap_),
base::Passed(&bitmap_pixels_lock)),
display_compositor::GLHelper::SCALER_QUALITY_FAST);
2016-07-18 14:16:23 +00:00
}
static void CopyFromCompositingSurfaceFinishedProxy(
2016-07-27 18:31:03 +00:00
base::WeakPtr<AtomCopyFrameGenerator> generator,
2016-07-18 14:16:23 +00:00
std::unique_ptr<cc::SingleReleaseCallback> release_callback,
const gfx::Rect& damage_rect,
std::unique_ptr<SkBitmap> bitmap,
std::unique_ptr<SkAutoLockPixels> bitmap_pixels_lock,
bool result) {
gpu::SyncToken sync_token;
if (result) {
display_compositor::GLHelper* gl_helper =
2016-07-18 14:16:23 +00:00
content::ImageTransportFactory::GetInstance()->GetGLHelper();
if (gl_helper)
gl_helper->GenerateSyncToken(&sync_token);
}
const bool lost_resource = !sync_token.HasData();
release_callback->Run(sync_token, lost_resource);
if (generator) {
generator->CopyFromCompositingSurfaceFinished(
damage_rect, std::move(bitmap), std::move(bitmap_pixels_lock),
result);
} else {
bitmap_pixels_lock.reset();
bitmap.reset();
}
}
void CopyFromCompositingSurfaceFinished(
const gfx::Rect& damage_rect,
std::unique_ptr<SkBitmap> bitmap,
std::unique_ptr<SkAutoLockPixels> bitmap_pixels_lock,
bool result) {
DCHECK(!bitmap_);
bitmap_ = std::move(bitmap);
if (result) {
OnCopyFrameCaptureSuccess(damage_rect, *bitmap_,
std::move(bitmap_pixels_lock));
} else {
bitmap_pixels_lock.reset();
OnCopyFrameCaptureFailure(damage_rect);
}
}
void PrepareBitmapCopyOutputResult(
const gfx::Rect& damage_rect,
std::unique_ptr<cc::CopyOutputResult> result) {
DCHECK(result->HasBitmap());
std::unique_ptr<SkBitmap> source = result->TakeBitmap();
DCHECK(source);
if (source) {
std::unique_ptr<SkAutoLockPixels> bitmap_pixels_lock(
new SkAutoLockPixels(*source));
OnCopyFrameCaptureSuccess(damage_rect, *source,
std::move(bitmap_pixels_lock));
} else {
OnCopyFrameCaptureFailure(damage_rect);
}
}
void OnCopyFrameCaptureFailure(
const gfx::Rect& damage_rect) {
pending_damage_rect_.Union(damage_rect);
const bool force_frame = (++frame_retry_count_ <= kFrameRetryLimit);
OnCopyFrameCaptureCompletion(force_frame);
}
void OnCopyFrameCaptureSuccess(
const gfx::Rect& damage_rect,
const SkBitmap& bitmap,
std::unique_ptr<SkAutoLockPixels> bitmap_pixels_lock) {
2016-08-04 04:22:19 +00:00
view_->OnPaint(damage_rect, bitmap);
2016-07-18 14:16:23 +00:00
if (frame_retry_count_ > 0)
frame_retry_count_ = 0;
OnCopyFrameCaptureCompletion(false);
}
void OnCopyFrameCaptureCompletion(bool force_frame) {
frame_in_progress_ = false;
if (frame_pending_) {
2016-07-29 22:51:19 +00:00
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
base::Bind(&AtomCopyFrameGenerator::GenerateCopyFrame,
weak_ptr_factory_.GetWeakPtr(),
force_frame,
gfx::Rect()));
2016-07-18 14:16:23 +00:00
}
}
int frame_rate_threshold_ms_;
2016-07-29 12:50:27 +00:00
OffScreenRenderWidgetHostView* view_;
2016-07-18 14:16:23 +00:00
base::Time last_time_;
2016-07-18 14:16:23 +00:00
base::TimeTicks frame_start_time_;
bool frame_pending_;
bool frame_in_progress_;
int frame_retry_count_;
std::unique_ptr<SkBitmap> bitmap_;
gfx::Rect pending_damage_rect_;
2016-07-27 18:31:03 +00:00
base::WeakPtrFactory<AtomCopyFrameGenerator> weak_ptr_factory_;
2016-07-18 14:16:23 +00:00
2016-07-27 18:31:03 +00:00
DISALLOW_COPY_AND_ASSIGN(AtomCopyFrameGenerator);
2016-07-18 14:16:23 +00:00
};
2016-07-27 18:31:03 +00:00
class AtomBeginFrameTimer : public cc::DelayBasedTimeSourceClient {
2016-07-18 14:16:23 +00:00
public:
2016-07-27 18:31:03 +00:00
AtomBeginFrameTimer(int frame_rate_threshold_ms,
const base::Closure& callback)
2016-07-18 14:16:23 +00:00
: callback_(callback) {
2016-09-06 08:24:37 +00:00
time_source_.reset(new cc::DelayBasedTimeSource(
2016-11-30 07:30:03 +00:00
content::BrowserThread::GetTaskRunnerForThread(
content::BrowserThread::UI).get()));
2016-07-18 14:16:23 +00:00
time_source_->SetClient(this);
}
void SetActive(bool active) {
time_source_->SetActive(active);
}
bool IsActive() const {
return time_source_->Active();
}
void SetFrameRateThresholdMs(int frame_rate_threshold_ms) {
time_source_->SetTimebaseAndInterval(
base::TimeTicks::Now(),
base::TimeDelta::FromMilliseconds(frame_rate_threshold_ms));
}
private:
void OnTimerTick() override {
callback_.Run();
}
const base::Closure callback_;
std::unique_ptr<cc::DelayBasedTimeSource> time_source_;
2016-07-27 18:31:03 +00:00
DISALLOW_COPY_AND_ASSIGN(AtomBeginFrameTimer);
2016-07-18 14:16:23 +00:00
};
2016-07-29 12:50:27 +00:00
OffScreenRenderWidgetHostView::OffScreenRenderWidgetHostView(
2016-08-03 04:46:34 +00:00
bool transparent,
const OnPaintCallback& callback,
2016-08-03 04:46:34 +00:00
content::RenderWidgetHost* host,
NativeWindow* native_window)
: render_widget_host_(content::RenderWidgetHostImpl::From(host)),
native_window_(native_window),
software_output_device_(nullptr),
transparent_(transparent),
callback_(callback),
2016-08-03 04:46:34 +00:00
frame_rate_(60),
frame_rate_threshold_ms_(0),
last_time_(base::Time::Now()),
scale_factor_(kDefaultScaleFactor),
is_showing_(!render_widget_host_->is_hidden()),
size_(native_window->GetSize()),
painting_(true),
2016-09-07 08:33:09 +00:00
#if !defined(OS_MACOSX)
2016-08-03 04:46:34 +00:00
delegated_frame_host_(new content::DelegatedFrameHost(this)),
2016-09-07 08:33:09 +00:00
#endif
2016-08-03 04:46:34 +00:00
weak_ptr_factory_(this) {
2016-07-13 15:43:00 +00:00
DCHECK(render_widget_host_);
render_widget_host_->SetView(this);
2016-07-05 19:33:22 +00:00
2016-09-07 08:33:09 +00:00
#if !defined(OS_MACOSX)
root_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR));
#endif
2016-07-27 16:02:54 +00:00
#if defined(OS_MACOSX)
CreatePlatformWidget();
2016-08-03 04:46:34 +00:00
#else
2016-07-18 14:16:23 +00:00
compositor_.reset(
new ui::Compositor(content::GetContextFactory(),
base::ThreadTaskRunnerHandle::Get()));
2016-08-03 04:46:34 +00:00
compositor_->SetAcceleratedWidget(native_window_->GetAcceleratedWidget());
2016-07-13 15:43:00 +00:00
compositor_->SetRootLayer(root_layer_.get());
2016-09-07 08:33:09 +00:00
#endif
GetCompositor()->SetDelegate(this);
native_window_->AddObserver(this);
ResizeRootLayer();
2016-07-22 11:55:58 +00:00
}
2016-07-18 14:16:23 +00:00
2016-07-29 22:51:19 +00:00
OffScreenRenderWidgetHostView::~OffScreenRenderWidgetHostView() {
if (native_window_)
native_window_->RemoveObserver(this);
2016-09-07 08:33:09 +00:00
#if defined(OS_MACOSX)
if (is_showing_)
browser_compositor_->SetRenderWidgetHostIsHidden(true);
#else
// Marking the DelegatedFrameHost as removed from the window hierarchy is
// necessary to remove all connections to its old ui::Compositor.
2016-07-29 22:51:19 +00:00
if (is_showing_)
delegated_frame_host_->WasHidden();
delegated_frame_host_->ResetCompositor();
2016-09-07 08:33:09 +00:00
#endif
2016-07-29 22:51:19 +00:00
2016-08-02 10:56:03 +00:00
#if defined(OS_MACOSX)
2016-07-29 22:51:19 +00:00
DestroyPlatformWidget();
2016-08-02 10:56:03 +00:00
#endif
2016-07-18 14:16:23 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::OnBeginFrameTimerTick() {
2016-07-18 14:16:23 +00:00
const base::TimeTicks frame_time = base::TimeTicks::Now();
const base::TimeDelta vsync_period =
base::TimeDelta::FromMilliseconds(frame_rate_threshold_ms_);
SendBeginFrame(frame_time, vsync_period);
}
2016-08-03 04:46:34 +00:00
void OffScreenRenderWidgetHostView::SendBeginFrame(
base::TimeTicks frame_time, base::TimeDelta vsync_period) {
2016-07-18 14:16:23 +00:00
base::TimeTicks display_time = frame_time + vsync_period;
base::TimeDelta estimated_browser_composite_time =
base::TimeDelta::FromMicroseconds(
(1.0f * base::Time::kMicrosecondsPerSecond) / (3.0f * 60));
base::TimeTicks deadline = display_time - estimated_browser_composite_time;
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)));
}
2016-07-29 22:51:19 +00:00
bool OffScreenRenderWidgetHostView::OnMessageReceived(
const IPC::Message& message) {
2016-07-05 19:33:22 +00:00
bool handled = true;
2016-07-29 12:50:27 +00:00
IPC_BEGIN_MESSAGE_MAP(OffScreenRenderWidgetHostView, message)
2016-07-05 19:33:22 +00:00
IPC_MESSAGE_HANDLER(ViewHostMsg_SetNeedsBeginFrames,
SetNeedsBeginFrames)
2016-07-05 19:33:22 +00:00
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
2016-07-13 15:43:00 +00:00
if (!handled)
return content::RenderWidgetHostViewBase::OnMessageReceived(message);
2016-07-05 19:33:22 +00:00
return handled;
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::InitAsChild(gfx::NativeView) {
2016-07-05 19:33:22 +00:00
}
2016-07-29 22:51:19 +00:00
content::RenderWidgetHost* OffScreenRenderWidgetHostView::GetRenderWidgetHost()
const {
2016-07-13 15:43:00 +00:00
return render_widget_host_;
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::SetSize(const gfx::Size& size) {
2016-07-13 15:43:00 +00:00
size_ = size;
ResizeRootLayer();
if (render_widget_host_)
render_widget_host_->WasResized();
GetDelegatedFrameHost()->WasResized();
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::SetBounds(const gfx::Rect& new_bounds) {
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
gfx::Vector2dF OffScreenRenderWidgetHostView::GetLastScrollOffset() const {
2016-07-05 19:33:22 +00:00
return last_scroll_offset_;
}
2016-07-29 12:50:27 +00:00
gfx::NativeView OffScreenRenderWidgetHostView::GetNativeView() const {
2016-07-27 16:02:54 +00:00
return gfx::NativeView();
2016-07-05 19:33:22 +00:00
}
2016-07-29 22:51:19 +00:00
gfx::NativeViewAccessible
2016-08-03 04:46:34 +00:00
OffScreenRenderWidgetHostView::GetNativeViewAccessible() {
2016-07-13 15:43:00 +00:00
return gfx::NativeViewAccessible();
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
ui::TextInputClient* OffScreenRenderWidgetHostView::GetTextInputClient() {
2016-07-05 19:33:22 +00:00
return nullptr;
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::Focus() {
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
bool OffScreenRenderWidgetHostView::HasFocus() const {
2016-07-31 23:01:55 +00:00
return false;
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
bool OffScreenRenderWidgetHostView::IsSurfaceAvailableForCopy() const {
2016-09-07 08:33:09 +00:00
return GetDelegatedFrameHost()->CanCopyToBitmap();
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::Show() {
2016-07-13 15:43:00 +00:00
if (is_showing_)
return;
2016-07-05 19:33:22 +00:00
2016-07-13 15:43:00 +00:00
is_showing_ = true;
2016-09-07 08:33:09 +00:00
#if defined(OS_MACOSX)
browser_compositor_->SetRenderWidgetHostIsHidden(false);
#else
2016-07-13 15:43:00 +00:00
delegated_frame_host_->SetCompositor(compositor_.get());
delegated_frame_host_->WasShown(ui::LatencyInfo());
2016-09-07 08:33:09 +00:00
#endif
if (render_widget_host_)
render_widget_host_->WasShown(ui::LatencyInfo());
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::Hide() {
2016-07-13 15:43:00 +00:00
if (!is_showing_)
return;
2016-07-18 14:16:23 +00:00
if (render_widget_host_)
render_widget_host_->WasHidden();
2016-09-07 08:33:09 +00:00
#if defined(OS_MACOSX)
browser_compositor_->SetRenderWidgetHostIsHidden(true);
#else
GetDelegatedFrameHost()->WasHidden();
GetDelegatedFrameHost()->ResetCompositor();
#endif
2016-07-13 15:43:00 +00:00
is_showing_ = false;
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
bool OffScreenRenderWidgetHostView::IsShowing() {
2016-07-13 15:43:00 +00:00
return is_showing_;
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
gfx::Rect OffScreenRenderWidgetHostView::GetViewBounds() const {
2016-07-05 19:33:22 +00:00
return gfx::Rect(size_);
}
2016-08-01 11:27:39 +00:00
void OffScreenRenderWidgetHostView::SetBackgroundColor(SkColor color) {
if (transparent_)
color = SkColorSetARGB(SK_AlphaTRANSPARENT, 0, 0, 0);
content::RenderWidgetHostViewBase::SetBackgroundColor(color);
const bool opaque = !transparent_ && GetBackgroundOpaque();
if (render_widget_host_)
render_widget_host_->SetBackgroundOpaque(opaque);
}
2016-07-29 12:50:27 +00:00
gfx::Size OffScreenRenderWidgetHostView::GetVisibleViewportSize() const {
2016-07-05 19:33:22 +00:00
return size_;
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::SetInsets(const gfx::Insets& insets) {
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
bool OffScreenRenderWidgetHostView::LockMouse() {
2016-07-05 19:33:22 +00:00
return false;
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::UnlockMouse() {
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::OnSwapCompositorFrame(
2016-07-13 15:43:00 +00:00
uint32_t output_surface_id,
2016-09-06 08:24:37 +00:00
cc::CompositorFrame frame) {
2016-07-29 22:51:19 +00:00
TRACE_EVENT0("electron",
"OffScreenRenderWidgetHostView::OnSwapCompositorFrame");
2016-07-13 15:43:00 +00:00
2016-09-06 08:24:37 +00:00
if (frame.metadata.root_scroll_offset != last_scroll_offset_) {
last_scroll_offset_ = frame.metadata.root_scroll_offset;
2016-07-13 15:43:00 +00:00
}
2016-09-06 08:24:37 +00:00
if (frame.delegated_frame_data) {
2016-07-27 17:44:41 +00:00
if (software_output_device_) {
if (!begin_frame_timer_.get()) {
software_output_device_->SetActive(painting_);
2016-07-27 17:44:41 +00:00
}
2016-07-22 11:55:58 +00:00
2016-09-07 08:33:09 +00:00
// 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
2016-07-27 17:44:41 +00:00
delegated_frame_host_->SwapDelegatedFrame(output_surface_id,
std::move(frame));
2016-09-07 08:33:09 +00:00
#endif
2016-07-27 17:44:41 +00:00
} else {
if (!copy_frame_generator_.get()) {
copy_frame_generator_.reset(
2016-07-27 18:31:03 +00:00
new AtomCopyFrameGenerator(frame_rate_threshold_ms_, this));
2016-07-27 17:44:41 +00:00
}
2016-09-07 08:33:09 +00:00
// Determine the damage rectangle for the current frame. This is the same
// calculation that SwapDelegatedFrame uses.
2016-07-27 17:44:41 +00:00
cc::RenderPass* root_pass =
2016-09-06 08:24:37 +00:00
frame.delegated_frame_data->render_pass_list.back().get();
2016-07-27 17:44:41 +00:00
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));
2016-09-07 08:33:09 +00:00
#if defined(OS_MACOSX)
browser_compositor_->SwapCompositorFrame(output_surface_id,
std::move(frame));
#else
2016-07-27 17:44:41 +00:00
delegated_frame_host_->SwapDelegatedFrame(output_surface_id,
std::move(frame));
2016-09-07 08:33:09 +00:00
#endif
2016-07-27 17:44:41 +00:00
2016-09-07 08:33:09 +00:00
// Request a copy of the last compositor frame which will eventually call
// OnPaint asynchronously.
copy_frame_generator_->GenerateCopyFrame(true, damage_rect);
2016-07-27 17:44:41 +00:00
}
2016-07-18 14:16:23 +00:00
}
2016-07-13 15:43:00 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::ClearCompositorFrame() {
2016-09-07 08:33:09 +00:00
GetDelegatedFrameHost()->ClearDelegatedFrame();
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::InitAsPopup(
2016-08-01 10:06:46 +00:00
content::RenderWidgetHostView* parent_host_view, const gfx::Rect& pos) {
2016-07-05 19:33:22 +00:00
}
2016-07-29 22:51:19 +00:00
void OffScreenRenderWidgetHostView::InitAsFullscreen(
content::RenderWidgetHostView *) {
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::UpdateCursor(const content::WebCursor &) {
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::SetIsLoading(bool loading) {
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::TextInputStateChanged(
const content::TextInputState& params) {
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::ImeCancelComposition() {
2016-07-05 19:33:22 +00:00
}
2016-07-31 10:19:56 +00:00
void OffScreenRenderWidgetHostView::RenderProcessGone(base::TerminationStatus,
int) {
2016-07-05 19:33:22 +00:00
Destroy();
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::Destroy() {
2016-07-30 19:25:49 +00:00
delete this;
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::SetTooltipText(const base::string16 &) {
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::SelectionBoundsChanged(
2016-07-13 15:43:00 +00:00
const ViewHostMsg_SelectionBounds_Params &) {
2016-07-05 19:33:22 +00:00
}
2016-07-31 10:19:56 +00:00
void OffScreenRenderWidgetHostView::CopyFromCompositingSurface(
const gfx::Rect& src_subrect,
const gfx::Size& dst_size,
const content::ReadbackRequestCallback& callback,
const SkColorType preferred_color_type) {
2016-09-07 08:33:09 +00:00
GetDelegatedFrameHost()->CopyFromCompositingSurface(
2016-07-05 19:33:22 +00:00
src_subrect, dst_size, callback, preferred_color_type);
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::CopyFromCompositingSurfaceToVideoFrame(
2016-07-31 10:19:56 +00:00
const gfx::Rect& src_subrect,
const scoped_refptr<media::VideoFrame>& target,
const base::Callback<void(const gfx::Rect&, bool)>& callback) {
2016-09-07 08:33:09 +00:00
GetDelegatedFrameHost()->CopyFromCompositingSurfaceToVideoFrame(
2016-07-05 19:33:22 +00:00
src_subrect, target, callback);
}
2016-07-29 12:50:27 +00:00
bool OffScreenRenderWidgetHostView::CanCopyToVideoFrame() const {
2016-09-07 08:33:09 +00:00
return GetDelegatedFrameHost()->CanCopyToVideoFrame();
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::BeginFrameSubscription(
2016-07-05 19:33:22 +00:00
std::unique_ptr<content::RenderWidgetHostViewFrameSubscriber> subscriber) {
2016-09-07 08:33:09 +00:00
GetDelegatedFrameHost()->BeginFrameSubscription(std::move(subscriber));
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::EndFrameSubscription() {
2016-09-07 08:33:09 +00:00
GetDelegatedFrameHost()->EndFrameSubscription();
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
bool OffScreenRenderWidgetHostView::HasAcceleratedSurface(const gfx::Size &) {
2016-07-05 19:33:22 +00:00
return false;
}
2016-07-29 12:50:27 +00:00
gfx::Rect OffScreenRenderWidgetHostView::GetBoundsInRootWindow() {
2016-07-05 19:33:22 +00:00
return gfx::Rect(size_);
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::LockCompositingSurface() {
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::UnlockCompositingSurface() {
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::ImeCompositionRangeChanged(
2016-07-05 19:33:22 +00:00
const gfx::Range &, const std::vector<gfx::Rect>&) {
}
2016-07-29 12:50:27 +00:00
gfx::Size OffScreenRenderWidgetHostView::GetPhysicalBackingSize() const {
2016-07-13 15:43:00 +00:00
return size_;
}
2016-07-29 12:50:27 +00:00
gfx::Size OffScreenRenderWidgetHostView::GetRequestedRendererSize() const {
2016-07-13 15:43:00 +00:00
return size_;
}
2016-11-30 07:30:03 +00:00
#if !defined(OS_MACOSX)
2016-07-29 12:50:27 +00:00
ui::Layer* OffScreenRenderWidgetHostView::DelegatedFrameHostGetLayer() const {
2016-07-13 15:43:00 +00:00
return const_cast<ui::Layer*>(root_layer_.get());
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
bool OffScreenRenderWidgetHostView::DelegatedFrameHostIsVisible() const {
2016-07-13 15:43:00 +00:00
return !render_widget_host_->is_hidden();
2016-07-05 19:33:22 +00:00
}
2016-07-29 22:51:19 +00:00
SkColor OffScreenRenderWidgetHostView::DelegatedFrameHostGetGutterColor(
SkColor color) const {
2016-07-05 19:33:22 +00:00
return color;
}
2016-07-29 22:51:19 +00:00
gfx::Size OffScreenRenderWidgetHostView::DelegatedFrameHostDesiredSizeInDIP()
const {
2016-07-05 19:33:22 +00:00
return size_;
}
2016-07-29 12:50:27 +00:00
bool OffScreenRenderWidgetHostView::DelegatedFrameCanCreateResizeLock() const {
2016-07-05 19:33:22 +00:00
return false;
}
std::unique_ptr<content::ResizeLock>
2016-07-31 10:19:56 +00:00
OffScreenRenderWidgetHostView::DelegatedFrameHostCreateResizeLock(
bool defer_compositor_lock) {
2016-07-05 19:33:22 +00:00
return nullptr;
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::DelegatedFrameHostResizeLockWasReleased() {
2016-07-13 15:43:00 +00:00
return render_widget_host_->WasResized();
2016-07-05 19:33:22 +00:00
}
2016-11-30 07:30:03 +00:00
void
OffScreenRenderWidgetHostView::DelegatedFrameHostSendReclaimCompositorResources(
int output_surface_id,
bool is_swap_ack,
const cc::ReturnedResourceArray& resources) {
2016-07-13 15:43:00 +00:00
render_widget_host_->Send(new ViewMsg_ReclaimCompositorResources(
2016-11-30 07:30:03 +00:00
render_widget_host_->GetRoutingID(), output_surface_id, is_swap_ack,
resources));
2016-07-05 19:33:22 +00:00
}
2016-07-31 10:19:56 +00:00
void OffScreenRenderWidgetHostView::
DelegatedFrameHostOnLostCompositorResources() {
2016-07-13 15:43:00 +00:00
render_widget_host_->ScheduleComposite();
2016-07-05 19:33:22 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::DelegatedFrameHostUpdateVSyncParameters(
2016-07-05 19:33:22 +00:00
const base::TimeTicks& timebase, const base::TimeDelta& interval) {
2016-07-13 15:43:00 +00:00
render_widget_host_->UpdateVSyncParameters(timebase, interval);
2016-07-05 19:33:22 +00:00
}
2016-08-03 04:46:34 +00:00
void OffScreenRenderWidgetHostView::SetBeginFrameSource(
cc::BeginFrameSource* source) {
2016-08-01 11:27:39 +00:00
}
2016-11-30 07:30:03 +00:00
#endif // !defined(OS_MACOSX)
2016-07-27 17:44:41 +00:00
std::unique_ptr<cc::SoftwareOutputDevice>
2016-07-31 10:19:56 +00:00
OffScreenRenderWidgetHostView::CreateSoftwareOutputDevice(
ui::Compositor* compositor) {
2016-09-07 08:33:09 +00:00
DCHECK_EQ(GetCompositor(), compositor);
2016-07-27 16:24:58 +00:00
DCHECK(!copy_frame_generator_);
DCHECK(!software_output_device_);
2016-07-27 17:59:01 +00:00
2016-08-04 04:22:19 +00:00
software_output_device_ = new OffScreenOutputDevice(
transparent_,
2016-07-29 12:50:27 +00:00
base::Bind(&OffScreenRenderWidgetHostView::OnPaint,
2016-07-27 17:44:41 +00:00
weak_ptr_factory_.GetWeakPtr()));
2016-07-27 16:24:58 +00:00
return base::WrapUnique(software_output_device_);
}
2016-07-05 19:33:22 +00:00
2016-08-03 04:46:34 +00:00
bool OffScreenRenderWidgetHostView::InstallTransparency() {
if (transparent_) {
SetBackgroundColor(SkColor());
2016-09-07 08:33:09 +00:00
#if defined(OS_MACOSX)
browser_compositor_->SetHasTransparentBackground(true);
#else
2016-08-03 04:46:34 +00:00
compositor_->SetHostHasTransparentBackground(true);
2016-09-07 08:33:09 +00:00
#endif
2016-08-03 04:46:34 +00:00
return true;
}
return false;
}
bool OffScreenRenderWidgetHostView::IsAutoResizeEnabled() const {
return false;
}
void OffScreenRenderWidgetHostView::SetNeedsBeginFrames(
bool needs_begin_frames) {
SetupFrameRate(false);
begin_frame_timer_->SetActive(needs_begin_frames);
if (software_output_device_) {
software_output_device_->SetActive(needs_begin_frames && painting_);
}
2016-07-22 11:55:58 +00:00
}
2016-07-29 12:50:27 +00:00
void OffScreenRenderWidgetHostView::OnPaint(
2016-08-04 04:22:19 +00:00
const gfx::Rect& damage_rect, const SkBitmap& bitmap) {
2016-07-29 12:50:27 +00:00
TRACE_EVENT0("electron", "OffScreenRenderWidgetHostView::OnPaint");
2016-08-04 04:22:19 +00:00
callback_.Run(damage_rect, bitmap);
2016-07-27 17:44:41 +00:00
}
void OffScreenRenderWidgetHostView::SetPainting(bool painting) {
painting_ = painting;
2016-07-31 20:05:36 +00:00
if (software_output_device_) {
software_output_device_->SetActive(painting_);
2016-07-31 20:05:36 +00:00
}
}
bool OffScreenRenderWidgetHostView::IsPainting() const {
return painting_;
}
void OffScreenRenderWidgetHostView::SetFrameRate(int frame_rate) {
if (frame_rate <= 0)
frame_rate = 1;
if (frame_rate > 60)
frame_rate = 60;
frame_rate_ = frame_rate;
SetupFrameRate(true);
}
int OffScreenRenderWidgetHostView::GetFrameRate() const {
return frame_rate_;
}
2016-09-07 08:33:09 +00:00
#if !defined(OS_MACOSX)
ui::Compositor* OffScreenRenderWidgetHostView::GetCompositor() const {
return compositor_.get();
}
ui::Layer* OffScreenRenderWidgetHostView::GetRootLayer() const {
return root_layer_.get();
}
content::DelegatedFrameHost*
OffScreenRenderWidgetHostView::GetDelegatedFrameHost() const {
return delegated_frame_host_.get();
}
#endif
2016-08-03 04:46:34 +00:00
void OffScreenRenderWidgetHostView::SetupFrameRate(bool force) {
if (!force && frame_rate_threshold_ms_ != 0)
return;
frame_rate_threshold_ms_ = 1000 / frame_rate_;
2016-09-07 08:33:09 +00:00
GetCompositor()->vsync_manager()->SetAuthoritativeVSyncInterval(
2016-08-03 04:46:34 +00:00
base::TimeDelta::FromMilliseconds(frame_rate_threshold_ms_));
if (copy_frame_generator_.get()) {
copy_frame_generator_->set_frame_rate_threshold_ms(
frame_rate_threshold_ms_);
}
if (begin_frame_timer_.get()) {
begin_frame_timer_->SetFrameRateThresholdMs(frame_rate_threshold_ms_);
} else {
begin_frame_timer_.reset(new AtomBeginFrameTimer(
frame_rate_threshold_ms_,
base::Bind(&OffScreenRenderWidgetHostView::OnBeginFrameTimerTick,
weak_ptr_factory_.GetWeakPtr())));
}
}
void OffScreenRenderWidgetHostView::Invalidate() {
const gfx::Rect& bounds_in_pixels = GetViewBounds();
if (software_output_device_) {
software_output_device_->OnPaint(bounds_in_pixels);
} else if (copy_frame_generator_.get()) {
copy_frame_generator_->GenerateCopyFrame(true, bounds_in_pixels);
}
}
2016-08-03 04:46:34 +00:00
void OffScreenRenderWidgetHostView::ResizeRootLayer() {
SetupFrameRate(false);
const float orgScaleFactor = scale_factor_;
const bool scaleFactorDidChange = (orgScaleFactor != scale_factor_);
gfx::Size size = GetViewBounds().size();
2016-09-07 08:33:09 +00:00
if (!scaleFactorDidChange && size == GetRootLayer()->bounds().size())
2016-08-03 04:46:34 +00:00
return;
const gfx::Size& size_in_pixels =
gfx::ConvertSizeToPixel(scale_factor_, size);
2016-09-07 08:33:09 +00:00
GetRootLayer()->SetBounds(gfx::Rect(size));
GetCompositor()->SetScaleAndSize(scale_factor_, size_in_pixels);
2016-08-03 04:46:34 +00:00
}
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;
}
2016-07-05 19:33:22 +00:00
} // namespace atom