ladybird/Libraries/LibWeb/CSS/StyleValues/RadialGradientStyleValue.cpp
Andreas Kling 222c1f7044 LibWeb: Move CSS image loading state to Document
Move the SharedResourceRequest, animation timer, and current frame
state out of ImageStyleValue and into a Document-owned table keyed
by resolved image URL. ImageStyleValue now keeps only URL metadata
and its client list, so image style values no longer need to trace
GC edges themselves.

Thread the Document through AbstractImageStyleValue APIs that need
decoded image data. CSS image fetches snapshot the stylesheet base URL,
referrer behavior, and origin-clean state instead of retaining the
stylesheet.

Remember each client's registered resolved URL when unregistering. This
keeps a later document base change from leaving an animated image
resource alive.

Add text coverage for inline relative image base URLs, stylesheet
referrers, imported stylesheet origin-clean behavior, inline @import
initiator type, and unregistering an animated background image after a
base element change.
2026-06-06 23:29:48 +02:00

122 lines
5.1 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "RadialGradientStyleValue.h"
#include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
#include <LibWeb/CSS/StyleValues/RadialSizeStyleValue.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Painting/DisplayListRecorder.h>
namespace Web::CSS {
void RadialGradientStyleValue::serialize(StringBuilder& builder, SerializationMode mode) const
{
if (is_repeating())
builder.append("repeating-"sv);
builder.append("radial-gradient("sv);
// AD-HOC: We need to check the serialized size to determine if it should be included.
auto const& serialized_size = m_properties.size->to_string(mode);
bool has_size = serialized_size != "farthest-corner"sv;
bool has_position = !m_properties.position->is_center(mode);
bool has_color_space = m_properties.color_interpolation_method && m_properties.color_interpolation_method->as_color_interpolation_method().color_interpolation_method() != ColorInterpolationMethodStyleValue::default_color_interpolation_method(m_properties.color_syntax);
if (has_size)
m_properties.size->serialize(builder, mode);
if (has_position) {
if (has_size)
builder.append(' ');
builder.append("at "sv);
m_properties.position->serialize(builder, mode);
}
if (has_color_space) {
if (has_size || has_position)
builder.append(' ');
m_properties.color_interpolation_method->serialize(builder, mode);
}
if (has_size || has_position || has_color_space)
builder.append(", "sv);
serialize_color_stop_list(builder, m_properties.color_stop_list, mode);
builder.append(')');
}
CSSPixelSize RadialGradientStyleValue::resolve_size(CSSPixelPoint center, CSSPixelRect const& reference_box, Layout::NodeWithStyle const& node) const
{
if (m_properties.ending_shape == EndingShape::Circle) {
auto radius = m_properties.size->as_radial_size().resolve_circle_size(center, reference_box, node);
return CSSPixelSize { radius, radius };
}
return m_properties.size->as_radial_size().resolve_ellipse_size(center, reference_box, node);
}
void RadialGradientStyleValue::resolve_for_size(Layout::NodeWithStyle const& node, CSSPixelSize paint_size) const
{
CSSPixelRect gradient_box { { 0, 0 }, paint_size };
auto center = m_properties.position->resolved(node, gradient_box);
auto gradient_size = resolve_size(center, gradient_box, node);
if (m_resolved_size != paint_size) {
m_resolved_size = move(paint_size);
m_resolved = ResolvedData {
Painting::resolve_radial_gradient_data(node, gradient_size, *this),
gradient_size,
center,
};
}
}
ValueComparingNonnullRefPtr<StyleValue const> RadialGradientStyleValue::absolutized(ComputationContext const& context) const
{
Vector<ColorStopListElement> absolutized_color_stops;
absolutized_color_stops.ensure_capacity(m_properties.color_stop_list.size());
for (auto const& color_stop : m_properties.color_stop_list) {
absolutized_color_stops.unchecked_append(color_stop.absolutized(context));
}
auto absolutized_size = m_properties.size->absolutized(context);
NonnullRefPtr absolutized_position = m_properties.position->absolutized(context)->as_position();
auto absolutized_color_interpolation_method = m_properties.color_interpolation_method ? ValueComparingRefPtr<StyleValue const> { m_properties.color_interpolation_method->absolutized(context) } : nullptr;
return create(m_properties.ending_shape, move(absolutized_size), move(absolutized_position), move(absolutized_color_stops), m_properties.repeating, move(absolutized_color_interpolation_method));
}
bool RadialGradientStyleValue::equals(StyleValue const& other) const
{
if (type() != other.type())
return false;
auto& other_gradient = other.as_radial_gradient();
return m_properties == other_gradient.m_properties;
}
bool RadialGradientStyleValue::is_computationally_independent() const
{
return m_properties.size->is_computationally_independent()
&& m_properties.position->is_computationally_independent()
&& all_of(m_properties.color_stop_list, [&](auto const& stop) { return stop.color_stop.color->is_computationally_independent(); })
&& (!m_properties.color_interpolation_method || m_properties.color_interpolation_method->is_computationally_independent());
}
void RadialGradientStyleValue::paint(DisplayListRecordingContext& context, DOM::Document const&, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
{
VERIFY(m_resolved.has_value());
auto center = context.rounded_device_point(m_resolved->center).to_type<int>();
auto size = context.rounded_device_size(m_resolved->gradient_size).to_type<int>();
context.display_list_recorder().fill_rect_with_radial_gradient(dest_rect.to_type<int>(), m_resolved->data, center, size);
}
}