675d2ed7dc5d8ab80857beaad69b89285fd3e052
[platform/core/graphics/tizenvg.git] / src / examples / ImageScaleUp.cpp
1 /*
2  * Copyright (c) 2021 - 2022 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
24 #include "Common.h"
25
26 /************************************************************************/
27 /* Drawing Commands                                                     */
28 /************************************************************************/
29
30 static tvg::Picture* pPicture = nullptr;
31
32 void tvgDrawCmds(tvg::Canvas* canvas)
33 {
34     if (!canvas) return;
35
36     //Original
37     auto picture = tvg::Picture::gen();
38
39     if (picture->load(EXAMPLE_DIR"/scaleup.png") != tvg::Result::Success) {
40         cout << "The PNG file is not loaded correctly. Did you enable PNG Loader?" << endl;
41         return;
42     }
43     if (canvas->push(move(picture)) == tvg::Result::Success) {
44         pPicture = picture.get();
45     }
46 }
47
48 void tvgUpdateCmds(tvg::Canvas* canvas, float progress)
49 {
50     if (!canvas || !pPicture) return;
51
52     auto scale = 1.0f;
53
54     if (progress > 0.875f) scale = 4.0f;
55     else if (progress > 0.75f) scale = 3.0f;
56     else if (progress > 0.625f) scale = 2.5f;
57     else if (progress > 0.5f) scale = 2.0f;
58     else if (progress > 0.375f) scale = 1.75f;
59     else if (progress > 0.25f) scale = 1.5f;
60     else if (progress > 0.125f) scale = 1.25f;
61     else scale = 1.0f;
62
63     pPicture->scale(scale);
64
65     canvas->update(pPicture);
66 }
67
68 /************************************************************************/
69 /* Sw Engine Test Code                                                  */
70 /************************************************************************/
71
72 static unique_ptr<tvg::SwCanvas> swCanvas;
73
74 void tvgSwTest(uint32_t* buffer)
75 {
76     //Create a Canvas
77     swCanvas = tvg::SwCanvas::gen();
78     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
79
80     /* Push the shape into the Canvas drawing list
81        When this shape is into the canvas list, the shape could update & prepare
82        internal data asynchronously for coming rendering.
83        Canvas keeps this shape node unless user call canvas->clear() */
84     tvgDrawCmds(swCanvas.get());
85 }
86
87 void transitSwCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
88 {
89     tvgUpdateCmds(swCanvas.get(), progress);
90
91     //Update Efl Canvas
92     Eo* img = (Eo*) effect;
93     evas_object_image_data_update_add(img, 0, 0, WIDTH, HEIGHT);
94     evas_object_image_pixels_dirty_set(img, EINA_TRUE);
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     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
130     gl->glClear(GL_COLOR_BUFFER_BIT);
131
132     if (glCanvas->draw() == tvg::Result::Success) {
133         glCanvas->sync();
134     }
135 }
136
137 void transitGlCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
138 {
139     tvgUpdateCmds(glCanvas.get(), progress);
140     elm_glview_changed_set((Evas_Object*)effect);
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, 0) == tvg::Result::Success) {
165
166         elm_init(argc, argv);
167
168         Elm_Transit *transit = elm_transit_add();
169
170         if (tvgEngine == tvg::CanvasEngine::Sw) {
171             auto view = createSwView();
172             elm_transit_effect_add(transit, transitSwCb, view, nullptr);
173         } else {
174             auto view = createGlView();
175             elm_transit_effect_add(transit, transitGlCb, view, nullptr);
176         }
177
178         elm_transit_duration_set(transit, 7);
179         elm_transit_repeat_times_set(transit, -1);
180         elm_transit_auto_reverse_set(transit, EINA_TRUE);
181         elm_transit_go(transit);
182
183         elm_run();
184         elm_shutdown();
185
186         //Terminate ThorVG Engine
187         tvg::Initializer::term(tvgEngine);
188
189     } else {
190         cout << "engine is not supported" << endl;
191     }
192     return 0;
193 }