1 #include <Ecore_Evas.h>
7 #include <livebox-service.h>
8 #include <livebox-errno.h>
13 #define QUALITY_N_COMPRESS "quality=100 compress=1"
14 #define PUBLIC __attribute__((visibility("default")))
16 struct snapshot_info {
18 void (*flush_cb)(Evas_Object *snapshot_window, const char *id, int status, void *data);
21 Ecore_Timer *flush_timer;
27 static void post_render_cb(void *data, Evas *e, void *event_info);
28 static void pre_render_cb(void *data, Evas *e, void *event_info);
32 static inline Evas *create_virtual_canvas(int w, int h)
34 Ecore_Evas *internal_ee;
37 // Create virtual canvas
38 internal_ee = ecore_evas_buffer_new(w, h);
40 LOGD("Failed to create a new canvas buffer\n");
44 ecore_evas_alpha_set(internal_ee, EINA_TRUE);
45 ecore_evas_manual_render_set(internal_ee, EINA_FALSE);
47 // Get the "Evas" object from a virtual canvas
48 internal_e = ecore_evas_get(internal_ee);
50 ecore_evas_free(internal_ee);
51 LOGD("Faield to get Evas object\n");
60 static inline int flush_data_to_file(Evas *e, char *data, const char *filename, int w, int h)
64 output = evas_object_image_add(e);
66 LOGD("Failed to create an image object\n");
67 return LB_STATUS_ERROR_FAULT;
70 evas_object_image_data_set(output, NULL);
71 evas_object_image_colorspace_set(output, EVAS_COLORSPACE_ARGB8888);
72 evas_object_image_alpha_set(output, EINA_TRUE);
73 evas_object_image_size_set(output, w, h);
74 evas_object_image_smooth_scale_set(output, EINA_TRUE);
75 evas_object_image_data_set(output, data);
76 evas_object_image_fill_set(output, 0, 0, w, h);
77 evas_object_image_data_update_add(output, 0, 0, w, h);
79 if (evas_object_image_save(output, filename, NULL, QUALITY_N_COMPRESS) == EINA_FALSE) {
80 evas_object_del(output);
81 LOGD("Faield to save a captured image (%s)\n", filename);
82 return LB_STATUS_ERROR_IO;
85 evas_object_del(output);
87 if (access(filename, F_OK) != 0) {
88 LOGD("File %s is not found\n", filename);
89 return LB_STATUS_ERROR_IO;
92 LOGD("Flush data to a file (%s)\n", filename);
93 return LB_STATUS_SUCCESS;
98 static inline int destroy_virtual_canvas(Evas *e)
102 ee = ecore_evas_ecore_evas_get(e);
104 LOGD("Failed to ecore evas object\n");
105 return LB_STATUS_ERROR_FAULT;
109 return LB_STATUS_SUCCESS;
114 static inline int flush_to_file(void *canvas, const char *filename, int w, int h)
120 shot_e = create_virtual_canvas(w, h);
122 LOGE("Unable to create a new virtual window\n");
123 return LB_STATUS_ERROR_FAULT;
126 shot_ee = ecore_evas_ecore_evas_get(shot_e);
128 LOGE("Unable to get Ecore_Evas\n");
129 destroy_virtual_canvas(shot_e);
130 return LB_STATUS_ERROR_FAULT;
133 ecore_evas_manual_render_set(shot_ee, EINA_TRUE);
135 status = flush_data_to_file(shot_e, canvas, filename, w, h);
136 destroy_virtual_canvas(shot_e);
143 static void del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
145 struct snapshot_info *info;
147 info = evas_object_data_del(obj, "snapshot,info");
151 LOGD("Delete object (%s)\n", info->id);
153 if (info->flush_cb) {
154 info->flush_cb(obj, info->id, LB_STATUS_ERROR_CANCEL, info->data);
155 LOGD("Flush is canceled\n");
160 * Render callback will be deleted.
162 destroy_virtual_canvas(e);
169 static Eina_Bool direct_snapshot_cb(void *data)
177 void (*flush_cb)(Evas_Object *snapshot_window, const char *id, int status, void *data);
178 Evas_Object *snapshot_win = data;
179 struct snapshot_info *info;
181 info = evas_object_data_get(snapshot_win, "snapshot,info");
183 LOGE("Unable to get snapshot info\n");
184 return ECORE_CALLBACK_CANCEL;
187 info->flush_timer = NULL;
188 flush_cb = info->flush_cb;
189 info->flush_cb = NULL; /* To prevent call this from the delete callback */
191 e = evas_object_evas_get(snapshot_win);
193 LOGE("Invalid object, failed to get Evas\n");
195 flush_cb(snapshot_win, info->id, LB_STATUS_ERROR_FAULT, info->data);
196 return ECORE_CALLBACK_CANCEL;
199 ee = ecore_evas_ecore_evas_get(e);
201 LOGE("Unable to get Ecore_Evas object\n");
203 flush_cb(snapshot_win, info->id, LB_STATUS_ERROR_FAULT, info->data);
204 return ECORE_CALLBACK_CANCEL;
207 ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
208 ecore_evas_manual_render_set(ee, EINA_TRUE);
210 canvas = ecore_evas_buffer_pixels_get(ee);
212 LOGE("Failed to get canvas\n");
214 flush_cb(snapshot_win, info->id, LB_STATUS_ERROR_FAULT, info->data);
215 return ECORE_CALLBACK_CANCEL;
218 status = flush_to_file(canvas, info->id, w, h);
220 flush_cb(snapshot_win, info->id, status, info->data);
221 return ECORE_CALLBACK_CANCEL;
226 static Eina_Bool snapshot_cb(void *data)
228 Evas_Object *snapshot_win = data;
229 struct snapshot_info *info;
233 void (*flush_cb)(Evas_Object *snapshot_window, const char *id, int status, void *data);
235 info = evas_object_data_get(snapshot_win, "snapshot,info");
237 LOGE("Invalid object\n");
238 return ECORE_CALLBACK_CANCEL;
241 info->flush_timer = NULL;
242 flush_cb = info->flush_cb;
243 info->flush_cb = NULL; /* To prevent call this from the delete callback */
245 e = evas_object_evas_get(snapshot_win);
247 LOGE("Invalid object\n");
249 flush_cb(snapshot_win, info->id, LB_STATUS_ERROR_FAULT, info->data);
250 return ECORE_CALLBACK_CANCEL;
253 ee = ecore_evas_ecore_evas_get(e);
255 LOGE("Invalid object (ee)\n");
257 flush_cb(snapshot_win, info->id, LB_STATUS_ERROR_FAULT, info->data);
258 return ECORE_CALLBACK_CANCEL;
261 canvas = (void*)ecore_evas_buffer_pixels_get(ee);
263 LOGD("Failed to get pixel canvas\n");
265 flush_cb(snapshot_win, info->id, LB_STATUS_ERROR_FAULT, info->data);
266 return ECORE_CALLBACK_CANCEL;
274 ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
276 LOGD("Flush size: %dx%d\n", w, h);
277 status = flush_to_file(canvas, info->id, w, h);
279 flush_cb(snapshot_win, info->id, status, info->data);
281 * Do not access info after this.
285 return ECORE_CALLBACK_CANCEL;
290 static void post_render_cb(void *data, Evas *e, void *event_info)
292 Evas_Object *snapshot_win = data;
293 struct snapshot_info *info;
295 info = evas_object_data_get(snapshot_win, "snapshot,info");
297 LOGE("snapshot info is not valid\n");
303 if (info->flush_timer) {
305 * This has not to be happens.
307 LOGE("Flush timer is not cleared\n");
308 ecore_timer_del(info->flush_timer);
309 info->flush_timer = NULL;
312 if (!info->flush_cb) {
313 LOGD("Flush request is not initiated yet\n");
319 * Even if tehre is no timer registered, we should capture the content
320 * from out of this callback.
321 * Or we can met unexpected problems.
322 * To avoid it, use the 0.0001f to get timer callback ASAP, not in this function.
324 LOGD("Fire the flush timer %lf (%d)\n", info->timeout, info->render_cnt);
325 info->flush_timer = ecore_timer_add(info->timeout, snapshot_cb, snapshot_win);
326 if (!info->flush_timer) {
327 void (*flush_cb)(Evas_Object *snapshot_window, const char *id, int status, void *data);
329 LOGE("Unalbe to add timer for getting the snapshot\n");
330 flush_cb = info->flush_cb;
331 info->flush_cb = NULL;
333 flush_cb(snapshot_win, info->id, LB_STATUS_ERROR_FAULT, info->data);
336 * Do not access info after from here.
343 static void pre_render_cb(void *data, Evas *e, void *event_info)
345 Evas_Object *snapshot_win = data;
346 struct snapshot_info *info;
348 info = evas_object_data_get(snapshot_win, "snapshot,info");
350 LOGE("snapshot info is not valid\n");
354 if (info->flush_timer) {
355 ecore_timer_del(info->flush_timer);
356 info->flush_timer = NULL;
357 LOGD("Clear the flush timer\n");
360 LOGD("Pre-render callback\n");
365 static void resize_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
373 ee = ecore_evas_ecore_evas_get(e);
377 ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
378 evas_object_geometry_get(obj, NULL, NULL, &ow, &oh);
379 if (ow == w && oh == h) {
380 LOGD("Size is not changed: %dx%d\n", w, h);
385 * Box(parent object) is resized.
386 * Try to resize the canvas too.
388 ecore_evas_resize(ee, w, h);
389 LOGD("Canvas is resized to %dx%d\n", w, h);
394 PUBLIC Evas_Object *livebox_snapshot_window_add(const char *id, int size_type)
396 struct snapshot_info *info;
397 Evas_Object *snapshot_win;
402 if (livebox_service_get_size(size_type, &w, &h) != LB_STATUS_SUCCESS) {
403 LOGE("Invalid size\n");
407 info = malloc(sizeof(*info));
409 LOGE("Heap: %s\n", strerror(errno));
413 info->id = strdup(id);
415 LOGE("Heap: %s\n", strerror(errno));
420 info->flush_cb = NULL;
422 info->flush_timer = NULL;
423 info->render_cnt = 0;
425 e = create_virtual_canvas(w, h);
432 snapshot_win = evas_object_rectangle_add(e);
434 destroy_virtual_canvas(e);
440 LOGD("Add new window %dx%d\n", w, h);
441 evas_object_event_callback_add(snapshot_win, EVAS_CALLBACK_DEL, del_cb, NULL);
442 evas_object_event_callback_add(snapshot_win, EVAS_CALLBACK_RESIZE, resize_cb, NULL);
443 evas_object_resize(snapshot_win, w, h);
444 evas_object_show(snapshot_win);
446 evas_object_data_set(snapshot_win, "snapshot,info", info);
448 evas_event_callback_add(e, EVAS_CALLBACK_RENDER_POST, post_render_cb, snapshot_win);
449 evas_event_callback_add(e, EVAS_CALLBACK_RENDER_PRE, pre_render_cb, snapshot_win);
456 PUBLIC int livebox_snapshot_window_flush(Evas_Object *snapshot_win, double timeout, void (*flush_cb)(Evas_Object *snapshot_window, const char *id, int status, void *data), void *data)
458 struct snapshot_info *info;
460 if (!flush_cb || timeout < 0.0f) {
461 LOGE("Invalid argument (%p, %lf)\n", flush_cb, timeout);
462 return LB_STATUS_ERROR_INVALID;
465 info = evas_object_data_get(snapshot_win, "snapshot,info");
467 LOGE("Invalid argument\n");
468 return LB_STATUS_ERROR_INVALID;
471 info->timeout = timeout;
473 if (timeout == 0.0f) {
475 * This timer is just used for guarantees same behavious even if it flushes content directly,
476 * The callback should be called from next loop.
477 * or the developer will get confused
479 info->flush_timer = ecore_timer_add(0.0001f, direct_snapshot_cb, snapshot_win);
480 if (!info->flush_timer)
481 return LB_STATUS_ERROR_FAULT;
482 } else if (info->render_cnt) {
484 * Try to watit pre-render callback.
485 * If there is rendered contents.
487 DbgPrint("Rendered %d times already\n", info->render_cnt);
488 info->flush_timer = ecore_timer_add(info->timeout, snapshot_cb, snapshot_win);
489 if (!info->flush_timer)
490 return LB_STATUS_ERROR_FAULT;
493 info->flush_cb = flush_cb;
496 return LB_STATUS_SUCCESS;
501 PUBLIC int livebox_snapshot_window_del(Evas_Object *snapshot_win)
504 if (!snapshot_win || !evas_object_data_get(snapshot_win, "snapshot,info"))
505 return LB_STATUS_ERROR_INVALID;
507 e = evas_object_evas_get(snapshot_win);
508 evas_event_callback_del(e, EVAS_CALLBACK_RENDER_POST, post_render_cb);
509 evas_event_callback_del(e, EVAS_CALLBACK_RENDER_PRE, pre_render_cb);
511 evas_object_del(snapshot_win);
512 return LB_STATUS_SUCCESS;