ImageStyleValue.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "ImageStyleValue.h"
  10. #include <LibWeb/CSS/ComputedValues.h>
  11. #include <LibWeb/CSS/Serialize.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/HTML/DecodedImageData.h>
  14. #include <LibWeb/HTML/ImageRequest.h>
  15. #include <LibWeb/HTML/PotentialCORSRequest.h>
  16. #include <LibWeb/Painting/DisplayListRecorder.h>
  17. #include <LibWeb/Painting/PaintContext.h>
  18. #include <LibWeb/Platform/Timer.h>
  19. namespace Web::CSS {
  20. ImageStyleValue::ImageStyleValue(URL::URL const& url)
  21. : AbstractImageStyleValue(Type::Image)
  22. , m_url(url)
  23. {
  24. }
  25. ImageStyleValue::~ImageStyleValue() = default;
  26. void ImageStyleValue::load_any_resources(DOM::Document& document)
  27. {
  28. if (m_image_request)
  29. return;
  30. m_document = &document;
  31. m_image_request = HTML::SharedImageRequest::get_or_create(document.realm(), document.page(), m_url);
  32. m_image_request->add_callbacks(
  33. [this, weak_this = make_weak_ptr()] {
  34. if (!weak_this)
  35. return;
  36. if (!m_document)
  37. return;
  38. // FIXME: Do less than a full repaint if possible?
  39. if (auto navigable = m_document->navigable())
  40. navigable->set_needs_display();
  41. auto image_data = m_image_request->image_data();
  42. if (image_data->is_animated() && image_data->frame_count() > 1) {
  43. m_timer = Platform::Timer::create();
  44. m_timer->set_interval(image_data->frame_duration(0));
  45. m_timer->on_timeout = [this] { animate(); };
  46. m_timer->start();
  47. }
  48. },
  49. nullptr);
  50. if (m_image_request->needs_fetching()) {
  51. auto request = HTML::create_potential_CORS_request(document.vm(), m_url, Fetch::Infrastructure::Request::Destination::Image, HTML::CORSSettingAttribute::NoCORS);
  52. request->set_client(&document.relevant_settings_object());
  53. m_image_request->fetch_image(document.realm(), request);
  54. }
  55. }
  56. void ImageStyleValue::animate()
  57. {
  58. if (!m_image_request)
  59. return;
  60. auto image_data = m_image_request->image_data();
  61. if (!image_data)
  62. return;
  63. m_current_frame_index = (m_current_frame_index + 1) % image_data->frame_count();
  64. auto current_frame_duration = image_data->frame_duration(m_current_frame_index);
  65. if (current_frame_duration != m_timer->interval())
  66. m_timer->restart(current_frame_duration);
  67. if (m_current_frame_index == image_data->frame_count() - 1) {
  68. ++m_loops_completed;
  69. if (m_loops_completed > 0 && m_loops_completed == image_data->loop_count())
  70. m_timer->stop();
  71. }
  72. if (on_animate)
  73. on_animate();
  74. }
  75. bool ImageStyleValue::is_paintable() const
  76. {
  77. return image_data();
  78. }
  79. Gfx::ImmutableBitmap const* ImageStyleValue::bitmap(size_t frame_index, Gfx::IntSize size) const
  80. {
  81. if (auto image_data = this->image_data())
  82. return image_data->bitmap(frame_index, size);
  83. return nullptr;
  84. }
  85. String ImageStyleValue::to_string() const
  86. {
  87. return serialize_a_url(MUST(m_url.to_string()));
  88. }
  89. bool ImageStyleValue::equals(StyleValue const& other) const
  90. {
  91. if (type() != other.type())
  92. return false;
  93. return m_url == other.as_image().m_url;
  94. }
  95. Optional<CSSPixels> ImageStyleValue::natural_width() const
  96. {
  97. if (auto image_data = this->image_data())
  98. return image_data->intrinsic_width();
  99. return {};
  100. }
  101. Optional<CSSPixels> ImageStyleValue::natural_height() const
  102. {
  103. if (auto image_data = this->image_data())
  104. return image_data->intrinsic_height();
  105. return {};
  106. }
  107. Optional<CSSPixelFraction> ImageStyleValue::natural_aspect_ratio() const
  108. {
  109. if (auto image_data = this->image_data())
  110. return image_data->intrinsic_aspect_ratio();
  111. return {};
  112. }
  113. void ImageStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering image_rendering, Vector<Gfx::Path> const& clip_paths) const
  114. {
  115. if (auto const* b = bitmap(m_current_frame_index, dest_rect.size().to_type<int>()); b != nullptr) {
  116. auto scaling_mode = to_gfx_scaling_mode(image_rendering, b->rect(), dest_rect.to_type<int>());
  117. context.display_list_recorder().draw_scaled_immutable_bitmap(dest_rect.to_type<int>(), *b, b->rect(), scaling_mode, clip_paths);
  118. }
  119. }
  120. JS::GCPtr<HTML::DecodedImageData> ImageStyleValue::image_data() const
  121. {
  122. if (!m_image_request)
  123. return nullptr;
  124. return m_image_request->image_data();
  125. }
  126. Optional<Gfx::Color> ImageStyleValue::color_if_single_pixel_bitmap() const
  127. {
  128. if (auto const* b = bitmap(m_current_frame_index)) {
  129. if (b->width() == 1 && b->height() == 1)
  130. return b->bitmap().get_pixel(0, 0);
  131. }
  132. return {};
  133. }
  134. }