From a05f094117781e7c3d061547a11f440d0fb6f147 Mon Sep 17 00:00:00 2001 From: Eunki, Hong Date: Tue, 5 Dec 2023 15:45:30 +0900 Subject: [PATCH] Optimized benchmark animation (Reduce event thread overhead) --- examples/benchmark-2dphysics/benchmark-2d-physics-controller.cpp | 219 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------------------------- 1 file changed, 120 insertions(+), 99 deletions(-) diff --git a/examples/benchmark-2dphysics/benchmark-2d-physics-controller.cpp b/examples/benchmark-2dphysics/benchmark-2d-physics-controller.cpp index 190549a..397a674 100644 --- a/examples/benchmark-2dphysics/benchmark-2d-physics-controller.cpp +++ b/examples/benchmark-2dphysics/benchmark-2d-physics-controller.cpp @@ -108,22 +108,103 @@ public: case BenchmarkType::ANIMATION: default: { + DALI_LOG_ERROR("CreateAnimationSimulation\n"); CreateAnimationSimulation(); break; } case BenchmarkType::PHYSICS_2D: { + DALI_LOG_ERROR("CreatePhysicsSimulation\n"); CreatePhysicsSimulation(); break; } } } + bool AnimationSimFinished() + { + switch(mType) + { + case BenchmarkType::ANIMATION: + default: + { + UnparentAndReset(mAnimationSimRootActor); + for(auto&& animation : mBallAnimations) + { + animation.Stop(); + animation.Clear(); + } + mBallAnimations.clear(); + + mType = BenchmarkType::PHYSICS_2D; + + CreateSimulation(); + return true; + } + case BenchmarkType::PHYSICS_2D: + { + mApplication.Quit(); + break; + } + } + return false; + } + + void OnTerminate(Application& application) + { + UnparentAndReset(mAnimationSimRootActor); + UnparentAndReset(mPhysicsRoot); + } + + void OnWindowResize(Window window, Window::WindowSize newSize) + { + switch(mType) + { + case BenchmarkType::ANIMATION: + default: + { + // TODO : Implement here if you want. + break; + } + case BenchmarkType::PHYSICS_2D: + { + if(mPhysicsAdaptor) + { + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor(); + cpSpace* space = scopedAccessor->GetNative().Get(); + + CreateBounds(space, newSize); + } + break; + } + } + } + + bool OnTouched(Dali::Actor actor, const Dali::TouchEvent& touch) + { + mApplication.Quit(); + return false; + } + + void OnKeyEv(const Dali::KeyEvent& event) + { + if(event.GetState() == KeyEvent::DOWN) + { + if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK)) + { + mApplication.Quit(); + } + } + } + + // BenchmarkType::ANIMATION + void CreateAnimationSimulation() { Window::WindowSize windowSize = mWindow.GetSize(); mBallActors.resize(mBallNumber); mBallVelocity.resize(mBallNumber); + mBallAnimations.resize(mBallNumber); mAnimationSimRootActor = Layer::New(); mAnimationSimRootActor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS); @@ -172,49 +253,22 @@ public: PropertyNotification bottomNotify = mBallActors[i].AddPropertyNotification(Actor::Property::POSITION_Y, GreaterThanCondition(height - margin)); bottomNotify.NotifySignal().Connect(this, &Physics2dBenchmarkController::OnHitBottomWall); + + ContinueAnimation(i); } title.RaiseToTop(); - ContinueAnimation(); - } - - bool AnimationSimFinished() - { - switch(mType) - { - case BenchmarkType::ANIMATION: - default: - { - UnparentAndReset(mAnimationSimRootActor); - mBallAnimation.Stop(); - mBallAnimation.Clear(); - - mType = BenchmarkType::PHYSICS_2D; - - CreateSimulation(); - return true; - } - case BenchmarkType::PHYSICS_2D: - { - mApplication.Quit(); - break; - } - } - return false; } - void ContinueAnimation() + void ContinueAnimation(int index) { - if(mBallAnimation) + if(mBallAnimations[index]) { - mBallAnimation.Clear(); + mBallAnimations[index].Clear(); } - mBallAnimation = Animation::New(MAX_ANIMATION_DURATION); - for(int i = 0; i < mBallNumber; ++i) - { - mBallAnimation.AnimateBy(Property(mBallActors[i], Actor::Property::POSITION), mBallVelocity[i] * MAX_ANIMATION_DURATION); - } - mBallAnimation.Play(); + mBallAnimations[index] = Animation::New(MAX_ANIMATION_DURATION); + mBallAnimations[index].AnimateBy(Property(mBallActors[index], Actor::Property::POSITION), mBallVelocity[index] * MAX_ANIMATION_DURATION); + mBallAnimations[index].Play(); } void OnHitLeftWall(PropertyNotification& source) @@ -222,9 +276,12 @@ public: auto actor = Actor::DownCast(source.GetTarget()); if(actor) { - int index = actor["index"]; - mBallVelocity[index].x = fabsf(mBallVelocity[index].x); - ContinueAnimation(); + int index = actor["index"]; + if(mBallVelocity[index].x < 0.0f) + { + mBallVelocity[index].x = fabsf(mBallVelocity[index].x); + ContinueAnimation(index); + } } } @@ -233,9 +290,12 @@ public: auto actor = Actor::DownCast(source.GetTarget()); if(actor) { - int index = actor["index"]; - mBallVelocity[index].x = -fabsf(mBallVelocity[index].x); - ContinueAnimation(); + int index = actor["index"]; + if(mBallVelocity[index].x > 0.0f) + { + mBallVelocity[index].x = -fabsf(mBallVelocity[index].x); + ContinueAnimation(index); + } } } @@ -244,9 +304,12 @@ public: auto actor = Actor::DownCast(source.GetTarget()); if(actor) { - int index = actor["index"]; - mBallVelocity[index].y = -fabsf(mBallVelocity[index].y); - ContinueAnimation(); + int index = actor["index"]; + if(mBallVelocity[index].y > 0.0f) + { + mBallVelocity[index].y = -fabsf(mBallVelocity[index].y); + ContinueAnimation(index); + } } } @@ -255,12 +318,17 @@ public: auto actor = Actor::DownCast(source.GetTarget()); if(actor) { - int index = actor["index"]; - mBallVelocity[index].y = fabsf(mBallVelocity[index].y); - ContinueAnimation(); + int index = actor["index"]; + if(mBallVelocity[index].y < 0.0f) + { + mBallVelocity[index].y = fabsf(mBallVelocity[index].y); + ContinueAnimation(index); + } } } + // BenchmarkType::PHYSICS_2D + void CreatePhysicsSimulation() { Window::WindowSize windowSize = mWindow.GetSize(); @@ -370,53 +438,6 @@ public: return shape; } - void OnTerminate(Application& application) - { - UnparentAndReset(mAnimationSimRootActor); - UnparentAndReset(mPhysicsRoot); - } - - void OnWindowResize(Window window, Window::WindowSize newSize) - { - switch(mType) - { - case BenchmarkType::ANIMATION: - default: - { - // TODO : Implement here if you want. - break; - } - case BenchmarkType::PHYSICS_2D: - { - if(mPhysicsAdaptor) - { - auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor(); - cpSpace* space = scopedAccessor->GetNative().Get(); - - CreateBounds(space, newSize); - } - break; - } - } - } - - bool OnTouched(Dali::Actor actor, const Dali::TouchEvent& touch) - { - mApplication.Quit(); - return false; - } - - void OnKeyEv(const Dali::KeyEvent& event) - { - if(event.GetState() == KeyEvent::DOWN) - { - if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK)) - { - mApplication.Quit(); - } - } - } - private: Application& mApplication; Window mWindow; @@ -434,11 +455,11 @@ private: cpShape* mTopBound{nullptr}; cpShape* mBottomBound{nullptr}; - std::vector mBallActors; - std::vector mBallVelocity; - int mBallNumber; - Animation mBallAnimation; - Timer mTimer; + std::vector mBallActors; + std::vector mBallVelocity; + std::vector mBallAnimations; + int mBallNumber; + Timer mTimer; }; int DALI_EXPORT_API main(int argc, char** argv) -- libgit2 0.21.4