VideoPaintable.cpp 9.4 KB

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