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.

juce_ImageCache.cpp 4.5KB

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