common paint: fix the wrong fast track logic.
[platform/core/graphics/tizenvg.git] / src / examples / Boundary2.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
3
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22
23 #include "Common.h"
24
25 /************************************************************************/
26 /* Drawing Commands                                                     */
27 /************************************************************************/
28
29 void tvgDrawCmds(tvg::Canvas* canvas)
30 {
31     if (!canvas) return;
32
33     canvas->reserve(6);             //reserve 6 pictures (optional)
34
35     //Picture0
36     auto picture0 = tvg::Picture::gen();
37     if (picture0->load(EXAMPLE_DIR"/test.jpg") == tvg::Result::Success) {
38         picture0->translate(-300,-300);
39         picture0->scale(3.0f);
40         if (canvas->push(move(picture0)) != tvg::Result::Success) return;
41     }
42
43     //Picture1
44     auto picture1 = tvg::Picture::gen();
45     if (picture1->load(EXAMPLE_DIR"/test.jpg") == tvg::Result::Success) {
46         picture1->translate(-200, -200);
47         if (canvas->push(move(picture1)) != tvg::Result::Success) return;
48     }
49
50     //Picture2
51     auto picture2 = tvg::Picture::gen();
52     if (picture2->load(EXAMPLE_DIR"/test.jpg") == tvg::Result::Success) {
53         picture2->translate(600, 600);
54         if (canvas->push(move(picture2)) != tvg::Result::Success) return;
55     }
56
57     //Picture3
58     auto picture3 = tvg::Picture::gen();
59     if (picture3->load(EXAMPLE_DIR"/test.jpg") == tvg::Result::Success) {
60         picture3->translate(-200, 600);
61         if (canvas->push(move(picture3)) != tvg::Result::Success) return;
62     }
63
64     //Picture4
65     auto picture4 = tvg::Picture::gen();
66     if (picture4->load(EXAMPLE_DIR"/test.jpg") == tvg::Result::Success) {
67         picture4->translate(600, -200);
68         if (canvas->push(move(picture4)) != tvg::Result::Success) return;
69     }
70 }
71
72 /************************************************************************/
73 /* Sw Engine Test Code                                                  */
74 /************************************************************************/
75
76 static unique_ptr<tvg::SwCanvas> swCanvas;
77
78 void tvgSwTest(uint32_t* buffer)
79 {
80     //Create a Canvas
81     swCanvas = tvg::SwCanvas::gen();
82     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
83
84     /* Push the shape into the Canvas drawing list
85        When this shape is into the canvas list, the shape could update & prepare
86        internal data asynchronously for coming rendering.
87        Canvas keeps this shape node unless user call canvas->clear() */
88     tvgDrawCmds(swCanvas.get());
89 }
90
91 void drawSwView(void* data, Eo* obj)
92 {
93     if (swCanvas->draw() == tvg::Result::Success) {
94         swCanvas->sync();
95     }
96 }
97
98
99 /************************************************************************/
100 /* GL Engine Test Code                                                  */
101 /************************************************************************/
102
103 static unique_ptr<tvg::GlCanvas> glCanvas;
104
105 void initGLview(Evas_Object *obj)
106 {
107     static constexpr auto BPP = 4;
108
109     //Create a Canvas
110     glCanvas = tvg::GlCanvas::gen();
111     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
112
113     /* Push the shape into the Canvas drawing list
114        When this shape is into the canvas list, the shape could update & prepare
115        internal data asynchronously for coming rendering.
116        Canvas keeps this shape node unless user call canvas->clear() */
117     tvgDrawCmds(glCanvas.get());
118 }
119
120 void drawGLview(Evas_Object *obj)
121 {
122     auto gl = elm_glview_gl_api_get(obj);
123     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
124     gl->glClear(GL_COLOR_BUFFER_BIT);
125
126     if (glCanvas->draw() == tvg::Result::Success) {
127         glCanvas->sync();
128     }
129 }
130
131
132 /************************************************************************/
133 /* Main Code                                                            */
134 /************************************************************************/
135
136 int main(int argc, char **argv)
137 {
138     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
139
140     if (argc > 1) {
141         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
142     }
143
144     //Initialize ThorVG Engine
145     if (tvgEngine == tvg::CanvasEngine::Sw) {
146         cout << "tvg engine: software" << endl;
147     } else {
148         cout << "tvg engine: opengl" << endl;
149     }
150
151     //Threads Count
152     auto threads = std::thread::hardware_concurrency();
153     if (threads > 0) --threads;    //Allow the designated main thread capacity
154
155     //Initialize ThorVG Engine
156     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
157
158         elm_init(argc, argv);
159
160         if (tvgEngine == tvg::CanvasEngine::Sw) {
161             createSwView();
162         } else {
163             createGlView();
164         }
165
166         elm_run();
167         elm_shutdown();
168
169         //Terminate ThorVG Engine
170         tvg::Initializer::term(tvgEngine);
171
172     } else {
173         cout << "engine is not supported" << endl;
174     }
175     return 0;
176 }