Skip to content

Commit

Permalink
Task: Resource handling needs refactoring.
Browse files Browse the repository at this point in the history
Phase One:
1.
fixed lots of memory leaks related to Texture2D's not being deleted.
2.
Texture2D now prints warning in destructor,
if cleanupResources() was not properly called before.

Task-number: QTBUG-24043
Change-Id: Ia6a8bbe38ad66778a38705e0a4464f24373d8846
Reviewed-by: Danny Pope <[email protected]>
  • Loading branch information
Sergey Dubitskiy authored and Qt by Nokia committed Feb 12, 2012
1 parent ce825d2 commit 2c29c50
Show file tree
Hide file tree
Showing 27 changed files with 70 additions and 63 deletions.
2 changes: 1 addition & 1 deletion demos/qt3d/cubehouse/cubeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void CubeView::initializeGL(QGLPainter *painter)
builder.popNode();

int index;
QGLMaterialCollection *palette = builder.sceneNode()->palette();
QSharedPointer<QGLMaterialCollection> palette = builder.sceneNode()->palette();

QGLMaterial *mat1 = new QGLMaterial();
mat1->setDiffuseColor(QColor(128, 100, 0));
Expand Down
2 changes: 1 addition & 1 deletion demos/qt3d/photobrowser3d/buttons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#include "buttons.h"
#include "qglpainter.h"

Buttons::Buttons(QObject *parent, QGLMaterialCollection *palette)
Buttons::Buttons(QObject *parent, QSharedPointer<QGLMaterialCollection> palette)
: QGLSceneNode(parent)
{
setObjectName("Buttons");
Expand Down
2 changes: 1 addition & 1 deletion demos/qt3d/photobrowser3d/buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Buttons : public QGLSceneNode
{
Q_OBJECT
public:
explicit Buttons(QObject *parent, QGLMaterialCollection *palette);
explicit Buttons(QObject *parent, QSharedPointer<QGLMaterialCollection> palette);
void draw(QGLPainter *painter);
void clearPositions();
private:
Expand Down
2 changes: 1 addition & 1 deletion demos/qt3d/photobrowser3d/imagedisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static inline void qAddPane(QSizeF size, QGeometryData *data)
data->appendIndices(k, k+2, k+3);
}

ImageDisplay::ImageDisplay(QObject *parent, QGLMaterialCollection *materials, qreal wallSize)
ImageDisplay::ImageDisplay(QObject *parent, QSharedPointer<QGLMaterialCollection> materials, qreal wallSize)
: QGLSceneNode(parent)
, m_wall(0)
, m_frames(0)
Expand Down
2 changes: 1 addition & 1 deletion demos/qt3d/photobrowser3d/imagedisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ImageDisplay : public QGLSceneNode
{
Q_OBJECT
public:
ImageDisplay(QObject *parent, QGLMaterialCollection *materials, qreal wallSize = 4.0);
ImageDisplay(QObject *parent, QSharedPointer<QGLMaterialCollection> materials, qreal wallSize = 4.0);
~ImageDisplay();
int maxImages() const { return m_maxImages; }
void setMaxImages(int max) { m_maxImages = max; }
Expand Down
2 changes: 1 addition & 1 deletion demos/qt3d/photobrowser3d/photobrowser3dview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ PhotoBrowser3DView::PhotoBrowser3DView()
, m_images(0)
, m_buttons(0)
, m_skybox(0)
, m_palette(new QGLMaterialCollection(this))
, m_palette(new QGLMaterialCollection())
, m_state(0)
, m_app(0)
, m_zoomed(0)
Expand Down
2 changes: 1 addition & 1 deletion demos/qt3d/photobrowser3d/photobrowser3dview.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private slots:
ImageManager *m_images;
Buttons *m_buttons;
SkyBox *m_skybox;
QGLMaterialCollection *m_palette;
QSharedPointer<QGLMaterialCollection> m_palette;
QStateMachine *m_state;
QState *m_app;
QState *m_zoomed;
Expand Down
1 change: 1 addition & 0 deletions demos/qt3d/photobrowser3d/qatlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ void QAtlas::initialize(QGLPainter *painter)
m_data = new QOpenGLFramebufferObject(m_size);
m_renderTarget = new QGLFramebufferObjectSurface(m_data);
m_tex = QGLTexture2D::fromTextureId(m_data->texture(), m_size);
m_tex->setParent(m_material);
m_material->setTexture(m_tex, 1);
m_material->setObjectName("Atlas material");
m_initialized = true;
Expand Down
3 changes: 2 additions & 1 deletion demos/qt3d/photobrowser3d/skybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
#include <QFileInfo>

SkyBox::SkyBox(QGLView *view, const QString &imagePath)
: m_scene(0)
: QObject(view)
, m_scene(0)
, m_view(view)
, m_camera(new QGLCamera(this))
{
Expand Down
5 changes: 2 additions & 3 deletions demos/qt3d/photobrowser3d/thumbnailnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ThumbnailNode::ThumbnailNode(QObject *parent)
, m_manager(0)
, m_lastDistance(ThumbnailNode::Unknown)
{
setPalette(new QGLMaterialCollection(this));
setPalette(QSharedPointer<QGLMaterialCollection>(new QGLMaterialCollection(this)));
}

ThumbnailNode::~ThumbnailNode()
Expand Down Expand Up @@ -139,13 +139,12 @@ void ThumbnailNode::loadFullImage()
m_full->materialIndex() == m_defaultMaterial)
{
QGLMaterial *mat = new QGLMaterial;
QGLTexture2D *tex = new QGLTexture2D;
QGLTexture2D *tex = new QGLTexture2D(mat);
tex->setImage(m_image.data());
mat->setTexture(tex);
mat->setObjectName(m_image.url().path());
int ix = palette()->addMaterial(mat);
m_full->setMaterialIndex(ix);
mat->setParent(m_full);
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/qt3d/geometry/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
// http://en.wikipedia.org/wiki/Icosahedron#Cartesian_coordinates
const float phi = 1.618033988749894848f;

Geometry::Geometry(QObject *parent, QGLMaterialCollection *materials)
Geometry::Geometry(QObject *parent, QSharedPointer<QGLMaterialCollection> materials)
: QGLSceneNode(parent)
{
setPalette(materials);
Expand Down
2 changes: 1 addition & 1 deletion examples/qt3d/geometry/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ QT_END_NAMESPACE
class Geometry : public QGLSceneNode
{
public:
Geometry(QObject *parent = 0, QGLMaterialCollection *materials = 0);
Geometry(QObject *parent, QSharedPointer<QGLMaterialCollection> materials);
};

#endif // GEOMETRY_H
2 changes: 1 addition & 1 deletion examples/qt3d/geometry/geometryview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ GeometryView::GeometryView(QWindow *parent)
lp->setSpotExponent(24);
lp->setSpotDirection(QVector3D(0.0, -1.0, -2.0));

palette = new QGLMaterialCollection(this);
QSharedPointer<QGLMaterialCollection> palette(new QGLMaterialCollection());

// first set up the geometry - an icosahedron
// this one handles all its own colors and textures
Expand Down
2 changes: 1 addition & 1 deletion examples/qt3d/tank/tankview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ TankView::TankView(QWindow *parent)
, m_count(0)
{
m_tankScene->setParent(this);
m_tankScene->setPalette(new QGLMaterialCollection(this));
m_tankScene->setPalette(QSharedPointer<QGLMaterialCollection>(new QGLMaterialCollection(this)));
m_tankScene->setEffect(QGL::LitDecalTexture2D);
m_tankScene->addNode(new QuadPlane(0, QSizeF(25, 25)));
Tank *tank = addTank();
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/sceneformats/assimp/qailoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,6 @@ void QAiLoader::loadTextures(aiMaterial *ma, QGLMaterial *mq)
*/
void QAiLoader::loadMaterial(aiMaterial *ma)
{
QGLMaterialCollection *palette = m_builder.palette();
QGLMaterial *mq = new QGLMaterial;
mq->setObjectName("___DEFAULT_NAME___");

Expand Down Expand Up @@ -548,7 +547,7 @@ void QAiLoader::loadMaterial(aiMaterial *ma)
//
// executive summary: don't muck around with the palettte outside of this call

int k = palette->addMaterial(mq);
int k = m_builder.palette()->addMaterial(mq);

Q_UNUSED(k);
//qDebug() << "loaded material" << k << mq;
Expand Down
1 change: 1 addition & 0 deletions src/plugins/sceneformats/assimp/qaiscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ QAiScene::QAiScene(const aiScene *scene, QAiSceneHandler *handler)
Q_ASSERT(scene);
QAiLoader loader(scene, handler);
m_root = loader.loadMeshes();
m_root->setParent(this);
m_animations = loader.loadAnimations();
}

Expand Down
3 changes: 0 additions & 3 deletions src/plugins/sceneformats/assimp/qaiscenehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,6 @@ void QAiSceneHandler::decodeOptions(const QString &options)

QGLAbstractScene *QAiSceneHandler::read()
{
qDebug() << "QAiSceneHandler::read()";


AiLoaderIOSystem *ios = new AiLoaderIOSystem(device(), url());
m_importer.SetIOHandler(ios);

Expand Down
8 changes: 4 additions & 4 deletions src/quick3d/qdeclarativeeffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,24 @@ class QDeclarativeEffectPrivate

~QDeclarativeEffectPrivate()
{
delete palette;
palette.clear();
}

QColor color;
bool useLighting;
bool decal;
bool blending;
QGLMaterialCollection *palette;
QSharedPointer<QGLMaterialCollection> palette;
int materialIndex;
qreal progress;
inline void ensureMaterial() {
if (!palette)
{
palette = new QGLMaterialCollection();
palette = QSharedPointer<QGLMaterialCollection>(new QGLMaterialCollection());
}
if (materialIndex == -1)
{
materialIndex = palette->addMaterial(new QGLMaterial(palette));
materialIndex = palette->addMaterial(new QGLMaterial(palette.data()));
palette->material(materialIndex)->setColor(color);
}
// TODO: decal & blending
Expand Down
15 changes: 6 additions & 9 deletions src/quick3d/qdeclarativemesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ class QDeclarativeMeshPrivate
*/
QDeclarativeMesh::QDeclarativeMesh(QObject *parent)
: QObject(parent)
{
d = new QDeclarativeMeshPrivate();
}
,d(new QDeclarativeMeshPrivate())
{}

/*!
\internal
Expand Down Expand Up @@ -324,7 +323,7 @@ void QDeclarativeMesh::initSceneObjectList()
*/
void QDeclarativeMesh::initAnimations()
{
qDebug("QDeclarativeMesh::initAnimations()");
//qDebug("QDeclarativeMesh::initAnimations()");
d->originalAnimations.clear();
if (d->scene) {
const QList<QGLSceneAnimation *>& rSrcList = d->scene->animations();
Expand Down Expand Up @@ -506,7 +505,7 @@ int QDeclarativeMesh::nextSceneBranchId() const
destruction rules apply, however this \a parent parameter gives the user extra flexibility if
required.
*/
int QDeclarativeMesh::createSceneBranch(QString nodeName, QObject *parent)
int QDeclarativeMesh::createSceneBranch(QString nodeName)
{
if (!d->scene) {
qWarning() << "Unable to split mesh: no scene initialised - attempt to add scene object failed.";
Expand All @@ -525,9 +524,7 @@ int QDeclarativeMesh::createSceneBranch(QString nodeName, QObject *parent)
if (parentNode)
parentNode->removeNode(sceneNode); //this becomes irrelevant.

//sceneNode->setParent(d->scene); //TODO: currently this fails as sceneNode changes make problems.
//If no specific parent is nominated, use the scene specified by the mesh
parent ? sceneNode->setParent(parent) : sceneNode->setParent(d->scene);
sceneNode->setParent(d->scene);
addSceneBranch(sceneNode, prevParent);

return branchId;
Expand Down Expand Up @@ -688,7 +685,7 @@ QObject *QDeclarativeMesh::material(const QString& nodeName, const QString& mate

QGLSceneNode *node = qobject_cast<QGLSceneNode *>(sceneObject);

QGLMaterialCollection *p = node->palette();
QGLMaterialCollection *p = node->palette().data();

QGLMaterial *params = p->material(materialName);
if (params && !d->connected.contains(params)) {
Expand Down
2 changes: 1 addition & 1 deletion src/quick3d/qdeclarativemesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Q_QT3D_QUICK_EXPORT QDeclarativeMesh : public QObject, public QDeclarative

//The following functions relate to splitting the main scene into sub-branches
int nextSceneBranchId() const;
int createSceneBranch(QString nodeName, QObject *parent = 0);
int createSceneBranch(QString nodeName);
int addSceneBranch(QGLSceneNode *rootSceneObject, QObject *previousParent=NULL);
void restoreSceneBranch(int branchId);
void resetSceneBranches();
Expand Down
13 changes: 11 additions & 2 deletions src/threed/geometry/qglbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,16 @@ QGLBuilder::QGLBuilder(QGLMaterialCollection *materials)
{
dptr->rootNode = new QGLSceneNode;
if (!materials)
materials = new QGLMaterialCollection(dptr->rootNode);
materials = new QGLMaterialCollection();
dptr->rootNode->setPalette(QSharedPointer<QGLMaterialCollection>(materials));
}

QGLBuilder::QGLBuilder(QSharedPointer<QGLMaterialCollection> materials)
: dptr(new QGLBuilderPrivate(this))
{
dptr->rootNode = new QGLSceneNode;
if (materials.isNull())
materials = QSharedPointer<QGLMaterialCollection>(new QGLMaterialCollection());
dptr->rootNode->setPalette(materials);
}

Expand Down Expand Up @@ -1344,7 +1353,7 @@ QGLSceneNode *QGLBuilder::popNode()
\sa sceneNode()
*/
QGLMaterialCollection *QGLBuilder::palette()
QSharedPointer<QGLMaterialCollection> QGLBuilder::palette()
{
return dptr->rootNode->palette();
}
Expand Down
3 changes: 2 additions & 1 deletion src/threed/geometry/qglbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Q_QT3D_EXPORT QGLBuilder
{
public:
explicit QGLBuilder(QGLMaterialCollection *materials = 0);
explicit QGLBuilder(QSharedPointer<QGLMaterialCollection> materials);
virtual ~QGLBuilder();

// section management
Expand All @@ -77,7 +78,7 @@ class Q_QT3D_EXPORT QGLBuilder
QGLSceneNode *newNode();
QGLSceneNode *pushNode();
QGLSceneNode *popNode();
QGLMaterialCollection *palette();
QSharedPointer<QGLMaterialCollection> palette();
QGLSceneNode *finalizedSceneNode();

// geometry building by primitive
Expand Down
12 changes: 6 additions & 6 deletions src/threed/scene/qglscenenode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ void QGLSceneNode::setMaterial(QGLMaterial *material)
{
Q_D(QGLSceneNode);
if (!d->palette)
d->palette = new QGLMaterialCollection(this);
d->palette = QSharedPointer<QGLMaterialCollection>(new QGLMaterialCollection(this));
int ix = d->palette->indexOf(material);
if (ix == -1)
ix = d->palette->addMaterial(material);
Expand Down Expand Up @@ -997,7 +997,7 @@ void QGLSceneNode::setBackMaterial(QGLMaterial *material)
{
Q_D(QGLSceneNode);
if (!d->palette)
d->palette = new QGLMaterialCollection(this);
d->palette = QSharedPointer<QGLMaterialCollection>(new QGLMaterialCollection(this));
int ix = d->palette->indexOf(material);
if (ix == -1)
ix = d->palette->addMaterial(material);
Expand All @@ -1009,7 +1009,7 @@ void QGLSceneNode::setBackMaterial(QGLMaterial *material)
\sa setPalette()
*/
QGLMaterialCollection *QGLSceneNode::palette() const
QSharedPointer<QGLMaterialCollection> QGLSceneNode::palette() const
{
Q_D(const QGLSceneNode);
return d->palette;
Expand All @@ -1020,10 +1020,10 @@ QGLMaterialCollection *QGLSceneNode::palette() const
\sa palette()
*/
void QGLSceneNode::setPalette(QGLMaterialCollection *palette)
void QGLSceneNode::setPalette(QSharedPointer<QGLMaterialCollection> palette)
{
Q_D(QGLSceneNode);
if (d->palette != palette) {
if (d->palette.data() != palette.data()) {
d->palette = palette;
emit updated();
}
Expand Down Expand Up @@ -2056,7 +2056,7 @@ void qDumpScene(QGLSceneNode *node, bool detailed, int indent, const QSet<QGLSce
{
qDebug("%s material: %d", qPrintable(ind), node->materialIndex());
QGLMaterial *mat = node->material();
QGLMaterialCollection *pal = node->palette();
QGLMaterialCollection *pal = node->palette().data();
if (pal)
qDebug("%s palette: %p", qPrintable(ind), pal);
else
Expand Down
4 changes: 2 additions & 2 deletions src/threed/scene/qglscenenode.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ class Q_QT3D_EXPORT QGLSceneNode : public QObject
QGLMaterial *backMaterial() const;
void setBackMaterial(QGLMaterial *material);

QGLMaterialCollection *palette() const;
void setPalette(QGLMaterialCollection *palette);
QSharedPointer<QGLMaterialCollection> palette() const;
void setPalette(QSharedPointer<QGLMaterialCollection> palette);

QList<QGLSceneNode*> allChildren() const;
QList<QGLSceneNode *> children() const;
Expand Down
4 changes: 3 additions & 1 deletion src/threed/scene/qglscenenode_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
#include <QtCore/qstringlist.h>
#include <QtCore/qset.h>

#include <QSharedPointer>

QT_BEGIN_NAMESPACE

class QGLAbstractEffect;
Expand Down Expand Up @@ -118,7 +120,7 @@ class QGLSceneNodePrivate
}

QGeometryData geometry;
QGLMaterialCollection *palette;
QSharedPointer<QGLMaterialCollection> palette;
QMatrix4x4 localTransform;
QVector3D translate;
QList<QGraphicsTransform3D *> transforms;
Expand Down
Loading

0 comments on commit 2c29c50

Please sign in to comment.