1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:17:36 +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

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibCore/Timer.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h>
#include <LibWeb/Loader/ImageLoader.h>
@ -32,6 +33,7 @@
namespace Web {
ImageLoader::ImageLoader()
: m_timer(Core::Timer::construct())
{
}
@ -72,10 +74,40 @@ void ImageLoader::resource_did_load()
}
m_decoder = resource()->ensure_decoder();
if (m_decoder->is_animated() && m_decoder->frame_count() > 1) {
const auto& first_frame = m_decoder->frame(0);
m_timer->set_interval(first_frame.duration);
m_timer->on_timeout = [this] { animate(); };
m_timer->start();
}
if (on_load)
on_load();
}
void ImageLoader::animate()
{
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();
}
}
if (on_animate)
on_animate();
}
void ImageLoader::resource_did_fail()
{
dbg() << "ImageLoader: Resource did fail. URL: " << resource()->url();
@ -90,7 +122,11 @@ void ImageLoader::resource_did_replace_decoder()
const Gfx::Bitmap* ImageLoader::bitmap() const
{
return m_decoder ? m_decoder->bitmap() : nullptr;
if (!m_decoder)
return nullptr;
if (m_decoder->is_animated())
return m_decoder->frame(m_current_frame_index).image;
return m_decoder->bitmap();
}
const Gfx::ImageDecoder* ImageLoader::image_decoder() const

View file

@ -27,6 +27,7 @@
#pragma once
#include <AK/Function.h>
#include <LibCore/Timer.h>
#include <LibWeb/Loader/ImageResource.h>
namespace Web {
@ -44,6 +45,7 @@ public:
Function<void()> on_load;
Function<void()> on_fail;
Function<void()> on_animate;
private:
// ^ImageResourceClient
@ -52,8 +54,14 @@ private:
virtual void resource_did_replace_decoder() override;
virtual bool is_visible_in_viewport() const override { return m_visible_in_viewport; }
void animate();
RefPtr<Gfx::ImageDecoder> m_decoder;
bool m_visible_in_viewport { false };
size_t m_current_frame_index { 0 };
size_t m_loops_completed { 0 };
NonnullRefPtr<Core::Timer> m_timer;
};
}