updated copyright.
[platform/core/graphics/tizenvg.git] / src / examples / Performance.cpp
1 /*
2  * Copyright (c) 2021 - 2023 the ThorVG project. 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 "Common.h"
24
25 /************************************************************************/
26 /* Drawing Commands                                                     */
27 /************************************************************************/
28
29 static tvg::Picture* pPicture = nullptr;
30 static double updateTime = 0;
31 static double accumulateTime = 0;
32 static uint32_t cnt = 0;
33
34 void tvgDrawCmds(tvg::Canvas* canvas)
35 {
36     if (!canvas) return;
37
38     auto picture = tvg::Picture::gen();
39
40     picture->load(EXAMPLE_DIR"/tiger.svg");
41     picture->size(WIDTH, HEIGHT);
42     pPicture = picture.get();
43     canvas->push(move(picture));
44 }
45
46 void tvgUpdateCmds(tvg::Canvas* canvas, float progress)
47 {
48     if (!canvas) return;
49
50     pPicture->translate(WIDTH * progress * 0.05f, HEIGHT * progress * 0.05f);
51
52     auto before = ecore_time_get();
53
54     canvas->update(pPicture);
55
56     auto after = ecore_time_get();
57
58     updateTime = after - before;
59 }
60
61
62 /************************************************************************/
63 /* Sw Engine Test Code                                                  */
64 /************************************************************************/
65
66 static unique_ptr<tvg::SwCanvas> swCanvas;
67
68 void tvgSwTest(uint32_t* buffer)
69 {
70     //Create a Canvas
71     swCanvas = tvg::SwCanvas::gen();
72     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
73
74     /* Push the shape into the Canvas drawing list
75        When this shape is into the canvas list, the shape could update & prepare
76        internal data asynchronously for coming rendering.
77        Canvas keeps this shape node unless user call canvas->clear() */
78     tvgDrawCmds(swCanvas.get());
79 }
80
81 void transitSwCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
82 {
83     tvgUpdateCmds(swCanvas.get(), progress);
84
85     //Update Efl Canvas
86     Eo* img = (Eo*) effect;
87     evas_object_image_data_update_add(img, 0, 0, WIDTH, HEIGHT);
88     evas_object_image_pixels_dirty_set(img, EINA_TRUE);
89 }
90
91 void drawSwView(void* data, Eo* obj)
92 {
93     auto before = ecore_time_get();
94
95     if (swCanvas->draw() == tvg::Result::Success) {
96         swCanvas->sync();
97     }
98
99     auto after = ecore_time_get();
100
101     auto rasterTime = after - before;
102
103     ++cnt;
104
105     accumulateTime += (updateTime + rasterTime);
106
107     printf("[%5d]: rendering = %fs,  average = %fs\n", cnt, updateTime + rasterTime, accumulateTime / cnt);
108 }
109
110
111 /************************************************************************/
112 /* GL Engine Test Code                                                  */
113 /************************************************************************/
114
115 static unique_ptr<tvg::GlCanvas> glCanvas;
116
117 void initGLview(Evas_Object *obj)
118 {
119     static constexpr auto BPP = 4;
120
121     //Create a Canvas
122     glCanvas = tvg::GlCanvas::gen();
123     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
124
125     /* Push the shape into the Canvas drawing list
126        When this shape is into the canvas list, the shape could update & prepare
127        internal data asynchronously for coming rendering.
128        Canvas keeps this shape node unless user call canvas->clear() */
129     tvgDrawCmds(glCanvas.get());
130 }
131
132 void drawGLview(Evas_Object *obj)
133 {
134     auto gl = elm_glview_gl_api_get(obj);
135     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
136     gl->glClear(GL_COLOR_BUFFER_BIT);
137
138     if (glCanvas->draw() == tvg::Result::Success) {
139         glCanvas->sync();
140     }
141 }
142
143 void transitGlCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
144 {
145     tvgUpdateCmds(glCanvas.get(), progress);
146     elm_glview_changed_set((Evas_Object*)effect);
147 }
148
149
150 /************************************************************************/
151 /* Main Code                                                            */
152 /************************************************************************/
153
154 int main(int argc, char **argv)
155 {
156     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
157
158     if (argc > 1) {
159         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
160     }
161
162     //Initialize ThorVG Engine
163     if (tvgEngine == tvg::CanvasEngine::Sw) {
164         cout << "tvg engine: software" << endl;
165     } else {
166         cout << "tvg engine: opengl" << endl;
167     }
168
169     //Initialize ThorVG Engine
170     if (tvg::Initializer::init(tvgEngine, 0) == tvg::Result::Success) {
171
172         elm_init(argc, argv);
173
174         Elm_Transit *transit = elm_transit_add();
175
176         if (tvgEngine == tvg::CanvasEngine::Sw) {
177             auto view = createSwView();
178             elm_transit_effect_add(transit, transitSwCb, view, nullptr);
179         } else {
180             auto view = createGlView();
181             elm_transit_effect_add(transit, transitGlCb, view, nullptr);
182         }
183
184         elm_transit_duration_set(transit, 2);
185         elm_transit_repeat_times_set(transit, -1);
186         elm_transit_auto_reverse_set(transit, EINA_TRUE);
187         elm_transit_go(transit);
188
189         elm_run();
190         elm_shutdown();
191
192         //Terminate ThorVG Engine
193         tvg::Initializer::term(tvgEngine);
194
195     } else {
196         cout << "engine is not supported" << endl;
197     }
198     return 0;
199 }