48540765c257d2f455b7d417dc102d708e29099b
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / rolling-gif-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 "rolling-gif-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 RollingGifImageCache::RollingGifImageCache(
62   TextureManager& textureManager, GifLoading& gifLoading, uint32_t frameCount, ImageCache::FrameReadyObserver& observer,
63   uint16_t cacheSize, uint16_t batchSize )
64 : ImageCache( textureManager, observer, batchSize ),
65   mGifLoading( gifLoading ),
66   mFrameCount( frameCount ),
67   mFrameIndex( 0 ),
68   mCacheSize( cacheSize ),
69   mQueue( cacheSize )
70 {
71   mImageUrls.resize( mFrameCount );
72   LoadBatch();
73 }
74
75 RollingGifImageCache::~RollingGifImageCache()
76 {
77   if( mTextureManagerAlive )
78   {
79     while( !mQueue.IsEmpty() )
80     {
81       ImageFrame imageFrame = mQueue.PopFront();
82       Dali::Toolkit::TextureManager::RemoveTexture( mImageUrls[ imageFrame.mFrameNumber ].mUrl );
83     }
84   }
85 }
86
87
88 TextureSet RollingGifImageCache::FirstFrame()
89 {
90   return GetFrontTextureSet();
91 }
92
93 TextureSet RollingGifImageCache::NextFrame()
94 {
95   TextureSet textureSet;
96
97   ImageFrame imageFrame = mQueue.PopFront();
98   Dali::Toolkit::TextureManager::RemoveTexture( mImageUrls[ imageFrame.mFrameNumber ].mUrl );
99   mImageUrls[ imageFrame.mFrameNumber ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
100
101   LoadBatch();
102
103   return GetFrontTextureSet();
104 }
105
106 bool RollingGifImageCache::IsFrontReady() const
107 {
108   return ( !mQueue.IsEmpty() );
109 }
110
111 void RollingGifImageCache::LoadBatch()
112 {
113   // Try and load up to mBatchSize images, until the cache is filled.
114   // Once the cache is filled, as frames progress, the old frame is
115   // removed, and another frame is loaded
116
117   std::vector<Dali::PixelData> pixelDataList;
118
119   // Get the smallest number of frames we need to load
120   int batchSize = std::min( std::size_t(mBatchSize), mCacheSize - mQueue.Count() );
121   DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "RollingGifImageCache::LoadBatch() mFrameIndex:%d  batchSize:%d\n", mFrameIndex, batchSize );
122
123   if( mGifLoading.LoadNextNFrames(  mFrameIndex, batchSize, pixelDataList) )
124   {
125     unsigned int pixelDataListCount = pixelDataList.size();
126
127     for( unsigned int i = 0; i < pixelDataListCount && !mQueue.IsFull(); ++i )
128     {
129       ImageFrame imageFrame;
130
131       // create the texture for uploading the pixel data
132       Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
133                                       pixelDataList[i].GetPixelFormat(),
134                                       pixelDataList[i].GetWidth(),
135                                       pixelDataList[i].GetHeight() );
136
137       texture.Upload( pixelDataList[i] );
138
139       mImageUrls[ mUrlIndex ].mUrl = Dali::Toolkit::TextureManager::AddTexture(texture);
140       imageFrame.mFrameNumber = mUrlIndex;
141
142       ++mUrlIndex;
143       mUrlIndex %= mImageUrls.size();
144
145       mQueue.PushBack( imageFrame );
146
147       bool synchronousLoading = false;
148       bool atlasingStatus = false;
149       bool loadingStatus = false;
150       TextureManager::MaskingDataPointer maskInfo = nullptr;
151       AtlasUploadObserver* atlasObserver = nullptr;
152       ImageAtlasManagerPtr imageAtlasManager = nullptr;
153       Vector4 textureRect;
154       Dali::ImageDimensions textureRectSize;
155       auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
156
157       mTextureManager.LoadTexture(
158         mImageUrls[ imageFrame.mFrameNumber ].mUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL,
159         SamplingMode::BOX_THEN_LINEAR, maskInfo,
160         synchronousLoading, mImageUrls[ imageFrame.mFrameNumber ].mTextureId, textureRect, textureRectSize,
161         atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
162         Dali::WrapMode::Type::DEFAULT, NULL,
163         atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED, preMultiply );
164     }
165
166     mFrameIndex += batchSize;
167     mFrameIndex %= mFrameCount;
168   }
169
170   LOG_CACHE;
171 }
172
173 TextureSet RollingGifImageCache::GetFrontTextureSet() const
174 {
175   DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "RollingGifImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[ 0 ].mFrameNumber );
176
177   TextureManager::TextureId textureId = GetCachedTextureId( 0 );
178   return mTextureManager.GetTextureSet( textureId );
179 }
180
181 TextureManager::TextureId RollingGifImageCache::GetCachedTextureId( int index ) const
182 {
183   return mImageUrls[ mQueue[ index ].mFrameNumber ].mTextureId;
184 }
185
186 } //namespace Internal
187 } //namespace Toolkit
188 } //namespace Dali