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