Skip to content

Truly independent web browser

LibWeb: Darken the content of SVG images under force dark

sideshowbarkercc7a807aadf9
diff --git a/Libraries/LibCompositing/Rust/src/force_dark.rs b/Libraries/LibCompositing/Rust/src/force_dark.rs
index c7b558e537b..82d27c4e6ba 100644
--- a/Libraries/LibCompositing/Rust/src/force_dark.rs
+++ b/Libraries/LibCompositing/Rust/src/force_dark.rs
@@ -345,6 +345,10 @@ pub struct ForceDarkResolver {
const INVERTED_COLOR_CACHE_CAPACITY: usize = 1024;
impl ForceDarkResolver {
+ pub fn settings(&self) -> ForceDarkSettings {
+ self.settings
+ }
+
pub fn new(settings: ForceDarkSettings) -> Self {
Self {
settings,
diff --git a/Libraries/LibWeb/Painting/PaintingRustBridge.cpp b/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
index 05468308445..d55288ded7a 100644
--- a/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
+++ b/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
@@ -402,7 +402,12 @@ static Layout::RustFFI::FfiRecordingPublishCallbacks recording_publish_callbacks
auto const* svg_image_data = as_if<SVG::SVGDecodedImageData>(decoded_image_data.ptr());
if (!svg_image_data)
return empty_display_list();
- auto display_list = svg_image_data->record_display_list_at_scale({ request->css_width, request->css_height }, request->raster_scale, static_cast<CSS::PreferredColorScheme>(request->color_scheme), context.resource_storage);
+ // The image's content darkens with the page that draws it, so a force-dark page renders it force-dark too.
+ HTML::PaintConfig paint_config;
+ paint_config.force_dark_enabled = request->force_dark_enabled;
+ paint_config.force_dark_foreground_threshold = request->force_dark_foreground_threshold;
+ paint_config.force_dark_background_threshold = request->force_dark_background_threshold;
+ auto display_list = svg_image_data->record_display_list_at_scale({ request->css_width, request->css_height }, request->raster_scale, static_cast<CSS::PreferredColorScheme>(request->color_scheme), paint_config, context.resource_storage);
if (!display_list.has_value())
return empty_display_list();
return context.resource_storage.add_display_list(move(*display_list)).value();
diff --git a/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs b/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs
index 3a29697a1f9..cd7e3d75f48 100644
--- a/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs
+++ b/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs
@@ -904,6 +904,11 @@ impl DisplayListRecorder {
.bytes
}
+ /// The force-dark settings the recorder applies, which content it records while force dark is suspended has none of.
+ pub fn force_dark_settings(&self) -> Option<ForceDarkSettings> {
+ self.force_dark.as_ref().map(ForceDarkResolver::settings)
+ }
+
pub fn suspend_force_dark(&mut self) -> Option<ForceDarkResolver> {
self.force_dark.take()
}
diff --git a/Libraries/LibWeb/Rust/src/painting/host/paint.rs b/Libraries/LibWeb/Rust/src/painting/host/paint.rs
index 2578ae1063d..059c9de63df 100644
--- a/Libraries/LibWeb/Rust/src/painting/host/paint.rs
+++ b/Libraries/LibWeb/Rust/src/painting/host/paint.rs
@@ -473,6 +473,9 @@ pub struct FfiVectorImageRenderRequest {
pub css_height: crate::css::css_pixels::CssPixels,
pub raster_scale: f32,
pub color_scheme: u8,
+ pub force_dark_enabled: bool,
+ pub force_dark_foreground_threshold: i32,
+ pub force_dark_background_threshold: i32,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
diff --git a/Libraries/LibWeb/Rust/src/painting/record/mod.rs b/Libraries/LibWeb/Rust/src/painting/record/mod.rs
index c6e542c1970..f1617d60851 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/mod.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/mod.rs
@@ -225,6 +225,12 @@ impl<O: Observer> PaintRecorder<'_, O> {
.unwrap_or(self.inputs.image_fallback_color_scheme)
}
+ /// The force-dark settings an SVG image the recording draws renders its content with: the recorder's own, so the
+ /// image darkens along with the page around it, and stays as authored in a mask, where force dark is suspended.
+ pub(crate) fn vector_image_force_dark(&self) -> Option<crate::painting::force_dark::ForceDarkSettings> {
+ self.recorder.force_dark_settings()
+ }
+
pub(crate) fn paint_vector_image(
&mut self,
source: vector_images::VectorImageSource,
@@ -245,6 +251,7 @@ impl<O: Observer> PaintRecorder<'_, O> {
geometry.raster_scale,
content_identity,
self.vector_image_color_scheme(source.owner()),
+ self.vector_image_force_dark(),
));
if compositing_and_blending_operator != CompositingAndBlendingOperator::Normal {
let dest_device_rect = libgfx_rust::enclosing_int_rect(dest_rect);
@@ -769,6 +776,7 @@ impl<O: Observer> PaintRecorder<'_, O> {
pattern: pattern.index,
root_transform_bits: root_transform.values.map(f32::to_bits),
tile_surface_size_bits: [tile_surface_rect.width.to_bits(), tile_surface_rect.height.to_bits()],
+ force_dark: self.recorder.force_dark_settings().is_some(),
};
if let Some(records) = self.scratch.pattern_tile_records.get(&key) {
return records.clone();
@@ -791,4 +799,7 @@ struct PatternTileKey {
pattern: u32,
root_transform_bits: [u32; 6],
tile_surface_size_bits: [u32; 2],
+ // A tile recorded inside a mask, where force dark is suspended, has authored colors, which a tile on the page
+ // can't reuse.
+ force_dark: bool,
}
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
index 0e826c6b6c0..643299e4f01 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
@@ -853,6 +853,7 @@ fn paint_image_layer_in_current_space<O: Observer>(
1.0,
*content_identity,
recorder.vector_image_color_scheme(vector_image_source.owner()),
+ recorder.vector_image_force_dark(),
),
);
let tile_size = IntSize {
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/border_image.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/border_image.rs
index b279dc1fd8f..994e2c4ec62 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/border_image.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/border_image.rs
@@ -303,6 +303,7 @@ fn paint_vector_border_image_slice<O: Observer>(
geometry.raster_scale,
image.content_identity,
recorder.vector_image_color_scheme(image.source.owner()),
+ recorder.vector_image_force_dark(),
));
let group = recorder.recorder.begin_repeated_tile();
// The tile holds the whole image, offset so that the slice fills it, and clips away the rest.
diff --git a/Libraries/LibWeb/Rust/src/painting/record/vector_images.rs b/Libraries/LibWeb/Rust/src/painting/record/vector_images.rs
index 8cfeefdee67..80ddfcaf674 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/vector_images.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/vector_images.rs
@@ -7,6 +7,7 @@
use crate::css::css_pixels::CssPixels;
use crate::layout::node_data::NodeSlotId;
use crate::painting::display_list::commands::DisplayListResourceId;
+use crate::painting::force_dark::ForceDarkSettings;
use crate::painting::host::{FfiLayerImageList, FfiVectorImageRenderRequest};
use libgfx_rust::{FloatRect, FloatSize, IntSize};
@@ -47,11 +48,15 @@ pub(crate) struct VectorImageRenderRequest {
css_width_raw: i32,
css_height_raw: i32,
raster_scale_bits: u32,
- /// What the SVG image shows, which the host renders it from: a request with the same identity, size, scale and
- /// color scheme renders the same display list.
+ /// What the SVG image shows, which the host renders it from: a request with the same identity, size, scale, color
+ /// scheme and force-dark settings renders the same display list.
content_identity: u64,
/// The color scheme the host renders the SVG image in.
color_scheme: u8,
+ /// The force-dark settings of the page that draws the image, which the host renders the image's content with.
+ force_dark_enabled: bool,
+ force_dark_foreground_threshold: i32,
+ force_dark_background_threshold: i32,
}
impl VectorImageRenderRequest {
@@ -62,7 +67,9 @@ impl VectorImageRenderRequest {
raster_scale: f32,
content_identity: u64,
color_scheme: u8,
+ force_dark: Option<ForceDarkSettings>,
) -> Self {
+ let force_dark_settings = force_dark.unwrap_or_default();
Self {
source,
css_width_raw: css_width.raw_value(),
@@ -70,6 +77,9 @@ impl VectorImageRenderRequest {
raster_scale_bits: raster_scale.to_bits(),
content_identity,
color_scheme,
+ force_dark_enabled: force_dark.is_some(),
+ force_dark_foreground_threshold: force_dark_settings.foreground_brightness_threshold,
+ force_dark_background_threshold: force_dark_settings.background_brightness_threshold,
}
}
@@ -95,6 +105,9 @@ impl VectorImageRenderRequest {
css_height: CssPixels::from_raw(self.css_height_raw),
raster_scale: self.raster_scale(),
color_scheme: self.color_scheme,
+ force_dark_enabled: self.force_dark_enabled,
+ force_dark_foreground_threshold: self.force_dark_foreground_threshold,
+ force_dark_background_threshold: self.force_dark_background_threshold,
}
}
}
diff --git a/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp b/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp
index 71a670c7f65..80b2759a8b5 100644
--- a/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp
+++ b/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp
@@ -256,16 +256,16 @@ void SVGDecodedImageData::append_paint_command_cache_source_resources(Compositin
Optional<Compositing::DisplayListResource> SVGDecodedImageData::record_display_list(Gfx::IntSize size, CSS::PreferredColorScheme color_scheme, Compositing::DisplayListResourceStorage& destination_resource_storage) const
{
- return record_display_list_at_scale(size.to_type<CSSPixels>(), 1, color_scheme, destination_resource_storage);
+ return record_display_list_at_scale(size.to_type<CSSPixels>(), 1, color_scheme, {}, destination_resource_storage);
}
-Optional<Compositing::DisplayListResource> SVGDecodedImageData::record_display_list_at_scale(CSSPixelSize css_size, float raster_scale, CSS::PreferredColorScheme color_scheme, Compositing::DisplayListResourceStorage& destination_resource_storage) const
+Optional<Compositing::DisplayListResource> SVGDecodedImageData::record_display_list_at_scale(CSSPixelSize css_size, float raster_scale, CSS::PreferredColorScheme color_scheme, HTML::PaintConfig const& paint_config, Compositing::DisplayListResourceStorage& destination_resource_storage) const
{
ScopedSVGImageDocument scoped_document { *m_document, ScopedSVGImageDocument::FrameRequests::RouteToCurrentImage };
auto& navigable = *m_document->navigable();
auto& resource_storage = navigable.display_list_resource_storage();
- RenderKey const key { css_size, raster_scale, color_scheme };
+ RenderKey const key { css_size, raster_scale, color_scheme, paint_config };
if (auto it = m_cached_display_lists.find(key); it != m_cached_display_lists.end()) {
copy_referenced_resources_to(destination_resource_storage, resource_storage, it->value.referenced_resources);
return Compositing::DisplayListResource { *it->value.display_list, it->value.visual_context_tree };
@@ -312,7 +312,7 @@ Optional<Compositing::DisplayListResource> SVGDecodedImageData::record_display_l
navigable.set_viewport_size(css_size);
Layout::ForcedReadScope read { *m_document };
m_document->update_layout(DOM::UpdateLayoutReason::SVGDecodedImageDataRender);
- auto display_list = m_document->record_display_list(read, {}, resource_storage, Painting::PaintCommandCacheMode::ReadWrite);
+ auto display_list = m_document->record_display_list(read, paint_config, resource_storage, Painting::PaintCommandCacheMode::ReadWrite);
if (!display_list)
return {};
@@ -624,7 +624,7 @@ Optional<Painting::ImagePaint> SVGDecodedImageData::image_paint(Painting::ImageP
raster_scale = 1;
auto maximum_raster_scale = static_cast<float>(maximum_raster_dimension) / max(css_size.width(), css_size.height()).to_float();
raster_scale = min(raster_scale, maximum_raster_scale);
- display_list = record_display_list_at_scale(css_size, raster_scale, color_scheme, request.resource_storage);
+ display_list = record_display_list_at_scale(css_size, raster_scale, color_scheme, {}, request.resource_storage);
list_size = {
max(1, static_cast<int>(lroundf(css_size.width().to_float() * raster_scale))),
max(1, static_cast<int>(lroundf(css_size.height().to_float() * raster_scale))),
diff --git a/Libraries/LibWeb/SVG/SVGDecodedImageData.h b/Libraries/LibWeb/SVG/SVGDecodedImageData.h
index 03ee3d10dbe..b46ed6c80bd 100644
--- a/Libraries/LibWeb/SVG/SVGDecodedImageData.h
+++ b/Libraries/LibWeb/SVG/SVGDecodedImageData.h
@@ -15,6 +15,7 @@
#include <LibGfx/DecodedImageFrame.h>
#include <LibWeb/CSS/Sizing.h>
#include <LibWeb/HTML/DecodedImageData.h>
+#include <LibWeb/HTML/PaintConfig.h>
#include <LibWeb/Page/Page.h>
#include <LibWebCommon/Page/PageId.h>
#include <LibWebCommon/Page/QueuedInputEvent.h>
@@ -41,7 +42,7 @@ public:
virtual Optional<Compositing::DisplayListResource> record_display_list(Gfx::IntSize, CSS::PreferredColorScheme, Compositing::DisplayListResourceStorage&) const override;
// Lays the inner document out at the CSS size and records at css × raster_scale resolution.
- Optional<Compositing::DisplayListResource> record_display_list_at_scale(CSSPixelSize css_size, float raster_scale, CSS::PreferredColorScheme, Compositing::DisplayListResourceStorage&) const;
+ Optional<Compositing::DisplayListResource> record_display_list_at_scale(CSSPixelSize css_size, float raster_scale, CSS::PreferredColorScheme, HTML::PaintConfig const&, Compositing::DisplayListResourceStorage&) const;
// FIXME: Support SVG animations. :^)
DOM::Document const& svg_document() const { return *m_document; }
@@ -88,13 +89,17 @@ private:
CSSPixelSize css_size;
float raster_scale { 1 };
CSS::PreferredColorScheme color_scheme;
+ HTML::PaintConfig paint_config;
bool operator==(RenderKey const&) const = default;
};
struct RenderKeyTraits : public Traits<RenderKey> {
static unsigned hash(RenderKey const& key)
{
auto size_hash = pair_int_hash(key.css_size.width().raw_value(), key.css_size.height().raw_value());
- return pair_int_hash(pair_int_hash(size_hash, bit_cast<u32>(key.raster_scale)), to_underlying(key.color_scheme));
+ auto hash = pair_int_hash(pair_int_hash(size_hash, bit_cast<u32>(key.raster_scale)), to_underlying(key.color_scheme));
+ if (!key.paint_config.force_dark_enabled)
+ return hash;
+ return pair_int_hash(hash, pair_int_hash(key.paint_config.force_dark_foreground_threshold, key.paint_config.force_dark_background_threshold));
}
static bool equals(RenderKey const& a, RenderKey const& b) { return a == b; }
};
diff --git a/Tests/LibWeb/Ref/expected/force-dark-darkens-svg-images-like-the-page-ref.html b/Tests/LibWeb/Ref/expected/force-dark-darkens-svg-images-like-the-page-ref.html
new file mode 100644
index 00000000000..43e53f1d7d1
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/force-dark-darkens-svg-images-like-the-page-ref.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<style>
+ body { margin: 0; background: #808080; }
+ svg { position: absolute; top: 0; }
+</style>
+<svg style="left: 0" width="100" height="100"><rect width="100" height="100" fill="#f6d2dd"/><rect x="25" y="25" width="50" height="50" fill="#202020"/></svg>
+<svg style="left: 100px" width="100" height="100"><rect width="100" height="100" fill="#f6d2dd"/><rect x="25" y="25" width="50" height="50" fill="#202020"/></svg>
+<script>
+ if (window.internals)
+ internals.setForceDarkEnabled(true);
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ document.documentElement.classList.remove("reftest-wait");
+ });
+ });
+</script>
+</html>
diff --git a/Tests/LibWeb/Ref/expected/force-dark-keeps-svg-images-in-masks-as-authored-ref.html b/Tests/LibWeb/Ref/expected/force-dark-keeps-svg-images-in-masks-as-authored-ref.html
new file mode 100644
index 00000000000..b689d77e301
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/force-dark-keeps-svg-images-in-masks-as-authored-ref.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<style>
+ body { margin: 0; background: #808080; }
+</style>
+<svg width="100" height="100">
+ <rect width="50" height="100" fill="green"/>
+</svg>
+<script>
+ if (window.internals)
+ internals.setForceDarkEnabled(true);
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ document.documentElement.classList.remove("reftest-wait");
+ });
+ });
+</script>
+</html>
diff --git a/Tests/LibWeb/Ref/input/force-dark-darkens-svg-images-like-the-page.html b/Tests/LibWeb/Ref/input/force-dark-darkens-svg-images-like-the-page.html
new file mode 100644
index 00000000000..8170f1a3477
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/force-dark-darkens-svg-images-like-the-page.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="match" href="../expected/force-dark-darkens-svg-images-like-the-page-ref.html">
+<style>
+ body { margin: 0; background: #808080; }
+ div, img { position: absolute; top: 0; width: 100px; height: 100px; }
+ div { left: 0; background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIj48cmVjdCB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIgZmlsbD0iI2Y2ZDJkZCIvPjxyZWN0IHg9IjI1IiB5PSIyNSIgd2lkdGg9IjUwIiBoZWlnaHQ9IjUwIiBmaWxsPSIjMjAyMDIwIi8+PC9zdmc+"); }
+ img { left: 100px; }
+</style>
+<div></div>
+<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIj48cmVjdCB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIgZmlsbD0iI2Y2ZDJkZCIvPjxyZWN0IHg9IjI1IiB5PSIyNSIgd2lkdGg9IjUwIiBoZWlnaHQ9IjUwIiBmaWxsPSIjMjAyMDIwIi8+PC9zdmc+">
+<script>
+ // An SVG image's content darkens with the page that draws it: Drawn as a background image or as an img, it lands
+ // on the same colors as the same SVG inline in the page.
+ if (window.internals)
+ internals.setForceDarkEnabled(true);
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ document.documentElement.classList.remove("reftest-wait");
+ });
+ });
+</script>
+</html>
diff --git a/Tests/LibWeb/Ref/input/force-dark-keeps-svg-images-in-masks-as-authored.html b/Tests/LibWeb/Ref/input/force-dark-keeps-svg-images-in-masks-as-authored.html
new file mode 100644
index 00000000000..8463f33c61c
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/force-dark-keeps-svg-images-in-masks-as-authored.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="match" href="../expected/force-dark-keeps-svg-images-in-masks-as-authored-ref.html">
+<style>
+ body { margin: 0; background: #808080; }
+</style>
+<svg width="100" height="100">
+ <mask id="mask"><image href="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIj48cmVjdCB3aWR0aD0iNTAiIGhlaWdodD0iMTAwIiBmaWxsPSJ3aGl0ZSIvPjxyZWN0IHg9IjUwIiB3aWR0aD0iNTAiIGhlaWdodD0iMTAwIiBmaWxsPSJibGFjayIvPjwvc3ZnPg==" width="100" height="100"/></mask>
+ <rect width="100" height="100" fill="green" mask="url(#mask)"/>
+</svg>
+<script>
+ // A mask's content isn't painted but read for its luminance, so force dark leaves it alone, and an SVG image in a
+ // mask renders as authored too: Its black half hides the rect's right half, rather than turning light to show it.
+ if (window.internals)
+ internals.setForceDarkEnabled(true);
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ document.documentElement.classList.remove("reftest-wait");
+ });
+ });
+</script>
+</html>