ImageStyleValue.cpp 4.8 KB

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