Support weak handle for BaseHandle
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / rolling-animated-image-cache.cpp
1 /*
2  * Copyright (c) 2020 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 "rolling-animated-image-cache.h"
19
20 // EXTERNAL HEADERS
21
22 // INTERNAL HEADERS
23 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
24 #include <dali-toolkit/internal/visuals/image-atlas-manager.h> // For ImageAtlasManagerPtr
25 #include <dali/integration-api/debug.h>
26
27 namespace
28 {
29 #if defined(DEBUG_ENABLED)
30 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
31
32 #define LOG_CACHE                                                       \
33   {                                                                     \
34     std::ostringstream oss;                                             \
35     oss<<"Size:"<<mQueue.Count()<<" [ ";                                \
36     for(std::size_t _i=0; _i<mQueue.Count(); ++_i)                      \
37     {                                                                   \
38       oss<<_i<<                                                         \
39         "={ frm#: " << mQueue[_i].mFrameNumber <<                        \
40            " tex: " << mImageUrls[mQueue[_i].mFrameNumber].mTextureId<<"}, ";  \
41     }                                                                   \
42     oss<<" ]"<<std::endl;                                               \
43     DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"%s",oss.str().c_str()); \
44   }
45
46 #else
47   #define LOG_CACHE
48 #endif
49
50 const bool ENABLE_ORIENTATION_CORRECTION( true );
51
52 }
53
54 namespace Dali
55 {
56 namespace Toolkit
57 {
58 namespace Internal
59 {
60
61 RollingAnimatedImageCache::RollingAnimatedImageCache(
62   TextureManager& textureManager, AnimatedImageLoading& animatedImageLoading, uint32_t frameCount, ImageCache::FrameReadyObserver& observer,
63   uint16_t cacheSize, uint16_t batchSize )
64 : ImageCache( textureManager, observer, batchSize ),
65   mAnimatedImageLoading( animatedImageLoading ),
66   mFrameCount( frameCount ),
67   mFrameIndex( 0 ),
68   mCacheSize( cacheSize ),
69   mQueue( cacheSize )
70 {
71   mImageUrls.resize( mFrameCount );
72   LoadBatch();
73 }
74
75 RollingAnimatedImageCache::~RollingAnimatedImageCache()
76 {
77   if( mTextureManagerAlive )
78   {
79     while( IsFrontReady() )
80     {
81       ImageFrame imageFrame = mQueue.PopFront();
82       Dali::Toolkit::TextureManager::RemoveTexture( mImageUrls[ imageFrame.mFrameNumber ].mUrl );
83     }
84   }
85 }
86
87 TextureSet RollingAnimatedImageCache::Frame( uint32_t frameIndex )
88 {
89   bool popExist = false;
90   while( IsFrontReady() && mQueue.Front().mFrameNumber != frameIndex )
91   {
92     ImageFrame imageFrame = mQueue.PopFront();
93     Dali::Toolkit::TextureManager::RemoveTexture( mImageUrls[ imageFrame.mFrameNumber ].mUrl );
94     mImageUrls[ imageFrame.mFrameNumber ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
95     popExist = true;
96   }
97   if( popExist || mImageUrls[ frameIndex ].mTextureId == TextureManager::INVALID_TEXTURE_ID )
98   {
99     // If the frame of frameIndex was already loaded, load batch from the last frame of queue
100     if( IsFrontReady() )
101     {
102       mFrameIndex = ( mQueue.Back().mFrameNumber + 1 ) % mFrameCount;
103     }
104     // If the queue is empty, load batch from the frame of frameIndex
105     else
106     {
107       mFrameIndex = frameIndex;
108     }
109     LoadBatch();
110   }
111
112   return GetFrontTextureSet();
113 }
114
115 TextureSet RollingAnimatedImageCache::FirstFrame()
116 {
117   return Frame( 0u );
118 }
119
120 uint32_t RollingAnimatedImageCache::GetFrameInterval( uint32_t frameIndex )
121 {
122   Frame( frameIndex );
123   return mAnimatedImageLoading.GetFrameInterval( frameIndex );
124 }
125
126 bool RollingAnimatedImageCache::IsFrontReady() const
127 {
128   return ( !mQueue.IsEmpty() );
129 }
130
131 void RollingAnimatedImageCache::LoadBatch()
132 {
133   // Try and load up to mBatchSize images, until the cache is filled.
134   // Once the cache is filled, as frames progress, the old frame is
135   // removed, and another frame is loaded
136
137   std::vector<Dali::PixelData> pixelDataList;
138
139   // Get the smallest number of frames we need to load
140   int batchSize = std::min( std::size_t(mBatchSize), mCacheSize - mQueue.Count() );
141   DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::LoadBatch() mFrameIndex:%d  batchSize:%d\n", mFrameIndex, batchSize );
142   if( mAnimatedImageLoading.LoadNextNFrames( mFrameIndex, batchSize, pixelDataList) )
143   {
144     unsigned int pixelDataListCount = pixelDataList.size();
145
146     for( unsigned int i = 0; i < pixelDataListCount && !mQueue.IsFull(); ++i )
147     {
148       ImageFrame imageFrame;
149
150       // create the texture for uploading the pixel data
151       Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
152                                       pixelDataList[i].GetPixelFormat(),
153                                       pixelDataList[i].GetWidth(),
154                                       pixelDataList[i].GetHeight() );
155
156       texture.Upload( pixelDataList[i] );
157
158       mImageUrls[ mUrlIndex ].mUrl = Dali::Toolkit::TextureManager::AddTexture(texture);
159       imageFrame.mFrameNumber = mUrlIndex;
160
161       ++mUrlIndex;
162       mUrlIndex %= mImageUrls.size();
163
164       mQueue.PushBack( imageFrame );
165
166       bool synchronousLoading = false;
167       bool atlasingStatus = false;
168       bool loadingStatus = false;
169       TextureManager::MaskingDataPointer maskInfo = nullptr;
170       AtlasUploadObserver* atlasObserver = nullptr;
171       ImageAtlasManagerPtr imageAtlasManager = nullptr;
172       Vector4 textureRect;
173       Dali::ImageDimensions textureRectSize;
174       auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
175
176       mTextureManager.LoadTexture(
177         mImageUrls[ imageFrame.mFrameNumber ].mUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL,
178         SamplingMode::BOX_THEN_LINEAR, maskInfo,
179         synchronousLoading, mImageUrls[ imageFrame.mFrameNumber ].mTextureId, textureRect, textureRectSize,
180         atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
181         Dali::WrapMode::Type::DEFAULT, NULL,
182         atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply );
183     }
184
185     mFrameIndex += batchSize;
186     mFrameIndex %= mFrameCount;
187   }
188
189   LOG_CACHE;
190 }
191
192 TextureSet RollingAnimatedImageCache::GetFrontTextureSet() const
193 {
194   DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "RollingAnimatedImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[ 0 ].mFrameNumber );
195
196   TextureManager::TextureId textureId = GetCachedTextureId( 0 );
197   return mTextureManager.GetTextureSet( textureId );
198 }
199
200 TextureManager::TextureId RollingAnimatedImageCache::GetCachedTextureId( int index ) const
201 {
202   return mImageUrls[ mQueue[ index ].mFrameNumber ].mTextureId;
203 }
204
205 } //namespace Internal
206 } //namespace Toolkit
207 } //namespace Dali