2024-04-04 21:48:58 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2024, Lucas Chollet <lucas.chollet@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2026-05-24 09:38:33 +02:00
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2024-04-04 21:48:58 -04:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2026-05-06 00:34:52 +02:00
|
|
|
#include <LibJS/Runtime/ExternalMemory.h>
|
2026-04-18 10:54:06 +02:00
|
|
|
#include <LibWeb/Bindings/ImageBitmap.h>
|
2024-04-04 21:48:58 -04:00
|
|
|
#include <LibWeb/HTML/ImageBitmap.h>
|
2025-07-14 21:32:04 +02:00
|
|
|
#include <LibWeb/HTML/StructuredSerialize.h>
|
|
|
|
|
#include <LibWeb/WebIDL/DOMException.h>
|
|
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
2024-04-04 21:48:58 -04:00
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(ImageBitmap);
|
2024-04-04 21:48:58 -04:00
|
|
|
|
2026-05-24 09:38:33 +02:00
|
|
|
[[nodiscard]] static WebIDL::ExceptionOr<NonnullRefPtr<Gfx::Bitmap>> create_bitmap_from_bitmap_data(JS::Realm& realm, Gfx::BitmapFormat const format, Gfx::AlphaType const alpha_type, int const width, int const height, size_t const pitch, ByteBuffer data)
|
2025-07-14 21:32:04 +02:00
|
|
|
{
|
2026-05-24 09:38:33 +02:00
|
|
|
auto bitmap_data = TRY_OR_THROW_OOM(realm.vm(), try_make<ByteBuffer>(move(data)));
|
|
|
|
|
auto* pixels = bitmap_data->data();
|
|
|
|
|
return TRY_OR_THROW_OOM(realm.vm(), Gfx::Bitmap::create_wrapper(format, alpha_type, Gfx::IntSize(width, height), pitch, pixels, [bitmap_data = move(bitmap_data)] { }));
|
2025-07-14 21:32:04 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-20 18:29:06 +01:00
|
|
|
static void serialize_bitmap(HTML::TransferDataEncoder& encoder, RefPtr<Gfx::Bitmap> const& bitmap)
|
2025-07-14 21:32:04 +02:00
|
|
|
{
|
2025-07-20 18:29:06 +01:00
|
|
|
if (!bitmap) {
|
|
|
|
|
encoder.encode(0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
encoder.encode(bitmap->width());
|
|
|
|
|
encoder.encode(bitmap->height());
|
|
|
|
|
encoder.encode(bitmap->pitch());
|
|
|
|
|
encoder.encode(bitmap->format());
|
|
|
|
|
encoder.encode(bitmap->alpha_type());
|
|
|
|
|
encoder.encode(ReadonlyBytes { bitmap->scanline_u8(0), bitmap->data_size() });
|
2025-07-14 21:32:04 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-20 18:29:06 +01:00
|
|
|
[[nodiscard]] static WebIDL::ExceptionOr<RefPtr<Gfx::Bitmap>> deserialize_bitmap(JS::Realm& realm, HTML::TransferDataDecoder& decoder)
|
2025-07-14 21:32:04 +02:00
|
|
|
{
|
|
|
|
|
auto const width = decoder.decode<int>();
|
2025-07-20 18:29:06 +01:00
|
|
|
if (width == 0)
|
|
|
|
|
return nullptr;
|
2025-07-14 21:32:04 +02:00
|
|
|
auto const height = decoder.decode<int>();
|
|
|
|
|
auto const pitch = decoder.decode<size_t>();
|
|
|
|
|
auto const format = decoder.decode<Gfx::BitmapFormat>();
|
|
|
|
|
auto const alpha_type = decoder.decode<Gfx::AlphaType>();
|
2026-05-24 09:38:33 +02:00
|
|
|
auto data = TRY(decoder.decode_buffer(realm));
|
|
|
|
|
return TRY(create_bitmap_from_bitmap_data(realm, format, alpha_type, width, height, pitch, move(data)));
|
2025-07-14 21:32:04 +02:00
|
|
|
}
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<ImageBitmap> ImageBitmap::create(JS::Realm& realm)
|
2024-04-04 21:48:58 -04:00
|
|
|
{
|
2024-11-14 05:50:17 +13:00
|
|
|
return realm.create<ImageBitmap>(realm);
|
2024-04-04 21:48:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ImageBitmap::ImageBitmap(JS::Realm& realm)
|
|
|
|
|
: Bindings::PlatformObject(realm)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-29 10:16:38 +01:00
|
|
|
ImageBitmap::~ImageBitmap() = default;
|
|
|
|
|
|
2024-04-04 21:48:58 -04:00
|
|
|
void ImageBitmap::initialize(JS::Realm& realm)
|
|
|
|
|
{
|
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(ImageBitmap);
|
2025-04-20 16:22:57 +02:00
|
|
|
Base::initialize(realm);
|
2024-04-04 21:48:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageBitmap::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 00:34:52 +02:00
|
|
|
size_t ImageBitmap::external_memory_size() const
|
|
|
|
|
{
|
|
|
|
|
auto size = Base::external_memory_size();
|
|
|
|
|
if (m_bitmap)
|
|
|
|
|
size = JS::saturating_add_external_memory_size(size, m_bitmap->data_size());
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-14 21:32:04 +02:00
|
|
|
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#the-imagebitmap-interface:serialization-steps
|
|
|
|
|
WebIDL::ExceptionOr<void> ImageBitmap::serialization_steps(HTML::TransferDataEncoder& serialized, bool, HTML::SerializationMemory&)
|
2024-04-04 21:48:58 -04:00
|
|
|
{
|
2025-07-14 21:32:04 +02:00
|
|
|
// FIXME: 1. If value's origin-clean flag is not set, then throw a "DataCloneError" DOMException.
|
|
|
|
|
|
|
|
|
|
// 2. Set serialized.[[BitmapData]] to a copy of value's bitmap data.
|
2025-07-20 18:29:06 +01:00
|
|
|
serialize_bitmap(serialized, m_bitmap);
|
2025-07-14 21:32:04 +02:00
|
|
|
|
2024-04-04 21:48:58 -04:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-14 21:32:04 +02:00
|
|
|
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#the-imagebitmap-interface:deserialization-steps
|
|
|
|
|
WebIDL::ExceptionOr<void> ImageBitmap::deserialization_steps(HTML::TransferDataDecoder& serialized, HTML::DeserializationMemory&)
|
2024-04-04 21:48:58 -04:00
|
|
|
{
|
2025-07-14 21:32:04 +02:00
|
|
|
// 1. Set value's bitmap data to serialized.[[BitmapData]].
|
2025-10-28 16:45:41 +00:00
|
|
|
set_bitmap(TRY(deserialize_bitmap(this->realm(), serialized)));
|
2025-07-14 21:32:04 +02:00
|
|
|
|
2024-04-04 21:48:58 -04:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 08:07:15 +02:00
|
|
|
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#the-imagebitmap-interface:transfer-steps
|
|
|
|
|
WebIDL::ExceptionOr<void> ImageBitmap::transfer_steps(HTML::TransferDataEncoder& data_holder)
|
2024-04-04 21:48:58 -04:00
|
|
|
{
|
2025-07-19 08:07:15 +02:00
|
|
|
// FIXME: 1. If value's origin-clean flag is not set, then throw a "DataCloneError" DOMException.
|
|
|
|
|
|
|
|
|
|
// 2. Set dataHolder.[[BitmapData]] to value's bitmap data.
|
2025-07-20 18:29:06 +01:00
|
|
|
serialize_bitmap(data_holder, m_bitmap);
|
2025-07-19 08:07:15 +02:00
|
|
|
|
|
|
|
|
// 3. Unset value's bitmap data.
|
|
|
|
|
m_bitmap = nullptr;
|
|
|
|
|
|
2024-04-04 21:48:58 -04:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 08:07:15 +02:00
|
|
|
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#the-imagebitmap-interface:transfer-receiving-steps
|
|
|
|
|
WebIDL::ExceptionOr<void> ImageBitmap::transfer_receiving_steps(HTML::TransferDataDecoder& data_holder)
|
2024-04-04 21:48:58 -04:00
|
|
|
{
|
2025-07-19 08:07:15 +02:00
|
|
|
// 1. Set value's bitmap data to dataHolder.[[BitmapData]].
|
2025-10-28 16:45:41 +00:00
|
|
|
set_bitmap(TRY(deserialize_bitmap(this->realm(), data_holder)));
|
2025-07-19 08:07:15 +02:00
|
|
|
|
2024-04-04 21:48:58 -04:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HTML::TransferType ImageBitmap::primary_interface() const
|
|
|
|
|
{
|
2025-07-19 08:07:15 +02:00
|
|
|
return TransferType::ImageBitmap;
|
2024-04-04 21:48:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#dom-imagebitmap-width
|
|
|
|
|
WebIDL::UnsignedLong ImageBitmap::width() const
|
|
|
|
|
{
|
|
|
|
|
// 1. If this's [[Detached]] internal slot's value is true, then return 0.
|
|
|
|
|
if (is_detached())
|
|
|
|
|
return 0;
|
|
|
|
|
// 2. Return this's width, in CSS pixels.
|
|
|
|
|
return m_width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#dom-imagebitmap-height
|
|
|
|
|
WebIDL::UnsignedLong ImageBitmap::height() const
|
|
|
|
|
{
|
|
|
|
|
// 1. If this's [[Detached]] internal slot's value is true, then return 0.
|
|
|
|
|
if (is_detached())
|
|
|
|
|
return 0;
|
|
|
|
|
// 2. Return this's height, in CSS pixels.
|
|
|
|
|
return m_height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#dom-imagebitmap-close
|
|
|
|
|
void ImageBitmap::close()
|
|
|
|
|
{
|
|
|
|
|
// 1. Set this's [[Detached]] internal slot value to true.
|
|
|
|
|
set_detached(true);
|
|
|
|
|
|
|
|
|
|
// 2. Unset this's bitmap data.
|
|
|
|
|
m_bitmap = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageBitmap::set_bitmap(RefPtr<Gfx::Bitmap> bitmap)
|
|
|
|
|
{
|
|
|
|
|
m_bitmap = move(bitmap);
|
2025-07-20 04:14:43 +01:00
|
|
|
m_width = m_bitmap ? m_bitmap->width() : 0;
|
|
|
|
|
m_height = m_bitmap ? m_bitmap->height() : 0;
|
2024-04-04 21:48:58 -04:00
|
|
|
}
|
|
|
|
|
|
2024-04-05 09:15:45 -04:00
|
|
|
Gfx::Bitmap* ImageBitmap::bitmap() const
|
|
|
|
|
{
|
|
|
|
|
return m_bitmap.ptr();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-04 21:48:58 -04:00
|
|
|
}
|