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 timer.timeout.connect(bind(&LoaderQueue::loadNext, this));
116 void LoaderQueue::startLoader()
118 LYXERR(Debug::GRAPHICS, "LoaderQueue: waking up");
120 timer.setTimeout(s_millisecs_);
125 void LoaderQueue::stopLoader()
129 LYXERR(Debug::GRAPHICS, "LoaderQueue: I'm going to sleep");
133 bool LoaderQueue::running() const
139 void LoaderQueue::touch(Cache::ItemPtr const & item)
141 if (! cache_set_.insert(item).second) {
142 list<Cache::ItemPtr>::iterator
143 it = cache_queue_.begin();
144 list<Cache::ItemPtr>::iterator
145 end = cache_queue_.end();
147 it = find(it, end, item);
149 cache_queue_.erase(it);
151 cache_queue_.push_front(item);
158 /////////////////////////////////////////////////////////////////////
162 /////////////////////////////////////////////////////////////////////
164 typedef std::shared_ptr<Image> ImagePtr;
166 class Loader::Impl : public boost::signals2::trackable {
170 Impl(FileName const & doc_file);
174 void resetFile(FileName const &);
176 void resetParams(Params const &);
182 Params const & params() const { return params_; }
186 /// The loading status of the image.
188 /** Must store a copy of the cached item to ensure that it is not
189 * erased unexpectedly by the cache itself.
191 Cache::ItemPtr cached_item_;
192 /// We modify a local copy of the image once it is loaded.
194 /// This signal is emitted when the image loading status changes.
195 boost::signals2::signal<void()> signal_;
196 /// The connection of the signal StatusChanged
197 boost::signals2::connection sc_;
199 double displayPixelRatio() const
201 return params_.pixel_ratio;
203 void setDisplayPixelRatio(double scale)
205 params_.pixel_ratio = scale;
210 void statusChanged();
212 void checkedLoading();
219 Loader::Loader(FileName const & doc_file)
220 : pimpl_(new Impl(doc_file))
224 Loader::Loader(FileName const & doc_file, FileName const & file, bool display)
225 : pimpl_(new Impl(doc_file))
227 reset(file, display);
231 Loader::Loader(FileName const & doc_file, FileName const & file, Params const & params)
232 : pimpl_(new Impl(doc_file))
238 Loader::Loader(FileName const & doc_file, Loader const & other)
239 : pimpl_(new Impl(doc_file))
241 Params const & params = other.pimpl_->params();
242 reset(params.filename, params);
246 Loader::Loader(Loader const & other)
247 : pimpl_(new Impl(other.pimpl_->doc_file_))
249 Params const & params = other.pimpl_->params();
250 reset(params.filename, params);
260 Loader & Loader::operator=(Loader const & other)
262 LASSERT(false, /**/);
263 if (this != &other) {
265 pimpl_ = new Impl(other.pimpl_->doc_file_);
266 Params const & params = other.pimpl_->params();
267 reset(params.filename, params);
273 void Loader::reset(FileName const & file, bool display) const
276 params.display = display;
277 pimpl_->resetParams(params);
279 pimpl_->resetFile(file);
280 pimpl_->createPixmap();
284 void Loader::reset(FileName const & file, Params const & params) const
286 pimpl_->resetParams(params);
287 pimpl_->resetFile(file);
288 pimpl_->createPixmap();
292 void Loader::reset(Params const & params) const
294 pimpl_->resetParams(params);
295 pimpl_->createPixmap();
299 void Loader::startLoading() const
301 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_)
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 unsigned long Loader::checksum() const
333 if (!pimpl_->cached_item_)
336 return pimpl_->cached_item_->checksum();
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 boost::signals2::connection Loader::connect(slot_type 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 cached_item_.reset();
408 if (status_ != Converting) {
409 Cache::get().remove(old_file);
411 //TODO remove cache item when it is not busy any more, see #7163
415 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
418 if (cached_item_ || file.empty())
421 Cache & gc = Cache::get();
422 if (!gc.inCache(file))
423 gc.add(file, doc_file_);
425 // We /must/ make a local copy of this.
426 cached_item_ = gc.item(file);
427 status_ = cached_item_->status();
429 if (continue_monitoring && !cached_item_->monitoring())
430 cached_item_->startMonitoring();
432 sc_ = cached_item_->connect(bind(&Impl::statusChanged, this));
436 void Loader::Impl::resetParams(Params const & params)
438 if (params == params_)
442 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
447 void Loader::Impl::statusChanged()
449 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
455 void Loader::Impl::createPixmap()
457 if (!params_.display || status_ != Loaded)
461 LYXERR(Debug::GRAPHICS, "pixmap not cached yet");
465 if (!cached_item_->image()) {
466 // There must have been a problem reading the file.
467 LYXERR(Debug::GRAPHICS, "Graphics file not loaded.");
471 image_.reset(cached_item_->image()->clone());
473 if (params_.pixel_ratio == 1.0) {
474 string filename = cached_item_->filename().absFileName();
475 size_t idx = filename.find_last_of('.');
476 if (idx != string::npos && idx > 3) {
477 if (filename.substr(idx - 3, 3) == "@2x") {
478 params_.pixel_ratio = 2.0;
483 bool const success = image_->setPixmap(params_);
489 status_ = ErrorGeneratingPixmap;
493 void Loader::Impl::startLoading()
495 if (status_ != WaitingToLoad)
498 if (cached_item_->tryDisplayFormat()) {
504 LoaderQueue::get().touch(cached_item_);
508 } // namespace graphics