LibWeb: Use an IIFE to determine a VideoPaintable's representation

This commit is contained in:
Zaggy1024 2025-09-12 17:36:33 -05:00 committed by Jelle Raaijmakers
commit 861c10f37f
Notes: github-actions[bot] 2025-09-16 08:27:25 +00:00

View file

@ -74,8 +74,7 @@ void VideoPaintable::paint(DisplayListRecordingContext& context, PaintPhase phas
auto current_playback_position = video_element.current_playback_position(); auto current_playback_position = video_element.current_playback_position();
auto ready_state = video_element.ready_state(); auto ready_state = video_element.ready_state();
enum class Representation { enum class Representation : u8 {
Unknown,
FirstVideoFrame, FirstVideoFrame,
CurrentVideoFrame, CurrentVideoFrame,
LastRenderedVideoFrame, LastRenderedVideoFrame,
@ -83,50 +82,49 @@ void VideoPaintable::paint(DisplayListRecordingContext& context, PaintPhase phas
TransparentBlack, TransparentBlack,
}; };
auto representation = Representation::Unknown; auto representation = [&]() {
// https://html.spec.whatwg.org/multipage/media.html#the-video-element:the-video-element-7
// A video element represents what is given for the first matching condition in the list below:
// https://html.spec.whatwg.org/multipage/media.html#the-video-element:the-video-element-7 // -> When no video data is available (the element's readyState attribute is either HAVE_NOTHING, or HAVE_METADATA
// A video element represents what is given for the first matching condition in the list below: // but no video data has yet been obtained at all, or the element's readyState attribute is any subsequent value
// but the media resource does not have a video channel)
if (ready_state == HTML::HTMLMediaElement::ReadyState::HaveNothing
|| (ready_state >= HTML::HTMLMediaElement::ReadyState::HaveMetadata && video_element.video_tracks()->length() == 0)) {
// The video element represents its poster frame, if any, or else transparent black with no intrinsic dimensions.
return poster_frame ? Representation::PosterFrame : Representation::TransparentBlack;
}
// -> When no video data is available (the element's readyState attribute is either HAVE_NOTHING, or HAVE_METADATA // -> When the video element is paused, the current playback position is the first frame of video, and the element's
// but no video data has yet been obtained at all, or the element's readyState attribute is any subsequent value // show poster flag is set
// but the media resource does not have a video channel) if (video_element.paused() && current_playback_position == 0 && video_element.show_poster()) {
if (ready_state == HTML::HTMLMediaElement::ReadyState::HaveNothing // The video element represents its poster frame, if any, or else the first frame of the video.
|| (ready_state >= HTML::HTMLMediaElement::ReadyState::HaveMetadata && video_element.video_tracks()->length() == 0)) { return poster_frame ? Representation::PosterFrame : Representation::FirstVideoFrame;
// The video element represents its poster frame, if any, or else transparent black with no intrinsic dimensions. }
representation = poster_frame ? Representation::PosterFrame : Representation::TransparentBlack;
}
// -> When the video element is paused, the current playback position is the first frame of video, and the element's // -> When the video element is paused, and the frame of video corresponding to the current playback position
// show poster flag is set // is not available (e.g. because the video is seeking or buffering)
else if (video_element.paused() && current_playback_position == 0 && video_element.show_poster()) { // -> When the video element is neither potentially playing nor paused (e.g. when seeking or stalled)
// The video element represents its poster frame, if any, or else the first frame of the video. if (
representation = poster_frame ? Representation::PosterFrame : Representation::FirstVideoFrame; (video_element.paused() && current_playback_position != current_frame.position)
} || (!video_element.potentially_playing() && !video_element.paused())) {
// The video element represents the last frame of the video to have been rendered.
return Representation::LastRenderedVideoFrame;
}
// -> When the video element is paused, and the frame of video corresponding to the current playback position // -> When the video element is paused
// is not available (e.g. because the video is seeking or buffering) if (video_element.paused()) {
// -> When the video element is neither potentially playing nor paused (e.g. when seeking or stalled) // The video element represents the frame of video corresponding to the current playback position.
else if ( return Representation::CurrentVideoFrame;
(video_element.paused() && current_playback_position != current_frame.position) }
|| (!video_element.potentially_playing() && !video_element.paused())) {
// The video element represents the last frame of the video to have been rendered.
representation = Representation::LastRenderedVideoFrame;
}
// -> When the video element is paused // -> Otherwise (the video element has a video channel and is potentially playing)
else if (video_element.paused()) { //
// The video element represents the frame of video corresponding to the current playback position. // The video element represents the frame of video at the continuously increasing "current" position. When the
representation = Representation::CurrentVideoFrame; // current playback position changes such that the last frame rendered is no longer the frame corresponding to
} // the current playback position in the video, the new frame must be rendered.
return Representation::CurrentVideoFrame;
// -> Otherwise (the video element has a video channel and is potentially playing) }();
else {
// The video element represents the frame of video at the continuously increasing "current" position. When the
// current playback position changes such that the last frame rendered is no longer the frame corresponding to
// the current playback position in the video, the new frame must be rendered.
representation = Representation::CurrentVideoFrame;
}
auto paint_frame = [&](auto const& frame) { auto paint_frame = [&](auto const& frame) {
auto scaling_mode = to_gfx_scaling_mode(computed_values().image_rendering(), frame->rect(), video_rect.to_type<int>()); auto scaling_mode = to_gfx_scaling_mode(computed_values().image_rendering(), frame->rect(), video_rect.to_type<int>());
@ -173,9 +171,6 @@ void VideoPaintable::paint(DisplayListRecordingContext& context, PaintPhase phas
if (paint_user_agent_controls) if (paint_user_agent_controls)
paint_placeholder_video_controls(context, video_rect, mouse_position); paint_placeholder_video_controls(context, video_rect, mouse_position);
break; break;
case Representation::Unknown:
VERIFY_NOT_REACHED();
} }
} }