4 * Copyright 2012 Samsung Electronics Co., Ltd
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
10 * http://floralicense.org/license/
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.
21 #include <Evas_Engine_Buffer.h>
23 #include "paper_size.h"
24 #include "pgen_debug.h"
26 #include "evas_render.h"
28 int init_buffer_canvas(Evas *canvas, const struct size_px *page_size)
31 Evas_Engine_Info_Buffer *einfo;
35 int width = page_size->x;
36 int height = page_size->y;
38 PGEN_RETV_IF(page_size == NULL || canvas == NULL, -1 , "Invalid argument");
40 method = evas_render_method_lookup("buffer");
41 PGEN_RETV_IF(method <= 0 , -1, "method is less than zero");
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");
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));
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");
68 int init_temp_buffer_canvas(Evas *canvas)
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);
75 int reinit_buffer_canvas(Evas *canvas, const struct size_px *page_size)
77 Evas_Engine_Info_Buffer *einfo;
80 PGEN_RETV_IF(canvas == NULL || page_size == NULL, -1, "Invalid argument");
82 einfo = (Evas_Engine_Info_Buffer *)evas_engine_info_get(canvas);
83 PGEN_RETV_IF(einfo == NULL, -1 , "einfo is NULL");
85 oldbuf = einfo->info.dest_buffer;
86 if (init_buffer_canvas(canvas, page_size) < 0) {
89 //memcpy(einfo->info.dest_buffer, oldbuf, 100 * 100 * sizeof(int));
90 PGEN_IF_FREE_MEM(oldbuf);
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
101 Evas *create_canvas(const struct size_px *page_size)
105 PGEN_RETV_IF(page_size == NULL, NULL, "Invalid argument");
108 PGEN_RETV_IF(canvas == NULL, NULL, "canvas is NULL");
110 if (init_buffer_canvas(canvas, page_size) < 0) {
119 void destroy_canvas(Evas *canvas)
122 PGEN_RET_IF(canvas == NULL, "Invalid argument");
124 Evas_Engine_Info_Buffer *einfo;
125 einfo = (Evas_Engine_Info_Buffer *)evas_engine_info_get(canvas);
127 PGEN_DEBUG("ERROR: could not get evas engine info!\n");
131 PGEN_IF_FREE_MEM(einfo->info.dest_buffer);
138 * @brief Renders canvas (based on Evas usage example)
139 * @param[in] canvas canvas to render
141 void draw_scene(Evas *canvas)
144 PGEN_RET_IF(canvas == NULL, "Invalid argument");
146 Eina_List *updates;/*, *n;*/
147 /*Eina_Rectangle *update; */
149 /* render and get the updated rectangles: */
150 updates = evas_render_updates(canvas);
152 /* free list of updates */
153 evas_render_updates_free(updates);
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
164 void save_scene(Evas *canvas, const char *dest)
167 PGEN_RET_IF(canvas == NULL || dest == NULL, "Invalid argument");
169 Evas_Engine_Info_Buffer *einfo;
170 const unsigned int *pixels, *pixels_end;
174 einfo = (Evas_Engine_Info_Buffer *)evas_engine_info_get(canvas);
175 PGEN_RET_IF(einfo == NULL, "einfo is NULL");
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));
181 pixels = einfo->info.dest_buffer;
182 pixels_end = pixels + (width * height);
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++) {
189 r = ((*pixels) & 0xff0000) >> 16;
190 g = ((*pixels) & 0x00ff00) >> 8;
191 b = (*pixels) & 0x0000ff;
193 fprintf(f, "%c%c%c", r, g, b);
197 PGEN_DEBUG("saved scene as '%s'\n", dest);