2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali/devel-api/rendering/renderer.h>
20 #include <dali/public-api/common/dali-common.h>
21 #include <dali/integration-api/resource-policies.h>
22 #include <dali/integration-api/debug.h>
26 using namespace Dali::Toolkit;
31 const char* IMAGE_PATH[] = {
32 DALI_IMAGE_DIR "gallery-medium-1.jpg",
33 DALI_IMAGE_DIR "gallery-medium-2.jpg",
34 DALI_IMAGE_DIR "gallery-medium-3.jpg",
35 DALI_IMAGE_DIR "gallery-medium-4.jpg",
36 DALI_IMAGE_DIR "gallery-medium-5.jpg",
37 DALI_IMAGE_DIR "gallery-medium-6.jpg",
38 DALI_IMAGE_DIR "gallery-medium-7.jpg",
39 DALI_IMAGE_DIR "gallery-medium-8.jpg",
40 DALI_IMAGE_DIR "gallery-medium-9.jpg",
41 DALI_IMAGE_DIR "gallery-medium-10.jpg",
42 DALI_IMAGE_DIR "gallery-medium-11.jpg",
43 DALI_IMAGE_DIR "gallery-medium-12.jpg",
44 DALI_IMAGE_DIR "gallery-medium-13.jpg",
45 DALI_IMAGE_DIR "gallery-medium-14.jpg",
46 DALI_IMAGE_DIR "gallery-medium-15.jpg",
47 DALI_IMAGE_DIR "gallery-medium-16.jpg",
48 DALI_IMAGE_DIR "gallery-medium-17.jpg",
49 DALI_IMAGE_DIR "gallery-medium-18.jpg",
50 DALI_IMAGE_DIR "gallery-medium-19.jpg",
51 DALI_IMAGE_DIR "gallery-medium-20.jpg",
52 DALI_IMAGE_DIR "gallery-medium-21.jpg",
53 DALI_IMAGE_DIR "gallery-medium-22.jpg",
54 DALI_IMAGE_DIR "gallery-medium-23.jpg",
55 DALI_IMAGE_DIR "gallery-medium-24.jpg",
56 DALI_IMAGE_DIR "gallery-medium-25.jpg",
57 DALI_IMAGE_DIR "gallery-medium-26.jpg",
58 DALI_IMAGE_DIR "gallery-medium-27.jpg",
59 DALI_IMAGE_DIR "gallery-medium-28.jpg",
60 DALI_IMAGE_DIR "gallery-medium-29.jpg",
61 DALI_IMAGE_DIR "gallery-medium-30.jpg",
62 DALI_IMAGE_DIR "gallery-medium-31.jpg",
63 DALI_IMAGE_DIR "gallery-medium-32.jpg",
64 DALI_IMAGE_DIR "gallery-medium-33.jpg",
65 DALI_IMAGE_DIR "gallery-medium-34.jpg",
66 DALI_IMAGE_DIR "gallery-medium-35.jpg",
67 DALI_IMAGE_DIR "gallery-medium-36.jpg",
68 DALI_IMAGE_DIR "gallery-medium-37.jpg",
69 DALI_IMAGE_DIR "gallery-medium-38.jpg",
70 DALI_IMAGE_DIR "gallery-medium-39.jpg",
71 DALI_IMAGE_DIR "gallery-medium-40.jpg",
72 DALI_IMAGE_DIR "gallery-medium-41.jpg",
73 DALI_IMAGE_DIR "gallery-medium-42.jpg",
74 DALI_IMAGE_DIR "gallery-medium-43.jpg",
75 DALI_IMAGE_DIR "gallery-medium-44.jpg",
76 DALI_IMAGE_DIR "gallery-medium-45.jpg",
77 DALI_IMAGE_DIR "gallery-medium-46.jpg",
78 DALI_IMAGE_DIR "gallery-medium-47.jpg",
79 DALI_IMAGE_DIR "gallery-medium-48.jpg",
80 DALI_IMAGE_DIR "gallery-medium-49.jpg",
81 DALI_IMAGE_DIR "gallery-medium-50.jpg",
82 DALI_IMAGE_DIR "gallery-medium-51.jpg",
83 DALI_IMAGE_DIR "gallery-medium-52.jpg",
84 DALI_IMAGE_DIR "gallery-medium-53.jpg",
87 const char* NINEPATCH_IMAGE_PATH[] = {
88 DALI_IMAGE_DIR "selection-popup-bg.1.9.png",
89 DALI_IMAGE_DIR "selection-popup-bg.2.9.png",
90 DALI_IMAGE_DIR "selection-popup-bg.3.9.png",
91 DALI_IMAGE_DIR "selection-popup-bg.4.9.png",
92 DALI_IMAGE_DIR "selection-popup-bg.5.9.png",
93 DALI_IMAGE_DIR "selection-popup-bg.6.9.png",
94 DALI_IMAGE_DIR "selection-popup-bg.7.9.png",
95 DALI_IMAGE_DIR "selection-popup-bg.8.9.png",
96 DALI_IMAGE_DIR "selection-popup-bg.9.9.png",
97 DALI_IMAGE_DIR "selection-popup-bg.10.9.png",
98 DALI_IMAGE_DIR "selection-popup-bg.11.9.png",
99 DALI_IMAGE_DIR "selection-popup-bg.12.9.png",
100 DALI_IMAGE_DIR "selection-popup-bg.13.9.png",
101 DALI_IMAGE_DIR "selection-popup-bg.14.9.png",
102 DALI_IMAGE_DIR "selection-popup-bg.15.9.png",
103 DALI_IMAGE_DIR "selection-popup-bg.16.9.png",
104 DALI_IMAGE_DIR "selection-popup-bg.17.9.png",
105 DALI_IMAGE_DIR "selection-popup-bg.18.9.png",
106 DALI_IMAGE_DIR "selection-popup-bg.19.9.png",
107 DALI_IMAGE_DIR "selection-popup-bg.20.9.png",
108 DALI_IMAGE_DIR "selection-popup-bg.21.9.png",
109 DALI_IMAGE_DIR "selection-popup-bg.22.9.png",
110 DALI_IMAGE_DIR "selection-popup-bg.23.9.png",
111 DALI_IMAGE_DIR "selection-popup-bg.24.9.png",
112 DALI_IMAGE_DIR "selection-popup-bg.25.9.png",
113 DALI_IMAGE_DIR "selection-popup-bg.26.9.png",
114 DALI_IMAGE_DIR "selection-popup-bg.27.9.png",
115 DALI_IMAGE_DIR "selection-popup-bg.28.9.png",
116 DALI_IMAGE_DIR "selection-popup-bg.29.9.png",
117 DALI_IMAGE_DIR "selection-popup-bg.30.9.png",
118 DALI_IMAGE_DIR "selection-popup-bg.31.9.png",
119 DALI_IMAGE_DIR "selection-popup-bg.32.9.png",
120 DALI_IMAGE_DIR "selection-popup-bg.33.9.png",
121 DALI_IMAGE_DIR "button-disabled.9.png",
122 DALI_IMAGE_DIR "button-down.9.png",
123 DALI_IMAGE_DIR "button-down-disabled.9.png",
124 DALI_IMAGE_DIR "button-up-1.9.png",
125 DALI_IMAGE_DIR "button-up-2.9.png",
126 DALI_IMAGE_DIR "button-up-3.9.png",
127 DALI_IMAGE_DIR "button-up-4.9.png",
128 DALI_IMAGE_DIR "button-up-5.9.png",
129 DALI_IMAGE_DIR "button-up-6.9.png",
130 DALI_IMAGE_DIR "button-up-7.9.png",
131 DALI_IMAGE_DIR "button-up-8.9.png",
132 DALI_IMAGE_DIR "button-up-9.9.png",
133 DALI_IMAGE_DIR "button-up-10.9.png",
134 DALI_IMAGE_DIR "button-up-11.9.png",
135 DALI_IMAGE_DIR "button-up-12.9.png",
136 DALI_IMAGE_DIR "button-up-13.9.png",
137 DALI_IMAGE_DIR "button-up-14.9.png",
138 DALI_IMAGE_DIR "button-up-15.9.png",
139 DALI_IMAGE_DIR "button-up-16.9.png",
140 DALI_IMAGE_DIR "button-up-17.9.png",
143 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATH) / sizeof(char*);
144 const unsigned int NUM_NINEPATCH_IMAGES = sizeof(NINEPATCH_IMAGE_PATH) / sizeof(char*);
146 const float ANIMATION_TIME ( 5.0f ); // animation length in seconds
148 struct VertexWithTexture
154 VertexWithTexture gQuadWithTexture[] = {
155 { Vector2( -0.5f, -0.5f ), Vector2( 0.0f, 0.0f ) },
156 { Vector2( 0.5f, -0.5f ), Vector2( 1.0f, 0.0f ) },
157 { Vector2( -0.5f, 0.5f ), Vector2( 0.0f, 1.0f ) },
158 { Vector2( 0.5f, 0.5f ), Vector2( 1.0f, 1.0f ) }
161 const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
162 attribute mediump vec2 aPosition;\n
163 attribute mediump vec2 aTexCoord;\n
164 uniform mediump mat4 uMvpMatrix;\n
165 uniform mediump vec3 uSize;\n
166 varying mediump vec2 vTexCoord;\n
169 vec4 position = vec4(aPosition,0.0,1.0)*vec4(uSize,1.0);\n
170 gl_Position = uMvpMatrix * position;\n
171 vTexCoord = aTexCoord;\n
175 const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
176 uniform lowp vec4 uColor;\n
177 uniform sampler2D sTexture;\n
178 varying mediump vec2 vTexCoord;\n
182 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
189 static Geometry mesh;
192 PropertyBuffer vertexBuffer;
193 Property::Map vertexFormat;
194 vertexFormat["aPosition"] = Property::VECTOR2;
195 vertexFormat["aTexCoord"] = Property::VECTOR2;
197 //Create a vertex buffer for vertex positions and texture coordinates
198 vertexBuffer = PropertyBuffer::New( vertexFormat, 4u );
199 vertexBuffer.SetData( gQuadWithTexture );
201 //Create the geometry
202 mesh = Geometry::New();
203 mesh.AddVertexBuffer( vertexBuffer );
204 mesh.SetGeometryType(Geometry::TRIANGLE_STRIP );
209 bool gUseMesh(false);
210 bool gUseImageActor(false);
211 bool gNinePatch(false);
212 unsigned int gRowsPerPage(25);
213 unsigned int gColumnsPerPage( 25 );
214 unsigned int gPageCount(13);
216 Renderer CreateRenderer( unsigned int index )
219 int numImages = !gNinePatch ? NUM_IMAGES : NUM_NINEPATCH_IMAGES;
220 static Renderer* renderers = new Renderer[numImages];
221 if( !renderers[index] )
223 //Create the renderer
224 Shader shader = Shader::New( VERTEX_SHADER_TEXTURE, FRAGMENT_SHADER_TEXTURE );
226 const char* imagePath = !gNinePatch ? IMAGE_PATH[index] : NINEPATCH_IMAGE_PATH[index];
227 Image image = ResourceImage::New(imagePath);
228 Material material = Material::New( shader );
229 material.AddTexture( image, "sTexture" );
230 material.SetBlendMode( BlendingMode::OFF );
231 renderers[index] = Renderer::New( QuadMesh(), material );
233 return renderers[index];
236 Actor CreateMeshActor( unsigned int index)
238 Renderer renderer = CreateRenderer(index);
239 Actor meshActor = Actor::New();
240 meshActor.AddRenderer( renderer );
245 // Test application to compare performance between ImageActor and ImageView
246 // By default, the application consist of 10 pages of 25x25 Image views, this can be modified using the following command line arguments:
247 // -r NumberOfRows (Modifies the number of rows per page)
248 // -c NumberOfColumns (Modifies the number of columns per page)
249 // -p NumberOfPages (Modifies the nimber of pages )
250 // --use-image-actor ( Use ImageActor instead of ImageView )
251 // --use-mesh ( Use new renderer API (as ImageView) but shares renderers between actors when possible )
252 // --nine-patch ( Use nine patch images )
255 class Benchmark : public ConnectionTracker
259 Benchmark( Application& application )
260 : mApplication( application ),
261 mRowsPerPage( gRowsPerPage ),
262 mColumnsPerPage( gColumnsPerPage ),
263 mPageCount( gPageCount )
265 // Connect to the Application's Init signal
266 mApplication.InitSignal().Connect( this, &Benchmark::Create );
271 // Nothing to do here;
274 // The Init signal is received once (only) during the Application lifetime
275 void Create( Application& application )
277 // Get a handle to the stage
278 Stage stage = Stage::GetCurrent();
279 stage.SetBackgroundColor( Color::WHITE );
280 Vector2 stageSize = stage.GetSize();
282 stage.GetRootLayer().SetDepthTestDisabled(true);
284 mSize = Vector3( stageSize.x / mColumnsPerPage, stageSize.y / mRowsPerPage, 0.0f );
286 // Respond to a click anywhere on the stage
287 stage.GetRootLayer().TouchedSignal().Connect( this, &Benchmark::OnTouch );
293 else if( gUseImageActor )
305 bool OnTouch( Actor actor, const TouchEvent& touch )
307 // quit the application
312 const char* ImagePath( int i )
314 return !gNinePatch ? IMAGE_PATH[i % NUM_IMAGES] : NINEPATCH_IMAGE_PATH[i % NUM_NINEPATCH_IMAGES];
317 void CreateImageActors()
319 Stage stage = Stage::GetCurrent();
320 unsigned int actorCount(mRowsPerPage*mColumnsPerPage * mPageCount);
321 mActor.resize(actorCount);
323 for( size_t i(0); i<actorCount; ++i )
325 Image image = ResourceImage::New(ImagePath(i));
326 mActor[i] = ImageActor::New(image);
327 mActor[i].SetSize(Vector3(0.0f,0.0f,0.0f));
328 mActor[i].SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
329 stage.Add(mActor[i]);
333 void CreateImageViews()
335 Stage stage = Stage::GetCurrent();
336 unsigned int actorCount(mRowsPerPage*mColumnsPerPage * mPageCount);
337 mImageView.resize(actorCount);
339 for( size_t i(0); i<actorCount; ++i )
341 mImageView[i] = ImageView::New(ImagePath(i));
342 mImageView[i].SetSize(Vector3(0.0f,0.0f,0.0f));
343 mImageView[i].SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
344 stage.Add(mImageView[i]);
348 void CreateMeshActors()
350 Stage stage = Stage::GetCurrent();
351 unsigned int actorCount(mRowsPerPage*mColumnsPerPage * mPageCount);
352 mActor.resize(actorCount);
353 for( size_t i(0); i<actorCount; ++i )
355 size_t numImages = !gNinePatch ? NUM_IMAGES : NUM_NINEPATCH_IMAGES;
356 mActor[i] = CreateMeshActor(i % numImages);
357 mActor[i].SetSize(0.0f,0.0f,0.0f);
359 stage.Add(mActor[i]);
363 void OnAnimationEnd( Animation& source )
365 if( source == mShow )
369 else if( source == mScroll )
381 Stage stage = Stage::GetCurrent();
382 Vector3 initialPosition( stage.GetSize().x * 0.5f, stage.GetSize().y*0.5f, 1000.0f );
384 unsigned int totalColumns = mColumnsPerPage * mPageCount;
388 mShow = Animation::New(0.0f);
390 float totalDuration( 10.0f );
391 float durationPerActor( 0.5f );
392 float delayBetweenActors = ( totalDuration - durationPerActor) / (mRowsPerPage*mColumnsPerPage);
393 for( size_t i(0); i<totalColumns; ++i )
397 for( size_t j(0);j<mRowsPerPage;++j)
403 float duration = 0.0f;
404 if( count < mRowsPerPage*mColumnsPerPage )
406 duration = durationPerActor;
407 delay = delayBetweenActors * count;
409 if( gUseImageActor || gUseMesh )
411 mActor[count].SetPosition( initialPosition );
412 mActor[count].SetSize( Vector3(0.0f,0.0f,0.0f) );
413 mActor[count].SetOrientation( Quaternion( Radian(0.0f),Vector3::XAXIS));
414 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 ));
415 mShow.AnimateTo( Property( mActor[count], Actor::Property::SIZE), mSize, AlphaFunction::EASE_OUT_BACK, TimePeriod( delay, duration ));
419 mImageView[count].SetPosition( initialPosition );
420 mImageView[count].SetSize( Vector3(0.0f,0.0f,0.0f) );
421 mImageView[count].SetOrientation( Quaternion( Radian(0.0f),Vector3::XAXIS));
422 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 ));
423 mShow.AnimateTo( Property( mImageView[count], Actor::Property::SIZE), mSize, AlphaFunction::EASE_OUT_BACK, TimePeriod( delay, duration ));
429 mShow.FinishedSignal().Connect( this, &Benchmark::OnAnimationEnd );
432 void ScrollAnimation()
434 Stage stage = Stage::GetCurrent();
435 Vector3 stageSize( stage.GetSize() );
437 mScroll = Animation::New(10.0f);
438 size_t actorCount( mRowsPerPage*mColumnsPerPage*mPageCount);
439 for( size_t i(0); i<actorCount; ++i )
441 if( gUseImageActor || gUseMesh )
443 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));
444 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));
445 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));
446 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));
450 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));
451 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));
452 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));
453 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));
457 mScroll.FinishedSignal().Connect( this, &Benchmark::OnAnimationEnd );
463 unsigned int actorsPerPage( mRowsPerPage*mColumnsPerPage );
464 mHide = Animation::New(0.0f);
466 unsigned int totalColumns = mColumnsPerPage * mPageCount;
468 float finalZ = Dali::Stage::GetCurrent().GetRenderTaskList().GetTask(0).GetCameraActor().GetCurrentWorldPosition().z;
469 float totalDuration( 5.0f);
470 float durationPerActor( 0.5f );
471 float delayBetweenActors = ( totalDuration - durationPerActor) / (mRowsPerPage*mColumnsPerPage);
472 for( size_t i(0); i<mRowsPerPage; ++i )
474 for( size_t j(0);j<totalColumns;++j)
477 float duration = 0.0f;
478 if( count < actorsPerPage )
480 duration = durationPerActor;
481 delay = delayBetweenActors * count;
484 if( gUseImageActor || gUseMesh )
486 mHide.AnimateTo( Property( mActor[count], Actor::Property::ORIENTATION), Quaternion( Radian( Degree( 70.0f ) ), Vector3::XAXIS ), AlphaFunction::EASE_OUT, TimePeriod( delay, duration ));
487 mHide.AnimateBy( Property( mActor[count], Actor::Property::POSITION_Z), finalZ, AlphaFunction::EASE_OUT_BACK, TimePeriod( delay +delayBetweenActors*actorsPerPage + duration, duration ));
491 mHide.AnimateTo( Property( mImageView[count], Actor::Property::ORIENTATION), Quaternion( Radian( Degree( 70.0f ) ), Vector3::XAXIS ), AlphaFunction::EASE_OUT, TimePeriod( delay, duration ));
492 mHide.AnimateBy( Property( mImageView[count], Actor::Property::POSITION_Z), finalZ, AlphaFunction::EASE_OUT_BACK, TimePeriod( delay +delayBetweenActors*actorsPerPage + duration, duration ));
499 mHide.FinishedSignal().Connect( this, &Benchmark::OnAnimationEnd );
503 Application& mApplication;
505 std::vector<Actor> mActor;
506 std::vector<ImageView> mImageView;
509 unsigned int mRowsPerPage;
510 unsigned int mColumnsPerPage;
511 unsigned int mPageCount;
518 void RunTest( Application& application )
520 Benchmark test( application );
522 application.MainLoop();
525 // Entry point for Linux & Tizen applications
527 int main( int argc, char **argv )
529 Application application = Application::New( &argc, &argv );
531 for( int i(1) ; i < argc; ++i )
533 std::string arg( argv[i] );
534 if( arg.compare("--use-mesh") == 0)
538 else if( arg.compare("--use-image-actor") == 0)
540 gUseImageActor = true;
542 else if( arg.compare("--nine-patch" ) == 0)
546 else if( arg.compare(0, 2, "-r" ) == 0)
548 gRowsPerPage = atoi( arg.substr( 2, arg.size()).c_str());
550 else if( arg.compare(0, 2, "-c" ) == 0)
552 gColumnsPerPage = atoi( arg.substr( 2, arg.size()).c_str());
554 else if( arg.compare(0, 2, "-p" ) == 0)
556 gPageCount = atoi( arg.substr( 2, arg.size()).c_str());
560 RunTest( application );