]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsLoader.cpp
Avoid duplicate generation of the same preview
[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         // Disconnected when this is destroyed
113         timer.timeout.connect([this](){ loadNext(); });
114 }
115
116
117 void LoaderQueue::startLoader()
118 {
119         LYXERR(Debug::GRAPHICS, "LoaderQueue: waking up");
120         running_ = true;
121         timer.setTimeout(s_millisecs_);
122         timer.start();
123 }
124
125
126 void LoaderQueue::stopLoader()
127 {
128         timer.stop();
129         running_ = false ;
130         LYXERR(Debug::GRAPHICS, "LoaderQueue: I'm going to sleep");
131 }
132
133
134 bool LoaderQueue::running() const
135 {
136         return running_ ;
137 }
138
139
140 void LoaderQueue::touch(Cache::ItemPtr const & item)
141 {
142         if (! cache_set_.insert(item).second) {
143                 list<Cache::ItemPtr>::iterator
144                         it = cache_queue_.begin();
145                 list<Cache::ItemPtr>::iterator
146                         end = cache_queue_.end();
147
148                 it = find(it, end, item);
149                 if (it != end)
150                         cache_queue_.erase(it);
151         }
152         cache_queue_.push_front(item);
153         if (!running_)
154                 startLoader();
155 }
156
157
158
159 /////////////////////////////////////////////////////////////////////
160 //
161 // GraphicsLoader
162 //
163 /////////////////////////////////////////////////////////////////////
164
165 typedef std::shared_ptr<Image> ImagePtr;
166
167 class Loader::Impl {
168         friend class Loader;
169 public:
170         ///
171         Impl(FileName const & doc_file);
172         ///
173         ~Impl();
174         ///
175         void resetFile(FileName const &);
176         ///
177         void resetParams(Params const &);
178         ///
179         void createPixmap();
180         ///
181         void startLoading();
182         ///
183         Params const & params() const { return params_; }
184
185         ///
186         FileName doc_file_;
187         /// The loading status of the image.
188         ImageStatus status_;
189         /** Must store a copy of the cached item to ensure that it is not
190          *  erased unexpectedly by the cache itself.
191          */
192         Cache::ItemPtr cached_item_;
193         /// We modify a local copy of the image once it is loaded.
194         ImagePtr image_;
195         /// This signal is emitted when the image loading status changes.
196         signals2::signal<void()> signal_;
197         /// The connection of the signal statusChanged
198         signals2::scoped_connection connection_;
199
200         double displayPixelRatio() const
201         {
202                 return params_.pixel_ratio;
203         }
204         void setDisplayPixelRatio(double scale)
205         {
206                 params_.pixel_ratio = scale;
207         }
208
209 private:
210         ///
211         void statusChanged();
212         ///
213         void checkedLoading();
214
215         ///
216         Params params_;
217 };
218
219
220 Loader::Loader(FileName const & doc_file)
221         : pimpl_(new Impl(doc_file))
222 {}
223
224
225 Loader::Loader(FileName const & doc_file, FileName const & file, bool display)
226         : pimpl_(new Impl(doc_file))
227 {
228         reset(file, display);
229 }
230
231
232 Loader::Loader(FileName const & doc_file, FileName const & file, Params const & params)
233         : pimpl_(new Impl(doc_file))
234 {
235         reset(file, params);
236 }
237
238
239 Loader::Loader(FileName const & doc_file, Loader const & other)
240         : pimpl_(new Impl(doc_file))
241 {
242         Params const & params = other.pimpl_->params();
243         reset(params.filename, params);
244 }
245
246
247 Loader::Loader(Loader const & other)
248         : pimpl_(new Impl(other.pimpl_->doc_file_))
249 {
250         Params const & params = other.pimpl_->params();
251         reset(params.filename, params);
252 }
253
254
255 Loader::~Loader()
256 {
257         delete pimpl_;
258 }
259
260
261 Loader & Loader::operator=(Loader const & other)
262 {
263   LASSERT(false, /**/);
264         if (this != &other) {
265                 delete pimpl_;
266                 pimpl_ = new Impl(other.pimpl_->doc_file_);
267                 Params const & params = other.pimpl_->params();
268                 reset(params.filename, params);
269         }
270         return *this;
271 }
272
273
274 void Loader::reset(FileName const & file, bool display) const
275 {
276         Params params;
277         params.display = display;
278         pimpl_->resetParams(params);
279
280         pimpl_->resetFile(file);
281         pimpl_->createPixmap();
282 }
283
284
285 void Loader::reset(FileName const & file, Params const & params) const
286 {
287         pimpl_->resetParams(params);
288         pimpl_->resetFile(file);
289         pimpl_->createPixmap();
290 }
291
292
293 void Loader::reset(Params const & params) const
294 {
295         pimpl_->resetParams(params);
296         pimpl_->createPixmap();
297 }
298
299
300 void Loader::startLoading() const
301 {
302         if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_
303             || pimpl_->cached_item_->status() == Converting)
304                 return;
305         pimpl_->startLoading();
306 }
307
308
309 void Loader::reload() const
310 {
311         pimpl_->cached_item_->startLoading();
312 }
313
314
315 void Loader::startMonitoring() const
316 {
317         if (!pimpl_->cached_item_)
318                 return;
319
320         pimpl_->cached_item_->startMonitoring();
321 }
322
323
324 bool Loader::monitoring() const
325 {
326         if (!pimpl_->cached_item_)
327                 return false;
328
329         return pimpl_->cached_item_->monitoring();
330 }
331
332
333 void Loader::checkModifiedAsync() const
334 {
335         if (!pimpl_->cached_item_)
336                 return;
337
338         pimpl_->cached_item_->checkModifiedAsync();
339 }
340
341
342 FileName const & Loader::filename() const
343 {
344         static FileName const empty;
345         return pimpl_->cached_item_ ?
346                 pimpl_->cached_item_->filename() : empty;
347 }
348
349
350 ImageStatus Loader::status() const
351 {
352         return pimpl_->status_;
353 }
354
355
356 double Loader::displayPixelRatio() const
357 {
358         return pimpl_->displayPixelRatio();
359 }
360
361
362 void Loader::setDisplayPixelRatio(double scale)
363 {
364         pimpl_->setDisplayPixelRatio(scale);
365 }
366
367
368 signals2::connection Loader::connect(slot const & slot) const
369 {
370         return pimpl_->signal_.connect(slot);
371 }
372
373
374 Image const * Loader::image() const
375 {
376         return pimpl_->image_.get();
377 }
378
379
380 Loader::Impl::Impl(FileName const & doc_file)
381         : doc_file_(doc_file), status_(WaitingToLoad)
382 {
383 }
384
385
386 Loader::Impl::~Impl()
387 {
388         resetFile(FileName());
389 }
390
391
392 void Loader::Impl::resetFile(FileName const & file)
393 {
394         FileName const old_file = cached_item_ ?
395                 cached_item_->filename() : FileName();
396
397         if (file == old_file)
398                 return;
399
400         // If monitoring() the current file, should continue to monitor the
401         // new file.
402         bool continue_monitoring = false;
403
404         if (!old_file.empty()) {
405                 continue_monitoring = cached_item_->monitoring();
406                 // cached_item_ is going to be reset, so the connected
407                 // signal needs to be disconnected.
408                 try {
409                         // This can in theory throw a BufferException
410                         connection_.disconnect();
411                 } catch (...) {
412                         LYXERR(Debug::GRAPHICS, "Unable to disconnect signal.");
413                 }
414                 cached_item_.reset();
415                 if (status_ != Converting) {
416                         Cache::get().remove(old_file);
417                 } else {
418                         //TODO remove cache item when it is not busy any more, see #7163
419                 }
420         }
421
422         status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
423         image_.reset();
424
425         if (cached_item_ || file.empty())
426                 return;
427
428         Cache & gc = Cache::get();
429         if (!gc.inCache(file))
430                 gc.add(file, doc_file_);
431
432         // We /must/ make a local copy of this.
433         cached_item_ = gc.item(file);
434         status_ = cached_item_->status();
435
436         if (continue_monitoring && !cached_item_->monitoring())
437                 cached_item_->startMonitoring();
438
439         // This is a scoped connection
440         connection_ = cached_item_->connect([this](){ statusChanged(); });
441 }
442
443
444 void Loader::Impl::resetParams(Params const & params)
445 {
446         if (params == params_)
447                 return;
448
449         params_ = params;
450         status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
451         image_.reset();
452 }
453
454
455 void Loader::Impl::statusChanged()
456 {
457         status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
458         createPixmap();
459         signal_();
460 }
461
462
463 void Loader::Impl::createPixmap()
464 {
465         if (!params_.display || status_ != Loaded)
466                 return;
467
468         if (!cached_item_) {
469                 LYXERR(Debug::GRAPHICS, "pixmap not cached yet");
470                 return;
471         }
472
473         if (!cached_item_->image()) {
474                 // There must have been a problem reading the file.
475                 LYXERR(Debug::GRAPHICS, "Graphics file not loaded.");
476                 return;
477         }
478
479         image_.reset(cached_item_->image()->clone());
480
481         if (params_.pixel_ratio == 1.0) {
482                 string filename = cached_item_->filename().absFileName();
483                 size_t idx = filename.find_last_of('.');
484                 if (idx != string::npos && idx > 3) {
485                         if (filename.substr(idx - 3, 3) == "@2x") {
486                                 params_.pixel_ratio = 2.0;
487                         }
488                 }
489         }
490
491         bool const success = image_->setPixmap(params_);
492
493         if (success) {
494                 status_ = Ready;
495         } else {
496                 image_.reset();
497                 status_ = ErrorGeneratingPixmap;
498         }
499 }
500
501 void Loader::Impl::startLoading()
502 {
503         if (status_ != WaitingToLoad)
504                 return;
505
506         if (cached_item_->tryDisplayFormat()) {
507                 status_ = Loaded;
508                 createPixmap();
509                 return;
510         }
511
512         LoaderQueue::get().touch(cached_item_);
513 }
514
515
516 } // namespace graphics
517 } // namespace lyx