Initialize the project.
[apps/livebox/data-provider-master.git] / src / fb.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (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
7  *
8  * http://www.tizenopensource.org/license
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <unistd.h> /* access */
19 #include <errno.h>
20
21 #include <dlog.h>
22 #include <Ecore_Evas.h>
23
24 #include "util.h"
25 #include "conf.h"
26 #include "debug.h"
27 #include "buffer_handler.h"
28 #include "fb.h"
29
30 int errno;
31
32 struct fb_info {
33         Ecore_Evas *ee;
34
35         struct buffer_info *buffer;
36 };
37
38 HAPI int fb_init(void)
39 {
40         return 0;
41 }
42
43 HAPI int fb_fini(void)
44 {
45         return 0;
46 }
47
48 static void *alloc_fb(void *data, int size)
49 {
50         struct fb_info *info = data;
51
52         DbgPrint("FB size: %d\n", size);
53
54         if (buffer_handler_load(info->buffer) < 0) {
55                 ErrPrint("Failed to load buffer handler\n");
56                 return NULL;
57         }
58
59         return buffer_handler_fb(info->buffer);
60 }
61
62 static void free_fb(void *data, void *ptr)
63 {
64         struct fb_info *info = data;
65
66         if (!info->buffer) {
67                 ErrPrint("Buffer is not valid (maybe already released)\n");
68                 return;
69         }
70
71         if (buffer_handler_fb(info->buffer) != ptr)
72                 ErrPrint("Buffer pointer is not matched\n");
73
74         (void)buffer_handler_unload(info->buffer);
75 }
76
77 HAPI struct fb_info *fb_create(struct inst_info *inst, int w, int h, enum buffer_type type)
78 {
79         struct fb_info *info;
80
81         info = calloc(1, sizeof(*info));
82         if (!info) {
83                 ErrPrint("Heap: %s\n", strerror(errno));
84                 return NULL;
85         }
86
87         info->buffer = buffer_handler_create(inst, type, w, h, sizeof(int));
88         if (!info->buffer) {
89                 ErrPrint("Failed to create a buffer\n");
90                 DbgFree(info);
91                 return NULL;
92         }
93
94         info->ee = NULL;
95         return info;
96 }
97
98 static void render_pre_cb(void *data, Evas *e, void *event_info)
99 {
100         fb_pixmap_render_pre(data);
101 }
102
103 static void render_post_cb(void *data, Evas *e, void *event_info)
104 {
105         fb_pixmap_render_post(data);
106 }
107
108 HAPI int fb_create_buffer(struct fb_info *info)
109 {
110         int ow;
111         int oh;
112
113         buffer_handler_get_size(info->buffer, &ow, &oh);
114         DbgPrint("Buffer handler size: %dx%d\n", ow, oh);
115         if (ow == 0 && oh == 0) {
116                 DbgPrint("ZERO Size FB accessed\n");
117                 return 0;
118         }
119
120         if (info->ee) {
121                 int w = 0;
122                 int h = 0;
123
124                 ecore_evas_geometry_get(info->ee, NULL, NULL, &w, &h);
125                 if (w != ow || h != oh) {
126                         ErrPrint("EE exists, size mismatched requested (%dx%d) but (%dx%d)\n", ow, oh, w, h);
127                         ecore_evas_resize(info->ee, ow, oh);
128                 }
129
130                 return 0;
131         }
132
133         info->ee = ecore_evas_buffer_allocfunc_new(ow, oh, alloc_fb, free_fb, info);
134         if (!info->ee) {
135                 ErrPrint("Failed to create a buffer\n");
136                 return -EFAULT;
137         }
138
139         if (buffer_handler_type(info->buffer) == BUFFER_TYPE_PIXMAP) {
140                 Evas *e;
141                 e = ecore_evas_get(info->ee);
142                 if (e) {
143                         evas_event_callback_add(e, EVAS_CALLBACK_RENDER_PRE, render_pre_cb, info);
144                         evas_event_callback_add(e, EVAS_CALLBACK_RENDER_POST, render_post_cb, info);
145
146                         /*!
147                          * \note
148                          * ecore_evas_alpha_set tries to access the canvas buffer.
149                          * Without any render_pre/render_post callback.
150                          */
151                         fb_pixmap_render_pre(info);
152                         ecore_evas_alpha_set(info->ee, EINA_TRUE);
153                         fb_pixmap_render_post(info);
154                 }
155         } else {
156                 ecore_evas_alpha_set(info->ee, EINA_TRUE);
157         }
158
159         return 0;
160 }
161
162 HAPI int fb_destroy_buffer(struct fb_info *info)
163 {
164         if (!info->ee) {
165                 ErrPrint("EE is not exists (Maybe ZERO byte ee?)\n");
166                 return -EINVAL;
167         }
168
169         if (buffer_handler_type(info->buffer) == BUFFER_TYPE_PIXMAP) {
170                 Evas *e;
171                 e = ecore_evas_get(info->ee);
172                 if (e) {
173                         evas_event_callback_del(e, EVAS_CALLBACK_RENDER_POST, render_post_cb);
174                         evas_event_callback_del(e, EVAS_CALLBACK_RENDER_PRE, render_pre_cb);
175                 }
176         }
177
178         ecore_evas_free(info->ee);
179         info->ee = NULL;
180         return 0;
181 }
182
183 HAPI int fb_destroy(struct fb_info *info)
184 {
185         fb_destroy_buffer(info);
186         DbgFree(info);
187         return 0;
188 }
189
190 HAPI Ecore_Evas * const fb_canvas(struct fb_info *info)
191 {
192         return info->ee;
193 }
194
195 HAPI const char *fb_id(struct fb_info *fb)
196 {
197         return fb ? buffer_handler_id(fb->buffer) : "";
198 }
199
200 HAPI int fb_resize(struct fb_info *info, int w, int h)
201 {
202         buffer_handler_update_size(info->buffer, w, h);
203
204         if (info->ee) {
205                 ecore_evas_resize(info->ee, w, h);
206         } else if (!info->ee && !info->buffer) {
207                 /*!
208                  * This object has no size at the initial time.
209                  * Create a new buffer and use it
210                  */
211         }
212
213         return 0;
214 }
215
216 HAPI int fb_get_size(struct fb_info *info, int *w, int *h)
217 {
218         return buffer_handler_get_size(info->buffer, w, h);
219 }
220
221 HAPI void fb_sync(struct fb_info *info)
222 {
223         buffer_handler_flush(info->buffer);
224 }
225
226 HAPI void *fb_pixmap_render_pre(struct fb_info *info)
227 {
228         void *canvas;
229         canvas = buffer_handler_pixmap_acquire_buffer(info->buffer);
230         return canvas;
231 }
232
233 HAPI int fb_pixmap_render_post(struct fb_info *info)
234 {
235         void *canvas;
236
237         /*!
238          * \note
239          * info->buffer == struct buffer_info
240          */
241         canvas = buffer_handler_pixmap_buffer(info->buffer);
242         return buffer_handler_pixmap_release_buffer(canvas);
243 }
244
245 /* End of a file */