2 * Copyright (c) 2018 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.
19 #include "animated-image-visual.h"
22 #include <dali/devel-api/adaptor-framework/image-loading.h>
23 #include <dali/integration-api/debug.h>
27 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
28 #include <dali-toolkit/public-api/visuals/visual-properties.h>
29 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
30 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
31 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
32 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
33 #include <dali-toolkit/internal/visuals/animated-image/fixed-image-cache.h>
34 #include <dali-toolkit/internal/visuals/animated-image/rolling-image-cache.h>
35 #include <dali-toolkit/internal/visuals/animated-image/rolling-gif-image-cache.h>
36 #include <dali-toolkit/internal/visuals/image/image-visual.h>
37 #include <dali-toolkit/devel-api/image-loader/image-atlas.h>
38 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
52 DALI_ENUM_TO_STRING_TABLE_BEGIN( WRAP_MODE )
53 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, DEFAULT )
54 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, CLAMP_TO_EDGE )
55 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, REPEAT )
56 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, MIRRORED_REPEAT )
57 DALI_ENUM_TO_STRING_TABLE_END( WRAP_MODE )
59 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
60 constexpr auto LOOP_FOREVER = -1;
62 #if defined(DEBUG_ENABLED)
63 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
69 * Multi-image Flow of execution
75 * | cache->LoadBatch()
78 * | PrepareTextureSet()
79 * | cache->FirstFrame()
80 * | CreateRenderer() (Doesn't become ready until first frame loads)
83 * | FrameReady(textureSet)
84 * | start first frame:
87 * | mRenderer.SetTextures(textureSet)
90 * | DisplayNextFrame()
91 * | if front frame is ready,
92 * | mRenderer.SetTextures( front frame's texture )
94 * | mWaitingForTexture=true
95 * | cache->LoadBatch()
97 * | FrameReady(textureSet)
98 * | mRenderer.SetTextures(textureSet)
103 AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties )
105 AnimatedImageVisualPtr visual( new AnimatedImageVisual( factoryCache ) );
106 visual->InitializeGif( imageUrl );
107 visual->SetProperties( properties );
109 if( visual->mFrameCount > 0 )
111 visual->LoadFirstBatch();
117 AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCache, const Property::Array& imageUrls, const Property::Map& properties )
119 AnimatedImageVisualPtr visual( new AnimatedImageVisual( factoryCache ) );
120 visual->mImageUrls = new ImageCache::UrlList();
121 visual->mImageUrls->reserve( imageUrls.Count() );
123 for( unsigned int i=0; i < imageUrls.Count(); ++i)
125 ImageCache::UrlStore urlStore;
126 urlStore.mTextureId = TextureManager::INVALID_TEXTURE_ID;
127 urlStore.mUrl = imageUrls[i].Get<std::string>();
128 visual->mImageUrls->push_back( urlStore );
130 visual->mFrameCount = imageUrls.Count();
131 visual->SetProperties( properties );
133 if( visual->mFrameCount > 0 )
135 visual->LoadFirstBatch();
141 AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl )
143 AnimatedImageVisualPtr visual( new AnimatedImageVisual( factoryCache ) );
144 visual->InitializeGif( imageUrl );
146 if( visual->mFrameCount > 0 )
148 visual->LoadFirstBatch();
154 void AnimatedImageVisual::InitializeGif( const VisualUrl& imageUrl )
156 mImageUrl = imageUrl;
157 mGifLoading = GifLoading::New( imageUrl.GetUrl(), imageUrl.IsLocalResource() );
158 mFrameCount = mGifLoading->GetImageCount();
159 mGifLoading->LoadFrameDelays( mFrameDelayContainer );
162 AnimatedImageVisual::AnimatedImageVisual( VisualFactoryCache& factoryCache )
163 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ),
166 mPixelArea( FULL_TEXTURE_RECT ),
168 mGifLoading( nullptr ),
169 mCurrentFrameIndex( 0 ),
175 mLoopCount( LOOP_FOREVER ),
176 mCurrentLoopIndex( 0 ),
180 mWrapModeU( WrapMode::DEFAULT ),
181 mWrapModeV( WrapMode::DEFAULT ),
182 mActionStatus( DevelAnimatedImageVisual::Action::PLAY ),
183 mStartFirstFrame(false)
186 AnimatedImageVisual::~AnimatedImageVisual()
192 void AnimatedImageVisual::GetNaturalSize( Vector2& naturalSize )
194 if( mImageSize.GetWidth() == 0 && mImageSize.GetHeight() == 0)
196 if( mImageUrl.IsValid() )
198 mImageSize = mGifLoading->GetImageSize();
200 else if( mImageUrls && mImageUrls->size() > 0 )
202 mImageSize = Dali::GetClosestImageSize( (*mImageUrls)[0].mUrl );
206 naturalSize.width = mImageSize.GetWidth();
207 naturalSize.height = mImageSize.GetHeight();
210 void AnimatedImageVisual::DoCreatePropertyMap( Property::Map& map ) const
214 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::ANIMATED_IMAGE );
216 if( mImageUrl.IsValid() )
218 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
220 if( mImageUrls != NULL && ! mImageUrls->empty() )
222 Property::Array urls;
223 for( unsigned int i=0; i<mImageUrls->size(); ++i)
225 urls.Add( (*mImageUrls)[i].mUrl );
227 Property::Value value( const_cast<Property::Array&>(urls) );
228 map.Insert( Toolkit::ImageVisual::Property::URL, value );
231 map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
232 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
233 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
235 map.Insert( Toolkit::ImageVisual::Property::BATCH_SIZE, static_cast<int>(mBatchSize) );
236 map.Insert( Toolkit::ImageVisual::Property::CACHE_SIZE, static_cast<int>(mCacheSize) );
237 map.Insert( Toolkit::ImageVisual::Property::FRAME_DELAY, static_cast<int>(mFrameDelay) );
238 map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, static_cast<int>(mLoopCount) );
241 void AnimatedImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
246 void AnimatedImageVisual::OnDoAction( const Dali::Property::Index actionId, const Dali::Property::Value& attributes )
248 // Check if action is valid for this visual type and perform action if possible
252 case DevelAnimatedImageVisual::Action::PAUSE:
254 // Pause will be executed on next timer tick
255 mActionStatus = DevelAnimatedImageVisual::Action::PAUSE;
258 case DevelAnimatedImageVisual::Action::PLAY:
260 if( IsOnStage() && mActionStatus != DevelAnimatedImageVisual::Action::PLAY )
262 mFrameDelayTimer.Start();
264 mActionStatus = DevelAnimatedImageVisual::Action::PLAY;
267 case DevelAnimatedImageVisual::Action::STOP:
269 // STOP reset functionality will actually be done in a future change
270 // Stop will be executed on next timer tick
271 mCurrentFrameIndex = 0;
272 mActionStatus = DevelAnimatedImageVisual::Action::STOP;
278 void AnimatedImageVisual::DoSetProperties( const Property::Map& propertyMap )
280 // url[s] already passed in from constructor
282 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
284 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
285 if( keyValue.first.type == Property::Key::INDEX )
287 DoSetProperty( keyValue.first.indexKey, keyValue.second );
291 if( keyValue.first == PIXEL_AREA_UNIFORM_NAME )
293 DoSetProperty( Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second );
295 else if( keyValue.first == IMAGE_WRAP_MODE_U )
297 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second );
299 else if( keyValue.first == IMAGE_WRAP_MODE_V )
301 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second );
303 else if( keyValue.first == BATCH_SIZE_NAME )
305 DoSetProperty( Toolkit::ImageVisual::Property::BATCH_SIZE, keyValue.second );
307 else if( keyValue.first == CACHE_SIZE_NAME )
309 DoSetProperty( Toolkit::ImageVisual::Property::CACHE_SIZE, keyValue.second );
311 else if( keyValue.first == FRAME_DELAY_NAME )
313 DoSetProperty( Toolkit::ImageVisual::Property::FRAME_DELAY, keyValue.second );
315 else if( keyValue.first == LOOP_COUNT_NAME )
317 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
323 void AnimatedImageVisual::DoSetProperty( Property::Index index,
324 const Property::Value& value )
328 case Toolkit::ImageVisual::Property::PIXEL_AREA:
330 value.Get( mPixelArea );
333 case Toolkit::ImageVisual::Property::WRAP_MODE_U:
336 if(Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode ))
338 mWrapModeU = Dali::WrapMode::Type(wrapMode);
342 mWrapModeU = Dali::WrapMode::Type::DEFAULT;
346 case Toolkit::ImageVisual::Property::WRAP_MODE_V:
349 if(Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode ))
351 mWrapModeV = Dali::WrapMode::Type(wrapMode);
355 mWrapModeV = Dali::WrapMode::Type::DEFAULT;
360 case Toolkit::ImageVisual::Property::BATCH_SIZE:
363 if( value.Get( batchSize ) )
365 mBatchSize = batchSize;
370 case Toolkit::ImageVisual::Property::CACHE_SIZE:
373 if( value.Get( cacheSize ) )
375 mCacheSize = cacheSize;
380 case Toolkit::ImageVisual::Property::FRAME_DELAY:
383 if( value.Get( frameDelay ) )
385 mFrameDelay = frameDelay;
390 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
393 if( value.Get( loopCount ) )
395 mLoopCount = loopCount;
402 void AnimatedImageVisual::DoSetOnStage( Actor& actor )
404 mPlacementActor = actor;
405 TextureSet textureSet = PrepareTextureSet();
406 CreateRenderer(); // Always create a renderer when on stage
408 if( textureSet ) // if the image loading is successful
410 StartFirstFrame( textureSet );
414 mStartFirstFrame = true;
418 void AnimatedImageVisual::DoSetOffStage( Actor& actor )
420 DALI_ASSERT_DEBUG( (bool)mImpl->mRenderer && "There should always be a renderer whilst on stage");
422 if( mFrameDelayTimer )
424 mFrameDelayTimer.Stop();
425 mFrameDelayTimer.Reset();
428 actor.RemoveRenderer( mImpl->mRenderer );
429 mImpl->mRenderer.Reset();
430 mPlacementActor.Reset();
433 void AnimatedImageVisual::OnSetTransform()
435 if( mImpl->mRenderer )
437 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
441 void AnimatedImageVisual::CreateRenderer()
443 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
444 bool atlasing = false;
445 Shader shader = ImageVisual::GetImageShader( mFactoryCache, atlasing, defaultWrapMode );
447 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
449 mImpl->mRenderer = Renderer::New( geometry, shader );
451 // Register transform properties
452 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
454 if( !defaultWrapMode ) // custom wrap mode
456 Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
457 wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
458 mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
461 if( mPixelArea != FULL_TEXTURE_RECT )
463 mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
466 mCurrentFrameIndex = 0;
469 void AnimatedImageVisual::LoadFirstBatch()
471 // Ensure the batch size and cache size are no bigger than the number of URLs,
472 // and that the cache is at least as big as the batch size.
473 uint16_t numUrls = 0;
474 uint16_t batchSize = 1;
475 uint16_t cacheSize = 1;
479 numUrls = mImageUrls->size();
483 numUrls = mFrameCount;
486 batchSize = std::min( mBatchSize, numUrls );
487 cacheSize = std::min( std::max( batchSize, mCacheSize ), numUrls );
489 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::LoadFirstBatch() batchSize:%d cacheSize:%d\n", batchSize, cacheSize);
492 TextureManager& textureManager = mFactoryCache.GetTextureManager();
494 if( mGifLoading != nullptr )
496 mImageCache = new RollingGifImageCache( textureManager, *mGifLoading, mFrameCount, *this, cacheSize, batchSize );
498 else if( mImageUrls )
500 if( batchSize > 0 && cacheSize > 0 )
502 if( cacheSize < numUrls )
504 mImageCache = new RollingImageCache( textureManager, *mImageUrls, *this, cacheSize, batchSize );
508 mImageCache = new FixedImageCache( textureManager, *mImageUrls, *this, batchSize );
513 mImageCache = new RollingImageCache( textureManager, *mImageUrls, *this, 1, 1 );
519 DALI_LOG_ERROR("mImageCache is null");
523 void AnimatedImageVisual::StartFirstFrame( TextureSet& textureSet )
525 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::StartFirstFrame()\n");
527 mStartFirstFrame = false;
528 mImpl->mRenderer.SetTextures( textureSet );
529 Actor actor = mPlacementActor.GetHandle();
532 actor.AddRenderer( mImpl->mRenderer );
533 mPlacementActor.Reset();
536 mCurrentFrameIndex = 0;
538 if( mFrameCount > 1 )
540 int frameDelay = mFrameDelay; // from URL array
541 if( mFrameDelayContainer.Count() > 0 ) // from GIF
543 frameDelay = mFrameDelayContainer[0];
546 mFrameDelayTimer = Timer::New( frameDelay );
547 mFrameDelayTimer.TickSignal().Connect( this, &AnimatedImageVisual::DisplayNextFrame );
548 mFrameDelayTimer.Start();
550 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"ResourceReady(ResourceStatus::READY)\n");
551 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
554 TextureSet AnimatedImageVisual::PrepareTextureSet()
556 TextureSet textureSet;
558 textureSet = mImageCache->FirstFrame();
561 SetImageSize( textureSet );
565 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"ResourceReady(ResourceStatus::FAILED)\n");
566 ResourceReady( Toolkit::Visual::ResourceStatus::FAILED );
572 void AnimatedImageVisual::SetImageSize( TextureSet& textureSet )
576 Texture texture = textureSet.GetTexture( 0 );
579 mImageSize.SetWidth( texture.GetWidth() );
580 mImageSize.SetHeight( texture.GetHeight() );
585 void AnimatedImageVisual::FrameReady( TextureSet textureSet )
587 SetImageSize( textureSet );
589 if( mStartFirstFrame )
591 StartFirstFrame( textureSet );
595 mImpl->mRenderer.SetTextures( textureSet );
599 bool AnimatedImageVisual::DisplayNextFrame()
601 if( mActionStatus == DevelAnimatedImageVisual::Action::STOP || mActionStatus == DevelAnimatedImageVisual::Action::PAUSE )
605 if( mFrameCount > 1 )
607 // Wrap the frame index
608 ++mCurrentFrameIndex;
610 if( mLoopCount < 0 || mCurrentLoopIndex <= mLoopCount)
612 mCurrentFrameIndex %= mFrameCount;
613 if( mCurrentFrameIndex == 0 )
620 // This will stop timer
624 DALI_LOG_INFO( gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::DisplayNextFrame(this:%p) FrameCount:%d\n", this, mCurrentFrameIndex);
626 if( mFrameDelayContainer.Count() > 0 )
628 unsigned int delay = mFrameDelayContainer[mCurrentFrameIndex];
630 if( mFrameDelayTimer.GetInterval() != delay )
632 mFrameDelayTimer.SetInterval( delay );
636 TextureSet textureSet;
639 textureSet = mImageCache->NextFrame();
642 SetImageSize( textureSet );
643 mImpl->mRenderer.SetTextures( textureSet );
647 // Keep timer ticking
652 } // namespace Internal
654 } // namespace Toolkit