Skip to content

Truly independent web browser

Compositor: Repaint only the videos whose frame changed

Aliaksandr Kalenik708269764929
diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cpp
index a4b9eaa699e..6d460575cf0 100644
--- a/Services/Compositor/CompositorState.cpp
+++ b/Services/Compositor/CompositorState.cpp
@@ -349,7 +349,7 @@ void CompositorState::present_contexts_drawing_video_sink(CompositorStateWebCont
for (auto const& resource_entry : context.video_sink_handles()) {
if (resource_entry.value == handle) {
if (auto rect = context.presenter().self_present_rect(); rect.has_value())
- schedule_present_frame(context_entry.key, context, *rect);
+ schedule_present_frame(context_entry.key, context, PendingFrame::repainting_changes(*rect));
break;
}
}
diff --git a/Services/Compositor/ContextPresenter.cpp b/Services/Compositor/ContextPresenter.cpp
index 5f653bdaa10..1c12ede2e6c 100644
--- a/Services/Compositor/ContextPresenter.cpp
+++ b/Services/Compositor/ContextPresenter.cpp
@@ -15,6 +15,8 @@
#include <LibCore/Timer.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/SkiaUtils.h>
+#include <LibMedia/Sinks/VideoSink.h>
+#include <LibMedia/VideoFrame.h>
#include <core/SkCanvas.h>
#include <core/SkImage.h>
#include <math.h>
@@ -446,6 +448,17 @@ Gfx::IntRect ContextPresenter::damage_since_last_raster(Gfx::IntSize viewport_si
if (m_canvas_surfaces.canvas_content_generation(canvas_id) != content_generation)
canvases_with_new_content.append(canvas_id);
}
+ Vector<Compositing::VideoSinkResourceId> video_sinks_with_new_frames;
+ auto video_sinks_drawn_by_list = m_scene.display_list()->referenced_resource_ids().video_sinks;
+ for (auto const& [video_sink_id, frame_slot] : last_frame.video_frame_slots) {
+ if (current_video_frame_slot(video_sink_id) == frame_slot)
+ continue;
+ // A video drawn only by a nested display list, such as an SVG image's, has no command in
+ // this list to damage.
+ if (!video_sinks_drawn_by_list.contains_slow(video_sink_id.value()))
+ return viewport_rect;
+ video_sinks_with_new_frames.append(video_sink_id);
+ }
auto display_list_damage = Compositing::compute_display_list_damage(
*last_frame.display_list,
last_frame.visual_context_tree,
@@ -454,7 +467,7 @@ Gfx::IntRect ContextPresenter::damage_since_last_raster(Gfx::IntSize viewport_si
m_scene.visual_context_tree_for_compositing(),
presented_scroll_state_snapshot,
viewport_rect,
- { .canvases = canvases_with_new_content, .video_sinks = {} });
+ { .canvases = canvases_with_new_content, .video_sinks = video_sinks_with_new_frames });
if (!display_list_damage.has_value())
return viewport_rect;
@@ -477,12 +490,35 @@ void ContextPresenter::remember_rasterized_frame(Gfx::IntSize viewport_size, Com
Compositing::CanvasId canvas_id { id };
canvas_content_generations.set(canvas_id, m_canvas_surfaces.canvas_content_generation(canvas_id));
}
+ // Every video the context holds is drawn by the list or by a display list it nests.
+ HashMap<Compositing::VideoSinkResourceId, Optional<VideoFrameSlot>> video_frame_slots;
+ for (auto const& [id, handle] : m_scene.video_sink_handles()) {
+ Compositing::VideoSinkResourceId video_sink_id { id };
+ video_frame_slots.set(video_sink_id, current_video_frame_slot(video_sink_id));
+ }
m_last_rasterized_frame = RasterizedFrame {
.display_list = NonnullRefPtr { *m_scene.display_list() },
.visual_context_tree = m_scene.visual_context_tree_for_compositing(),
.scroll_state_snapshot = presented_scroll_state_snapshot,
.viewport_size = viewport_size,
.canvas_content_generations = move(canvas_content_generations),
+ .video_frame_slots = move(video_frame_slots),
+ };
+}
+
+Optional<ContextPresenter::VideoFrameSlot> ContextPresenter::current_video_frame_slot(Compositing::VideoSinkResourceId video_sink_id)
+{
+ auto sink = m_scene.resource_storage().video_sink(video_sink_id);
+ if (!sink)
+ return {};
+ auto frame = sink->current_frame();
+ if (!frame)
+ return {};
+ auto handle = Media::VideoFrameHandle::for_frame(*frame);
+ return VideoFrameSlot {
+ .pool_id = handle.pool_id,
+ .slot_index = handle.slot_index,
+ .slot_acquisition_id = handle.slot_acquisition_id,
};
}
diff --git a/Services/Compositor/ContextPresenter.h b/Services/Compositor/ContextPresenter.h
index 8baded0bccb..772b07dd8a6 100644
--- a/Services/Compositor/ContextPresenter.h
+++ b/Services/Compositor/ContextPresenter.h
@@ -29,6 +29,7 @@
#include <LibGfx/Rect.h>
#include <LibGfx/ShareableBitmap.h>
#include <LibGfx/Size.h>
+#include <LibMedia/VideoFrameHandle.h>
namespace Gfx {
@@ -121,12 +122,23 @@ public:
void paint_screenshot(DisplayListPlayerSkia&, Gfx::ShareableBitmap&, FrameContent const&);
private:
+ // A video frame named by the pool slot acquisition it was decoded into, which is new for every
+ // frame even when frames share a surface.
+ struct VideoFrameSlot {
+ Media::VideoFramePoolID pool_id;
+ u32 slot_index { 0 };
+ u64 slot_acquisition_id { 0 };
+
+ bool operator==(VideoFrameSlot const&) const = default;
+ };
+
struct RasterizedFrame {
NonnullRefPtr<Compositing::DisplayList const> display_list;
Compositing::AccumulatedVisualContextTree visual_context_tree;
Compositing::ScrollStateSnapshot scroll_state_snapshot;
Gfx::IntSize viewport_size;
HashMap<Compositing::CanvasId, u64> canvas_content_generations;
+ HashMap<Compositing::VideoSinkResourceId, Optional<VideoFrameSlot>> video_frame_slots;
};
enum class PaintUIOverlay : u8 {
@@ -142,6 +154,7 @@ private:
Gfx::IntRect frame_damage_for(PendingFrame const&, FrameContent const&);
Gfx::IntRect damage_since_last_raster(Gfx::IntSize viewport_size, FrameContent const&);
void remember_rasterized_frame(Gfx::IntSize viewport_size, Compositing::ScrollStateSnapshot const& presented_scroll_state_snapshot);
+ Optional<VideoFrameSlot> current_video_frame_slot(Compositing::VideoSinkResourceId);
void stop_backing_store_shrink_timer();
void stop_surplus_backing_store_retirement_timer();