feat: GPU shared texture offscreen rendering (33-x-y) (#44511)
* feat: GPU shared texture offscreen rendering * fix: EmitWarning in node utils. Signed-off-by: reito <cnschwarzer@qq.com> * fix: resolve conflict and reformat --------- Signed-off-by: reito <cnschwarzer@qq.com>
This commit is contained in:
parent
c8dc8717a4
commit
65cd09ffaf
34 changed files with 1009 additions and 102 deletions
|
@ -122,6 +122,7 @@
|
|||
#include "shell/common/gin_converters/image_converter.h"
|
||||
#include "shell/common/gin_converters/net_converter.h"
|
||||
#include "shell/common/gin_converters/optional_converter.h"
|
||||
#include "shell/common/gin_converters/osr_converter.h"
|
||||
#include "shell/common/gin_converters/value_converter.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/gin_helper/error_thrower.h"
|
||||
|
@ -760,9 +761,23 @@ WebContents::WebContents(v8::Isolate* isolate,
|
|||
// Get transparent for guest view
|
||||
options.Get("transparent", &guest_transparent_);
|
||||
|
||||
bool b = false;
|
||||
if (options.Get(options::kOffscreen, &b) && b)
|
||||
type_ = Type::kOffScreen;
|
||||
// Offscreen rendering
|
||||
v8::Local<v8::Value> use_offscreen;
|
||||
if (options.Get(options::kOffscreen, &use_offscreen)) {
|
||||
if (use_offscreen->IsBoolean()) {
|
||||
bool b = false;
|
||||
if (options.Get(options::kOffscreen, &b) && b) {
|
||||
type_ = Type::kOffScreen;
|
||||
}
|
||||
} else if (use_offscreen->IsObject()) {
|
||||
type_ = Type::kOffScreen;
|
||||
auto use_offscreen_dict =
|
||||
gin_helper::Dictionary::CreateEmpty(options.isolate());
|
||||
options.Get(options::kOffscreen, &use_offscreen_dict);
|
||||
use_offscreen_dict.Get(options::kUseSharedTexture,
|
||||
&offscreen_use_shared_texture_);
|
||||
}
|
||||
}
|
||||
|
||||
// Init embedder earlier
|
||||
options.Get("embedder", &embedder_);
|
||||
|
@ -798,7 +813,7 @@ WebContents::WebContents(v8::Isolate* isolate,
|
|||
|
||||
if (embedder_ && embedder_->IsOffScreen()) {
|
||||
auto* view = new OffScreenWebContentsView(
|
||||
false,
|
||||
false, offscreen_use_shared_texture_,
|
||||
base::BindRepeating(&WebContents::OnPaint, base::Unretained(this)));
|
||||
params.view = view;
|
||||
params.delegate_view = view;
|
||||
|
@ -818,7 +833,7 @@ WebContents::WebContents(v8::Isolate* isolate,
|
|||
|
||||
content::WebContents::CreateParams params(session->browser_context());
|
||||
auto* view = new OffScreenWebContentsView(
|
||||
transparent,
|
||||
transparent, offscreen_use_shared_texture_,
|
||||
base::BindRepeating(&WebContents::OnPaint, base::Unretained(this)));
|
||||
params.view = view;
|
||||
params.delegate_view = view;
|
||||
|
@ -3547,8 +3562,23 @@ bool WebContents::IsOffScreen() const {
|
|||
return type_ == Type::kOffScreen;
|
||||
}
|
||||
|
||||
void WebContents::OnPaint(const gfx::Rect& dirty_rect, const SkBitmap& bitmap) {
|
||||
Emit("paint", dirty_rect, gfx::Image::CreateFrom1xBitmap(bitmap));
|
||||
void WebContents::OnPaint(const gfx::Rect& dirty_rect,
|
||||
const SkBitmap& bitmap,
|
||||
const OffscreenSharedTexture& tex) {
|
||||
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
|
||||
gin::Handle<gin_helper::internal::Event> event =
|
||||
gin_helper::internal::Event::New(isolate);
|
||||
v8::Local<v8::Object> event_object = event.ToV8().As<v8::Object>();
|
||||
gin_helper::Dictionary dict(isolate, event_object);
|
||||
|
||||
if (offscreen_use_shared_texture_) {
|
||||
dict.Set("texture", tex);
|
||||
}
|
||||
|
||||
EmitWithoutEvent("paint", event, dirty_rect,
|
||||
gfx::Image::CreateFrom1xBitmap(bitmap));
|
||||
}
|
||||
|
||||
void WebContents::StartPainting() {
|
||||
|
|
|
@ -39,6 +39,7 @@
|
|||
#include "shell/browser/background_throttling_source.h"
|
||||
#include "shell/browser/event_emitter_mixin.h"
|
||||
#include "shell/browser/extended_web_contents_observer.h"
|
||||
#include "shell/browser/osr/osr_paint_event.h"
|
||||
#include "shell/browser/ui/inspectable_web_contents_delegate.h"
|
||||
#include "shell/browser/ui/inspectable_web_contents_view_delegate.h"
|
||||
#include "shell/common/gin_helper/cleaned_up_at_exit.h"
|
||||
|
@ -301,7 +302,9 @@ class WebContents final : public ExclusiveAccessContext,
|
|||
|
||||
// Methods for offscreen rendering
|
||||
bool IsOffScreen() const;
|
||||
void OnPaint(const gfx::Rect& dirty_rect, const SkBitmap& bitmap);
|
||||
void OnPaint(const gfx::Rect& dirty_rect,
|
||||
const SkBitmap& bitmap,
|
||||
const OffscreenSharedTexture& info);
|
||||
void StartPainting();
|
||||
void StopPainting();
|
||||
bool IsPainting() const;
|
||||
|
@ -834,6 +837,9 @@ class WebContents final : public ExclusiveAccessContext,
|
|||
|
||||
bool offscreen_ = false;
|
||||
|
||||
// Whether offscreen rendering use gpu shared texture
|
||||
bool offscreen_use_shared_texture_ = false;
|
||||
|
||||
// Whether window is fullscreened by HTML5 api.
|
||||
bool html_fullscreen_ = false;
|
||||
|
||||
|
|
|
@ -69,7 +69,7 @@ void LayeredWindowUpdater::Draw(const gfx::Rect& damage_rect,
|
|||
|
||||
if (active_ && canvas_->peekPixels(&pixmap)) {
|
||||
bitmap.installPixels(pixmap);
|
||||
callback_.Run(damage_rect, bitmap);
|
||||
callback_.Run(damage_rect, bitmap, {});
|
||||
}
|
||||
|
||||
std::move(draw_callback).Run();
|
||||
|
|
|
@ -11,15 +11,16 @@
|
|||
#include "base/memory/shared_memory_mapping.h"
|
||||
#include "components/viz/host/host_display_client.h"
|
||||
#include "services/viz/privileged/mojom/compositing/layered_window_updater.mojom.h"
|
||||
#include "shell/browser/osr/osr_paint_event.h"
|
||||
#include "third_party/skia/include/core/SkBitmap.h"
|
||||
#include "third_party/skia/include/core/SkCanvas.h"
|
||||
#include "ui/gfx/native_widget_types.h"
|
||||
|
||||
class SkBitmap;
|
||||
class SkCanvas;
|
||||
|
||||
namespace electron {
|
||||
|
||||
typedef base::RepeatingCallback<void(const gfx::Rect&, const SkBitmap&)>
|
||||
OnPaintCallback;
|
||||
|
||||
class LayeredWindowUpdater : public viz::mojom::LayeredWindowUpdater {
|
||||
public:
|
||||
explicit LayeredWindowUpdater(
|
||||
|
|
|
@ -32,7 +32,7 @@ void OffScreenHostDisplayClient::OnDisplayReceivedCALayerParams(
|
|||
kPremul_SkAlphaType),
|
||||
pixels, stride);
|
||||
bitmap.setImmutable();
|
||||
callback_.Run(ca_layer_params.damage, bitmap);
|
||||
callback_.Run(ca_layer_params.damage, bitmap, {});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
31
shell/browser/osr/osr_paint_event.cc
Normal file
31
shell/browser/osr/osr_paint_event.cc
Normal file
|
@ -0,0 +1,31 @@
|
|||
|
||||
// Copyright (c) 2024 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be found in
|
||||
// the LICENSE file.
|
||||
|
||||
#include "shell/browser/osr/osr_paint_event.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
OffscreenNativePixmapPlaneInfo::~OffscreenNativePixmapPlaneInfo() = default;
|
||||
OffscreenNativePixmapPlaneInfo::OffscreenNativePixmapPlaneInfo(
|
||||
const OffscreenNativePixmapPlaneInfo& other) = default;
|
||||
OffscreenNativePixmapPlaneInfo::OffscreenNativePixmapPlaneInfo(uint32_t stride,
|
||||
uint64_t offset,
|
||||
uint64_t size,
|
||||
int fd)
|
||||
: stride(stride), offset(offset), size(size), fd(fd) {}
|
||||
|
||||
OffscreenReleaserHolder::~OffscreenReleaserHolder() = default;
|
||||
OffscreenReleaserHolder::OffscreenReleaserHolder(
|
||||
gfx::GpuMemoryBufferHandle gmb_handle,
|
||||
mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
|
||||
releaser)
|
||||
: gmb_handle(std::move(gmb_handle)), releaser(std::move(releaser)) {}
|
||||
|
||||
OffscreenSharedTextureValue::OffscreenSharedTextureValue() = default;
|
||||
OffscreenSharedTextureValue::~OffscreenSharedTextureValue() = default;
|
||||
OffscreenSharedTextureValue::OffscreenSharedTextureValue(
|
||||
const OffscreenSharedTextureValue& other) = default;
|
||||
|
||||
} // namespace electron
|
113
shell/browser/osr/osr_paint_event.h
Normal file
113
shell/browser/osr/osr_paint_event.h
Normal file
|
@ -0,0 +1,113 @@
|
|||
|
||||
// Copyright (c) 2024 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be found in
|
||||
// the LICENSE file.
|
||||
|
||||
#ifndef ELECTRON_SHELL_BROWSER_OSR_OSR_PAINT_EVENT_H
|
||||
#define ELECTRON_SHELL_BROWSER_OSR_OSR_PAINT_EVENT_H
|
||||
|
||||
#include "base/functional/callback_helpers.h"
|
||||
#include "content/public/common/widget_type.h"
|
||||
#include "media/base/video_types.h"
|
||||
#include "mojo/public/cpp/bindings/pending_remote.h"
|
||||
#include "services/viz/privileged/mojom/compositing/frame_sink_video_capture.mojom.h"
|
||||
#include "third_party/skia/include/core/SkCanvas.h"
|
||||
#include "ui/gfx/canvas.h"
|
||||
#include "ui/gfx/native_widget_types.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace electron {
|
||||
|
||||
struct OffscreenNativePixmapPlaneInfo {
|
||||
// The strides and offsets in bytes to be used when accessing the buffers
|
||||
// via a memory mapping. One per plane per entry. Size in bytes of the
|
||||
// plane is necessary to map the buffers.
|
||||
uint32_t stride;
|
||||
uint64_t offset;
|
||||
uint64_t size;
|
||||
|
||||
// File descriptor for the underlying memory object (usually dmabuf).
|
||||
int fd;
|
||||
|
||||
OffscreenNativePixmapPlaneInfo() = delete;
|
||||
~OffscreenNativePixmapPlaneInfo();
|
||||
OffscreenNativePixmapPlaneInfo(const OffscreenNativePixmapPlaneInfo& other);
|
||||
OffscreenNativePixmapPlaneInfo(uint32_t stride,
|
||||
uint64_t offset,
|
||||
uint64_t size,
|
||||
int fd);
|
||||
};
|
||||
|
||||
struct OffscreenReleaserHolder {
|
||||
OffscreenReleaserHolder() = delete;
|
||||
~OffscreenReleaserHolder();
|
||||
OffscreenReleaserHolder(
|
||||
gfx::GpuMemoryBufferHandle gmb_handle,
|
||||
mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
|
||||
releaser);
|
||||
|
||||
// GpuMemoryBufferHandle, keep the scoped handle alive
|
||||
gfx::GpuMemoryBufferHandle gmb_handle;
|
||||
|
||||
// Releaser, hold this to prevent FrameSinkVideoCapturer recycle frame
|
||||
mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
|
||||
releaser;
|
||||
};
|
||||
|
||||
struct OffscreenSharedTextureValue {
|
||||
OffscreenSharedTextureValue();
|
||||
~OffscreenSharedTextureValue();
|
||||
OffscreenSharedTextureValue(const OffscreenSharedTextureValue& other);
|
||||
|
||||
// It is user's responsibility to compose popup widget textures.
|
||||
content::WidgetType widget_type;
|
||||
|
||||
// The pixel format of the shared texture, RGBA or BGRA depends on platform.
|
||||
media::VideoPixelFormat pixel_format;
|
||||
|
||||
// The full dimensions of the video frame data.
|
||||
gfx::Size coded_size;
|
||||
|
||||
// A subsection of [0, 0, coded_size().width(), coded_size.height()].
|
||||
// In OSR case, it is expected to have the full area of the section.
|
||||
gfx::Rect visible_rect;
|
||||
|
||||
// The region of the video frame that capturer would like to populate.
|
||||
// In OSR case, it is the same with `dirtyRect` that needs to be painted.
|
||||
gfx::Rect content_rect;
|
||||
|
||||
// Extra metadata for the video frame.
|
||||
// See comments in src\media\base\video_frame_metadata.h for more details.
|
||||
std::optional<gfx::Rect> capture_update_rect;
|
||||
std::optional<gfx::Size> source_size;
|
||||
std::optional<gfx::Rect> region_capture_rect;
|
||||
|
||||
// The capture timestamp, microseconds since capture start
|
||||
int64_t timestamp;
|
||||
|
||||
// The frame count
|
||||
int64_t frame_count;
|
||||
|
||||
// Releaser holder
|
||||
raw_ptr<OffscreenReleaserHolder> releaser_holder;
|
||||
|
||||
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
|
||||
// On Windows it is a HANDLE to the shared D3D11 texture.
|
||||
// On macOS it is a IOSurface* to the shared IOSurface.
|
||||
uintptr_t shared_texture_handle;
|
||||
#elif BUILDFLAG(IS_LINUX)
|
||||
std::vector<OffscreenNativePixmapPlaneInfo> planes;
|
||||
uint64_t modifier;
|
||||
#endif
|
||||
};
|
||||
|
||||
typedef std::optional<OffscreenSharedTextureValue> OffscreenSharedTexture;
|
||||
|
||||
typedef base::RepeatingCallback<
|
||||
void(const gfx::Rect&, const SkBitmap&, const OffscreenSharedTexture&)>
|
||||
OnPaintCallback;
|
||||
|
||||
} // namespace electron
|
||||
|
||||
#endif // ELECTRON_SHELL_BROWSER_OSR_OSR_PAINT_EVENT_H
|
|
@ -177,6 +177,7 @@ class ElectronDelegatedFrameHostClient
|
|||
|
||||
OffScreenRenderWidgetHostView::OffScreenRenderWidgetHostView(
|
||||
bool transparent,
|
||||
bool offscreen_use_shared_texture,
|
||||
bool painting,
|
||||
int frame_rate,
|
||||
const OnPaintCallback& callback,
|
||||
|
@ -187,6 +188,7 @@ OffScreenRenderWidgetHostView::OffScreenRenderWidgetHostView(
|
|||
render_widget_host_(content::RenderWidgetHostImpl::From(host)),
|
||||
parent_host_view_(parent_host_view),
|
||||
transparent_(transparent),
|
||||
offscreen_use_shared_texture_(offscreen_use_shared_texture),
|
||||
callback_(callback),
|
||||
frame_rate_(frame_rate),
|
||||
size_(initial_size),
|
||||
|
@ -544,8 +546,9 @@ OffScreenRenderWidgetHostView::CreateViewForWidget(
|
|||
}
|
||||
|
||||
return new OffScreenRenderWidgetHostView(
|
||||
transparent_, true, embedder_host_view->frame_rate(), callback_,
|
||||
render_widget_host, embedder_host_view, size());
|
||||
transparent_, offscreen_use_shared_texture_, true,
|
||||
embedder_host_view->frame_rate(), callback_, render_widget_host,
|
||||
embedder_host_view, size());
|
||||
}
|
||||
|
||||
const viz::FrameSinkId& OffScreenRenderWidgetHostView::GetFrameSinkId() const {
|
||||
|
@ -654,8 +657,15 @@ uint64_t OffScreenRenderWidgetHostView::GetNSViewId() const {
|
|||
}
|
||||
#endif
|
||||
|
||||
void OffScreenRenderWidgetHostView::OnPaint(const gfx::Rect& damage_rect,
|
||||
const SkBitmap& bitmap) {
|
||||
void OffScreenRenderWidgetHostView::OnPaint(
|
||||
const gfx::Rect& damage_rect,
|
||||
const SkBitmap& bitmap,
|
||||
const OffscreenSharedTexture& texture) {
|
||||
if (texture.has_value()) {
|
||||
callback_.Run(damage_rect, {}, texture);
|
||||
return;
|
||||
}
|
||||
|
||||
backing_ = std::make_unique<SkBitmap>();
|
||||
backing_->allocN32Pixels(bitmap.width(), bitmap.height(), !transparent_);
|
||||
bitmap.readPixels(backing_->pixmap());
|
||||
|
@ -711,7 +721,7 @@ void OffScreenRenderWidgetHostView::CompositeFrame(
|
|||
}
|
||||
|
||||
callback_.Run(gfx::IntersectRects(gfx::Rect(size_in_pixels), damage_rect),
|
||||
frame);
|
||||
frame, {});
|
||||
|
||||
ReleaseResize();
|
||||
}
|
||||
|
|
|
@ -25,14 +25,17 @@
|
|||
#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 "shell/browser/osr/osr_host_display_client.h"
|
||||
#include "shell/browser/osr/osr_video_consumer.h"
|
||||
#include "shell/browser/osr/osr_view_proxy.h"
|
||||
#include "third_party/blink/public/mojom/widget/record_content_to_visible_time_request.mojom-forward.h"
|
||||
#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"
|
||||
|
||||
|
@ -59,8 +62,6 @@ class ElectronCopyFrameGenerator;
|
|||
class ElectronDelegatedFrameHostClient;
|
||||
class OffScreenHostDisplayClient;
|
||||
|
||||
using OnPaintCallback =
|
||||
base::RepeatingCallback<void(const gfx::Rect&, const SkBitmap&)>;
|
||||
using OnPopupPaintCallback = base::RepeatingCallback<void(const gfx::Rect&)>;
|
||||
|
||||
class OffScreenRenderWidgetHostView
|
||||
|
@ -70,6 +71,7 @@ class OffScreenRenderWidgetHostView
|
|||
private OffscreenViewProxyObserver {
|
||||
public:
|
||||
OffScreenRenderWidgetHostView(bool transparent,
|
||||
bool offscreen_use_shared_texture,
|
||||
bool painting,
|
||||
int frame_rate,
|
||||
const OnPaintCallback& callback,
|
||||
|
@ -204,7 +206,9 @@ class OffScreenRenderWidgetHostView
|
|||
void RemoveViewProxy(OffscreenViewProxy* proxy);
|
||||
void ProxyViewDestroyed(OffscreenViewProxy* proxy) override;
|
||||
|
||||
void OnPaint(const gfx::Rect& damage_rect, const SkBitmap& bitmap);
|
||||
void OnPaint(const gfx::Rect& damage_rect,
|
||||
const SkBitmap& bitmap,
|
||||
const OffscreenSharedTexture& texture);
|
||||
void OnPopupPaint(const gfx::Rect& damage_rect);
|
||||
void OnProxyViewPaint(const gfx::Rect& damage_rect) override;
|
||||
|
||||
|
@ -231,6 +235,10 @@ class OffScreenRenderWidgetHostView
|
|||
void SetFrameRate(int frame_rate);
|
||||
int frame_rate() const { return frame_rate_; }
|
||||
|
||||
bool offscreen_use_shared_texture() const {
|
||||
return offscreen_use_shared_texture_;
|
||||
}
|
||||
|
||||
ui::Layer* root_layer() const { return root_layer_.get(); }
|
||||
|
||||
content::DelegatedFrameHost* delegated_frame_host() const {
|
||||
|
@ -274,6 +282,7 @@ class OffScreenRenderWidgetHostView
|
|||
std::set<OffscreenViewProxy*> proxy_views_;
|
||||
|
||||
const bool transparent_;
|
||||
const bool offscreen_use_shared_texture_;
|
||||
OnPaintCallback callback_;
|
||||
OnPopupPaintCallback parent_callback_;
|
||||
|
||||
|
|
|
@ -16,21 +16,6 @@
|
|||
#include "third_party/skia/include/core/SkRegion.h"
|
||||
#include "ui/gfx/skbitmap_operations.h"
|
||||
|
||||
namespace {
|
||||
|
||||
bool IsValidMinAndMaxFrameSize(gfx::Size min_frame_size,
|
||||
gfx::Size max_frame_size) {
|
||||
// Returns true if
|
||||
// 0 < |min_frame_size| <= |max_frame_size| <= media::limits::kMaxDimension.
|
||||
return 0 < min_frame_size.width() && 0 < min_frame_size.height() &&
|
||||
min_frame_size.width() <= max_frame_size.width() &&
|
||||
min_frame_size.height() <= max_frame_size.height() &&
|
||||
max_frame_size.width() <= media::limits::kMaxDimension &&
|
||||
max_frame_size.height() <= media::limits::kMaxDimension;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace electron {
|
||||
|
||||
OffScreenVideoConsumer::OffScreenVideoConsumer(
|
||||
|
@ -43,7 +28,23 @@ OffScreenVideoConsumer::OffScreenVideoConsumer(
|
|||
video_capturer_->SetMinSizeChangePeriod(base::TimeDelta());
|
||||
video_capturer_->SetFormat(media::PIXEL_FORMAT_ARGB);
|
||||
|
||||
SizeChanged(view_->SizeInPixels());
|
||||
// Previous design of OSR try to set the resolution constraint to match the
|
||||
// view's size. It is actually not necessary and creates faulty textures
|
||||
// when the window/view's size changes frequently. The constraint may not
|
||||
// take effect before the internal frame size changes, and makes the capturer
|
||||
// try to resize the new frame size to the old constraint size, which makes
|
||||
// the output image blurry. (For example, small window suddenly expands to
|
||||
// maximum size, will actually produce a small output image with maximized
|
||||
// window resized to fit the small image, however, the expected output is
|
||||
// a maximized image without resizing).
|
||||
// So, we just set the constraint to no limit (1x1 to max). When the window
|
||||
// size changed, a new frame with new size will be automatically generated.
|
||||
// There's no need to manually set the constraint and request a new frame.
|
||||
video_capturer_->SetResolutionConstraints(
|
||||
gfx::Size(1, 1),
|
||||
gfx::Size(media::limits::kMaxDimension, media::limits::kMaxDimension),
|
||||
false);
|
||||
|
||||
SetFrameRate(view_->frame_rate());
|
||||
}
|
||||
|
||||
|
@ -51,7 +52,10 @@ OffScreenVideoConsumer::~OffScreenVideoConsumer() = default;
|
|||
|
||||
void OffScreenVideoConsumer::SetActive(bool active) {
|
||||
if (active) {
|
||||
video_capturer_->Start(this, viz::mojom::BufferFormatPreference::kDefault);
|
||||
video_capturer_->Start(
|
||||
this, view_->offscreen_use_shared_texture()
|
||||
? viz::mojom::BufferFormatPreference::kPreferGpuMemoryBuffer
|
||||
: viz::mojom::BufferFormatPreference::kDefault);
|
||||
} else {
|
||||
video_capturer_->Stop();
|
||||
}
|
||||
|
@ -61,43 +65,77 @@ void OffScreenVideoConsumer::SetFrameRate(int frame_rate) {
|
|||
video_capturer_->SetMinCapturePeriod(base::Seconds(1) / frame_rate);
|
||||
}
|
||||
|
||||
void OffScreenVideoConsumer::SizeChanged(const gfx::Size& size_in_pixels) {
|
||||
DCHECK(IsValidMinAndMaxFrameSize(size_in_pixels, size_in_pixels));
|
||||
video_capturer_->SetResolutionConstraints(size_in_pixels, size_in_pixels,
|
||||
true);
|
||||
video_capturer_->RequestRefreshFrame();
|
||||
}
|
||||
|
||||
void OffScreenVideoConsumer::OnFrameCaptured(
|
||||
::media::mojom::VideoBufferHandlePtr data,
|
||||
::media::mojom::VideoFrameInfoPtr info,
|
||||
const gfx::Rect& content_rect,
|
||||
mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
|
||||
callbacks) {
|
||||
auto& data_region = data->get_read_only_shmem_region();
|
||||
// Since we don't call ProvideFeedback, just need Done to release the frame,
|
||||
// there's no need to call the callbacks, see in_flight_frame_delivery.cc
|
||||
// The destructor will call Done for us once the pipe closed.
|
||||
|
||||
if (!CheckContentRect(content_rect)) {
|
||||
SizeChanged(view_->SizeInPixels());
|
||||
// Offscreen using GPU shared texture
|
||||
if (view_->offscreen_use_shared_texture()) {
|
||||
CHECK(data->is_gpu_memory_buffer_handle());
|
||||
|
||||
auto& orig_handle = data->get_gpu_memory_buffer_handle();
|
||||
CHECK(!orig_handle.is_null());
|
||||
|
||||
// Clone the handle to support keep the handle alive after the callback
|
||||
auto gmb_handle = orig_handle.Clone();
|
||||
|
||||
OffscreenSharedTextureValue texture;
|
||||
texture.pixel_format = info->pixel_format;
|
||||
texture.coded_size = info->coded_size;
|
||||
texture.visible_rect = info->visible_rect;
|
||||
texture.content_rect = content_rect;
|
||||
texture.timestamp = info->timestamp.InMicroseconds();
|
||||
texture.frame_count = info->metadata.capture_counter.value_or(0);
|
||||
texture.capture_update_rect = info->metadata.capture_update_rect;
|
||||
texture.source_size = info->metadata.source_size;
|
||||
texture.region_capture_rect = info->metadata.region_capture_rect;
|
||||
texture.widget_type = view_->GetWidgetType();
|
||||
|
||||
#if BUILDFLAG(IS_WIN)
|
||||
texture.shared_texture_handle =
|
||||
reinterpret_cast<uintptr_t>(gmb_handle.dxgi_handle.Get());
|
||||
#elif BUILDFLAG(IS_APPLE)
|
||||
texture.shared_texture_handle =
|
||||
reinterpret_cast<uintptr_t>(gmb_handle.io_surface.get());
|
||||
#elif BUILDFLAG(IS_LINUX)
|
||||
const auto& native_pixmap = gmb_handle.native_pixmap_handle;
|
||||
texture.modifier = native_pixmap.modifier;
|
||||
for (const auto& plane : native_pixmap.planes) {
|
||||
texture.planes.emplace_back(plane.stride, plane.offset, plane.size,
|
||||
plane.fd.get());
|
||||
}
|
||||
#endif
|
||||
|
||||
// The release holder will be released from JS side when `release` called
|
||||
texture.releaser_holder = new OffscreenReleaserHolder(std::move(gmb_handle),
|
||||
std::move(callbacks));
|
||||
|
||||
callback_.Run(content_rect, {}, std::move(texture));
|
||||
return;
|
||||
}
|
||||
|
||||
mojo::Remote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
|
||||
callbacks_remote(std::move(callbacks));
|
||||
// Regular shared texture capture using shared memory
|
||||
const auto& data_region = data->get_read_only_shmem_region();
|
||||
|
||||
if (!data_region.IsValid()) {
|
||||
callbacks_remote->Done();
|
||||
return;
|
||||
}
|
||||
|
||||
base::ReadOnlySharedMemoryMapping mapping = data_region.Map();
|
||||
if (!mapping.IsValid()) {
|
||||
DLOG(ERROR) << "Shared memory mapping failed.";
|
||||
callbacks_remote->Done();
|
||||
return;
|
||||
}
|
||||
|
||||
if (mapping.size() <
|
||||
media::VideoFrame::AllocationSize(info->pixel_format, info->coded_size)) {
|
||||
DLOG(ERROR) << "Shared memory size was less than expected.";
|
||||
callbacks_remote->Done();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -127,30 +165,17 @@ void OffScreenVideoConsumer::OnFrameCaptured(
|
|||
[](void* addr, void* context) {
|
||||
delete static_cast<FramePinner*>(context);
|
||||
},
|
||||
new FramePinner{std::move(mapping), callbacks_remote.Unbind()});
|
||||
new FramePinner{std::move(mapping), std::move(callbacks)});
|
||||
bitmap.setImmutable();
|
||||
|
||||
// Since update_rect is already offset-ed with same origin of content_rect,
|
||||
// there's nothing more to do with the imported bitmap.
|
||||
std::optional<gfx::Rect> update_rect = info->metadata.capture_update_rect;
|
||||
if (!update_rect.has_value() || update_rect->IsEmpty()) {
|
||||
update_rect = content_rect;
|
||||
}
|
||||
|
||||
callback_.Run(*update_rect, bitmap);
|
||||
}
|
||||
|
||||
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;
|
||||
callback_.Run(*update_rect, bitmap, {});
|
||||
}
|
||||
|
||||
} // namespace electron
|
||||
|
|
|
@ -14,14 +14,12 @@
|
|||
#include "components/viz/host/client_frame_sink_video_capturer.h"
|
||||
#include "media/capture/mojom/video_capture_buffer.mojom-forward.h"
|
||||
#include "media/capture/mojom/video_capture_types.mojom.h"
|
||||
#include "shell/browser/osr/osr_paint_event.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
class OffScreenRenderWidgetHostView;
|
||||
|
||||
typedef base::RepeatingCallback<void(const gfx::Rect&, const SkBitmap&)>
|
||||
OnPaintCallback;
|
||||
|
||||
class OffScreenVideoConsumer : public viz::mojom::FrameSinkVideoConsumer {
|
||||
public:
|
||||
OffScreenVideoConsumer(OffScreenRenderWidgetHostView* view,
|
||||
|
@ -34,7 +32,6 @@ class OffScreenVideoConsumer : public viz::mojom::FrameSinkVideoConsumer {
|
|||
|
||||
void SetActive(bool active);
|
||||
void SetFrameRate(int frame_rate);
|
||||
void SizeChanged(const gfx::Size& size_in_pixels);
|
||||
|
||||
private:
|
||||
// viz::mojom::FrameSinkVideoConsumer implementation.
|
||||
|
@ -49,8 +46,6 @@ class OffScreenVideoConsumer : public viz::mojom::FrameSinkVideoConsumer {
|
|||
void OnStopped() override {}
|
||||
void OnLog(const std::string& message) override {}
|
||||
|
||||
bool CheckContentRect(const gfx::Rect& content_rect);
|
||||
|
||||
OnPaintCallback callback_;
|
||||
|
||||
raw_ptr<OffScreenRenderWidgetHostView> view_;
|
||||
|
|
|
@ -15,8 +15,11 @@ namespace electron {
|
|||
|
||||
OffScreenWebContentsView::OffScreenWebContentsView(
|
||||
bool transparent,
|
||||
bool offscreen_use_shared_texture,
|
||||
const OnPaintCallback& callback)
|
||||
: transparent_(transparent), callback_(callback) {
|
||||
: transparent_(transparent),
|
||||
offscreen_use_shared_texture_(offscreen_use_shared_texture),
|
||||
callback_(callback) {
|
||||
#if BUILDFLAG(IS_MAC)
|
||||
PlatformCreate();
|
||||
#endif
|
||||
|
@ -109,8 +112,8 @@ OffScreenWebContentsView::CreateViewForWidget(
|
|||
return static_cast<content::RenderWidgetHostViewBase*>(rwhv);
|
||||
|
||||
return new OffScreenRenderWidgetHostView(
|
||||
transparent_, painting_, GetFrameRate(), callback_, render_widget_host,
|
||||
nullptr, GetSize());
|
||||
transparent_, offscreen_use_shared_texture_, painting_, GetFrameRate(),
|
||||
callback_, render_widget_host, nullptr, GetSize());
|
||||
}
|
||||
|
||||
content::RenderWidgetHostViewBase*
|
||||
|
@ -124,9 +127,9 @@ OffScreenWebContentsView::CreateViewForChildWidget(
|
|||
? web_contents_impl->GetOuterWebContents()->GetRenderWidgetHostView()
|
||||
: web_contents_impl->GetRenderWidgetHostView());
|
||||
|
||||
return new OffScreenRenderWidgetHostView(transparent_, painting_,
|
||||
view->frame_rate(), callback_,
|
||||
render_widget_host, view, GetSize());
|
||||
return new OffScreenRenderWidgetHostView(
|
||||
transparent_, offscreen_use_shared_texture_, painting_,
|
||||
view->frame_rate(), callback_, render_widget_host, view, GetSize());
|
||||
}
|
||||
|
||||
void OffScreenWebContentsView::RenderViewReady() {
|
||||
|
|
|
@ -34,7 +34,9 @@ class OffScreenWebContentsView : public content::WebContentsView,
|
|||
public content::RenderViewHostDelegateView,
|
||||
private NativeWindowObserver {
|
||||
public:
|
||||
OffScreenWebContentsView(bool transparent, const OnPaintCallback& callback);
|
||||
OffScreenWebContentsView(bool transparent,
|
||||
bool offscreen_use_shared_texture,
|
||||
const OnPaintCallback& callback);
|
||||
~OffScreenWebContentsView() override;
|
||||
|
||||
void SetWebContents(content::WebContents*);
|
||||
|
@ -105,6 +107,7 @@ class OffScreenWebContentsView : public content::WebContentsView,
|
|||
raw_ptr<NativeWindow> native_window_ = nullptr;
|
||||
|
||||
const bool transparent_;
|
||||
const bool offscreen_use_shared_texture_;
|
||||
bool painting_ = true;
|
||||
int frame_rate_ = 60;
|
||||
OnPaintCallback callback_;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue