refactor: rename the atom directory to shell

This commit is contained in:
Samuel Attard 2019-06-19 13:43:10 -07:00 committed by Samuel Attard
parent 4575a4aae3
commit d7f07e8a80
631 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,122 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/osr/osr_host_display_client.h"
#include <utility>
#include "base/memory/shared_memory.h"
#include "components/viz/common/resources/resource_format.h"
#include "components/viz/common/resources/resource_sizes.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkRect.h"
#include "third_party/skia/src/core/SkDevice.h"
#include "ui/gfx/skia_util.h"
#if defined(OS_WIN)
#include "skia/ext/skia_utils_win.h"
#endif
namespace atom {
LayeredWindowUpdater::LayeredWindowUpdater(
viz::mojom::LayeredWindowUpdaterRequest request,
OnPaintCallback callback)
: callback_(callback), binding_(this, std::move(request)) {}
LayeredWindowUpdater::~LayeredWindowUpdater() = default;
void LayeredWindowUpdater::SetActive(bool active) {
active_ = active;
}
void LayeredWindowUpdater::OnAllocatedSharedMemory(
const gfx::Size& pixel_size,
mojo::ScopedSharedBufferHandle scoped_buffer_handle) {
canvas_.reset();
// Make sure |pixel_size| is sane.
size_t expected_bytes;
bool size_result = viz::ResourceSizes::MaybeSizeInBytes(
pixel_size, viz::ResourceFormat::RGBA_8888, &expected_bytes);
if (!size_result)
return;
#if defined(WIN32)
base::SharedMemoryHandle shm_handle;
size_t required_bytes;
MojoResult unwrap_result = mojo::UnwrapSharedMemoryHandle(
std::move(scoped_buffer_handle), &shm_handle, &required_bytes, nullptr);
if (unwrap_result != MOJO_RESULT_OK)
return;
base::SharedMemory shm(shm_handle, false);
if (!shm.Map(required_bytes)) {
DLOG(ERROR) << "Failed to map " << required_bytes << " bytes";
return;
}
canvas_ = skia::CreatePlatformCanvasWithSharedSection(
pixel_size.width(), pixel_size.height(), false, shm.handle().GetHandle(),
skia::CRASH_ON_FAILURE);
#else
auto shm =
mojo::UnwrapWritableSharedMemoryRegion(std::move(scoped_buffer_handle));
if (!shm.IsValid()) {
DLOG(ERROR) << "Failed to unwrap shared memory region";
return;
}
shm_mapping_ = shm.Map();
if (!shm_mapping_.IsValid()) {
DLOG(ERROR) << "Failed to map shared memory region";
return;
}
canvas_ = skia::CreatePlatformCanvasWithPixels(
pixel_size.width(), pixel_size.height(), false,
static_cast<uint8_t*>(shm_mapping_.memory()), skia::CRASH_ON_FAILURE);
#endif
}
void LayeredWindowUpdater::Draw(const gfx::Rect& damage_rect,
DrawCallback draw_callback) {
SkPixmap pixmap;
SkBitmap bitmap;
if (active_ && canvas_->peekPixels(&pixmap)) {
bitmap.installPixels(pixmap);
callback_.Run(damage_rect, bitmap);
}
std::move(draw_callback).Run();
}
OffScreenHostDisplayClient::OffScreenHostDisplayClient(
gfx::AcceleratedWidget widget,
OnPaintCallback callback)
: viz::HostDisplayClient(widget), callback_(callback) {}
OffScreenHostDisplayClient::~OffScreenHostDisplayClient() {}
void OffScreenHostDisplayClient::SetActive(bool active) {
active_ = active;
if (layered_window_updater_) {
layered_window_updater_->SetActive(active_);
}
}
void OffScreenHostDisplayClient::IsOffscreen(IsOffscreenCallback callback) {
std::move(callback).Run(true);
}
void OffScreenHostDisplayClient::CreateLayeredWindowUpdater(
viz::mojom::LayeredWindowUpdaterRequest request) {
layered_window_updater_ =
std::make_unique<LayeredWindowUpdater>(std::move(request), callback_);
layered_window_updater_->SetActive(active_);
}
} // namespace atom

View file

@ -0,0 +1,77 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_OSR_OSR_HOST_DISPLAY_CLIENT_H_
#define ATOM_BROWSER_OSR_OSR_HOST_DISPLAY_CLIENT_H_
#include <memory>
#include "base/callback.h"
#include "base/memory/shared_memory_mapping.h"
#include "components/viz/host/host_display_client.h"
#include "services/viz/privileged/interfaces/compositing/layered_window_updater.mojom.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "ui/gfx/native_widget_types.h"
namespace atom {
typedef base::Callback<void(const gfx::Rect&, const SkBitmap&)> OnPaintCallback;
class LayeredWindowUpdater : public viz::mojom::LayeredWindowUpdater {
public:
explicit LayeredWindowUpdater(viz::mojom::LayeredWindowUpdaterRequest request,
OnPaintCallback callback);
~LayeredWindowUpdater() override;
void SetActive(bool active);
// viz::mojom::LayeredWindowUpdater implementation.
void OnAllocatedSharedMemory(
const gfx::Size& pixel_size,
mojo::ScopedSharedBufferHandle scoped_buffer_handle) override;
void Draw(const gfx::Rect& damage_rect, DrawCallback draw_callback) override;
private:
OnPaintCallback callback_;
mojo::Binding<viz::mojom::LayeredWindowUpdater> binding_;
std::unique_ptr<SkCanvas> canvas_;
bool active_ = false;
#if !defined(WIN32)
base::WritableSharedMemoryMapping shm_mapping_;
#endif
DISALLOW_COPY_AND_ASSIGN(LayeredWindowUpdater);
};
class OffScreenHostDisplayClient : public viz::HostDisplayClient {
public:
explicit OffScreenHostDisplayClient(gfx::AcceleratedWidget widget,
OnPaintCallback callback);
~OffScreenHostDisplayClient() override;
void SetActive(bool active);
private:
void IsOffscreen(IsOffscreenCallback callback) override;
#if defined(OS_MACOSX)
void OnDisplayReceivedCALayerParams(
const gfx::CALayerParams& ca_layer_params) override;
#endif
void CreateLayeredWindowUpdater(
viz::mojom::LayeredWindowUpdaterRequest request) override;
std::unique_ptr<LayeredWindowUpdater> layered_window_updater_;
OnPaintCallback callback_;
bool active_ = false;
DISALLOW_COPY_AND_ASSIGN(OffScreenHostDisplayClient);
};
} // namespace atom
#endif // ATOM_BROWSER_OSR_OSR_HOST_DISPLAY_CLIENT_H_

View file

@ -0,0 +1,35 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/osr/osr_host_display_client.h"
#include <IOSurface/IOSurface.h>
namespace atom {
void OffScreenHostDisplayClient::OnDisplayReceivedCALayerParams(
const gfx::CALayerParams& ca_layer_params) {
if (!ca_layer_params.is_empty) {
base::ScopedCFTypeRef<IOSurfaceRef> io_surface(
IOSurfaceLookupFromMachPort(ca_layer_params.io_surface_mach_port));
gfx::Size pixel_size_ = ca_layer_params.pixel_size;
void* pixels = static_cast<void*>(IOSurfaceGetBaseAddress(io_surface));
size_t stride = IOSurfaceGetBytesPerRow(io_surface);
struct IOSurfacePinner {
base::ScopedCFTypeRef<IOSurfaceRef> io_surface;
};
SkBitmap bitmap;
bitmap.installPixels(
SkImageInfo::MakeN32(pixel_size_.width(), pixel_size_.height(),
kPremul_SkAlphaType),
pixels, stride);
bitmap.setImmutable();
callback_.Run(ca_layer_params.damage, bitmap);
}
}
} // namespace atom

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,323 @@
// Copyright (c) 2016 GitHub, Inc.
// 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_
#include <memory>
#include <set>
#include <string>
#include <vector>
#if defined(OS_WIN)
#include <windows.h>
#endif
#include "atom/browser/osr/osr_host_display_client.h"
#include "atom/browser/osr/osr_video_consumer.h"
#include "atom/browser/osr/osr_view_proxy.h"
#include "base/process/kill.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"
#include "components/viz/common/frame_sinks/begin_frame_source.h"
#include "components/viz/common/quads/compositor_frame.h"
#include "components/viz/common/surfaces/parent_local_surface_id_allocator.h"
#include "content/browser/frame_host/render_widget_host_view_guest.h" // nogncheck
#include "content/browser/renderer_host/delegated_frame_host.h" // nogncheck
#include "content/browser/renderer_host/input/mouse_wheel_phase_handler.h" // nogncheck
#include "content/browser/renderer_host/render_widget_host_impl.h" // nogncheck
#include "content/browser/renderer_host/render_widget_host_view_base.h" // nogncheck
#include "content/browser/web_contents/web_contents_view.h" // nogncheck
#include "third_party/blink/public/platform/web_vector.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer_delegate.h"
#include "ui/compositor/layer_owner.h"
#include "ui/gfx/geometry/point.h"
#include "components/viz/host/host_display_client.h"
#include "ui/compositor/external_begin_frame_client.h"
#if defined(OS_WIN)
#include "ui/gfx/win/window_impl.h"
#endif
namespace content {
class CursorManager;
} // namespace content
namespace atom {
class AtomCopyFrameGenerator;
class AtomBeginFrameTimer;
class AtomDelegatedFrameHostClient;
typedef base::Callback<void(const gfx::Rect&, const SkBitmap&)> OnPaintCallback;
typedef base::Callback<void(const gfx::Rect&)> OnPopupPaintCallback;
class OffScreenRenderWidgetHostView : public content::RenderWidgetHostViewBase,
public ui::ExternalBeginFrameClient,
public ui::CompositorDelegate,
public OffscreenViewProxyObserver {
public:
OffScreenRenderWidgetHostView(bool transparent,
bool painting,
int frame_rate,
const OnPaintCallback& callback,
content::RenderWidgetHost* render_widget_host,
OffScreenRenderWidgetHostView* parent_host_view,
gfx::Size initial_size);
~OffScreenRenderWidgetHostView() override;
content::BrowserAccessibilityManager* CreateBrowserAccessibilityManager(
content::BrowserAccessibilityDelegate*,
bool) override;
void OnDisplayDidFinishFrame(const viz::BeginFrameAck& ack) override;
void OnNeedsExternalBeginFrames(bool needs_begin_frames) override;
// content::RenderWidgetHostView:
void InitAsChild(gfx::NativeView) override;
void SetSize(const gfx::Size&) override;
void SetBounds(const gfx::Rect&) override;
gfx::NativeView GetNativeView(void) override;
gfx::NativeViewAccessible GetNativeViewAccessible(void) override;
ui::TextInputClient* GetTextInputClient() override;
void Focus(void) override;
bool HasFocus(void) override;
uint32_t GetCaptureSequenceNumber() const override;
bool IsSurfaceAvailableForCopy(void) override;
void Show(void) override;
void Hide(void) override;
bool IsShowing(void) override;
void EnsureSurfaceSynchronizedForWebTest() override;
gfx::Rect GetViewBounds(void) override;
gfx::Size GetVisibleViewportSize() override;
void SetInsets(const gfx::Insets&) override;
void SetBackgroundColor(SkColor color) override;
base::Optional<SkColor> GetBackgroundColor() override;
void UpdateBackgroundColor() override;
bool LockMouse(void) override;
void UnlockMouse(void) override;
void TakeFallbackContentFrom(content::RenderWidgetHostView* view) override;
void SetNeedsBeginFrames(bool needs_begin_frames) override;
void SetWantsAnimateOnlyBeginFrames() override;
#if defined(OS_MACOSX)
void SetActive(bool active) override;
void ShowDefinitionForSelection() override;
void SpeakSelection() override;
bool UpdateNSViewAndDisplay();
#endif // defined(OS_MACOSX)
// content::RenderWidgetHostViewBase:
void DidCreateNewRendererCompositorFrameSink(
viz::mojom::CompositorFrameSinkClient* renderer_compositor_frame_sink)
override;
void SubmitCompositorFrame(
const viz::LocalSurfaceId& local_surface_id,
viz::CompositorFrame frame,
base::Optional<viz::HitTestRegionList> hit_test_region_list) override;
void ClearCompositorFrame(void) override;
void ResetFallbackToFirstNavigationSurface() 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() override;
void Destroy(void) override;
void SetTooltipText(const base::string16&) override;
content::CursorManager* GetCursorManager() override;
void CopyFromSurface(
const gfx::Rect& src_rect,
const gfx::Size& output_size,
base::OnceCallback<void(const SkBitmap&)> callback) override;
void GetScreenInfo(content::ScreenInfo* results) override;
void InitAsGuest(content::RenderWidgetHostView*,
content::RenderWidgetHostViewGuest*) override;
void TransformPointToRootSurface(gfx::PointF* point) override;
gfx::Rect GetBoundsInRootWindow(void) override;
viz::SurfaceId GetCurrentSurfaceId() const override;
std::unique_ptr<content::SyntheticGestureTarget>
CreateSyntheticGestureTarget() override;
void ImeCompositionRangeChanged(const gfx::Range&,
const std::vector<gfx::Rect>&) override;
gfx::Size GetCompositorViewportPixelSize() override;
content::RenderWidgetHostViewBase* CreateViewForWidget(
content::RenderWidgetHost*,
content::RenderWidgetHost*,
content::WebContentsView*) override;
const viz::LocalSurfaceIdAllocation& GetLocalSurfaceIdAllocation()
const override;
const viz::FrameSinkId& GetFrameSinkId() const override;
void DidNavigate() override;
bool TransformPointToLocalCoordSpaceLegacy(
const gfx::PointF& point,
const viz::SurfaceId& original_surface,
gfx::PointF* transformed_point) override;
bool TransformPointToCoordSpaceForView(
const gfx::PointF& point,
RenderWidgetHostViewBase* target_view,
gfx::PointF* transformed_point) override;
// ui::CompositorDelegate:
std::unique_ptr<viz::HostDisplayClient> CreateHostDisplayClient(
ui::Compositor* compositor) override;
bool InstallTransparency();
void OnBeginFrameTimerTick();
void SendBeginFrame(base::TimeTicks frame_time, base::TimeDelta vsync_period);
void CancelWidget();
void AddGuestHostView(OffScreenRenderWidgetHostView* guest_host);
void RemoveGuestHostView(OffScreenRenderWidgetHostView* guest_host);
void AddViewProxy(OffscreenViewProxy* proxy);
void RemoveViewProxy(OffscreenViewProxy* proxy);
void ProxyViewDestroyed(OffscreenViewProxy* proxy) override;
void OnPaint(const gfx::Rect& damage_rect, const SkBitmap& bitmap);
void OnPopupPaint(const gfx::Rect& damage_rect);
void OnProxyViewPaint(const gfx::Rect& damage_rect) override;
gfx::Size SizeInPixels();
void CompositeFrame(const gfx::Rect& damage_rect);
bool IsPopupWidget() const {
return widget_type_ == content::WidgetType::kPopup;
}
const SkBitmap& GetBacking() { return *backing_.get(); }
void HoldResize();
void ReleaseResize();
void SynchronizeVisualProperties();
void SendMouseEvent(const blink::WebMouseEvent& event);
void SendMouseWheelEvent(const blink::WebMouseWheelEvent& event);
void SetPainting(bool painting);
bool IsPainting() const;
void SetFrameRate(int frame_rate);
int GetFrameRate() const;
ui::Compositor* GetCompositor() const;
ui::Layer* GetRootLayer() const;
content::DelegatedFrameHost* GetDelegatedFrameHost() const;
void Invalidate();
void InvalidateBounds(const gfx::Rect&);
content::RenderWidgetHostImpl* render_widget_host() const {
return render_widget_host_;
}
gfx::Size size() const { return size_; }
void set_popup_host_view(OffScreenRenderWidgetHostView* popup_view) {
popup_host_view_ = popup_view;
}
void set_child_host_view(OffScreenRenderWidgetHostView* child_view) {
child_host_view_ = child_view;
}
private:
void SetupFrameRate(bool force);
void ResizeRootLayer(bool force);
viz::FrameSinkId AllocateFrameSinkId(bool is_guest_view_hack);
// Applies background color without notifying the RenderWidget about
// opaqueness changes.
void UpdateBackgroundColorFromRenderer(SkColor color);
// Weak ptrs.
content::RenderWidgetHostImpl* render_widget_host_;
OffScreenRenderWidgetHostView* parent_host_view_ = nullptr;
OffScreenRenderWidgetHostView* popup_host_view_ = nullptr;
OffScreenRenderWidgetHostView* child_host_view_ = nullptr;
std::set<OffScreenRenderWidgetHostView*> guest_host_views_;
std::set<OffscreenViewProxy*> proxy_views_;
const bool transparent_;
OnPaintCallback callback_;
OnPopupPaintCallback parent_callback_;
int frame_rate_ = 0;
int frame_rate_threshold_us_ = 0;
base::Time last_time_ = base::Time::Now();
gfx::Vector2dF last_scroll_offset_;
gfx::Size size_;
bool painting_;
bool is_showing_ = false;
bool is_destroyed_ = false;
gfx::Rect popup_position_;
bool hold_resize_ = false;
bool pending_resize_ = false;
bool paint_callback_running_ = false;
viz::LocalSurfaceIdAllocation delegated_frame_host_allocation_;
viz::ParentLocalSurfaceIdAllocator delegated_frame_host_allocator_;
viz::LocalSurfaceIdAllocation compositor_allocation_;
viz::ParentLocalSurfaceIdAllocator compositor_allocator_;
std::unique_ptr<ui::Layer> root_layer_;
std::unique_ptr<ui::Compositor> compositor_;
std::unique_ptr<content::DelegatedFrameHost> delegated_frame_host_;
std::unique_ptr<content::CursorManager> cursor_manager_;
std::unique_ptr<AtomBeginFrameTimer> begin_frame_timer_;
OffScreenHostDisplayClient* host_display_client_;
std::unique_ptr<OffScreenVideoConsumer> video_consumer_;
// Provides |source_id| for BeginFrameArgs that we create.
viz::StubBeginFrameSource begin_frame_source_;
uint64_t begin_frame_number_ = viz::BeginFrameArgs::kStartingFrameNumber;
std::unique_ptr<AtomDelegatedFrameHostClient> delegated_frame_host_client_;
content::MouseWheelPhaseHandler mouse_wheel_phase_handler_;
viz::mojom::CompositorFrameSinkClient* renderer_compositor_frame_sink_ =
nullptr;
// Latest capture sequence number which is incremented when the caller
// requests surfaces be synchronized via
// EnsureSurfaceSynchronizedForWebTest().
uint32_t latest_capture_sequence_number_ = 0u;
SkColor background_color_ = SkColor();
std::unique_ptr<SkBitmap> backing_;
base::WeakPtrFactory<OffScreenRenderWidgetHostView> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(OffScreenRenderWidgetHostView);
};
} // namespace atom
#endif // ATOM_BROWSER_OSR_OSR_RENDER_WIDGET_HOST_VIEW_H_

View file

@ -0,0 +1,137 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/osr/osr_video_consumer.h"
#include <utility>
#include "atom/browser/osr/osr_render_widget_host_view.h"
#include "media/base/video_frame_metadata.h"
#include "media/capture/mojom/video_capture_types.mojom.h"
#include "ui/gfx/skbitmap_operations.h"
namespace atom {
OffScreenVideoConsumer::OffScreenVideoConsumer(
OffScreenRenderWidgetHostView* view,
OnPaintCallback callback)
: callback_(callback),
view_(view),
video_capturer_(view->CreateVideoCapturer()),
weak_ptr_factory_(this) {
video_capturer_->SetResolutionConstraints(view_->SizeInPixels(),
view_->SizeInPixels(), true);
video_capturer_->SetAutoThrottlingEnabled(false);
video_capturer_->SetMinSizeChangePeriod(base::TimeDelta());
video_capturer_->SetFormat(media::PIXEL_FORMAT_ARGB,
gfx::ColorSpace::CreateREC709());
SetFrameRate(view_->GetFrameRate());
}
OffScreenVideoConsumer::~OffScreenVideoConsumer() = default;
void OffScreenVideoConsumer::SetActive(bool active) {
if (active) {
video_capturer_->Start(this);
} else {
video_capturer_->Stop();
}
}
void OffScreenVideoConsumer::SetFrameRate(int frame_rate) {
video_capturer_->SetMinCapturePeriod(base::TimeDelta::FromSeconds(1) /
frame_rate);
}
void OffScreenVideoConsumer::SizeChanged() {
video_capturer_->SetResolutionConstraints(view_->SizeInPixels(),
view_->SizeInPixels(), true);
video_capturer_->RequestRefreshFrame();
}
void OffScreenVideoConsumer::OnFrameCaptured(
base::ReadOnlySharedMemoryRegion data,
::media::mojom::VideoFrameInfoPtr info,
const gfx::Rect& content_rect,
viz::mojom::FrameSinkVideoConsumerFrameCallbacksPtr callbacks) {
if (!CheckContentRect(content_rect)) {
gfx::Size view_size = view_->SizeInPixels();
video_capturer_->SetResolutionConstraints(view_size, view_size, true);
video_capturer_->RequestRefreshFrame();
return;
}
if (!data.IsValid()) {
callbacks->Done();
return;
}
base::ReadOnlySharedMemoryMapping mapping = data.Map();
if (!mapping.IsValid()) {
DLOG(ERROR) << "Shared memory mapping failed.";
return;
}
if (mapping.size() <
media::VideoFrame::AllocationSize(info->pixel_format, info->coded_size)) {
DLOG(ERROR) << "Shared memory size was less than expected.";
return;
}
// The SkBitmap's pixels will be marked as immutable, but the installPixels()
// API requires a non-const pointer. So, cast away the const.
void* const pixels = const_cast<void*>(mapping.memory());
// Call installPixels() with a |releaseProc| that: 1) notifies the capturer
// that this consumer has finished with the frame, and 2) releases the shared
// memory mapping.
struct FramePinner {
// Keeps the shared memory that backs |frame_| mapped.
base::ReadOnlySharedMemoryMapping mapping;
// Prevents FrameSinkVideoCapturer from recycling the shared memory that
// backs |frame_|.
viz::mojom::FrameSinkVideoConsumerFrameCallbacksPtr releaser;
};
SkBitmap bitmap;
bitmap.installPixels(
SkImageInfo::MakeN32(content_rect.width(), content_rect.height(),
kPremul_SkAlphaType),
pixels,
media::VideoFrame::RowBytes(media::VideoFrame::kARGBPlane,
info->pixel_format, info->coded_size.width()),
[](void* addr, void* context) {
delete static_cast<FramePinner*>(context);
},
new FramePinner{std::move(mapping), std::move(callbacks)});
bitmap.setImmutable();
media::VideoFrameMetadata metadata;
metadata.MergeInternalValuesFrom(info->metadata);
gfx::Rect damage_rect;
auto UPDATE_RECT = media::VideoFrameMetadata::CAPTURE_UPDATE_RECT;
if (!metadata.GetRect(UPDATE_RECT, &damage_rect)) {
damage_rect = content_rect;
}
callback_.Run(damage_rect, bitmap);
}
void OffScreenVideoConsumer::OnStopped() {}
bool OffScreenVideoConsumer::CheckContentRect(const gfx::Rect& content_rect) {
gfx::Size view_size = view_->SizeInPixels();
gfx::Size content_size = content_rect.size();
if (std::abs(view_size.width() - content_size.width()) > 2) {
return false;
}
if (std::abs(view_size.height() - content_size.height()) > 2) {
return false;
}
return true;
}
} // namespace atom

View file

@ -0,0 +1,54 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_OSR_OSR_VIDEO_CONSUMER_H_
#define ATOM_BROWSER_OSR_OSR_VIDEO_CONSUMER_H_
#include <memory>
#include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "components/viz/host/client_frame_sink_video_capturer.h"
namespace atom {
class OffScreenRenderWidgetHostView;
typedef base::RepeatingCallback<void(const gfx::Rect&, const SkBitmap&)>
OnPaintCallback;
class OffScreenVideoConsumer : public viz::mojom::FrameSinkVideoConsumer {
public:
OffScreenVideoConsumer(OffScreenRenderWidgetHostView* view,
OnPaintCallback callback);
~OffScreenVideoConsumer() override;
void SetActive(bool active);
void SetFrameRate(int frame_rate);
void SizeChanged();
private:
// viz::mojom::FrameSinkVideoConsumer implementation.
void OnFrameCaptured(
base::ReadOnlySharedMemoryRegion data,
::media::mojom::VideoFrameInfoPtr info,
const gfx::Rect& content_rect,
viz::mojom::FrameSinkVideoConsumerFrameCallbacksPtr callbacks) override;
void OnStopped() override;
bool CheckContentRect(const gfx::Rect& content_rect);
OnPaintCallback callback_;
OffScreenRenderWidgetHostView* view_;
std::unique_ptr<viz::ClientFrameSinkVideoCapturer> video_capturer_;
base::WeakPtrFactory<OffScreenVideoConsumer> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(OffScreenVideoConsumer);
};
} // namespace atom
#endif // ATOM_BROWSER_OSR_OSR_VIDEO_CONSUMER_H_

View file

@ -0,0 +1,56 @@
// Copyright (c) 2017 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/osr/osr_view_proxy.h"
namespace atom {
OffscreenViewProxy::OffscreenViewProxy(views::View* view) : view_(view) {
view_bitmap_.reset(new SkBitmap);
}
OffscreenViewProxy::~OffscreenViewProxy() {
if (observer_) {
observer_->ProxyViewDestroyed(this);
}
}
void OffscreenViewProxy::SetObserver(OffscreenViewProxyObserver* observer) {
if (observer_) {
observer_->ProxyViewDestroyed(this);
}
observer_ = observer;
}
void OffscreenViewProxy::RemoveObserver() {
observer_ = nullptr;
}
const SkBitmap* OffscreenViewProxy::GetBitmap() const {
return view_bitmap_.get();
}
void OffscreenViewProxy::SetBitmap(const SkBitmap& bitmap) {
if (view_bounds_.width() == bitmap.width() &&
view_bounds_.height() == bitmap.height() && observer_) {
view_bitmap_.reset(new SkBitmap(bitmap));
observer_->OnProxyViewPaint(view_bounds_);
}
}
const gfx::Rect& OffscreenViewProxy::GetBounds() {
return view_bounds_;
}
void OffscreenViewProxy::SetBounds(const gfx::Rect& bounds) {
view_bounds_ = bounds;
}
void OffscreenViewProxy::OnEvent(ui::Event* event) {
if (view_) {
view_->OnEvent(event);
}
}
} // namespace atom

View file

@ -0,0 +1,55 @@
// Copyright (c) 2017 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_OSR_OSR_VIEW_PROXY_H_
#define ATOM_BROWSER_OSR_OSR_VIEW_PROXY_H_
#include <memory>
#include <set>
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/events/event.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/view.h"
namespace atom {
class OffscreenViewProxy;
class OffscreenViewProxyObserver {
public:
virtual void OnProxyViewPaint(const gfx::Rect& damage_rect) = 0;
virtual void ProxyViewDestroyed(OffscreenViewProxy* proxy) = 0;
};
class OffscreenViewProxy {
public:
explicit OffscreenViewProxy(views::View* view);
~OffscreenViewProxy();
void SetObserver(OffscreenViewProxyObserver* observer);
void RemoveObserver();
const SkBitmap* GetBitmap() const;
void SetBitmap(const SkBitmap& bitmap);
const gfx::Rect& GetBounds();
void SetBounds(const gfx::Rect& bounds);
void OnEvent(ui::Event* event);
void ResetView() { view_ = nullptr; }
private:
views::View* view_;
gfx::Rect view_bounds_;
std::unique_ptr<SkBitmap> view_bitmap_;
OffscreenViewProxyObserver* observer_ = nullptr;
};
} // namespace atom
#endif // ATOM_BROWSER_OSR_OSR_VIEW_PROXY_H_

View file

@ -0,0 +1,225 @@
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/osr/osr_web_contents_view.h"
#include "content/browser/web_contents/web_contents_impl.h" // nogncheck
#include "content/public/browser/render_view_host.h"
#include "third_party/blink/public/platform/web_screen_info.h"
#include "ui/display/screen.h"
namespace atom {
OffScreenWebContentsView::OffScreenWebContentsView(
bool transparent,
const OnPaintCallback& callback)
: native_window_(nullptr), transparent_(transparent), callback_(callback) {
#if defined(OS_MACOSX)
PlatformCreate();
#endif
}
OffScreenWebContentsView::~OffScreenWebContentsView() {
if (native_window_)
native_window_->RemoveObserver(this);
#if defined(OS_MACOSX)
PlatformDestroy();
#endif
}
void OffScreenWebContentsView::SetWebContents(
content::WebContents* web_contents) {
web_contents_ = web_contents;
RenderViewCreated(web_contents_->GetRenderViewHost());
}
void OffScreenWebContentsView::SetNativeWindow(NativeWindow* window) {
if (native_window_)
native_window_->RemoveObserver(this);
native_window_ = window;
if (native_window_)
native_window_->AddObserver(this);
OnWindowResize();
}
void OffScreenWebContentsView::OnWindowResize() {
// In offscreen mode call RenderWidgetHostView's SetSize explicitly
if (GetView())
GetView()->SetSize(GetSize());
}
void OffScreenWebContentsView::OnWindowClosed() {
if (native_window_) {
native_window_->RemoveObserver(this);
native_window_ = nullptr;
}
}
gfx::Size OffScreenWebContentsView::GetSize() {
return native_window_ ? native_window_->GetSize() : gfx::Size();
}
#if !defined(OS_MACOSX)
gfx::NativeView OffScreenWebContentsView::GetNativeView() const {
if (!native_window_)
return gfx::NativeView();
return native_window_->GetNativeView();
}
gfx::NativeView OffScreenWebContentsView::GetContentNativeView() const {
if (!native_window_)
return gfx::NativeView();
return native_window_->GetNativeView();
}
gfx::NativeWindow OffScreenWebContentsView::GetTopLevelNativeWindow() const {
if (!native_window_)
return gfx::NativeWindow();
return native_window_->GetNativeWindow();
}
#endif
void OffScreenWebContentsView::GetContainerBounds(gfx::Rect* out) const {
*out = GetViewBounds();
}
void OffScreenWebContentsView::SizeContents(const gfx::Size& size) {}
void OffScreenWebContentsView::Focus() {}
void OffScreenWebContentsView::SetInitialFocus() {}
void OffScreenWebContentsView::StoreFocus() {}
void OffScreenWebContentsView::RestoreFocus() {}
void OffScreenWebContentsView::FocusThroughTabTraversal(bool reverse) {}
content::DropData* OffScreenWebContentsView::GetDropData() const {
return nullptr;
}
gfx::Rect OffScreenWebContentsView::GetViewBounds() const {
return GetView() ? GetView()->GetViewBounds() : gfx::Rect();
}
void OffScreenWebContentsView::CreateView(const gfx::Size& initial_size,
gfx::NativeView context) {}
content::RenderWidgetHostViewBase*
OffScreenWebContentsView::CreateViewForWidget(
content::RenderWidgetHost* render_widget_host,
bool is_guest_view_hack) {
if (render_widget_host->GetView()) {
return static_cast<content::RenderWidgetHostViewBase*>(
render_widget_host->GetView());
}
return new OffScreenRenderWidgetHostView(
transparent_, painting_, GetFrameRate(), callback_, render_widget_host,
nullptr, GetSize());
}
content::RenderWidgetHostViewBase*
OffScreenWebContentsView::CreateViewForChildWidget(
content::RenderWidgetHost* render_widget_host) {
content::WebContentsImpl* web_contents_impl =
static_cast<content::WebContentsImpl*>(web_contents_);
OffScreenRenderWidgetHostView* view =
static_cast<OffScreenRenderWidgetHostView*>(
web_contents_impl->GetOuterWebContents()
? web_contents_impl->GetOuterWebContents()
->GetRenderWidgetHostView()
: web_contents_impl->GetRenderWidgetHostView());
return new OffScreenRenderWidgetHostView(transparent_, painting_,
view->GetFrameRate(), callback_,
render_widget_host, view, GetSize());
}
void OffScreenWebContentsView::SetPageTitle(const base::string16& title) {}
void OffScreenWebContentsView::RenderViewCreated(
content::RenderViewHost* host) {
if (GetView())
GetView()->InstallTransparency();
}
void OffScreenWebContentsView::RenderViewReady() {}
void OffScreenWebContentsView::RenderViewHostChanged(
content::RenderViewHost* old_host,
content::RenderViewHost* new_host) {}
void OffScreenWebContentsView::SetOverscrollControllerEnabled(bool enabled) {}
#if defined(OS_MACOSX)
bool OffScreenWebContentsView::CloseTabAfterEventTrackingIfNeeded() {
return false;
}
#endif // defined(OS_MACOSX)
void OffScreenWebContentsView::StartDragging(
const content::DropData& drop_data,
blink::WebDragOperationsMask allowed_ops,
const gfx::ImageSkia& image,
const gfx::Vector2d& image_offset,
const content::DragEventSourceInfo& event_info,
content::RenderWidgetHostImpl* source_rwh) {
if (web_contents_)
web_contents_->SystemDragEnded(source_rwh);
}
void OffScreenWebContentsView::UpdateDragCursor(
blink::WebDragOperation operation) {}
void OffScreenWebContentsView::SetPainting(bool painting) {
auto* view = GetView();
painting_ = painting;
if (view != nullptr) {
view->SetPainting(painting);
}
}
bool OffScreenWebContentsView::IsPainting() const {
auto* view = GetView();
if (view != nullptr) {
return view->IsPainting();
} else {
return painting_;
}
}
void OffScreenWebContentsView::SetFrameRate(int frame_rate) {
auto* view = GetView();
frame_rate_ = frame_rate;
if (view != nullptr) {
view->SetFrameRate(frame_rate);
}
}
int OffScreenWebContentsView::GetFrameRate() const {
auto* view = GetView();
if (view != nullptr) {
return view->GetFrameRate();
} else {
return frame_rate_;
}
}
OffScreenRenderWidgetHostView* OffScreenWebContentsView::GetView() const {
if (web_contents_) {
return static_cast<OffScreenRenderWidgetHostView*>(
web_contents_->GetRenderViewHost()->GetWidget()->GetView());
}
return nullptr;
}
} // namespace atom

View file

@ -0,0 +1,112 @@
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_OSR_OSR_WEB_CONTENTS_VIEW_H_
#define ATOM_BROWSER_OSR_OSR_WEB_CONTENTS_VIEW_H_
#include "atom/browser/native_window.h"
#include "atom/browser/native_window_observer.h"
#include "atom/browser/osr/osr_render_widget_host_view.h"
#include "content/browser/renderer_host/render_view_host_delegate_view.h" // nogncheck
#include "content/browser/web_contents/web_contents_view.h" // nogncheck
#include "content/public/browser/web_contents.h"
#if defined(OS_MACOSX)
#ifdef __OBJC__
@class OffScreenView;
#else
class OffScreenView;
#endif
#endif
namespace atom {
class OffScreenWebContentsView : public content::WebContentsView,
public content::RenderViewHostDelegateView,
public NativeWindowObserver {
public:
OffScreenWebContentsView(bool transparent, const OnPaintCallback& callback);
~OffScreenWebContentsView() override;
void SetWebContents(content::WebContents*);
void SetNativeWindow(NativeWindow* window);
// NativeWindowObserver:
void OnWindowResize() override;
void OnWindowClosed() override;
gfx::Size GetSize();
// content::WebContentsView:
gfx::NativeView GetNativeView() const override;
gfx::NativeView GetContentNativeView() const override;
gfx::NativeWindow GetTopLevelNativeWindow() const override;
void GetContainerBounds(gfx::Rect* out) const override;
void SizeContents(const gfx::Size& size) override;
void Focus() override;
void SetInitialFocus() override;
void StoreFocus() override;
void RestoreFocus() override;
void FocusThroughTabTraversal(bool reverse) override;
content::DropData* GetDropData() const override;
gfx::Rect GetViewBounds() const override;
void CreateView(const gfx::Size& initial_size,
gfx::NativeView context) override;
content::RenderWidgetHostViewBase* CreateViewForWidget(
content::RenderWidgetHost* render_widget_host,
bool is_guest_view_hack) override;
content::RenderWidgetHostViewBase* CreateViewForChildWidget(
content::RenderWidgetHost* render_widget_host) override;
void SetPageTitle(const base::string16& title) override;
void RenderViewCreated(content::RenderViewHost* host) override;
void RenderViewReady() override;
void RenderViewHostChanged(content::RenderViewHost* old_host,
content::RenderViewHost* new_host) override;
void SetOverscrollControllerEnabled(bool enabled) override;
#if defined(OS_MACOSX)
bool CloseTabAfterEventTrackingIfNeeded() override;
#endif
// content::RenderViewHostDelegateView
void StartDragging(const content::DropData& drop_data,
blink::WebDragOperationsMask allowed_ops,
const gfx::ImageSkia& image,
const gfx::Vector2d& image_offset,
const content::DragEventSourceInfo& event_info,
content::RenderWidgetHostImpl* source_rwh) override;
void UpdateDragCursor(blink::WebDragOperation operation) override;
void SetPainting(bool painting);
bool IsPainting() const;
void SetFrameRate(int frame_rate);
int GetFrameRate() const;
private:
#if defined(OS_MACOSX)
void PlatformCreate();
void PlatformDestroy();
#endif
OffScreenRenderWidgetHostView* GetView() const;
NativeWindow* native_window_;
const bool transparent_;
bool painting_ = true;
int frame_rate_ = 60;
OnPaintCallback callback_;
// Weak refs.
content::WebContents* web_contents_ = nullptr;
#if defined(OS_MACOSX)
OffScreenView* offScreenView_;
#endif
};
} // namespace atom
#endif // ATOM_BROWSER_OSR_OSR_WEB_CONTENTS_VIEW_H_

View file

@ -0,0 +1,54 @@
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/osr/osr_web_contents_view.h"
#import <Cocoa/Cocoa.h>
@interface OffScreenView : NSView
@end
@implementation OffScreenView
- (void)drawRect:(NSRect)dirtyRect {
NSString* str = @"No content under offscreen mode";
NSMutableParagraphStyle* paragraphStyle =
[[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
[paragraphStyle setAlignment:NSCenterTextAlignment];
NSDictionary* attributes =
[NSDictionary dictionaryWithObject:paragraphStyle
forKey:NSParagraphStyleAttributeName];
NSAttributedString* text =
[[[NSAttributedString alloc] initWithString:str
attributes:attributes] autorelease];
NSRect frame = NSMakeRect(0, (self.frame.size.height - text.size.height) / 2,
self.frame.size.width, text.size.height);
[str drawInRect:frame withAttributes:attributes];
}
@end
namespace atom {
gfx::NativeView OffScreenWebContentsView::GetNativeView() const {
return offScreenView_;
}
gfx::NativeView OffScreenWebContentsView::GetContentNativeView() const {
return offScreenView_;
}
gfx::NativeWindow OffScreenWebContentsView::GetTopLevelNativeWindow() const {
return [offScreenView_ window];
}
void OffScreenWebContentsView::PlatformCreate() {
offScreenView_ = [[OffScreenView alloc] init];
}
void OffScreenWebContentsView::PlatformDestroy() {
[offScreenView_ release];
}
} // namespace atom