File indexing completed on 2024-12-22 04:12:54
0001 /* 0002 * SPDX-FileCopyrightText: 2018 Dmitry Kazakov <dimula73@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 #include "KisFrameCacheStoreTest.h" 0007 0008 #include <simpletest.h> 0009 #include <testutil.h> 0010 0011 #include <KoColor.h> 0012 #include "KisAsyncAnimationRendererBase.h" 0013 #include "kis_image_animation_interface.h" 0014 #include "opengl/KisOpenGLUpdateInfoBuilder.h" 0015 0016 #include "KoColorSpaceRegistry.h" 0017 #include "KoColorSpace.h" 0018 #include "KisLockFrameGenerationLock.h" 0019 0020 // TODO: conversion options into a separate file! 0021 #include "kis_update_info.h" 0022 0023 #include "opengl/kis_texture_tile_update_info.h" 0024 0025 0026 #include "KisFrameCacheStore.h" 0027 0028 static const int maxTileSize = 256; 0029 0030 bool compareTextureTileUpdateInfo(KisTextureTileUpdateInfoSP tile1, KisTextureTileUpdateInfoSP tile2) 0031 { 0032 KIS_COMPARE_RF(tile1->patchLevelOfDetail(), tile2->patchLevelOfDetail()); 0033 KIS_COMPARE_RF(tile1->realPatchOffset(), tile2->realPatchOffset()); 0034 KIS_COMPARE_RF(tile1->realPatchRect(), tile2->realPatchRect()); 0035 KIS_COMPARE_RF(tile1->realTileSize(), tile2->realTileSize()); 0036 KIS_COMPARE_RF(tile1->isTopmost(), tile2->isTopmost()); 0037 KIS_COMPARE_RF(tile1->isLeftmost(), tile2->isLeftmost()); 0038 KIS_COMPARE_RF(tile1->isRightmost(), tile2->isRightmost()); 0039 KIS_COMPARE_RF(tile1->isBottommost(), tile2->isBottommost()); 0040 KIS_COMPARE_RF(tile1->isEntireTileUpdated(), tile2->isEntireTileUpdated()); 0041 0042 KIS_COMPARE_RF(tile1->tileCol(), tile2->tileCol()); 0043 KIS_COMPARE_RF(tile1->tileRow(), tile2->tileRow()); 0044 KIS_COMPARE_RF(tile1->pixelSize(), tile2->pixelSize()); 0045 KIS_COMPARE_RF(tile1->valid(), tile2->valid()); 0046 0047 KIS_COMPARE_RF(tile1->patchPixelsLength(), tile2->patchPixelsLength()); 0048 0049 0050 const uint numRealPixelBytes = static_cast<uint>(tile1->realPatchRect().width() * tile1->realPatchRect().height() * tile1->pixelSize()); 0051 0052 if (memcmp(tile1->data(), tile2->data(), numRealPixelBytes) != 0) { 0053 qWarning() << "Tile pixels differ!"; 0054 qWarning() << " " << ppVar(tile1->tileCol()) << ppVar(tile1->tileRow()); 0055 qWarning() << " " << ppVar(numRealPixelBytes); 0056 0057 quint8 *src = tile1->data(); 0058 quint8 *dst = tile2->data(); 0059 0060 for (uint i = 0; i < numRealPixelBytes; i++) { 0061 if (*src != *dst) { 0062 qDebug() << " " << ppVar(i) << ppVar(*src) << ppVar(*dst); 0063 } 0064 0065 src++; 0066 dst++; 0067 } 0068 0069 return false; 0070 } 0071 0072 return true; 0073 } 0074 0075 0076 bool compareUpdateInfo(KisOpenGLUpdateInfoSP info1, KisOpenGLUpdateInfoSP info2) 0077 { 0078 KIS_COMPARE_RF(info1->dirtyImageRect(), info2->dirtyImageRect()); 0079 KIS_COMPARE_RF(info1->levelOfDetail(), info2->levelOfDetail()); 0080 KIS_COMPARE_RF(info1->tileList.size(), info2->tileList.size()); 0081 0082 for (int i = 0; i < info1->tileList.size(); i++) { 0083 if (!compareTextureTileUpdateInfo(info1->tileList[i], info2->tileList[i])) { 0084 return false; 0085 } 0086 } 0087 0088 return true; 0089 } 0090 0091 0092 class TestFramesRenderer : public KisAsyncAnimationRendererBase 0093 { 0094 Q_OBJECT 0095 0096 public: 0097 TestFramesRenderer() 0098 : m_pool(m_poolRegistry.getPool(maxTileSize, maxTileSize)) 0099 { 0100 m_updateInfoBuilder.setTextureInfoPool(m_pool); 0101 0102 const KoColorSpace *dstColorSpace = KoColorSpaceRegistry::instance()->rgb8(); 0103 m_updateInfoBuilder.setConversionOptions( 0104 ConversionOptions(dstColorSpace, 0105 KoColorConversionTransformation::internalRenderingIntent(), 0106 KoColorConversionTransformation::internalConversionFlags())); 0107 0108 // TODO: refactor setting texture size in raw values! 0109 m_updateInfoBuilder.setTextureBorder(8); 0110 m_updateInfoBuilder.setEffectiveTextureSize(QSize(256 - 16, 256 - 16)); 0111 0112 connect(this, SIGNAL(sigCompleteRegenerationInternal(int)), SLOT(notifyFrameCompleted(int))); 0113 connect(this, SIGNAL(sigCancelRegenerationInternal(int, CancelReason)), SLOT(notifyFrameCancelled(int, CancelReason))); 0114 } 0115 0116 void frameCompletedCallback(int frame, const KisRegion &requestedRegion) override { 0117 KisImageSP image = requestedImage(); 0118 KIS_SAFE_ASSERT_RECOVER_NOOP(frame == image->animationInterface()->currentTime()); 0119 0120 // by default we request update for the entire image 0121 KIS_SAFE_ASSERT_RECOVER_NOOP(requestedRegion == image->bounds()); 0122 0123 KisOpenGLUpdateInfoSP info = m_updateInfoBuilder.buildUpdateInfo(image->bounds(), image, true); 0124 0125 KIS_ASSERT_RECOVER_NOOP(info); 0126 qDebug() << ppVar(info->tileList.size()); 0127 0128 KisOpenGLUpdateInfoSP infoForSave = m_updateInfoBuilder.buildUpdateInfo(image->bounds(), image, true); 0129 m_store.saveFrame(11, infoForSave, image->bounds()); 0130 0131 KIS_SAFE_ASSERT_RECOVER_NOOP(m_store.hasFrame(11)); 0132 0133 KisOpenGLUpdateInfoSP loadedInfo = m_store.loadFrame(11, m_updateInfoBuilder); 0134 0135 qDebug() << ppVar(loadedInfo->tileList.size()); 0136 0137 KIS_SAFE_ASSERT_RECOVER_NOOP(compareUpdateInfo(info, loadedInfo)); 0138 0139 0140 emit sigCompleteRegenerationInternal(frame); 0141 } 0142 0143 void frameCancelledCallback(int frame, CancelReason cancelReason) override { 0144 emit sigCancelRegenerationInternal(frame, cancelReason); 0145 } 0146 0147 Q_SIGNALS: 0148 void sigCompleteRegenerationInternal(int frame); 0149 void sigCancelRegenerationInternal(int frame, CancelReason cancelReason); 0150 0151 private: 0152 KisOpenGLUpdateInfoBuilder m_updateInfoBuilder; 0153 KisTextureTileInfoPoolRegistry m_poolRegistry; 0154 KisTextureTileInfoPoolSP m_pool; 0155 KisFrameCacheStore m_store; 0156 }; 0157 0158 0159 0160 0161 void KisFrameCacheStoreTest::test() 0162 { 0163 QRect refRect(QRect(0,0,512,512)); 0164 TestUtil::MaskParent p(refRect); 0165 const KoColor fillColor(Qt::red, p.image->colorSpace()); 0166 0167 KisPaintLayerSP layer1 = p.layer; 0168 layer1->paintDevice()->fill(QRect(100,100,300,300), fillColor); 0169 0170 KisLockFrameGenerationLock lock(p.image->animationInterface()); 0171 TestFramesRenderer renderer; 0172 renderer.startFrameRegeneration(p.image, 10, TestFramesRenderer::None, std::move(lock)); 0173 0174 0175 p.image->waitForDone(); 0176 0177 while (renderer.isActive()) { 0178 QTest::qWait(500); 0179 } 0180 0181 } 0182 0183 SIMPLE_TEST_MAIN(KisFrameCacheStoreTest) 0184 0185 #include "KisFrameCacheStoreTest.moc"