Previously animation ownership was a messy split between `AnimatedBitmapDecodedImageData` and the consumers (i.e. `ImageStyleValueResource`, `HTMLImageElement`, and `SVGImageElement`) with `AnimatedBitmapDecodedImageData` owning the frames and a current frame index, and the consumers owning the rest of the state (e.g. loop count, timers to drive the animation forward, their own current index). This had a couple of main issues: - While `AnimatedDecodedImageData` partially synchronized animations by dropping unexpected advancement notifications, this didn't apply to other animation state which meant, for instance, that a later started consumer could drive the animation of an earlier one past the max loop count (albeit without invalidating the earlier consumer). - Multiple consumers didn't share frame timings, meaning animations could be up to a full frame out of sync visually. - Animations were paused depending on whether there were any consumers, this is different to the behavior in other browsers (where they continue regardless of whether there are any consumers). - It was an overgeneralization of how animations need to work - only `AnimatedBitmapDecodedImageData` works with an indexed frame model, with animated SVGs (although not yet implemented) relying on their internal event loop to be driven forward. Given the above the new approach implemented in this commit is: - The API for `DecodedImageData` is animation system agnostic, only exposing `default_frame`, `current_frame`, and `restart_animation` methods not reliant on providing a specific frame index. - `AnimatedBitmapDecodedImageData` owns its own timer, loop count, etc. The animation starts when the first consumer registers and ends when the document is hidden or becomes inactive (or completes in the case of finite animations). - Consumers are invalidated by `AnimatedBitmapDecodedImageData` when required. Tests have been added for: - Animations being paused when the document becomes inactive and restarted when it becomes active again. - Frame timings being synchronized across consumers. - Restarts triggered by `HTMLImageElement` applying to all consumers. - Processing ending once a non-infinite animation plays to completion. The tests to ensure animations are cancelled when consumers are removed (e.g. `animated-background-image-timer-stops-when-hidden.html`) have been updated to assert the inverse since animation state is now per resource not per consumer.
102 lines
4.4 KiB
C++
102 lines
4.4 KiB
C++
/*
|
|
* Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
|
|
* Copyright (c) 2026, Jelle Raaijmakers <jelle@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
|
|
#include <LibWeb/HTML/DecodedImageData.h>
|
|
#include <LibWeb/HTML/HTMLImageElement.h>
|
|
#include <LibWeb/Painting/BorderRadiusCornerClipper.h>
|
|
#include <LibWeb/Painting/DisplayListRecorder.h>
|
|
#include <LibWeb/Painting/ImagePaintable.h>
|
|
#include <LibWeb/Painting/ReplacedElementCommon.h>
|
|
#include <LibWeb/Platform/FontPlugin.h>
|
|
|
|
namespace Web::Painting {
|
|
|
|
NonnullRefPtr<ImagePaintable> ImagePaintable::create(Layout::SVGImageBox const& layout_box)
|
|
{
|
|
return adopt_ref(*new ImagePaintable(layout_box, layout_box.dom_node(), false, String {}, true));
|
|
}
|
|
|
|
NonnullRefPtr<ImagePaintable> ImagePaintable::create(Layout::ImageBox const& layout_box)
|
|
{
|
|
String alt;
|
|
if (auto element = layout_box.dom_node())
|
|
alt = element->get_attribute_value(HTML::AttributeNames::alt);
|
|
return adopt_ref(*new ImagePaintable(layout_box, layout_box.image_provider(), layout_box.renders_as_alt_text(), move(alt), false));
|
|
}
|
|
|
|
ImagePaintable::ImagePaintable(Layout::Box const& layout_box, Layout::ImageProvider const& image_provider, bool renders_as_alt_text, String alt_text, bool is_svg_image)
|
|
: PaintableBox(layout_box)
|
|
, m_renders_as_alt_text(renders_as_alt_text)
|
|
, m_alt_text(move(alt_text))
|
|
, m_image_provider(image_provider)
|
|
, m_is_svg_image(is_svg_image)
|
|
{
|
|
}
|
|
|
|
void ImagePaintable::reset_for_relayout()
|
|
{
|
|
PaintableBox::reset_for_relayout();
|
|
|
|
if (!m_is_svg_image) {
|
|
m_renders_as_alt_text = !m_image_provider.is_image_available();
|
|
if (auto const* image_box = as_if<Layout::ImageBox>(layout_node())) {
|
|
if (auto element = image_box->dom_node())
|
|
m_alt_text = element->get_attribute_value(HTML::AttributeNames::alt);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ImagePaintable::paint(DisplayListRecordingContext& context, PaintPhase phase) const
|
|
{
|
|
if (!is_visible())
|
|
return;
|
|
|
|
PaintableBox::paint(context, phase);
|
|
|
|
if (phase == PaintPhase::Foreground) {
|
|
auto image_rect = absolute_rect();
|
|
auto image_rect_device_pixels = context.rounded_device_rect(image_rect);
|
|
auto renders_as_alt_text = m_is_svg_image ? m_renders_as_alt_text : !m_image_provider.is_image_available();
|
|
if (renders_as_alt_text) {
|
|
if (!m_alt_text.is_empty()) {
|
|
auto enclosing_rect = context.enclosing_device_rect(image_rect).to_type<int>();
|
|
context.display_list_recorder().draw_text(enclosing_rect, Utf16String::from_utf8(m_alt_text), layout_node().font(context), Gfx::TextAlignment::Center, computed_values().color());
|
|
}
|
|
} else if (auto decoded_image_data = m_image_provider.decoded_image_data()) {
|
|
ScopedCornerRadiusClip corner_clip { context, image_rect_device_pixels, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) };
|
|
auto image_int_rect_device_pixels = image_rect_device_pixels.to_type<int>();
|
|
|
|
// https://drafts.csswg.org/css-images/#the-object-fit
|
|
auto object_fit = m_is_svg_image ? CSS::ObjectFit::Contain : computed_values().object_fit();
|
|
|
|
auto intrinsic_size = m_image_provider.intrinsic_size()
|
|
.map([](auto size) { return size.template to_type<int>(); })
|
|
.value_or(image_int_rect_device_pixels.size());
|
|
|
|
auto draw_rect = get_replaced_box_painting_area(*this, context, object_fit, intrinsic_size);
|
|
if (!draw_rect.is_empty()) {
|
|
auto draw_rect_needs_clip = !image_int_rect_device_pixels.contains(draw_rect);
|
|
if (draw_rect_needs_clip) {
|
|
context.display_list_recorder().save();
|
|
context.display_list_recorder().add_clip_rect(image_int_rect_device_pixels);
|
|
}
|
|
decoded_image_data->paint(context, draw_rect, computed_values().image_rendering());
|
|
if (draw_rect_needs_clip)
|
|
context.display_list_recorder().restore();
|
|
}
|
|
}
|
|
|
|
if (selection_state() != SelectionState::None) {
|
|
auto selection_background_color = selection_style().background_color;
|
|
if (selection_background_color.alpha() > 0)
|
|
context.display_list_recorder().fill_rect(image_rect_device_pixels.to_type<int>(), selection_background_color);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|