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.

129 lines
4.9KB

  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. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A global cache of images that have been loaded from files or memory.
  24. If you're loading an image and may need to use the image in more than one
  25. place, this is used to allow the same image to be shared rather than loading
  26. multiple copies into memory.
  27. Another advantage is that after images are released, they will be kept in
  28. memory for a few seconds before it is actually deleted, so if you're repeatedly
  29. loading/deleting the same image, it'll reduce the chances of having to reload it
  30. each time.
  31. @see Image, ImageFileFormat
  32. @tags{Graphics}
  33. */
  34. class JUCE_API ImageCache
  35. {
  36. public:
  37. //==============================================================================
  38. /** Loads an image from a file, (or just returns the image if it's already cached).
  39. If the cache already contains an image that was loaded from this file,
  40. that image will be returned. Otherwise, this method will try to load the
  41. file, add it to the cache, and return it.
  42. Remember that the image returned is shared, so drawing into it might
  43. affect other things that are using it! If you want to draw on it, first
  44. call Image::duplicateIfShared()
  45. @param file the file to try to load
  46. @returns the image, or null if it there was an error loading it
  47. @see getFromMemory, getFromCache, ImageFileFormat::loadFrom
  48. */
  49. static Image getFromFile (const File& file);
  50. /** Loads an image from an in-memory image file, (or just returns the image if it's already cached).
  51. If the cache already contains an image that was loaded from this block of memory,
  52. that image will be returned. Otherwise, this method will try to load the
  53. file, add it to the cache, and return it.
  54. Remember that the image returned is shared, so drawing into it might
  55. affect other things that are using it! If you want to draw on it, first
  56. call Image::duplicateIfShared()
  57. @param imageData the block of memory containing the image data
  58. @param dataSize the data size in bytes
  59. @returns the image, or an invalid image if it there was an error loading it
  60. @see getFromMemory, getFromCache, ImageFileFormat::loadFrom
  61. */
  62. static Image getFromMemory (const void* imageData, int dataSize);
  63. //==============================================================================
  64. /** Checks the cache for an image with a particular hashcode.
  65. If there's an image in the cache with this hashcode, it will be returned,
  66. otherwise it will return an invalid image.
  67. @param hashCode the hash code that was associated with the image by addImageToCache()
  68. @see addImageToCache
  69. */
  70. static Image getFromHashCode (int64 hashCode);
  71. /** Adds an image to the cache with a user-defined hash-code.
  72. The image passed-in will be referenced (not copied) by the cache, so it's probably
  73. a good idea not to draw into it after adding it, otherwise this will affect all
  74. instances of it that may be in use.
  75. @param image the image to add
  76. @param hashCode the hash-code to associate with it
  77. @see getFromHashCode
  78. */
  79. static void addImageToCache (const Image& image, int64 hashCode);
  80. /** Changes the amount of time before an unused image will be removed from the cache.
  81. By default this is about 5 seconds.
  82. */
  83. static void setCacheTimeout (int millisecs);
  84. /** Releases any images in the cache that aren't being referenced by active
  85. Image objects.
  86. */
  87. static void releaseUnusedImages();
  88. private:
  89. //==============================================================================
  90. struct Pimpl;
  91. friend struct Pimpl;
  92. ImageCache();
  93. ~ImageCache();
  94. JUCE_DECLARE_NON_COPYABLE (ImageCache)
  95. };
  96. } // namespace juce