ImageStyleValue.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.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_resource_request)
  29. return;
  30. m_document = &document;
  31. m_resource_request = HTML::SharedResourceRequest::get_or_create(document.realm(), document.page(), m_url);
  32. m_resource_request->add_callbacks(
  33. [this, weak_this = make_weak_ptr()] {
  34. if (!weak_this)
  35. return;
  36. if (!m_document)
  37. return;
  38. if (auto navigable = m_document->navigable()) {
  39. // Once the image has loaded, we need to re-resolve CSS properties that depend on the image's dimensions.
  40. m_document->set_needs_to_resolve_paint_only_properties();
  41. // FIXME: Do less than a full repaint if possible?
  42. m_document->set_needs_display();
  43. }
  44. auto image_data = m_resource_request->image_data();
  45. if (image_data->is_animated() && image_data->frame_count() > 1) {
  46. m_timer = Platform::Timer::create();
  47. m_timer->set_interval(image_data->frame_duration(0));
  48. m_timer->on_timeout = [this] { animate(); };
  49. m_timer->start();
  50. }
  51. },
  52. nullptr);
  53. if (m_resource_request->needs_fetching()) {
  54. auto request = HTML::create_potential_CORS_request(document.vm(), m_url, Fetch::Infrastructure::Request::Destination::Image, HTML::CORSSettingAttribute::NoCORS);
  55. request->set_client(&document.relevant_settings_object());
  56. m_resource_request->fetch_resource(document.realm(), request);
  57. }
  58. }
  59. void ImageStyleValue::animate()
  60. {
  61. if (!m_resource_request)
  62. return;
  63. auto image_data = m_resource_request->image_data();
  64. if (!image_data)
  65. return;
  66. m_current_frame_index = (m_current_frame_index + 1) % image_data->frame_count();
  67. auto current_frame_duration = image_data->frame_duration(m_current_frame_index);
  68. if (current_frame_duration != m_timer->interval())
  69. m_timer->restart(current_frame_duration);
  70. if (m_current_frame_index == image_data->frame_count() - 1) {
  71. ++m_loops_completed;
  72. if (m_loops_completed > 0 && m_loops_completed == image_data->loop_count())
  73. m_timer->stop();
  74. }
  75. if (on_animate)
  76. on_animate();
  77. }
  78. bool ImageStyleValue::is_paintable() const
  79. {
  80. return image_data();
  81. }
  82. Gfx::ImmutableBitmap const* ImageStyleValue::bitmap(size_t frame_index, Gfx::IntSize size) const
  83. {
  84. if (auto image_data = this->image_data())
  85. return image_data->bitmap(frame_index, size);
  86. return nullptr;
  87. }
  88. String ImageStyleValue::to_string() const
  89. {
  90. return serialize_a_url(MUST(m_url.to_string()));
  91. }
  92. bool ImageStyleValue::equals(CSSStyleValue const& other) const
  93. {
  94. if (type() != other.type())
  95. return false;
  96. return m_url == other.as_image().m_url;
  97. }
  98. Optional<CSSPixels> ImageStyleValue::natural_width() const
  99. {
  100. if (auto image_data = this->image_data())
  101. return image_data->intrinsic_width();
  102. return {};
  103. }
  104. Optional<CSSPixels> ImageStyleValue::natural_height() const
  105. {
  106. if (auto image_data = this->image_data())
  107. return image_data->intrinsic_height();
  108. return {};
  109. }
  110. Optional<CSSPixelFraction> ImageStyleValue::natural_aspect_ratio() const
  111. {
  112. if (auto image_data = this->image_data())
  113. return image_data->intrinsic_aspect_ratio();
  114. return {};
  115. }
  116. void ImageStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering image_rendering) const
  117. {
  118. if (auto const* b = bitmap(m_current_frame_index, dest_rect.size().to_type<int>()); b != nullptr) {
  119. auto scaling_mode = to_gfx_scaling_mode(image_rendering, b->rect(), dest_rect.to_type<int>());
  120. context.display_list_recorder().draw_scaled_immutable_bitmap(dest_rect.to_type<int>(), *b, b->rect(), scaling_mode);
  121. }
  122. }
  123. Gfx::ImmutableBitmap const* ImageStyleValue::current_frame_bitmap(DevicePixelRect const& dest_rect) const
  124. {
  125. return bitmap(m_current_frame_index, dest_rect.size().to_type<int>());
  126. }
  127. JS::GCPtr<HTML::DecodedImageData> ImageStyleValue::image_data() const
  128. {
  129. if (!m_resource_request)
  130. return nullptr;
  131. return m_resource_request->image_data();
  132. }
  133. Optional<Gfx::Color> ImageStyleValue::color_if_single_pixel_bitmap() const
  134. {
  135. if (auto const* b = bitmap(m_current_frame_index)) {
  136. if (b->width() == 1 && b->height() == 1)
  137. return b->bitmap().get_pixel(0, 0);
  138. }
  139. return {};
  140. }
  141. }