The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. struct ImageCache::Pimpl : private Timer,
  21. private DeletedAtShutdown
  22. {
  23. Pimpl() = default;
  24. ~Pimpl() override
  25. {
  26. stopTimer();
  27. clearSingletonInstance();
  28. }
  29. JUCE_DECLARE_SINGLETON (ImageCache::Pimpl, false)
  30. Image getFromHashCode (const int64 hashCode) noexcept
  31. {
  32. const ScopedLock sl (lock);
  33. for (auto& item : images)
  34. {
  35. if (item.hashCode == hashCode)
  36. {
  37. item.lastUseTime = Time::getApproximateMillisecondCounter();
  38. return item.image;
  39. }
  40. }
  41. return {};
  42. }
  43. void addImageToCache (const Image& image, const int64 hashCode)
  44. {
  45. if (image.isValid())
  46. {
  47. if (! isTimerRunning())
  48. startTimer (2000);
  49. const ScopedLock sl (lock);
  50. images.add ({ image, hashCode, Time::getApproximateMillisecondCounter() });
  51. }
  52. }
  53. void timerCallback() override
  54. {
  55. auto now = Time::getApproximateMillisecondCounter();
  56. const ScopedLock sl (lock);
  57. for (int i = images.size(); --i >= 0;)
  58. {
  59. auto& item = images.getReference (i);
  60. if (item.image.getReferenceCount() <= 1)
  61. {
  62. if (now > item.lastUseTime + cacheTimeout || now < item.lastUseTime - 1000)
  63. images.remove (i);
  64. }
  65. else
  66. {
  67. item.lastUseTime = now; // multiply-referenced, so this image is still in use.
  68. }
  69. }
  70. if (images.isEmpty())
  71. stopTimer();
  72. }
  73. void releaseUnusedImages()
  74. {
  75. const ScopedLock sl (lock);
  76. for (int i = images.size(); --i >= 0;)
  77. if (images.getReference (i).image.getReferenceCount() <= 1)
  78. images.remove (i);
  79. }
  80. struct Item
  81. {
  82. Image image;
  83. int64 hashCode;
  84. uint32 lastUseTime;
  85. };
  86. Array<Item> images;
  87. CriticalSection lock;
  88. unsigned int cacheTimeout = 5000;
  89. JUCE_DECLARE_NON_COPYABLE (Pimpl)
  90. };
  91. JUCE_IMPLEMENT_SINGLETON (ImageCache::Pimpl)
  92. //==============================================================================
  93. Image ImageCache::getFromHashCode (const int64 hashCode)
  94. {
  95. if (Pimpl::getInstanceWithoutCreating() != nullptr)
  96. return Pimpl::getInstanceWithoutCreating()->getFromHashCode (hashCode);
  97. return {};
  98. }
  99. void ImageCache::addImageToCache (const Image& image, const int64 hashCode)
  100. {
  101. Pimpl::getInstance()->addImageToCache (image, hashCode);
  102. }
  103. Image ImageCache::getFromFile (const File& file)
  104. {
  105. auto hashCode = file.hashCode64();
  106. auto image = getFromHashCode (hashCode);
  107. if (image.isNull())
  108. {
  109. image = ImageFileFormat::loadFrom (file);
  110. addImageToCache (image, hashCode);
  111. }
  112. return image;
  113. }
  114. Image ImageCache::getFromMemory (const void* imageData, const int dataSize)
  115. {
  116. auto hashCode = (int64) (pointer_sized_int) imageData;
  117. auto image = getFromHashCode (hashCode);
  118. if (image.isNull())
  119. {
  120. image = ImageFileFormat::loadFrom (imageData, (size_t) dataSize);
  121. addImageToCache (image, hashCode);
  122. }
  123. return image;
  124. }
  125. void ImageCache::setCacheTimeout (const int millisecs)
  126. {
  127. jassert (millisecs >= 0);
  128. Pimpl::getInstance()->cacheTimeout = (unsigned int) millisecs;
  129. }
  130. void ImageCache::releaseUnusedImages()
  131. {
  132. Pimpl::getInstance()->releaseUnusedImages();
  133. }
  134. } // namespace juce