1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-15 07:04:59 +00:00

LibWeb: Move bitmap animation from HTMLImageElement to ImageLoader

Since ImageLoader manages the image decoder anyway, let it manage
animation as well.
This commit is contained in:
Andreas Kling 2020-06-14 19:26:25 +02:00
parent 63b1c8e882
commit c45615128b
4 changed files with 51 additions and 44 deletions

View file

@ -38,15 +38,8 @@ namespace Web {
HTMLImageElement::HTMLImageElement(Document& document, const FlyString& tag_name)
: HTMLElement(document, tag_name)
, m_timer(Core::Timer::construct())
{
m_image_loader.on_load = [this] {
if (image_decoder() && image_decoder()->is_animated() && image_decoder()->frame_count() > 1) {
const auto& first_frame = image_decoder()->frame(0);
m_timer->set_interval(first_frame.duration);
m_timer->on_timeout = [this] { animate(); };
m_timer->start();
}
this->document().update_layout();
dispatch_event(Event::create("load"));
};
@ -56,6 +49,11 @@ HTMLImageElement::HTMLImageElement(Document& document, const FlyString& tag_name
this->document().update_layout();
dispatch_event(Event::create("error"));
};
m_image_loader.on_animate = [this] {
if (layout_node())
layout_node()->set_needs_display();
};
}
HTMLImageElement::~HTMLImageElement()
@ -70,31 +68,6 @@ void HTMLImageElement::parse_attribute(const FlyString& name, const String& valu
m_image_loader.load(document().complete_url(value));
}
void HTMLImageElement::animate()
{
if (!layout_node())
return;
auto* decoder = image_decoder();
ASSERT(decoder);
m_current_frame_index = (m_current_frame_index + 1) % decoder->frame_count();
const auto& current_frame = decoder->frame(m_current_frame_index);
if (current_frame.duration != m_timer->interval()) {
m_timer->restart(current_frame.duration);
}
if (m_current_frame_index == decoder->frame_count() - 1) {
++m_loops_completed;
if (m_loops_completed > 0 && m_loops_completed == decoder->loop_count()) {
m_timer->stop();
}
}
layout_node()->set_needs_display();
}
RefPtr<LayoutNode> HTMLImageElement::create_layout_node(const StyleProperties* parent_style) const
{
auto style = document().style_resolver().resolve_style(*this, parent_style);
@ -111,12 +84,7 @@ const Gfx::ImageDecoder* HTMLImageElement::image_decoder() const
const Gfx::Bitmap* HTMLImageElement::bitmap() const
{
auto* decoder = image_decoder();
if (!decoder)
return nullptr;
if (decoder->is_animated())
return decoder->frame(m_current_frame_index).image;
return decoder->bitmap();
return m_image_loader.bitmap();
}
void HTMLImageElement::set_visible_in_viewport(Badge<LayoutDocument>, bool visible_in_viewport)