X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Fanimated-image%2Fanimated-image-visual.cpp;h=acaede834fa0a6ce42198866b758465d925277f5;hp=217994e822268b5d6180f9b678ecf87ef4a464cd;hb=e0c063be9e7ecde0e5665079289489d456828abf;hpb=ddef399f249128bef52cbb79eb321b0bfdaa0ced diff --git a/dali-toolkit/internal/visuals/animated-image/animated-image-visual.cpp b/dali-toolkit/internal/visuals/animated-image/animated-image-visual.cpp index 217994e..da7fb09 100644 --- a/dali-toolkit/internal/visuals/animated-image/animated-image-visual.cpp +++ b/dali-toolkit/internal/visuals/animated-image/animated-image-visual.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * Copyright (c) 2021 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,235 +19,827 @@ #include "animated-image-visual.h" // EXTERNAL INCLUDES -#include +#include +#include +#include // INTERNAL INCLUDES -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include #include +#include #include -#include -#include -#include +#include +#include namespace Dali { - namespace Toolkit { - namespace Internal { - namespace { +// stop behavior +DALI_ENUM_TO_STRING_TABLE_BEGIN(STOP_BEHAVIOR) + DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::DevelImageVisual::StopBehavior, CURRENT_FRAME) + DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::DevelImageVisual::StopBehavior, FIRST_FRAME) + DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::DevelImageVisual::StopBehavior, LAST_FRAME) +DALI_ENUM_TO_STRING_TABLE_END(STOP_BEHAVIOR) + // wrap modes -DALI_ENUM_TO_STRING_TABLE_BEGIN( WRAP_MODE ) -DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, DEFAULT ) -DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, CLAMP_TO_EDGE ) -DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, REPEAT ) -DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, MIRRORED_REPEAT ) -DALI_ENUM_TO_STRING_TABLE_END( WRAP_MODE ) +DALI_ENUM_TO_STRING_TABLE_BEGIN(WRAP_MODE) + DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, DEFAULT) + DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, CLAMP_TO_EDGE) + DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, REPEAT) + DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, MIRRORED_REPEAT) +DALI_ENUM_TO_STRING_TABLE_END(WRAP_MODE) + +const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f); +constexpr auto LOOP_FOREVER = -1; + +#if defined(DEBUG_ENABLED) +Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ANIMATED_IMAGE"); +#endif +} // namespace + +/** + * Multi-image Flow of execution + * + * | New + * | DoSetProperties() + * | LoadFirstBatch() + * | new cache + * | cache->LoadBatch() + * | + * | DoSetOnScene() + * | PrepareTextureSet() + * | cache->FirstFrame() + * | CreateRenderer() (Doesn't become ready until first frame loads) + * | StartFirstFrame() + * | + * | FrameReady(textureSet) + * | start first frame: + * | actor.AddRenderer + * | start timer + * | mRenderer.SetTextures(textureSet) + * | + * | Timer ticks + * | DisplayNextFrame() + * | if front frame is ready, + * | mRenderer.SetTextures( front frame's texture ) + * | else + * | mWaitingForTexture=true + * | cache->LoadBatch() + * | + * | FrameReady(textureSet) + * | mRenderer.SetTextures(textureSet) + * V + * Time + */ -const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f); +AnimatedImageVisualPtr AnimatedImageVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties) +{ + AnimatedImageVisualPtr visual(new AnimatedImageVisual(factoryCache, shaderFactory)); + visual->InitializeAnimatedImage(imageUrl); + visual->SetProperties(properties); + if(visual->mFrameCount > 0) + { + visual->LoadFirstBatch(); + } + + visual->Initialize(); + + return visual; } -AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties ) +AnimatedImageVisualPtr AnimatedImageVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Property::Array& imageUrls, const Property::Map& properties) { - AnimatedImageVisual* visual = new AnimatedImageVisual( factoryCache ); - visual->mImageUrl = imageUrl; - visual->SetProperties( properties ); + AnimatedImageVisualPtr visual(new AnimatedImageVisual(factoryCache, shaderFactory)); + visual->mImageUrls = new ImageCache::UrlList(); + visual->mImageUrls->reserve(imageUrls.Count()); + + for(unsigned int i = 0; i < imageUrls.Count(); ++i) + { + ImageCache::UrlStore urlStore; + urlStore.mTextureId = TextureManager::INVALID_TEXTURE_ID; + urlStore.mUrl = imageUrls[i].Get(); + visual->mImageUrls->push_back(urlStore); + } + visual->mFrameCount = imageUrls.Count(); + visual->SetProperties(properties); + + if(visual->mFrameCount > 0) + { + visual->LoadFirstBatch(); + } + + visual->Initialize(); return visual; } -AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl ) +AnimatedImageVisualPtr AnimatedImageVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl) { - AnimatedImageVisual* visual = new AnimatedImageVisual( factoryCache ); - visual->mImageUrl = imageUrl; + AnimatedImageVisualPtr visual(new AnimatedImageVisual(factoryCache, shaderFactory)); + visual->InitializeAnimatedImage(imageUrl); + + if(visual->mFrameCount > 0) + { + visual->LoadFirstBatch(); + } + + visual->Initialize(); return visual; } -AnimatedImageVisual::AnimatedImageVisual( VisualFactoryCache& factoryCache ) -: Visual::Base( factoryCache ), +void AnimatedImageVisual::InitializeAnimatedImage(const VisualUrl& imageUrl) +{ + mImageUrl = imageUrl; + mAnimatedImageLoading = AnimatedImageLoading::New(imageUrl.GetUrl(), imageUrl.IsLocalResource()); + mFrameCount = mAnimatedImageLoading.GetImageCount(); +} + +AnimatedImageVisual::AnimatedImageVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory) +: Visual::Base(factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::ANIMATED_IMAGE), mFrameDelayTimer(), - mPixelArea( FULL_TEXTURE_RECT ), + mPlacementActor(), + mImageVisualShaderFactory(shaderFactory), + mPixelArea(FULL_TEXTURE_RECT), mImageUrl(), + mAnimatedImageLoading(), + mFrameIndexForJumpTo(0), + mImageUrls(NULL), + mImageCache(NULL), + mCacheSize(2), + mBatchSize(2), + mFrameDelay(100), + mLoopCount(LOOP_FOREVER), + mCurrentLoopIndex(0), + mUrlIndex(0), + mFrameCount(0), mImageSize(), - mCurrentFrameIndex( 0 ), - mWrapModeU( WrapMode::DEFAULT ), - mWrapModeV( WrapMode::DEFAULT ) -{} + mActionStatus(DevelAnimatedImageVisual::Action::PLAY), + mWrapModeU(WrapMode::DEFAULT), + mWrapModeV(WrapMode::DEFAULT), + mStopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME), + mStartFirstFrame(false), + mIsJumpTo(false) +{ +} AnimatedImageVisual::~AnimatedImageVisual() { + delete mImageCache; + delete mImageUrls; } -void AnimatedImageVisual::GetNaturalSize( Vector2& naturalSize ) +void AnimatedImageVisual::GetNaturalSize(Vector2& naturalSize) { - if( mImageSize.GetWidth() == 0 && mImageSize.GetHeight() == 0) + if(mImageSize.GetWidth() == 0 && mImageSize.GetHeight() == 0) { - mImageSize = Dali::GetGifImageSize( mImageUrl.GetUrl() ); + if(mImageUrl.IsValid()) + { + mImageSize = mAnimatedImageLoading.GetImageSize(); + } + else if(mImageUrls && mImageUrls->size() > 0) + { + mImageSize = Dali::GetClosestImageSize((*mImageUrls)[0].mUrl); + } } - naturalSize.width = mImageSize.GetWidth(); + naturalSize.width = mImageSize.GetWidth(); naturalSize.height = mImageSize.GetHeight(); } -void AnimatedImageVisual::DoCreatePropertyMap( Property::Map& map ) const +void AnimatedImageVisual::DoCreatePropertyMap(Property::Map& map) const { map.Clear(); - map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_IMAGE ); + bool sync = IsSynchronousLoadingRequired(); + map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, sync); + + map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::ANIMATED_IMAGE); - if( mImageUrl.IsValid() ) + if(mImageUrl.IsValid()) + { + map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl()); + } + if(mImageUrls != NULL && !mImageUrls->empty()) { - map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() ); + Property::Array urls; + for(unsigned int i = 0; i < mImageUrls->size(); ++i) + { + urls.Add((*mImageUrls)[i].mUrl); + } + Property::Value value(const_cast(urls)); + map.Insert(Toolkit::ImageVisual::Property::URL, value); } - map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea ); - map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU ); - map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV ); + map.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea); + map.Insert(Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU); + map.Insert(Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV); + + map.Insert(Toolkit::ImageVisual::Property::BATCH_SIZE, static_cast(mBatchSize)); + map.Insert(Toolkit::ImageVisual::Property::CACHE_SIZE, static_cast(mCacheSize)); + map.Insert(Toolkit::ImageVisual::Property::FRAME_DELAY, static_cast(mFrameDelay)); + map.Insert(Toolkit::DevelImageVisual::Property::LOOP_COUNT, static_cast(mLoopCount)); + map.Insert(Toolkit::DevelImageVisual::Property::CURRENT_FRAME_NUMBER, (mImageCache) ? static_cast(mImageCache->GetCurrentFrameIndex()) : -1); + map.Insert(Toolkit::DevelImageVisual::Property::TOTAL_FRAME_NUMBER, (mImageCache) ? static_cast(mImageCache->GetTotalFrameCount()) : -1); + + map.Insert(Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, mStopBehavior); } -void AnimatedImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const +void AnimatedImageVisual::DoCreateInstancePropertyMap(Property::Map& map) const { // Do nothing } -void AnimatedImageVisual::DoSetProperties( const Property::Map& propertyMap ) +void AnimatedImageVisual::OnDoAction(const Dali::Property::Index actionId, const Dali::Property::Value& attributes) { - // url already passed in from constructor - - Property::Value* pixelAreaValue = propertyMap.Find( Toolkit::ImageVisual::Property::PIXEL_AREA, PIXEL_AREA_UNIFORM_NAME ); - if( pixelAreaValue ) + // Make not set any action when the resource status is already failed. + if(mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED) { - pixelAreaValue->Get( mPixelArea ); + return; } - Property::Value* wrapModeValueU = propertyMap.Find( Toolkit::ImageVisual::Property::WRAP_MODE_U, IMAGE_WRAP_MODE_U ); - if( wrapModeValueU ) + // Check if action is valid for this visual type and perform action if possible + switch(actionId) { - int value; - Scripting::GetEnumerationProperty( *wrapModeValueU, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, value ); - mWrapModeU = Dali::WrapMode::Type( value ); + case DevelAnimatedImageVisual::Action::PAUSE: + { + // Pause will be executed on next timer tick + mActionStatus = DevelAnimatedImageVisual::Action::PAUSE; + break; + } + case DevelAnimatedImageVisual::Action::PLAY: + { + if(mFrameDelayTimer && IsOnScene() && mActionStatus != DevelAnimatedImageVisual::Action::PLAY) + { + mFrameDelayTimer.Start(); + } + mActionStatus = DevelAnimatedImageVisual::Action::PLAY; + break; + } + case DevelAnimatedImageVisual::Action::STOP: + { + // STOP reset functionality will actually be done in a future change + // Stop will be executed on next timer tick + mActionStatus = DevelAnimatedImageVisual::Action::STOP; + if(IsOnScene()) + { + DisplayNextFrame(); + } + break; + } + case DevelAnimatedImageVisual::Action::JUMP_TO: + { + int32_t frameNumber; + if(attributes.Get(frameNumber)) + { + if(frameNumber < 0 || frameNumber >= static_cast(mFrameCount)) + { + DALI_LOG_ERROR("Invalid frame index used.\n"); + } + else + { + mIsJumpTo = true; + mFrameIndexForJumpTo = frameNumber; + if(IsOnScene()) + { + DisplayNextFrame(); + } + } + } + break; + } } +} - Property::Value* wrapModeValueV = propertyMap.Find( Toolkit::ImageVisual::Property::WRAP_MODE_V, IMAGE_WRAP_MODE_V ); - if( wrapModeValueV ) +void AnimatedImageVisual::DoSetProperties(const Property::Map& propertyMap) +{ + // url[s] already passed in from constructor + for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter) { - int value; - Scripting::GetEnumerationProperty( *wrapModeValueV, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, value ); - mWrapModeV = Dali::WrapMode::Type( value ); + KeyValuePair keyValue = propertyMap.GetKeyValue(iter); + if(keyValue.first.type == Property::Key::INDEX) + { + DoSetProperty(keyValue.first.indexKey, keyValue.second); + } + else + { + if(keyValue.first == PIXEL_AREA_UNIFORM_NAME) + { + DoSetProperty(Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second); + } + else if(keyValue.first == IMAGE_WRAP_MODE_U) + { + DoSetProperty(Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second); + } + else if(keyValue.first == IMAGE_WRAP_MODE_V) + { + DoSetProperty(Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second); + } + else if(keyValue.first == BATCH_SIZE_NAME) + { + DoSetProperty(Toolkit::ImageVisual::Property::BATCH_SIZE, keyValue.second); + } + else if(keyValue.first == CACHE_SIZE_NAME) + { + DoSetProperty(Toolkit::ImageVisual::Property::CACHE_SIZE, keyValue.second); + } + else if(keyValue.first == FRAME_DELAY_NAME) + { + DoSetProperty(Toolkit::ImageVisual::Property::FRAME_DELAY, keyValue.second); + } + else if(keyValue.first == LOOP_COUNT_NAME) + { + DoSetProperty(Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second); + } + else if(keyValue.first == STOP_BEHAVIOR_NAME) + { + DoSetProperty(Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, keyValue.second); + } + } } } -void AnimatedImageVisual::DoSetOnStage( Actor& actor ) +void AnimatedImageVisual::DoSetProperty(Property::Index index, + const Property::Value& value) { - Texture texture = PrepareAnimatedImage(); - if( texture ) // if the image loading is successful + switch(index) { - bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE; - Shader shader = ImageVisual::GetImageShader( mFactoryCache, true, defaultWrapMode ); - - Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY ); - - TextureSet textureSet = TextureSet::New(); - textureSet.SetTexture( 0u, texture ); + case Toolkit::ImageVisual::Property::PIXEL_AREA: + { + value.Get(mPixelArea); + break; + } + case Toolkit::ImageVisual::Property::WRAP_MODE_U: + { + int wrapMode = 0; + if(Scripting::GetEnumerationProperty(value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode)) + { + mWrapModeU = Dali::WrapMode::Type(wrapMode); + } + else + { + mWrapModeU = Dali::WrapMode::Type::DEFAULT; + } + break; + } + case Toolkit::ImageVisual::Property::WRAP_MODE_V: + { + int wrapMode = 0; + if(Scripting::GetEnumerationProperty(value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode)) + { + mWrapModeV = Dali::WrapMode::Type(wrapMode); + } + else + { + mWrapModeV = Dali::WrapMode::Type::DEFAULT; + } + break; + } - mImpl->mRenderer = Renderer::New( geometry, shader ); - mImpl->mRenderer.SetTextures( textureSet ); + case Toolkit::ImageVisual::Property::BATCH_SIZE: + { + int batchSize; + if(value.Get(batchSize)) + { + if(batchSize < 2) + { + DALI_LOG_ERROR("The minimum value of batch size is 2."); + } + else + { + mBatchSize = batchSize; + } + } + break; + } - // Register transform properties - mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT ); + case Toolkit::ImageVisual::Property::CACHE_SIZE: + { + int cacheSize; + if(value.Get(cacheSize)) + { + if(cacheSize < 2) + { + DALI_LOG_ERROR("The minimum value of cache size is 2."); + } + else + { + mCacheSize = cacheSize; + } + } + break; + } - if( !defaultWrapMode ) // custom wrap mode + case Toolkit::ImageVisual::Property::FRAME_DELAY: { - Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE); - wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) ); - mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode ); + int frameDelay; + if(value.Get(frameDelay)) + { + mFrameDelay = frameDelay; + } + break; } - if( mPixelArea != FULL_TEXTURE_RECT ) + case Toolkit::DevelImageVisual::Property::LOOP_COUNT: { - mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea ); + int loopCount; + if(value.Get(loopCount)) + { + mLoopCount = loopCount; + } + break; } - mCurrentFrameIndex = 0; - mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mTextureRectContainer[mCurrentFrameIndex] ); - if( mFrameDelayContainer.Count() > 1 ) + case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR: { - mFrameDelayTimer = Timer::New( mFrameDelayContainer[0] ); - mFrameDelayTimer.TickSignal().Connect( this, &AnimatedImageVisual::DisplayNextFrame ); - mFrameDelayTimer.Start(); + int32_t stopBehavior = mStopBehavior; + if(Scripting::GetEnumerationProperty(value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior)) + { + mStopBehavior = DevelImageVisual::StopBehavior::Type(stopBehavior); + } + break; } - actor.AddRenderer( mImpl->mRenderer ); + case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING: + { + bool sync = false; + value.Get(sync); + if(sync) + { + mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING; + } + else + { + mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING; + } + break; + } } } -void AnimatedImageVisual::DoSetOffStage( Actor& actor ) +void AnimatedImageVisual::DoSetOnScene(Actor& actor) { - if( !mImpl->mRenderer ) + mPlacementActor = actor; + TextureSet textureSet = PrepareTextureSet(); + + // Loading animated image file is failed. + if(!mImageCache || + (mAnimatedImageLoading && !mAnimatedImageLoading.HasLoadingSucceeded())) { - return; + textureSet = SetLoadingFailed(); } - if( mFrameDelayTimer ) + if(textureSet) // if the image loading is successful + { + StartFirstFrame(textureSet); + } + else + { + mStartFirstFrame = true; + } +} + +void AnimatedImageVisual::DoSetOffScene(Actor& actor) +{ + DALI_ASSERT_DEBUG((bool)mImpl->mRenderer && "There should always be a renderer whilst on stage"); + + if(mFrameDelayTimer) { mFrameDelayTimer.Stop(); mFrameDelayTimer.Reset(); } - mTextureRectContainer.Clear(); - mFrameDelayContainer.Clear(); - - actor.RemoveRenderer( mImpl->mRenderer ); - mImpl->mRenderer.Reset(); + actor.RemoveRenderer(mImpl->mRenderer); + mPlacementActor.Reset(); + mStartFirstFrame = false; } void AnimatedImageVisual::OnSetTransform() { - if( mImpl->mRenderer ) + if(mImpl->mRenderer) { - mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT ); + mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT); } } -Texture AnimatedImageVisual::PrepareAnimatedImage() +void AnimatedImageVisual::UpdateShader() { - // load from image file - std::vector pixelDataList; + if(mImpl->mRenderer) + { + Shader shader = GenerateShader(); + mImpl->mRenderer.SetShader(shader); + } +} + +void AnimatedImageVisual::OnInitialize() +{ + bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE; + Shader shader = GenerateShader(); + + Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY); + + mImpl->mRenderer = Renderer::New(geometry, shader); - if( mImageUrl.IsLocal() ) + // Register transform properties + mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT); + + if(!defaultWrapMode) // custom wrap mode + { + Vector2 wrapMode(mWrapModeU - WrapMode::CLAMP_TO_EDGE, mWrapModeV - WrapMode::CLAMP_TO_EDGE); + wrapMode.Clamp(Vector2::ZERO, Vector2(2.f, 2.f)); + mImpl->mRenderer.RegisterProperty(WRAP_MODE_UNIFORM_NAME, wrapMode); + } + + if(mPixelArea != FULL_TEXTURE_RECT) { - if( Dali::LoadAnimatedGifFromFile( mImageUrl.GetUrl().c_str() , pixelDataList, mFrameDelayContainer ) ) + mImpl->mRenderer.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, mPixelArea); + } +} + +void AnimatedImageVisual::LoadFirstBatch() +{ + // Ensure the batch size and cache size are no bigger than the number of URLs, + // and that the cache is at least as big as the batch size. + uint16_t numUrls = 0; + uint16_t batchSize = 1; + uint16_t cacheSize = 1; + + if(mImageUrls) + { + numUrls = mImageUrls->size(); + } + else + { + numUrls = mFrameCount; + } + + batchSize = std::min(mBatchSize, numUrls); + cacheSize = std::min(std::max(batchSize, mCacheSize), numUrls); + + DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::LoadFirstBatch() batchSize:%d cacheSize:%d\n", batchSize, cacheSize); + + mUrlIndex = 0; + TextureManager& textureManager = mFactoryCache.GetTextureManager(); + + if(mAnimatedImageLoading) + { + mImageCache = new RollingAnimatedImageCache(textureManager, mAnimatedImageLoading, mFrameCount, *this, cacheSize, batchSize, IsSynchronousLoadingRequired()); + } + else if(mImageUrls) + { + if(batchSize > 0 && cacheSize > 0) + { + if(cacheSize < numUrls) + { + mImageCache = new RollingImageCache(textureManager, *mImageUrls, *this, cacheSize, batchSize); + } + else + { + mImageCache = new FixedImageCache(textureManager, *mImageUrls, *this, batchSize); + } + } + else + { + mImageCache = new RollingImageCache(textureManager, *mImageUrls, *this, 1, 1); + } + } + + if(!mImageCache) + { + DALI_LOG_ERROR("mImageCache is null\n"); + } +} + +void AnimatedImageVisual::StartFirstFrame(TextureSet& textureSet) +{ + DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::StartFirstFrame()\n"); + + mStartFirstFrame = false; + if(mImpl->mRenderer) + { + mImpl->mRenderer.SetTextures(textureSet); + + Actor actor = mPlacementActor.GetHandle(); + if(actor) { - mImageSize.SetWidth( pixelDataList[0].GetWidth() ); - mImageSize.SetHeight( pixelDataList[0].GetHeight() ); + actor.AddRenderer(mImpl->mRenderer); + mPlacementActor.Reset(); + } + } - return Toolkit::ImageAtlas::PackToAtlas( pixelDataList, mTextureRectContainer ); + if(mFrameCount > 1) + { + int frameDelay = mImageCache->GetFrameInterval(0); + if(frameDelay == 0u) + { + frameDelay = mFrameDelay; // from URL array + } + mFrameDelayTimer = Timer::New(frameDelay); + mFrameDelayTimer.TickSignal().Connect(this, &AnimatedImageVisual::DisplayNextFrame); + mFrameDelayTimer.Start(); + } + + if(mImpl->mResourceStatus != Toolkit::Visual::ResourceStatus::FAILED) + { + DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "ResourceReady(ResourceStatus::READY)\n"); + ResourceReady(Toolkit::Visual::ResourceStatus::READY); + } +} + +TextureSet AnimatedImageVisual::PrepareTextureSet() +{ + TextureSet textureSet; + if(mImageCache) + { + textureSet = mImageCache->FirstFrame(); + } + + if(textureSet) + { + SetImageSize(textureSet); + } + + return textureSet; +} + +void AnimatedImageVisual::SetImageSize(TextureSet& textureSet) +{ + if(textureSet) + { + Texture texture = textureSet.GetTexture(0); + if(texture) + { + mImageSize.SetWidth(texture.GetWidth()); + mImageSize.SetHeight(texture.GetHeight()); } } +} + +void AnimatedImageVisual::FrameReady(TextureSet textureSet) +{ + // When image visual requested to load new frame to mImageCache and it is failed. + if(!textureSet) + { + textureSet = SetLoadingFailed(); + } + + SetImageSize(textureSet); - return Texture(); + if(mStartFirstFrame) + { + StartFirstFrame(textureSet); + } + else + { + if(mImpl->mRenderer) + { + mImpl->mRenderer.SetTextures(textureSet); + } + } } bool AnimatedImageVisual::DisplayNextFrame() { - mCurrentFrameIndex = (mCurrentFrameIndex+1) % mFrameDelayContainer.Count(); - mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mTextureRectContainer[mCurrentFrameIndex] ); - if( mFrameDelayTimer.GetInterval() != mFrameDelayContainer[mCurrentFrameIndex] ) + TextureSet textureSet; + bool continueTimer = false; + + if(mImageCache) + { + bool nextFrame = false; + uint32_t frameIndex = mImageCache->GetCurrentFrameIndex(); + + if(mIsJumpTo) + { + mIsJumpTo = false; + frameIndex = mFrameIndexForJumpTo; + } + else if(mActionStatus == DevelAnimatedImageVisual::Action::PAUSE) + { + return false; + } + else if(mActionStatus == DevelAnimatedImageVisual::Action::STOP) + { + frameIndex = 0; + if(mStopBehavior == DevelImageVisual::StopBehavior::FIRST_FRAME) + { + frameIndex = 0; + } + else if(mStopBehavior == DevelImageVisual::StopBehavior::LAST_FRAME) + { + frameIndex = mFrameCount - 1; + } + else + { + return false; // Do not draw already rendered scene twice. + } + } + else + { + if(mFrameCount > 1) + { + nextFrame = true; + frameIndex++; + if(frameIndex >= mFrameCount) + { + frameIndex %= mFrameCount; + ++mCurrentLoopIndex; + } + + if(mLoopCount >= 0 && mCurrentLoopIndex >= mLoopCount) + { + // This will stop timer + mActionStatus = DevelAnimatedImageVisual::Action::STOP; + return DisplayNextFrame(); + } + } + + unsigned int delay = mImageCache->GetFrameInterval(frameIndex); + if(delay > 0u) + { + if(mFrameDelayTimer.GetInterval() != delay) + { + mFrameDelayTimer.SetInterval(delay); + } + } + } + + DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "AnimatedImageVisual::DisplayNextFrame(this:%p) CurrentFrameIndex:%d\n", this, frameIndex); + + if(nextFrame) + { + textureSet = mImageCache->NextFrame(); + } + else + { + textureSet = mImageCache->Frame(frameIndex); + } + + continueTimer = (mActionStatus == DevelAnimatedImageVisual::Action::PLAY) ? true : false; + } + + if(textureSet) + { + SetImageSize(textureSet); + if(mImpl->mRenderer) + { + mImpl->mRenderer.SetTextures(textureSet); + } + } + + return continueTimer; +} + +TextureSet AnimatedImageVisual::SetLoadingFailed() +{ + DALI_LOG_INFO(gAnimImgLogFilter, Debug::Concise, "ResourceReady(ResourceStatus::FAILED)\n"); + ResourceReady(Toolkit::Visual::ResourceStatus::FAILED); + + Actor actor = mPlacementActor.GetHandle(); + Vector2 imageSize = Vector2::ZERO; + if(actor) + { + imageSize = actor.GetProperty(Actor::Property::SIZE).Get(); + } + mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize); + TextureSet textureSet = mImpl->mRenderer.GetTextures(); + + if(mFrameDelayTimer) { - mFrameDelayTimer.SetInterval( mFrameDelayContainer[mCurrentFrameIndex] ); + mFrameDelayTimer.Stop(); + mFrameDelayTimer.Reset(); } - return true; + SetImageSize(textureSet); + + return textureSet; } +Shader AnimatedImageVisual::GenerateShader() const +{ + bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE; + Shader shader; + shader = mImageVisualShaderFactory.GetShader( + mFactoryCache, + ImageVisualShaderFeature::FeatureBuilder() + .ApplyDefaultTextureWrapMode(defaultWrapMode) + .EnableRoundedCorner(IsRoundedCornerRequired()) + .EnableBorderline(IsBorderlineRequired()) + ); + return shader; +} } // namespace Internal