Commit 990d6c2f authored by Dmitry Kazakov's avatar Dmitry Kazakov

Made a scheduler for updates

I've faced with a great problem right now. Not all the code of Krita
is reentrant. There are at least three places for the crash:
   - KoColorSpaceFactory::grabColorspace
   - KisLayer::hasEffectMasks
   - KisMementoManager::removeMemento

svn path=/trunk/koffice/; revision=1133445
parent 128f90af
......@@ -128,6 +128,7 @@ set(kritaimage_LIB_SRCS
kis_updater_context.cpp
kis_abstract_update_queue.cpp
kis_simple_update_queue.cpp
kis_update_scheduler.cpp
kis_group_layer.cc
kis_count_visitor.cpp
kis_histogram.cc
......
......@@ -65,8 +65,8 @@ void KisAbstractUpdateQueue::executeJobSync(KisBaseRectsWalkerSP walker,
updaterContext.addJob(walker);
updaterContext.waitForDone();
startProcessing(updaterContext);
updaterContext.unlock();
startProcessing(updaterContext);
}
/*
* Copyright (c) 2010 Dmitry Kazakov <dimula73@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kis_update_scheduler.h"
#include "kis_merge_walker.h"
#include "kis_full_refresh_walker.h"
#include "kis_simple_update_queue.h"
KisUpdateScheduler::KisUpdateScheduler(KisImageWSP image)
: m_image(image)
{
/**
* FIXME: Make queues configurable with a factory
*/
m_workQueue = new KisSimpleUpdateQueue();
connect(&m_updaterContext, SIGNAL(sigContinueUpdate(const QRect&)),
m_image, SLOT(slotProjectionUpdated(const QRect&)),
Qt::DirectConnection);
connect(&m_updaterContext, SIGNAL(sigDoSomeUsefulWork()),
SLOT(doSomeUsefulWork()), Qt::DirectConnection);
connect(&m_updaterContext, SIGNAL(sigSpareThreadAppeared()),
SLOT(spareThreadAppeared()), Qt::DirectConnection);
}
KisUpdateScheduler::~KisUpdateScheduler()
{
}
void KisUpdateScheduler::updateProjection(KisNodeSP node, const QRect& rc)
{
const QRect cropRect = m_image->bounds();
KisBaseRectsWalkerSP walker = new KisMergeWalker(cropRect);
walker->collectRects(node, rc);
m_workQueue->addJob(walker);
m_workQueue->processQueue(m_updaterContext);
}
void KisUpdateScheduler::fullRefresh(KisNodeSP root)
{
const QRect cropRect = m_image->bounds();
KisBaseRectsWalkerSP walker = new KisFullRefreshWalker(cropRect);
walker->collectRects(root, cropRect);
m_workQueue->executeJobSync(walker, m_updaterContext);
}
void KisUpdateScheduler::lock()
{
m_workQueue->blockProcessing(m_updaterContext);
}
void KisUpdateScheduler::unlock()
{
m_workQueue->startProcessing(m_updaterContext);
}
void KisUpdateScheduler::doSomeUsefulWork()
{
m_workQueue->optimize();
}
void KisUpdateScheduler::spareThreadAppeared()
{
m_workQueue->processQueue(m_updaterContext);
}
/*
* Copyright (c) 2010 Dmitry Kazakov <dimula73@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef __KIS_UPDATE_SCHEDULER_H
#define __KIS_UPDATE_SCHEDULER_H
#include "kis_updater_context.h"
#include "kis_abstract_update_queue.h"
class KRITAIMAGE_EXPORT KisUpdateScheduler : public QObject
{
Q_OBJECT
public:
KisUpdateScheduler(KisImageWSP image);
virtual ~KisUpdateScheduler();
void lock();
void unlock();
void updateProjection(KisNodeSP node, const QRect& rc);
void fullRefresh(KisNodeSP root);
protected slots:
void doSomeUsefulWork();
void spareThreadAppeared();
protected:
KisImageWSP m_image;
KisAbstractUpdateQueue* m_workQueue;
KisUpdaterContext m_updaterContext;
};
#endif /* __KIS_UPDATE_SCHEDULER_H */
......@@ -22,7 +22,7 @@
#include <QMutex>
#include <QRunnable>
#include "kis_merge_walker.h"
#include "kis_base_rects_walker.h"
#include "kis_async_merger.h"
class KisUpdateJobItem : public QObject, public QRunnable
......@@ -35,6 +35,7 @@ public:
}
void run() {
qDebug() << "Executing job" << m_walker->changeRect() << "on thread" << QThread::currentThreadId();
m_merger.startMerge(*m_walker);
QRect changeRect = m_walker->changeRect();
......@@ -74,7 +75,7 @@ class KRITAIMAGE_EXPORT KisUpdaterContext : public QObject
public:
KisUpdaterContext(qint32 threadCount = -1);
~KisUpdaterContext();
virtual ~KisUpdaterContext();
/**
......
......@@ -340,6 +340,12 @@ target_link_libraries(KisSimpleUpdateQueueTest ${KDE4_KDEUI_LIBS} kritaimage ${
########### next target ###############
set(kis_update_scheduler_test_SRCS kis_update_scheduler_test.cpp )
kde4_add_unit_test(KisUpdateSchedulerTest TESTNAME krita-image-KisUpdateSchedulerTest ${kis_update_scheduler_test_SRCS})
target_link_libraries(KisUpdateSchedulerTest ${KDE4_KDEUI_LIBS} kritaimage ${QT_QTTEST_LIBRARY})
########### next target ###############
set(kis_macro_test_SRCS kis_macro_test.cpp )
kde4_add_unit_test(KisMacroTest TESTNAME krita-image-KisMacroTest ${kis_macro_test_SRCS})
target_link_libraries(KisMacroTest ${KDE4_KDEUI_LIBS} kritaimage ${QT_QTTEST_LIBRARY})
......
......@@ -22,6 +22,7 @@
#include <KoColorSpace.h>
#include <KoColorSpaceRegistry.h>
#include "kis_merge_walker.h"
#include "kis_simple_update_queue.h"
void KisSimpleUpdateQueueTest::testJobProcessing()
......
/*
* Copyright (c) 2010 Dmitry Kazakov <dimula73@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kis_update_scheduler_test.h"
#include <qtest_kde.h>
#include <KoColorSpace.h>
#include <KoColorSpaceRegistry.h>
#include "kis_update_scheduler.h"
#include "../../sdk/tests/testutil.h"
void KisUpdateSchedulerTest::testMerge()
{
QImage sourceImage1(QString(FILES_DATA_DIR) + QDir::separator() + "hakonepa.png");
QImage sourceImage2(QString(FILES_DATA_DIR) + QDir::separator() + "inverted_hakonepa.png");
QRect imageRect = QRect(QPoint(0,0), sourceImage1.size());
const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
KisImageSP image = new KisImage(0, imageRect.width(), imageRect.height(), cs, "merge test");
KisFilterSP filter = KisFilterRegistry::instance()->value("blur");
Q_ASSERT(filter);
KisFilterConfiguration *configuration = filter->defaultConfiguration(0);
Q_ASSERT(configuration);
KisPaintLayerSP paintLayer1 = new KisPaintLayer(image, "paint1", OPACITY_OPAQUE_U8);
KisPaintLayerSP paintLayer2 = new KisPaintLayer(image, "paint2", OPACITY_OPAQUE_U8 / 3);
KisLayerSP blur1 = new KisAdjustmentLayer(image, "blur1", configuration, 0);
paintLayer1->paintDevice()->convertFromQImage(sourceImage1, "", 0, 0);
paintLayer2->paintDevice()->convertFromQImage(sourceImage2, "", 0, 0);
image->lock();
image->addNode(paintLayer1);
image->addNode(paintLayer2);
image->addNode(blur1);
image->unlock();
KisUpdateScheduler scheduler(image);
KisLayerSP rootLayer;
/**
* Test synchronous Full Refresh
*/
scheduler.fullRefresh(image->rootLayer());
rootLayer = image->rootLayer();
QCOMPARE(rootLayer->exactBounds(), image->bounds());
QImage resultFRProjection = rootLayer->projection()->convertToQImage(0);
resultFRProjection.save(QString(FILES_OUTPUT_DIR) + QDir::separator() + "scheduler_fr_merge_result.png");
/**
* Test incremental updates
*/
rootLayer->projection()->clear();
const qint32 num = 4;
qint32 width = imageRect.width() / num;
qint32 lastWidth = imageRect.width() - width;
QVector<QRect> dirtyRects(num);
for(qint32 i = 0; i < num-1; i++) {
dirtyRects[i] = QRect(width*i, 0, width, imageRect.height());
}
dirtyRects[num-1] = QRect(width*(num-1), 0, lastWidth, imageRect.height());
for(qint32 i = 0; i < num; i+=2) {
scheduler.updateProjection(paintLayer1, dirtyRects[i]);
}
for(qint32 i = 1; i < num; i+=2) {
scheduler.updateProjection(paintLayer1, dirtyRects[i]);
}
QTest::qSleep(1000);
QCOMPARE(rootLayer->exactBounds(), image->bounds());
QImage resultDirtyProjection = rootLayer->projection()->convertToQImage(0);
resultDirtyProjection.save(QString(FILES_OUTPUT_DIR) + QDir::separator() + "scheduler_dp_merge_result.png");
QPoint pt;
QVERIFY(TestUtil::compareQImages(pt, resultFRProjection, resultDirtyProjection));
}
QTEST_KDEMAIN(KisUpdateSchedulerTest, NoGUI)
#include "kis_update_scheduler_test.moc"
/*
* Copyright (c) 2010 Dmitry Kazakov <dimula73@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef KIS_UPDATE_SCHEDULER_TEST_H
#define KIS_UPDATE_SCHEDULER_TEST_H
#include <QtTest/QtTest>
class KisUpdateSchedulerTest : public QObject
{
Q_OBJECT
private slots:
void testMerge();
};
#endif /* KIS_UPDATE_SCHEDULER_TEST_H */
......@@ -22,6 +22,7 @@
#include <KoColorSpace.h>
#include <KoColorSpaceRegistry.h>
#include "kis_merge_walker.h"
#include "kis_updater_context.h"
void KisUpdaterContextTest::testJobInterference()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment