LibWeb: Remove Gfx::ImageDecoder from ImageLoader

We still use a Gfx::ImageDecoder for GIF images, but there's no need
for the ImageLoader object to have its own pointer to it. Just grab
the ImageDecoder from the ImageResource when needed.
This commit is contained in:
Andreas Kling 2020-06-23 13:48:32 +02:00
parent 9ef5d46277
commit fbd760379a
Notes: sideshowbarker 2024-07-19 05:25:58 +09:00
4 changed files with 12 additions and 32 deletions

View File

@ -76,10 +76,10 @@ void ImageLoader::resource_did_load()
#endif
if (resource()->should_decode_in_process()) {
m_decoder = resource()->ensure_decoder();
auto& decoder = resource()->ensure_decoder();
if (m_decoder->is_animated() && m_decoder->frame_count() > 1) {
const auto& first_frame = m_decoder->frame(0);
if (decoder.is_animated() && decoder.frame_count() > 1) {
const auto& first_frame = decoder.frame(0);
m_timer->set_interval(first_frame.duration);
m_timer->on_timeout = [this] { animate(); };
m_timer->start();
@ -95,19 +95,18 @@ void ImageLoader::animate()
if (!m_visible_in_viewport)
return;
auto* decoder = image_decoder();
ASSERT(decoder);
auto& decoder = resource()->ensure_decoder();
m_current_frame_index = (m_current_frame_index + 1) % decoder->frame_count();
const auto& current_frame = decoder->frame(m_current_frame_index);
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) {
if (m_current_frame_index == decoder.frame_count() - 1) {
++m_loops_completed;
if (m_loops_completed > 0 && m_loops_completed == decoder->loop_count()) {
if (m_loops_completed > 0 && m_loops_completed == decoder.loop_count()) {
m_timer->stop();
}
}
@ -123,19 +122,12 @@ void ImageLoader::resource_did_fail()
on_fail();
}
void ImageLoader::resource_did_replace_decoder()
{
if (resource()->should_decode_in_process()) {
m_decoder = resource()->ensure_decoder();
}
}
bool ImageLoader::has_image() const
{
if (!resource())
return false;
if (resource()->should_decode_in_process())
return image_decoder();
return const_cast<ImageResource*>(resource())->ensure_decoder().bitmap();
return true;
}
@ -144,7 +136,7 @@ unsigned ImageLoader::width() const
if (!resource())
return 0;
if (resource()->should_decode_in_process())
return image_decoder() ? image_decoder()->width() : 0;
return const_cast<ImageResource*>(resource())->ensure_decoder().width();
return bitmap() ? bitmap()->width() : 0;
}
@ -153,7 +145,7 @@ unsigned ImageLoader::height() const
if (!resource())
return 0;
if (resource()->should_decode_in_process())
return image_decoder() ? image_decoder()->height() : 0;
return const_cast<ImageResource*>(resource())->ensure_decoder().height();
return bitmap() ? bitmap()->height() : 0;
}
@ -161,12 +153,7 @@ const Gfx::Bitmap* ImageLoader::bitmap() const
{
if (!resource())
return nullptr;
return resource()->bitmap();
}
const Gfx::ImageDecoder* ImageLoader::image_decoder() const
{
return m_decoder;
return resource()->bitmap(m_current_frame_index);
}
}

View File

@ -39,7 +39,6 @@ public:
void load(const URL&);
const Gfx::Bitmap* bitmap() const;
const Gfx::ImageDecoder* image_decoder() const;
bool has_image() const;
@ -56,12 +55,10 @@ private:
// ^ImageResourceClient
virtual void resource_did_load() override;
virtual void resource_did_fail() override;
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;
mutable bool m_visible_in_viewport { false };
size_t m_current_frame_index { 0 };

View File

@ -94,9 +94,6 @@ void ImageResource::update_volatility()
return;
m_decoder = nullptr;
for_each_client([&](auto& client) {
static_cast<ImageResourceClient&>(client).resource_did_replace_decoder();
});
}
ImageResourceClient::~ImageResourceClient()

View File

@ -54,7 +54,6 @@ public:
virtual ~ImageResourceClient();
virtual bool is_visible_in_viewport() const { return false; }
virtual void resource_did_replace_decoder() {}
protected:
ImageResource* resource() { return static_cast<ImageResource*>(ResourceClient::resource()); }