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.

166 lines
4.4KB

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