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