QmlProfiler: optimizations in canvas repaint

Change-Id: I866f8941129c5a42256f40dca4e4c4fe15b5a760
Reviewed-by: Kai Koehne <kai.koehne@nokia.com>
This commit is contained in:
Christiaan Janssen
2011-10-28 16:22:45 +02:00
parent 058878a6c6
commit 70b01ced49
12 changed files with 208 additions and 344 deletions

View File

@@ -2,10 +2,10 @@ INCLUDEPATH += $$PWD
HEADERS += $$PWD/qdeclarativecontext2d_p.h \
$$PWD/qdeclarativecanvas_p.h \
$$PWD/qdeclarativetiledcanvas_p.h \
$$PWD/qmlprofilercanvas.h \
$$PWD/qdeclarativecanvastimer_p.h
SOURCES += $$PWD/qdeclarativecontext2d.cpp \
$$PWD/qdeclarativecanvas.cpp \
$$PWD/qdeclarativetiledcanvas.cpp \
$$PWD/qmlprofilercanvas.cpp \
$$PWD/qdeclarativecanvastimer.cpp

View File

@@ -1,163 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (info@qt.nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qdeclarativetiledcanvas_p.h"
#include "qdeclarativecontext2d_p.h"
#include <QtGui/qpixmap.h>
#include <QtGui/qpainter.h>
TiledCanvas::TiledCanvas()
: m_context2d(new Context2D(this)), m_canvasSize(-1, -1), m_tileSize(100, 100)
{
setFlag(QGraphicsItem::ItemHasNoContents, false);
setAcceptedMouseButtons(Qt::LeftButton);
setCacheMode(QGraphicsItem::DeviceCoordinateCache);
}
QSizeF TiledCanvas::canvasSize() const
{
return m_canvasSize;
}
void TiledCanvas::setCanvasSize(const QSizeF &v)
{
if (m_canvasSize != v) {
m_canvasSize = v;
emit canvasSizeChanged();
update();
}
}
QSize TiledCanvas::tileSize() const
{
return m_tileSize;
}
void TiledCanvas::setTileSize(const QSize &v)
{
if (v != m_tileSize) {
m_tileSize = v;
emit tileSizeChanged();
update();
}
}
QRectF TiledCanvas::canvasWindow() const
{
return m_canvasWindow;
}
void TiledCanvas::setCanvasWindow(const QRectF &v)
{
if (m_canvasWindow != v) {
m_canvasWindow = v;
emit canvasWindowChanged();
update();
}
}
void TiledCanvas::requestPaint()
{
update();
}
void TiledCanvas::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
{
if (m_context2d->size() != m_tileSize)
m_context2d->setSize(m_tileSize);
const int tw = m_tileSize.width();
const int th = m_tileSize.height();
int h1 = m_canvasWindow.left() / tw;
int htiles = ((m_canvasWindow.right() - h1 * tw) + tw - 1) / tw;
int v1 = m_canvasWindow.top() / th;
int vtiles = ((m_canvasWindow.bottom() - v1 * th) + th - 1) / th;
for (int yy = 0; yy < vtiles; ++yy) {
for (int xx = 0; xx < htiles; ++xx) {
int ht = xx + h1;
int vt = yy + v1;
m_context2d->reset();
m_context2d->setPainterTranslate(QPoint(-ht * tw, -vt * th));
emit drawRegion(m_context2d, QRect(ht * tw, vt * th, tw, th));
p->drawPixmap(-m_canvasWindow.x() + ht * tw, -m_canvasWindow.y() + vt * th, m_context2d->pixmap());
}
}
}
void TiledCanvas::componentComplete()
{
const QMetaObject *metaObject = this->metaObject();
int propertyCount = metaObject->propertyCount();
int requestPaintMethod = metaObject->indexOfMethod("requestPaint()");
for (int ii = TiledCanvas::staticMetaObject.propertyCount(); ii < propertyCount; ++ii) {
QMetaProperty p = metaObject->property(ii);
if (p.hasNotifySignal())
QMetaObject::connect(this, p.notifySignalIndex(), this, requestPaintMethod, 0, 0);
}
QDeclarativeItem::componentComplete();
}
void TiledCanvas::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event);
}
QPixmap TiledCanvas::getTile(int xx, int yy)
{
QPixmap pix(m_tileSize);
pix.fill(Qt::green);
QString text = QString::number(xx) + QLatin1Char(' ') + QString::number(yy);
QPainter p(&pix);
p.drawText(pix.rect(), Qt::AlignHCenter | Qt::AlignVCenter, text);
return pix;
}

View File

@@ -1,123 +0,0 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#ifndef QDECLARATIVETILEDCANVAS_P_H
#define QDECLARATIVETILEDCANVAS_P_H
#include <QtDeclarative/qdeclarativeitem.h>
QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE
QT_MODULE(Declarative)
class Context2D;
class TiledCanvas : public QDeclarativeItem
{
Q_OBJECT/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
Q_PROPERTY(QSizeF canvasSize READ canvasSize WRITE setCanvasSize NOTIFY canvasSizeChanged)
Q_PROPERTY(QSize tileSize READ tileSize WRITE setTileSize NOTIFY tileSizeChanged)
Q_PROPERTY(QRectF canvasWindow READ canvasWindow WRITE setCanvasWindow NOTIFY canvasWindowChanged)
public:
TiledCanvas();
QSizeF canvasSize() const;
void setCanvasSize(const QSizeF &);
QSize tileSize() const;
void setTileSize(const QSize &);
QRectF canvasWindow() const;
void setCanvasWindow(const QRectF &);
Q_SIGNALS:
void canvasSizeChanged();
void tileSizeChanged();
void canvasWindowChanged();
void drawRegion(Context2D *ctxt, const QRect &region);
public Q_SLOTS:
void requestPaint();
protected:
virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
virtual void componentComplete();
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
private:
QPixmap getTile(int, int);
Context2D *m_context2d;
QSizeF m_canvasSize;
QSize m_tileSize;
QRectF m_canvasWindow;
};
QT_END_NAMESPACE
QT_END_HEADER
#endif // QDECLARATIVETILEDCANVAS_P_H

View File

@@ -0,0 +1,93 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#include "qmlprofilercanvas.h"
#include "qdeclarativecontext2d_p.h"
#include <QtGui/qpixmap.h>
#include <QtGui/qpainter.h>
namespace QmlProfiler {
namespace Internal {
QmlProfilerCanvas::QmlProfilerCanvas()
: m_context2d(new Context2D(this))
{
setFlag(QGraphicsItem::ItemHasNoContents, false);
setAcceptedMouseButtons(Qt::LeftButton);
setCacheMode(QGraphicsItem::DeviceCoordinateCache);
}
void QmlProfilerCanvas::requestPaint()
{
update();
}
void QmlProfilerCanvas::requestRedraw()
{
setDirty(true);
update();
}
void QmlProfilerCanvas::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
{
if (m_context2d->size().width() != width() || m_context2d->size().height() != height()) {
m_dirty = true;
m_context2d->setSize(width(), height());
}
if (m_dirty) {
m_context2d->reset();
emit drawRegion(m_context2d, QRect(0, 0, width(), height()));
setDirty(false);
}
p->drawPixmap(0, 0, m_context2d->pixmap());
}
void QmlProfilerCanvas::componentComplete()
{
const QMetaObject *metaObject = this->metaObject();
int propertyCount = metaObject->propertyCount();
int requestPaintMethod = metaObject->indexOfMethod("requestPaint()");
for (int ii = QmlProfilerCanvas::staticMetaObject.propertyCount(); ii < propertyCount; ++ii) {
QMetaProperty p = metaObject->property(ii);
if (p.hasNotifySignal())
QMetaObject::connect(this, p.notifySignalIndex(), this, requestPaintMethod, 0, 0);
}
QDeclarativeItem::componentComplete();
}
}
}

View File

@@ -0,0 +1,83 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#ifndef QMLPROFILERCANVAS_H
#define QMLPROFILERCANVAS_H
#include <QtDeclarative/QDeclarativeItem>
class Context2D;
namespace QmlProfiler {
namespace Internal {
class QmlProfilerCanvas : public QDeclarativeItem
{
Q_OBJECT
Q_PROPERTY(bool dirty READ dirty WRITE setDirty NOTIFY dirtyChanged)
public:
QmlProfilerCanvas();
bool dirty() const { return m_dirty; }
void setDirty(bool dirty)
{
if (m_dirty != dirty) {
m_dirty = dirty;
emit dirtyChanged(dirty);
}
}
signals:
void dirtyChanged(bool dirty);
void drawRegion(Context2D *ctxt, const QRect &region);
public slots:
void requestPaint();
void requestRedraw();
protected:
virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
virtual void componentComplete();
private:
Context2D *m_context2d;
bool m_dirty;
};
}
}
#endif // QMLPROFILERCANVAS_H