4 #include <Elementary.h>
9 /************************************************************************/
10 /* Drawing Commands */
11 /************************************************************************/
14 #define NUM_PER_LINE 4
17 static size_t count = 0;
18 static std::vector<unique_ptr<tvg::Canvas>> canvases;
21 void win_del(void *data, Evas_Object *o, void* ev)
27 void tvgDrawCmds(tvg::Canvas* canvas, const char* path, const char* name)
29 auto picture = tvg::Picture::gen();
32 sprintf(buf,"%s/%s", path, name);
34 if (picture->load(buf) != tvg::Result::Success) return;
37 picture->viewbox(&x, &y, &w, &h);
39 float rate = (SIZE/(w > h ? w : h));
49 y -= (SIZE - h) * 0.5f;
51 x -= (SIZE - w) * 0.5f;
53 picture->translate(-x, -y);
55 if (canvas->push(move(picture)) != tvg::Result::Success) return;
57 cout << "SVG: " << buf << endl;
63 /************************************************************************/
64 /* Sw Engine Test Code */
65 /************************************************************************/
67 void sw_del(void* data, Evas* evas, Eo* obj, void* ev)
69 auto buffer = (uint32_t*) data;
74 void drawSwView(void* data, Eo* obj)
76 auto i = reinterpret_cast<size_t>(data);
77 auto& canvas = canvases[i];
79 if (canvas->draw() == tvg::Result::Success) {
85 void tvgSwTest(const char* name, const char* path, void* data)
89 uint32_t* buffer = (uint32_t*) calloc(sizeof(uint32_t), SIZE * SIZE);
91 Eo* view = evas_object_image_filled_add(evas_object_evas_get(win));
92 evas_object_image_size_set(view, SIZE, SIZE);
93 evas_object_image_data_set(view, buffer);
94 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
95 evas_object_image_data_update_add(view, 0, 0, SIZE, SIZE);
96 evas_object_image_alpha_set(view, EINA_TRUE);
97 evas_object_image_pixels_get_callback_set(view, drawSwView, reinterpret_cast<void*>(count));
98 evas_object_event_callback_add(view, EVAS_CALLBACK_DEL, sw_del, buffer);
99 evas_object_resize(view, SIZE, SIZE);
100 evas_object_move(view, (count % NUM_PER_LINE) * SIZE, SIZE * (count / NUM_PER_LINE));
101 evas_object_show(view);
104 auto canvas = tvg::SwCanvas::gen();
105 canvas->target(buffer, SIZE, SIZE, SIZE, tvg::SwCanvas::ARGB8888);
107 tvgDrawCmds(canvas.get(), path, name);
109 canvases.push_back(move(canvas));
113 /************************************************************************/
114 /* GL Engine Test Code */
115 /************************************************************************/
124 void gl_del(void* data, Evas* evas, Eo* obj, void* ev)
126 auto objData = (ObjData*) data;
130 void initGLview(Evas_Object *obj)
132 auto objData = reinterpret_cast<ObjData*>(evas_object_data_get(obj, "objdata"));
133 objData->idx = count;
135 static constexpr auto BPP = 4;
138 auto canvas = tvg::GlCanvas::gen();
139 canvas->target(nullptr, SIZE * BPP, SIZE, SIZE);
141 /* Push the shape into the Canvas drawing list
142 When this shape is into the canvas list, the shape could update & prepare
143 internal data asynchronously for coming rendering.
144 Canvas keeps this shape node unless user call canvas->clear() */
145 tvgDrawCmds(canvas.get(), objData->path, objData->name);
147 canvases.push_back(move(canvas));
151 void drawGLview(Evas_Object *obj)
153 auto gl = elm_glview_gl_api_get(obj);
154 gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
155 gl->glClear(GL_COLOR_BUFFER_BIT);
157 auto objData = reinterpret_cast<ObjData*>(evas_object_data_get(obj, "objdata"));
158 auto& canvas = canvases[objData->idx];
160 if (canvas->draw() == tvg::Result::Success) {
166 void tvgGlTest(const char* name, const char* path, void* data)
168 auto objData = new ObjData;
169 objData->name = strdup(name);
170 objData->path = strdup(path);
172 Eo* win = (Eo*) data;
174 Eo* view = elm_glview_add(win);
175 elm_glview_mode_set(view, ELM_GLVIEW_ALPHA);
176 elm_glview_resize_policy_set(view, ELM_GLVIEW_RESIZE_POLICY_RECREATE);
177 elm_glview_render_policy_set(view, ELM_GLVIEW_RENDER_POLICY_ON_DEMAND);
178 elm_glview_init_func_set(view, initGLview);
179 elm_glview_render_func_set(view, drawGLview);
180 evas_object_data_set(view, "objdata", reinterpret_cast<void*>(objData));
181 evas_object_event_callback_add(view, EVAS_CALLBACK_DEL, gl_del, objData);
182 evas_object_resize(view, SIZE, SIZE);
183 evas_object_move(view, (count % NUM_PER_LINE) * SIZE, SIZE * (count / NUM_PER_LINE));
184 evas_object_show(view);
188 /************************************************************************/
190 /************************************************************************/
192 int main(int argc, char **argv)
194 tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
197 if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
200 //Initialize ThorVG Engine
201 if (tvgEngine == tvg::CanvasEngine::Sw) {
202 cout << "tvg engine: software" << endl;
204 cout << "tvg engine: opengl" << endl;
208 auto threads = std::thread::hardware_concurrency();
210 //Initialize ThorVG Engine
211 if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
213 elm_init(argc, argv);
215 Eo* win = elm_win_util_standard_add(NULL, "ThorVG Test");
216 evas_object_smart_callback_add(win, "delete,request", win_del, 0);
218 if (tvgEngine == tvg::CanvasEngine::Sw) {
219 eina_file_dir_list("./svgs", EINA_TRUE, tvgSwTest, win);
221 eina_file_dir_list("./svgs", EINA_TRUE, tvgGlTest, win);
224 evas_object_geometry_set(win, 0, 0, WIDTH, HEIGHT);
225 evas_object_show(win);
230 //Terminate ThorVG Engine
231 tvg::Initializer::term(tvg::CanvasEngine::Sw);
234 cout << "engine is not supported" << endl;