Warning, file /graphics/krita/libs/image/tiles3/tests/kis_tile_data_store_test.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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_tile_data_store_test.h"
0008 #include <simpletest.h>
0009 
0010 #include "kis_debug.h"
0011 
0012 #include "kis_image_config.h"
0013 
0014 #include "tiles3/kis_tiled_data_manager.h"
0015 #include "tiles_test_utils.h"
0016 
0017 #include "tiles3/kis_tile_data_store.h"
0018 #include "tiles3/kis_tile_data_store_iterators.h"
0019 
0020 
0021 void KisTileDataStoreTest::testClockIterator()
0022 {
0023     KisTileDataStore *store = KisTileDataStore::instance();
0024     store->debugClear();
0025 
0026     const qint32 pixelSize = 1;
0027     quint8 defaultPixel = 128;
0028 
0029     QList<KisTileData*> tileDataList;
0030     KisTileData *item;
0031 
0032     item = new KisTileData(pixelSize, &defaultPixel, store, false);
0033     store->registerTileData(item);
0034     tileDataList.append(item);
0035     item = new KisTileData(pixelSize, &defaultPixel, store, false);
0036     store->registerTileData(item);
0037     tileDataList.append(item);
0038     item = new KisTileData(pixelSize, &defaultPixel, store, false);
0039     store->registerTileData(item);
0040     tileDataList.append(item);
0041 
0042 
0043     /// First, full cycle!
0044     KisTileDataStoreClockIterator *iter = store->beginClockIteration();
0045 
0046     QVERIFY(iter->hasNext());
0047     item = iter->next();
0048     QCOMPARE(item, tileDataList[0]);
0049 
0050     QVERIFY(iter->hasNext());
0051     item = iter->next();
0052     QCOMPARE(item, tileDataList[2]);
0053 
0054     QVERIFY(iter->hasNext());
0055     item = iter->next();
0056     QCOMPARE(item, tileDataList[1]);
0057 
0058     QVERIFY(!iter->hasNext());
0059 
0060     store->endIteration(iter);
0061 
0062 
0063     /// Second, iterate until the second item!
0064     iter = store->beginClockIteration();
0065 
0066     QVERIFY(iter->hasNext());
0067     item = iter->next();
0068     QCOMPARE(item, tileDataList[0]);
0069 
0070     store->endIteration(iter);
0071 
0072 
0073     /// Third, check the position restored!
0074     iter = store->beginClockIteration();
0075 
0076     QVERIFY(iter->hasNext());
0077     item = iter->next();
0078     QCOMPARE(item, tileDataList[2]);
0079 
0080     QVERIFY(iter->hasNext());
0081     item = iter->next();
0082     QCOMPARE(item, tileDataList[1]);
0083 
0084     QVERIFY(iter->hasNext());
0085     item = iter->next();
0086     QCOMPARE(item, tileDataList[0]);
0087 
0088     QVERIFY(!iter->hasNext());
0089 
0090     store->endIteration(iter);
0091 
0092 
0093     /// By this moment clock index has been set
0094     /// onto the last item.
0095     /// Let's try remove it and see what will happen...
0096 
0097     store->freeTileData(tileDataList[0]);
0098 
0099     iter = store->beginClockIteration();
0100 
0101     QVERIFY(iter->hasNext());
0102     item = iter->next();
0103     QCOMPARE(item, tileDataList[2]);
0104 
0105     QVERIFY(iter->hasNext());
0106     item = iter->next();
0107     QCOMPARE(item, tileDataList[1]);
0108 
0109     QVERIFY(!iter->hasNext());
0110 
0111     store->endIteration(iter);
0112 
0113     store->freeTileData(tileDataList[2]);
0114     store->freeTileData(tileDataList[1]);
0115 }
0116 
0117 void KisTileDataStoreTest::testLeaks()
0118 {
0119     KisTileDataStore::instance()->debugClear();
0120 
0121     QCOMPARE(KisTileDataStore::instance()->numTiles(), 0);
0122 
0123     const qint32 pixelSize = 1;
0124     quint8 defaultPixel = 128;
0125     KisTiledDataManager *dm = new KisTiledDataManager(pixelSize, &defaultPixel);
0126 
0127     KisTileSP tile = dm->getTile(0, 0, true);
0128     tile->lockForWrite();
0129     tile->unlockForWrite();
0130 
0131     tile = 0;
0132 
0133     delete dm;
0134 
0135     QCOMPARE(KisTileDataStore::instance()->numTiles(), 0);
0136 }
0137 
0138 #define COLUMN2COLOR(col) (col%255)
0139 
0140 void KisTileDataStoreTest::testSwapping()
0141 {
0142     KisImageConfig config(false);
0143     config.setMemoryHardLimitPercent(100.0 / KisImageConfig::totalRAM());
0144     config.setMemorySoftLimitPercent(0);
0145 
0146     KisTileDataStore::instance()->debugClear();
0147 
0148 
0149 
0150     const qint32 pixelSize = 1;
0151     quint8 defaultPixel = 128;
0152     KisTiledDataManager dm(pixelSize, &defaultPixel);
0153 
0154     for(qint32 col = 0; col < 1000; col++) {
0155         KisTileSP tile = dm.getTile(col, 0, true);
0156         tile->lockForWrite();
0157 
0158         KisTileData *td = tile->tileData();
0159         QVERIFY(memoryIsFilled(defaultPixel, td->data(), TILESIZE));
0160 
0161         memset(td->data(), COLUMN2COLOR(col), TILESIZE);
0162         QVERIFY(memoryIsFilled(COLUMN2COLOR(col), td->data(), TILESIZE));
0163 
0164         tile->unlockForWrite();
0165     }
0166 
0167     //KisTileDataStore::instance()->debugSwapAll();
0168 
0169     for(qint32 col = 0; col < 1000; col++) {
0170         KisTileSP tile = dm.getTile(col, 0, true);
0171         tile->lockForRead();
0172 
0173         KisTileData *td = tile->tileData();
0174         QVERIFY(memoryIsFilled(COLUMN2COLOR(col), td->data(), TILESIZE));
0175         tile->unlockForWrite();
0176     }
0177 }
0178 
0179 SIMPLE_TEST_MAIN(KisTileDataStoreTest)
0180