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"
29 using namespace lyx::support;
35 /////////////////////////////////////////////////////////////////////
39 /////////////////////////////////////////////////////////////////////
43 /// Use this to request that the item is loaded.
44 void touch(Cache::ItemPtr const & item);
45 /// Query whether the clock is ticking.
47 ///get the and only instance of the class
48 static LoaderQueue & get();
50 /// This class is a singleton class... use LoaderQueue::get() instead
52 /// The in-progress loading queue (elements are unique here).
53 list<Cache::ItemPtr> cache_queue_;
54 /// Used to make the insertion of new elements faster.
55 set<Cache::ItemPtr> cache_set_;
56 /// Newly touched elements go here. loadNext moves them to cache_queue_
57 queue<Cache::ItemPtr> bucket_;
63 /** This is the 'threaded' method, that does the loading in the
75 //static int const s_numimages_ = 5;
76 static int const s_numimages_ = 10;
77 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 timer.timeout.connect(bind(&LoaderQueue::loadNext, this));
114 void LoaderQueue::startLoader()
116 LYXERR(Debug::GRAPHICS, "LoaderQueue: waking up");
118 timer.setTimeout(s_millisecs_);
123 void LoaderQueue::stopLoader()
127 LYXERR(Debug::GRAPHICS, "LoaderQueue: I'm going to sleep");
131 bool LoaderQueue::running() const
137 void LoaderQueue::touch(Cache::ItemPtr const & item)
139 if (! cache_set_.insert(item).second) {
140 list<Cache::ItemPtr>::iterator
141 it = cache_queue_.begin();
142 list<Cache::ItemPtr>::iterator
143 end = cache_queue_.end();
145 it = find(it, end, item);
147 cache_queue_.erase(it);
149 cache_queue_.push_front(item);
156 /////////////////////////////////////////////////////////////////////
160 /////////////////////////////////////////////////////////////////////
162 typedef shared_ptr<Image> ImagePtr;
164 class Loader::Impl : public boost::signals::trackable {
171 void resetFile(FileName const &);
173 void resetParams(Params const &);
179 Params const & params() const { return params_; }
181 /// The loading status of the image.
183 /** Must store a copy of the cached item to ensure that it is not
184 * erased unexpectedly by the cache itself.
186 Cache::ItemPtr cached_item_;
187 /// We modify a local copy of the image once it is loaded.
189 /// This signal is emitted when the image loading status changes.
190 boost::signal<void()> signal_;
191 /// The connection of the signal StatusChanged
192 boost::signals::connection sc_;
194 double displayPixelRatio() const
196 return params_.pixel_ratio;
198 void setDisplayPixelRatio(double scale)
200 params_.pixel_ratio = scale;
205 void statusChanged();
207 void checkedLoading();
219 Loader::Loader(FileName const & file, bool display)
222 reset(file, display);
226 Loader::Loader(FileName const & file, Params const & params)
233 Loader::Loader(Loader const & other)
236 Params const & params = other.pimpl_->params();
237 reset(params.filename, params);
247 Loader & Loader::operator=(Loader const & other)
249 if (this != &other) {
250 Params const & params = other.pimpl_->params();
251 reset(params.filename, params);
257 void Loader::reset(FileName const & file, bool display) const
260 params.display = display;
261 pimpl_->resetParams(params);
263 pimpl_->resetFile(file);
264 pimpl_->createPixmap();
268 void Loader::reset(FileName const & file, Params const & params) const
270 pimpl_->resetParams(params);
271 pimpl_->resetFile(file);
272 pimpl_->createPixmap();
276 void Loader::reset(Params const & params) const
278 pimpl_->resetParams(params);
279 pimpl_->createPixmap();
283 void Loader::startLoading() const
285 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_)
287 pimpl_->startLoading();
291 void Loader::reload() const
293 pimpl_->cached_item_->startLoading();
297 void Loader::startMonitoring() const
299 if (!pimpl_->cached_item_)
302 pimpl_->cached_item_->startMonitoring();
306 bool Loader::monitoring() const
308 if (!pimpl_->cached_item_)
311 return pimpl_->cached_item_->monitoring();
315 unsigned long Loader::checksum() const
317 if (!pimpl_->cached_item_)
320 return pimpl_->cached_item_->checksum();
324 FileName const & Loader::filename() const
326 static FileName const empty;
327 return pimpl_->cached_item_ ?
328 pimpl_->cached_item_->filename() : empty;
332 ImageStatus Loader::status() const
334 return pimpl_->status_;
338 double Loader::displayPixelRatio() const
340 return pimpl_->displayPixelRatio();
344 void Loader::setDisplayPixelRatio(double scale)
346 pimpl_->setDisplayPixelRatio(scale);
350 boost::signals::connection Loader::connect(slot_type const & slot) const
352 return pimpl_->signal_.connect(slot);
356 Image const * Loader::image() const
358 return pimpl_->image_.get();
363 : status_(WaitingToLoad)
368 Loader::Impl::~Impl()
370 resetFile(FileName());
374 void Loader::Impl::resetFile(FileName const & file)
376 FileName const old_file = cached_item_ ?
377 cached_item_->filename() : FileName();
379 if (file == old_file)
382 // If monitoring() the current file, should continue to monitor the
384 bool continue_monitoring = false;
386 if (!old_file.empty()) {
387 continue_monitoring = cached_item_->monitoring();
388 // cached_item_ is going to be reset, so the connected
389 // signal needs to be disconnected.
391 cached_item_.reset();
392 if (status_ != Converting) {
393 Cache::get().remove(old_file);
395 //TODO remove cache item when it is not busy any more, see #7163
399 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
402 if (cached_item_ || file.empty())
405 Cache & gc = Cache::get();
406 if (!gc.inCache(file))
409 // We /must/ make a local copy of this.
410 cached_item_ = gc.item(file);
411 status_ = cached_item_->status();
413 if (continue_monitoring && !cached_item_->monitoring())
414 cached_item_->startMonitoring();
416 sc_ = cached_item_->connect(bind(&Impl::statusChanged, this));
420 void Loader::Impl::resetParams(Params const & params)
422 if (params == params_)
426 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
431 void Loader::Impl::statusChanged()
433 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
439 void Loader::Impl::createPixmap()
441 if (!params_.display || status_ != Loaded)
445 LYXERR(Debug::GRAPHICS, "pixmap not cached yet");
449 if (!cached_item_->image()) {
450 // There must have been a problem reading the file.
451 LYXERR(Debug::GRAPHICS, "Graphics file not loaded.");
455 image_.reset(cached_item_->image()->clone());
457 if (params_.pixel_ratio == 1.0) {
458 string filename = cached_item_->filename().absFileName();
459 size_t idx = filename.find_last_of('.');
460 if (idx != string::npos && idx > 3) {
461 if (filename.substr(idx - 3, 3) == "@2x") {
462 params_.pixel_ratio = 2.0;
467 bool const success = image_->setPixmap(params_);
473 status_ = ErrorGeneratingPixmap;
477 void Loader::Impl::startLoading()
479 if (status_ != WaitingToLoad)
482 if (cached_item_->tryDisplayFormat()) {
488 LoaderQueue::get().touch(cached_item_);
492 } // namespace graphics