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