2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "rolling-gif-image-cache.h"
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>
29 #if defined(DEBUG_ENABLED)
30 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
34 std::ostringstream oss; \
35 oss<<"Size:"<<mQueue.Count()<<" [ "; \
36 for(std::size_t _i=0; _i<mQueue.Count(); ++_i) \
39 "={ frm#: " << mQueue[_i].mFrameNumber << \
40 " tex: " << mImageUrls[mQueue[_i].mFrameNumber].mTextureId<<"}, "; \
42 oss<<" ]"<<std::endl; \
43 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"%s",oss.str().c_str()); \
50 const bool ENABLE_ORIENTATION_CORRECTION( true );
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 ),
68 mCacheSize( cacheSize ),
71 mImageUrls.resize( mFrameCount );
75 RollingGifImageCache::~RollingGifImageCache()
77 while( !mQueue.IsEmpty() )
79 ImageFrame imageFrame = mQueue.PopFront();
80 Dali::Toolkit::TextureManager::RemoveTexture( mImageUrls[ imageFrame.mFrameNumber ].mUrl );
84 TextureSet RollingGifImageCache::FirstFrame()
86 return GetFrontTextureSet();
89 TextureSet RollingGifImageCache::NextFrame()
91 TextureSet textureSet;
93 ImageFrame imageFrame = mQueue.PopFront();
94 Dali::Toolkit::TextureManager::RemoveTexture( mImageUrls[ imageFrame.mFrameNumber ].mUrl );
95 mImageUrls[ imageFrame.mFrameNumber ].mTextureId = TextureManager::INVALID_TEXTURE_ID;
99 return GetFrontTextureSet();
102 bool RollingGifImageCache::IsFrontReady() const
104 return ( !mQueue.IsEmpty() );
107 void RollingGifImageCache::LoadBatch()
109 // Try and load up to mBatchSize images, until the cache is filled.
110 // Once the cache is filled, as frames progress, the old frame is
111 // removed, and another frame is loaded
113 std::vector<Dali::PixelData> pixelDataList;
115 // Get the smallest number of frames we need to load
116 int batchSize = std::min( std::size_t(mBatchSize), mCacheSize - mQueue.Count() );
117 DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "RollingGifImageCache::LoadBatch() mFrameIndex:%d batchSize:%d\n", mFrameIndex, batchSize );
119 if( mGifLoading.LoadNextNFrames( mFrameIndex, batchSize, pixelDataList) )
121 unsigned int pixelDataListCount = pixelDataList.size();
123 for( unsigned int i = 0; i < pixelDataListCount && !mQueue.IsFull(); ++i )
125 ImageFrame imageFrame;
127 // create the texture for uploading the pixel data
128 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
129 pixelDataList[i].GetPixelFormat(),
130 pixelDataList[i].GetWidth(),
131 pixelDataList[i].GetHeight() );
133 texture.Upload( pixelDataList[i] );
135 mImageUrls[ mUrlIndex ].mUrl = Dali::Toolkit::TextureManager::AddTexture(texture);
136 imageFrame.mFrameNumber = mUrlIndex;
139 mUrlIndex %= mImageUrls.size();
141 mQueue.PushBack( imageFrame );
143 bool synchronousLoading = false;
144 bool atlasingStatus = false;
145 bool loadingStatus = false;
146 TextureManager::MaskingDataPointer maskInfo = nullptr;
147 AtlasUploadObserver* atlasObserver = nullptr;
148 ImageAtlasManagerPtr imageAtlasManager = nullptr;
151 mTextureManager.LoadTexture(
152 mImageUrls[ imageFrame.mFrameNumber ].mUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL,
153 SamplingMode::BOX_THEN_LINEAR, maskInfo,
154 synchronousLoading, mImageUrls[ imageFrame.mFrameNumber ].mTextureId, textureRect,
155 atlasingStatus, loadingStatus, Dali::WrapMode::Type::DEFAULT,
156 Dali::WrapMode::Type::DEFAULT, NULL,
157 atlasObserver, imageAtlasManager, ENABLE_ORIENTATION_CORRECTION, TextureManager::ReloadPolicy::CACHED );
160 mFrameIndex += batchSize;
161 mFrameIndex %= mFrameCount;
167 TextureSet RollingGifImageCache::GetFrontTextureSet() const
169 DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "RollingGifImageCache::GetFrontTextureSet() FrameNumber:%d\n", mQueue[ 0 ].mFrameNumber );
171 TextureManager::TextureId textureId = GetCachedTextureId( 0 );
172 return mTextureManager.GetTextureSet( textureId );
175 TextureManager::TextureId RollingGifImageCache::GetCachedTextureId( int index ) const
177 return mImageUrls[ mQueue[ index ].mFrameNumber ].mTextureId;
180 } //namespace Internal
181 } //namespace Toolkit