File indexing completed on 2024-12-22 04:10:25

0001 /*
0002  *  SPDX-FileCopyrightText: 2010 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_simple_update_queue_test.h"
0008 #include <simpletest.h>
0009 
0010 #include "kistest.h"
0011 
0012 #include <KoColorSpace.h>
0013 #include <KoColorSpaceRegistry.h>
0014 
0015 #include "kis_group_layer.h"
0016 #include "kis_paint_layer.h"
0017 #include "kis_adjustment_layer.h"
0018 #include "filter/kis_filter.h"
0019 #include "filter/kis_filter_configuration.h"
0020 #include "filter/kis_filter_registry.h"
0021 #include "kis_selection.h"
0022 
0023 #include "kis_update_job_item.h"
0024 #include "kis_simple_update_queue.h"
0025 #include "scheduler_utils.h"
0026 #include <KisGlobalResourcesInterface.h>
0027 
0028 #include "lod_override.h"
0029 
0030 
0031 
0032 void KisSimpleUpdateQueueTest::testJobProcessing()
0033 {
0034     KisTestableUpdaterContext context(2);
0035 
0036     QRect imageRect(0,0,200,200);
0037 
0038     const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
0039     KisImageSP image = new KisImage(0, imageRect.width(), imageRect.height(), cs, "merge test");
0040 
0041     KisPaintLayerSP paintLayer = new KisPaintLayer(image, "test", OPACITY_OPAQUE_U8);
0042 
0043     image->barrierLock();
0044     image->addNode(paintLayer);
0045     image->unlock();
0046 
0047     QRect dirtyRect1(0,0,50,100);
0048     QRect dirtyRect2(0,0,100,100);
0049     QRect dirtyRect3(50,0,50,100);
0050     QRect dirtyRect4(150,150,50,50);
0051     QRect dirtyRect5(dirtyRect4); // theoretically, should be merged with 4
0052 
0053     QVector<KisUpdateJobItem*> jobs;
0054     KisWalkersList walkersList;
0055 
0056     /**
0057      * Process the queue and look what has been added into
0058      * the updater context
0059      */
0060 
0061     KisTestableSimpleUpdateQueue queue;
0062 
0063     queue.addUpdateJob(paintLayer, dirtyRect1, imageRect, 0);
0064     queue.addUpdateJob(paintLayer, dirtyRect2, imageRect, 0);
0065     queue.addUpdateJob(paintLayer, dirtyRect3, imageRect, 0);
0066     queue.addUpdateJob(paintLayer, dirtyRect4, imageRect, 0);
0067 
0068     {
0069         TestUtil::LodOverride l(1, image);
0070         queue.addUpdateJob(paintLayer, dirtyRect5, imageRect, 1);
0071     }
0072 
0073     queue.processQueue(context);
0074 
0075     jobs = context.getJobs();
0076 
0077     QVERIFY(checkWalker(jobs[0]->walker(), dirtyRect2));
0078     QVERIFY(checkWalker(jobs[1]->walker(), dirtyRect4));
0079     QCOMPARE(jobs.size(), 2);
0080 
0081     QCOMPARE(context.currentLevelOfDetail(), 0);
0082 
0083 
0084     walkersList = queue.getWalkersList();
0085 
0086     QCOMPARE(walkersList.size(), 1);
0087     QVERIFY(checkWalker(walkersList[0], dirtyRect5, 1));
0088 }
0089 
0090 void KisSimpleUpdateQueueTest::testSplitUpdate()
0091 {
0092     testSplit(false);
0093 }
0094 
0095 void KisSimpleUpdateQueueTest::testSplitFullRefresh()
0096 {
0097     testSplit(true);
0098 }
0099 
0100 void KisSimpleUpdateQueueTest::testSplit(bool useFullRefresh)
0101 {
0102     QRect imageRect(0,0,1024,1024);
0103 
0104     const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
0105     KisImageSP image = new KisImage(0, imageRect.width(), imageRect.height(), cs, "merge test");
0106 
0107     KisPaintLayerSP paintLayer = new KisPaintLayer(image, "test", OPACITY_OPAQUE_U8);
0108 
0109     image->barrierLock();
0110     image->addNode(paintLayer);
0111     image->unlock();
0112 
0113     QRect dirtyRect1(0,0,1000,1000);
0114 
0115     KisTestableSimpleUpdateQueue queue;
0116     KisWalkersList& walkersList = queue.getWalkersList();
0117 
0118     if(!useFullRefresh) {
0119         queue.addUpdateJob(paintLayer, dirtyRect1, imageRect, 0);
0120     }
0121     else {
0122         queue.addFullRefreshJob(paintLayer, dirtyRect1, imageRect, 0);
0123     }
0124 
0125     QCOMPARE(walkersList.size(), 4);
0126 
0127     QVERIFY(checkWalker(walkersList[0], QRect(0,0,512,512)));
0128     QVERIFY(checkWalker(walkersList[1], QRect(512,0,488,512)));
0129     QVERIFY(checkWalker(walkersList[2], QRect(0,512,512,488)));
0130     QVERIFY(checkWalker(walkersList[3], QRect(512,512,488,488)));
0131 
0132     queue.optimize();
0133 
0134     //must change nothing
0135 
0136     QCOMPARE(walkersList.size(), 4);
0137     QVERIFY(checkWalker(walkersList[0], QRect(0,0,512,512)));
0138     QVERIFY(checkWalker(walkersList[1], QRect(512,0,488,512)));
0139     QVERIFY(checkWalker(walkersList[2], QRect(0,512,512,488)));
0140     QVERIFY(checkWalker(walkersList[3], QRect(512,512,488,488)));
0141 }
0142 
0143 void KisSimpleUpdateQueueTest::testChecksum()
0144 {
0145     QRect imageRect(0,0,512,512);
0146     QRect dirtyRect(100,100,100,100);
0147 
0148     const KoColorSpace * colorSpace = KoColorSpaceRegistry::instance()->rgb8();
0149     KisImageSP image = new KisImage(0, imageRect.width(), imageRect.height(), colorSpace, "test");
0150 
0151     KisLayerSP paintLayer1 = new KisPaintLayer(image, "paint1", OPACITY_OPAQUE_U8);
0152     KisAdjustmentLayerSP adjustmentLayer = new KisAdjustmentLayer(image, "adj", 0, 0);
0153 
0154     image->barrierLock();
0155     image->addNode(paintLayer1, image->rootLayer());
0156     image->addNode(adjustmentLayer, image->rootLayer());
0157     image->unlock();
0158 
0159     KisFilterSP filter = KisFilterRegistry::instance()->value("blur");
0160     Q_ASSERT(filter);
0161     KisFilterConfigurationSP configuration = filter->defaultConfiguration(KisGlobalResourcesInterface::instance());
0162 
0163 
0164     KisTestableSimpleUpdateQueue queue;
0165     KisWalkersList& walkersList = queue.getWalkersList();
0166 
0167     {
0168         TestUtil::LodOverride l(1, image);
0169         queue.addUpdateJob(adjustmentLayer, dirtyRect, imageRect, 1);
0170         QCOMPARE(walkersList[0]->checksumValid(), true);
0171         QCOMPARE(walkersList[0]->levelOfDetail(), 1);
0172     }
0173 
0174     adjustmentLayer->setFilter(configuration->cloneWithResourcesSnapshot());
0175 
0176     {
0177         TestUtil::LodOverride l(1, image);
0178         QCOMPARE(walkersList[0]->checksumValid(), false);
0179     }
0180 
0181 
0182     QVector<KisUpdateJobItem*> jobs;
0183     KisTestableUpdaterContext context(2);
0184 
0185     {
0186         TestUtil::LodOverride l(1, image);
0187         queue.processQueue(context);
0188     }
0189 
0190     jobs = context.getJobs();
0191 
0192     {
0193         TestUtil::LodOverride l(1, image);
0194         QCOMPARE(jobs[0]->walker()->checksumValid(), true);
0195     }
0196 }
0197 
0198 void KisSimpleUpdateQueueTest::testMixingTypes()
0199 {
0200     QRect imageRect(0,0,1024,1024);
0201 
0202     const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
0203     KisImageSP image = new KisImage(0, imageRect.width(), imageRect.height(), cs, "merge test");
0204 
0205     KisPaintLayerSP paintLayer = new KisPaintLayer(image, "test", OPACITY_OPAQUE_U8);
0206 
0207     image->barrierLock();
0208     image->addNode(paintLayer);
0209     image->unlock();
0210 
0211     QRect dirtyRect1(0,0,200,200);
0212     QRect dirtyRect2(0,0,200,200);
0213     QRect dirtyRect3(20,20,200,200);
0214 
0215     KisTestableSimpleUpdateQueue queue;
0216     KisWalkersList& walkersList = queue.getWalkersList();
0217 
0218     queue.addUpdateJob(paintLayer, dirtyRect1, imageRect, 0);
0219     queue.addFullRefreshJob(paintLayer, dirtyRect2, imageRect, 0);
0220     queue.addFullRefreshJob(paintLayer, dirtyRect3, imageRect, 0);
0221     queue.addUpdateNoFilthyJob(paintLayer, dirtyRect1, imageRect, 0);
0222     queue.addFullRefreshNoFilthyJob(paintLayer, {dirtyRect1}, imageRect, 0);
0223 
0224     QCOMPARE(walkersList.size(), 4);
0225 
0226     QVERIFY(checkWalker(walkersList[0], QRect(0,0,200,200)));
0227     QVERIFY(checkWalker(walkersList[1], QRect(0,0,220,220)));
0228     QVERIFY(checkWalker(walkersList[2], QRect(0,0,200,200)));
0229     QVERIFY(checkWalker(walkersList[3], QRect(0,0,200,200)));
0230 
0231     QCOMPARE(walkersList[0]->type(), KisBaseRectsWalker::UPDATE);
0232     QCOMPARE(walkersList[1]->type(), KisBaseRectsWalker::FULL_REFRESH);
0233     QCOMPARE(walkersList[2]->type(), KisBaseRectsWalker::UPDATE_NO_FILTHY);
0234     QCOMPARE(walkersList[3]->type(), KisBaseRectsWalker::FULL_REFRESH_NO_FILTHY);
0235 }
0236 
0237 void KisSimpleUpdateQueueTest::testSpontaneousJobsCompression()
0238 {
0239     KisTestableSimpleUpdateQueue queue;
0240     KisSpontaneousJobsList &jobsList = queue.getSpontaneousJobsList();
0241 
0242     QVERIFY(queue.isEmpty());
0243     QCOMPARE(queue.sizeMetric(), 0);
0244     QVERIFY(jobsList.isEmpty());
0245 
0246     // we cannot use scoped pointers for the jobs, since they become owned by the queue
0247     KisSpontaneousJob *job1 = new KisNoopSpontaneousJob(false);
0248     queue.addSpontaneousJob(job1);
0249 
0250     QVERIFY(!queue.isEmpty());
0251     QCOMPARE(queue.sizeMetric(), 1);
0252     QCOMPARE(jobsList.size(), 1);
0253     QCOMPARE(jobsList[0], job1);
0254 
0255     KisSpontaneousJob *job2 = new KisNoopSpontaneousJob(false);
0256     queue.addSpontaneousJob(job2);
0257 
0258     QVERIFY(!queue.isEmpty());
0259     QCOMPARE(queue.sizeMetric(), 2);
0260     QCOMPARE(jobsList.size(), 2);
0261     QCOMPARE(jobsList[0], job1);
0262     QCOMPARE(jobsList[1], job2);
0263 
0264     KisSpontaneousJob *job3 = new KisNoopSpontaneousJob(true);
0265     queue.addSpontaneousJob(job3);
0266 
0267     QVERIFY(!queue.isEmpty());
0268     QCOMPARE(queue.sizeMetric(), 1);
0269     QCOMPARE(jobsList.size(), 1);
0270     QCOMPARE(jobsList[0], job3);
0271 }
0272 
0273 KISTEST_MAIN(KisSimpleUpdateQueueTest)
0274