test: print current backend engine name
[platform/core/graphics/tizenvg.git] / test / testShape.cpp
1 #include <thorvg.h>
2 #include <iostream>
3 #include <Elementary.h>
4
5 using namespace std;
6
7 #define WIDTH 800
8 #define HEIGHT 800
9
10 unique_ptr<tvg::Paint> tvgDrawCmds()
11 {
12     //Prepare a Shape (Rectangle + Rectangle + Circle + Circle)
13     auto shape1 = tvg::Shape::gen();
14     shape1->appendRect(0, 0, 200, 200, 0);          //x, y, w, h, cornerRadius
15     shape1->appendRect(100, 100, 300, 300, 100);    //x, y, w, h, cornerRadius
16     shape1->appendCircle(400, 400, 100, 100);       //cx, cy, radiusW, radiusH
17     shape1->appendCircle(400, 500, 170, 100);       //cx, cy, radiusW, radiusH
18     shape1->fill(255, 255, 0, 255);                 //r, g, b, a
19
20     return move(shape1);
21 }
22
23 /************************************************************************/
24 /* Sw Engine Test Code                                                  */
25 /************************************************************************/
26
27 void tvgSwTest(uint32_t* buffer)
28 {
29     //Initialize ThorVG Engine
30     tvg::Initializer::init(tvg::CanvasEngine::Sw);
31
32     //Create a Canvas
33     auto canvas = tvg::SwCanvas::gen();
34     canvas->target(buffer, WIDTH, WIDTH, HEIGHT);
35
36     /* Push the shape into the Canvas drawing list
37        When this shape is into the canvas list, the shape could update & prepare
38        internal data asynchronously for coming rendering.
39        Canvas keeps this shape node unless user call canvas->clear() */
40     canvas->push(tvgDrawCmds());
41     canvas->draw();
42     canvas->sync();
43
44     //Terminate ThorVG Engine
45     tvg::Initializer::term(tvg::CanvasEngine::Sw);
46 }
47
48 /************************************************************************/
49 /* GL Engine Test Code                                                  */
50 /************************************************************************/
51
52 static unique_ptr<tvg::GlCanvas> canvas;
53
54 void initGLview(Evas_Object *obj)
55 {
56     static constexpr auto BPP = 4;
57
58     //Initialize ThorVG Engine
59     tvg::Initializer::init(tvg::CanvasEngine::Gl);
60
61     //Create a Canvas
62     canvas = tvg::GlCanvas::gen();
63     canvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
64
65     /* Push the shape into the Canvas drawing list
66        When this shape is into the canvas list, the shape could update & prepare
67        internal data asynchronously for coming rendering.
68        Canvas keeps this shape node unless user call canvas->clear() */
69     canvas->push(tvgDrawCmds());
70 }
71
72 void delGLview(Evas_Object *obj)
73 {
74     //Terminate ThorVG Engine
75     tvg::Initializer::term(tvg::CanvasEngine::Gl);
76 }
77
78 void drawGLview(Evas_Object *obj)
79 {
80     auto gl = elm_glview_gl_api_get(obj);
81     int w, h;
82     elm_glview_size_get(obj, &w, &h);
83     gl->glViewport(0, 0, w, h);
84     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
85     gl->glClear(GL_COLOR_BUFFER_BIT);
86     gl->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
87     gl->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
88     gl->glEnable(GL_BLEND);
89
90     canvas->draw();
91     canvas->sync();
92 }
93
94 /************************************************************************/
95 /* Common Infrastructure Code                                           */
96 /************************************************************************/
97
98 void win_del(void *data, Evas_Object *o, void *ev)
99 {
100    elm_exit();
101 }
102
103 int main(int argc, char **argv)
104 {
105     bool swEngine = true;
106
107     if (argc > 1) {
108         if (!strcmp(argv[1], "gl")) swEngine = false;
109     }
110
111     if (swEngine) cout << "engine: software" << endl;
112     else cout << "engine: opengl" << endl;
113
114     elm_init(argc, argv);
115
116     //Show the result using EFL...
117     elm_config_accel_preference_set("gl");
118
119     Eo* win = elm_win_util_standard_add(NULL, "ThorVG Test");
120     evas_object_smart_callback_add(win, "delete,request", win_del, 0);
121
122     Eo* viewer;
123
124     if (swEngine) {
125         static uint32_t buffer[WIDTH * HEIGHT];
126         viewer = evas_object_image_filled_add(evas_object_evas_get(win));
127         evas_object_image_size_set(viewer, WIDTH, HEIGHT);
128         evas_object_image_data_set(viewer, buffer);
129         evas_object_size_hint_weight_set(viewer, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
130         evas_object_show(viewer);
131         tvgSwTest(buffer);
132     //GlEngine
133     } else {
134         viewer = elm_glview_add(win);
135         evas_object_size_hint_weight_set(viewer, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
136         elm_glview_mode_set(viewer, ELM_GLVIEW_ALPHA);
137         elm_glview_resize_policy_set(viewer, ELM_GLVIEW_RESIZE_POLICY_RECREATE);
138         elm_glview_render_policy_set(viewer, ELM_GLVIEW_RENDER_POLICY_ON_DEMAND);
139         elm_glview_init_func_set(viewer, initGLview);
140         elm_glview_del_func_set(viewer, delGLview);
141         elm_glview_render_func_set(viewer, drawGLview);
142         evas_object_show(viewer);
143     }
144
145     elm_win_resize_object_add(win, viewer);
146     evas_object_geometry_set(win, 0, 0, WIDTH, HEIGHT);
147     evas_object_show(win);
148
149     elm_run();
150     elm_shutdown();
151 }