44
44
void LoadingTask::execute()
46
46
if (m_loadingTaskStatus == LoadingTaskStatusStopping)
48
51
DImg img(m_loadingDescription.filePath, this, m_loadingDescription.rawDecodingSettings);
49
52
m_thread->taskHasFinished();
50
53
m_thread->imageLoaded(m_loadingDescription, img);
55
58
return TaskTypeLoading;
58
void LoadingTask::progressInfo(const DImg *, float progress)
61
void LoadingTask::progressInfo(const DImg*, float progress)
60
63
if (m_loadingTaskStatus == LoadingTaskStatusLoading)
62
65
if (m_thread->querySendNotifyEvent())
63
67
m_thread->loadingProgress(m_loadingDescription, progress);
67
bool LoadingTask::continueQuery(const DImg *)
72
bool LoadingTask::continueQuery(const DImg*)
69
74
return m_loadingTaskStatus != LoadingTaskStatusStopping;
79
84
SharedLoadingTask::SharedLoadingTask(LoadSaveThread* thread, LoadingDescription description,
80
85
LoadSaveThread::AccessMode mode, LoadingTaskStatus loadingTaskStatus)
81
: LoadingTask(thread, description, loadingTaskStatus),
82
m_completed(false), m_accessMode(mode), m_usedProcess(0), m_resultLoadingDescription(description)
86
: LoadingTask(thread, description, loadingTaskStatus),
87
m_completed(false), m_accessMode(mode), m_usedProcess(0), m_resultLoadingDescription(description)
84
89
if (m_accessMode == LoadSaveThread::AccessModeRead && needsPostProcessing())
85
91
m_accessMode = LoadSaveThread::AccessModeReadWrite;
88
95
void SharedLoadingTask::execute()
90
97
if (m_loadingTaskStatus == LoadingTaskStatusStopping)
92
102
// send StartedLoadingEvent from each single Task, not via LoadingProcess list
93
103
m_thread->imageStartedLoading(m_loadingDescription);
95
LoadingCache *cache = LoadingCache::cache();
105
LoadingCache* cache = LoadingCache::cache();
97
107
LoadingCache::CacheLock lock(cache);
99
109
// find possible cached images
101
111
QStringList lookupKeys = m_loadingDescription.lookupCacheKeys();
102
112
foreach (const QString& key, lookupKeys)
122
136
// image is found in image cache, loading is successful
123
137
m_img = *cachedImg;
124
139
if (accessMode() == LoadSaveThread::AccessModeReadWrite)
125
141
m_img = m_img.copy();
126
144
// continues after else clause...
130
148
// find possible running loading process
131
149
m_usedProcess = 0;
132
for ( QStringList::Iterator it = lookupKeys.begin(); it != lookupKeys.end(); ++it ) {
151
for ( QStringList::Iterator it = lookupKeys.begin(); it != lookupKeys.end(); ++it )
133
153
if ( (m_usedProcess = cache->retrieveLoadingProcess(*it)) )
143
163
// attach this thread to the other thread, wait until loading
145
165
m_usedProcess->addListener(this);
146
167
// break loop when either the loading has completed, or this task is being stopped
147
168
while ( m_loadingTaskStatus != LoadingTaskStatusStopping && m_usedProcess && !m_usedProcess->completed() )
148
170
lock.timedWait();
149
173
// remove listener from process
150
174
if (m_usedProcess)
151
176
m_usedProcess->removeListener(this);
152
179
// wake up the process which is waiting until all listeners have removed themselves
154
181
// set to 0, as checked in setStatus
187
214
bool isCached = false;
189
216
LoadingCache::CacheLock lock(cache);
190
218
// put (valid) image into cache of loaded images
191
219
if (!m_img.isNull())
192
221
isCached = cache->putImage(m_loadingDescription.cacheKey(), new DImg(m_img), m_loadingDescription.filePath);
193
224
// remove this from the list of loading processes in cache
194
225
cache->removeLoadingProcess(this);
203
234
// dispatch image to all listeners, including this
204
235
for (int i=0; i<m_listeners.count(); ++i)
206
LoadingProcessListener *l = m_listeners[i];
237
LoadingProcessListener* l = m_listeners[i];
207
239
if (l->accessMode() == LoadSaveThread::AccessModeReadWrite)
209
241
// If a listener requested ReadWrite access, it gets a deep copy.
221
253
removeListener(this);
222
254
// wake all listeners waiting on cache condVar, so that they remove themselves
224
257
// wait until all listeners have removed themselves
225
258
while (m_listeners.count() != 0)
226
260
lock.timedWait();
227
263
// set to 0, as checked in setStatus
228
264
m_usedProcess = 0;
296
332
removeListener(this);
299
void SharedLoadingTask::progressInfo(const DImg *, float progress)
335
void SharedLoadingTask::progressInfo(const DImg*, float progress)
301
337
if (m_loadingTaskStatus == LoadingTaskStatusLoading)
303
LoadingCache *cache = LoadingCache::cache();
339
LoadingCache* cache = LoadingCache::cache();
304
340
LoadingCache::CacheLock lock(cache);
306
342
for (int i=0; i<m_listeners.size(); ++i)
308
LoadingProcessListener *l = m_listeners[i];
344
LoadingProcessListener* l = m_listeners[i];
309
346
if (l->querySendNotifyEvent())
310
348
l->loadSaveNotifier()->loadingProgress(m_loadingDescription, progress);
315
bool SharedLoadingTask::continueQuery(const DImg *)
354
bool SharedLoadingTask::continueQuery(const DImg*)
317
356
// If this is called, the thread is currently loading an image.
318
357
// In shared loading, we cannot stop until all listeners have been removed as well
322
361
void SharedLoadingTask::setStatus(LoadingTaskStatus status)
324
363
m_loadingTaskStatus = status;
325
365
if (m_loadingTaskStatus == LoadingTaskStatusStopping)
327
LoadingCache *cache = LoadingCache::cache();
367
LoadingCache* cache = LoadingCache::cache();
328
368
LoadingCache::CacheLock lock(cache);
330
370
// check for m_usedProcess, to avoid race condition that it has finished before
355
395
return m_loadingDescription.cacheKey();
358
void SharedLoadingTask::addListener(LoadingProcessListener *listener)
398
void SharedLoadingTask::addListener(LoadingProcessListener* listener)
360
400
m_listeners << listener;
363
void SharedLoadingTask::removeListener(LoadingProcessListener *listener)
403
void SharedLoadingTask::removeListener(LoadingProcessListener* listener)
365
405
m_listeners.removeAll(listener);
368
void SharedLoadingTask::notifyNewLoadingProcess(LoadingProcess *process, LoadingDescription description)
408
void SharedLoadingTask::notifyNewLoadingProcess(LoadingProcess* process, LoadingDescription description)
370
410
// Ok, we are notified that another task has been started in another thread.
371
411
// We are of course only interested if the task loads the same file,
416
456
return TaskTypeSaving;
419
void SavingTask::progressInfo(const DImg *, float progress)
459
void SavingTask::progressInfo(const DImg*, float progress)
421
461
if (m_thread->querySendNotifyEvent())
422
463
m_thread->savingProgress(m_filePath, progress);
425
bool SavingTask::continueQuery(const DImg *)
467
bool SavingTask::continueQuery(const DImg*)
427
469
return m_savingTaskStatus != SavingTaskStatusStopping;