4cc75a5101810075f4cb372f95cc255080c7cd36
[platform/core/uifw/lottie-player.git] / example / lottieviewtest.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the LGPL License, Version 2.1 (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  *     https://www.gnu.org/licenses/
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 #include "evasapp.h"
18 #include "lottieview.h"
19 #include<iostream>
20 #include <dirent.h>
21 #include <stdio.h>
22 using namespace std;
23
24 /*
25  * To check the frame rate with rendermode off run
26  * ECORE_EVAS_FPS_DEBUG=1 ./lottieviewTest --disable-render
27  *
28  * To check the frame rate with  render backend
29  * ECORE_EVAS_FPS_DEBUG=1 ./lottieviewTest
30  *
31  */
32
33 class LottieViewTest
34 {
35 public:
36   LottieViewTest(EvasApp *app, bool renderMode) {
37       mApp = app;
38       mRenderMode = renderMode;
39       ecore_animator_frametime_set(1.0/120.0);
40   }
41
42   void show(int numberOfImage) {
43     auto resource = EvasApp::jsonFiles(std::string(DEMO_DIR));
44
45     if (resource.empty()) return;
46
47     int count = numberOfImage;
48     int colums = (int) ceil(sqrt(count));
49     int offset = 3;
50     int vw = (mApp->width() - (offset * colums))/colums;
51     int vh = vw;
52     int posx = offset;
53     int posy = offset;
54     int resourceSize = resource.size();
55     for (int i = 0 ; i < numberOfImage; i++) {
56         int index = i % resourceSize;
57         std::unique_ptr<LottieView> view(new LottieView(mApp->evas(), mRenderMode));
58         view->setFilePath(resource[index].c_str());
59         view->setPos(posx, posy);
60         view->setSize(vw, vh);
61         view->show();
62         view->play();
63         view->loop(true);
64         //view->setRepeatMode(LottieView::RepeatMode::Reverse);
65
66         posx += vw+offset;
67         if ((mApp->width() - posx) < vw) {
68           posx = offset;
69           posy = posy + vh + offset;
70         }
71         mViews.push_back(std::move(view));
72     }
73   }
74
75   void render() {
76       //auto clock = std::chrono::high_resolution_clock::now();
77       for (auto &i : mViews) {
78           i->render();
79       }
80       //double d = std::chrono::duration<double, std::milli>(std::chrono::high_resolution_clock::now()-clock).count();
81       //printf("total time taken = %f\n", d);
82   }
83
84 public:
85   EvasApp     *mApp;
86   bool         mRenderMode = false;
87   std::vector<std::unique_ptr<LottieView>>   mViews;
88 };
89
90 static void
91 onExitCb(void *data, void *extra)
92 {
93     LottieViewTest *view = (LottieViewTest *)data;
94     delete view;
95 }
96
97 static void
98 onRenderPreCb(void *data, void *extra)
99 {
100     LottieViewTest *view = (LottieViewTest *)data;
101     view->render();
102 }
103
104 int
105 main(int argc, char **argv)
106 {
107    EvasApp *app = new EvasApp(800, 800);
108    app->setup();
109
110    bool renderMode = true;
111    if (argc > 1) {
112        if (!strcmp(argv[1],"--disable-render"))
113            renderMode = false;
114    }
115    LottieViewTest *view = new LottieViewTest(app, renderMode);
116    view->show(250);
117
118    app->addExitCb(onExitCb, view);
119    app->addRenderPreCb(onRenderPreCb, view);
120
121    app->run();
122    delete app;
123    return 0;
124 }
125
126
127
128
129