[Qt-jambi-interest] QPixmap erratic performance
Bart van Deenen
bvandeenen at infologic.nl
Mon Nov 17 11:27:25 CET 2008
On Monday 17 November 2008 10:09:51 Bart van Deenen wrote:
> On Monday 17 November 2008 09:54:38 Gunnar Sletta wrote:
> > > Ok. I'm starting to get somewhere. I've added instrumentation to the
> > > code (not shown here) which uses System.currentTimeMillis() to measure
> > > intervals, and it turns out that the interval between two consecutive
> > > calls to timerEvent is very erratic! My 'render' function (currently
> > > with a much simplified QPixmap tree under it) only takes a few ms. The
> > > QTimer is supposed to run at 40 ms interval, but the actual intervals
> > > are anywhere between 15 and 300 ms!
> >
> > A timer tries to fire as close to, but at least 40 ms after the last
> > one, but there are no guarantees. I've never seen such behaviour, so I
> > would have to see your code to know more
>
> I'm simplifying the code to narrow it down to where the problem occurs, and
> then I'll post it. Thanks for thinking along.
>
> Bart
I've narrowed down my source code to something simple. Unfortunately, now the
erraticness is gone! It seems to have to do with the number or size of
QPixmaps I'm using to draw my main QPixmap :-(
Here's my non-erratic code. The only difference with my problem code is that
the 'render' function here consists of one QPixmap being drawn into my root
object, and that with the trouble code there's a lot of QPixmaps rendering
into each other, and finally being combined into the root QPixmap. The problem
is not the overal time it takes to do the rendering; with a simplified tree of
objects that is consistently below 5 ms. I don't understand where the problem
is starting to occur :-(. Next I'll add some more of my 'sprite_object's to
see if it's the number or size of them that matters.
Bart
import java.lang.reflect.Method;
import com.trolltech.qt.core.QTimer;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QColor;
import com.trolltech.qt.gui.QFont;
import com.trolltech.qt.gui.QPaintEvent;
import com.trolltech.qt.gui.QPainter;
import com.trolltech.qt.gui.QPixmap;
import com.trolltech.qt.gui.QWidget;
public class Test extends QWidget
{
private QTimer timer;
private int x, y;
private FunctionTimer paint_function_timer = new FunctionTimer();
private FunctionTimer event_function_timer = new FunctionTimer();
private QPixmap root_pixmap;
private QPixmap sprite_pixmap;
public Test() throws Exception
{
super();
root_pixmap = new QPixmap(400, 600);
sprite_pixmap = new QPixmap(200, 100);
sprite_pixmap.fill(QColor.white);
QPainter p = new QPainter(sprite_pixmap);
p.setBrush(QColor.black);
p.setFont(new QFont("arial", 60));
p.drawText(5, sprite_pixmap.height() - 5, "This is the sprite");
this.setGeometry(root_pixmap.rect());
timer = new QTimer(this);
timer.timeout.connect(this, "qtimer_event()");
timer.start(40);
}
public void paintEvent(QPaintEvent g)
{
paint_function_timer.end("calls between paintEvent");
FunctionTimer ft = new FunctionTimer();
QPainter painter = new QPainter();
painter.begin(this);
painter.drawPixmap(10, 30, root_pixmap);
painter.end();
ft.end("putting root.pixmap on screen");
}
protected void qtimer_event()
{
event_function_timer.end("timerEvent");
FunctionTimer ft = new FunctionTimer();
this.render();
ft.end("this.render(root) ");
update();
}
private void render()
{
x = (x + 1) % (root_pixmap.width() - sprite_pixmap.width());
y = (y + 1) % (root_pixmap.height() - sprite_pixmap.height());
root_pixmap.fill(QColor.transparent);
QPainter p = new QPainter(root_pixmap);
p.drawPixmap(x, y, sprite_pixmap);
p.end();
}
public static void main(String[] args) throws Exception
{
QApplication.initialize(args);
Test qtMain = new Test();
qtMain.show();
QApplication.exec();
}
public class FunctionTimer
{
long t0;
public FunctionTimer()
{
t0 = System.currentTimeMillis();
}
public void end(String f, Object... stringformat)
{
long t = System.currentTimeMillis();
long delta = t - t0;
t0 = t;
Class[] argTypes = { String.class, Object[].class };
try
{
Method a = String.class.getMethod("format", argTypes);
System.out.println(a.invoke(null, f, stringformat) + " took "
+ delta + "ms.");
}
catch (Exception e)
{
}
}
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-jambi-interest/attachments/20081117/cfc4a1a9/attachment.html
More information about the Qt-jambi-interest
mailing list