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"
19 #include "support/debug.h"
21 #include "support/Timeout.h"
23 #include <boost/bind.hpp>
36 using support::FileName;
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 std::list<Cache::ItemPtr> cache_queue_;
57 /// Used to make the insertion of new elements faster.
58 std::set<Cache::ItemPtr> cache_set_;
59 /// Newly touched elements go here. loadNext moves them to cache_queue_
60 std::queue<Cache::ItemPtr> bucket_;
66 /** This is the 'threaded' method, that does the loading in the
77 //static int s_numimages_ = 5;
78 //static int s_millisecs_ = 500;
80 static int s_numimages_ = 10;
81 static int 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_.size() && 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_.size()) {
110 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
113 timer.timeout.connect(boost::bind(&LoaderQueue::loadNext, this));
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 = std::find(it, end, item);
150 cache_queue_.erase(it);
152 cache_queue_.push_front(item);
159 /////////////////////////////////////////////////////////////////////
163 /////////////////////////////////////////////////////////////////////
165 typedef boost::shared_ptr<Image> ImagePtr;
167 class Loader::Impl : public boost::signals::trackable {
174 void resetFile(FileName const &);
176 void resetParams(Params const &);
182 Params const & params() const { return params_; }
184 /// The loading status of the image.
186 /** Must store a copy of the cached item to ensure that it is not
187 * erased unexpectedly by the cache itself.
189 Cache::ItemPtr cached_item_;
190 /// We modify a local copy of the image once it is loaded.
192 /// This signal is emitted when the image loading status changes.
193 boost::signal<void()> signal_;
194 /// The connection of the signal StatusChanged
195 boost::signals::connection sc_;
199 void statusChanged();
201 void checkedLoading();
213 Loader::Loader(FileName const & file, DisplayType type)
220 Loader::Loader(FileName const & file, Params const & params)
227 Loader::Loader(Loader const & other)
230 Params const & params = other.pimpl_->params();
231 reset(params.filename, params);
241 Loader & Loader::operator=(Loader const & other)
243 if (this != &other) {
244 Params const & params = other.pimpl_->params();
245 reset(params.filename, params);
251 void Loader::reset(FileName const & file, DisplayType type) const
254 params.display = type;
255 pimpl_->resetParams(params);
257 pimpl_->resetFile(file);
258 pimpl_->createPixmap();
262 void Loader::reset(FileName const & file, Params const & params) const
264 pimpl_->resetParams(params);
265 pimpl_->resetFile(file);
266 pimpl_->createPixmap();
270 void Loader::reset(Params const & params) const
272 pimpl_->resetParams(params);
273 pimpl_->createPixmap();
277 void Loader::startLoading() const
279 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
281 pimpl_->startLoading();
285 void Loader::startMonitoring() const
287 if (!pimpl_->cached_item_.get())
290 pimpl_->cached_item_->startMonitoring();
294 bool Loader::monitoring() const
296 if (!pimpl_->cached_item_.get())
299 return pimpl_->cached_item_->monitoring();
303 unsigned long Loader::checksum() const
305 if (!pimpl_->cached_item_.get())
308 return pimpl_->cached_item_->checksum();
312 FileName const & Loader::filename() const
314 static FileName const empty;
315 return pimpl_->cached_item_.get() ?
316 pimpl_->cached_item_->filename() : empty;
320 ImageStatus Loader::status() const
322 return pimpl_->status_;
326 boost::signals::connection Loader::connect(slot_type const & slot) const
328 return pimpl_->signal_.connect(slot);
332 Image const * Loader::image() const
334 return pimpl_->image_.get();
339 : status_(WaitingToLoad)
344 Loader::Impl::~Impl()
346 resetFile(FileName());
350 void Loader::Impl::resetFile(FileName const & file)
352 FileName const old_file = cached_item_.get() ?
353 cached_item_->filename() : FileName();
355 if (file == old_file)
358 // If monitoring() the current file, should continue to monitor the
360 bool continue_monitoring = false;
362 if (!old_file.empty()) {
363 continue_monitoring = cached_item_->monitoring();
364 // cached_item_ is going to be reset, so the connected
365 // signal needs to be disconnected.
367 cached_item_.reset();
368 Cache::get().remove(old_file);
371 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
374 if (cached_item_.get() || file.empty())
377 Cache & gc = Cache::get();
378 if (!gc.inCache(file))
381 // We /must/ make a local copy of this.
382 cached_item_ = gc.item(file);
383 status_ = cached_item_->status();
385 if (continue_monitoring && !cached_item_->monitoring())
386 cached_item_->startMonitoring();
388 sc_ = cached_item_->connect(boost::bind(&Impl::statusChanged, this));
392 void Loader::Impl::resetParams(Params const & params)
394 if (params == params_)
398 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
403 void Loader::Impl::statusChanged()
405 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
411 void Loader::Impl::createPixmap()
413 if (!cached_item_.get() ||
414 params_.display == NoDisplay || status_ != Loaded)
417 image_.reset(cached_item_->image()->clone());
419 // These do nothing if there's nothing to do
420 image_->clip(params_);
421 image_->rotate(params_);
422 image_->scale(params_);
424 bool const success = image_->setPixmap(params_);
430 status_ = ErrorGeneratingPixmap;
434 void Loader::Impl::startLoading()
436 if (status_ != WaitingToLoad)
439 LoaderQueue::get().touch(cached_item_);
443 } // namespace graphics