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/image-visual-shader-factory.h>
34 #include <dali-toolkit/internal/visuals/animated-image/fixed-image-cache.h>
35 #include <dali-toolkit/internal/visuals/animated-image/rolling-image-cache.h>
36 #include <dali-toolkit/internal/visuals/animated-image/rolling-gif-image-cache.h>
37 #include <dali-toolkit/devel-api/image-loader/image-atlas.h>
38 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
39 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
53 DALI_ENUM_TO_STRING_TABLE_BEGIN( WRAP_MODE )
54 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, DEFAULT )
55 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, CLAMP_TO_EDGE )
56 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, REPEAT )
57 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, MIRRORED_REPEAT )
58 DALI_ENUM_TO_STRING_TABLE_END( WRAP_MODE )
60 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
61 constexpr auto LOOP_FOREVER = -1;
63 #if defined(DEBUG_ENABLED)
64 Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE");
70 * Multi-image Flow of execution
76 * | cache->LoadBatch()
79 * | PrepareTextureSet()
80 * | cache->FirstFrame()
81 * | CreateRenderer() (Doesn't become ready until first frame loads)
84 * | FrameReady(textureSet)
85 * | start first frame:
88 * | mRenderer.SetTextures(textureSet)
91 * | DisplayNextFrame()
92 * | if front frame is ready,
93 * | mRenderer.SetTextures( front frame's texture )
95 * | mWaitingForTexture=true
96 * | cache->LoadBatch()
98 * | FrameReady(textureSet)
99 * | mRenderer.SetTextures(textureSet)
104 AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
106 AnimatedImageVisualPtr visual( new AnimatedImageVisual( factoryCache, shaderFactory ) );
107 visual->InitializeGif( imageUrl );
108 visual->SetProperties( properties );
110 if( visual->mFrameCount > 0 )
112 visual->LoadFirstBatch();
118 AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Property::Array& imageUrls, const Property::Map& properties )
120 AnimatedImageVisualPtr visual( new AnimatedImageVisual( factoryCache, shaderFactory ) );
121 visual->mImageUrls = new ImageCache::UrlList();
122 visual->mImageUrls->reserve( imageUrls.Count() );
124 for( unsigned int i=0; i < imageUrls.Count(); ++i)
126 ImageCache::UrlStore urlStore;
127 urlStore.mTextureId = TextureManager::INVALID_TEXTURE_ID;
128 urlStore.mUrl = imageUrls[i].Get<std::string>();
129 visual->mImageUrls->push_back( urlStore );
131 visual->mFrameCount = imageUrls.Count();
132 visual->SetProperties( properties );
134 if( visual->mFrameCount > 0 )
136 visual->LoadFirstBatch();
142 AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
144 AnimatedImageVisualPtr visual( new AnimatedImageVisual( factoryCache, shaderFactory ) );
145 visual->InitializeGif( imageUrl );
147 if( visual->mFrameCount > 0 )
149 visual->LoadFirstBatch();
155 void AnimatedImageVisual::InitializeGif( const VisualUrl& imageUrl )
157 mImageUrl = imageUrl;
158 mGifLoading = GifLoading::New( imageUrl.GetUrl(), imageUrl.IsLocalResource() );
159 mFrameCount = mGifLoading->GetImageCount();
160 mGifLoading->LoadFrameDelays( mFrameDelayContainer );
163 AnimatedImageVisual::AnimatedImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory )
164 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ),
167 mImageVisualShaderFactory( shaderFactory ),
168 mPixelArea( FULL_TEXTURE_RECT ),
170 mGifLoading( nullptr ),
171 mCurrentFrameIndex( 0 ),
177 mLoopCount( LOOP_FOREVER ),
178 mCurrentLoopIndex( 0 ),
182 mWrapModeU( WrapMode::DEFAULT ),
183 mWrapModeV( WrapMode::DEFAULT ),
184 mActionStatus( DevelAnimatedImageVisual::Action::PLAY ),
185 mStartFirstFrame(false)
188 AnimatedImageVisual::~AnimatedImageVisual()
194 void AnimatedImageVisual::GetNaturalSize( Vector2& naturalSize )
196 if( mImageSize.GetWidth() == 0 && mImageSize.GetHeight() == 0)
198 if( mImageUrl.IsValid() )
200 mImageSize = mGifLoading->GetImageSize();
202 else if( mImageUrls && mImageUrls->size() > 0 )
204 mImageSize = Dali::GetClosestImageSize( (*mImageUrls)[0].mUrl );
208 naturalSize.width = mImageSize.GetWidth();
209 naturalSize.height = mImageSize.GetHeight();
212 void AnimatedImageVisual::DoCreatePropertyMap( Property::Map& map ) const
216 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::ANIMATED_IMAGE );
218 if( mImageUrl.IsValid() )
220 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
222 if( mImageUrls != NULL && ! mImageUrls->empty() )
224 Property::Array urls;
225 for( unsigned int i=0; i<mImageUrls->size(); ++i)
227 urls.Add( (*mImageUrls)[i].mUrl );
229 Property::Value value( const_cast<Property::Array&>(urls) );
230 map.Insert( Toolkit::ImageVisual::Property::URL, value );
233 map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
234 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
235 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
237 map.Insert( Toolkit::ImageVisual::Property::BATCH_SIZE, static_cast<int>(mBatchSize) );
238 map.Insert( Toolkit::ImageVisual::Property::CACHE_SIZE, static_cast<int>(mCacheSize) );
239 map.Insert( Toolkit::ImageVisual::Property::FRAME_DELAY, static_cast<int>(mFrameDelay) );
240 map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, static_cast<int>(mLoopCount) );
243 void AnimatedImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
248 void AnimatedImageVisual::OnDoAction( const Dali::Property::Index actionId, const Dali::Property::Value& attributes )
250 // Check if action is valid for this visual type and perform action if possible
254 case DevelAnimatedImageVisual::Action::PAUSE:
256 // Pause will be executed on next timer tick
257 mActionStatus = DevelAnimatedImageVisual::Action::PAUSE;
260 case DevelAnimatedImageVisual::Action::PLAY:
262 if( IsOnStage() && mActionStatus != DevelAnimatedImageVisual::Action::PLAY )
264 mFrameDelayTimer.Start();
266 mActionStatus = DevelAnimatedImageVisual::Action::PLAY;
269 case DevelAnimatedImageVisual::Action::STOP:
271 // STOP reset functionality will actually be done in a future change
272 // Stop will be executed on next timer tick
273 mCurrentFrameIndex = 0;
274 mActionStatus = DevelAnimatedImageVisual::Action::STOP;
280 void AnimatedImageVisual::DoSetProperties( const Property::Map& propertyMap )
282 // url[s] already passed in from constructor
284 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
286 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
287 if( keyValue.first.type == Property::Key::INDEX )
289 DoSetProperty( keyValue.first.indexKey, keyValue.second );
293 if( keyValue.first == PIXEL_AREA_UNIFORM_NAME )
295 DoSetProperty( Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second );
297 else if( keyValue.first == IMAGE_WRAP_MODE_U )
299 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second );
301 else if( keyValue.first == IMAGE_WRAP_MODE_V )
303 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second );
305 else if( keyValue.first == BATCH_SIZE_NAME )
307 DoSetProperty( Toolkit::ImageVisual::Property::BATCH_SIZE, keyValue.second );
309 else if( keyValue.first == CACHE_SIZE_NAME )
311 DoSetProperty( Toolkit::ImageVisual::Property::CACHE_SIZE, keyValue.second );
313 else if( keyValue.first == FRAME_DELAY_NAME )
315 DoSetProperty( Toolkit::ImageVisual::Property::FRAME_DELAY, keyValue.second );
317 else if( keyValue.first == LOOP_COUNT_NAME )
319 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
325 void AnimatedImageVisual::DoSetProperty( Property::Index index,
326 const Property::Value& value )
330 case Toolkit::ImageVisual::Property::PIXEL_AREA:
332 value.Get( mPixelArea );
335 case Toolkit::ImageVisual::Property::WRAP_MODE_U:
338 if(Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode ))
340 mWrapModeU = Dali::WrapMode::Type(wrapMode);
344 mWrapModeU = Dali::WrapMode::Type::DEFAULT;
348 case Toolkit::ImageVisual::Property::WRAP_MODE_V:
351 if(Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode ))
353 mWrapModeV = Dali::WrapMode::Type(wrapMode);
357 mWrapModeV = Dali::WrapMode::Type::DEFAULT;
362 case Toolkit::ImageVisual::Property::BATCH_SIZE:
365 if( value.Get( batchSize ) )
367 mBatchSize = batchSize;
372 case Toolkit::ImageVisual::Property::CACHE_SIZE:
375 if( value.Get( cacheSize ) )
377 mCacheSize = cacheSize;
382 case Toolkit::ImageVisual::Property::FRAME_DELAY:
385 if( value.Get( frameDelay ) )
387 mFrameDelay = frameDelay;
392 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
395 if( value.Get( loopCount ) )
397 mLoopCount = loopCount;
404 void AnimatedImageVisual::DoSetOnStage( Actor& actor )
406 mPlacementActor = actor;
407 TextureSet textureSet = PrepareTextureSet();
408 CreateRenderer(); // Always create a renderer when on stage
410 if( textureSet ) // if the image loading is successful
412 StartFirstFrame( textureSet );
416 mStartFirstFrame = true;
420 void AnimatedImageVisual::DoSetOffStage( Actor& actor )
422 DALI_ASSERT_DEBUG( (bool)mImpl->mRenderer && "There should always be a renderer whilst on stage");
424 if( mFrameDelayTimer )
426 mFrameDelayTimer.Stop();
427 mFrameDelayTimer.Reset();
430 actor.RemoveRenderer( mImpl->mRenderer );
431 mImpl->mRenderer.Reset();
432 mPlacementActor.Reset();
435 void AnimatedImageVisual::OnSetTransform()
437 if( mImpl->mRenderer )
439 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
443 void AnimatedImageVisual::CreateRenderer()
445 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
446 bool atlasing = false;
447 Shader shader = mImageVisualShaderFactory.GetShader( mFactoryCache, atlasing, defaultWrapMode );
449 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
451 mImpl->mRenderer = Renderer::New( geometry, shader );
453 // Register transform properties
454 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
456 if( !defaultWrapMode ) // custom wrap mode
458 Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
459 wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
460 mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
463 if( mPixelArea != FULL_TEXTURE_RECT )
465 mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
468 mCurrentFrameIndex = 0;
471 void AnimatedImageVisual::LoadFirstBatch()
473 // Ensure the batch size and cache size are no bigger than the number of URLs,
474 // and that the cache is at least as big as the batch size.
475 uint16_t numUrls = 0;
476 uint16_t batchSize = 1;
477 uint16_t cacheSize = 1;
481 numUrls = mImageUrls->size();
485 numUrls = mFrameCount;
488 batchSize = std::min( mBatchSize, numUrls );
489 cacheSize = std::min( std::max( batchSize, mCacheSize ), numUrls );
491 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::LoadFirstBatch() batchSize:%d cacheSize:%d\n", batchSize, cacheSize);
494 TextureManager& textureManager = mFactoryCache.GetTextureManager();
496 if( mGifLoading != nullptr )
498 mImageCache = new RollingGifImageCache( textureManager, *mGifLoading, mFrameCount, *this, cacheSize, batchSize );
500 else if( mImageUrls )
502 if( batchSize > 0 && cacheSize > 0 )
504 if( cacheSize < numUrls )
506 mImageCache = new RollingImageCache( textureManager, *mImageUrls, *this, cacheSize, batchSize );
510 mImageCache = new FixedImageCache( textureManager, *mImageUrls, *this, batchSize );
515 mImageCache = new RollingImageCache( textureManager, *mImageUrls, *this, 1, 1 );
521 DALI_LOG_ERROR("mImageCache is null");
525 void AnimatedImageVisual::StartFirstFrame( TextureSet& textureSet )
527 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::StartFirstFrame()\n");
529 mStartFirstFrame = false;
532 mImpl->mRenderer.SetTextures( textureSet );
534 Actor actor = mPlacementActor.GetHandle();
537 actor.AddRenderer( mImpl->mRenderer );
538 mPlacementActor.Reset();
541 mCurrentFrameIndex = 0;
543 if( mFrameCount > 1 )
545 int frameDelay = mFrameDelay; // from URL array
546 if( mFrameDelayContainer.Count() > 0 ) // from GIF
548 frameDelay = mFrameDelayContainer[0];
551 mFrameDelayTimer = Timer::New( frameDelay );
552 mFrameDelayTimer.TickSignal().Connect( this, &AnimatedImageVisual::DisplayNextFrame );
553 mFrameDelayTimer.Start();
555 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"ResourceReady(ResourceStatus::READY)\n");
556 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
559 TextureSet AnimatedImageVisual::PrepareTextureSet()
561 TextureSet textureSet;
563 textureSet = mImageCache->FirstFrame();
566 SetImageSize( textureSet );
570 DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"ResourceReady(ResourceStatus::FAILED)\n");
571 ResourceReady( Toolkit::Visual::ResourceStatus::FAILED );
577 void AnimatedImageVisual::SetImageSize( TextureSet& textureSet )
581 Texture texture = textureSet.GetTexture( 0 );
584 mImageSize.SetWidth( texture.GetWidth() );
585 mImageSize.SetHeight( texture.GetHeight() );
590 void AnimatedImageVisual::FrameReady( TextureSet textureSet )
592 SetImageSize( textureSet );
594 if( mStartFirstFrame )
596 StartFirstFrame( textureSet );
602 mImpl->mRenderer.SetTextures( textureSet );
607 bool AnimatedImageVisual::DisplayNextFrame()
609 if( mActionStatus == DevelAnimatedImageVisual::Action::STOP || mActionStatus == DevelAnimatedImageVisual::Action::PAUSE )
613 if( mFrameCount > 1 )
615 // Wrap the frame index
616 ++mCurrentFrameIndex;
618 if( mLoopCount < 0 || mCurrentLoopIndex <= mLoopCount)
620 mCurrentFrameIndex %= mFrameCount;
621 if( mCurrentFrameIndex == 0 )
628 // This will stop timer
632 DALI_LOG_INFO( gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::DisplayNextFrame(this:%p) FrameCount:%d\n", this, mCurrentFrameIndex);
634 if( mFrameDelayContainer.Count() > 0 )
636 unsigned int delay = mFrameDelayContainer[mCurrentFrameIndex];
638 if( mFrameDelayTimer.GetInterval() != delay )
640 mFrameDelayTimer.SetInterval( delay );
644 TextureSet textureSet;
647 textureSet = mImageCache->NextFrame();
650 SetImageSize( textureSet );
651 mImpl->mRenderer.SetTextures( textureSet );
655 // Keep timer ticking
660 } // namespace Internal
662 } // namespace Toolkit