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"
29 using namespace lyx::support;
36 /////////////////////////////////////////////////////////////////////
40 /////////////////////////////////////////////////////////////////////
44 /// Use this to request that the item is loaded.
45 void touch(Cache::ItemPtr const & item);
46 /// Query whether the clock is ticking.
48 ///get the and only instance of the class
49 static LoaderQueue & get();
51 /// This class is a singleton class... use LoaderQueue::get() instead
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_;
64 /** This is the 'threaded' method, that does the loading in the
76 //static int const s_numimages_ = 5;
77 static int const s_numimages_ = 10;
78 static int const s_millisecs_ = 500;
81 LoaderQueue & LoaderQueue::get()
83 static LoaderQueue singleton;
88 void LoaderQueue::loadNext()
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)
100 if (!cache_queue_.empty())
107 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
110 // Disconnected when this is destroyed
111 timer.timeout.connect([this](){ loadNext(); });
115 void LoaderQueue::startLoader()
117 LYXERR(Debug::GRAPHICS, "LoaderQueue: waking up");
119 timer.setTimeout(s_millisecs_);
124 void LoaderQueue::stopLoader()
128 LYXERR(Debug::GRAPHICS, "LoaderQueue: I'm going to sleep");
132 bool LoaderQueue::running() const
138 void LoaderQueue::touch(Cache::ItemPtr const & item)
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();
146 it = find(it, end, item);
148 cache_queue_.erase(it);
150 cache_queue_.push_front(item);
157 /////////////////////////////////////////////////////////////////////
161 /////////////////////////////////////////////////////////////////////
163 typedef std::shared_ptr<Image> ImagePtr;
169 Impl(FileName const & doc_file);
173 void resetFile(FileName const &);
175 void resetParams(Params const &);
181 Params const & params() const { return params_; }
185 /// The loading status of the image.
187 /** Must store a copy of the cached item to ensure that it is not
188 * erased unexpectedly by the cache itself.
190 Cache::ItemPtr cached_item_;
191 /// We modify a local copy of the image once it is loaded.
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_;
198 double displayPixelRatio() const
200 return params_.pixel_ratio;
202 void setDisplayPixelRatio(double scale)
204 params_.pixel_ratio = scale;
209 void statusChanged();
211 void checkedLoading();
218 Loader::Loader(FileName const & doc_file)
219 : pimpl_(new Impl(doc_file))
223 Loader::Loader(FileName const & doc_file, FileName const & file, bool display)
224 : pimpl_(new Impl(doc_file))
226 reset(file, display);
230 Loader::Loader(FileName const & doc_file, FileName const & file, Params const & params)
231 : pimpl_(new Impl(doc_file))
237 Loader::Loader(FileName const & doc_file, Loader const & other)
238 : pimpl_(new Impl(doc_file))
240 Params const & params = other.pimpl_->params();
241 reset(params.filename, params);
245 Loader::Loader(Loader const & other)
246 : pimpl_(new Impl(other.pimpl_->doc_file_))
248 Params const & params = other.pimpl_->params();
249 reset(params.filename, params);
259 Loader & Loader::operator=(Loader const & other)
261 LASSERT(false, /**/);
262 if (this != &other) {
264 pimpl_ = new Impl(other.pimpl_->doc_file_);
265 Params const & params = other.pimpl_->params();
266 reset(params.filename, params);
272 void Loader::reset(FileName const & file, bool display) const
275 params.display = display;
276 pimpl_->resetParams(params);
278 pimpl_->resetFile(file);
279 pimpl_->createPixmap();
283 void Loader::reset(FileName const & file, Params const & params) const
285 pimpl_->resetParams(params);
286 pimpl_->resetFile(file);
287 pimpl_->createPixmap();
291 void Loader::reset(Params const & params) const
293 pimpl_->resetParams(params);
294 pimpl_->createPixmap();
298 void Loader::startLoading() const
300 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_
301 || pimpl_->cached_item_->status() == Converting)
303 pimpl_->startLoading();
307 void Loader::reload() const
309 pimpl_->cached_item_->startLoading();
313 void Loader::startMonitoring() const
315 if (!pimpl_->cached_item_)
318 pimpl_->cached_item_->startMonitoring();
322 bool Loader::monitoring() const
324 if (!pimpl_->cached_item_)
327 return pimpl_->cached_item_->monitoring();
331 void Loader::checkModifiedAsync() const
333 if (!pimpl_->cached_item_)
336 pimpl_->cached_item_->checkModifiedAsync();
340 FileName const & Loader::filename() const
342 static FileName const empty;
343 return pimpl_->cached_item_ ?
344 pimpl_->cached_item_->filename() : empty;
348 ImageStatus Loader::status() const
350 return pimpl_->status_;
354 double Loader::displayPixelRatio() const
356 return pimpl_->displayPixelRatio();
360 void Loader::setDisplayPixelRatio(double scale)
362 pimpl_->setDisplayPixelRatio(scale);
366 signals2::connection Loader::connect(slot const & slot) const
368 return pimpl_->signal_.connect(slot);
372 Image const * Loader::image() const
374 return pimpl_->image_.get();
378 Loader::Impl::Impl(FileName const & doc_file)
379 : doc_file_(doc_file), status_(WaitingToLoad)
384 Loader::Impl::~Impl()
386 resetFile(FileName());
390 void Loader::Impl::resetFile(FileName const & file)
392 FileName const old_file = cached_item_ ?
393 cached_item_->filename() : FileName();
395 if (file == old_file)
398 // If monitoring() the current file, should continue to monitor the
400 bool continue_monitoring = false;
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.
407 // This can in theory throw a BufferException
408 connection_.disconnect();
410 LYXERR(Debug::GRAPHICS, "Unable to disconnect signal.");
412 cached_item_.reset();
413 if (status_ != Converting) {
414 Cache::get().remove(old_file);
416 //TODO remove cache item when it is not busy any more, see #7163
420 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
423 if (cached_item_ || file.empty())
426 Cache & gc = Cache::get();
427 if (!gc.inCache(file))
428 gc.add(file, doc_file_);
430 // We /must/ make a local copy of this.
431 cached_item_ = gc.item(file);
432 status_ = cached_item_->status();
434 if (continue_monitoring && !cached_item_->monitoring())
435 cached_item_->startMonitoring();
437 // This is a scoped connection
438 connection_ = cached_item_->connect([this](){ statusChanged(); });
442 void Loader::Impl::resetParams(Params const & params)
444 if (params == params_)
448 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
453 void Loader::Impl::statusChanged()
455 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
461 void Loader::Impl::createPixmap()
463 if (!params_.display || status_ != Loaded)
467 LYXERR(Debug::GRAPHICS, "pixmap not cached yet");
471 if (!cached_item_->image()) {
472 // There must have been a problem reading the file.
473 LYXERR(Debug::GRAPHICS, "Graphics file not loaded.");
477 image_.reset(cached_item_->image()->clone());
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;
489 bool const success = image_->setPixmap(params_);
495 status_ = ErrorGeneratingPixmap;
499 void Loader::Impl::startLoading()
501 if (status_ != WaitingToLoad)
504 if (cached_item_->tryDisplayFormat()) {
510 LoaderQueue::get().touch(cached_item_);
514 } // namespace graphics