Changes after TouchedSignal changes
[platform/core/uifw/dali-demo.git] / examples / benchmark / benchmark.cpp
index 96e1e51..b1d05d5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
  *
  */
 
+// EXTERNAL INCLUDES
 #include <dali-toolkit/dali-toolkit.h>
-#include <dali/devel-api/rendering/renderer.h>
-#include <dali/public-api/common/dali-common.h>
-#include <dali/integration-api/resource-policies.h>
-#include <dali/integration-api/debug.h>
-#include <iostream>
+
+// INTERNAL INCLUDES
+#include "shared/utility.h"
 
 using namespace Dali;
 using namespace Dali::Toolkit;
@@ -151,13 +150,6 @@ struct VertexWithTexture
   Vector2 texCoord;
 };
 
-VertexWithTexture gQuadWithTexture[] = {
-                                        { Vector2( -0.5f, -0.5f ), Vector2( 0.0f, 0.0f ) },
-                                        { Vector2(  0.5f, -0.5f ), Vector2( 1.0f, 0.0f ) },
-                                        { Vector2( -0.5f,  0.5f ), Vector2( 0.0f, 1.0f ) },
-                                        { Vector2(  0.5f,  0.5f ), Vector2( 1.0f, 1.0f ) }
-};
-
 const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
     attribute mediump vec2 aPosition;\n
     attribute mediump vec2 aTexCoord;\n
@@ -183,71 +175,30 @@ const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
     }\n
 );
 
-
-Geometry& QuadMesh()
-{
-  static Geometry mesh;
-  if( !mesh )
-  {
-    PropertyBuffer vertexBuffer;
-    Property::Map vertexFormat;
-    vertexFormat["aPosition"] = Property::VECTOR2;
-    vertexFormat["aTexCoord"] = Property::VECTOR2;
-
-    //Create a vertex buffer for vertex positions and texture coordinates
-    vertexBuffer = PropertyBuffer::New( vertexFormat, 4u );
-    vertexBuffer.SetData( gQuadWithTexture );
-
-    //Create the geometry
-    mesh = Geometry::New();
-    mesh.AddVertexBuffer( vertexBuffer );
-    mesh.SetGeometryType(Geometry::TRIANGLE_STRIP );
-  }
-  return mesh;
-}
-
 bool gUseMesh(false);
-bool gUseImageActor(false);
 bool gNinePatch(false);
 unsigned int gRowsPerPage(25);
 unsigned int gColumnsPerPage( 25 );
 unsigned int gPageCount(13);
 
-Renderer CreateRenderer( unsigned int index )
-{
-
-  int numImages = !gNinePatch ? NUM_IMAGES : NUM_NINEPATCH_IMAGES;
-  static Renderer* renderers = new Renderer[numImages];
-  if( !renderers[index] )
-  {
-    //Create the renderer
-    Shader shader = Shader::New( VERTEX_SHADER_TEXTURE, FRAGMENT_SHADER_TEXTURE );
-
-    const char* imagePath = !gNinePatch ? IMAGE_PATH[index] : NINEPATCH_IMAGE_PATH[index];
-    Image image = ResourceImage::New(imagePath);
-    Material material = Material::New( shader );
-    material.AddTexture( image, "sTexture" );
-    material.SetBlendMode( BlendingMode::OFF );
-    renderers[index] = Renderer::New( QuadMesh(), material );
-  }
-  return renderers[index];
-}
-
-Actor CreateMeshActor( unsigned int index)
+Renderer CreateRenderer( unsigned int index, Geometry geometry, Shader shader )
 {
-  Renderer renderer = CreateRenderer(index);
-  Actor meshActor = Actor::New();
-  meshActor.AddRenderer( renderer );
-  return meshActor;
+  Renderer renderer = Renderer::New( geometry, shader );
+  const char* imagePath = !gNinePatch ? IMAGE_PATH[index] : NINEPATCH_IMAGE_PATH[index];
+  Texture texture = DemoHelper::LoadTexture( imagePath );
+  TextureSet textureSet = TextureSet::New();
+  textureSet.SetTexture( 0u, texture );
+  renderer.SetTextures( textureSet );
+  renderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::OFF );
+  return renderer;
 }
 
 }
-// Test application to compare performance between ImageActor and ImageView
+// Test application to compare performance between using a mesh and ImageView
 // By default, the application consist of 10 pages of 25x25 Image views, this can be modified using the following command line arguments:
 // -r NumberOfRows  (Modifies the number of rows per page)
 // -c NumberOfColumns (Modifies the number of columns per page)
 // -p NumberOfPages (Modifies the nimber of pages )
-// --use-image-actor ( Use ImageActor instead of ImageView )
 // --use-mesh ( Use new renderer API (as ImageView) but shares renderers between actors when possible )
 // --nine-patch ( Use nine patch images )
 
@@ -257,43 +208,39 @@ class Benchmark : public ConnectionTracker
 public:
 
   Benchmark( Application& application )
-: mApplication( application ),
-  mRowsPerPage( gRowsPerPage ),
-  mColumnsPerPage( gColumnsPerPage ),
-  mPageCount( gPageCount )
-{
+  : mApplication( application ),
+    mRowsPerPage( gRowsPerPage ),
+    mColumnsPerPage( gColumnsPerPage ),
+    mPageCount( gPageCount )
+  {
     // Connect to the Application's Init signal
     mApplication.InitSignal().Connect( this, &Benchmark::Create );
-}
-
-  ~Benchmark()
-  {
-    // Nothing to do here;
   }
 
+  ~Benchmark() = default;
+
   // The Init signal is received once (only) during the Application lifetime
   void Create( Application& application )
   {
-    // Get a handle to the stage
-    Stage stage = Stage::GetCurrent();
-    stage.SetBackgroundColor( Color::WHITE );
-    Vector2 stageSize = stage.GetSize();
+    // Get a handle to the window
+    Window window = application.GetWindow();
+    window.SetBackgroundColor( Color::WHITE );
+    Vector2 windowSize = window.GetSize();
 
-    stage.GetRootLayer().SetDepthTestDisabled(true);
+    window.GetRootLayer().SetProperty( Layer::Property::DEPTH_TEST, false );
 
-    mSize = Vector3( stageSize.x / mColumnsPerPage, stageSize.y / mRowsPerPage, 0.0f );
+    mSize = Vector3( windowSize.x / mColumnsPerPage, windowSize.y / mRowsPerPage, 0.0f );
 
-    // Respond to a click anywhere on the stage
-    stage.GetRootLayer().TouchedSignal().Connect( this, &Benchmark::OnTouch );
+    // Respond to a click anywhere on the window
+    window.GetRootLayer().TouchedSignal().Connect( this, &Benchmark::OnTouch );
+
+    // Respond to key events
+    window.KeyEventSignal().Connect( this, &Benchmark::OnKeyEvent );
 
     if( gUseMesh )
     {
       CreateMeshActors();
     }
-    else if( gUseImageActor )
-    {
-      CreateImageActors();
-    }
     else
     {
       CreateImageViews();
@@ -314,49 +261,44 @@ public:
     return !gNinePatch ? IMAGE_PATH[i % NUM_IMAGES] : NINEPATCH_IMAGE_PATH[i % NUM_NINEPATCH_IMAGES];
   }
 
-  void CreateImageActors()
-  {
-    Stage stage = Stage::GetCurrent();
-    unsigned int actorCount(mRowsPerPage*mColumnsPerPage * mPageCount);
-    mActor.resize(actorCount);
-
-    for( size_t i(0); i<actorCount; ++i )
-    {
-      Image image = ResourceImage::New(ImagePath(i));
-      mActor[i] = ImageActor::New(image);
-      mActor[i].SetSize(Vector3(0.0f,0.0f,0.0f));
-      mActor[i].SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
-      stage.Add(mActor[i]);
-    }
-  }
-
   void CreateImageViews()
   {
-    Stage stage = Stage::GetCurrent();
+    Window window = mApplication.GetWindow();
     unsigned int actorCount(mRowsPerPage*mColumnsPerPage * mPageCount);
     mImageView.resize(actorCount);
 
     for( size_t i(0); i<actorCount; ++i )
     {
       mImageView[i] = ImageView::New(ImagePath(i));
-      mImageView[i].SetSize(Vector3(0.0f,0.0f,0.0f));
+      mImageView[i].SetProperty( Actor::Property::SIZE, Vector3(0.0f,0.0f,0.0f) );
       mImageView[i].SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
-      stage.Add(mImageView[i]);
+      window.Add(mImageView[i]);
     }
   }
 
   void CreateMeshActors()
   {
-    Stage stage = Stage::GetCurrent();
+    unsigned int numImages = !gNinePatch ? NUM_IMAGES : NUM_NINEPATCH_IMAGES;
+
+    //Create all the renderers
+    std::vector<Renderer> renderers( numImages );
+    Shader shader = Shader::New( VERTEX_SHADER_TEXTURE, FRAGMENT_SHADER_TEXTURE );
+    Geometry geometry = DemoHelper::CreateTexturedQuad();
+    for( unsigned int i(0); i<numImages; ++i )
+    {
+      renderers[i] = CreateRenderer( i, geometry, shader );
+    }
+
+    //Create the actors
+    Window window = mApplication.GetWindow();
     unsigned int actorCount(mRowsPerPage*mColumnsPerPage * mPageCount);
     mActor.resize(actorCount);
     for( size_t i(0); i<actorCount; ++i )
     {
-      size_t numImages = !gNinePatch ? NUM_IMAGES : NUM_NINEPATCH_IMAGES;
-      mActor[i] = CreateMeshActor(i % numImages);
-      mActor[i].SetSize(0.0f,0.0f,0.0f);
-
-      stage.Add(mActor[i]);
+      mActor[i] = Actor::New();
+      mActor[i].AddRenderer( renderers[i % numImages] );
+      mActor[i].SetProperty( Actor::Property::SIZE, Vector3(0.0f,0.0f,0.0f) );
+      window.Add(mActor[i]);
     }
   }
 
@@ -378,8 +320,9 @@ public:
 
   void ShowAnimation()
   {
-    Stage stage = Stage::GetCurrent();
-    Vector3 initialPosition( stage.GetSize().x * 0.5f, stage.GetSize().y*0.5f, 1000.0f );
+    Window window = mApplication.GetWindow();
+    const Vector2 windowSize(window.GetSize());
+    Vector3 initialPosition( windowSize.width * 0.5f, windowSize.height * 0.5f, 1000.0f );
 
     unsigned int totalColumns = mColumnsPerPage * mPageCount;
 
@@ -401,24 +344,24 @@ public:
 
         float delay = 0.0f;
         float duration = 0.0f;
-        if( count < mRowsPerPage*mColumnsPerPage )
+        if( count < ( static_cast< size_t >( mRowsPerPage ) * mColumnsPerPage ) )
         {
           duration = durationPerActor;
           delay = delayBetweenActors * count;
         }
-        if( gUseImageActor || gUseMesh )
+        if( gUseMesh )
         {
-          mActor[count].SetPosition( initialPosition );
-          mActor[count].SetSize( Vector3(0.0f,0.0f,0.0f) );
-          mActor[count].SetOrientation( Quaternion( Radian(0.0f),Vector3::XAXIS));
+          mActor[count].SetProperty( Actor::Property::POSITION, initialPosition );
+          mActor[count].SetProperty( Actor::Property::SIZE, Vector3(0.0f,0.0f,0.0f) );
+          mActor[count].SetProperty( Actor::Property::ORIENTATION, Quaternion( Radian(0.0f),Vector3::XAXIS) );
           mShow.AnimateTo( Property( mActor[count], Actor::Property::POSITION), Vector3(xpos+mSize.x*0.5f, ypos+mSize.y*0.5f, 0.0f), AlphaFunction::EASE_OUT_BACK, TimePeriod( delay, duration ));
           mShow.AnimateTo( Property( mActor[count], Actor::Property::SIZE), mSize, AlphaFunction::EASE_OUT_BACK, TimePeriod( delay, duration ));
         }
         else
         {
-          mImageView[count].SetPosition( initialPosition );
-          mImageView[count].SetSize( Vector3(0.0f,0.0f,0.0f) );
-          mImageView[count].SetOrientation( Quaternion( Radian(0.0f),Vector3::XAXIS));
+          mImageView[count].SetProperty( Actor::Property::POSITION, initialPosition );
+          mImageView[count].SetProperty( Actor::Property::SIZE, Vector3(0.0f,0.0f,0.0f) );
+          mImageView[count].SetProperty( Actor::Property::ORIENTATION, Quaternion( Radian(0.0f),Vector3::XAXIS) );
           mShow.AnimateTo( Property( mImageView[count], Actor::Property::POSITION), Vector3(xpos+mSize.x*0.5f, ypos+mSize.y*0.5f, 0.0f), AlphaFunction::EASE_OUT_BACK, TimePeriod( delay, duration ));
           mShow.AnimateTo( Property( mImageView[count], Actor::Property::SIZE), mSize, AlphaFunction::EASE_OUT_BACK, TimePeriod( delay, duration ));
         }
@@ -431,26 +374,26 @@ public:
 
   void ScrollAnimation()
   {
-    Stage stage = Stage::GetCurrent();
-    Vector3 stageSize( stage.GetSize() );
+    Window window = mApplication.GetWindow();
+    Vector3 windowSize( window.GetSize() );
 
     mScroll = Animation::New(10.0f);
-    size_t actorCount( mRowsPerPage*mColumnsPerPage*mPageCount);
+    size_t actorCount( static_cast< size_t >( mRowsPerPage ) * mColumnsPerPage * mPageCount );
     for( size_t i(0); i<actorCount; ++i )
     {
-      if( gUseImageActor || gUseMesh )
+      if( gUseMesh )
       {
-        mScroll.AnimateBy( Property( mActor[i], Actor::Property::POSITION), Vector3(-4.0f*stageSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(0.0f,3.0f));
-        mScroll.AnimateBy( Property( mActor[i], Actor::Property::POSITION), Vector3(-4.0f*stageSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(3.0f,3.0f));
-        mScroll.AnimateBy( Property( mActor[i], Actor::Property::POSITION), Vector3(-4.0f*stageSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(6.0f,2.0f));
-        mScroll.AnimateBy( Property( mActor[i], Actor::Property::POSITION), Vector3( 12.0f*stageSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(8.0f,2.0f));
+        mScroll.AnimateBy( Property( mActor[i], Actor::Property::POSITION), Vector3(-4.0f*windowSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(0.0f,3.0f));
+        mScroll.AnimateBy( Property( mActor[i], Actor::Property::POSITION), Vector3(-4.0f*windowSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(3.0f,3.0f));
+        mScroll.AnimateBy( Property( mActor[i], Actor::Property::POSITION), Vector3(-4.0f*windowSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(6.0f,2.0f));
+        mScroll.AnimateBy( Property( mActor[i], Actor::Property::POSITION), Vector3( 12.0f*windowSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(8.0f,2.0f));
       }
       else
       {
-        mScroll.AnimateBy( Property( mImageView[i], Actor::Property::POSITION), Vector3(-4.0f*stageSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(0.0f,3.0f));
-        mScroll.AnimateBy( Property( mImageView[i], Actor::Property::POSITION), Vector3(-4.0f*stageSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(3.0f,3.0f));
-        mScroll.AnimateBy( Property( mImageView[i], Actor::Property::POSITION), Vector3(-4.0f*stageSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(6.0f,2.0f));
-        mScroll.AnimateBy( Property( mImageView[i], Actor::Property::POSITION), Vector3( 12.0f*stageSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(8.0f,2.0f));
+        mScroll.AnimateBy( Property( mImageView[i], Actor::Property::POSITION), Vector3(-4.0f*windowSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(0.0f,3.0f));
+        mScroll.AnimateBy( Property( mImageView[i], Actor::Property::POSITION), Vector3(-4.0f*windowSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(3.0f,3.0f));
+        mScroll.AnimateBy( Property( mImageView[i], Actor::Property::POSITION), Vector3(-4.0f*windowSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(6.0f,2.0f));
+        mScroll.AnimateBy( Property( mImageView[i], Actor::Property::POSITION), Vector3( 12.0f*windowSize.x,0.0f, 0.0f), AlphaFunction::EASE_OUT, TimePeriod(8.0f,2.0f));
       }
     }
     mScroll.Play();
@@ -465,7 +408,7 @@ public:
 
     unsigned int totalColumns = mColumnsPerPage * mPageCount;
 
-    float finalZ = Dali::Stage::GetCurrent().GetRenderTaskList().GetTask(0).GetCameraActor().GetCurrentWorldPosition().z;
+    float finalZ = mApplication.GetWindow().GetRenderTaskList().GetTask(0).GetCameraActor().GetCurrentProperty< Vector3 >( Actor::Property::WORLD_POSITION ).z;
     float totalDuration( 5.0f);
     float durationPerActor( 0.5f );
     float delayBetweenActors = ( totalDuration - durationPerActor) / (mRowsPerPage*mColumnsPerPage);
@@ -481,7 +424,7 @@ public:
           delay = delayBetweenActors * count;
         }
 
-        if( gUseImageActor || gUseMesh )
+        if( gUseMesh )
         {
           mHide.AnimateTo( Property( mActor[count], Actor::Property::ORIENTATION),  Quaternion( Radian( Degree( 70.0f ) ), Vector3::XAXIS ), AlphaFunction::EASE_OUT, TimePeriod( delay, duration ));
           mHide.AnimateBy( Property( mActor[count], Actor::Property::POSITION_Z), finalZ, AlphaFunction::EASE_OUT_BACK, TimePeriod( delay +delayBetweenActors*actorsPerPage + duration, duration ));
@@ -499,6 +442,17 @@ public:
     mHide.FinishedSignal().Connect( this, &Benchmark::OnAnimationEnd );
   }
 
+  void OnKeyEvent( const 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;
 
@@ -515,16 +469,7 @@ private:
   Animation           mHide;
 };
 
-void RunTest( Application& application )
-{
-  Benchmark test( application );
-
-  application.MainLoop();
-}
-
-// Entry point for Linux & Tizen applications
-//
-int main( int argc, char **argv )
+int DALI_EXPORT_API main( int argc, char **argv )
 {
   Application application = Application::New( &argc, &argv );
 
@@ -535,10 +480,6 @@ int main( int argc, char **argv )
     {
       gUseMesh = true;
     }
-    else if( arg.compare("--use-image-actor") == 0)
-    {
-      gUseImageActor = true;
-    }
     else if( arg.compare("--nine-patch" ) == 0)
     {
       gNinePatch = true;
@@ -557,7 +498,8 @@ int main( int argc, char **argv )
     }
   }
 
-  RunTest( application );
+  Benchmark test( application );
+  application.MainLoop();
 
   return 0;
 }