[glview] delete docs
[framework/uifw/elementary.git] / src / lib / elm_glview.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5
6 struct _Widget_Data
7 {
8    Evas_Object              *glview_image;
9
10    Elm_GLView_Mode           mode;
11    Elm_GLView_Resize_Policy  scale_policy;
12    Elm_GLView_Render_Policy  render_policy;
13
14    Evas_GL                  *evasgl;
15    Evas_GL_Config            config;
16    Evas_GL_Surface          *surface;
17    Evas_GL_Context          *context;
18
19    Evas_Coord                w, h;
20
21    Elm_GLView_Func           init_func;
22    Elm_GLView_Func           del_func;
23    Elm_GLView_Func           resize_func;
24    Elm_GLView_Func           render_func;
25
26    Ecore_Idle_Enterer       *render_idle_enterer;
27
28    Eina_Bool                 initialized;
29    Eina_Bool                 resized;
30 };
31
32 static const char *widtype = NULL;
33 static void _del_hook(Evas_Object *obj);
34 static void _on_focus_hook(void *data, Evas_Object *obj);
35
36 static const char SIG_FOCUSED[] = "focused";
37 static const char SIG_UNFOCUSED[] = "unfocused";
38
39 static void
40 _del_hook(Evas_Object *obj)
41 {
42    Widget_Data *wd = elm_widget_data_get(obj);
43    if (!wd) return;
44
45    // Call delete func if it's registered
46    if (wd->del_func)
47      {
48         evas_gl_make_current(wd->evasgl, wd->surface, wd->context);
49         wd->del_func(obj);
50      }
51
52    if (wd->render_idle_enterer) ecore_idle_enterer_del(wd->render_idle_enterer);
53
54    if (wd->surface) evas_gl_surface_destroy(wd->evasgl, wd->surface);
55    if (wd->context) evas_gl_context_destroy(wd->evasgl, wd->context);
56    if (wd->evasgl) evas_gl_free(wd->evasgl);
57
58    free(wd);
59 }
60
61 static void
62 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
63 {
64    Widget_Data *wd = elm_widget_data_get(obj);
65    if (!wd) return;
66
67    if (elm_widget_focus_get(obj))
68      {
69         evas_object_focus_set(wd->glview_image, EINA_TRUE);
70         evas_object_smart_callback_call(obj, SIG_FOCUSED, NULL);
71      }
72    else
73      {
74         evas_object_focus_set(wd->glview_image, EINA_FALSE);
75         evas_object_smart_callback_call(obj, SIG_UNFOCUSED, NULL);
76      }
77 }
78
79 static void
80 _glview_update_surface(Evas_Object *obj)
81 {
82    Widget_Data *wd = elm_widget_data_get(obj);
83    if (!wd) return;
84
85    if (wd->surface)
86      {
87         evas_object_image_native_surface_set(wd->glview_image, NULL);
88         evas_gl_surface_destroy(wd->evasgl, wd->surface);
89         wd->surface = NULL;
90      }
91
92    evas_object_image_size_set(wd->glview_image, wd->w, wd->h);
93
94    if (!wd->surface)
95      {
96         Evas_Native_Surface ns;
97
98         wd->surface = evas_gl_surface_create(wd->evasgl, &wd->config,
99                                              wd->w, wd->h);
100         evas_gl_native_surface_get(wd->evasgl, wd->surface, &ns);
101         evas_object_image_native_surface_set(wd->glview_image, &ns);
102         elm_glview_changed_set(obj);
103      }
104 }
105
106 static void
107 _glview_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
108 {
109    Widget_Data *wd = elm_widget_data_get(data);
110    Evas_Coord w, h;
111
112    if (!wd) return;
113
114    wd->resized = EINA_TRUE;
115
116    if (wd->scale_policy == ELM_GLVIEW_RESIZE_POLICY_RECREATE)
117      {
118         evas_object_geometry_get(wd->glview_image, NULL, NULL, &w, &h);
119         if ((w == 0) || (h == 0))
120           {
121              w = 64;
122              h = 64;
123           }
124         if ((wd->w == w) && (wd->h == h)) return;
125         wd->w = w;
126         wd->h = h;
127         _glview_update_surface(data);
128         /*
129         if (wd->render_func)
130           {
131              evas_gl_make_current(wd->evasgl, wd->surface, wd->context);
132              wd->render_func(data);
133           }
134           */
135      }
136 }
137
138 static Eina_Bool
139 _render_cb(void *obj)
140 {
141    Widget_Data *wd = elm_widget_data_get(obj);
142    if (!wd) return EINA_FALSE;
143
144    // Do a make current
145    if (!evas_gl_make_current(wd->evasgl, wd->surface, wd->context))
146      {
147         wd->render_idle_enterer = NULL;
148         ERR("Failed doing make current.\n");
149         return EINA_FALSE;
150      }
151
152    // Call the init function if it hasn't been called already
153    if (!wd->initialized)
154      {
155         if (wd->init_func) wd->init_func(obj);
156         wd->initialized = EINA_TRUE;
157      }
158
159    if (wd->resized)
160      {
161         if (wd->resize_func) wd->resize_func(obj);
162         wd->resized = EINA_FALSE;
163      }
164
165    // Call the render function
166    if (wd->render_func) wd->render_func(obj);
167
168    // Depending on the policy return true or false
169    if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ON_DEMAND)
170      return EINA_TRUE;
171    else if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ALWAYS)
172      {
173         // Return false so it only runs once
174         wd->render_idle_enterer = NULL;
175         return EINA_FALSE;
176      }
177    else
178      {
179         ERR("Invalid Render Policy.\n");
180         wd->render_idle_enterer = NULL;
181         return EINA_FALSE;
182      }
183    return EINA_TRUE;
184 }
185
186 static void
187 _set_render_policy_callback(Evas_Object *obj)
188 {
189    Widget_Data *wd = elm_widget_data_get(obj);
190
191    switch (wd->render_policy)
192      {
193       case ELM_GLVIEW_RENDER_POLICY_ON_DEMAND:
194          // Delete idle_enterer if it for some reason is around
195          if (wd->render_idle_enterer)
196            {
197               ecore_idle_enterer_del(wd->render_idle_enterer);
198               wd->render_idle_enterer = NULL;
199            }
200
201          // Set pixel getter callback
202          evas_object_image_pixels_get_callback_set
203             (wd->glview_image, (Evas_Object_Image_Pixels_Get_Cb)_render_cb, obj);
204          break;
205       case ELM_GLVIEW_RENDER_POLICY_ALWAYS:
206          // Unset the pixel getter callback if set already
207          evas_object_image_pixels_get_callback_set(wd->glview_image, NULL, NULL);
208
209          break;
210       default:
211          ERR("Invalid Render Policy.\n");
212          return;
213      }
214 }
215
216 EAPI Evas_Object *
217 elm_glview_add(Evas_Object *parent)
218 {
219    Evas_Object *obj;
220    Evas *e;
221    Widget_Data *wd;
222    Evas_GL_Config cfg = { EVAS_GL_RGB_8,
223                           EVAS_GL_DEPTH_NONE,
224                           EVAS_GL_STENCIL_NONE };
225
226    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
227
228    ELM_SET_WIDTYPE(widtype, "glview");
229    elm_widget_type_set(obj, "glview");
230    elm_widget_sub_object_add(parent, obj);
231    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
232    elm_widget_data_set(obj, wd);
233    elm_widget_del_hook_set(obj, _del_hook);
234
235    // Evas_GL
236    wd->evasgl = evas_gl_new(e);
237    if (!wd->evasgl)
238      {
239         ERR("Failed Creating an Evas GL Object.\n");
240         return NULL;
241      }
242
243    // Create image to render Evas_GL Surface
244    wd->glview_image = evas_object_image_filled_add(e);
245    evas_object_image_size_set(wd->glview_image, 1, 1);
246    evas_object_event_callback_add(wd->glview_image, EVAS_CALLBACK_RESIZE,
247                                   _glview_resize, obj);
248    elm_widget_resize_object_set(obj, wd->glview_image);
249    evas_object_show(wd->glview_image);
250
251    // Initialize variables
252    wd->mode                = 0;
253    wd->scale_policy        = ELM_GLVIEW_RESIZE_POLICY_RECREATE;
254    wd->render_policy       = ELM_GLVIEW_RENDER_POLICY_ON_DEMAND;
255    wd->config              = cfg;
256    wd->surface             = NULL;
257
258    // Initialize it to (64,64)  (It's an arbitrary value)
259    wd->w                   = 64;
260    wd->h                   = 64;
261
262    // Initialize the rest of the values
263    wd->init_func           = NULL;
264    wd->del_func            = NULL;
265    wd->render_func         = NULL;
266    wd->render_idle_enterer = NULL;
267    wd->initialized         = EINA_FALSE;
268    wd->resized             = EINA_FALSE;
269
270    // Create Context
271    if (!wd->context)
272      {
273         wd->context = evas_gl_context_create(wd->evasgl, NULL);
274         if (!wd->context)
275           {
276              ERR("Error Creating an Evas_GL Context.\n");
277              return NULL;
278           }
279      }
280    return obj;
281 }
282
283 EAPI Evas_GL_API *
284 elm_glview_gl_api_get(const Evas_Object *obj)
285 {
286    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
287    Widget_Data *wd = elm_widget_data_get(obj);
288    if (!wd) return NULL;
289
290    return evas_gl_api_get(wd->evasgl);
291 }
292
293 EAPI Eina_Bool
294 elm_glview_mode_set(Evas_Object *obj, Elm_GLView_Mode mode)
295 {
296    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
297    Widget_Data *wd = elm_widget_data_get(obj);
298    Evas_GL_Config cfg = { EVAS_GL_RGBA_8,
299                           EVAS_GL_DEPTH_NONE,
300                           EVAS_GL_STENCIL_NONE };
301    if (!wd) return EINA_FALSE;
302
303    // Set the configs
304    if (mode & ELM_GLVIEW_ALPHA)
305      cfg.color_format = EVAS_GL_RGBA_8;
306
307    if (mode & ELM_GLVIEW_DEPTH)
308      cfg.depth_bits = EVAS_GL_DEPTH_BIT_24;
309
310    if (mode & ELM_GLVIEW_STENCIL)
311      cfg.stencil_bits = EVAS_GL_STENCIL_BIT_8;
312
313    // Check for Alpha Channel and enable it
314    if (mode & ELM_GLVIEW_ALPHA)
315      evas_object_image_alpha_set(wd->glview_image, EINA_TRUE);
316    else
317      evas_object_image_alpha_set(wd->glview_image, EINA_FALSE);
318
319    wd->mode   = mode;
320    wd->config = cfg;
321
322    elm_glview_changed_set(obj);
323
324    return EINA_TRUE;
325 }
326
327 EAPI Eina_Bool
328 elm_glview_resize_policy_set(Evas_Object *obj, Elm_GLView_Resize_Policy policy)
329 {
330    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
331    Widget_Data *wd = elm_widget_data_get(obj);
332    if (!wd) return EINA_FALSE;
333
334    if (policy == wd->scale_policy) return EINA_TRUE;
335    switch (policy)
336      {
337       case ELM_GLVIEW_RESIZE_POLICY_RECREATE:
338       case ELM_GLVIEW_RESIZE_POLICY_SCALE:
339          wd->scale_policy = policy;
340          return EINA_TRUE;
341       default:
342          ERR("Invalid Scale Policy.\n");
343          return EINA_FALSE;
344      }
345    _glview_update_surface(obj);
346    elm_glview_changed_set(obj);
347 }
348
349 EAPI Eina_Bool
350 elm_glview_render_policy_set(Evas_Object *obj, Elm_GLView_Render_Policy policy)
351 {
352    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
353    Widget_Data *wd = elm_widget_data_get(obj);
354    if (!wd) return EINA_FALSE;
355
356    if ((policy != ELM_GLVIEW_RENDER_POLICY_ON_DEMAND) &&
357        (policy != ELM_GLVIEW_RENDER_POLICY_ALWAYS))
358      {
359         ERR("Invalid Render Policy.\n");
360         return EINA_FALSE;
361      }
362    if (wd->render_policy == policy) return EINA_TRUE;
363    wd->render_policy = policy;
364    _set_render_policy_callback(obj);
365    _glview_update_surface(obj);
366    return EINA_TRUE;
367 }
368
369 EAPI void
370 elm_glview_size_set(Evas_Object *obj, int width, int height)
371 {
372    ELM_CHECK_WIDTYPE(obj, widtype);
373    Widget_Data *wd = elm_widget_data_get(obj);
374    if (!wd) return;
375
376    if ((width == wd->w) && (height == wd->h)) return;
377    wd->w = width;
378    wd->h = height;
379    _glview_update_surface(obj);
380    elm_glview_changed_set(obj);
381 }
382
383 EAPI void
384 elm_glview_size_get(const Evas_Object *obj, int *width, int *height)
385 {
386    ELM_CHECK_WIDTYPE(obj, widtype);
387    Widget_Data *wd = elm_widget_data_get(obj);
388    if (!wd) return;
389
390    if (width) *width = wd->w;
391    if (height) *height = wd->h;
392 }
393
394 EAPI void
395 elm_glview_init_func_set(Evas_Object *obj, Elm_GLView_Func func)
396 {
397    ELM_CHECK_WIDTYPE(obj, widtype);
398    Widget_Data *wd = elm_widget_data_get(obj);
399    if (!wd) return;
400
401    wd->initialized = EINA_FALSE;
402    wd->init_func = func;
403 }
404
405 EAPI void
406 elm_glview_del_func_set(Evas_Object *obj, Elm_GLView_Func func)
407 {
408    ELM_CHECK_WIDTYPE(obj, widtype);
409    Widget_Data *wd = elm_widget_data_get(obj);
410     if (!wd) return;
411
412    wd->del_func = func;
413 }
414
415 EAPI void
416 elm_glview_resize_func_set(Evas_Object *obj, Elm_GLView_Func func)
417 {
418    ELM_CHECK_WIDTYPE(obj, widtype);
419    Widget_Data *wd = elm_widget_data_get(obj);
420     if (!wd)
421      {
422         ERR("Invalid Widget Object.\n");
423         return;
424      }
425
426    wd->resize_func = func;
427 }
428
429 EAPI void
430 elm_glview_render_func_set(Evas_Object *obj, Elm_GLView_Func func)
431 {
432    ELM_CHECK_WIDTYPE(obj, widtype);
433    Widget_Data *wd = elm_widget_data_get(obj);
434    if (!wd) return;
435
436    wd->render_func = func;
437    _set_render_policy_callback(obj);
438 }
439
440 EAPI void
441 elm_glview_changed_set(Evas_Object *obj)
442 {
443    ELM_CHECK_WIDTYPE(obj, widtype);
444    Widget_Data *wd = elm_widget_data_get(obj);
445    if (!wd) return;
446
447    evas_object_image_pixels_dirty_set(wd->glview_image, EINA_TRUE);
448    if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ALWAYS)
449      {
450         if (!wd->render_idle_enterer)
451           wd->render_idle_enterer = ecore_idle_enterer_before_add((Ecore_Task_Cb)_render_cb, obj);
452      }
453 }
454
455 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/