ladybird/Libraries/LibWeb/HTML/AnimatedDecodedImageData.cpp

85 lines
2.7 KiB
C++
Raw Permalink Normal View History

LibWeb: Transfer animation ownership to `AnimatedBitmapDecodedImageData` 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.
2026-06-15 19:56:00 -03:00
/*
* Copyright (c) 2026, Callum Law <callumlaw1709@outlook.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "AnimatedDecodedImageData.h"
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/DocumentObserver.h>
namespace Web::HTML {
void AnimatedDecodedImageData::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_document_observer);
}
AnimatedDecodedImageData::AnimatedDecodedImageData(GC::Ref<DOM::DocumentObserver> document_observer)
: m_document_observer(document_observer)
{
auto weak_this = GC::Weak { *this };
// OPTIMIZATION: To avoid CPU churn in background tabs we cancel the animation when the document is inactive or
// hidden. Other browsers disagree on what should happen when the document becomes active again,
// Blink continues the animation from where it would be if it had been running the whole time, and
// Gecko restarts the animation. For now we restart the animation since that's simpler.
m_document_observer->set_document_became_inactive([weak_this] {
if (auto self = weak_this.ptr())
self->stop_animation();
});
m_document_observer->set_document_became_active([weak_this] {
if (auto self = weak_this.ptr()) {
if (!self->has_clients())
self->m_should_start_animation_on_client_registration = true;
self->restart_animation();
}
});
m_document_observer->set_document_visibility_state_observer([weak_this](HTML::VisibilityState visibility_state) {
if (auto self = weak_this.ptr()) {
switch (visibility_state) {
case HTML::VisibilityState::Hidden:
self->stop_animation();
break;
case HTML::VisibilityState::Visible:
if (!self->has_clients())
self->m_should_start_animation_on_client_registration = true;
self->restart_animation();
break;
}
}
});
}
void AnimatedDecodedImageData::restart_animation()
{
stop_animation();
reset_animation();
start_animation_if_needed();
}
void AnimatedDecodedImageData::on_client_registered()
{
if (!m_should_start_animation_on_client_registration)
return;
m_should_start_animation_on_client_registration = false;
start_animation_if_needed();
}
void AnimatedDecodedImageData::start_animation_if_needed()
{
// NB: Animations should start when the first client is registered while the document is active and visible.
if (!has_clients() || !m_document_observer->document()->is_fully_active() || m_document_observer->document()->hidden())
return;
start_animation();
}
}