JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_ImageCache.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29struct ImageCache::Pimpl : private Timer,
30 private DeletedAtShutdown
31{
32 Pimpl() = default;
33
34 ~Pimpl() override
35 {
36 stopTimer();
38 }
39
41
42 Image getFromHashCode (const int64 hashCode) noexcept
43 {
44 const ScopedLock sl (lock);
45
46 for (auto& item : images)
47 {
48 if (item.hashCode == hashCode)
49 {
50 item.lastUseTime = Time::getApproximateMillisecondCounter();
51 return item.image;
52 }
53 }
54
55 return {};
56 }
57
58 void addImageToCache (const Image& image, const int64 hashCode)
59 {
60 if (image.isValid())
61 {
62 if (! isTimerRunning())
63 startTimer (2000);
64
65 const ScopedLock sl (lock);
66 images.add ({ image, hashCode, Time::getApproximateMillisecondCounter() });
67 }
68 }
69
70 void timerCallback() override
71 {
73
74 const ScopedLock sl (lock);
75
76 for (int i = images.size(); --i >= 0;)
77 {
78 auto& item = images.getReference (i);
79
80 if (item.image.getReferenceCount() <= 1)
81 {
82 if (now > item.lastUseTime + cacheTimeout || now < item.lastUseTime - 1000)
83 images.remove (i);
84 }
85 else
86 {
87 item.lastUseTime = now; // multiply-referenced, so this image is still in use.
88 }
89 }
90
91 if (images.isEmpty())
92 stopTimer();
93 }
94
95 void releaseUnusedImages()
96 {
97 const ScopedLock sl (lock);
98
99 for (int i = images.size(); --i >= 0;)
100 if (images.getReference (i).image.getReferenceCount() <= 1)
101 images.remove (i);
102 }
103
104 struct Item
105 {
106 Image image;
107 int64 hashCode;
108 uint32 lastUseTime;
109 };
110
111 Array<Item> images;
112 CriticalSection lock;
113 unsigned int cacheTimeout = 5000;
114
116};
117
119
120
121//==============================================================================
122Image ImageCache::getFromHashCode (const int64 hashCode)
123{
124 if (Pimpl::getInstanceWithoutCreating() != nullptr)
125 return Pimpl::getInstanceWithoutCreating()->getFromHashCode (hashCode);
126
127 return {};
128}
129
130void ImageCache::addImageToCache (const Image& image, const int64 hashCode)
131{
132 Pimpl::getInstance()->addImageToCache (image, hashCode);
133}
134
136{
137 auto hashCode = file.hashCode64();
138 auto image = getFromHashCode (hashCode);
139
140 if (image.isNull())
141 {
142 image = ImageFileFormat::loadFrom (file);
143 addImageToCache (image, hashCode);
144 }
145
146 return image;
147}
148
149Image ImageCache::getFromMemory (const void* imageData, const int dataSize)
150{
151 auto hashCode = (int64) (pointer_sized_int) imageData;
152 auto image = getFromHashCode (hashCode);
153
154 if (image.isNull())
155 {
156 image = ImageFileFormat::loadFrom (imageData, (size_t) dataSize);
157 addImageToCache (image, hashCode);
158 }
159
160 return image;
161}
162
164{
165 jassert (millisecs >= 0);
166 Pimpl::getInstance()->cacheTimeout = (unsigned int) millisecs;
167}
168
170{
171 Pimpl::getInstance()->releaseUnusedImages();
172}
173
174} // namespace juce
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
bool isEmpty() const noexcept
Returns true if the array is empty, false otherwise.
Definition juce_Array.h:222
int size() const noexcept
Returns the current number of elements in the array.
Definition juce_Array.h:215
void remove(int indexToRemove)
Removes an element from the array.
Definition juce_Array.h:742
void add(const ElementType &newElement)
Appends a new element at the end of the array.
Definition juce_Array.h:418
ElementType & getReference(int index) noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in.
Definition juce_Array.h:267
Classes derived from this will be automatically deleted when the application exits.
Represents a local file or directory.
Definition juce_File.h:45
int64 hashCode64() const
Returns a 64-bit hash-code that identifies this file.
Automatically locks and unlocks a mutex object.
A global cache of images that have been loaded from files or memory.
static Image getFromMemory(const void *imageData, int dataSize)
Loads an image from an in-memory image file, (or just returns the image if it's already cached).
static void setCacheTimeout(int millisecs)
Changes the amount of time before an unused image will be removed from the cache.
static void addImageToCache(const Image &image, int64 hashCode)
Adds an image to the cache with a user-defined hash-code.
static void releaseUnusedImages()
Releases any images in the cache that aren't being referenced by active Image objects.
static Image getFromFile(const File &file)
Loads an image from a file, (or just returns the image if it's already cached).
static Image getFromHashCode(int64 hashCode)
Checks the cache for an image with a particular hashcode.
static Image loadFrom(InputStream &input)
Tries to load an image from a stream.
Holds a fixed-size bitmap.
Definition juce_Image.h:58
int getReferenceCount() const noexcept
Returns the number of Image objects which are currently referring to the same internal shared image d...
static uint32 getApproximateMillisecondCounter() noexcept
Less-accurate but faster version of getMillisecondCounter().
Makes repeated callbacks to a virtual method at a specified time interval.
Definition juce_Timer.h:52
void stopTimer() noexcept
Stops the timer.
bool isTimerRunning() const noexcept
Returns true if the timer is currently running.
Definition juce_Timer.h:111
void startTimer(int intervalInMilliseconds) noexcept
Starts the timer and sets the length of interval required.
#define jassert(expression)
Platform-independent assertion macro.
#define JUCE_DECLARE_NON_COPYABLE(className)
This is a shorthand macro for deleting a class's copy constructor and copy assignment operator.
#define JUCE_IMPLEMENT_SINGLETON(Classname)
This is a counterpart to the JUCE_DECLARE_SINGLETON macros.
#define JUCE_DECLARE_SINGLETON(Classname, doNotRecreateAfterDeletion)
Macro to generate the appropriate methods and boilerplate for a singleton class.
typedef int
JUCE Namespace.
int pointer_sized_int
A signed integer type that's guaranteed to be large enough to hold a pointer without truncating it.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
long long int64
A platform-independent 64-bit integer type.
void timerCallback() override
The user-defined callback routine that actually gets called periodically.