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.

178 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. class ImageCache::Pimpl : private Timer,
  19. private DeletedAtShutdown
  20. {
  21. public:
  22. Pimpl() : cacheTimeout (5000)
  23. {
  24. }
  25. ~Pimpl()
  26. {
  27. clearSingletonInstance();
  28. }
  29. Image getFromHashCode (const int64 hashCode)
  30. {
  31. const ScopedLock sl (lock);
  32. for (int i = images.size(); --i >= 0;)
  33. {
  34. const Item* const item = images.getUnchecked(i);
  35. if (item->hashCode == hashCode)
  36. return item->image;
  37. }
  38. return Image::null;
  39. }
  40. void addImageToCache (const Image& image, const int64 hashCode)
  41. {
  42. if (image.isValid())
  43. {
  44. if (! isTimerRunning())
  45. startTimer (2000);
  46. Item* const item = new Item();
  47. item->hashCode = hashCode;
  48. item->image = image;
  49. item->lastUseTime = Time::getApproximateMillisecondCounter();
  50. const ScopedLock sl (lock);
  51. images.add (item);
  52. }
  53. }
  54. void timerCallback()
  55. {
  56. const uint32 now = Time::getApproximateMillisecondCounter();
  57. const ScopedLock sl (lock);
  58. for (int i = images.size(); --i >= 0;)
  59. {
  60. Item* const item = images.getUnchecked(i);
  61. if (item->image.getReferenceCount() <= 1)
  62. {
  63. if (now > item->lastUseTime + cacheTimeout || now < item->lastUseTime - 1000)
  64. images.remove (i);
  65. }
  66. else
  67. {
  68. item->lastUseTime = now; // multiply-referenced, so this image is still in use.
  69. }
  70. }
  71. if (images.size() == 0)
  72. stopTimer();
  73. }
  74. void releaseUnusedImages()
  75. {
  76. const ScopedLock sl (lock);
  77. for (int i = images.size(); --i >= 0;)
  78. if (images.getUnchecked(i)->image.getReferenceCount() <= 1)
  79. images.remove (i);
  80. }
  81. struct Item
  82. {
  83. Image image;
  84. int64 hashCode;
  85. uint32 lastUseTime;
  86. };
  87. unsigned int cacheTimeout;
  88. juce_DeclareSingleton_SingleThreaded_Minimal (ImageCache::Pimpl);
  89. private:
  90. OwnedArray<Item> images;
  91. CriticalSection lock;
  92. JUCE_DECLARE_NON_COPYABLE (Pimpl)
  93. };
  94. juce_ImplementSingleton_SingleThreaded (ImageCache::Pimpl);
  95. //==============================================================================
  96. Image ImageCache::getFromHashCode (const int64 hashCode)
  97. {
  98. if (Pimpl::getInstanceWithoutCreating() != nullptr)
  99. return Pimpl::getInstanceWithoutCreating()->getFromHashCode (hashCode);
  100. return Image::null;
  101. }
  102. void ImageCache::addImageToCache (const Image& image, const int64 hashCode)
  103. {
  104. Pimpl::getInstance()->addImageToCache (image, hashCode);
  105. }
  106. Image ImageCache::getFromFile (const File& file)
  107. {
  108. const int64 hashCode = file.hashCode64();
  109. Image image (getFromHashCode (hashCode));
  110. if (image.isNull())
  111. {
  112. image = ImageFileFormat::loadFrom (file);
  113. addImageToCache (image, hashCode);
  114. }
  115. return image;
  116. }
  117. Image ImageCache::getFromMemory (const void* imageData, const int dataSize)
  118. {
  119. const int64 hashCode = (int64) (pointer_sized_int) imageData;
  120. Image image (getFromHashCode (hashCode));
  121. if (image.isNull())
  122. {
  123. image = ImageFileFormat::loadFrom (imageData, (size_t) dataSize);
  124. addImageToCache (image, hashCode);
  125. }
  126. return image;
  127. }
  128. void ImageCache::setCacheTimeout (const int millisecs)
  129. {
  130. jassert (millisecs >= 0);
  131. Pimpl::getInstance()->cacheTimeout = (unsigned int) millisecs;
  132. }
  133. void ImageCache::releaseUnusedImages()
  134. {
  135. Pimpl::getInstance()->releaseUnusedImages();
  136. }