Debian Codegraph @

core/libs/threadimageio/thumb/thumbnailtask.cpp

raw
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2007-06-05
7 * Description : Multithreaded loader for thumbnails
8 *
9 * SPDX-FileCopyrightText: 2006-2008 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
10 * SPDX-FileCopyrightText: 2006-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
11 *
12 * SPDX-License-Identifier: GPL-2.0-or-later
13 *
14 * ============================================================ */
15
16 #include "thumbnailtask.h"
17
18 // C++ includes
19
20 #include <cmath>
21
22 // Qt includes
23
24 #include <QApplication>
25 #include <QVariant>
26
27 // Local includes
28
29 #include "drawdecoder.h"
30 #include "digikam_debug.h"
31 #include "dmetadata.h"
32 #include "iccmanager.h"
33 #include "jpegutils.h"
34 #include "metaenginesettings.h"
35 #include "thumbnailloadthread.h"
36 #include "thumbnailcreator.h"
37
38 namespace Digikam
39 {
40
41 ThumbnailLoadingTask::ThumbnailLoadingTask(LoadSaveThread* const thread, const LoadingDescription& description)
42 : SharedLoadingTask(thread,
43 description,
44 LoadSaveThread::AccessModeRead,
45 LoadingTaskStatusLoading)
46 {
47 // Thread must be a ThumbnailLoadThread, crashes otherwise.
48 // Not a clean but pragmatic solution.
49
50 const ThumbnailLoadThread* const thumbThread = static_cast<ThumbnailLoadThread*>(thread);
51 m_creator = thumbThread->thumbnailCreator();
52 }
53
54 void ThumbnailLoadingTask::execute()
55 {
56 if (m_loadingTaskStatus == LoadingTaskStatusStopping)
57 {
58 m_thread->taskHasFinished();
59
60 return;
61 }
62
63 if (m_loadingDescription.previewParameters.onlyPregenerate())
64 {
65 setupCreator();
66
67 switch (m_loadingDescription.previewParameters.type)
68 {
69 case LoadingDescription::PreviewParameters::Thumbnail:
70 {
71 m_creator->pregenerate(m_loadingDescription.thumbnailIdentifier());
72 break;
73 }
74
75 case LoadingDescription::PreviewParameters::DetailThumbnail:
76 {
77 m_creator->pregenerateDetail(m_loadingDescription.thumbnailIdentifier(),
78 m_loadingDescription.previewParameters.extraParameter.toRect());
79 break;
80 }
81
82 default:
83 {
84 break;
85 }
86 }
87
88 m_thread->taskHasFinished();
89
90 // do not Q_EMIT any signal
91
92 return;
93 }
94
95 LoadingCache* const cache = LoadingCache::cache();
96 {
97 LoadingCache::CacheLock lock(cache);
98
99 // find possible cached images
100
101 const QImage* const cachedImage = cache->retrieveThumbnail(m_loadingDescription.cacheKey());
102
103 if (cachedImage)
104 {
105 m_qimage = *cachedImage;
106 }
107
108 if (m_qimage.isNull())
109 {
110 // find possible running loading process
111 // do not wait on other loading processes?
112
113 LoadingProcess* const usedProcess = cache->retrieveLoadingProcess(m_loadingDescription.cacheKey());
114
115 if (usedProcess)
116 {
117 // Other process is right now loading this image.
118 // Add this task to the list of listeners and
119 // attach this thread to the other thread, wait until loading
120 // has finished.
121
122 usedProcess->addListener(this);
123
124 // break loop when either the loading has completed, or this task is being stopped
125
126 // cppcheck-suppress knownConditionTrueFalse
127 while ((m_loadingTaskStatus != LoadingTaskStatusStopping) && !usedProcess->completed())
128 {
129 lock.timedWait();
130 }
131
132 // remove listener from process
133
134 usedProcess->removeListener(this);
135
136 // wake up the process which is waiting until all listeners have removed themselves
137
138 lock.wakeAll();
139 }
140 }
141 }
142
143 if (continueQuery() && m_qimage.isNull())
144 {
145 {
146 LoadingCache::CacheLock lock(cache);
147
148 // Neither in cache, nor currently loading in different thread.
149 // Load it here and now, add this LoadingProcess to cache list.
150
151 cache->addLoadingProcess(this);
152
153 // Notify other processes that we are now loading this image.
154 // They might be interested - see notifyNewLoadingProcess below
155
156 cache->notifyNewLoadingProcess(this, m_loadingDescription);
157 }
158
159 // Load or create thumbnail
160
161 setupCreator();
162
163 switch (m_loadingDescription.previewParameters.type)
164 {
165 case LoadingDescription::PreviewParameters::Thumbnail:
166 {
167 m_qimage = m_creator->load(m_loadingDescription.thumbnailIdentifier(),
168 m_loadingDescription.previewParameters.onlyFromStorage());
169 break;
170 }
171
172 case LoadingDescription::PreviewParameters::DetailThumbnail:
173 {
174 m_qimage = m_creator->loadDetail(m_loadingDescription.thumbnailIdentifier(),
175 m_loadingDescription.previewParameters.extraParameter.toRect(),
176 m_loadingDescription.previewParameters.onlyFromStorage());
177 break;
178 }
179
180 default:
181 {
182 break;
183 }
184 }
185
186 if (continueQuery() && !m_qimage.isNull())
187 {
188 postProcess();
189 }
190
191 {
192 LoadingCache::CacheLock lock(cache);
193
194 // remove this from the list of loading processes in cache
195
196 cache->removeLoadingProcess(this);
197
198 // put valid image into cache of loaded images
199
200 if (continueQuery() && !m_qimage.isNull())
201 {
202 cache->putThumbnail(m_loadingDescription.cacheKey(), m_qimage,
203 m_loadingDescription.filePath);
204
205 // dispatch image to all listeners
206
207 for (int i = 0 ; i < m_listeners.count() ; ++i)
208 {
209 ThumbnailLoadingTask* const task = dynamic_cast<ThumbnailLoadingTask*>(m_listeners.at(i));
210
211 if (task)
212 {
213 task->setThumbResult(m_loadingDescription, m_qimage);
214 }
215 }
216 }
217
218 // indicate that loading has finished so that listeners can stop waiting
219
220 m_completed = true;
221
222 // wake all listeners waiting on cache condVar, so that they remove themselves
223
224 lock.wakeAll();
225
226 // wait until all listeners have removed themselves
227
228 while (m_listeners.count() != 0)
229 {
230 lock.timedWait();
231 }
232 }
233 }
234
235 if (!continueQuery())
236 {
237 m_qimage = QImage();
238 }
239
240 m_thread->taskHasFinished();
241 m_thread->thumbnailLoaded(m_loadingDescription, m_qimage);
242 }
243
244 void ThumbnailLoadingTask::setupCreator()
245 {
246 m_creator->setThumbnailSize(m_loadingDescription.previewParameters.size);
247 m_creator->setExifRotate(MetaEngineSettings::instance()->settings().exifRotate);
248 m_creator->setLoadingProperties(this, m_loadingDescription.rawDecodingSettings);
249 }
250
251 void ThumbnailLoadingTask::setThumbResult(const LoadingDescription& loadingDescription, const QImage& qimage)
252 {
253 // This is called from another process's execute while this task is waiting on usedProcess.
254 // Note that loadingDescription need not equal m_loadingDescription (may be superior)
255
256 LoadingDescription tempDescription = loadingDescription;
257
258 // these are taken from our own description
259
260 tempDescription.postProcessingParameters = m_loadingDescription.postProcessingParameters;
261 m_loadingDescription = tempDescription;
262 m_qimage = qimage;
263 }
264
265 void ThumbnailLoadingTask::postProcess()
266 {
267 m_loadingDescription.postProcessingParameters.profile().description();
268
269 switch (m_loadingDescription.postProcessingParameters.colorManagement)
270 {
271 case LoadingDescription::NoColorConversion:
272 {
273 break;
274 }
275
276 case LoadingDescription::ConvertToSRGB:
277 {
278 // Thumbnails are stored in sRGB
279
280 break;
281 }
282
283 case LoadingDescription::ConvertForDisplay:
284 {
285 IccManager::transformForDisplay(m_qimage, m_loadingDescription.postProcessingParameters.profile());
286 break;
287 }
288
289 default:
290 {
291 qCDebug(DIGIKAM_GENERAL_LOG) << "Unsupported postprocessing parameter for thumbnail loading:"
292 << m_loadingDescription.postProcessingParameters.colorManagement;
293 break;
294 }
295 }
296 }
297
298 } // namespace Digikam