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