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.

164 lines
4.5KB

  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()
  23. : cacheTimeout (5000)
  24. {
  25. }
  26. ~Pimpl()
  27. {
  28. clearSingletonInstance();
  29. }
  30. Image getFromHashCode (const int64 hashCode)
  31. {
  32. const ScopedLock sl (lock);
  33. for (int i = images.size(); --i >= 0;)
  34. {
  35. Item* const item = images.getUnchecked(i);
  36. if (item->hashCode == hashCode)
  37. return item->image;
  38. }
  39. return Image::null;
  40. }
  41. void addImageToCache (const Image& image, const int64 hashCode)
  42. {
  43. if (image.isValid())
  44. {
  45. if (! isTimerRunning())
  46. startTimer (2000);
  47. Item* const item = new Item();
  48. item->hashCode = hashCode;
  49. item->image = image;
  50. item->lastUseTime = Time::getApproximateMillisecondCounter();
  51. const ScopedLock sl (lock);
  52. images.add (item);
  53. }
  54. }
  55. void timerCallback()
  56. {
  57. const uint32 now = Time::getApproximateMillisecondCounter();
  58. const ScopedLock sl (lock);
  59. for (int i = images.size(); --i >= 0;)
  60. {
  61. Item* const item = images.getUnchecked(i);
  62. if (item->image.getReferenceCount() <= 1)
  63. {
  64. if (now > item->lastUseTime + cacheTimeout || now < item->lastUseTime - 1000)
  65. images.remove (i);
  66. }
  67. else
  68. {
  69. item->lastUseTime = now; // multiply-referenced, so this image is still in use.
  70. }
  71. }
  72. if (images.size() == 0)
  73. stopTimer();
  74. }
  75. struct Item
  76. {
  77. Image image;
  78. int64 hashCode;
  79. uint32 lastUseTime;
  80. };
  81. int cacheTimeout;
  82. juce_DeclareSingleton_SingleThreaded_Minimal (ImageCache::Pimpl);
  83. private:
  84. OwnedArray<Item> images;
  85. CriticalSection lock;
  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 Image::null;
  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. const int64 hashCode = file.hashCode64();
  103. Image 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. const int64 hashCode = (int64) (pointer_sized_int) imageData;
  114. Image 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. Pimpl::getInstance()->cacheTimeout = millisecs;
  125. }