]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsLoader.cpp
#9376 prepare use of Length in lyxrc - move the class Length to support
[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 <queue>
25 #include <memory>
26 #include <set>
27
28 using namespace std;
29 using namespace lyx::support;
30
31 namespace lyx {
32
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         // Disconnected when this is destroyed
111         timer.timeout.connect([this](){ loadNext(); });
112 }
113
114
115 void LoaderQueue::startLoader()
116 {
117         LYXERR(Debug::GRAPHICS, "LoaderQueue: waking up");
118         running_ = true;
119         timer.setTimeout(s_millisecs_);
120         timer.start();
121 }
122
123
124 void LoaderQueue::stopLoader()
125 {
126         timer.stop();
127         running_ = false ;
128         LYXERR(Debug::GRAPHICS, "LoaderQueue: I'm going to sleep");
129 }
130
131
132 bool LoaderQueue::running() const
133 {
134         return running_ ;
135 }
136
137
138 void LoaderQueue::touch(Cache::ItemPtr const & item)
139 {
140         if (! cache_set_.insert(item).second) {
141                 list<Cache::ItemPtr>::iterator
142                         it = cache_queue_.begin();
143                 list<Cache::ItemPtr>::iterator
144                         end = cache_queue_.end();
145
146                 it = find(it, end, item);
147                 if (it != end)
148                         cache_queue_.erase(it);
149         }
150         cache_queue_.push_front(item);
151         if (!running_)
152                 startLoader();
153 }
154
155
156
157 /////////////////////////////////////////////////////////////////////
158 //
159 // GraphicsLoader
160 //
161 /////////////////////////////////////////////////////////////////////
162
163 typedef std::shared_ptr<Image> ImagePtr;
164
165 class Loader::Impl {
166         friend class Loader;
167 public:
168         ///
169         Impl(FileName const & doc_file);
170         ///
171         ~Impl();
172         ///
173         void resetFile(FileName const &);
174         ///
175         void resetParams(Params const &);
176         ///
177         void createPixmap();
178         ///
179         void startLoading();
180         ///
181         Params const & params() const { return params_; }
182
183         ///
184         FileName doc_file_;
185         /// The loading status of the image.
186         ImageStatus status_;
187         /** Must store a copy of the cached item to ensure that it is not
188          *  erased unexpectedly by the cache itself.
189          */
190         Cache::ItemPtr cached_item_;
191         /// We modify a local copy of the image once it is loaded.
192         ImagePtr image_;
193         /// This signal is emitted when the image loading status changes.
194         signals2::signal<void()> signal_;
195         /// The connection of the signal statusChanged
196         signals2::scoped_connection connection_;
197
198         double displayPixelRatio() const
199         {
200                 return params_.pixel_ratio;
201         }
202         void setDisplayPixelRatio(double scale)
203         {
204                 params_.pixel_ratio = scale;
205         }
206
207 private:
208         ///
209         void statusChanged();
210         ///
211         void checkedLoading();
212
213         ///
214         Params params_;
215 };
216
217
218 Loader::Loader(FileName const & doc_file)
219         : pimpl_(new Impl(doc_file))
220 {}
221
222
223 Loader::Loader(FileName const & doc_file, FileName const & file, bool display)
224         : pimpl_(new Impl(doc_file))
225 {
226         reset(file, display);
227 }
228
229
230 Loader::Loader(FileName const & doc_file, FileName const & file, Params const & params)
231         : pimpl_(new Impl(doc_file))
232 {
233         reset(file, params);
234 }
235
236
237 Loader::Loader(FileName const & doc_file, Loader const & other)
238         : pimpl_(new Impl(doc_file))
239 {
240         Params const & params = other.pimpl_->params();
241         reset(params.filename, params);
242 }
243
244
245 Loader::Loader(Loader const & other)
246         : pimpl_(new Impl(other.pimpl_->doc_file_))
247 {
248         Params const & params = other.pimpl_->params();
249         reset(params.filename, params);
250 }
251
252
253 Loader::~Loader()
254 {
255         delete pimpl_;
256 }
257
258
259 Loader & Loader::operator=(Loader const & other)
260 {
261   LASSERT(false, /**/);
262         if (this != &other) {
263                 delete pimpl_;
264                 pimpl_ = new Impl(other.pimpl_->doc_file_);
265                 Params const & params = other.pimpl_->params();
266                 reset(params.filename, params);
267         }
268         return *this;
269 }
270
271
272 void Loader::reset(FileName const & file, bool display) const
273 {
274         Params params;
275         params.display = display;
276         pimpl_->resetParams(params);
277
278         pimpl_->resetFile(file);
279         pimpl_->createPixmap();
280 }
281
282
283 void Loader::reset(FileName const & file, Params const & params) const
284 {
285         pimpl_->resetParams(params);
286         pimpl_->resetFile(file);
287         pimpl_->createPixmap();
288 }
289
290
291 void Loader::reset(Params const & params) const
292 {
293         pimpl_->resetParams(params);
294         pimpl_->createPixmap();
295 }
296
297
298 void Loader::startLoading() const
299 {
300         if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_
301             || pimpl_->cached_item_->status() == Converting)
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 void Loader::checkModifiedAsync() const
332 {
333         if (!pimpl_->cached_item_)
334                 return;
335
336         pimpl_->cached_item_->checkModifiedAsync();
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 signals2::connection Loader::connect(slot 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                 try {
407                         // This can in theory throw a BufferException
408                         connection_.disconnect();
409                 } catch (...) {
410                         LYXERR(Debug::GRAPHICS, "Unable to disconnect signal.");
411                 }
412                 cached_item_.reset();
413                 if (status_ != Converting) {
414                         Cache::get().remove(old_file);
415                 } else {
416                         //TODO remove cache item when it is not busy any more, see #7163
417                 }
418         }
419
420         status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
421         image_.reset();
422
423         if (cached_item_ || file.empty())
424                 return;
425
426         Cache & gc = Cache::get();
427         if (!gc.inCache(file))
428                 gc.add(file, doc_file_);
429
430         // We /must/ make a local copy of this.
431         cached_item_ = gc.item(file);
432         status_ = cached_item_->status();
433
434         if (continue_monitoring && !cached_item_->monitoring())
435                 cached_item_->startMonitoring();
436
437         // This is a scoped connection
438         connection_ = cached_item_->connect([this](){ statusChanged(); });
439 }
440
441
442 void Loader::Impl::resetParams(Params const & params)
443 {
444         if (params == params_)
445                 return;
446
447         params_ = params;
448         status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
449         image_.reset();
450 }
451
452
453 void Loader::Impl::statusChanged()
454 {
455         status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
456         createPixmap();
457         signal_();
458 }
459
460
461 void Loader::Impl::createPixmap()
462 {
463         if (!params_.display || status_ != Loaded)
464                 return;
465
466         if (!cached_item_) {
467                 LYXERR(Debug::GRAPHICS, "pixmap not cached yet");
468                 return;
469         }
470
471         if (!cached_item_->image()) {
472                 // There must have been a problem reading the file.
473                 LYXERR(Debug::GRAPHICS, "Graphics file not loaded.");
474                 return;
475         }
476
477         image_.reset(cached_item_->image()->clone());
478
479         if (params_.pixel_ratio == 1.0) {
480                 string filename = cached_item_->filename().absFileName();
481                 size_t idx = filename.find_last_of('.');
482                 if (idx != string::npos && idx > 3) {
483                         if (filename.substr(idx - 3, 3) == "@2x") {
484                                 params_.pixel_ratio = 2.0;
485                         }
486                 }
487         }
488
489         bool const success = image_->setPixmap(params_);
490
491         if (success) {
492                 status_ = Ready;
493         } else {
494                 image_.reset();
495                 status_ = ErrorGeneratingPixmap;
496         }
497 }
498
499 void Loader::Impl::startLoading()
500 {
501         if (status_ != WaitingToLoad)
502                 return;
503
504         if (cached_item_->tryDisplayFormat()) {
505                 status_ = Loaded;
506                 createPixmap();
507                 return;
508         }
509
510         LoaderQueue::get().touch(cached_item_);
511 }
512
513
514 } // namespace graphics
515 } // namespace lyx