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/Timeout.h"
23 #include "support/bind.h"
30 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;
82 LoaderQueue & LoaderQueue::get()
84 static LoaderQueue singleton;
89 void LoaderQueue::loadNext()
91 LYXERR(Debug::GRAPHICS, "LoaderQueue: "
92 << cache_queue_.size() << " items in the queue");
93 int counter = s_numimages_;
94 while (!cache_queue_.empty() && counter--) {
95 Cache::ItemPtr ptr = cache_queue_.front();
96 cache_set_.erase(ptr);
97 cache_queue_.pop_front();
98 if (ptr->status() == WaitingToLoad)
101 if (!cache_queue_.empty())
108 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
111 timer.timeout.connect(bind(&LoaderQueue::loadNext, this));
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;
165 class Loader::Impl : public boost::signals::trackable {
172 void resetFile(FileName const &);
174 void resetParams(Params const &);
180 Params const & params() const { return params_; }
182 /// The loading status of the image.
184 /** Must store a copy of the cached item to ensure that it is not
185 * erased unexpectedly by the cache itself.
187 Cache::ItemPtr cached_item_;
188 /// We modify a local copy of the image once it is loaded.
190 /// This signal is emitted when the image loading status changes.
191 boost::signal<void()> signal_;
192 /// The connection of the signal StatusChanged
193 boost::signals::connection sc_;
195 double displayPixelRatio() const
197 return params_.pixel_ratio;
199 void setDisplayPixelRatio(double scale)
201 params_.pixel_ratio = scale;
206 void statusChanged();
208 void checkedLoading();
220 Loader::Loader(FileName const & file, bool display)
223 reset(file, display);
227 Loader::Loader(FileName const & file, Params const & params)
234 Loader::Loader(Loader const & other)
237 Params const & params = other.pimpl_->params();
238 reset(params.filename, params);
248 Loader & Loader::operator=(Loader const & other)
250 if (this != &other) {
251 Params const & params = other.pimpl_->params();
252 reset(params.filename, params);
258 void Loader::reset(FileName const & file, bool display) const
261 params.display = display;
262 pimpl_->resetParams(params);
264 pimpl_->resetFile(file);
265 pimpl_->createPixmap();
269 void Loader::reset(FileName const & file, Params const & params) const
271 pimpl_->resetParams(params);
272 pimpl_->resetFile(file);
273 pimpl_->createPixmap();
277 void Loader::reset(Params const & params) const
279 pimpl_->resetParams(params);
280 pimpl_->createPixmap();
284 void Loader::startLoading() const
286 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_)
288 pimpl_->startLoading();
292 void Loader::reload() const
294 pimpl_->cached_item_->startLoading();
298 void Loader::startMonitoring() const
300 if (!pimpl_->cached_item_)
303 pimpl_->cached_item_->startMonitoring();
307 bool Loader::monitoring() const
309 if (!pimpl_->cached_item_)
312 return pimpl_->cached_item_->monitoring();
316 unsigned long Loader::checksum() const
318 if (!pimpl_->cached_item_)
321 return pimpl_->cached_item_->checksum();
325 FileName const & Loader::filename() const
327 static FileName const empty;
328 return pimpl_->cached_item_ ?
329 pimpl_->cached_item_->filename() : empty;
333 ImageStatus Loader::status() const
335 return pimpl_->status_;
339 double Loader::displayPixelRatio() const
341 return pimpl_->displayPixelRatio();
345 void Loader::setDisplayPixelRatio(double scale)
347 pimpl_->setDisplayPixelRatio(scale);
351 boost::signals::connection Loader::connect(slot_type const & slot) const
353 return pimpl_->signal_.connect(slot);
357 Image const * Loader::image() const
359 return pimpl_->image_.get();
364 : status_(WaitingToLoad)
369 Loader::Impl::~Impl()
371 resetFile(FileName());
375 void Loader::Impl::resetFile(FileName const & file)
377 FileName const old_file = cached_item_ ?
378 cached_item_->filename() : FileName();
380 if (file == old_file)
383 // If monitoring() the current file, should continue to monitor the
385 bool continue_monitoring = false;
387 if (!old_file.empty()) {
388 continue_monitoring = cached_item_->monitoring();
389 // cached_item_ is going to be reset, so the connected
390 // signal needs to be disconnected.
392 cached_item_.reset();
393 if (status_ != Converting) {
394 Cache::get().remove(old_file);
396 //TODO remove cache item when it is not busy any more, see #7163
400 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
403 if (cached_item_ || file.empty())
406 Cache & gc = Cache::get();
407 if (!gc.inCache(file))
410 // We /must/ make a local copy of this.
411 cached_item_ = gc.item(file);
412 status_ = cached_item_->status();
414 if (continue_monitoring && !cached_item_->monitoring())
415 cached_item_->startMonitoring();
417 sc_ = cached_item_->connect(bind(&Impl::statusChanged, this));
421 void Loader::Impl::resetParams(Params const & params)
423 if (params == params_)
427 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
432 void Loader::Impl::statusChanged()
434 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
440 void Loader::Impl::createPixmap()
442 if (!params_.display || status_ != Loaded)
446 LYXERR(Debug::GRAPHICS, "pixmap not cached yet");
450 if (!cached_item_->image()) {
451 // There must have been a problem reading the file.
452 LYXERR(Debug::GRAPHICS, "Graphics file not loaded.");
456 image_.reset(cached_item_->image()->clone());
458 if (params_.pixel_ratio == 1.0) {
459 string filename = cached_item_->filename().absFileName();
460 size_t idx = filename.find_last_of('.');
461 if (idx != string::npos && idx > 3) {
462 if (filename.substr(idx - 3, 3) == "@2x") {
463 params_.pixel_ratio = 2.0;
468 bool const success = image_->setPixmap(params_);
474 status_ = ErrorGeneratingPixmap;
478 void Loader::Impl::startLoading()
480 if (status_ != WaitingToLoad)
483 if (cached_item_->tryDisplayFormat()) {
489 LoaderQueue::get().touch(cached_item_);
493 } // namespace graphics