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