dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / animated_decoded_image_data.lucb
#==============================================================================================
#
# animated_decoded_image_data - HTML::AnimatedDecodedImageData: frames streamed by the decoder
#
# DESCRIPTION:
# Port of LibWeb/HTML/AnimatedDecodedImageData.cpp (lines 1-241) and the inline functions
# of AnimatedDecodedImageData.h: the session registry the ImageCodecPlugin delivers
# frames through, a pool of BUFFER_POOL_SIZE decoded frames refilled REQUEST_BATCH_SIZE
# frames at a time, and the DecodedImageData overrides. Decoding is phase 2: nothing
# creates one before the image codec plugin decodes animations.
#
#==============================================================================================
# mark: The session registry ====================================================================
## The registry's map (C++: a function-local static). Its values are raw pointers, removed by
## finalize, so the map does not keep the data alive: its storage lives in the C heap, which the
## collector neither scans nor frees (a module global is not scanned).
var html_animated_decoded_image_data_s_registry: ak.HashMap[i64, HtmlAnimatedDecodedImageData*?, ak.I64Traits]
## Ported from AnimatedDecodedImageData.cpp:20 Web::HTML::AnimatedDecodedImageData::session_registry.
pub func html_animated_decoded_image_data_session_registry() -> ak.HashMap[i64, HtmlAnimatedDecodedImageData*?, ak.I64Traits]*:
return &html_animated_decoded_image_data_s_registry
## The plugin install_frame_delivery_callback installed the delivery in (C++: a function-local
## `static bool installed`). Ladybird installs one ImageCodecPlugin per process, so the flag means
## "in this process's plugin"; here a process may install several one after another (the tests), and
## the next one gets the delivery too.
var html_animated_decoded_image_data_s_installed_in: PlatformImageCodecPlugin*?
## The first lambda of install_frame_delivery_callback: ImageCodecPlugin::on_animation_frames_decoded.
func html_animated_decoded_image_data_on_animation_frames_decoded(context: void*?, session_id: i64, bitmaps: ak.Vector[gfx.Bitmap*]):
html_animated_decoded_image_data_deliver_frames_for_session(session_id, bitmaps)
## The second lambda of install_frame_delivery_callback: ImageCodecPlugin::on_animation_decode_failed.
func html_animated_decoded_image_data_on_animation_decode_failed(context: void*?, session_id: i64):
if let it = ak.hash_map_get_k(html_animated_decoded_image_data_session_registry(), session_id):
if let data = *it:
data.m_request_in_flight = false
## Ported from AnimatedDecodedImageData.cpp:26 Web::HTML::AnimatedDecodedImageData::install_frame_delivery_callback.
pub func html_animated_decoded_image_data_install_frame_delivery_callback():
let plugin = platform_image_codec_plugin_the()
if html_animated_decoded_image_data_s_installed_in == plugin:
return
html_animated_decoded_image_data_s_installed_in = plugin
plugin.on_animation_frames_decoded = ak.Function2[i64, ak.Vector[gfx.Bitmap*], unit](call = html_animated_decoded_image_data_on_animation_frames_decoded, context = none)
plugin.on_animation_decode_failed = ak.Function1[i64, unit](call = html_animated_decoded_image_data_on_animation_decode_failed, context = none)
## Ported from AnimatedDecodedImageData.cpp:45 Web::HTML::AnimatedDecodedImageData::deliver_frames_for_session.
pub func html_animated_decoded_image_data_deliver_frames_for_session(session_id: i64, bitmaps: ak.Vector[gfx.Bitmap*]):
let it = ak.hash_map_get_k(html_animated_decoded_image_data_session_registry(), session_id) else return
if let data = *it:
html_animated_decoded_image_data_receive_frames(data, bitmaps, data.m_last_requested_start_frame)
# mark: Creation ================================================================================
## Ported from AnimatedDecodedImageData.cpp:54 Web::HTML::AnimatedDecodedImageData::create.
pub func html_animated_decoded_image_data_create(realm: Realm*, session_id: i64, frame_count: u32, loop_count: u32, size: gfx.IntSize, color_space: gfx.ColorSpace, durations: ak.Vector[u32], initial_bitmaps: ak.Vector[gfx.Bitmap*]) -> HtmlAnimatedDecodedImageData*:
let data = realm_create_html_animated_decoded_image_data(realm, session_id, frame_count, loop_count, size, color_space, durations)
# Place initial bitmaps into the buffer pool.
let bitmaps = ak.vector_span_const(&initial_bitmaps)
for i: u32 in 0..<u32(bitmaps.length):
let slot = &data.m_buffer_slots[i % html_animated_decoded_image_data_buffer_pool_size]
slot.frame_index = i
slot.bitmap = gfx.immutable_bitmap_create_color_space(bitmaps[i], data.m_color_space)
data.m_write_generation += 1
slot.generation = data.m_write_generation
data.m_highest_requested_frame = u32(bitmaps.length)
if bitmaps.length != 0:
data.m_last_displayed_bitmap = data.m_buffer_slots[0].bitmap
html_animated_decoded_image_data_install_frame_delivery_callback()
with memory.heap:
_ = ak.hash_map_set(html_animated_decoded_image_data_session_registry(), session_id, data)
return data
## Ported from AnimatedDecodedImageData.cpp:86 Web::HTML::AnimatedDecodedImageData::AnimatedDecodedImageData.
pub func html_animated_decoded_image_data_construct(this: HtmlAnimatedDecodedImageData*, session_id: i64, frame_count: u32, loop_count: u32, size: gfx.IntSize, color_space: gfx.ColorSpace, durations: ak.Vector[u32]):
html_decoded_image_data_construct(this.html_decoded_image_data())
this.m_session_id = session_id
this.m_frame_count = frame_count
this.m_loop_count = loop_count
this.m_size = size
this.m_color_space = color_space
this.m_durations = durations
## Ported from AnimatedDecodedImageData.cpp:104 Web::HTML::AnimatedDecodedImageData::finalize (override).
pub func html_animated_decoded_image_data_finalize_impl(base: gc.Cell*):
let this = (HtmlAnimatedDecodedImageData*)base
gc.cell_finalize_impl(base)
with memory.heap:
_ = ak.hash_map_remove_k(html_animated_decoded_image_data_session_registry(), this.m_session_id)
platform_image_codec_plugin_stop_animation_decode(platform_image_codec_plugin_the(), this.m_session_id)
# mark: The buffer pool =========================================================================
## Ported from AnimatedDecodedImageData.cpp:111 Web::HTML::AnimatedDecodedImageData::find_slot.
pub func html_animated_decoded_image_data_find_slot(this: HtmlAnimatedDecodedImageData*, frame_index: u32) -> const HtmlAnimatedDecodedImageDataBufferSlot*?:
for slot in &this.m_buffer_slots:
if slot.frame_index == frame_index and slot.bitmap != none:
return slot
return none
## Ported from AnimatedDecodedImageData.cpp:120 Web::HTML::AnimatedDecodedImageData::evict_oldest_slot.
pub func html_animated_decoded_image_data_evict_oldest_slot(this: HtmlAnimatedDecodedImageData*) -> HtmlAnimatedDecodedImageDataBufferSlot*:
var oldest = &this.m_buffer_slots[0]
for slot in &this.m_buffer_slots:
if slot.generation < oldest.generation:
oldest = slot
return oldest
# mark: DecodedImageData overrides ==============================================================
## Ported from AnimatedDecodedImageData.cpp:130 Web::HTML::AnimatedDecodedImageData::bitmap (override).
pub func html_animated_decoded_image_data_bitmap_impl(base: HtmlDecodedImageData*, frame_index: usize, arg1: gfx.IntSize) -> gfx.ImmutableBitmap*?:
let this = (HtmlAnimatedDecodedImageData*)base
if frame_index >= (usize)this.m_frame_count:
return this.m_last_displayed_bitmap
if let slot = html_animated_decoded_image_data_find_slot(this, (u32)frame_index):
this.m_last_displayed_bitmap = slot.bitmap
return slot.bitmap
# Frame not in pool; return last displayed frame as fallback.
return this.m_last_displayed_bitmap
## Ported from AnimatedDecodedImageData.cpp:144 Web::HTML::AnimatedDecodedImageData::frame_duration (override).
pub func html_animated_decoded_image_data_frame_duration_impl(base: HtmlDecodedImageData*, frame_index: usize) -> i32:
let this = (HtmlAnimatedDecodedImageData*)base
if frame_index >= ak.vector_size(&this.m_durations):
return 0
return (i32)*ak.vector_at(&this.m_durations, frame_index)
## Ported from AnimatedDecodedImageData.h:41 Web::HTML::AnimatedDecodedImageData::frame_count (override).
pub func html_animated_decoded_image_data_frame_count_impl(base: HtmlDecodedImageData*) -> usize:
return (usize)((HtmlAnimatedDecodedImageData*)base).m_frame_count
## Ported from AnimatedDecodedImageData.h:42 Web::HTML::AnimatedDecodedImageData::loop_count (override).
pub func html_animated_decoded_image_data_loop_count_impl(base: HtmlDecodedImageData*) -> usize:
return (usize)((HtmlAnimatedDecodedImageData*)base).m_loop_count
## Ported from AnimatedDecodedImageData.h:43 Web::HTML::AnimatedDecodedImageData::is_animated (override).
pub func html_animated_decoded_image_data_is_animated_impl(base: HtmlDecodedImageData*) -> bool:
return true
## Ported from AnimatedDecodedImageData.cpp:151 Web::HTML::AnimatedDecodedImageData::intrinsic_width (override).
pub func html_animated_decoded_image_data_intrinsic_width_impl(base: HtmlDecodedImageData*) -> gfx.CssPixels?:
let this = (HtmlAnimatedDecodedImageData*)base
return gfx.CssPixels.from_int(gfx.int_size_width(&this.m_size))
## Ported from AnimatedDecodedImageData.cpp:156 Web::HTML::AnimatedDecodedImageData::intrinsic_height (override).
pub func html_animated_decoded_image_data_intrinsic_height_impl(base: HtmlDecodedImageData*) -> gfx.CssPixels?:
let this = (HtmlAnimatedDecodedImageData*)base
return gfx.CssPixels.from_int(gfx.int_size_height(&this.m_size))
## Ported from AnimatedDecodedImageData.cpp:161 Web::HTML::AnimatedDecodedImageData::intrinsic_aspect_ratio (override).
pub func html_animated_decoded_image_data_intrinsic_aspect_ratio_impl(base: HtmlDecodedImageData*) -> gfx.CssPixelFraction?:
let this = (HtmlAnimatedDecodedImageData*)base
return gfx.CssPixels.from_int(gfx.int_size_width(&this.m_size)).div(gfx.CssPixels.from_int(gfx.int_size_height(&this.m_size)))
## Ported from AnimatedDecodedImageData.cpp:166 Web::HTML::AnimatedDecodedImageData::frame_rect (override).
pub func html_animated_decoded_image_data_frame_rect_impl(base: HtmlDecodedImageData*, frame_index: usize) -> gfx.DevicePixelRect?:
let this = (HtmlAnimatedDecodedImageData*)base
return gfx.DevicePixelRect(m_location = gfx.IntPoint(), m_size = this.m_size)
## Ported from AnimatedDecodedImageData.cpp:171 Web::HTML::AnimatedDecodedImageData::paint (override).
pub func html_animated_decoded_image_data_paint_impl(base: HtmlDecodedImageData*, context: DisplayListRecordingContext*, frame_index: usize, dst_rect: gfx.DevicePixelRect, clip_rect: gfx.DevicePixelRect, scaling_mode: gfx.ScalingMode):
let immutable_bitmap = html_animated_decoded_image_data_bitmap_impl(base, frame_index, gfx.IntSize()) else return
display_list.display_list_recorder_draw_scaled_immutable_bitmap(display_list_recording_context_display_list_recorder(context), dst_rect, clip_rect, immutable_bitmap, scaling_mode)
# mark: Receiving frames ========================================================================
## Ported from AnimatedDecodedImageData.cpp:179 Web::HTML::AnimatedDecodedImageData::receive_frames.
pub func html_animated_decoded_image_data_receive_frames(this: HtmlAnimatedDecodedImageData*, bitmaps: ak.Vector[gfx.Bitmap*], start_frame_index: u32):
this.m_request_in_flight = false
let frames = ak.vector_span_const(&bitmaps)
for i: u32 in 0..<u32(frames.length):
let frame_index = start_frame_index +% i
if frame_index >= this.m_frame_count:
break
# Check if this frame is already in the pool.
if html_animated_decoded_image_data_find_slot(this, frame_index) != none:
continue
let slot = html_animated_decoded_image_data_evict_oldest_slot(this)
slot.frame_index = frame_index
slot.bitmap = gfx.immutable_bitmap_create_color_space(frames[i], this.m_color_space)
this.m_write_generation += 1
slot.generation = this.m_write_generation
## Ported from AnimatedDecodedImageData.cpp:199 Web::HTML::AnimatedDecodedImageData::notify_frame_advanced (override).
pub func html_animated_decoded_image_data_notify_frame_advanced_impl(base: HtmlDecodedImageData*, caller_frame_index: usize) -> usize:
let this = (HtmlAnimatedDecodedImageData*)base
# We own the frame progression. Only advance when a caller reports
# the expected next frame (this deduplicates multiple callers per tick).
let expected_next = ((usize)this.m_current_frame_index + 1) % (usize)this.m_frame_count
if caller_frame_index == expected_next:
this.m_current_frame_index = (u32)expected_next
html_animated_decoded_image_data_maybe_request_more_frames(this, (usize)this.m_current_frame_index)
return (usize)this.m_current_frame_index
## Ported from AnimatedDecodedImageData.cpp:211 Web::HTML::AnimatedDecodedImageData::maybe_request_more_frames.
pub func html_animated_decoded_image_data_maybe_request_more_frames(this: HtmlAnimatedDecodedImageData*, current_frame_index: usize):
if this.m_request_in_flight:
return
# Count how many frames ahead of current are in the pool.
var frames_ahead: u32 = 0
for offset: u32 in 1..=html_animated_decoded_image_data_buffer_pool_size:
let future_index = (u32)((current_frame_index + (usize)offset) % (usize)this.m_frame_count)
if html_animated_decoded_image_data_find_slot(this, future_index) != none:
frames_ahead += 1
else:
break
# Request more when buffer is less than half full, giving the decoder
# time to respond while we still have frames to display.
if frames_ahead >= html_animated_decoded_image_data_request_batch_size:
return
# Determine which frame to request from.
let request_start = (u32)((current_frame_index + (usize)frames_ahead + 1) % (usize)this.m_frame_count)
let request_count = html_animated_decoded_image_data_request_batch_size
this.m_request_in_flight = true
this.m_last_requested_start_frame = request_start
this.m_highest_requested_frame = ak.max(this.m_highest_requested_frame, request_start +% request_count)
platform_image_codec_plugin_request_animation_frames(platform_image_codec_plugin_the(), this.m_session_id, request_start, request_count)
## Ported from AnimatedDecodedImageData.h:56 Web::HTML::AnimatedDecodedImageData::session_id.
pub func html_animated_decoded_image_data_session_id(this: HtmlAnimatedDecodedImageData*) -> i64:
return this.m_session_id