Repository navigation
Expand file tree
/
Copy paththumbnail_thread.cpp
More file actions
247 lines (227 loc) · 6.75 KB
/
Copy paththumbnail_thread.cpp
File metadata and controls
247 lines (227 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2012 Twan van Laarhoven and Sean Hunt |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <gui/thumbnail_thread.hpp>
#include <util/platform.hpp>
#include <util/error.hpp>
#include <wx/thread.h>
using std::make_pair;
using std::pair;
// ----------------------------------------------------------------------------- : Image Cache
String user_settings_dir();
String image_cache_dir() {
String dir = user_settings_dir() + _("/cache");
if (!wxDirExists(dir)) wxMkdir(dir);
return dir + _("/");
}
/// A name that is safe to use as a filename, for the cache
String safe_filename(const String& str) {
String ret; ret.reserve(str.size());
for(const auto& c : str) {
if (isAlnum(c)) {
ret += c;
} else if (c==_(' ') || c==_('-')) {
ret += _('-');
} else {
ret += _('_');
}
}
return ret;
}
// ----------------------------------------------------------------------------- : ThumbnailThreadWorker
class ThumbnailThreadWorker : public wxThread {
public:
ThumbnailThreadWorker(ThumbnailThread* parent);
virtual ExitCode Entry();
ThumbnailRequestP current; ///< Request we are working on
ThumbnailThread* parent;
bool stop; ///< Suspend computation
};
ThumbnailThreadWorker::ThumbnailThreadWorker(ThumbnailThread* parent)
: parent(parent)
, stop(false)
{}
wxThread::ExitCode ThumbnailThreadWorker::Entry() {
while (true) {
do {
Sleep(1);
} while (stop);
// get a request
{
wxMutexLocker lock(parent->mutex);
if (parent->open_requests.empty()) {
parent->worker = nullptr;
return 0; // No more requests
}
current = parent->open_requests.front();
parent->open_requests.pop_front();
}
// perform request
Image img;
try {
img = current->generate();
} catch (const Error& e) {
handle_error(e);
} catch (...) {
}
// store in cache
if (img.Ok()) {
String filename = image_cache_dir() + safe_filename(current->cache_name) + _(".png");
img.SaveFile(filename, wxBITMAP_TYPE_PNG);
// set modification time
wxFileName fn(filename);
fn.SetTimes(0, ¤t->modified, 0);
}
// store result in closed request list
{
wxMutexLocker lock(parent->mutex);
parent->closed_requests.push_back(make_pair(current,img));
current = ThumbnailRequestP();
parent->completed.Signal();
}
}
}
bool operator < (const ThumbnailRequestP& a, const ThumbnailRequestP& b) {
if (a->owner < b->owner) return true;
if (a->owner > b->owner) return false;
return a->cache_name < b->cache_name;
}
// ----------------------------------------------------------------------------- : ThumbnailThread
ThumbnailThread thumbnail_thread;
ThumbnailThread::ThumbnailThread()
: completed(mutex)
, worker(nullptr)
{}
void ThumbnailThread::request(const ThumbnailRequestP& request) {
assert(wxThread::IsMain());
// Is the request in progress?
if (request_names.find(request) != request_names.end()) {
return;
}
// Is the image in the cache?
String filename = image_cache_dir() + safe_filename(request->cache_name) + _(".png");
wxFileName fn(filename);
if (fn.FileExists()) {
wxDateTime modified;
if (fn.GetTimes(0, &modified, 0) && modified >= request->modified) {
// yes it is
Image img(filename);
request->store(img);
return;
}
}
if (request->threadSafe()) {
request_names.insert(request);
// request generation
{
wxMutexLocker lock(mutex);
open_requests.push_back(request);
}
// is there a worker?
if (!worker) {
worker = new ThumbnailThreadWorker(this);
worker->Create();
worker->Run();
}
}
else {
Image img;
try {
img = request->generate();
} catch (const Error& e) {
handle_error(e);
} catch (...) {
}
// store in cache
if (img.Ok()) {
String filename = image_cache_dir() + safe_filename(request->cache_name) + _(".png");
img.SaveFile(filename, wxBITMAP_TYPE_PNG);
// set modification time
wxFileName fn(filename);
fn.SetTimes(0, &request->modified, 0);
}
{
wxMutexLocker lock(mutex);
closed_requests.push_back(make_pair(request,img));
completed.Signal();
}
}
}
bool ThumbnailThread::done(void* owner) {
assert(wxThread::IsMain());
// find finished requests
vector<pair<ThumbnailRequestP,Image> > finished;
{
wxMutexLocker lock(mutex);
for (size_t i = 0 ; i < closed_requests.size() ; ) {
if (closed_requests[i].first->owner == owner) {
// move to finished list
finished.push_back(closed_requests[i]);
closed_requests.erase(closed_requests.begin() + i, closed_requests.begin() + i + 1);
} else {
++i;
}
}
}
// store them
for(auto& r : finished) {
// store image
r.first->store(r.second);
// remove from name list
request_names.erase(r.first);
}
return !finished.empty();
}
void ThumbnailThread::abort(void* owner) {
assert(wxThread::IsMain());
mutex.Lock();
if (worker && worker->current && worker->current->owner == owner) {
// a request for this owner is in progress, wait until it is done
worker->stop = true;
completed.Wait();
mutex.Lock();
worker->stop = false;
}
// remove open requests for this owner
for (size_t i = 0 ; i < open_requests.size() ; ) {
if (open_requests[i]->owner == owner) {
// remove
request_names.erase(open_requests[i]);
open_requests.erase(open_requests.begin() + i, open_requests.begin() + i + 1);
} else {
++i;
}
}
// remove closed requests for this owner
for (size_t i = 0 ; i < closed_requests.size() ; ) {
if (closed_requests[i].first->owner == owner) {
// remove
request_names.erase(closed_requests[i].first);
closed_requests.erase(closed_requests.begin() + i, closed_requests.begin() + i + 1);
} else {
++i;
}
}
mutex.Unlock();
}
void ThumbnailThread::abortAll() {
assert(wxThread::IsMain());
mutex.Lock();
open_requests.clear();
closed_requests.clear();
request_names.clear();
// end worker
if (worker && worker->current) {
completed.Wait();
} else {
mutex.Unlock();
}
// There may still be a worker, but if there is, it has no current object, so it is
// in, before or after the stop loop. It can do nothing but end.
// An unfortunate side effect is that we might leak some memory (of the worker object),
// when the thread gets Kill()ed by wx.
}