1 #include "testCommon.h"
3 /************************************************************************/
5 /************************************************************************/
6 tvg::Shape* pShape = nullptr;
7 tvg::Shape* pShape2 = nullptr;
8 tvg::Shape* pShape3 = nullptr;
10 void tvgDrawCmds(tvg::Canvas* canvas)
15 auto shape = tvg::Shape::gen();
17 /* Acquire shape pointer to access it again.
18 instead, you should consider not to interrupt this pointer life-cycle. */
21 shape->appendRect(-285, -300, 200, 200, 0, 0);
22 shape->appendRect(-185, -200, 300, 300, 100, 100);
23 shape->appendCircle(115, 100, 100, 100);
24 shape->appendCircle(115, 200, 170, 100);
27 auto fill = tvg::LinearGradient::gen();
28 fill->linear(-285, -300, 285, 300);
30 //Gradient Color Stops
31 tvg::Fill::ColorStop colorStops[3];
32 colorStops[0] = {0, 255, 0, 0, 255};
33 colorStops[1] = {0.5, 255, 255, 0, 255};
34 colorStops[2] = {1, 255, 255, 255, 255};
36 fill->colorStops(colorStops, 3);
37 shape->fill(move(fill));
38 shape->translate(385, 400);
39 if (canvas->push(move(shape)) != tvg::Result::Success) return;
42 auto shape2 = tvg::Shape::gen();
43 pShape2 = shape2.get();
44 shape2->appendRect(-50, -50, 100, 100, 0, 0);
45 shape2->translate(400, 400);
48 auto fill2 = tvg::LinearGradient::gen();
49 fill2->linear(-50, -50, 50, 50);
51 //Gradient Color Stops
52 tvg::Fill::ColorStop colorStops2[2];
53 colorStops2[0] = {0, 0, 0, 0, 255};
54 colorStops2[1] = {1, 255, 255, 255, 255};
56 fill2->colorStops(colorStops2, 2);
57 shape2->fill(move(fill2));
58 if (canvas->push(move(shape2)) != tvg::Result::Success) return;
61 auto shape3 = tvg::Shape::gen();
62 pShape3 = shape3.get();
64 /* Look, how shape3's origin is different with shape2
65 The center of the shape is the anchor point for transformation. */
66 shape3->appendRect(100, 100, 150, 100, 20, 20);
69 auto fill3 = tvg::RadialGradient::gen();
70 fill3->radial(175, 150, 75);
72 //Gradient Color Stops
73 tvg::Fill::ColorStop colorStops3[4];
74 colorStops3[0] = {0, 0, 127, 0, 127};
75 colorStops3[1] = {0.25, 0, 170, 170, 170};
76 colorStops3[2] = {0.5, 200, 0, 200, 200};
77 colorStops3[3] = {1, 255, 255, 255, 255};
79 fill3->colorStops(colorStops3, 4);
81 shape3->fill(move(fill3));
82 shape3->translate(400, 400);
83 if (canvas->push(move(shape3)) != tvg::Result::Success) return;
86 void tvgUpdateCmds(tvg::Canvas* canvas, float progress)
90 /* Update shape directly.
91 You can update only necessary properties of this shape,
92 while retaining other properties. */
95 pShape->scale(1 - 0.75 * progress);
96 pShape->rotate(360 * progress);
98 //Update shape for drawing (this may work asynchronously)
99 if (canvas->update(pShape) != tvg::Result::Success) return;
102 pShape2->rotate(360 * progress);
103 pShape2->translate(400 + progress * 300, 400);
104 if (canvas->update(pShape2) != tvg::Result::Success) return;
107 pShape3->rotate(-360 * progress);
108 pShape3->scale(0.5 + progress);
109 if (canvas->update(pShape3) != tvg::Result::Success) return;
113 /************************************************************************/
114 /* Sw Engine Test Code */
115 /************************************************************************/
117 static unique_ptr<tvg::SwCanvas> swCanvas;
119 void tvgSwTest(uint32_t* buffer)
122 swCanvas = tvg::SwCanvas::gen();
123 swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
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(swCanvas.get());
132 void transitSwCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
134 tvgUpdateCmds(swCanvas.get(), progress);
137 Eo* img = (Eo*) effect;
138 evas_object_image_data_update_add(img, 0, 0, WIDTH, HEIGHT);
139 evas_object_image_pixels_dirty_set(img, EINA_TRUE);
142 void drawSwView(void* data, Eo* obj)
144 if (swCanvas->draw() == tvg::Result::Success) {
150 /************************************************************************/
151 /* GL Engine Test Code */
152 /************************************************************************/
154 static unique_ptr<tvg::GlCanvas> glCanvas;
156 void initGLview(Evas_Object *obj)
158 static constexpr auto BPP = 4;
161 glCanvas = tvg::GlCanvas::gen();
162 glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
164 /* Push the shape into the Canvas drawing list
165 When this shape is into the canvas list, the shape could update & prepare
166 internal data asynchronously for coming rendering.
167 Canvas keeps this shape node unless user call canvas->clear() */
168 tvgDrawCmds(glCanvas.get());
171 void drawGLview(Evas_Object *obj)
173 auto gl = elm_glview_gl_api_get(obj);
174 gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
175 gl->glClear(GL_COLOR_BUFFER_BIT);
177 if (glCanvas->draw() == tvg::Result::Success) {
182 void transitGlCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
184 tvgUpdateCmds(glCanvas.get(), progress);
188 /************************************************************************/
190 /************************************************************************/
192 int main(int argc, char **argv)
194 tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
197 if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
200 //Initialize ThorVG Engine
201 if (tvgEngine == tvg::CanvasEngine::Sw) {
202 cout << "tvg engine: software" << endl;
204 cout << "tvg engine: opengl" << endl;
208 auto threads = std::thread::hardware_concurrency();
210 //Initialize ThorVG Engine
211 if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
213 elm_init(argc, argv);
215 Elm_Transit *transit = elm_transit_add();
217 if (tvgEngine == tvg::CanvasEngine::Sw) {
218 auto view = createSwView();
219 elm_transit_effect_add(transit, transitSwCb, view, nullptr);
221 auto view = createGlView();
222 elm_transit_effect_add(transit, transitGlCb, view, nullptr);
225 elm_transit_duration_set(transit, 2);
226 elm_transit_repeat_times_set(transit, -1);
227 elm_transit_auto_reverse_set(transit, EINA_TRUE);
228 elm_transit_go(transit);
233 //Terminate ThorVG Engine
234 tvg::Initializer::term(tvgEngine);
237 cout << "engine is not supported" << endl;