2 * Copyright (c) 2021 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 <dali/devel-api/adaptor-framework/vector-animation-renderer.h>
19 #include <dali/public-api/object/base-object.h>
20 #include <toolkit-application.h>
21 #include <toolkit-vector-animation-renderer.h>
22 #include <toolkit-event-thread-callback.h>
38 Dali::Internal::Adaptor::VectorAnimationRenderer* gVectorAnimationRenderer = nullptr;
41 class VectorAnimationRenderer: public Dali::BaseObject
45 VectorAnimationRenderer()
50 mTotalFrameNumber(VECTOR_ANIMATION_TOTAL_FRAME_NUMBER),
55 mNeedDroppedFrames(false),
56 mEventThreadCallback( new EventThreadCallback( MakeCallback( this, &VectorAnimationRenderer::OnTriggered ) ) )
66 ~VectorAnimationRenderer()
71 bool Load(const std::string& url)
74 if(mUrl == "invalid.json" || mUrl == "invalid.riv")
78 else if(mUrl == "framedrop.json")
80 // Change total frame number for test
81 mTotalFrameNumber = 200;
86 void SetRenderer( Dali::Renderer renderer )
90 if( mWidth != 0 && mHeight != 0 )
92 Dali::TextureSet textureSet = mRenderer.GetTextures();
93 Dali::Texture texture = Dali::Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight );
94 textureSet.SetTexture( 0, texture );
95 mUploadCompletedSignal.Emit();
99 void SetSize( uint32_t width, uint32_t height )
106 Dali::TextureSet textureSet = mRenderer.GetTextures();
107 Dali::Texture texture = Dali::Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight );
108 textureSet.SetTexture( 0, texture );
109 mUploadCompletedSignal.Emit();
113 bool Render( uint32_t frameNumber )
117 std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int32_t>(mDelayTime)));
119 mNeedDroppedFrames = true;
121 else if(mNeedDroppedFrames)
123 mDroppedFrames = (frameNumber > mPreviousFrame) ? frameNumber - mPreviousFrame - 1: frameNumber + (mTotalFrameNumber - mPreviousFrame) - 1;
125 mNeedDroppedFrames = false;
130 mEventThreadCallback->Trigger();
131 mNeedTrigger = false;
134 if( frameNumber == 1 && mPreviousFrame != frameNumber )
136 mPreviousFrame = frameNumber;
137 // For test corverage
140 mPreviousFrame = frameNumber;
144 uint32_t GetTotalFrameNumber() const
146 return mTotalFrameNumber;
149 float GetFrameRate() const
154 void GetDefaultSize( uint32_t& width, uint32_t& height ) const
160 bool GetMarkerInfo( const std::string& marker, uint32_t& startFrame, uint32_t& endFrame ) const
162 if( marker.compare( VECTOR_ANIMATION_MARKER_NAME_1 ) == 0 )
164 startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_1;
165 endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_1;
167 else if( marker.compare( VECTOR_ANIMATION_MARKER_NAME_2 ) == 0 )
169 startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_2;
170 endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_2;
179 Dali::VectorAnimationRenderer::UploadCompletedSignalType& UploadCompletedSignal()
181 return mUploadCompletedSignal;
190 static uint32_t mCount;
191 static bool mNeedTrigger;
194 Dali::Renderer mRenderer;
197 uint32_t mTotalFrameNumber;
198 uint32_t mPreviousFrame;
200 uint32_t mDroppedFrames;
202 bool mNeedDroppedFrames;
203 Dali::VectorAnimationRenderer::UploadCompletedSignalType mUploadCompletedSignal;
204 std::unique_ptr< EventThreadCallback > mEventThreadCallback;
207 uint32_t VectorAnimationRenderer::mCount = 0;
208 bool VectorAnimationRenderer::mNeedTrigger = true;
210 inline VectorAnimationRenderer& GetImplementation( Dali::VectorAnimationRenderer& renderer )
212 DALI_ASSERT_ALWAYS( renderer && "VectorAnimationRenderer handle is empty." );
213 BaseObject& handle = renderer.GetBaseObject();
214 return static_cast< Internal::Adaptor::VectorAnimationRenderer& >( handle );
217 inline const VectorAnimationRenderer& GetImplementation( const Dali::VectorAnimationRenderer& renderer )
219 DALI_ASSERT_ALWAYS( renderer && "VectorAnimationRenderer handle is empty." );
220 const BaseObject& handle = renderer.GetBaseObject();
221 return static_cast< const Internal::Adaptor::VectorAnimationRenderer& >( handle );
224 } // namespace Adaptor
226 } // namespace Internal
229 /********************************************************************************/
230 /********************************* PUBLIC CLASS *******************************/
231 /********************************************************************************/
233 VectorAnimationRenderer VectorAnimationRenderer::New()
235 Internal::Adaptor::VectorAnimationRenderer* animationRenderer = new Internal::Adaptor::VectorAnimationRenderer();
237 Internal::Adaptor::gVectorAnimationRenderer = animationRenderer;
239 return VectorAnimationRenderer( animationRenderer );
242 VectorAnimationRenderer::VectorAnimationRenderer()
246 VectorAnimationRenderer::~VectorAnimationRenderer()
250 VectorAnimationRenderer::VectorAnimationRenderer( Internal::Adaptor::VectorAnimationRenderer* internal )
251 : BaseHandle( internal )
255 VectorAnimationRenderer::VectorAnimationRenderer( const VectorAnimationRenderer& handle )
256 : BaseHandle( handle )
260 VectorAnimationRenderer& VectorAnimationRenderer::operator=( const VectorAnimationRenderer& rhs )
262 BaseHandle::operator=( rhs );
266 void VectorAnimationRenderer::Finalize()
270 bool VectorAnimationRenderer::Load(const std::string& url)
272 return Internal::Adaptor::GetImplementation( *this ).Load(url);
275 void VectorAnimationRenderer::SetRenderer( Renderer renderer )
277 Internal::Adaptor::GetImplementation( *this ).SetRenderer( renderer );
280 void VectorAnimationRenderer::SetSize( uint32_t width, uint32_t height )
282 Internal::Adaptor::GetImplementation( *this ).SetSize( width, height );
285 bool VectorAnimationRenderer::Render( uint32_t frameNumber )
287 return Internal::Adaptor::GetImplementation( *this ).Render( frameNumber );
290 uint32_t VectorAnimationRenderer::GetTotalFrameNumber() const
292 return Internal::Adaptor::GetImplementation( *this ).GetTotalFrameNumber();
295 float VectorAnimationRenderer::GetFrameRate() const
297 return Internal::Adaptor::GetImplementation( *this ).GetFrameRate();
300 void VectorAnimationRenderer::GetDefaultSize( uint32_t& width, uint32_t& height ) const
302 Internal::Adaptor::GetImplementation( *this ).GetDefaultSize( width, height );
305 void VectorAnimationRenderer::GetLayerInfo( Property::Map& map ) const
309 bool VectorAnimationRenderer::GetMarkerInfo( const std::string& marker, uint32_t& startFrame, uint32_t& endFrame ) const
311 return Internal::Adaptor::GetImplementation( *this ).GetMarkerInfo( marker, startFrame, endFrame );
314 VectorAnimationRenderer::UploadCompletedSignalType& VectorAnimationRenderer::UploadCompletedSignal()
316 return Internal::Adaptor::GetImplementation( *this ).UploadCompletedSignal();
323 namespace VectorAnimationRenderer
326 void RequestTrigger()
328 Dali::Internal::Adaptor::VectorAnimationRenderer::mNeedTrigger = true;
331 void DelayRendering(uint32_t delay)
333 Dali::Internal::Adaptor::gVectorAnimationRenderer->mDelayTime = delay;
336 uint32_t GetDroppedFrames()
338 return Dali::Internal::Adaptor::gVectorAnimationRenderer->mDroppedFrames;
341 } // VectorAnimationRenderer