Skip to content

Truly independent web browser

LibCompositing: Treat image frame pixels as live content

Andreas Kling520267a0ccaa
diff --git a/Libraries/LibCompositing/DisplayList/DisplayListDamage.cpp b/Libraries/LibCompositing/DisplayList/DisplayListDamage.cpp
index 5a22946f3e9..9b430b31294 100644
--- a/Libraries/LibCompositing/DisplayList/DisplayListDamage.cpp
+++ b/Libraries/LibCompositing/DisplayList/DisplayListDamage.cpp
@@ -31,6 +31,7 @@ Optional<Gfx::IntRect> compute_display_list_damage(
viewport_rect,
live_content_changes.canvases.data(), live_content_changes.canvases.size(),
live_content_changes.video_sinks.data(), live_content_changes.video_sinks.size(),
+ live_content_changes.image_frames.data(), live_content_changes.image_frames.size(),
live_content_changes.forced_damage_rect,
&damage_rect);
if (!damage_is_bounded)
diff --git a/Libraries/LibCompositing/DisplayList/DisplayListDamage.h b/Libraries/LibCompositing/DisplayList/DisplayListDamage.h
index accf4c2757b..9ebf11477b5 100644
--- a/Libraries/LibCompositing/DisplayList/DisplayListDamage.h
+++ b/Libraries/LibCompositing/DisplayList/DisplayListDamage.h
@@ -19,11 +19,12 @@ namespace Compositing {
class AccumulatedVisualContextTree;
class ScrollStateSnapshot;
-// The canvases and video sinks whose pixels changed since the old list was rasterized. The lists draw
-// them with commands that stay the same, so comparing the lists cannot see the change.
+// The canvases, video sinks and image frames whose pixels changed since the old list was rasterized. The lists
+// draw them with commands that stay the same, so comparing the lists cannot see the change.
struct LiveContentChanges {
ReadonlySpan<CanvasId> canvases;
ReadonlySpan<VideoSinkResourceId> video_sinks;
+ ReadonlySpan<ImageFrameResourceId> image_frames;
// What the compositor repaints whatever the lists hold, such as a blinking caret.
Gfx::IntRect forced_damage_rect;
};
diff --git a/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cpp b/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cpp
index 47764111b83..0ff143f703e 100644
--- a/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cpp
+++ b/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cpp
@@ -179,6 +179,16 @@ void DisplayListResourceStorage::set_image_frame(ImageFrameResourceId id, Gfx::D
m_image_frames.set(id.value(), make<DisplayListStoredImageFrameResource>(move(frame)));
}
+void DisplayListResourceStorage::replace_image_frame_pixels(ImageFrameResourceId id, Gfx::DecodedImageFrame frame)
+{
+ auto stored = m_image_frames.get(id.value());
+ VERIFY(stored.has_value());
+ // NB: Whether force-dark filters the image stays as it was, so that it does not flip between the frames of one
+ // image.
+ stored.value()->frame = move(frame);
+ m_image_frame_pixel_generations.set(id.value(), m_next_image_frame_pixel_generation++);
+}
+
Gfx::DecodedImageFrame const& DisplayListResourceStorage::image_frame(ImageFrameResourceId id) const
{
return m_image_frames.get(id.value()).value()->frame;
@@ -384,6 +394,7 @@ DisplayListResourceSet DisplayListResourceStorage::apply_transaction(DisplayList
removed_resources.fonts.set(id);
}
for (auto id : transaction.image_frame_ids_to_remove) {
+ m_image_frame_pixel_generations.remove(id.value());
if (m_image_frames.remove(id.value()))
removed_resources.image_frames.set(id);
}
@@ -413,6 +424,7 @@ DisplayListResourceSet DisplayListResourceStorage::retain_only(DisplayListResour
m_image_frames.remove_all_matching([&](auto id, auto const&) {
if (resource_set.image_frames.contains(ImageFrameResourceId { id }))
return false;
+ m_image_frame_pixel_generations.remove(id);
removed_resources.image_frames.set(ImageFrameResourceId { id });
return true;
});
diff --git a/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.h b/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.h
index ed5f257b35d..82c00911c65 100644
--- a/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.h
+++ b/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.h
@@ -121,6 +121,11 @@ public:
DisplayListResourceSet retain_only(DisplayListResourceSet const&);
bool has_resources_added_since_last_retain() const { return m_has_resources_added_since_last_retain; }
void set_video_sink(VideoSinkResourceId, RefPtr<Media::DisplayingVideoSink>);
+ // Shows other pixels under an id the display lists keep drawing, as an animated image moves to its next frame.
+ void replace_image_frame_pixels(ImageFrameResourceId, Gfx::DecodedImageFrame);
+ // A stamp for each image frame that has had its pixels replaced. No two replacements share a stamp, even across the
+ // removal of a frame, so an equal stamp always means the same pixels.
+ HashMap<u64, u64> const& image_frame_pixel_generations() const { return m_image_frame_pixel_generations; }
bool has_font(FontResourceId id) const { return m_fonts.contains(id.value()); }
bool has_image_frame(ImageFrameResourceId id) const { return m_image_frames.contains(id.value()); }
@@ -148,6 +153,8 @@ private:
bool m_has_resources_added_since_last_retain { false };
HashMap<u64, NonnullRefPtr<Gfx::Font const>> m_fonts;
HashMap<u64, NonnullOwnPtr<DisplayListStoredImageFrameResource>> m_image_frames;
+ HashMap<u64, u64> m_image_frame_pixel_generations;
+ u64 m_next_image_frame_pixel_generation { 1 };
HashMap<u64, Media::VideoSinkHandle> m_video_sink_handles;
HashMap<u64, NonnullOwnPtr<DisplayListStoredVideoSinkResource>> m_video_sinks;
HashMap<u64, DisplayListResource> m_display_lists;
diff --git a/Libraries/LibCompositing/Rust/src/display_list/damage.rs b/Libraries/LibCompositing/Rust/src/display_list/damage.rs
index 7df3482702d..0777447d254 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/damage.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/damage.rs
@@ -16,8 +16,8 @@ use crate::display_list::commands::{
CanvasId, ClipMode, ClipNodeIndex, CompositorScrollbar, ContextRef, DeclareMaskContent, DisplayListCommandHeader,
DisplayListCommandRun, DisplayListCommandType, DisplayListDataSpan, DisplayListInlineClip,
DisplayListPaintStyleType, DrawGlyphRun, DrawScaledDecodedImageFrame, EffectNodeIndex, FillPath, FillRect,
- INLINE_CLIP_ENTRY_SIZE, OptionalColor, OptionalFloatRect, PaintScrollBar, PaintTextShadow, PathPaintKind,
- SpatialNodeIndex, StrokePath, VISUAL_VIEWPORT_NODE_INDEX, VideoSinkResourceId,
+ INLINE_CLIP_ENTRY_SIZE, ImageFrameResourceId, OptionalColor, OptionalFloatRect, PaintScrollBar, PaintTextShadow,
+ PathPaintKind, SpatialNodeIndex, StrokePath, VISUAL_VIEWPORT_NODE_INDEX, VideoSinkResourceId,
};
use crate::fast_hash::FastMap;
use crate::host::FfiAnimatedContentViewportEffect;
@@ -1462,25 +1462,27 @@ pub struct DisplayListFrame<'a> {
pub scroll_offsets: &'a [FloatPoint],
}
-// The canvases and video sinks whose pixels changed since the old frame. The lists draw them with
-// commands that stay the same, so the diff cannot see the change.
+// The canvases, video sinks and image frames whose pixels changed since the old frame. The lists draw
+// them with commands that stay the same, so the diff cannot see the change.
#[derive(Clone, Copy, Default)]
pub struct LiveContentChanges<'a> {
pub canvases: &'a [CanvasId],
pub video_sinks: &'a [VideoSinkResourceId],
+ pub image_frames: &'a [ImageFrameResourceId],
/// What the compositor repaints whatever the lists hold, such as a blinking caret.
pub forced_damage_rect: IntRect,
}
impl LiveContentChanges<'_> {
fn is_empty(&self) -> bool {
- self.canvases.is_empty() && self.video_sinks.is_empty()
+ self.canvases.is_empty() && self.video_sinks.is_empty() && self.image_frames.is_empty()
}
fn includes(&self, content: LiveContent) -> bool {
match content {
LiveContent::Canvas(canvas_id) => self.canvases.contains(&canvas_id),
LiveContent::VideoFrame(video_sink_id) => self.video_sinks.contains(&video_sink_id),
+ LiveContent::ImageFrame(image_frame_id) => self.image_frames.contains(&image_frame_id),
}
}
}
@@ -1682,8 +1684,7 @@ mod tests {
use crate::display_list::builder::{HEADER_SIZE, note_command};
use crate::display_list::commands::{
BackdropFilterRegion, CanvasId, CompositorMainThreadWheelEventRegion, DisplayListCommand, DisplayListGlyph,
- DrawCanvas, DrawIsolatedGroup, DrawVideoFrame, FillRect, FontResourceId, ImageFrameResourceId, InlineClipKind,
- TextShadowLayer,
+ DrawCanvas, DrawIsolatedGroup, DrawVideoFrame, FillRect, FontResourceId, InlineClipKind, TextShadowLayer,
};
use crate::display_list::ffi_bytes::FfiBytes;
use crate::node_slot_id::NodeSlotId;
@@ -2118,6 +2119,32 @@ mod tests {
damage_for_live_content_changes(&video, &tree, changed_canvas),
Some(IntRect::default())
);
+
+ let image = command_bytes(
+ &DrawScaledDecodedImageFrame {
+ dst_rect: FloatRect::new(10.0, 10.0, 20.0, 20.0),
+ src_rect: OptionalFloatRect::default(),
+ frame_id: ImageFrameResourceId(1),
+ scaling_mode: ScalingMode::NearestNeighbor,
+ compositing_and_blending_operator: CompositingAndBlendingOperator::Normal,
+ isolated_backdrop_color: OptionalColor::default(),
+ apply_force_dark: false,
+ },
+ Some(rect),
+ ContextRef::default(),
+ );
+ let changed_image = LiveContentChanges {
+ image_frames: &[ImageFrameResourceId(1)],
+ ..LiveContentChanges::default()
+ };
+ assert_eq!(
+ damage_for_live_content_changes(&image, &tree, changed_image),
+ Some(IntRect::new(9, 9, 22, 22))
+ );
+ assert_eq!(
+ damage_for_live_content_changes(&image, &tree, changed_video),
+ Some(IntRect::default())
+ );
}
#[test]
diff --git a/Libraries/LibCompositing/Rust/src/display_list/summary.rs b/Libraries/LibCompositing/Rust/src/display_list/summary.rs
index 2402c47b952..4e9bc36fd43 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/summary.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/summary.rs
@@ -27,6 +27,7 @@ pub struct IndexedRecord {
pub enum LiveContent {
Canvas(CanvasId),
VideoFrame(VideoSinkResourceId),
+ ImageFrame(ImageFrameResourceId),
}
// A top-level record that draws live content, itself or in a record nested in it.
@@ -57,11 +58,21 @@ struct ReferencedIds {
}
fn live_content_of(command_type: DisplayListCommandType, payload: &[u8]) -> Option<LiveContent> {
+ use DisplayListCommandType as T;
match command_type {
- DisplayListCommandType::DrawCanvas => Some(LiveContent::Canvas(read_command::<DrawCanvas>(payload).canvas_id)),
- DisplayListCommandType::DrawVideoFrame => Some(LiveContent::VideoFrame(
+ T::DrawCanvas => Some(LiveContent::Canvas(read_command::<DrawCanvas>(payload).canvas_id)),
+ T::DrawVideoFrame => Some(LiveContent::VideoFrame(
read_command::<DrawVideoFrame>(payload).video_sink_id,
)),
+ T::DrawScaledDecodedImageFrame => Some(LiveContent::ImageFrame(
+ read_command::<DrawScaledDecodedImageFrame>(payload).frame_id,
+ )),
+ T::DrawRepeatedDecodedImageFrame => Some(LiveContent::ImageFrame(
+ read_command::<DrawRepeatedDecodedImageFrame>(payload).frame_id,
+ )),
+ T::DrawTiledDecodedImageFrame => Some(LiveContent::ImageFrame(
+ read_command::<DrawTiledDecodedImageFrame>(payload).frame_id,
+ )),
_ => None,
}
}
diff --git a/Libraries/LibCompositing/Rust/src/ffi.rs b/Libraries/LibCompositing/Rust/src/ffi.rs
index b073dc6a48d..5566b3a162e 100644
--- a/Libraries/LibCompositing/Rust/src/ffi.rs
+++ b/Libraries/LibCompositing/Rust/src/ffi.rs
@@ -61,6 +61,8 @@ pub unsafe extern "C" fn display_list_compute_damage(
canvases_with_new_content_len: usize,
video_sinks_with_new_frames: *const crate::display_list::commands::VideoSinkResourceId,
video_sinks_with_new_frames_len: usize,
+ image_frames_with_new_pixels: *const crate::display_list::commands::ImageFrameResourceId,
+ image_frames_with_new_pixels_len: usize,
forced_damage_rect: libgfx_rust::IntRect,
out_damage_rect: *mut libgfx_rust::IntRect,
) -> bool {
@@ -80,6 +82,7 @@ pub unsafe extern "C" fn display_list_compute_damage(
crate::display_list::damage::LiveContentChanges {
canvases: ak::slice_from_raw_parts(canvases_with_new_content, canvases_with_new_content_len),
video_sinks: ak::slice_from_raw_parts(video_sinks_with_new_frames, video_sinks_with_new_frames_len),
+ image_frames: ak::slice_from_raw_parts(image_frames_with_new_pixels, image_frames_with_new_pixels_len),
forced_damage_rect,
}
};
diff --git a/Services/Compositor/ContextPresenter.cpp b/Services/Compositor/ContextPresenter.cpp
index 609aa9cabe4..80946b91f3b 100644
--- a/Services/Compositor/ContextPresenter.cpp
+++ b/Services/Compositor/ContextPresenter.cpp
@@ -459,6 +459,16 @@ Gfx::IntRect ContextPresenter::damage_since_last_raster(Gfx::IntSize viewport_si
return viewport_rect;
video_sinks_with_new_frames.append(video_sink_id);
}
+ Vector<Compositing::ImageFrameResourceId> image_frames_with_new_pixels;
+ auto image_frames_drawn_by_list = m_scene.display_list()->referenced_resource_ids().image_frames;
+ for (auto const& [image_frame_id, generation] : m_scene.resource_storage().image_frame_pixel_generations()) {
+ if (last_frame.image_frame_pixel_generations.get(image_frame_id) == generation)
+ continue;
+ // An image drawn only by a nested display list, such as an SVG image's, has no command in this list to damage.
+ if (!image_frames_drawn_by_list.contains_slow(image_frame_id))
+ return viewport_rect;
+ image_frames_with_new_pixels.append(Compositing::ImageFrameResourceId { image_frame_id });
+ }
for (auto const& scrollbar : frame_content.scrollbar_controller.scrollbars()) {
if (!scrollbar.is_painted_by_compositor)
continue;
@@ -474,7 +484,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 = video_sinks_with_new_frames, .forced_damage_rect = forced_damage_rect });
+ { .canvases = canvases_with_new_content, .video_sinks = video_sinks_with_new_frames, .image_frames = image_frames_with_new_pixels, .forced_damage_rect = forced_damage_rect });
if (!display_list_damage.has_value())
return viewport_rect;
return display_list_damage->intersected(viewport_rect);
@@ -500,6 +510,7 @@ void ContextPresenter::remember_rasterized_frame(Gfx::IntSize viewport_size, Com
.viewport_size = viewport_size,
.canvas_content_generations = move(canvas_content_generations),
.video_frame_ids = move(video_frame_ids),
+ .image_frame_pixel_generations = m_scene.resource_storage().image_frame_pixel_generations(),
};
}
diff --git a/Services/Compositor/ContextPresenter.h b/Services/Compositor/ContextPresenter.h
index cdf9a2c6aa4..417741fa43e 100644
--- a/Services/Compositor/ContextPresenter.h
+++ b/Services/Compositor/ContextPresenter.h
@@ -129,6 +129,7 @@ private:
Gfx::IntSize viewport_size;
HashMap<Compositing::CanvasId, u64> canvas_content_generations;
HashMap<Compositing::VideoSinkResourceId, Optional<u64>> video_frame_ids;
+ HashMap<u64, u64> image_frame_pixel_generations;
};
enum class PaintUIOverlay : u8 {