StelTextureMgr.cpp   StelTextureMgr.cpp 
skipping to change at line 34 skipping to change at line 34
#include "StelPainter.hpp" #include "StelPainter.hpp"
#include <QFileInfo> #include <QFileInfo>
#include <QFile> #include <QFile>
#include <QDebug> #include <QDebug>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QThread> #include <QThread>
#include <QSettings> #include <QSettings>
#include <cstdlib> #include <cstdlib>
#include <QOpenGLContext> #include <QOpenGLContext>
#include <QThreadPool>
StelTextureMgr::StelTextureMgr() : glMemoryUsage(0) StelTextureMgr::StelTextureMgr(QObject *parent)
{ : QObject(parent), glMemoryUsage(0), loaderThreadPool(new QThreadPoo
l(this))
}
void StelTextureMgr::init()
{ {
#ifdef Q_PROCESSOR_X86_64
//allow up to 4 textures to be loaded in parallel on 64 bit
loaderThreadPool->setMaxThreadCount(std::min(4,QThread::idealThreadC
ount()));
#else
//on other archs, for now ensure that just 1 texture is at once in b
ackground
//otherwise, for large textures loaded in parallel (some scenery3d s
cenes), the risk of an out-of-memory error is greater on 32bit systems
loaderThreadPool->setMaxThreadCount(1);
#endif
} }
StelTextureSP StelTextureMgr::createTexture(const QString& afilename, const StelTexture::StelTextureParams& params) StelTextureSP StelTextureMgr::createTexture(const QString& afilename, const StelTexture::StelTextureParams& params)
{ {
if (afilename.isEmpty()) if (afilename.isEmpty())
return StelTextureSP(); return StelTextureSP();
QFileInfo info(afilename);
QString canPath = info.canonicalFilePath();
if(canPath.isEmpty()) //file does not exist
{
qWarning()<<"Texture"<<afilename<<"does not exist";
return StelTextureSP();
}
//lock it for thread safety
QMutexLocker locker(&mutex);
//try to find out if the tex is already loaded
StelTextureSP cache = lookupCache(canPath);
if(!cache.isNull()) return cache;
StelTextureSP tex = StelTextureSP(new StelTexture(this)); StelTextureSP tex = StelTextureSP(new StelTexture(this));
tex->fullPath = afilename; tex->fullPath = canPath;
QImage image(tex->fullPath); QImage image(tex->fullPath);
if (image.isNull()) if (image.isNull())
return StelTextureSP(); return StelTextureSP();
tex->loadParams = params; tex->loadParams = params;
if (tex->glLoad(image)) if (tex->glLoad(image))
{
textureCache.insert(canPath,tex);
return tex; return tex;
}
else else
{ {
qWarning()<<tex->getErrorMessage(); qWarning()<<tex->getErrorMessage();
return StelTextureSP(); return StelTextureSP();
} }
} }
StelTextureSP StelTextureMgr::createTextureThread(const QString& url, const StelTexture::StelTextureParams& params, bool lazyLoading) StelTextureSP StelTextureMgr::createTextureThread(const QString& url, const StelTexture::StelTextureParams& params, bool lazyLoading)
{ {
if (url.isEmpty()) if (url.isEmpty())
return StelTextureSP(); return StelTextureSP();
QString canPath = url;
if(!url.startsWith("http"))
{
QFileInfo info(url);
canPath = info.canonicalFilePath();
}
if(canPath.isEmpty()) //file does not exist
{
qWarning()<<"Texture"<<url<<"does not exist";
return StelTextureSP();
}
//lock it for thread safety
QMutexLocker locker(&mutex);
//try to find out if the tex is already loaded
StelTextureSP cache = lookupCache(canPath);
if(!cache.isNull()) return cache;
StelTextureSP tex = StelTextureSP(new StelTexture(this)); StelTextureSP tex = StelTextureSP(new StelTexture(this));
tex->loadParams = params; tex->loadParams = params;
tex->fullPath = url; tex->fullPath = canPath;
if (!lazyLoading) if (!lazyLoading)
{ {
tex->bind(); //use load() instead of bind() to prevent potential - if ver
y unlikey - OpenGL errors
//because GL must be called in the main thread
tex->load();
} }
textureCache.insert(canPath,tex);
return tex; return tex;
} }
StelTextureSP StelTextureMgr::wrapperForGLTexture(GLuint texId)
{
IdMap::iterator it = idMap.find(texId);
if(it!=idMap.end())
{
//find out if it is valid
StelTextureSP ref = it->toStrongRef();
if(ref)
{
return ref; //valid texture!
}
else
{
//remove the cache entry
it=idMap.erase(it);
}
}
//no existing tex with this ID found, create a new wrapper
StelTextureSP newTex(new StelTexture(this));
newTex->wrapGLTexture(texId);
if(!newTex->errorOccured)
{
idMap.insert(texId, newTex);
return newTex;
}
else
{
//error while wrapping
qWarning()<<newTex->getErrorMessage();
return StelTextureSP();
}
}
StelTextureSP StelTextureMgr::lookupCache(const QString &file)
{
TexCache::iterator it = textureCache.find(file);
if(it!=textureCache.end())
{
//find out if it is valid
StelTextureSP ref = it->toStrongRef();
if(ref)
{
return ref; //valid texture!
}
else
{
//remove the cache entry
it=textureCache.erase(it);
}
}
return StelTextureSP();
}
 End of changes. 12 change blocks. 
9 lines changed or deleted 61 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/