3db41b9d98a1bed65b8dd5c766303116795b6137
[platform/core/graphics/tizenvg.git] / test / testMultiShapes.cpp
1 #include "testCommon.h"
2
3 /************************************************************************/
4 /* Drawing Commands                                                     */
5 /************************************************************************/
6
7 void tvgDrawCmds(tvg::Canvas* canvas)
8 {
9     if (!canvas) return;
10
11     canvas->reserve(3);                          //reserve 3 shape nodes (optional)
12
13     //Prepare Round Rectangle
14     auto shape1 = tvg::Shape::gen();
15     shape1->appendRect(0, 0, 400, 400, 50, 50);  //x, y, w, h, rx, ry
16     shape1->fill(0, 255, 0, 255);                //r, g, b, a
17     if (canvas->push(move(shape1)) != tvg::Result::Success) return;
18
19     //Prepare Circle
20     auto shape2 = tvg::Shape::gen();
21     shape2->appendCircle(400, 400, 200, 200);    //cx, cy, radiusW, radiusH
22     shape2->fill(255, 255, 0, 255);              //r, g, b, a
23     if (canvas->push(move(shape2)) != tvg::Result::Success) return;
24
25     //Prepare Ellipse
26     auto shape3 = tvg::Shape::gen();
27     shape3->appendCircle(600, 600, 150, 100);    //cx, cy, radiusW, radiusH
28     shape3->fill(0, 255, 255, 255);              //r, g, b, a
29     if (canvas->push(move(shape3)) != tvg::Result::Success) return;
30 }
31
32
33 /************************************************************************/
34 /* Sw Engine Test Code                                                  */
35 /************************************************************************/
36
37 static unique_ptr<tvg::SwCanvas> swCanvas;
38
39 void tvgSwTest(uint32_t* buffer)
40 {
41     //Create a Canvas
42     swCanvas = tvg::SwCanvas::gen();
43     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
44
45     /* Push the shape into the Canvas drawing list
46        When this shape is into the canvas list, the shape could update & prepare
47        internal data asynchronously for coming rendering.
48        Canvas keeps this shape node unless user call canvas->clear() */
49     tvgDrawCmds(swCanvas.get());
50 }
51
52 void drawSwView(void* data, Eo* obj)
53 {
54     if (swCanvas->draw() == tvg::Result::Success) {
55         swCanvas->sync();
56     }
57 }
58
59
60 /************************************************************************/
61 /* GL Engine Test Code                                                  */
62 /************************************************************************/
63
64 static unique_ptr<tvg::GlCanvas> glCanvas;
65
66 void initGLview(Evas_Object *obj)
67 {
68     static constexpr auto BPP = 4;
69
70     //Create a Canvas
71     glCanvas = tvg::GlCanvas::gen();
72     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
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(glCanvas.get());
79 }
80
81 void drawGLview(Evas_Object *obj)
82 {
83     auto gl = elm_glview_gl_api_get(obj);
84     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
85     gl->glClear(GL_COLOR_BUFFER_BIT);
86
87     if (glCanvas->draw() == tvg::Result::Success) {
88         glCanvas->sync();
89     }
90 }
91
92
93 /************************************************************************/
94 /* Main Code                                                            */
95 /************************************************************************/
96
97 int main(int argc, char **argv)
98 {
99     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
100
101     if (argc > 1) {
102         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
103     }
104
105     //Initialize ThorVG Engine
106     if (tvgEngine == tvg::CanvasEngine::Sw) {
107         cout << "tvg engine: software" << endl;
108     } else {
109         cout << "tvg engine: opengl" << endl;
110     }
111
112     //Threads Count
113     auto threads = std::thread::hardware_concurrency();
114
115     //Initialize ThorVG Engine
116     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
117
118         elm_init(argc, argv);
119
120         if (tvgEngine == tvg::CanvasEngine::Sw) {
121             createSwView();
122         } else {
123             createGlView();
124         }
125
126         elm_run();
127         elm_shutdown();
128
129         //Terminate ThorVG Engine
130         tvg::Initializer::term(tvgEngine);
131
132     } else {
133         cout << "engine is not supported" << endl;
134     }
135     return 0;
136 }