ImageStyleValue.cpp 5.6 KB

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