Optimized benchmark animation (Reduce event thread overhead) 93/302293/3
authorEunki, Hong <eunkiki.hong@samsung.com>
Tue, 5 Dec 2023 06:45:30 +0000 (15:45 +0900)
committerEunki, Hong <eunkiki.hong@samsung.com>
Tue, 5 Dec 2023 08:17:55 +0000 (17:17 +0900)
Since we were create new animations every PropertyNotify, and at that time,
we iterate whole balls.

If event thread time delayed (~= the ball is very much) event callback
stacked a lot, and then break the app.

To avoid it, we make the number of animations relative with ball +
change the animation only if we need, not always.

It will reduce the event thread overhead.

Change-Id: Ic6da267e827d012c9e68c7d64594596f3617233a
Signed-off-by: Eunki, Hong <eunkiki.hong@samsung.com>
examples/benchmark-2dphysics/benchmark-2d-physics-controller.cpp

index 190549a17a6073f275afd8fe8953a206add1cf37..397a674a281382d576ff355c3a28406033c4df25 100644 (file)
@@ -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<cpSpace*>();
+
+          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<cpSpace*>();
-
-          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<Actor>   mBallActors;
-  std::vector<Vector3> mBallVelocity;
-  int                  mBallNumber;
-  Animation            mBallAnimation;
-  Timer                mTimer;
+  std::vector<Actor>     mBallActors;
+  std::vector<Vector3>   mBallVelocity;
+  std::vector<Animation> mBallAnimations;
+  int                    mBallNumber;
+  Timer                  mTimer;
 };
 
 int DALI_EXPORT_API main(int argc, char** argv)