ladybird/Libraries/LibWeb/HTML/BitmapDecodedImageData.cpp
Andreas Kling 97986f9739 LibWeb: Stream animated image frames on demand
Add AnimatedDecodedImageData which implements DecodedImageData with
an 8-slot buffer pool instead of storing all frames in memory.
Frames are requested on demand from the ImageDecoder service as
the animation progresses.

For a 344-frame animated image at 1920x1080, this reduces
WebContent memory from ~1.3 GB to ~66 MB.

The streaming class owns frame progression and synchronizes
multiple callers (HTMLImageElement and ImageStyleValue) through
notify_frame_advanced() returning the authoritative frame index.
When a frame isn't in the pool, the last displayed frame is shown
as a fallback (brief freeze rather than blank).

Rename the old AnimatedBitmapDecodedImageData (which now only
handles static/single-frame images) to BitmapDecodedImageData.
2026-02-13 18:34:24 +01:00

72 lines
2.2 KiB
C++

/*
* Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGC/Heap.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImmutableBitmap.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/HTML/BitmapDecodedImageData.h>
#include <LibWeb/Painting/DisplayListRecorder.h>
#include <LibWeb/Painting/DisplayListRecordingContext.h>
namespace Web::HTML {
GC_DEFINE_ALLOCATOR(BitmapDecodedImageData);
ErrorOr<GC::Ref<BitmapDecodedImageData>> BitmapDecodedImageData::create(JS::Realm& realm, Vector<Frame>&& frames, size_t loop_count, bool animated)
{
return realm.create<BitmapDecodedImageData>(move(frames), loop_count, animated);
}
BitmapDecodedImageData::BitmapDecodedImageData(Vector<Frame>&& frames, size_t loop_count, bool animated)
: m_frames(move(frames))
, m_loop_count(loop_count)
, m_animated(animated)
{
}
BitmapDecodedImageData::~BitmapDecodedImageData() = default;
RefPtr<Gfx::ImmutableBitmap> BitmapDecodedImageData::bitmap(size_t frame_index, Gfx::IntSize) const
{
if (frame_index >= m_frames.size())
return nullptr;
return m_frames[frame_index].bitmap;
}
int BitmapDecodedImageData::frame_duration(size_t frame_index) const
{
if (frame_index >= m_frames.size())
return 0;
return m_frames[frame_index].duration;
}
Optional<CSSPixels> BitmapDecodedImageData::intrinsic_width() const
{
return m_frames.first().bitmap->width();
}
Optional<CSSPixels> BitmapDecodedImageData::intrinsic_height() const
{
return m_frames.first().bitmap->height();
}
Optional<CSSPixelFraction> BitmapDecodedImageData::intrinsic_aspect_ratio() const
{
return CSSPixels(m_frames.first().bitmap->width()) / CSSPixels(m_frames.first().bitmap->height());
}
Optional<Gfx::IntRect> BitmapDecodedImageData::frame_rect(size_t frame_index) const
{
return m_frames[frame_index].bitmap->rect();
}
void BitmapDecodedImageData::paint(DisplayListRecordingContext& context, size_t frame_index, Gfx::IntRect dst_rect, Gfx::IntRect clip_rect, Gfx::ScalingMode scaling_mode) const
{
context.display_list_recorder().draw_scaled_immutable_bitmap(dst_rect, clip_rect, *m_frames[frame_index].bitmap, scaling_mode);
}
}