1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:48:12 +00:00

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
4 changed files with 12 additions and 32 deletions

View file

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

View file

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

View file

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

View file

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