]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsLoader.cpp
Use call_once to ensure something is only called once
[lyx.git] / src / graphics / GraphicsLoader.cpp
1 /**
2  * \file GraphicsLoader.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GraphicsLoader.h"
14
15 #include "GraphicsCacheItem.h"
16 #include "GraphicsImage.h"
17 #include "GraphicsParams.h"
18 #include "GraphicsCache.h"
19
20 #include "support/debug.h"
21 #include "support/Timeout.h"
22
23 #include "support/bind.h"
24
25 #include <queue>
26 #include <memory>
27 #include <set>
28
29 using namespace std;
30 using namespace lyx::support;
31
32 namespace lyx {
33 namespace graphics {
34
35
36 /////////////////////////////////////////////////////////////////////
37 //
38 // LoaderQueue
39 //
40 /////////////////////////////////////////////////////////////////////
41
42 class LoaderQueue {
43 public:
44         /// Use this to request that the item is loaded.
45         void touch(Cache::ItemPtr const & item);
46         /// Query whether the clock is ticking.
47         bool running() const;
48         ///get the and only instance of the class
49         static LoaderQueue & get();
50 private:
51         /// This class is a singleton class... use LoaderQueue::get() instead
52         LoaderQueue();
53         /// The in-progress loading queue (elements are unique here).
54         list<Cache::ItemPtr> cache_queue_;
55         /// Used to make the insertion of new elements faster.
56         set<Cache::ItemPtr> cache_set_;
57         /// Newly touched elements go here. loadNext moves them to cache_queue_
58         queue<Cache::ItemPtr> bucket_;
59         ///
60         Timeout timer;
61         ///
62         bool running_;
63
64         /** This is the 'threaded' method, that does the loading in the
65          *  background.
66          */
67         void loadNext();
68         ///
69         void startLoader();
70         ///
71         void stopLoader();
72 };
73
74
75
76 //static int const s_numimages_ = 5;
77 static int const s_numimages_ = 10;
78 static int const s_millisecs_ = 500;
79
80
81 LoaderQueue & LoaderQueue::get()
82 {
83         static LoaderQueue singleton;
84         return singleton;
85 }
86
87
88 void LoaderQueue::loadNext()
89 {
90         LYXERR(Debug::GRAPHICS, "LoaderQueue: "
91                 << cache_queue_.size() << " items in the queue");
92         int counter = s_numimages_;
93         while (!cache_queue_.empty() && counter--) {
94                 Cache::ItemPtr ptr = cache_queue_.front();
95                 cache_set_.erase(ptr);
96                 cache_queue_.pop_front();
97                 if (ptr->status() == WaitingToLoad)
98                         ptr->startLoading();
99         }
100         if (!cache_queue_.empty())
101                 startLoader();
102         else
103                 stopLoader();
104 }
105
106
107 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
108                              running_(false)
109 {
110         timer.timeout.connect(bind(&LoaderQueue::loadNext, this));
111 }
112
113
114 void LoaderQueue::startLoader()
115 {
116         LYXERR(Debug::GRAPHICS, "LoaderQueue: waking up");
117         running_ = true ;
118         timer.setTimeout(s_millisecs_);
119         timer.start();
120 }
121
122
123 void LoaderQueue::stopLoader()
124 {
125         timer.stop();
126         running_ = false ;
127         LYXERR(Debug::GRAPHICS, "LoaderQueue: I'm going to sleep");
128 }
129
130
131 bool LoaderQueue::running() const
132 {
133         return running_ ;
134 }
135
136
137 void LoaderQueue::touch(Cache::ItemPtr const & item)
138 {
139         if (! cache_set_.insert(item).second) {
140                 list<Cache::ItemPtr>::iterator
141                         it = cache_queue_.begin();
142                 list<Cache::ItemPtr>::iterator
143                         end = cache_queue_.end();
144
145                 it = find(it, end, item);
146                 if (it != end)
147                         cache_queue_.erase(it);
148         }
149         cache_queue_.push_front(item);
150         if (!running_)
151                 startLoader();
152 }
153
154
155
156 /////////////////////////////////////////////////////////////////////
157 //
158 // GraphicsLoader
159 //
160 /////////////////////////////////////////////////////////////////////
161
162 typedef std::shared_ptr<Image> ImagePtr;
163
164 class Loader::Impl : public boost::signals2::trackable {
165 public:
166         ///
167         Impl();
168         ///
169         ~Impl();
170         ///
171         void resetFile(FileName const &);
172         ///
173         void resetParams(Params const &);
174         ///
175         void createPixmap();
176         ///
177         void startLoading();
178         ///
179         Params const & params() const { return params_; }
180
181         /// The loading status of the image.
182         ImageStatus status_;
183         /** Must store a copy of the cached item to ensure that it is not
184          *  erased unexpectedly by the cache itself.
185          */
186         Cache::ItemPtr cached_item_;
187         /// We modify a local copy of the image once it is loaded.
188         ImagePtr image_;
189         /// This signal is emitted when the image loading status changes.
190         boost::signals2::signal<void()> signal_;
191         /// The connection of the signal StatusChanged  
192         boost::signals2::connection sc_;
193
194         double displayPixelRatio() const
195         {
196                 return params_.pixel_ratio;
197         }
198         void setDisplayPixelRatio(double scale)
199         {
200                 params_.pixel_ratio = scale;
201         }
202
203 private:
204         ///
205         void statusChanged();
206         ///
207         void checkedLoading();
208
209         ///
210         Params params_;
211 };
212
213
214 Loader::Loader()
215         : pimpl_(new Impl)
216 {}
217
218
219 Loader::Loader(FileName const & file, bool display)
220         : pimpl_(new Impl)
221 {
222         reset(file, display);
223 }
224
225
226 Loader::Loader(FileName const & file, Params const & params)
227         : pimpl_(new Impl)
228 {
229         reset(file, params);
230 }
231
232
233 Loader::Loader(Loader const & other)
234         : pimpl_(new Impl)
235 {
236         Params const & params = other.pimpl_->params();
237         reset(params.filename, params);
238 }
239
240
241 Loader::~Loader()
242 {
243         delete pimpl_;
244 }
245
246
247 Loader & Loader::operator=(Loader const & other)
248 {
249         if (this != &other) {
250                 Params const & params = other.pimpl_->params();
251                 reset(params.filename, params);
252         }
253         return *this;
254 }
255
256
257 void Loader::reset(FileName const & file, bool display) const
258 {
259         Params params;
260         params.display = display;
261         pimpl_->resetParams(params);
262
263         pimpl_->resetFile(file);
264         pimpl_->createPixmap();
265 }
266
267
268 void Loader::reset(FileName const & file, Params const & params) const
269 {
270         pimpl_->resetParams(params);
271         pimpl_->resetFile(file);
272         pimpl_->createPixmap();
273 }
274
275
276 void Loader::reset(Params const & params) const
277 {
278         pimpl_->resetParams(params);
279         pimpl_->createPixmap();
280 }
281
282
283 void Loader::startLoading() const
284 {
285         if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_)
286                 return;
287         pimpl_->startLoading();
288 }
289
290
291 void Loader::reload() const 
292 {
293         pimpl_->cached_item_->startLoading();
294 }
295
296
297 void Loader::startMonitoring() const
298 {
299         if (!pimpl_->cached_item_)
300                 return;
301
302         pimpl_->cached_item_->startMonitoring();
303 }
304
305
306 bool Loader::monitoring() const
307 {
308         if (!pimpl_->cached_item_)
309                 return false;
310
311         return pimpl_->cached_item_->monitoring();
312 }
313
314
315 unsigned long Loader::checksum() const
316 {
317         if (!pimpl_->cached_item_)
318                 return 0;
319
320         return pimpl_->cached_item_->checksum();
321 }
322
323
324 FileName const & Loader::filename() const
325 {
326         static FileName const empty;
327         return pimpl_->cached_item_ ?
328                 pimpl_->cached_item_->filename() : empty;
329 }
330
331
332 ImageStatus Loader::status() const
333 {
334         return pimpl_->status_;
335 }
336
337
338 double Loader::displayPixelRatio() const
339 {
340         return pimpl_->displayPixelRatio();
341 }
342
343
344 void Loader::setDisplayPixelRatio(double scale)
345 {
346         pimpl_->setDisplayPixelRatio(scale);
347 }
348
349
350 boost::signals2::connection Loader::connect(slot_type const & slot) const
351 {
352         return pimpl_->signal_.connect(slot);
353 }
354
355
356 Image const * Loader::image() const
357 {
358         return pimpl_->image_.get();
359 }
360
361
362 Loader::Impl::Impl()
363         : status_(WaitingToLoad)
364 {
365 }
366
367
368 Loader::Impl::~Impl()
369 {
370         resetFile(FileName());
371 }
372
373
374 void Loader::Impl::resetFile(FileName const & file)
375 {
376         FileName const old_file = cached_item_ ?
377                 cached_item_->filename() : FileName();
378
379         if (file == old_file)
380                 return;
381
382         // If monitoring() the current file, should continue to monitor the
383         // new file.
384         bool continue_monitoring = false;
385
386         if (!old_file.empty()) {
387                 continue_monitoring = cached_item_->monitoring();
388                 // cached_item_ is going to be reset, so the connected
389                 // signal needs to be disconnected.
390                 sc_.disconnect();
391                 cached_item_.reset();
392                 if (status_ != Converting) {
393                         Cache::get().remove(old_file);
394                 } else {
395                         //TODO remove cache item when it is not busy any more, see #7163
396                 }
397         }
398
399         status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
400         image_.reset();
401
402         if (cached_item_ || file.empty())
403                 return;
404
405         Cache & gc = Cache::get();
406         if (!gc.inCache(file))
407                 gc.add(file);
408
409         // We /must/ make a local copy of this.
410         cached_item_ = gc.item(file);
411         status_ = cached_item_->status();
412
413         if (continue_monitoring && !cached_item_->monitoring())
414                 cached_item_->startMonitoring();
415
416         sc_ = cached_item_->connect(bind(&Impl::statusChanged, this));
417 }
418
419
420 void Loader::Impl::resetParams(Params const & params)
421 {
422         if (params == params_)
423                 return;
424
425         params_ = params;
426         status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
427         image_.reset();
428 }
429
430
431 void Loader::Impl::statusChanged()
432 {
433         status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
434         createPixmap();
435         signal_();
436 }
437
438
439 void Loader::Impl::createPixmap()
440 {
441         if (!params_.display || status_ != Loaded)
442                 return;
443
444         if (!cached_item_) {
445                 LYXERR(Debug::GRAPHICS, "pixmap not cached yet");
446                 return;
447         }
448
449         if (!cached_item_->image()) {
450                 // There must have been a problem reading the file.
451                 LYXERR(Debug::GRAPHICS, "Graphics file not loaded.");
452                 return;
453         }
454
455         image_.reset(cached_item_->image()->clone());
456
457         if (params_.pixel_ratio == 1.0) {
458                 string filename = cached_item_->filename().absFileName();
459                 size_t idx = filename.find_last_of('.');
460                 if (idx != string::npos && idx > 3) {
461                         if (filename.substr(idx - 3, 3) == "@2x") {
462                                 params_.pixel_ratio = 2.0;
463                         }
464                 }
465         }
466
467         bool const success = image_->setPixmap(params_);
468
469         if (success) {
470                 status_ = Ready;
471         } else {
472                 image_.reset();
473                 status_ = ErrorGeneratingPixmap;
474         }
475 }
476
477 void Loader::Impl::startLoading()
478 {
479         if (status_ != WaitingToLoad)
480                 return;
481
482         if (cached_item_->tryDisplayFormat()) {
483                 status_ = Loaded;
484                 createPixmap();
485                 return;
486         }
487
488         LoaderQueue::get().touch(cached_item_);
489 }
490
491
492 } // namespace graphics
493 } // namespace lyx