2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <Elementary.h>
18 #include <Ecore_Evas.h>
24 #include <dynamicbox_service.h>
25 #include <dynamicbox_errno.h>
27 #include "dynamicbox.h"
30 #define QUALITY_N_COMPRESS "quality=100 compress=1"
31 #define PUBLIC __attribute__((visibility("default")))
35 struct snapshot_info {
37 dynamicbox_flush_cb flush_cb;
40 Ecore_Timer *flush_timer;
46 static void post_render_cb(void *data, Evas *e, void *event_info);
47 static void pre_render_cb(void *data, Evas *e, void *event_info);
51 static inline Evas *create_virtual_canvas(int w, int h)
53 Ecore_Evas *internal_ee;
56 // Create virtual canvas
57 internal_ee = ecore_evas_buffer_new(w, h);
59 DbgPrint("Failed to create a new canvas buffer\n");
63 ecore_evas_alpha_set(internal_ee, EINA_TRUE);
64 ecore_evas_manual_render_set(internal_ee, EINA_FALSE);
66 // Get the "Evas" object from a virtual canvas
67 internal_e = ecore_evas_get(internal_ee);
69 ecore_evas_free(internal_ee);
70 DbgPrint("Faield to get Evas object\n");
79 static inline int flush_data_to_file(Evas *e, char *data, const char *filename, int w, int h)
83 output = evas_object_image_add(e);
85 DbgPrint("Failed to create an image object\n");
86 return DBOX_STATUS_ERROR_FAULT;
89 evas_object_image_data_set(output, NULL);
90 evas_object_image_colorspace_set(output, EVAS_COLORSPACE_ARGB8888);
91 evas_object_image_alpha_set(output, EINA_TRUE);
92 evas_object_image_size_set(output, w, h);
93 evas_object_image_smooth_scale_set(output, EINA_TRUE);
94 evas_object_image_data_set(output, data);
95 evas_object_image_fill_set(output, 0, 0, w, h);
96 evas_object_image_data_update_add(output, 0, 0, w, h);
98 if (evas_object_image_save(output, filename, NULL, QUALITY_N_COMPRESS) == EINA_FALSE) {
99 evas_object_del(output);
100 DbgPrint("Faield to save a captured image (%s)\n", filename);
101 return DBOX_STATUS_ERROR_IO_ERROR;
104 evas_object_del(output);
106 if (access(filename, F_OK) != 0) {
107 DbgPrint("File %s is not found\n", filename);
108 return DBOX_STATUS_ERROR_IO_ERROR;
111 DbgPrint("Flush data to a file (%s)\n", filename);
112 return DBOX_STATUS_ERROR_NONE;
117 static inline int destroy_virtual_canvas(Evas *e)
121 ee = ecore_evas_ecore_evas_get(e);
123 DbgPrint("Failed to ecore evas object\n");
124 return DBOX_STATUS_ERROR_FAULT;
128 return DBOX_STATUS_ERROR_NONE;
133 static inline int flush_to_file(const void *canvas, const char *filename, int w, int h)
139 shot_e = create_virtual_canvas(w, h);
141 ErrPrint("Unable to create a new virtual window\n");
142 return DBOX_STATUS_ERROR_FAULT;
145 shot_ee = ecore_evas_ecore_evas_get(shot_e);
147 ErrPrint("Unable to get Ecore_Evas\n");
148 destroy_virtual_canvas(shot_e);
149 return DBOX_STATUS_ERROR_FAULT;
152 ecore_evas_manual_render_set(shot_ee, EINA_TRUE);
154 status = flush_data_to_file(shot_e, canvas, filename, w, h);
155 destroy_virtual_canvas(shot_e);
162 static Eina_Bool snapshot_cb(void *data)
164 Evas_Object *snapshot_win = data;
165 struct snapshot_info *info;
169 dynamicbox_flush_cb flush_cb;
170 int status = DBOX_STATUS_ERROR_NONE;
172 info = evas_object_data_get(snapshot_win, "snapshot,info");
174 ErrPrint("Invalid object\n");
175 return ECORE_CALLBACK_CANCEL;
178 if (info->flush_timer) {
179 info->flush_timer = NULL;
181 status = DBOX_STATUS_ERROR_CANCEL;
184 flush_cb = info->flush_cb;
185 info->flush_cb = NULL; /* To prevent call this from the delete callback */
187 e = evas_object_evas_get(snapshot_win);
189 ErrPrint("Invalid object\n");
191 flush_cb(snapshot_win, info->id, DBOX_STATUS_ERROR_FAULT, info->data);
193 return ECORE_CALLBACK_CANCEL;
196 ee = ecore_evas_ecore_evas_get(e);
198 ErrPrint("Invalid object (ee)\n");
200 flush_cb(snapshot_win, info->id, DBOX_STATUS_ERROR_FAULT, info->data);
202 return ECORE_CALLBACK_CANCEL;
205 canvas = (void*)ecore_evas_buffer_pixels_get(ee);
207 DbgPrint("Failed to get pixel canvas\n");
209 flush_cb(snapshot_win, info->id, DBOX_STATUS_ERROR_FAULT, info->data);
211 return ECORE_CALLBACK_CANCEL;
219 ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
221 DbgPrint("Flush size: %dx%d\n", w, h);
222 ret = flush_to_file(canvas, info->id, w, h);
223 if (status == DBOX_STATUS_ERROR_NONE) {
227 flush_cb(snapshot_win, info->id, status, info->data);
229 * Do not access info after this.
233 return ECORE_CALLBACK_CANCEL;
238 static void del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
240 struct snapshot_info *info;
242 info = evas_object_data_del(obj, "snapshot,info");
247 DbgPrint("Delete object (%s)\n", info->id);
249 evas_event_callback_del(e, EVAS_CALLBACK_RENDER_POST, post_render_cb);
250 evas_event_callback_del(e, EVAS_CALLBACK_RENDER_PRE, pre_render_cb);
252 if (info->flush_timer) {
253 ecore_timer_del(info->flush_timer);
254 info->flush_timer = NULL;
256 (void)snapshot_cb(obj);
257 DbgPrint("Flush is canceled\n");
262 * Render callback will be deleted.
270 static Eina_Bool direct_snapshot_cb(void *data)
278 dynamicbox_flush_cb flush_cb;
279 Evas_Object *snapshot_win = data;
280 struct snapshot_info *info;
282 info = evas_object_data_get(snapshot_win, "snapshot,info");
284 ErrPrint("Unable to get snapshot info\n");
285 return ECORE_CALLBACK_CANCEL;
288 info->flush_timer = NULL;
289 flush_cb = info->flush_cb;
290 info->flush_cb = NULL; /* To prevent call this from the delete callback */
292 e = evas_object_evas_get(snapshot_win);
294 ErrPrint("Invalid object, failed to get Evas\n");
296 flush_cb(snapshot_win, info->id, DBOX_STATUS_ERROR_FAULT, info->data);
298 return ECORE_CALLBACK_CANCEL;
301 ee = ecore_evas_ecore_evas_get(e);
303 ErrPrint("Unable to get Ecore_Evas object\n");
305 flush_cb(snapshot_win, info->id, DBOX_STATUS_ERROR_FAULT, info->data);
307 return ECORE_CALLBACK_CANCEL;
310 ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
311 ecore_evas_manual_render_set(ee, EINA_TRUE);
313 canvas = ecore_evas_buffer_pixels_get(ee);
315 ErrPrint("Failed to get canvas\n");
317 flush_cb(snapshot_win, info->id, DBOX_STATUS_ERROR_FAULT, info->data);
319 return ECORE_CALLBACK_CANCEL;
322 status = flush_to_file(canvas, info->id, w, h);
324 flush_cb(snapshot_win, info->id, status, info->data);
326 return ECORE_CALLBACK_CANCEL;
331 static void post_render_cb(void *data, Evas *e, void *event_info)
333 Evas_Object *snapshot_win = data;
334 struct snapshot_info *info;
336 info = evas_object_data_get(snapshot_win, "snapshot,info");
338 ErrPrint("snapshot info is not valid\n");
344 if (info->flush_timer) {
347 * This has not to be happens.
349 ErrPrint("Flush timer is not cleared\n");
350 ecore_timer_del(info->flush_timer);
351 info->flush_timer = NULL;
354 if (!info->flush_cb) {
355 DbgPrint("Flush request is not initiated yet\n");
361 * Even if tehre is no timer registered, we should capture the content
362 * from out of this callback.
363 * Or we can met unexpected problems.
364 * To avoid it, use the 0.0001f to get timer callback ASAP, not in this function.
366 DbgPrint("Fire the flush timer %lf (%d)\n", info->timeout, info->render_cnt);
367 info->flush_timer = ecore_timer_add(info->timeout, snapshot_cb, snapshot_win);
368 if (!info->flush_timer) {
369 dynamicbox_flush_cb flush_cb;
371 ErrPrint("Unalbe to add timer for getting the snapshot\n");
372 flush_cb = info->flush_cb;
373 info->flush_cb = NULL;
376 flush_cb(snapshot_win, info->id, DBOX_STATUS_ERROR_FAULT, info->data);
380 * Do not access info after from here.
387 static void pre_render_cb(void *data, Evas *e, void *event_info)
389 Evas_Object *snapshot_win = data;
390 struct snapshot_info *info;
392 info = evas_object_data_get(snapshot_win, "snapshot,info");
394 ErrPrint("snapshot info is not valid\n");
398 if (info->flush_timer) {
399 ecore_timer_del(info->flush_timer);
400 info->flush_timer = NULL;
401 DbgPrint("Clear the flush timer\n");
404 DbgPrint("Pre-render callback\n");
409 PUBLIC void *dynamicbox_snapshot_window_add(const char *id, int size_type)
411 struct snapshot_info *info;
412 Evas_Object *snapshot_win;
418 if (dynamicbox_service_get_size(size_type, &w, &h) != DBOX_STATUS_ERROR_NONE) {
419 ErrPrint("Invalid size\n");
423 info = malloc(sizeof(*info));
425 ErrPrint("Heap: %s\n", strerror(errno));
429 info->id = strdup(id);
431 ErrPrint("Heap: %s\n", strerror(errno));
436 info->flush_cb = NULL;
438 info->flush_timer = NULL;
439 info->render_cnt = 0;
441 e = create_virtual_canvas(w, h);
448 parent = evas_object_rectangle_add(e);
450 destroy_virtual_canvas(e);
456 snapshot_win = elm_win_add(parent, "DBox,Snapshot", ELM_WIN_DYNAMIC_BOX);
457 evas_object_del(parent);
459 destroy_virtual_canvas(e);
465 DbgPrint("Create a new window %dx%d for %s\n", w, h, id);
466 evas_object_event_callback_add(snapshot_win, EVAS_CALLBACK_DEL, del_cb, NULL);
467 evas_object_resize(snapshot_win, w, h);
468 evas_object_show(snapshot_win);
470 evas_object_data_set(snapshot_win, "snapshot,info", info);
472 evas_event_callback_add(e, EVAS_CALLBACK_RENDER_POST, post_render_cb, snapshot_win);
473 evas_event_callback_add(e, EVAS_CALLBACK_RENDER_PRE, pre_render_cb, snapshot_win);
480 PUBLIC int dynamicbox_snapshot_window_flush(void *snapshot_win, double timeout, dynamicbox_flush_cb flush_cb, void *data)
482 struct snapshot_info *info;
484 if (!flush_cb || timeout < 0.0f) {
485 ErrPrint("Invalid argument (%p, %lf)\n", flush_cb, timeout);
486 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
489 info = evas_object_data_get(snapshot_win, "snapshot,info");
491 ErrPrint("Invalid argument\n");
492 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
495 info->timeout = timeout;
497 if (timeout == 0.0f) {
499 * This timer is just used for guarantees same behavious even if it flushes content directly,
500 * The callback should be called from next loop.
501 * or the developer will get confused
503 info->flush_timer = ecore_timer_add(0.0001f, direct_snapshot_cb, snapshot_win);
504 if (!info->flush_timer) {
505 return DBOX_STATUS_ERROR_FAULT;
507 } else if (info->render_cnt) {
509 * Try to watit pre-render callback.
510 * If there is rendered contents.
512 DbgPrint("Rendered %d times already\n", info->render_cnt);
513 info->flush_timer = ecore_timer_add(info->timeout, snapshot_cb, snapshot_win);
514 if (!info->flush_timer) {
515 return DBOX_STATUS_ERROR_FAULT;
519 info->flush_cb = flush_cb;
522 return DBOX_STATUS_ERROR_NONE;