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.

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