ImageStyleValue.cpp 4.3 KB

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