HTMLImageElement.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/Timer.h>
  27. #include <LibGfx/Bitmap.h>
  28. #include <LibGfx/ImageDecoder.h>
  29. #include <LibWeb/CSS/StyleResolver.h>
  30. #include <LibWeb/DOM/Document.h>
  31. #include <LibWeb/DOM/Event.h>
  32. #include <LibWeb/DOM/HTMLImageElement.h>
  33. #include <LibWeb/Layout/LayoutImage.h>
  34. #include <LibWeb/Loader/ResourceLoader.h>
  35. namespace Web {
  36. HTMLImageElement::HTMLImageElement(Document& document, const FlyString& tag_name)
  37. : HTMLElement(document, tag_name)
  38. , m_timer(Core::Timer::construct())
  39. {
  40. }
  41. HTMLImageElement::~HTMLImageElement()
  42. {
  43. }
  44. void HTMLImageElement::parse_attribute(const FlyString& name, const String& value)
  45. {
  46. HTMLElement::parse_attribute(name, value);
  47. if (name.equals_ignoring_case("src"))
  48. load_image(value);
  49. }
  50. void HTMLImageElement::load_image(const String& src)
  51. {
  52. LoadRequest request;
  53. request.set_url(document().complete_url(src));
  54. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  55. }
  56. void HTMLImageElement::resource_did_load()
  57. {
  58. ASSERT(resource());
  59. if (!resource()->has_encoded_data()) {
  60. dbg() << "HTMLImageElement: Resource did load, but encoded data empty: " << this->src();
  61. return;
  62. }
  63. dbg() << "HTMLImageElement: Resource did load, encoded data looks tasty: " << this->src();
  64. m_image_decoder = resource()->ensure_decoder();
  65. if (m_image_decoder->is_animated() && m_image_decoder->frame_count() > 1) {
  66. const auto& first_frame = m_image_decoder->frame(0);
  67. m_timer->set_interval(first_frame.duration);
  68. m_timer->on_timeout = [this] { animate(); };
  69. m_timer->start();
  70. }
  71. document().update_layout();
  72. dispatch_event(Event::create("load"));
  73. }
  74. void HTMLImageElement::resource_did_fail()
  75. {
  76. dbg() << "HTMLImageElement: Resource did fail: " << this->src();
  77. m_image_decoder = nullptr;
  78. document().update_layout();
  79. dispatch_event(Event::create("error"));
  80. }
  81. void HTMLImageElement::resource_did_replace_decoder()
  82. {
  83. m_image_decoder = resource()->ensure_decoder();
  84. }
  85. void HTMLImageElement::animate()
  86. {
  87. if (!layout_node()) {
  88. return;
  89. }
  90. m_current_frame_index = (m_current_frame_index + 1) % m_image_decoder->frame_count();
  91. const auto& current_frame = m_image_decoder->frame(m_current_frame_index);
  92. if (current_frame.duration != m_timer->interval()) {
  93. m_timer->restart(current_frame.duration);
  94. }
  95. if (m_current_frame_index == m_image_decoder->frame_count() - 1) {
  96. ++m_loops_completed;
  97. if (m_loops_completed > 0 && m_loops_completed == m_image_decoder->loop_count()) {
  98. m_timer->stop();
  99. }
  100. }
  101. layout_node()->set_needs_display();
  102. }
  103. int HTMLImageElement::preferred_width() const
  104. {
  105. bool ok = false;
  106. int width = attribute(HTML::AttributeNames::width).to_int(ok);
  107. if (ok)
  108. return width;
  109. if (m_image_decoder)
  110. return m_image_decoder->width();
  111. return 0;
  112. }
  113. int HTMLImageElement::preferred_height() const
  114. {
  115. bool ok = false;
  116. int height = attribute(HTML::AttributeNames::height).to_int(ok);
  117. if (ok)
  118. return height;
  119. if (m_image_decoder)
  120. return m_image_decoder->height();
  121. return 0;
  122. }
  123. RefPtr<LayoutNode> HTMLImageElement::create_layout_node(const StyleProperties* parent_style) const
  124. {
  125. auto style = document().style_resolver().resolve_style(*this, parent_style);
  126. auto display = style->string_or_fallback(CSS::PropertyID::Display, "inline");
  127. if (display == "none")
  128. return nullptr;
  129. return adopt(*new LayoutImage(*this, move(style)));
  130. }
  131. const Gfx::Bitmap* HTMLImageElement::bitmap() const
  132. {
  133. if (!m_image_decoder)
  134. return nullptr;
  135. if (m_image_decoder->is_animated()) {
  136. return m_image_decoder->frame(m_current_frame_index).image;
  137. }
  138. return m_image_decoder->bitmap();
  139. }
  140. void HTMLImageElement::set_visible_in_viewport(Badge<LayoutDocument>, bool visible_in_viewport)
  141. {
  142. if (m_visible_in_viewport == visible_in_viewport)
  143. return;
  144. m_visible_in_viewport = visible_in_viewport;
  145. // FIXME: Don't update volatility every time. If we're here, we're probably scanning through
  146. // the whole document, updating "is visible in viewport" flags, and this could lead
  147. // to the same bitmap being marked volatile back and forth unnecessarily.
  148. if (resource())
  149. resource()->update_volatility();
  150. }
  151. }