Audio plugin host https://kx.studio/carla
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.

170 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 { clearSingletonInstance(); }
  25. JUCE_DECLARE_SINGLETON (ImageCache::Pimpl, false)
  26. Image getFromHashCode (const int64 hashCode) noexcept
  27. {
  28. const ScopedLock sl (lock);
  29. for (auto& item : images)
  30. {
  31. if (item.hashCode == hashCode)
  32. {
  33. item.lastUseTime = Time::getApproximateMillisecondCounter();
  34. return item.image;
  35. }
  36. }
  37. return {};
  38. }
  39. void addImageToCache (const Image& image, const int64 hashCode)
  40. {
  41. if (image.isValid())
  42. {
  43. if (! isTimerRunning())
  44. startTimer (2000);
  45. const ScopedLock sl (lock);
  46. images.add ({ image, hashCode, Time::getApproximateMillisecondCounter() });
  47. }
  48. }
  49. void timerCallback() override
  50. {
  51. auto now = Time::getApproximateMillisecondCounter();
  52. const ScopedLock sl (lock);
  53. for (int i = images.size(); --i >= 0;)
  54. {
  55. auto& item = images.getReference(i);
  56. if (item.image.getReferenceCount() <= 1)
  57. {
  58. if (now > item.lastUseTime + cacheTimeout || now < item.lastUseTime - 1000)
  59. images.remove (i);
  60. }
  61. else
  62. {
  63. item.lastUseTime = now; // multiply-referenced, so this image is still in use.
  64. }
  65. }
  66. if (images.isEmpty())
  67. stopTimer();
  68. }
  69. void releaseUnusedImages()
  70. {
  71. const ScopedLock sl (lock);
  72. for (int i = images.size(); --i >= 0;)
  73. if (images.getReference(i).image.getReferenceCount() <= 1)
  74. images.remove (i);
  75. }
  76. struct Item
  77. {
  78. Image image;
  79. int64 hashCode;
  80. uint32 lastUseTime;
  81. };
  82. Array<Item> images;
  83. CriticalSection lock;
  84. unsigned int cacheTimeout = 5000;
  85. JUCE_DECLARE_NON_COPYABLE (Pimpl)
  86. };
  87. JUCE_IMPLEMENT_SINGLETON (ImageCache::Pimpl)
  88. //==============================================================================
  89. Image ImageCache::getFromHashCode (const int64 hashCode)
  90. {
  91. if (Pimpl::getInstanceWithoutCreating() != nullptr)
  92. return Pimpl::getInstanceWithoutCreating()->getFromHashCode (hashCode);
  93. return {};
  94. }
  95. void ImageCache::addImageToCache (const Image& image, const int64 hashCode)
  96. {
  97. Pimpl::getInstance()->addImageToCache (image, hashCode);
  98. }
  99. Image ImageCache::getFromFile (const File& file)
  100. {
  101. auto hashCode = file.hashCode64();
  102. auto image = getFromHashCode (hashCode);
  103. if (image.isNull())
  104. {
  105. image = ImageFileFormat::loadFrom (file);
  106. addImageToCache (image, hashCode);
  107. }
  108. return image;
  109. }
  110. Image ImageCache::getFromMemory (const void* imageData, const int dataSize)
  111. {
  112. auto hashCode = (int64) (pointer_sized_int) imageData;
  113. auto image = getFromHashCode (hashCode);
  114. if (image.isNull())
  115. {
  116. image = ImageFileFormat::loadFrom (imageData, (size_t) dataSize);
  117. addImageToCache (image, hashCode);
  118. }
  119. return image;
  120. }
  121. void ImageCache::setCacheTimeout (const int millisecs)
  122. {
  123. jassert (millisecs >= 0);
  124. Pimpl::getInstance()->cacheTimeout = (unsigned int) millisecs;
  125. }
  126. void ImageCache::releaseUnusedImages()
  127. {
  128. Pimpl::getInstance()->releaseUnusedImages();
  129. }
  130. } // namespace juce