From e76ea9e634775180ffbb3fd9c6479a31aeaa43fd Mon Sep 17 00:00:00 2001 From: giersch Date: Wed, 7 Nov 2007 10:26:06 +0000 Subject: [PATCH] . --- DrawingWindow.cpp | 66 ++++++++++++++++++++++++++++------------------- test/hello.cpp | 14 ++++------ test/hello.pro | 10 +++---- 3 files changed, 50 insertions(+), 40 deletions(-) diff --git a/DrawingWindow.cpp b/DrawingWindow.cpp index 64508ff..eb79aec 100644 --- a/DrawingWindow.cpp +++ b/DrawingWindow.cpp @@ -36,8 +36,8 @@ public: QBasicTimer timer; QMutex imageMutex; - QMutex paintMutex; - QWaitCondition paintCondition; + QMutex syncMutex; + QWaitCondition syncCondition; bool painted; int lockCount; @@ -66,6 +66,7 @@ public: void dirty(int x1, int y1, int x2, int y2); void dirty(const QRect &rect); + void update(); }; //--- DrawingWindow ---------------------------------------------------- @@ -155,12 +156,11 @@ void DrawingWindow::drawRect(int x1, int y1, int x2, int y2) bool DrawingWindow::sync(unsigned long time) { - bool ret; - d->safeLock(d->paintMutex); - QApplication::postEvent(this, new QEvent(QEvent::User)); - ret = d->paintCondition.wait(&d->paintMutex, time); - d->safeUnlock(d->paintMutex); - return ret; + d->safeLock(d->syncMutex); + qApp->postEvent(this, new QEvent(QEvent::User)); + bool synced = d->syncCondition.wait(&d->syncMutex, time); + d->safeUnlock(d->syncMutex); + return synced; } void DrawingWindow::sleep(unsigned long secs) @@ -182,24 +182,32 @@ void DrawingWindow::closeEvent(QCloseEvent *ev) { d->timer.stop(); d->thread->terminate(); - d->paintCondition.wakeAll(); + d->syncMutex.lock(); + d->syncCondition.wakeAll(); + d->syncMutex.unlock(); QWidget::closeEvent(ev); d->thread->wait(); } void DrawingWindow::customEvent(QEvent *) { - d->paintMutex.lock(); - d->imageMutex.lock(); - if (d->dirtyFlag) { - QRect r = d->dirtyRect; - d->dirtyFlag = false; - d->imageMutex.unlock(); - repaint(r); - } else - d->imageMutex.unlock(); - d->paintCondition.wakeAll(); - d->paintMutex.unlock(); + d->syncMutex.lock(); + d->update(); + qApp->sendPostedEvents(this, QEvent::UpdateLater); + qApp->sendPostedEvents(this, QEvent::UpdateRequest); + qApp->sendPostedEvents(this, QEvent::Paint); + if (0) { // Disabled because this can lead to + // dead-lock if a close event is + // processed ! + qApp->processEvents(QEventLoop::ExcludeUserInputEvents | + QEventLoop::ExcludeSocketNotifiers | + QEventLoop::DeferredDeletion | + QEventLoop::X11ExcludeTimers); + } + qApp->flush(); + qApp->syncX(); + d->syncCondition.wakeAll(); + d->syncMutex.unlock(); } void DrawingWindow::keyPressEvent(QKeyEvent *ev) @@ -237,12 +245,7 @@ void DrawingWindow::showEvent(QShowEvent *ev) void DrawingWindow::timerEvent(QTimerEvent *ev) { if (ev->timerId() == d->timer.timerId()) { - d->imageMutex.lock(); - if (d->dirtyFlag) { - update(d->dirtyRect); - d->dirtyFlag = false; - } - d->imageMutex.unlock(); + d->update(); d->timer.start(d->paintInterval, this); } else { QWidget::timerEvent(ev); @@ -329,6 +332,17 @@ void DrawingWindowPrivate::dirty(const QRect &rect) } } +void DrawingWindowPrivate::update() +{ + imageMutex.lock(); + bool dirty = dirtyFlag; + QRect rect = dirtyRect; + dirtyFlag = false; + imageMutex.unlock(); + if (dirty) + q->update(rect); +} + //--- DrawingThread ---------------------------------------------------- DrawingThread::DrawingThread(DrawingWindow &w, DrawingWindow::ThreadFunction f) diff --git a/test/hello.cpp b/test/hello.cpp index fe2f2a0..a63827b 100644 --- a/test/hello.cpp +++ b/test/hello.cpp @@ -32,24 +32,18 @@ void flip(DrawingWindow &w) int c = 0; int y = 0; -// int h = w.height; -// int w = w.width; int count = 50;//1 << 31; while (1) { -// std::cerr << "loooooooooooooooooooooop " -// << y << " (" << c << ")\n"; w.setColor(c, c, c); for (int yy = y; yy < y + 10; yy++) { for (int x = 0; x < w.width; x++) w.drawPoint(x, yy); - w.sync(); } if ((y += 10) >= w.height) { + w.sync(); y = 0; c = !c; if (!--count) break; -// std::cerr << "loooooooooooooooooooooop " -// << y << " (" << c << ")\n"; } } } @@ -116,7 +110,8 @@ void mandel(DrawingWindow &w) ci += pi; } cr += pr; -// w.sync(); +// if (x % 10 == 0) +// w.sync(); } } @@ -135,7 +130,8 @@ void lines(DrawingWindow &w) int y2 = rand() % ymax; w.setColor(r, g, b); w.drawLine(x1, y1, x2, y2); - w.sync(); + if (n % 100 == 0) + w.sync(); } } diff --git a/test/hello.pro b/test/hello.pro index 146e9c3..6d133ee 100644 --- a/test/hello.pro +++ b/test/hello.pro @@ -3,12 +3,12 @@ TARGET = hello CONFIG += qt debug -QMAKE_CFLAGS += -O3 -QMAKE_CXXFLAGS += -O3 +QMAKE_CFLAGS += -O2 +QMAKE_CXXFLAGS += -O2 -QMAKE_CFLAGS += -pg -QMAKE_CXXFLAGS += -pg -QMAKE_LFLAGS += -pg +#QMAKE_CFLAGS += -pg +#QMAKE_CXXFLAGS += -pg +#QMAKE_LFLAGS += -pg HEADERS += DrawingWindow.h -- 2.20.1