example: added a perf test to keep track of performance
[platform/core/uifw/lottie-player.git] / example / lottieviewtest.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
3
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22
23 #include "evasapp.h"
24 #include "lottieview.h"
25 #include<iostream>
26 #include <dirent.h>
27 #include <stdio.h>
28 #include <chrono>
29 using namespace std;
30
31 static Eina_Bool onTestDone(void *data);
32 /*
33  * To check the frame rate with rendermode off run
34  * ECORE_EVAS_FPS_DEBUG=1 ./lottieviewTest --disable-render
35  *
36  * To check the frame rate with  render backend
37  * ECORE_EVAS_FPS_DEBUG=1 ./lottieviewTest
38  *
39  */
40
41 class LottieViewTest
42 {
43 public:
44   LottieViewTest(EvasApp *app, Strategy st, double timeout) {
45       mStartTime = std::chrono::high_resolution_clock::now();
46       mStrategy = st;
47       mApp = app;
48
49       if (timeout > 0) {
50         ecore_timer_add(timeout, onTestDone, mApp);
51       }
52       // work around for 60fps
53       ecore_animator_frametime_set(1.0f/120.0f);
54   }
55
56   void show(int numberOfImage) {
57     auto resource = EvasApp::jsonFiles(std::string(DEMO_DIR));
58
59     if (resource.empty()) return;
60
61     int count = numberOfImage;
62     int colums = (int) ceil(sqrt(count));
63     int offset = 3;
64     int vw = (mApp->width() - (offset * colums))/colums;
65     int vh = vw;
66     int posx = offset;
67     int posy = offset;
68     int resourceSize = resource.size();
69     for (int i = 0 ; i < numberOfImage; i++) {
70         int index = i % resourceSize;
71         std::unique_ptr<LottieView> view(new LottieView(mApp->evas(), mStrategy));
72         view->setFilePath(resource[index].c_str());
73         view->setPos(posx, posy);
74         view->setSize(vw, vh);
75         view->show();
76         view->play();
77         view->loop(true);
78         //view->setRepeatMode(LottieView::RepeatMode::Reverse);
79         posx += vw+offset;
80         if ((mApp->width() - posx) < vw) {
81           posx = offset;
82           posy = posy + vh + offset;
83         }
84         mViews.push_back(std::move(view));
85     }
86   }
87
88     ~LottieViewTest() {
89       const auto frames = mViews.empty() ? 0 : mViews[0]->renderCount();
90       const double secs = std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - mStartTime).count();
91       std::cout<<"\tTestTime : "<< secs<<" sec \n\tTotalFrames : "<<frames<<"\n\tFramesPerSecond : "<< frames / secs <<" fps\n";
92     }
93
94   static int help() {
95             printf("Usage ./lottieviewTest [-s] [strategy] [-t] [timeout] [-c] [count]\n");
96             printf("\n \t-t : timeout duration in seconds\n");
97             printf("\n \t-c : number of resource in the grid\n");
98             printf("\n \t-s : Rendering Strategy\n");
99             printf("\t\t 0  - Test Lottie SYNC Renderer with CPP API\n");
100             printf("\t\t 1  - Test Lottie ASYNC Renderer with CPP API\n");
101             printf("\t\t 2  - Test Lottie SYNC Renderer with C API\n");
102             printf("\t\t 3  - Test Lottie ASYNC Renderer with C API\n");
103             printf("\t\t 4  - Test Lottie Tree Api using Efl VG Render\n");
104             printf(" Default : ./lottieviewTest -s 1 \n");
105             return 0;
106   }
107 public:
108   EvasApp     *mApp;
109   Strategy     mStrategy;
110   std::vector<std::unique_ptr<LottieView>>   mViews;
111   std::chrono::high_resolution_clock::time_point  mStartTime;
112 };
113
114 static void
115 onExitCb(void *data, void */*extra*/)
116 {
117     LottieViewTest *view = (LottieViewTest *)data;
118     delete view;
119 }
120
121
122 static Eina_Bool
123 onTestDone(void *data)
124 {
125     EvasApp *app = (EvasApp *)data;
126     app->exit();
127     return ECORE_CALLBACK_CANCEL;
128 }
129
130 int
131 main(int argc, char **argv)
132 {
133     Strategy st = Strategy::renderCppAsync;
134     auto index = 0;
135     double timeOut = 0;
136     size_t itemCount = 250;
137     while (index < argc) {
138       const char* option = argv[index];
139       index++;
140       if (!strcmp(option,"--help") || !strcmp(option,"-h")) {
141           return LottieViewTest::help();
142       } else if (!strcmp(option,"-s")) {
143          st = (index < argc) ? static_cast<Strategy>(atoi(argv[index])) : Strategy::renderCppAsync;
144          index++;
145       } else if (!strcmp(option,"-t")) {
146          timeOut = (index < argc) ? atoi(argv[index]) : 10;
147          index++;
148       } else if (!strcmp(option,"-c")) {
149          itemCount = (index < argc) ? atoi(argv[index]) : 10;
150          index++;
151       }
152     }
153
154    EvasApp *app = new EvasApp(800, 800);
155    app->setup();
156
157    LottieViewTest *view = new LottieViewTest(app, st, timeOut);
158    view->show(itemCount);
159
160    app->addExitCb(onExitCb, view);
161
162    app->run();
163    delete app;
164    return 0;
165 }
166
167
168
169
170