Merge "Revert "Added tag for capture privilege"" into devel/master
[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 // INTERNAL HEADERS
21 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
22
23 namespace Dali
24 {
25 namespace Toolkit
26 {
27 namespace Internal
28 {
29
30 namespace
31 {
32 const bool ENABLE_ORIENTATION_CORRECTION( true );
33 } // namespace
34
35 FixedImageCache::FixedImageCache(
36   TextureManager& textureManager, UrlList& urlList, ImageCache::FrameReadyObserver& observer,
37   unsigned int batchSize )
38 : ImageCache( textureManager, observer, batchSize ),
39   mImageUrls( urlList ),
40   mFront(0u)
41 {
42   mReadyFlags.reserve( mImageUrls.size() );
43   LoadBatch();
44 }
45
46 FixedImageCache::~FixedImageCache()
47 {
48   if( mTextureManagerAlive )
49   {
50     for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
51     {
52       mTextureManager.Remove( mImageUrls[i].mTextureId );
53     }
54   }
55 }
56
57 TextureSet FixedImageCache::FirstFrame()
58 {
59   TextureSet textureSet = GetFrontTextureSet();
60
61   if( ! textureSet )
62   {
63     mWaitingForReadyFrame = true;
64   }
65
66   return textureSet;
67 }
68
69 TextureSet FixedImageCache::NextFrame()
70 {
71   TextureSet textureSet;
72   ++mFront;
73   mFront %= mImageUrls.size();
74
75   if( IsFrontReady() == true )
76   {
77     textureSet = GetFrontTextureSet();
78   }
79   else
80   {
81     mWaitingForReadyFrame = true;
82   }
83
84   LoadBatch();
85
86   return textureSet;
87 }
88
89 bool FixedImageCache::IsFrontReady() const
90 {
91   return ( mReadyFlags.size() > 0 && mReadyFlags[mFront] == true );
92 }
93
94 void FixedImageCache::LoadBatch()
95 {
96   // Try and load up to mBatchSize images, until the cache is filled.
97   // Once the cache is filled, mUrlIndex exceeds mImageUrls size and
98   // no more images are loaded.
99   bool frontFrameReady = IsFrontReady();;
100
101   for( unsigned int i=0; i< mBatchSize && mUrlIndex < mImageUrls.size(); ++i )
102   {
103     std::string& url = mImageUrls[ mUrlIndex ].mUrl;
104
105     mReadyFlags.push_back(false);
106
107     // Note, if the image is already loaded, then UploadComplete will get called
108     // from within this method. This means it won't yet have a texture id, so we
109     // need to account for this inside the UploadComplete method using mRequestingLoad.
110     mRequestingLoad = true;
111
112     bool synchronousLoading = false;
113     bool atlasingStatus = false;
114     bool loadingStatus = false;
115     TextureManager::MaskingDataPointer maskInfo = nullptr;
116     AtlasUploadObserver* atlasObserver = nullptr;
117     ImageAtlasManagerPtr imageAtlasManager = nullptr;
118     Vector4 textureRect;
119     auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
120
121     mTextureManager.LoadTexture(
122       url, ImageDimensions(), FittingMode::SCALE_TO_FILL,
123       SamplingMode::BOX_THEN_LINEAR, maskInfo,
124       synchronousLoading, mImageUrls[ mUrlIndex ].mTextureId, textureRect,
125       atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
126       Dali::WrapMode::Type::DEFAULT, this,
127       atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED,
128       preMultiply );
129
130     if( loadingStatus == false )  // not loading, means it's already ready.
131     {
132       SetImageFrameReady( mImageUrls[ mUrlIndex ].mTextureId );
133     }
134     mRequestingLoad = false;
135     ++mUrlIndex;
136   }
137
138   CheckFrontFrame( frontFrameReady );
139 }
140
141 void FixedImageCache::SetImageFrameReady( TextureManager::TextureId textureId )
142 {
143   for( std::size_t i = 0; i < mImageUrls.size() ; ++i )
144   {
145     if( mImageUrls[i].mTextureId == textureId )
146     {
147       mReadyFlags[i] = true;
148       break;
149     }
150   }
151 }
152
153 TextureSet FixedImageCache::GetFrontTextureSet() const
154 {
155   return mTextureManager.GetTextureSet( mImageUrls[mFront].mTextureId );
156 }
157
158 void FixedImageCache::CheckFrontFrame( bool wasReady )
159 {
160   if( mWaitingForReadyFrame && wasReady == false && IsFrontReady() )
161   {
162     mWaitingForReadyFrame = false;
163     mObserver.FrameReady( GetFrontTextureSet() );
164   }
165 }
166
167 void FixedImageCache::UploadComplete(
168   bool           loadSuccess,
169   int32_t        textureId,
170   TextureSet     textureSet,
171   bool           useAtlasing,
172   const Vector4& atlasRect,
173   bool           preMultiplied)
174 {
175   bool frontFrameReady = IsFrontReady();
176
177   if( ! mRequestingLoad )
178   {
179     SetImageFrameReady( textureId );
180
181     CheckFrontFrame( frontFrameReady );
182   }
183   else
184   {
185     // UploadComplete has been called from within RequestLoad. TextureManager must
186     // therefore already have the texture cached, so make the texture ready.
187     // (Use the last texture, as the texture id hasn't been assigned yet)
188     mReadyFlags.back() = true;
189   }
190 }
191
192 } //namespace Internal
193 } //namespace Toolkit
194 } //namespace Dali