feat: GPU shared texture offscreen rendering (#42953)

* feat: GPU shared texture offscreen rendering

* docs: clarify texture infos that passed by the paint event.

* feat: make gpu osr spec test optional

* fix: osr image compare

* fix: remove duplicate test

* fix: update patch file

* fix: code review

* feat: expose more metadata

* feat: use better switch design

* feat: add warning when user forget to release the texture.

* fix: typo

* chore: update patch

* fix: update patch

* fix: update patch description

* fix: update docs

* fix: apply suggestions from code review

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* fix: apply suggested fixes

---------

Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
reito 2024-08-23 08:23:13 +08:00 committed by GitHub
parent b481966f02
commit 1aeca6fd0e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
34 changed files with 1009 additions and 102 deletions

View file

@ -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();

View file

@ -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(

View file

@ -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, {});
}
}

View 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

View 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

View file

@ -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();
}

View file

@ -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_;

View file

@ -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

View file

@ -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_;

View file

@ -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() {

View file

@ -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_;