ImageResource.cpp 3.8 KB

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