ImageResource.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/Function.h>
  27. #include <LibGfx/Bitmap.h>
  28. #include <LibImageDecoderClient/Client.h>
  29. #include <LibWeb/Loader/ImageResource.h>
  30. namespace Web {
  31. ImageResource::ImageResource(const LoadRequest& request)
  32. : Resource(Type::Image, request)
  33. {
  34. }
  35. ImageResource::~ImageResource()
  36. {
  37. }
  38. int ImageResource::frame_duration(size_t frame_index) const
  39. {
  40. decode_if_needed();
  41. if (frame_index >= m_decoded_frames.size())
  42. return 0;
  43. return m_decoded_frames[frame_index].duration;
  44. }
  45. static ImageDecoderClient::Client& image_decoder_client()
  46. {
  47. static RefPtr<ImageDecoderClient::Client> image_decoder_client;
  48. if (!image_decoder_client) {
  49. image_decoder_client = ImageDecoderClient::Client::construct();
  50. image_decoder_client->on_death = [&] {
  51. image_decoder_client = nullptr;
  52. };
  53. }
  54. return *image_decoder_client;
  55. }
  56. void ImageResource::decode_if_needed() const
  57. {
  58. if (!has_encoded_data())
  59. return;
  60. if (m_has_attempted_decode)
  61. return;
  62. if (!m_decoded_frames.is_empty())
  63. return;
  64. NonnullRefPtr decoder = image_decoder_client();
  65. auto image = decoder->decode_image(encoded_data());
  66. if (image.has_value()) {
  67. m_loop_count = image.value().loop_count;
  68. m_animated = image.value().is_animated;
  69. m_decoded_frames.resize(image.value().frames.size());
  70. for (size_t i = 0; i < m_decoded_frames.size(); ++i) {
  71. auto& frame = m_decoded_frames[i];
  72. frame.bitmap = image.value().frames[i].bitmap;
  73. frame.duration = image.value().frames[i].duration;
  74. }
  75. }
  76. m_has_attempted_decode = true;
  77. }
  78. const Gfx::Bitmap* ImageResource::bitmap(size_t frame_index) const
  79. {
  80. decode_if_needed();
  81. if (frame_index >= m_decoded_frames.size())
  82. return nullptr;
  83. return m_decoded_frames[frame_index].bitmap;
  84. }
  85. void ImageResource::update_volatility()
  86. {
  87. bool visible_in_viewport = false;
  88. for_each_client([&](auto& client) {
  89. if (static_cast<const ImageResourceClient&>(client).is_visible_in_viewport())
  90. visible_in_viewport = true;
  91. });
  92. if (!visible_in_viewport) {
  93. for (auto& frame : m_decoded_frames) {
  94. if (frame.bitmap)
  95. frame.bitmap->set_volatile();
  96. }
  97. return;
  98. }
  99. bool still_has_decoded_image = true;
  100. for (auto& frame : m_decoded_frames) {
  101. if (!frame.bitmap) {
  102. still_has_decoded_image = false;
  103. } else {
  104. bool still_has_frame = frame.bitmap->set_nonvolatile();
  105. if (!still_has_frame)
  106. still_has_decoded_image = false;
  107. }
  108. }
  109. if (still_has_decoded_image)
  110. return;
  111. m_decoded_frames.clear();
  112. m_has_attempted_decode = false;
  113. }
  114. ImageResourceClient::~ImageResourceClient()
  115. {
  116. }
  117. }