Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-demo.git] / examples / perf-scroll / perf-scroll.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali/devel-api/rendering/renderer.h>
20 #include <dali/devel-api/rendering/sampler.h>
21 #include <dali/public-api/common/dali-common.h>
22 #include <dali/integration-api/resource-policies.h>
23 #include <dali/integration-api/debug.h>
24 #include <iostream>
25
26 using namespace Dali;
27 using namespace Dali::Toolkit;
28
29
30 namespace
31 {
32 const char* IMAGE_PATH[] = {
33                             DEMO_IMAGE_DIR "gallery-medium-1.jpg",
34                             DEMO_IMAGE_DIR "gallery-medium-2.jpg",
35                             DEMO_IMAGE_DIR "gallery-medium-3.jpg",
36                             DEMO_IMAGE_DIR "gallery-medium-4.jpg",
37                             DEMO_IMAGE_DIR "gallery-medium-5.jpg",
38                             DEMO_IMAGE_DIR "gallery-medium-6.jpg",
39                             DEMO_IMAGE_DIR "gallery-medium-7.jpg",
40                             DEMO_IMAGE_DIR "gallery-medium-8.jpg",
41                             DEMO_IMAGE_DIR "gallery-medium-9.jpg",
42                             DEMO_IMAGE_DIR "gallery-medium-10.jpg",
43                             DEMO_IMAGE_DIR "gallery-medium-11.jpg",
44                             DEMO_IMAGE_DIR "gallery-medium-12.jpg",
45                             DEMO_IMAGE_DIR "gallery-medium-13.jpg",
46                             DEMO_IMAGE_DIR "gallery-medium-14.jpg",
47                             DEMO_IMAGE_DIR "gallery-medium-15.jpg",
48                             DEMO_IMAGE_DIR "gallery-medium-16.jpg",
49                             DEMO_IMAGE_DIR "gallery-medium-17.jpg",
50                             DEMO_IMAGE_DIR "gallery-medium-18.jpg",
51                             DEMO_IMAGE_DIR "gallery-medium-19.jpg",
52                             DEMO_IMAGE_DIR "gallery-medium-20.jpg",
53                             DEMO_IMAGE_DIR "gallery-medium-21.jpg",
54                             DEMO_IMAGE_DIR "gallery-medium-22.jpg",
55                             DEMO_IMAGE_DIR "gallery-medium-23.jpg",
56                             DEMO_IMAGE_DIR "gallery-medium-24.jpg",
57                             DEMO_IMAGE_DIR "gallery-medium-25.jpg",
58                             DEMO_IMAGE_DIR "gallery-medium-26.jpg",
59                             DEMO_IMAGE_DIR "gallery-medium-27.jpg",
60                             DEMO_IMAGE_DIR "gallery-medium-28.jpg",
61                             DEMO_IMAGE_DIR "gallery-medium-29.jpg",
62                             DEMO_IMAGE_DIR "gallery-medium-30.jpg",
63                             DEMO_IMAGE_DIR "gallery-medium-31.jpg",
64                             DEMO_IMAGE_DIR "gallery-medium-32.jpg",
65                             DEMO_IMAGE_DIR "gallery-medium-33.jpg",
66                             DEMO_IMAGE_DIR "gallery-medium-34.jpg",
67                             DEMO_IMAGE_DIR "gallery-medium-35.jpg",
68                             DEMO_IMAGE_DIR "gallery-medium-36.jpg",
69                             DEMO_IMAGE_DIR "gallery-medium-37.jpg",
70                             DEMO_IMAGE_DIR "gallery-medium-38.jpg",
71                             DEMO_IMAGE_DIR "gallery-medium-39.jpg",
72                             DEMO_IMAGE_DIR "gallery-medium-40.jpg",
73                             DEMO_IMAGE_DIR "gallery-medium-41.jpg",
74                             DEMO_IMAGE_DIR "gallery-medium-42.jpg",
75                             DEMO_IMAGE_DIR "gallery-medium-43.jpg",
76                             DEMO_IMAGE_DIR "gallery-medium-44.jpg",
77                             DEMO_IMAGE_DIR "gallery-medium-45.jpg",
78                             DEMO_IMAGE_DIR "gallery-medium-46.jpg",
79                             DEMO_IMAGE_DIR "gallery-medium-47.jpg",
80                             DEMO_IMAGE_DIR "gallery-medium-48.jpg",
81                             DEMO_IMAGE_DIR "gallery-medium-49.jpg",
82                             DEMO_IMAGE_DIR "gallery-medium-50.jpg",
83                             DEMO_IMAGE_DIR "gallery-medium-51.jpg",
84                             DEMO_IMAGE_DIR "gallery-medium-52.jpg",
85                             DEMO_IMAGE_DIR "gallery-medium-53.jpg",
86 };
87
88 const char* NINEPATCH_IMAGE_PATH[] = {
89                             DEMO_IMAGE_DIR "selection-popup-bg.1.9.png",
90                             DEMO_IMAGE_DIR "selection-popup-bg.2.9.png",
91                             DEMO_IMAGE_DIR "selection-popup-bg.3.9.png",
92                             DEMO_IMAGE_DIR "selection-popup-bg.4.9.png",
93                             DEMO_IMAGE_DIR "selection-popup-bg.5.9.png",
94                             DEMO_IMAGE_DIR "selection-popup-bg.6.9.png",
95                             DEMO_IMAGE_DIR "selection-popup-bg.7.9.png",
96                             DEMO_IMAGE_DIR "selection-popup-bg.8.9.png",
97                             DEMO_IMAGE_DIR "selection-popup-bg.9.9.png",
98                             DEMO_IMAGE_DIR "selection-popup-bg.10.9.png",
99                             DEMO_IMAGE_DIR "selection-popup-bg.11.9.png",
100                             DEMO_IMAGE_DIR "selection-popup-bg.12.9.png",
101                             DEMO_IMAGE_DIR "selection-popup-bg.13.9.png",
102                             DEMO_IMAGE_DIR "selection-popup-bg.14.9.png",
103                             DEMO_IMAGE_DIR "selection-popup-bg.15.9.png",
104                             DEMO_IMAGE_DIR "selection-popup-bg.16.9.png",
105                             DEMO_IMAGE_DIR "selection-popup-bg.17.9.png",
106                             DEMO_IMAGE_DIR "selection-popup-bg.18.9.png",
107                             DEMO_IMAGE_DIR "selection-popup-bg.19.9.png",
108                             DEMO_IMAGE_DIR "selection-popup-bg.20.9.png",
109                             DEMO_IMAGE_DIR "selection-popup-bg.21.9.png",
110                             DEMO_IMAGE_DIR "selection-popup-bg.22.9.png",
111                             DEMO_IMAGE_DIR "selection-popup-bg.23.9.png",
112                             DEMO_IMAGE_DIR "selection-popup-bg.24.9.png",
113                             DEMO_IMAGE_DIR "selection-popup-bg.25.9.png",
114                             DEMO_IMAGE_DIR "selection-popup-bg.26.9.png",
115                             DEMO_IMAGE_DIR "selection-popup-bg.27.9.png",
116                             DEMO_IMAGE_DIR "selection-popup-bg.28.9.png",
117                             DEMO_IMAGE_DIR "selection-popup-bg.29.9.png",
118                             DEMO_IMAGE_DIR "selection-popup-bg.30.9.png",
119                             DEMO_IMAGE_DIR "selection-popup-bg.31.9.png",
120                             DEMO_IMAGE_DIR "selection-popup-bg.32.9.png",
121                             DEMO_IMAGE_DIR "selection-popup-bg.33.9.png",
122                             DEMO_IMAGE_DIR "button-disabled.9.png",
123                             DEMO_IMAGE_DIR "button-down.9.png",
124                             DEMO_IMAGE_DIR "button-down-disabled.9.png",
125                             DEMO_IMAGE_DIR "button-up-1.9.png",
126                             DEMO_IMAGE_DIR "button-up-2.9.png",
127                             DEMO_IMAGE_DIR "button-up-3.9.png",
128                             DEMO_IMAGE_DIR "button-up-4.9.png",
129                             DEMO_IMAGE_DIR "button-up-5.9.png",
130                             DEMO_IMAGE_DIR "button-up-6.9.png",
131                             DEMO_IMAGE_DIR "button-up-7.9.png",
132                             DEMO_IMAGE_DIR "button-up-8.9.png",
133                             DEMO_IMAGE_DIR "button-up-9.9.png",
134                             DEMO_IMAGE_DIR "button-up-10.9.png",
135                             DEMO_IMAGE_DIR "button-up-11.9.png",
136                             DEMO_IMAGE_DIR "button-up-12.9.png",
137                             DEMO_IMAGE_DIR "button-up-13.9.png",
138                             DEMO_IMAGE_DIR "button-up-14.9.png",
139                             DEMO_IMAGE_DIR "button-up-15.9.png",
140                             DEMO_IMAGE_DIR "button-up-16.9.png",
141                             DEMO_IMAGE_DIR "button-up-17.9.png",
142 };
143
144 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATH) / sizeof(char*);
145 const unsigned int NUM_NINEPATCH_IMAGES = sizeof(NINEPATCH_IMAGE_PATH) / sizeof(char*);
146
147 const float ANIMATION_TIME ( 5.0f ); // animation length in seconds
148
149 struct VertexWithTexture
150 {
151   Vector2 position;
152   Vector2 texCoord;
153 };
154
155 VertexWithTexture gQuadWithTexture[] = {
156                                         { Vector2( -0.5f, -0.5f ), Vector2( 0.0f, 0.0f ) },
157                                         { Vector2(  0.5f, -0.5f ), Vector2( 1.0f, 0.0f ) },
158                                         { Vector2( -0.5f,  0.5f ), Vector2( 0.0f, 1.0f ) },
159                                         { Vector2(  0.5f,  0.5f ), Vector2( 1.0f, 1.0f ) }
160 };
161
162 const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
163     attribute mediump vec2 aPosition;\n
164     attribute mediump vec2 aTexCoord;\n
165     uniform mediump mat4 uMvpMatrix;\n
166     uniform mediump vec3 uSize;\n
167     varying mediump vec2 vTexCoord;\n
168     void main()\n
169     {\n
170       vec4 position = vec4(aPosition,0.0,1.0)*vec4(uSize,1.0);\n
171       gl_Position = uMvpMatrix * position;\n
172       vTexCoord = aTexCoord;\n
173     }\n
174 );
175
176 const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
177     uniform lowp vec4 uColor;\n
178     uniform sampler2D sTexture;\n
179     varying mediump vec2 vTexCoord;\n
180
181     void main()\n
182     {\n
183       gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
184     }\n
185 );
186
187
188 Geometry& QuadMesh()
189 {
190   static Geometry mesh;
191   if( !mesh )
192   {
193     PropertyBuffer vertexBuffer;
194     Property::Map vertexFormat;
195     vertexFormat["aPosition"] = Property::VECTOR2;
196     vertexFormat["aTexCoord"] = Property::VECTOR2;
197
198     //Create a vertex buffer for vertex positions and texture coordinates
199     vertexBuffer = PropertyBuffer::New( vertexFormat );
200     vertexBuffer.SetData( gQuadWithTexture, 4u );
201
202     //Create the geometry
203     mesh = Geometry::New();
204     mesh.AddVertexBuffer( vertexBuffer );
205     mesh.SetGeometryType( Geometry::TRIANGLE_STRIP );
206   }
207   return mesh;
208 }
209
210 bool gUseMesh(false);
211 bool gNinePatch(false);
212 unsigned int gRowsPerPage(15);
213 unsigned int gColumnsPerPage(15);
214 unsigned int gPageCount(10);
215 float gDuration(10.0f);
216
217 Renderer CreateRenderer( unsigned int index )
218 {
219
220   int numImages = !gNinePatch ? NUM_IMAGES : NUM_NINEPATCH_IMAGES;
221   static Renderer* renderers = new Renderer[numImages];
222   if( !renderers[index] )
223   {
224     //Create the renderer
225     Shader shader = Shader::New( VERTEX_SHADER_TEXTURE, FRAGMENT_SHADER_TEXTURE );
226
227     const char* imagePath = !gNinePatch ? IMAGE_PATH[index] : NINEPATCH_IMAGE_PATH[index];
228     Image image = ResourceImage::New( imagePath );
229
230     TextureSet textureSet = TextureSet::New();
231     textureSet.SetImage( 0u, image );
232     renderers[index] = Renderer::New( QuadMesh(), shader );
233     renderers[index].SetTextures( textureSet );
234     renderers[index].SetProperty( Renderer::Property::BLENDING_MODE, BlendingMode::OFF );
235
236   }
237   return renderers[index];
238 }
239
240 Actor CreateMeshActor( unsigned int index)
241 {
242   Renderer renderer = CreateRenderer( index );
243   Actor meshActor = Actor::New();
244   meshActor.AddRenderer( renderer );
245   return meshActor;
246 }
247
248 }
249 // Test application to compare performance between ImageActor and ImageView
250 // By default, the application consist of 10 pages of 25x25 ImageActors, this can be modified using the following command line arguments:
251 // -t duration (sec )
252 // --use-imageview ( Use ImageView instead of ImageActor )
253 // --use-mesh ( Use new renderer API (as ImageView) but shares renderers between actors when possible )
254
255 //
256 class PerfScroll : public ConnectionTracker
257 {
258 public:
259
260   PerfScroll( Application& application )
261   : mApplication( application ),
262   mRowsPerPage( gRowsPerPage ),
263   mColumnsPerPage( gColumnsPerPage ),
264   mPageCount( gPageCount )
265   {
266     // Connect to the Application's Init signal
267     mApplication.InitSignal().Connect( this, &PerfScroll::Create );
268   }
269
270   ~PerfScroll()
271   {
272     // Nothing to do here;
273   }
274
275   // The Init signal is received once (only) during the Application lifetime
276   void Create( Application& application )
277   {
278     // Get a handle to the stage
279     Stage stage = Stage::GetCurrent();
280     stage.SetBackgroundColor( Color::WHITE );
281     Vector2 stageSize = stage.GetSize();
282
283     stage.GetRootLayer().SetDepthTestDisabled(true);
284
285     mSize = Vector3( stageSize.x / mColumnsPerPage, stageSize.y / mRowsPerPage, 0.0f );
286
287     // Respond to a click anywhere on the stage
288     stage.GetRootLayer().TouchedSignal().Connect( this, &PerfScroll::OnTouch );
289
290     mParent = Actor::New();
291     mParent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
292     stage.Add(mParent);
293
294     if( gUseMesh )
295     {
296       CreateMeshActors();
297     }
298     else
299     {
300       CreateImageViews();
301     }
302
303     ShowAnimation();
304   }
305
306   bool OnTouch( Actor actor, const TouchEvent& touch )
307   {
308     // quit the application
309     mApplication.Quit();
310     return true;
311   }
312
313   const char* ImagePath( int i )
314   {
315     return !gNinePatch ? IMAGE_PATH[i % NUM_IMAGES] : NINEPATCH_IMAGE_PATH[i % NUM_NINEPATCH_IMAGES];
316   }
317
318   void CreateImageViews()
319   {
320     Stage stage = Stage::GetCurrent();
321     unsigned int actorCount( mRowsPerPage*mColumnsPerPage * mPageCount );
322     mImageView.resize( actorCount );
323
324     for( size_t i(0); i<actorCount; ++i )
325     {
326       mImageView[i] = ImageView::New( ImagePath(i) );
327       mImageView[i].SetSize( Vector3(0.0f,0.0f,0.0f) );
328       mImageView[i].SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
329       mParent.Add( mImageView[i] );
330     }
331   }
332
333   void CreateMeshActors()
334   {
335     Stage stage = Stage::GetCurrent();
336
337     unsigned int actorCount( mRowsPerPage * mColumnsPerPage * mPageCount );
338     mActor.resize( actorCount );
339
340     for( size_t i(0); i<actorCount; ++i )
341     {
342       size_t numImages = !gNinePatch ? NUM_IMAGES : NUM_NINEPATCH_IMAGES;
343       mActor[i] = CreateMeshActor( i % numImages );
344       mActor[i].SetSize(0.0f,0.0f,0.0f);
345
346       mParent.Add( mActor[i] );
347     }
348   }
349
350   void OnAnimationEnd( Animation& source )
351   {
352     if( source == mShow )
353     {
354       ScrollAnimation();
355     }
356     else if( source == mScroll )
357     {
358       HideAnimation();
359     }
360     else
361     {
362       mApplication.Quit();
363     }
364   }
365
366   void ShowAnimation()
367   {
368     Stage stage = Stage::GetCurrent();
369     Vector3 initialPosition( stage.GetSize().x * 0.5f, stage.GetSize().y*0.5f, 1000.0f );
370
371     unsigned int totalColumns = mColumnsPerPage * mPageCount;
372
373     size_t count(0);
374     float xpos, ypos;
375     mShow = Animation::New( 0.0f );
376
377     float totalDuration( 0.0f );
378     float durationPerActor( 0.0f );
379     float delayBetweenActors = ( totalDuration - durationPerActor ) / ( mRowsPerPage * mColumnsPerPage );
380
381     for( size_t i(0); i<totalColumns; ++i )
382     {
383       xpos = mSize.x * i;
384
385       for( size_t j(0); j<mRowsPerPage; ++j )
386       {
387
388         ypos = mSize.y * j;
389
390         float delay = 0.0f;
391         float duration = 0.0f;
392         if( count < mRowsPerPage*mColumnsPerPage )
393         {
394           duration = durationPerActor;
395           delay = delayBetweenActors * count;
396         }
397         if( gUseMesh )
398         {
399           mActor[count].SetPosition( initialPosition );
400           mActor[count].SetSize( Vector3(0.0f,0.0f,0.0f) );
401           mActor[count].SetOrientation( Quaternion( Radian( 0.0f ), Vector3::XAXIS ) );
402           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 ) );
403           mShow.AnimateTo( Property( mActor[count], Actor::Property::SIZE ), mSize, AlphaFunction::EASE_OUT_BACK, TimePeriod( delay, duration ) );
404         }
405         else
406         {
407           mImageView[count].SetPosition( initialPosition );
408           mImageView[count].SetSize( Vector3(0.0f,0.0f,0.0f) );
409           mImageView[count].SetOrientation( Quaternion( Radian(0.0f),Vector3::XAXIS ) );
410           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 ) );
411           mShow.AnimateTo( Property( mImageView[count], Actor::Property::SIZE ), mSize, AlphaFunction::EASE_OUT_BACK, TimePeriod( delay, duration ) );
412         }
413         ++count;
414       }
415     }
416     mShow.Play();
417     mShow.FinishedSignal().Connect( this, &PerfScroll::OnAnimationEnd );
418   }
419
420   void ScrollAnimation()
421   {
422     Stage stage = Stage::GetCurrent();
423     Vector3 stageSize( stage.GetSize() );
424
425     mScroll = Animation::New( gDuration );
426
427     mScroll.AnimateBy( Property( mParent, Actor::Property::POSITION ), Vector3( -(gPageCount-1.)*stageSize.x,0.0f, 0.0f) );
428     mScroll.Play();
429     mScroll.FinishedSignal().Connect( this, &PerfScroll::OnAnimationEnd );
430   }
431
432   void HideAnimation()
433   {
434     size_t count(0);
435     unsigned int actorsPerPage( mRowsPerPage*mColumnsPerPage );
436     mHide = Animation::New( 0.0f );
437
438     unsigned int totalColumns = mColumnsPerPage * mPageCount;
439
440     float totalDuration( 0.0f );
441     float durationPerActor( 0.0f );
442     float delayBetweenActors = ( totalDuration - durationPerActor ) / ( mRowsPerPage * mColumnsPerPage );
443     for( size_t i(0); i<mRowsPerPage; ++i )
444     {
445       for( size_t j(0); j<totalColumns; ++j )
446       {
447         float delay = 0.0f;
448         float duration = 0.0f;
449         if( count < actorsPerPage )
450         {
451           duration = durationPerActor;
452           delay = delayBetweenActors * count;
453         }
454
455         if( gUseMesh )
456         {
457           mHide.AnimateTo( Property( mActor[count], Actor::Property::ORIENTATION ),  Quaternion( Radian( Degree( 70.0f ) ), Vector3::XAXIS ), AlphaFunction::EASE_OUT, TimePeriod( delay, duration ) );
458           mHide.AnimateBy( Property( mActor[count], Actor::Property::POSITION_Z ), 1000.0f, AlphaFunction::EASE_OUT_BACK, TimePeriod( delay +delayBetweenActors*actorsPerPage + duration, duration ) );
459         }
460         else
461         {
462           mHide.AnimateTo( Property( mImageView[count], Actor::Property::ORIENTATION ),  Quaternion( Radian( Degree( 70.0f ) ), Vector3::XAXIS ), AlphaFunction::EASE_OUT, TimePeriod( delay, duration ) );
463           mHide.AnimateBy( Property( mImageView[count], Actor::Property::POSITION_Z ), 1000.0f, AlphaFunction::EASE_OUT_BACK, TimePeriod( delay +delayBetweenActors*actorsPerPage + duration, duration ) );
464         }
465         ++count;
466       }
467     }
468
469     mHide.Play();
470     mHide.FinishedSignal().Connect( this, &PerfScroll::OnAnimationEnd );
471   }
472
473 private:
474   Application&  mApplication;
475
476   std::vector<Actor>  mActor;
477   std::vector<ImageView>  mImageView;
478   Actor mParent;
479
480   Vector3             mSize;
481   unsigned int        mRowsPerPage;
482   unsigned int        mColumnsPerPage;
483   unsigned int        mPageCount;
484
485   Animation           mShow;
486   Animation           mScroll;
487   Animation           mHide;
488 };
489
490 void RunTest( Application& application )
491 {
492   PerfScroll test( application );
493
494   application.MainLoop();
495 }
496
497 // Entry point for Linux & Tizen applications
498 //
499 int DALI_EXPORT_API main( int argc, char **argv )
500 {
501   Application application = Application::New( &argc, &argv );
502
503   for( int i(1) ; i < argc; ++i )
504   {
505     std::string arg( argv[i] );
506     if( arg.compare("--use-mesh") == 0)
507     {
508       gUseMesh = true;
509     }
510     else if( arg.compare("--nine-patch" ) == 0)
511     {
512       gNinePatch = true;
513     }
514     else if( arg.compare(0, 2, "-t" ) == 0)
515     {
516       gDuration = atof( arg.substr( 2, arg.size()).c_str());
517     }
518   }
519
520   RunTest( application );
521
522   return 0;
523 }