2020-06-02 20:27:26 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-02 20:27:26 +02:00
|
|
|
*/
|
|
|
|
|
|
2022-04-30 11:21:21 +02:00
|
|
|
#include <AK/Function.h>
|
2020-06-02 20:27:26 +02:00
|
|
|
#include <LibGfx/Bitmap.h>
|
|
|
|
|
#include <LibWeb/Loader/ImageResource.h>
|
2022-09-16 15:01:47 +02:00
|
|
|
#include <LibWeb/Platform/ImageCodecPlugin.h>
|
2020-06-02 20:27:26 +02:00
|
|
|
|
|
|
|
|
namespace Web {
|
|
|
|
|
|
2022-03-23 08:20:41 -04:00
|
|
|
NonnullRefPtr<ImageResource> ImageResource::convert_from_resource(Resource& resource)
|
|
|
|
|
{
|
|
|
|
|
return adopt_ref(*new ImageResource(resource));
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ImageResource::ImageResource(LoadRequest const& request)
|
2020-06-02 20:27:26 +02:00
|
|
|
: Resource(Type::Image, request)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 08:20:41 -04:00
|
|
|
ImageResource::ImageResource(Resource& resource)
|
|
|
|
|
: Resource(Type::Image, resource)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
ImageResource::~ImageResource() = default;
|
2020-06-02 20:27:26 +02:00
|
|
|
|
2021-01-29 22:30:48 +01:00
|
|
|
int ImageResource::frame_duration(size_t frame_index) const
|
2020-06-22 21:41:10 +02:00
|
|
|
{
|
2021-01-29 22:30:48 +01:00
|
|
|
decode_if_needed();
|
|
|
|
|
if (frame_index >= m_decoded_frames.size())
|
|
|
|
|
return 0;
|
|
|
|
|
return m_decoded_frames[frame_index].duration;
|
2020-06-22 21:41:10 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-29 22:30:48 +01:00
|
|
|
void ImageResource::decode_if_needed() const
|
2020-06-02 20:27:26 +02:00
|
|
|
{
|
2021-01-29 22:30:48 +01:00
|
|
|
if (!has_encoded_data())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (m_has_attempted_decode)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!m_decoded_frames.is_empty())
|
|
|
|
|
return;
|
|
|
|
|
|
2022-09-16 15:01:47 +02:00
|
|
|
auto image = Platform::ImageCodecPlugin::the().decode_image(encoded_data());
|
2022-11-19 15:06:04 +01:00
|
|
|
m_has_attempted_decode = true;
|
2021-02-20 11:35:00 +01:00
|
|
|
|
2022-11-19 15:06:04 +01:00
|
|
|
if (!image.has_value()) {
|
|
|
|
|
dbgln("Could not decode image resource {}", url());
|
|
|
|
|
return;
|
2021-01-29 22:30:48 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-19 15:06:04 +01:00
|
|
|
m_loop_count = image.value().loop_count;
|
|
|
|
|
m_animated = image.value().is_animated;
|
|
|
|
|
m_decoded_frames.resize(image.value().frames.size());
|
|
|
|
|
for (size_t i = 0; i < m_decoded_frames.size(); ++i) {
|
|
|
|
|
auto& frame = m_decoded_frames[i];
|
|
|
|
|
frame.bitmap = image.value().frames[i].bitmap;
|
|
|
|
|
frame.duration = image.value().frames[i].duration;
|
|
|
|
|
}
|
2020-06-02 20:27:26 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Gfx::Bitmap const* ImageResource::bitmap(size_t frame_index) const
|
2020-06-22 21:41:10 +02:00
|
|
|
{
|
2021-01-29 22:30:48 +01:00
|
|
|
decode_if_needed();
|
|
|
|
|
if (frame_index >= m_decoded_frames.size())
|
2020-06-22 21:41:10 +02:00
|
|
|
return nullptr;
|
2021-01-29 22:30:48 +01:00
|
|
|
return m_decoded_frames[frame_index].bitmap;
|
2020-06-22 21:41:10 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-02 20:27:26 +02:00
|
|
|
void ImageResource::update_volatility()
|
|
|
|
|
{
|
|
|
|
|
bool visible_in_viewport = false;
|
|
|
|
|
for_each_client([&](auto& client) {
|
|
|
|
|
if (static_cast<const ImageResourceClient&>(client).is_visible_in_viewport())
|
|
|
|
|
visible_in_viewport = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!visible_in_viewport) {
|
2021-01-29 22:30:48 +01:00
|
|
|
for (auto& frame : m_decoded_frames) {
|
|
|
|
|
if (frame.bitmap)
|
|
|
|
|
frame.bitmap->set_volatile();
|
|
|
|
|
}
|
2020-06-02 20:27:26 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-29 22:30:48 +01:00
|
|
|
bool still_has_decoded_image = true;
|
|
|
|
|
for (auto& frame : m_decoded_frames) {
|
|
|
|
|
if (!frame.bitmap) {
|
|
|
|
|
still_has_decoded_image = false;
|
|
|
|
|
} else {
|
2021-07-24 22:49:48 +02:00
|
|
|
bool was_purged = false;
|
|
|
|
|
bool bitmap_has_memory = frame.bitmap->set_nonvolatile(was_purged);
|
|
|
|
|
if (!bitmap_has_memory || was_purged)
|
2021-01-29 22:30:48 +01:00
|
|
|
still_has_decoded_image = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-02 20:27:26 +02:00
|
|
|
if (still_has_decoded_image)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-01-29 22:30:48 +01:00
|
|
|
m_decoded_frames.clear();
|
|
|
|
|
m_has_attempted_decode = false;
|
2020-06-02 20:27:26 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
ImageResourceClient::~ImageResourceClient() = default;
|
2020-06-02 20:27:26 +02:00
|
|
|
|
|
|
|
|
}
|