68623dc327814cc13f66ad6a670abf9249c55ddb
[platform/core/graphics/tizenvg.git] / test / testBlending.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(5);
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, 170);              //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(400, 400, 250, 100);    //cx, cy, radiusW, radiusH
28     shape3->fill(255, 255, 255, 100);            //r, g, b, a
29     if (canvas->push(move(shape3)) != tvg::Result::Success) return;
30
31     //Prepare Star
32     auto shape4 = tvg::Shape::gen();
33     shape4->moveTo(199, 234);
34     shape4->lineTo(253, 343);
35     shape4->lineTo(374, 360);
36     shape4->lineTo(287, 444);
37     shape4->lineTo(307, 565);
38     shape4->lineTo(199, 509);
39     shape4->lineTo(97, 565);
40     shape4->lineTo(112, 445);
41     shape4->lineTo(26, 361);
42     shape4->lineTo(146, 343);
43     shape4->close();
44     shape4->fill(255, 0, 200, 200);
45     if (canvas->push(move(shape4)) != tvg::Result::Success) return;
46
47     //Prepare Opaque Ellipse
48     auto shape5 = tvg::Shape::gen();
49     shape5->appendCircle(600, 650, 200, 150);
50     shape5->fill(0, 0, 255, 255);
51     if (canvas->push(move(shape5)) != tvg::Result::Success) return;
52 }
53
54
55 /************************************************************************/
56 /* Sw Engine Test Code                                                  */
57 /************************************************************************/
58
59 static unique_ptr<tvg::SwCanvas> swCanvas;
60
61 void tvgSwTest(uint32_t* buffer)
62 {
63     //Create a Canvas
64     swCanvas = tvg::SwCanvas::gen();
65     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
66
67     /* Push the shape into the Canvas drawing list
68        When this shape is into the canvas list, the shape could update & prepare
69        internal data asynchronously for coming rendering.
70        Canvas keeps this shape node unless user call canvas->clear() */
71     tvgDrawCmds(swCanvas.get());
72 }
73
74 void drawSwView(void* data, Eo* obj)
75 {
76     if (swCanvas->draw() == tvg::Result::Success) {
77         swCanvas->sync();
78     }
79 }
80
81
82 /************************************************************************/
83 /* GL Engine Test Code                                                  */
84 /************************************************************************/
85
86 static unique_ptr<tvg::GlCanvas> glCanvas;
87
88 void initGLview(Evas_Object *obj)
89 {
90     static constexpr auto BPP = 4;
91
92     //Create a Canvas
93     glCanvas = tvg::GlCanvas::gen();
94     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
95
96     /* Push the shape into the Canvas drawing list
97        When this shape is into the canvas list, the shape could update & prepare
98        internal data asynchronously for coming rendering.
99        Canvas keeps this shape node unless user call canvas->clear() */
100     tvgDrawCmds(glCanvas.get());
101 }
102
103 void drawGLview(Evas_Object *obj)
104 {
105     auto gl = elm_glview_gl_api_get(obj);
106     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
107     gl->glClear(GL_COLOR_BUFFER_BIT);
108
109     if (glCanvas->draw() == tvg::Result::Success) {
110         glCanvas->sync();
111     }
112 }
113
114
115 /************************************************************************/
116 /* Main Code                                                            */
117 /************************************************************************/
118
119 int main(int argc, char **argv)
120 {
121     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
122
123     if (argc > 1) {
124         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
125     }
126
127     //Initialize ThorVG Engine
128     if (tvgEngine == tvg::CanvasEngine::Sw) {
129         cout << "tvg engine: software" << endl;
130     } else {
131         cout << "tvg engine: opengl" << endl;
132     }
133
134     //Threads Count
135     auto threads = std::thread::hardware_concurrency();
136
137     //Initialize ThorVG Engine
138     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
139
140         elm_init(argc, argv);
141
142         if (tvgEngine == tvg::CanvasEngine::Sw) {
143             createSwView();
144         } else {
145             createGlView();
146         }
147
148         elm_run();
149         elm_shutdown();
150
151         //Terminate ThorVG Engine
152         tvg::Initializer::term(tvgEngine);
153
154     } else {
155         cout << "engine is not supported" << endl;
156     }
157     return 0;
158 }