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