2 * \file GraphicsLoader.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
13 #include "GraphicsLoader.h"
15 #include "GraphicsCacheItem.h"
16 #include "GraphicsImage.h"
17 #include "GraphicsParams.h"
18 #include "GraphicsCache.h"
20 #include "support/debug.h"
21 #include "support/lassert.h"
22 #include "support/Timeout.h"
24 #include "support/bind.h"
31 using namespace lyx::support;
38 /////////////////////////////////////////////////////////////////////
42 /////////////////////////////////////////////////////////////////////
46 /// Use this to request that the item is loaded.
47 void touch(Cache::ItemPtr const & item);
48 /// Query whether the clock is ticking.
50 ///get the and only instance of the class
51 static LoaderQueue & get();
53 /// This class is a singleton class... use LoaderQueue::get() instead
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_;
66 /** This is the 'threaded' method, that does the loading in the
78 //static int const s_numimages_ = 5;
79 static int const s_numimages_ = 10;
80 static int const s_millisecs_ = 500;
83 LoaderQueue & LoaderQueue::get()
85 static LoaderQueue singleton;
90 void LoaderQueue::loadNext()
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)
102 if (!cache_queue_.empty())
109 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
112 // Disconnected when this is destroyed
113 timer.timeout.connect([this](){ loadNext(); });
117 void LoaderQueue::startLoader()
119 LYXERR(Debug::GRAPHICS, "LoaderQueue: waking up");
121 timer.setTimeout(s_millisecs_);
126 void LoaderQueue::stopLoader()
130 LYXERR(Debug::GRAPHICS, "LoaderQueue: I'm going to sleep");
134 bool LoaderQueue::running() const
140 void LoaderQueue::touch(Cache::ItemPtr const & item)
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();
148 it = find(it, end, item);
150 cache_queue_.erase(it);
152 cache_queue_.push_front(item);
159 /////////////////////////////////////////////////////////////////////
163 /////////////////////////////////////////////////////////////////////
165 typedef std::shared_ptr<Image> ImagePtr;
171 Impl(FileName const & doc_file);
175 void resetFile(FileName const &);
177 void resetParams(Params const &);
183 Params const & params() const { return params_; }
187 /// The loading status of the image.
189 /** Must store a copy of the cached item to ensure that it is not
190 * erased unexpectedly by the cache itself.
192 Cache::ItemPtr cached_item_;
193 /// We modify a local copy of the image once it is loaded.
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_;
200 double displayPixelRatio() const
202 return params_.pixel_ratio;
204 void setDisplayPixelRatio(double scale)
206 params_.pixel_ratio = scale;
211 void statusChanged();
213 void checkedLoading();
220 Loader::Loader(FileName const & doc_file)
221 : pimpl_(new Impl(doc_file))
225 Loader::Loader(FileName const & doc_file, FileName const & file, bool display)
226 : pimpl_(new Impl(doc_file))
228 reset(file, display);
232 Loader::Loader(FileName const & doc_file, FileName const & file, Params const & params)
233 : pimpl_(new Impl(doc_file))
239 Loader::Loader(FileName const & doc_file, Loader const & other)
240 : pimpl_(new Impl(doc_file))
242 Params const & params = other.pimpl_->params();
243 reset(params.filename, params);
247 Loader::Loader(Loader const & other)
248 : pimpl_(new Impl(other.pimpl_->doc_file_))
250 Params const & params = other.pimpl_->params();
251 reset(params.filename, params);
261 Loader & Loader::operator=(Loader const & other)
263 LASSERT(false, /**/);
264 if (this != &other) {
266 pimpl_ = new Impl(other.pimpl_->doc_file_);
267 Params const & params = other.pimpl_->params();
268 reset(params.filename, params);
274 void Loader::reset(FileName const & file, bool display) const
277 params.display = display;
278 pimpl_->resetParams(params);
280 pimpl_->resetFile(file);
281 pimpl_->createPixmap();
285 void Loader::reset(FileName const & file, Params const & params) const
287 pimpl_->resetParams(params);
288 pimpl_->resetFile(file);
289 pimpl_->createPixmap();
293 void Loader::reset(Params const & params) const
295 pimpl_->resetParams(params);
296 pimpl_->createPixmap();
300 void Loader::startLoading() const
302 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_)
304 pimpl_->startLoading();
308 void Loader::reload() const
310 pimpl_->cached_item_->startLoading();
314 void Loader::startMonitoring() const
316 if (!pimpl_->cached_item_)
319 pimpl_->cached_item_->startMonitoring();
323 bool Loader::monitoring() const
325 if (!pimpl_->cached_item_)
328 return pimpl_->cached_item_->monitoring();
332 void Loader::checkModifiedAsync() const
334 if (!pimpl_->cached_item_)
337 pimpl_->cached_item_->checkModifiedAsync();
341 FileName const & Loader::filename() const
343 static FileName const empty;
344 return pimpl_->cached_item_ ?
345 pimpl_->cached_item_->filename() : empty;
349 ImageStatus Loader::status() const
351 return pimpl_->status_;
355 double Loader::displayPixelRatio() const
357 return pimpl_->displayPixelRatio();
361 void Loader::setDisplayPixelRatio(double scale)
363 pimpl_->setDisplayPixelRatio(scale);
367 signals2::connection Loader::connect(slot const & slot) const
369 return pimpl_->signal_.connect(slot);
373 Image const * Loader::image() const
375 return pimpl_->image_.get();
379 Loader::Impl::Impl(FileName const & doc_file)
380 : doc_file_(doc_file), status_(WaitingToLoad)
385 Loader::Impl::~Impl()
387 resetFile(FileName());
391 void Loader::Impl::resetFile(FileName const & file)
393 FileName const old_file = cached_item_ ?
394 cached_item_->filename() : FileName();
396 if (file == old_file)
399 // If monitoring() the current file, should continue to monitor the
401 bool continue_monitoring = false;
403 if (!old_file.empty()) {
404 continue_monitoring = cached_item_->monitoring();
405 // cached_item_ is going to be reset, so the connected
406 // signal needs to be disconnected.
408 // This can in theory throw a BufferException
409 connection_.disconnect();
411 LYXERR(Debug::GRAPHICS, "Unable to disconnect signal.");
413 cached_item_.reset();
414 if (status_ != Converting) {
415 Cache::get().remove(old_file);
417 //TODO remove cache item when it is not busy any more, see #7163
421 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
424 if (cached_item_ || file.empty())
427 Cache & gc = Cache::get();
428 if (!gc.inCache(file))
429 gc.add(file, doc_file_);
431 // We /must/ make a local copy of this.
432 cached_item_ = gc.item(file);
433 status_ = cached_item_->status();
435 if (continue_monitoring && !cached_item_->monitoring())
436 cached_item_->startMonitoring();
438 // This is a scoped connection
439 connection_ = cached_item_->connect([this](){ statusChanged(); });
443 void Loader::Impl::resetParams(Params const & params)
445 if (params == params_)
449 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
454 void Loader::Impl::statusChanged()
456 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
462 void Loader::Impl::createPixmap()
464 if (!params_.display || status_ != Loaded)
468 LYXERR(Debug::GRAPHICS, "pixmap not cached yet");
472 if (!cached_item_->image()) {
473 // There must have been a problem reading the file.
474 LYXERR(Debug::GRAPHICS, "Graphics file not loaded.");
478 image_.reset(cached_item_->image()->clone());
480 if (params_.pixel_ratio == 1.0) {
481 string filename = cached_item_->filename().absFileName();
482 size_t idx = filename.find_last_of('.');
483 if (idx != string::npos && idx > 3) {
484 if (filename.substr(idx - 3, 3) == "@2x") {
485 params_.pixel_ratio = 2.0;
490 bool const success = image_->setPixmap(params_);
496 status_ = ErrorGeneratingPixmap;
500 void Loader::Impl::startLoading()
502 if (status_ != WaitingToLoad)
505 if (cached_item_->tryDisplayFormat()) {
511 LoaderQueue::get().touch(cached_item_);
515 } // namespace graphics