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.
64 lines
1.7 KiB
C++
64 lines
1.7 KiB
C++
/*
|
|
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibGfx/DecodedImageFrame.h>
|
|
#include <LibWeb/HTML/DecodedImageData.h>
|
|
#include <LibWeb/Layout/ImageBox.h>
|
|
#include <LibWeb/Layout/ImageProvider.h>
|
|
|
|
namespace Web::Layout {
|
|
|
|
void ImageProvider::did_update_alt_text(ImageBox& layout_node)
|
|
{
|
|
layout_node.dom_node_did_update_alt_text({});
|
|
}
|
|
|
|
Optional<CSSPixels> ImageProvider::intrinsic_width() const
|
|
{
|
|
if (auto const& data = decoded_image_data())
|
|
return data->intrinsic_width();
|
|
return {};
|
|
}
|
|
|
|
Optional<CSSPixels> ImageProvider::intrinsic_height() const
|
|
{
|
|
if (auto const& data = decoded_image_data())
|
|
return data->intrinsic_height();
|
|
return {};
|
|
}
|
|
|
|
Optional<CSSPixelFraction> ImageProvider::intrinsic_aspect_ratio() const
|
|
{
|
|
if (auto const& data = decoded_image_data())
|
|
return data->intrinsic_aspect_ratio();
|
|
return {};
|
|
}
|
|
|
|
Optional<CSSPixelSize> ImageProvider::intrinsic_size() const
|
|
{
|
|
auto width = intrinsic_width();
|
|
auto height = intrinsic_height();
|
|
if (!width.has_value() || !height.has_value())
|
|
return {};
|
|
|
|
return CSSPixelSize { *width, *height };
|
|
}
|
|
|
|
Optional<Gfx::DecodedImageFrame> ImageProvider::current_image_frame(Optional<Gfx::IntSize> size) const
|
|
{
|
|
if (auto const& data = decoded_image_data())
|
|
return data->current_frame(size.value_or(intrinsic_size().value_or({}).to_type<int>()));
|
|
return {};
|
|
}
|
|
|
|
Optional<Gfx::DecodedImageFrame> ImageProvider::default_image_frame(Optional<Gfx::IntSize> size) const
|
|
{
|
|
if (auto const& data = decoded_image_data())
|
|
return data->default_frame(size.value_or(intrinsic_size().value_or({}).to_type<int>()));
|
|
return {};
|
|
}
|
|
|
|
}
|