1. Changed license year
[apps/home/mobileprint.git] / mobileprint / previewgen / lib / evas_render.c
1 /*
2 *  Mobileprint
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.1 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9
10 * http://floralicense.org/license/
11
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 #include <Evas.h>
21 #include <Evas_Engine_Buffer.h>
22
23 #include "paper_size.h"
24 #include "pgen_debug.h"
25
26 #include "evas_render.h"
27
28 int init_buffer_canvas(Evas *canvas, const struct size_px *page_size)
29 {
30         PGEN_TRACE_BEGIN;
31         Evas_Engine_Info_Buffer *einfo;
32         Eina_Bool ret;
33         int method;
34         void *pixels;
35         int width = page_size->x;
36         int height = page_size->y;
37
38         PGEN_RETV_IF(page_size == NULL || canvas == NULL, -1 , "Invalid argument");
39
40         method = evas_render_method_lookup("buffer");
41         PGEN_RETV_IF(method <= 0 , -1, "method is less than zero");
42
43         evas_output_method_set(canvas, method);
44         evas_output_size_set(canvas, width, height);
45         evas_output_viewport_set(canvas, 0, 0, width, height);
46         einfo = (Evas_Engine_Info_Buffer *)evas_engine_info_get(canvas);
47         PGEN_RETV_IF(einfo == NULL, -1, "einfo is NULL");
48
49         PGEN_DEBUG("pixels malloc(%d) width %d height %d ",width*height*sizeof(int), width, height);
50         pixels = malloc(width * height * sizeof(int));
51         PGEN_RETV_IF(pixels == NULL, -1, "pixels is NULL");
52         memset(pixels, 0, width * height * sizeof(int));
53
54         einfo->info.depth_type = EVAS_ENGINE_BUFFER_DEPTH_ARGB32;
55         einfo->info.dest_buffer = pixels;
56         einfo->info.dest_buffer_row_bytes = width * sizeof(int);
57         einfo->info.use_color_key = 0;
58         einfo->info.alpha_threshold = 0;
59         einfo->info.func.new_update_region = NULL;
60         einfo->info.func.free_update_region = NULL;
61         ret = evas_engine_info_set(canvas, (Evas_Engine_Info *)einfo);
62         PGEN_RETV_IF(ret == EINA_FALSE, -1, "Failed to evas_engine_info_set");
63
64         PGEN_TRACE_END;
65         return 0;
66 }
67
68 int init_temp_buffer_canvas(Evas *canvas)
69 {
70         PGEN_RETV_IF(canvas == NULL, -1 , "Invalid argument");
71         struct size_px page_size = {100, 100};
72         return init_buffer_canvas(canvas, &page_size);
73 }
74
75 int reinit_buffer_canvas(Evas *canvas, const struct size_px *page_size)
76 {
77         Evas_Engine_Info_Buffer *einfo;
78         void *oldbuf;
79
80         PGEN_RETV_IF(canvas == NULL || page_size == NULL, -1, "Invalid argument");
81
82         einfo = (Evas_Engine_Info_Buffer *)evas_engine_info_get(canvas);
83         PGEN_RETV_IF(einfo == NULL, -1 , "einfo is NULL");
84
85         oldbuf = einfo->info.dest_buffer;
86         if (init_buffer_canvas(canvas, page_size) < 0) {
87                 return -1;
88         }
89         //memcpy(einfo->info.dest_buffer, oldbuf, 100 * 100 * sizeof(int));
90         PGEN_IF_FREE_MEM(oldbuf);
91
92         return 0;
93 }
94
95 /**
96  * @brief       Provides Evas canvas based on memory buffer
97  *              (based on Evas usage example)
98  * @param[in]   page size in px
99  * @return      canvas based on memory buffer
100  */
101 Evas *create_canvas(const struct size_px *page_size)
102 {
103         PGEN_TRACE_BEGIN;
104         Evas *canvas;
105         PGEN_RETV_IF(page_size == NULL, NULL, "Invalid argument");
106
107         canvas = evas_new();
108         PGEN_RETV_IF(canvas == NULL, NULL, "canvas is NULL");
109
110         if (init_buffer_canvas(canvas, page_size) < 0) {
111                 evas_free(canvas);
112                 return NULL;
113         }
114
115         PGEN_TRACE_END;
116         return canvas;
117 }
118
119 void destroy_canvas(Evas *canvas)
120 {
121         PGEN_TRACE_BEGIN;
122         PGEN_RET_IF(canvas == NULL, "Invalid argument");
123
124         Evas_Engine_Info_Buffer *einfo;
125         einfo = (Evas_Engine_Info_Buffer *)evas_engine_info_get(canvas);
126         if (!einfo) {
127                 PGEN_DEBUG("ERROR: could not get evas engine info!\n");
128                 evas_free(canvas);
129                 return;
130         }
131         PGEN_IF_FREE_MEM(einfo->info.dest_buffer);
132         evas_free(canvas);
133
134         PGEN_TRACE_END;
135 }
136
137 /**
138  * @brief       Renders canvas (based on Evas usage example)
139  * @param[in]   canvas  canvas to render
140  */
141 void draw_scene(Evas *canvas)
142 {
143         PGEN_TRACE_BEGIN;
144         PGEN_RET_IF(canvas == NULL, "Invalid argument");
145
146         Eina_List *updates;/*, *n;*/
147         /*Eina_Rectangle *update; */
148
149         /* render and get the updated rectangles: */
150         updates = evas_render_updates(canvas);
151
152         /* free list of updates */
153         evas_render_updates_free(updates);
154
155         PGEN_TRACE_END;
156 }
157
158 /**
159  * @brief       Saves canvas to specified image file in PPM format
160  *              (based on Evas usage example)
161  * @param[in]   canvas  canvas to save
162  * @param[in]   dest    output PPM file name
163  */
164 void save_scene(Evas *canvas, const char *dest)
165 {
166         PGEN_TRACE_BEGIN;
167         PGEN_RET_IF(canvas == NULL || dest == NULL, "Invalid argument");
168
169         Evas_Engine_Info_Buffer *einfo;
170         const unsigned int *pixels, *pixels_end;
171         int width, height;
172         FILE *f;
173
174         einfo = (Evas_Engine_Info_Buffer *)evas_engine_info_get(canvas);
175         PGEN_RET_IF(einfo == NULL, "einfo is NULL");
176
177         evas_output_size_get(canvas, &width, &height);
178         f = fopen(dest, "wb+");
179         PGEN_RET_IF(f == NULL, "ERROR: could not open for writing '%s': %s",dest, strerror(errno));
180
181         pixels = einfo->info.dest_buffer;
182         pixels_end = pixels + (width * height);
183
184         /* PPM P6 format is dead simple to write: */
185         fprintf(f, "P6\n%d %d\n255\n",  width, height);
186         for (; pixels < pixels_end; pixels++) {
187                 int r, g, b;
188
189                 r = ((*pixels) & 0xff0000) >> 16;
190                 g = ((*pixels) & 0x00ff00) >> 8;
191                 b = (*pixels) & 0x0000ff;
192
193                 fprintf(f, "%c%c%c", r, g, b);
194         }
195
196         fclose(f);
197         PGEN_DEBUG("saved scene as '%s'\n", dest);
198
199         PGEN_TRACE_END;
200 }
201