Skip to content

Commit

Permalink
fix: use scoped release to trigger release callback
Browse files Browse the repository at this point in the history
  • Loading branch information
reitowo committed May 2, 2024
1 parent 7e4d1a9 commit 08563b3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/api/web-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ Returns:
* `event` Event
* `dirtyRect` [Rectangle](structures/rectangle.md)
* `image` [NativeImage](native-image.md) - The image data of the whole frame.
* `texture` [OffscreenSharedTexture](structures/offscreen-shared-texture.md) - The GPU shared texture of the frame. `null` if `webPreferences.offscreenUseSharedTexture` is `false`.
* `texture` [OffscreenSharedTexture](structures/offscreen-shared-texture.md) - The GPU shared texture of the frame. `null` if `webPreferences.offscreenUseSharedTexture` is `false`. This is only valid before return, avoid use in promises and async functions.

Emitted when a new frame is generated. Only the dirty area is passed in the
buffer.
Expand Down
31 changes: 24 additions & 7 deletions shell/browser/osr/osr_video_consumer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ bool IsValidMinAndMaxFrameSize(gfx::Size min_frame_size,
max_frame_size.height() <= media::limits::kMaxDimension;
}

class ScopedVideoFrameDone {
public:
explicit ScopedVideoFrameDone(
mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
callbacks)
: callbacks_(std::move(callbacks)) {}
~ScopedVideoFrameDone() {
if (callbacks_)
callbacks_->Done();
}

mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
Unbind() {
return callbacks_.Unbind();
}

private:
mojo::Remote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks> callbacks_;
};

} // namespace

namespace electron {
Expand Down Expand Up @@ -75,9 +95,12 @@ void OffScreenVideoConsumer::OnFrameCaptured(
const gfx::Rect& content_rect,
mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
callbacks) {
ScopedVideoFrameDone scoped_done(std::move(callbacks));

// Offscreen using GPU shared texture
if (view_->offscreen_use_shared_texture()) {
CHECK(data->is_gpu_memory_buffer_handle());

auto& gmb_handle = data->get_gpu_memory_buffer_handle();
OffscreenSharedTextureValue texture;
texture.pixel_format = info->pixel_format;
Expand Down Expand Up @@ -108,23 +131,17 @@ void OffScreenVideoConsumer::OnFrameCaptured(
return;
}

mojo::Remote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
callbacks_remote(std::move(callbacks));

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;
}

Expand Down Expand Up @@ -154,7 +171,7 @@ 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), scoped_done.Unbind()});
bitmap.setImmutable();

std::optional<gfx::Rect> update_rect = info->metadata.capture_update_rect;
Expand Down

0 comments on commit 08563b3

Please sign in to comment.