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.

170 lines
4.6KB

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