2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FUiAnim_AnimationManager.cpp
20 * @brief This file contains implementation of _AnimationManager class
22 * This file contains implementation _AnimationManager class.
27 #include <FBaseSysLog.h>
29 #include <FUiAnimLinearTimingFunction.h>
30 #include <FUiAnimVisualElementPropertyAnimation.h>
31 #include <FUiAnimVisualElement.h>
32 #include <FUiAnimIAnimationTransactionEventListener.h>
34 #include "FUi_EcoreEvas.h"
35 #include "FUi_EcoreEvasMgr.h"
37 #include "FUiAnim_RootVisualElement.h"
38 #include "FUiAnim_VisualElementImpl.h"
39 #include "FUiAnim_VisualElementAnimationImpl.h"
40 #include "FUiAnim_TransactionNode.h"
41 #include "FUiAnim_VisualElementAnimationVariantInterpolator.h"
43 #include "FUiAnim_AnimationManager.h"
44 #include "FUiAnim_DisplayManager.h"
46 #include "FUiAnim_Debug.h"
52 ElapsedTime(const char* tag = null)
58 gettimeofday(&tv, null);
59 start_time = (long long)tv.tv_sec * 1000000LL + (long long)tv.tv_usec / 1LL;
66 gettimeofday(&tv, null);
67 end_time = (long long)tv.tv_sec * 1000000LL + (long long)tv.tv_usec / 1LL;
69 // Tizen::Ui::Animations::_AnimationManager* pAnimationManager = Tizen::Ui::Animations::_AnimationManager::GetInstance();
70 // Tizen::Ui::Animations::_RootVisualElement* pRoot = (Tizen::Ui::Animations::_RootVisualElement*)pAnimationManager->GetRoot()->GetPresentation();
81 static const double fpsLimitInterval = 1.0 / 55.0;
82 Ecore_Animator* pAnimator = null;
86 __AnimatorCallback(void* pData)
88 #ifndef VE_VSYNC_UPDATE
89 pAnimationManager->ProcessAnimationTick();
90 // pManager->PreRender();
96 namespace Tizen { namespace Ui { namespace Animations
99 #ifdef VE_DEBUG_MODULE
101 __SigProcUser(void* pData, int type, void* pEvent)
103 _VeDebug::DumpVeTree(0, 0);
104 _VeDebug::DumpEvasTree(0, 0);
105 _VeDebug::DumpAllEvas();
106 return ECORE_CALLBACK_PASS_ON;
111 // default transaction event listener
112 class _TransactionEventListener
113 : public IAnimationTransactionEventListener
114 , virtual public Tizen::Base::Runtime::IEventListener
117 _TransactionEventListener(void);
118 virtual ~_TransactionEventListener(void);
120 void SetTransactionEventListener(const IAnimationTransactionEventListener* pListener);
122 virtual void OnAnimationTransactionStarted(int transactionId);
123 virtual void OnAnimationTransactionStopped(int transactionId);
124 virtual void OnAnimationTransactionFinished(int transactionId);
127 IAnimationTransactionEventListener* __pEventListener;
130 _TransactionEventListener::_TransactionEventListener(void)
131 : __pEventListener(null)
135 _TransactionEventListener::~_TransactionEventListener(void)
140 _TransactionEventListener::SetTransactionEventListener(const IAnimationTransactionEventListener* pListener)
142 __pEventListener = const_cast< IAnimationTransactionEventListener* >(pListener);
146 _TransactionEventListener::OnAnimationTransactionStarted(int transactionId)
148 if (__pEventListener)
150 __pEventListener->OnAnimationTransactionStarted(transactionId);
155 _TransactionEventListener::OnAnimationTransactionStopped(int transactionId)
157 if (__pEventListener)
159 __pEventListener->OnAnimationTransactionStopped(transactionId);
164 _TransactionEventListener::OnAnimationTransactionFinished(int transactionId)
166 if (__pEventListener)
168 __pEventListener->OnAnimationTransactionFinished(transactionId);
172 _AnimationManager* _AnimationManager::__pInstance = null;
174 _AnimationManager::_AnimationManager(void)
175 : __isAnimating(false)
176 , __isInAnimationTick(false)
177 , __pRootTransaction(null)
178 , __pCurrentTransaction(null)
179 , __pCurrentTransactionExceptGroup(null)
180 , __pDefaultTransactionEventListener(null)
181 , __pDefaultTimingFunction(null)
182 , __pDefaultInterpolator(null)
186 _AnimationManager::~_AnimationManager(void)
191 _AnimationManager::GetInstance(void)
197 _AnimationManager::CreateInstance(void)
199 SysAssertf((__pInstance == null), "Already created.");
201 __pInstance = new (std::nothrow) _AnimationManager();
202 SysTryReturnResult(NID_UI_ANIM, __pInstance != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
204 result r = __pInstance->Construct();
207 SysLogException(NID_UI_ANIM, r, "[%s] Failed to construct _AnimationManager instance.", GetErrorMessage(r));
219 _AnimationManager::ReleaseInstance(void)
221 if (__pInstance != null)
223 __pInstance->Release();
231 _AnimationManager::Release(void)
235 ecore_animator_del(pAnimator);
240 delete __pRootTransaction;
241 __pRootTransaction = null;
243 delete __pDefaultTransactionEventListener;
244 __pDefaultTransactionEventListener = null;
246 delete __pDefaultTimingFunction;
247 __pDefaultTimingFunction = null;
249 delete __pDefaultInterpolator;
250 __pDefaultInterpolator = null;
256 _AnimationManager::Construct(void)
258 result r = E_SUCCESS;
260 #ifdef VE_DEBUG_MODULE
261 ecore_event_handler_add(ECORE_EVENT_SIGNAL_USER, __SigProcUser, null);
264 __pRootTransaction = new (std::nothrow) _TransactionNode();
265 SysTryReturnResult(NID_UI_ANIM, (__pRootTransaction != null), E_OUT_OF_MEMORY, "Memory allocation failed. Failed to create root transaction");
268 SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), , r, "[%s] Failed to create root transaction.", GetErrorMessage(r));
270 __pRootTransaction->Commit(); //root is always committed.
272 __pCurrentTransaction = __pRootTransaction;
273 __pCurrentTransactionExceptGroup = __pRootTransaction;
275 __pDefaultTransactionEventListener = new (std::nothrow) _TransactionEventListener();
276 SysTryCatch(NID_UI_ANIM, __pDefaultTransactionEventListener != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
277 "[E_OUT_OF_MEMORY] Memory allocation failed. Failed to create default transaction event listener.");
279 __pDefaultTimingFunction = new (std::nothrow) LinearTimingFunction();
280 SysTryCatch(NID_UI_ANIM, __pDefaultTimingFunction != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
281 "[E_OUT_OF_MEMORY] Memory allocation failed. Failed to create default timing function map.");
283 __pDefaultInterpolator = new (std::nothrow) _VisualElementAnimationVariantInterpolator();
284 SysTryCatch(NID_UI_ANIM, __pDefaultInterpolator != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
285 "[E_OUT_OF_MEMORY] Memory allocation failed. Failed to create default interpolator.");
290 delete __pDefaultInterpolator;
291 delete __pDefaultTimingFunction;
292 delete __pDefaultTransactionEventListener;
293 delete __pRootTransaction;
300 static long __RenderCount = 0; //Need?
304 _AnimationManager::SetAnimatorEnabled(bool animating)
306 #ifdef VE_VSYNC_UPDATE
308 __isAnimating = animating;
310 if (likely(animating))
312 if (likely(pAnimator == null))
314 ecore_animator_frametime_set(fpsLimitInterval);
315 pAnimator = (Ecore_Animator*)ecore_animator_add(__AnimatorCallback, _AnimationManager::GetInstance());
322 ecore_animator_del(pAnimator);
329 _DisplayManager::GetInstance()->AddWakeUpEvent();
335 _AnimationManager::AddAnimation(VisualElement& target, const Tizen::Base::String* pKeyName, VisualElementAnimation& animation)
337 SysTryReturnResult(NID_UI_ANIM, (__pCurrentTransaction != null), E_SYSTEM, "A system error has been occurred. Current transaction is null.");
339 if (pKeyName != null && pKeyName->IsEmpty() == false)
341 SysTryReturnResult(NID_UI_ANIM, (DoesAnimationExist(target, *pKeyName) == false), E_INVALID_ARG, "Invalid argument(s) is used. Animation with keyName exists. key name = %ls", pKeyName->GetPointer());
344 result r = E_SUCCESS;
346 if (_VisualElementAnimationImpl::GetInstance(animation)->GetTimingFunction() == null)
348 animation.SetTimingFunction(__pDefaultTimingFunction);
351 if (_VisualElementAnimationImpl::GetInstance(animation)->GetValueInterpolator() == null)
353 animation.SetValueInterpolator(__pDefaultInterpolator);
356 // current trasaction
357 r = __pCurrentTransaction->AddAnimation(target, pKeyName, animation);
358 SysTryReturnResult(NID_UI_ANIM, r == E_SUCCESS, r, "Failed to add animation.");
360 if (__pCurrentTransaction == __pRootTransaction && __isAnimating == false)
362 SetAnimatorEnabled(true);
369 _AnimationManager::RemoveAnimation(VisualElement& target, const Tizen::Base::String& keyName)
371 return __pRootTransaction->RemoveChildrenAnimation(target, keyName);
375 _AnimationManager::RemoveAnimationForProperty(VisualElement& target, const Tizen::Base::String& property)
377 __pRootTransaction->RemoveChildrenAnimationByProperty(target, property);
381 _AnimationManager::RemoveAllAnimations(VisualElement& target)
383 __pRootTransaction->RemoveChildrenAllAnimations(target);
386 VisualElementAnimation*
387 _AnimationManager::GetAnimationN(VisualElement& target, const Tizen::Base::String& keyName) const
389 VisualElementAnimation* pAnimation = const_cast< VisualElementAnimation* >(__pRootTransaction->GetChildrenAnimation(target, keyName));
391 if (pAnimation != null)
393 SetLastResult(E_SUCCESS);
394 return pAnimation->CloneN();
397 SetLastResult(E_OBJ_NOT_FOUND);
402 _AnimationManager::DoesAnimationExist(VisualElement& target, const Tizen::Base::String& keyName) const
404 return (__pRootTransaction->GetChildrenAnimation(target, keyName) != null);
407 VisualElementAnimation*
408 _AnimationManager::GetAnimationForPropertyN(VisualElement& target, const Tizen::Base::String& property) const
410 VisualElementAnimation* pAnimation = const_cast< VisualElementAnimation* >(__pRootTransaction->GetChildrenAnimationByProperty(target, property));
412 if (pAnimation != null)
414 SetLastResult(E_SUCCESS);
415 return pAnimation->CloneN();
418 SetLastResult(E_OBJ_NOT_FOUND);
423 _AnimationManager::BeginTransaction(void)
425 SysTryReturn(NID_UI_ANIM, (__pCurrentTransaction != null), -1, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Current transaction is null.");
427 _TransactionNode* pNode = new (std::nothrow) _TransactionNode(*__pCurrentTransaction);
428 SysTryReturn(NID_UI_ANIM, (pNode != null), -1, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed. Failed to create _TransactionNode.");
430 result r = GetLastResult();
431 SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), , r, "[%s] Failed to create _TransactionNode instance.", GetErrorMessage(r));
433 r = __pCurrentTransaction->AddChild(*pNode);
434 SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), , r, "[%s] Failed to add child.", GetErrorMessage(r));
436 __pCurrentTransaction = pNode;
437 __pCurrentTransactionExceptGroup = pNode;
439 pNode->SetDefaultEventListener(__pDefaultTransactionEventListener);
441 return pNode->GetId();
450 _AnimationManager::BeginGroupTransaction(VisualElement& target, const Tizen::Base::String* pKeyName, VisualElementAnimation& animation)
452 SysTryReturn(NID_UI_ANIM, (__pCurrentTransaction != null), -1, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Current transaction is null.");
454 if (pKeyName != null && pKeyName->IsEmpty() == false)
456 SysTryReturn(NID_UI_ANIM, (DoesAnimationExist(target, *pKeyName) == false), 0, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument(s) is used. Animation with keyName exists.");
459 _AnimationGroupNode* pNode = new (std::nothrow) _AnimationGroupNode(target, pKeyName, animation);
460 SysTryReturn(NID_UI_ANIM, (pNode != null), -1, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed. Failed to create _AnimationNode.");
462 result r = GetLastResult();
463 SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), , r, "[%s] Failed to _AnimationGroupNode instance.", GetErrorMessage(r));
465 r = __pCurrentTransaction->AddChild(*pNode);
466 SysTryCatch(NID_UI_ANIM, r == E_SUCCESS, , r, "[%s] Failed to add a child.", GetErrorMessage(r));
468 __pCurrentTransaction = pNode;
470 return pNode->GetId();
480 _AnimationManager::CommitTransaction(void)
482 SysTryReturnResult(NID_UI_ANIM, __pCurrentTransaction != null, E_SYSTEM, "A system error has been occurred. Current transaction is null.");
483 SysTryReturnResult(NID_UI_ANIM, __pCurrentTransaction != __pRootTransaction, E_INVALID_OPERATION, "Transaction isn't opened.");
485 _TransactionNode* pParentTransaction = __pCurrentTransaction->GetParent();
487 if (__pCurrentTransaction->IsEmpty())
489 pParentTransaction->RemoveChild(*__pCurrentTransaction);
493 if (pParentTransaction == __pRootTransaction && __pRootTransaction->IsEmpty() == false)
495 __pCurrentTransaction->SetChildrenBaseTime(_AnimationTime::GetTime());
497 SetAnimatorEnabled(true);
500 __pCurrentTransaction->Commit();
503 __pCurrentTransaction = pParentTransaction;
505 if (dynamic_cast< _AnimationGroupNode* > (pParentTransaction) == null)
507 __pCurrentTransactionExceptGroup = pParentTransaction;
514 _AnimationManager::DiscardTransaction(void)
516 SysTryReturnResult(NID_UI_ANIM, (__pCurrentTransaction != __pRootTransaction), E_INVALID_OPERATION, "Transaction isn't opened.");
518 _TransactionNode* pLastChild = __pRootTransaction->GetLastChild();
519 SysTryReturnResult(NID_UI_ANIM, pLastChild != null, E_SYSTEM, "A system error has been occurred. Last child is null.");
521 __pRootTransaction->RemoveChild(*pLastChild);
523 __pCurrentTransaction = __pRootTransaction;
524 __pCurrentTransactionExceptGroup = __pRootTransaction;
530 _AnimationManager::StopTransaction(int transactionId)
532 _TransactionNode* pChild = __pRootTransaction->GetChild(transactionId);
534 if (pChild != null && pChild->IsCommitted(true) == true)
536 __pRootTransaction->RemoveChild(transactionId);
541 return E_INVALID_ARG;
545 _AnimationManager::CancelTransaction(int transactionId)
547 if (__pCurrentTransaction->GetId() == transactionId)
549 _TransactionNode* pParentTransaction = __pCurrentTransaction->GetParent();
551 __pCurrentTransaction = pParentTransaction;
553 if (dynamic_cast< _AnimationGroupNode* > (pParentTransaction) == null)
555 __pCurrentTransactionExceptGroup = pParentTransaction;
559 __pRootTransaction->RemoveChild(transactionId);
565 _AnimationManager::IsTransactionRunning(int transactionId) const
567 _TransactionNode* pChild = __pRootTransaction->GetChild(transactionId);
571 return pChild->IsRunning();
578 _AnimationManager::IsImplicitAnimationEnabled(void) const
580 return __pCurrentTransactionExceptGroup->IsImplicitAnimationEnabled();
584 _AnimationManager::GetCurrentTransaction(bool withGroup) const
586 if (__pCurrentTransaction == __pRootTransaction)
591 if (withGroup == false)
593 return __pCurrentTransactionExceptGroup;
596 return __pCurrentTransaction;
600 _AnimationManager::SetTransactionEventListener(const IAnimationTransactionEventListener* pListener)
602 static_cast< _TransactionEventListener* >(__pDefaultTransactionEventListener)->SetTransactionEventListener(pListener);
606 _AnimationManager::ProcessAnimationTick(void)
608 if (unlikely(__isInAnimationTick))
613 __isInAnimationTick = true;
615 // process transactions;
616 __pRootTransaction->ProcessAnimationTick(_AnimationTime::GetTime());
618 if (likely(__pRootTransaction->IsEmpty() == true))
620 SetAnimatorEnabled(false);
623 __isInAnimationTick = false;
627 }}} // Tizen::Ui::Animations