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.2KB

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