Adding support for multiple images in AnimatedImageVisual
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / fixed-image-cache.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // CLASS HEADER
18 #include <dali-toolkit/internal/visuals/animated-image/fixed-image-cache.h>
19
20 namespace Dali
21 {
22 namespace Toolkit
23 {
24 namespace Internal
25 {
26
27 FixedImageCache::FixedImageCache(
28   TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
29   unsigned int batchSize )
30 : ImageCache( textureManager, urlList, observer, batchSize ),
31   mFront(0u)
32 {
33   mReadyFlags.reserve( mImageUrls.size() );
34   LoadBatch();
35 }
36
37 FixedImageCache::~FixedImageCache()
38 {
39   for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
40   {
41     mTextureManager.Remove( mImageUrls[i].mTextureId );
42   }
43 }
44
45 TextureSet FixedImageCache::FirstFrame()
46 {
47   TextureSet textureSet = GetFrontTextureSet();
48
49   if( ! textureSet )
50   {
51     mWaitingForReadyFrame = true;
52   }
53
54   return textureSet;
55 }
56
57 TextureSet FixedImageCache::NextFrame()
58 {
59   TextureSet textureSet;
60   ++mFront;
61   mFront %= mImageUrls.size();
62
63   if( IsFrontReady() == true )
64   {
65     textureSet = GetFrontTextureSet();
66   }
67   else
68   {
69     mWaitingForReadyFrame = true;
70   }
71
72   LoadBatch();
73
74   return textureSet;
75 }
76
77 bool FixedImageCache::IsFrontReady() const
78 {
79   return ( mReadyFlags.size() > 0 && mReadyFlags[mFront] == true );
80 }
81
82 void FixedImageCache::LoadBatch()
83 {
84   // Try and load up to mBatchSize images, until the cache is filled.
85   // Once the cache is filled, mUrlIndex exceeds mImageUrls size and
86   // no more images are loaded.
87   bool frontFrameReady = IsFrontReady();;
88
89   for( unsigned int i=0; i< mBatchSize && mUrlIndex < mImageUrls.size(); ++i )
90   {
91     std::string& url = mImageUrls[ mUrlIndex ].mUrl;
92
93     mReadyFlags.push_back(false);
94
95     // Note, if the image is already loaded, then UploadComplete will get called
96     // from within this method. This means it won't yet have a texture id, so we
97     // need to account for this inside the UploadComplete method using mRequestingLoad.
98     mRequestingLoad = true;
99
100     mImageUrls[ mUrlIndex ].mTextureId =
101       mTextureManager.RequestLoad( url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
102                                    SamplingMode::BOX_THEN_LINEAR, TextureManager::NO_ATLAS,
103                                    this );
104     mRequestingLoad = false;
105     ++mUrlIndex;
106   }
107
108   CheckFrontFrame( frontFrameReady );
109 }
110
111 void FixedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
112 {
113   for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
114   {
115     if( mImageUrls[i].mTextureId == textureId )
116     {
117       mReadyFlags[i] = true;
118       break;
119     }
120   }
121 }
122
123 TextureSet FixedImageCache::GetFrontTextureSet() const
124 {
125   return mTextureManager.GetTextureSet( mImageUrls[mFront].mTextureId );
126 }
127
128 void FixedImageCache::CheckFrontFrame( bool wasReady )
129 {
130   if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
131   {
132     mWaitingForReadyFrame = false;
133     mObserver.FrameReady( GetFrontTextureSet() );
134   }
135 }
136
137 void FixedImageCache::UploadComplete(
138   bool           loadSuccess,
139   int32_t        textureId,
140   TextureSet     textureSet,
141   bool           useAtlasing,
142   const Vector4& atlasRect )
143 {
144   bool frontFrameReady = IsFrontReady();
145
146   if( ! mRequestingLoad )
147   {
148     SetImageFrameReady( textureId );
149
150     CheckFrontFrame( frontFrameReady );
151   }
152   else
153   {
154     // UploadComplete has been called from within RequestLoad. TextureManager must
155     // therefore already have the texture cached, so make the texture ready.
156     // (Use the last texture, as the texture id hasn't been assigned yet)
157     mReadyFlags.back() = true;
158   }
159 }
160
161 } //namespace Internal
162 } //namespace Toolkit
163 } //namespace Dali