tizen 2.3.1 release
[framework/appfw/app-core.git] / src / virtual_canvas.c
1 /*
2  *  starter
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <Elementary.h>
23 #include <Ecore_Evas.h>
24 #include <Ecore_X.h>
25
26 #include "appcore-internal.h"
27 #include "virtual_canvas.h"
28
29
30 #define QUALITY_N_COMPRESS "quality=100 compress=1"
31
32
33
34 Evas *virtual_canvas_create(int w, int h)
35 {
36         Ecore_Evas *internal_ee;
37         Evas *internal_e;
38
39         // Create virtual canvas
40         internal_ee = ecore_evas_buffer_new(w, h);
41         if (!internal_ee) {
42                 _DBG("Failed to create a new canvas buffer\n");
43                 return NULL;
44         }
45
46         ecore_evas_alpha_set(internal_ee, EINA_TRUE);
47         ecore_evas_manual_render_set(internal_ee, EINA_TRUE);
48
49         // Get the "Evas" object from a virtual canvas
50         internal_e = ecore_evas_get(internal_ee);
51         if (!internal_e) {
52                 ecore_evas_free(internal_ee);
53                 _DBG("Faield to get Evas object\n");
54                 return NULL;
55         }
56
57         return internal_e;
58 }
59
60
61
62 static bool _flush_data_to_file(Evas *e, char *data, const char *filename, int w, int h)
63 {
64         Evas_Object *output;
65
66         output = evas_object_image_add(e);
67         if (!output) {
68                 _DBG("Failed to create an image object\n");
69                 return false;
70         }
71
72         evas_object_image_data_set(output, NULL);
73         evas_object_image_colorspace_set(output, EVAS_COLORSPACE_ARGB8888);
74         evas_object_image_alpha_set(output, EINA_TRUE);
75         evas_object_image_size_set(output, w, h);
76         evas_object_image_smooth_scale_set(output, EINA_TRUE);
77         evas_object_image_data_set(output, data);
78         evas_object_image_data_update_add(output, 0, 0, w, h);
79
80         if (evas_object_image_save(output, filename, NULL, QUALITY_N_COMPRESS) == EINA_FALSE) {
81                 evas_object_del(output);
82                 _DBG("Faild to save a captured image (%s)\n", filename);
83                 return false;
84         }
85
86         evas_object_del(output);
87
88         if (access(filename, F_OK) != 0) {
89                 _DBG("File %s is not found\n", filename);
90                 return false;
91         }
92
93         return true;
94 }
95
96
97
98 bool virtual_canvas_flush_to_file(Evas *e, const char *filename, int w, int h)
99 {
100         void *data;
101         Ecore_Evas *internal_ee;
102
103         internal_ee = ecore_evas_ecore_evas_get(e);
104         if (!internal_ee) {
105                 _DBG("Failed to get ecore evas\n");
106                 return false;
107         }
108
109         ecore_evas_manual_render(internal_ee);
110
111         // Get a pointer of a buffer of the virtual canvas
112         data = (void *) ecore_evas_buffer_pixels_get(internal_ee);
113         if (!data) {
114                 _DBG("Failed to get pixel data\n");
115                 return false;
116         }
117
118         return _flush_data_to_file(e, data, filename, w, h);
119 }
120
121
122
123 bool virtual_canvas_destroy(Evas *e)
124 {
125         Ecore_Evas *ee;
126
127         ee = ecore_evas_ecore_evas_get(e);
128         if (!ee) {
129                 _DBG("Failed to ecore evas object\n");
130                 return false;
131         }
132
133         ecore_evas_free(ee);
134         return true;
135 }
136
137
138
139 // End of a file