VideoPaintable.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Array.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/HTMLMediaElement.h>
  9. #include <LibWeb/HTML/HTMLVideoElement.h>
  10. #include <LibWeb/HTML/VideoTrackList.h>
  11. #include <LibWeb/Layout/VideoBox.h>
  12. #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
  13. #include <LibWeb/Painting/VideoPaintable.h>
  14. namespace Web::Painting {
  15. static constexpr auto control_box_color = Gfx::Color::from_rgb(0x26'26'26);
  16. static constexpr auto control_highlight_color = Gfx::Color::from_rgb(0x1d'99'f3);
  17. GC_DEFINE_ALLOCATOR(VideoPaintable);
  18. static constexpr Gfx::Color control_button_color(bool is_hovered)
  19. {
  20. if (!is_hovered)
  21. return Color::White;
  22. return control_highlight_color;
  23. }
  24. GC::Ref<VideoPaintable> VideoPaintable::create(Layout::VideoBox const& layout_box)
  25. {
  26. return layout_box.heap().allocate<VideoPaintable>(layout_box);
  27. }
  28. VideoPaintable::VideoPaintable(Layout::VideoBox const& layout_box)
  29. : MediaPaintable(layout_box)
  30. {
  31. }
  32. Layout::VideoBox& VideoPaintable::layout_box()
  33. {
  34. return static_cast<Layout::VideoBox&>(layout_node());
  35. }
  36. Layout::VideoBox const& VideoPaintable::layout_box() const
  37. {
  38. return static_cast<Layout::VideoBox const&>(layout_node());
  39. }
  40. void VideoPaintable::paint(PaintContext& context, PaintPhase phase) const
  41. {
  42. if (!is_visible())
  43. return;
  44. Base::paint(context, phase);
  45. if (phase != PaintPhase::Foreground)
  46. return;
  47. DisplayListRecorderStateSaver saver { context.display_list_recorder() };
  48. auto video_rect = context.rounded_device_rect(absolute_rect());
  49. context.display_list_recorder().add_clip_rect(video_rect.to_type<int>());
  50. ScopedCornerRadiusClip corner_clip { context, video_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) };
  51. auto const& video_element = layout_box().dom_node();
  52. auto mouse_position = MediaPaintable::mouse_position(context, video_element);
  53. auto const& current_frame = video_element.current_frame();
  54. auto const& poster_frame = video_element.poster_frame();
  55. auto current_playback_position = video_element.current_playback_position();
  56. auto ready_state = video_element.ready_state();
  57. enum class Representation {
  58. Unknown,
  59. FirstVideoFrame,
  60. CurrentVideoFrame,
  61. LastRenderedVideoFrame,
  62. PosterFrame,
  63. TransparentBlack,
  64. };
  65. auto representation = Representation::Unknown;
  66. // https://html.spec.whatwg.org/multipage/media.html#the-video-element:the-video-element-7
  67. // A video element represents what is given for the first matching condition in the list below:
  68. // -> When no video data is available (the element's readyState attribute is either HAVE_NOTHING, or HAVE_METADATA
  69. // but no video data has yet been obtained at all, or the element's readyState attribute is any subsequent value
  70. // but the media resource does not have a video channel)
  71. if (ready_state == HTML::HTMLMediaElement::ReadyState::HaveNothing
  72. || (ready_state >= HTML::HTMLMediaElement::ReadyState::HaveMetadata && video_element.video_tracks()->length() == 0)) {
  73. // The video element represents its poster frame, if any, or else transparent black with no intrinsic dimensions.
  74. representation = poster_frame ? Representation::PosterFrame : Representation::TransparentBlack;
  75. }
  76. // -> When the video element is paused, the current playback position is the first frame of video, and the element's
  77. // show poster flag is set
  78. else if (video_element.paused() && current_playback_position == 0 && video_element.show_poster()) {
  79. // The video element represents its poster frame, if any, or else the first frame of the video.
  80. representation = poster_frame ? Representation::PosterFrame : Representation::FirstVideoFrame;
  81. }
  82. // -> When the video element is paused, and the frame of video corresponding to the current playback position
  83. // is not available (e.g. because the video is seeking or buffering)
  84. // -> When the video element is neither potentially playing nor paused (e.g. when seeking or stalled)
  85. else if (
  86. (video_element.paused() && current_playback_position != current_frame.position)
  87. || (!video_element.potentially_playing() && !video_element.paused())) {
  88. // The video element represents the last frame of the video to have been rendered.
  89. representation = Representation::LastRenderedVideoFrame;
  90. }
  91. // -> When the video element is paused
  92. else if (video_element.paused()) {
  93. // The video element represents the frame of video corresponding to the current playback position.
  94. representation = Representation::CurrentVideoFrame;
  95. }
  96. // -> Otherwise (the video element has a video channel and is potentially playing)
  97. else {
  98. // The video element represents the frame of video at the continuously increasing "current" position. When the
  99. // current playback position changes such that the last frame rendered is no longer the frame corresponding to
  100. // the current playback position in the video, the new frame must be rendered.
  101. representation = Representation::CurrentVideoFrame;
  102. }
  103. auto paint_frame = [&](auto const& frame) {
  104. auto scaling_mode = to_gfx_scaling_mode(computed_values().image_rendering(), frame->rect(), video_rect.to_type<int>());
  105. context.display_list_recorder().draw_scaled_immutable_bitmap(video_rect.to_type<int>(), Gfx::ImmutableBitmap::create(*frame), frame->rect(), scaling_mode);
  106. };
  107. auto paint_transparent_black = [&]() {
  108. static constexpr auto transparent_black = Gfx::Color::from_argb(0x00'00'00'00);
  109. context.display_list_recorder().fill_rect(video_rect.to_type<int>(), transparent_black);
  110. };
  111. auto paint_loaded_video_controls = [&]() {
  112. auto is_hovered = document().hovered_node() == &video_element;
  113. auto is_paused = video_element.paused();
  114. if (is_hovered || is_paused)
  115. paint_media_controls(context, video_element, video_rect, mouse_position);
  116. };
  117. auto paint_user_agent_controls = video_element.has_attribute(HTML::AttributeNames::controls) || video_element.is_scripting_disabled();
  118. switch (representation) {
  119. case Representation::FirstVideoFrame:
  120. case Representation::CurrentVideoFrame:
  121. case Representation::LastRenderedVideoFrame:
  122. // FIXME: We likely need to cache all (or a subset of) decoded video frames along with their position. We at least
  123. // will need the first video frame and the last-rendered video frame.
  124. if (current_frame.frame)
  125. paint_frame(current_frame.frame);
  126. if (paint_user_agent_controls)
  127. paint_loaded_video_controls();
  128. break;
  129. case Representation::PosterFrame:
  130. VERIFY(poster_frame);
  131. paint_frame(poster_frame);
  132. if (paint_user_agent_controls)
  133. paint_placeholder_video_controls(context, video_rect, mouse_position);
  134. break;
  135. case Representation::TransparentBlack:
  136. paint_transparent_black();
  137. if (paint_user_agent_controls)
  138. paint_placeholder_video_controls(context, video_rect, mouse_position);
  139. break;
  140. case Representation::Unknown:
  141. VERIFY_NOT_REACHED();
  142. }
  143. }
  144. void VideoPaintable::paint_placeholder_video_controls(PaintContext& context, DevicePixelRect video_rect, Optional<DevicePixelPoint> const& mouse_position) const
  145. {
  146. auto maximum_control_box_size = context.rounded_device_pixels(100);
  147. auto maximum_playback_button_size = context.rounded_device_pixels(40);
  148. auto center = video_rect.center();
  149. auto control_box_size = min(maximum_control_box_size, min(video_rect.width(), video_rect.height()) * 4 / 5);
  150. auto control_box_offset_x = control_box_size / 2;
  151. auto control_box_offset_y = control_box_size / 2;
  152. auto control_box_location = center.translated(-control_box_offset_x, -control_box_offset_y);
  153. DevicePixelRect control_box_rect { control_box_location, { control_box_size, control_box_size } };
  154. auto playback_button_size = min(maximum_playback_button_size, min(video_rect.width(), video_rect.height()) * 2 / 5);
  155. auto playback_button_offset_x = playback_button_size / 2;
  156. auto playback_button_offset_y = playback_button_size / 2;
  157. // We want to center the play button on its center of mass, which is not the midpoint of its vertices.
  158. // To do so, reduce its desired x offset by a factor of tan(30 degrees) / 2 (about 0.288685).
  159. playback_button_offset_x -= 0.288685f * static_cast<float>(static_cast<DevicePixels::Type>(playback_button_offset_x));
  160. auto playback_button_location = center.translated(-playback_button_offset_x, -playback_button_offset_y);
  161. Array<Gfx::IntPoint, 3> play_button_coordinates { {
  162. { 0, 0 },
  163. { static_cast<int>(playback_button_size), static_cast<int>(playback_button_size) / 2 },
  164. { 0, static_cast<int>(playback_button_size) },
  165. } };
  166. auto playback_button_is_hovered = mouse_position.has_value() && control_box_rect.contains(*mouse_position);
  167. auto playback_button_color = control_button_color(playback_button_is_hovered);
  168. context.display_list_recorder().fill_ellipse(control_box_rect.to_type<int>(), control_box_color);
  169. fill_triangle(context.display_list_recorder(), playback_button_location.to_type<int>(), play_button_coordinates, playback_button_color);
  170. }
  171. }