Merge "SvgLoader: Inherit parent opacity" into tizen
[platform/core/graphics/tizenvg.git] / test / testScene.cpp
1 #include "testCommon.h"
2
3 /************************************************************************/
4 /* Drawing Commands                                                     */
5 /************************************************************************/
6
7 void tvgDrawCmds(tvg::Canvas* canvas)
8 {
9     //Create a Scene
10     auto scene = tvg::Scene::gen();
11     scene->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     scene->push(move(shape1));
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     scene->push(move(shape2));
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     scene->push(move(shape3));
30
31     //Create another Scene
32     auto scene2 = tvg::Scene::gen();
33     scene2->reserve(2);   //reserve 2 shape nodes (optional)
34
35     //Star
36     auto shape4 = tvg::Shape::gen();
37
38     //Appends Paths
39     shape4->moveTo(199, 34);
40     shape4->lineTo(253, 143);
41     shape4->lineTo(374, 160);
42     shape4->lineTo(287, 244);
43     shape4->lineTo(307, 365);
44     shape4->lineTo(199, 309);
45     shape4->lineTo(97, 365);
46     shape4->lineTo(112, 245);
47     shape4->lineTo(26, 161);
48     shape4->lineTo(146, 143);
49     shape4->close();
50     shape4->fill(0, 0, 255, 255);
51     scene2->push(move(shape4));
52
53     //Circle
54     auto shape5 = tvg::Shape::gen();
55
56     auto cx = 550.0f;
57     auto cy = 550.0f;
58     auto radius = 125.0f;
59     auto halfRadius = radius * 0.552284f;
60
61     //Append Paths
62     shape5->moveTo(cx, cy - radius);
63     shape5->cubicTo(cx + halfRadius, cy - radius, cx + radius, cy - halfRadius, cx + radius, cy);
64     shape5->cubicTo(cx + radius, cy + halfRadius, cx + halfRadius, cy + radius, cx, cy+ radius);
65     shape5->cubicTo(cx - halfRadius, cy + radius, cx - radius, cy + halfRadius, cx - radius, cy);
66     shape5->cubicTo(cx - radius, cy - halfRadius, cx - halfRadius, cy - radius, cx, cy - radius);
67     shape5->fill(255, 0, 0, 255);
68     scene2->push(move(shape5));
69
70     //Push scene2 onto the scene
71     scene->push(move(scene2));
72
73     //Draw the Scene onto the Canvas
74     canvas->push(move(scene));
75 }
76
77
78 /************************************************************************/
79 /* Sw Engine Test Code                                                  */
80 /************************************************************************/
81
82 static unique_ptr<tvg::SwCanvas> swCanvas;
83
84 void tvgSwTest(uint32_t* buffer)
85 {
86     //Create a Canvas
87     swCanvas = tvg::SwCanvas::gen();
88     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT);
89
90     /* Push the shape into the Canvas drawing list
91        When this shape is into the canvas list, the shape could update & prepare
92        internal data asynchronously for coming rendering.
93        Canvas keeps this shape node unless user call canvas->clear() */
94     tvgDrawCmds(swCanvas.get());
95 }
96
97 void drawSwView(void* data, Eo* obj)
98 {
99     if (swCanvas->draw() == tvg::Result::Success) {
100         swCanvas->sync();
101     }
102 }
103
104
105 /************************************************************************/
106 /* GL Engine Test Code                                                  */
107 /************************************************************************/
108
109 static unique_ptr<tvg::GlCanvas> glCanvas;
110
111 void initGLview(Evas_Object *obj)
112 {
113     static constexpr auto BPP = 4;
114
115     //Create a Canvas
116     glCanvas = tvg::GlCanvas::gen();
117     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
118
119     /* Push the shape into the Canvas drawing list
120        When this shape is into the canvas list, the shape could update & prepare
121        internal data asynchronously for coming rendering.
122        Canvas keeps this shape node unless user call canvas->clear() */
123     tvgDrawCmds(glCanvas.get());
124 }
125
126 void drawGLview(Evas_Object *obj)
127 {
128     auto gl = elm_glview_gl_api_get(obj);
129     int w, h;
130     elm_glview_size_get(obj, &w, &h);
131     gl->glViewport(0, 0, w, h);
132     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
133     gl->glClear(GL_COLOR_BUFFER_BIT);
134     gl->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
135     gl->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
136     gl->glEnable(GL_BLEND);
137
138     if (glCanvas->draw() == tvg::Result::Success) {
139         glCanvas->sync();
140     }
141 }
142
143
144 /************************************************************************/
145 /* Main Code                                                            */
146 /************************************************************************/
147
148 int main(int argc, char **argv)
149 {
150     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
151
152     if (argc > 1) {
153         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
154     }
155
156     //Initialize ThorVG Engine
157     if (tvgEngine == tvg::CanvasEngine::Sw) {
158         cout << "tvg engine: software" << endl;
159     } else {
160         cout << "tvg engine: opengl" << endl;
161     }
162
163     //Initialize ThorVG Engine
164     if (tvg::Initializer::init(tvgEngine) == tvg::Result::Success) {
165
166         elm_init(argc, argv);
167
168         if (tvgEngine == tvg::CanvasEngine::Sw) {
169             createSwView();
170         } else {
171             createGlView();
172         }
173
174         elm_run();
175         elm_shutdown();
176
177         //Terminate ThorVG Engine
178         tvg::Initializer::term(tvgEngine);
179
180     } else {
181         cout << "engine is not supported" << endl;
182     }
183     return 0;
184 }