ImageLoader.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 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 <AK/Debug.h>
  27. #include <LibCore/Timer.h>
  28. #include <LibGfx/Bitmap.h>
  29. #include <LibGfx/ImageDecoder.h>
  30. #include <LibWeb/Loader/ImageLoader.h>
  31. #include <LibWeb/Loader/ResourceLoader.h>
  32. namespace Web {
  33. ImageLoader::ImageLoader()
  34. : m_timer(Core::Timer::construct())
  35. {
  36. }
  37. void ImageLoader::load(const URL& url)
  38. {
  39. m_loading_state = LoadingState::Loading;
  40. LoadRequest request;
  41. request.set_url(url);
  42. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  43. }
  44. void ImageLoader::set_visible_in_viewport(bool visible_in_viewport) const
  45. {
  46. if (m_visible_in_viewport == visible_in_viewport)
  47. return;
  48. m_visible_in_viewport = visible_in_viewport;
  49. // FIXME: Don't update volatility every time. If we're here, we're probably scanning through
  50. // the whole document, updating "is visible in viewport" flags, and this could lead
  51. // to the same bitmap being marked volatile back and forth unnecessarily.
  52. if (resource())
  53. const_cast<ImageResource*>(resource())->update_volatility();
  54. }
  55. void ImageLoader::resource_did_load()
  56. {
  57. ASSERT(resource());
  58. if (!resource()->mime_type().starts_with("image/")) {
  59. m_loading_state = LoadingState::Failed;
  60. if (on_fail)
  61. on_fail();
  62. return;
  63. }
  64. m_loading_state = LoadingState::Loaded;
  65. if constexpr (debug_image_loader) {
  66. if (!resource()->has_encoded_data()) {
  67. dbgln("ImageLoader: Resource did load, no encoded data. URL: {}", resource()->url());
  68. } else {
  69. dbgln("ImageLoader: Resource did load, has encoded data. URL: {}", resource()->url());
  70. }
  71. }
  72. if (resource()->should_decode_in_process()) {
  73. auto& decoder = resource()->ensure_decoder();
  74. if (decoder.is_animated() && decoder.frame_count() > 1) {
  75. const auto& first_frame = decoder.frame(0);
  76. m_timer->set_interval(first_frame.duration);
  77. m_timer->on_timeout = [this] { animate(); };
  78. m_timer->start();
  79. }
  80. }
  81. if (on_load)
  82. on_load();
  83. }
  84. void ImageLoader::animate()
  85. {
  86. if (!m_visible_in_viewport)
  87. return;
  88. auto& decoder = resource()->ensure_decoder();
  89. m_current_frame_index = (m_current_frame_index + 1) % decoder.frame_count();
  90. const auto& current_frame = decoder.frame(m_current_frame_index);
  91. if (current_frame.duration != m_timer->interval()) {
  92. m_timer->restart(current_frame.duration);
  93. }
  94. if (m_current_frame_index == decoder.frame_count() - 1) {
  95. ++m_loops_completed;
  96. if (m_loops_completed > 0 && m_loops_completed == decoder.loop_count()) {
  97. m_timer->stop();
  98. }
  99. }
  100. if (on_animate)
  101. on_animate();
  102. }
  103. void ImageLoader::resource_did_fail()
  104. {
  105. dbgln("ImageLoader: Resource did fail. URL: {}", resource()->url());
  106. m_loading_state = LoadingState::Failed;
  107. if (on_fail)
  108. on_fail();
  109. }
  110. bool ImageLoader::has_image() const
  111. {
  112. if (!resource())
  113. return false;
  114. if (resource()->should_decode_in_process())
  115. return const_cast<ImageResource*>(resource())->ensure_decoder().bitmap();
  116. return true;
  117. }
  118. unsigned ImageLoader::width() const
  119. {
  120. if (!resource())
  121. return 0;
  122. if (resource()->should_decode_in_process())
  123. return const_cast<ImageResource*>(resource())->ensure_decoder().width();
  124. return bitmap() ? bitmap()->width() : 0;
  125. }
  126. unsigned ImageLoader::height() const
  127. {
  128. if (!resource())
  129. return 0;
  130. if (resource()->should_decode_in_process())
  131. return const_cast<ImageResource*>(resource())->ensure_decoder().height();
  132. return bitmap() ? bitmap()->height() : 0;
  133. }
  134. const Gfx::Bitmap* ImageLoader::bitmap() const
  135. {
  136. if (!resource())
  137. return nullptr;
  138. return resource()->bitmap(m_current_frame_index);
  139. }
  140. }