1 /*
2  *  Copyright (c) 2015 Jouni Pentikäinen <joupent@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "kis_animation_frame_cache_test.h"
20 
21 #include <QTest>
22 #include <testutil.h>
23 
24 #include "kis_animation_frame_cache.h"
25 #include "kis_image_animation_interface.h"
26 #include "opengl/kis_opengl_image_textures.h"
27 #include "kis_time_range.h"
28 #include "kis_keyframe_channel.h"
29 #include "kistest.h"
30 
31 #include "kundo2command.h"
32 
verifyRangeIsCachedStatus(KisAnimationFrameCacheSP cache,int start,int end,KisAnimationFrameCache::CacheStatus status)33 void verifyRangeIsCachedStatus(KisAnimationFrameCacheSP cache, int start, int end, KisAnimationFrameCache::CacheStatus status)
34 {
35     for (int t = start; t <= end; t++) {
36         QVERIFY2(
37             cache->frameStatus(t) == status,
38             qPrintable(QString("Expected status %1 for frame %2 in range %3 to %4").arg(status == KisAnimationFrameCache::Cached ? "Cached" : "Uncached").arg(t).arg(start).arg(end))
39         );
40     }
41 }
42 
testCache()43 void KisAnimationFrameCacheTest::testCache()
44 {
45     TestUtil::MaskParent p;
46     KisImageSP image = p.image;
47     KisImageAnimationInterface *animation = image->animationInterface();
48     KisPaintLayerSP layer1 = p.layer;
49     KisPaintLayerSP layer2 = new KisPaintLayer(p.image, "", OPACITY_OPAQUE_U8);
50     KisPaintLayerSP layer3 = new KisPaintLayer(p.image, "", OPACITY_OPAQUE_U8);
51     image->addNode(layer2);
52     image->addNode(layer3);
53 
54     KUndo2Command parentCommand;
55 
56     KisKeyframeChannel *rasterChannel2 = layer2->getKeyframeChannel(KisKeyframeChannel::Content.id(), true);
57 
58     rasterChannel2->addKeyframe(10, &parentCommand);
59     rasterChannel2->addKeyframe(20, &parentCommand);
60     rasterChannel2->addKeyframe(30, &parentCommand);
61 
62     KisKeyframeChannel *rasterChannel3 = layer2->getKeyframeChannel(KisKeyframeChannel::Content.id(), true);
63     rasterChannel3->addKeyframe(17, &parentCommand);
64 
65     KisOpenGLImageTexturesSP glTex = KisOpenGLImageTextures::getImageTextures(image, 0, KoColorConversionTransformation::IntentPerceptual, KoColorConversionTransformation::Empty);
66     KisAnimationFrameCacheSP cache = new KisAnimationFrameCache(glTex);
67     glTex->testingForceInitialized();
68 
69     m_globalAnimationCache = cache.data();
70     connect(animation, SIGNAL(sigFrameReady(int)), this, SLOT(slotFrameGerenationFinished(int)));
71 
72     int t;
73     animation->saveAndResetCurrentTime(11, &t);
74     animation->notifyFrameReady();
75 
76     QCOMPARE(cache->frameStatus(9), KisAnimationFrameCache::Uncached);
77     verifyRangeIsCachedStatus(cache, 10, 16, KisAnimationFrameCache::Cached);
78     QCOMPARE(cache->frameStatus(17), KisAnimationFrameCache::Uncached);
79 
80     animation->saveAndResetCurrentTime(30, &t);
81     animation->notifyFrameReady();
82 
83     QCOMPARE(cache->frameStatus(29), KisAnimationFrameCache::Uncached);
84     verifyRangeIsCachedStatus(cache, 30, 40, KisAnimationFrameCache::Cached);
85     QCOMPARE(cache->frameStatus(9999), KisAnimationFrameCache::Cached);
86 
87     image->invalidateFrames(KisTimeRange::fromTime(10, 12), QRect());
88     verifyRangeIsCachedStatus(cache, 10, 12, KisAnimationFrameCache::Uncached);
89     verifyRangeIsCachedStatus(cache, 13, 16, KisAnimationFrameCache::Cached);
90 
91     image->invalidateFrames(KisTimeRange::fromTime(15, 20), QRect());
92     verifyRangeIsCachedStatus(cache, 13, 14, KisAnimationFrameCache::Cached);
93     verifyRangeIsCachedStatus(cache, 15, 20, KisAnimationFrameCache::Uncached);
94 
95     image->invalidateFrames(KisTimeRange::infinite(100), QRect());
96     verifyRangeIsCachedStatus(cache, 90, 99, KisAnimationFrameCache::Cached);
97     verifyRangeIsCachedStatus(cache, 100, 110, KisAnimationFrameCache::Uncached);
98 
99     image->invalidateFrames(KisTimeRange::fromTime(90, 100), QRect());
100     verifyRangeIsCachedStatus(cache, 80, 89, KisAnimationFrameCache::Cached);
101     verifyRangeIsCachedStatus(cache, 90, 100, KisAnimationFrameCache::Uncached);
102 
103     image->invalidateFrames(KisTimeRange::infinite(14), QRect());
104     QCOMPARE(cache->frameStatus(13), KisAnimationFrameCache::Cached);
105     verifyRangeIsCachedStatus(cache, 15, 100, KisAnimationFrameCache::Uncached);
106 
107 }
108 
slotFrameGerenationFinished(int time)109 void KisAnimationFrameCacheTest::slotFrameGerenationFinished(int time)
110 {
111     KisImageSP image = m_globalAnimationCache->image();
112     KisOpenGLUpdateInfoSP info = m_globalAnimationCache->fetchFrameData(time, image, KisRegion(image->bounds()));
113     m_globalAnimationCache->addConvertedFrameData(info, time);
114 
115 }
116 
117 KISTEST_MAIN(KisAnimationFrameCacheTest)
118