1 #include <Elementary.h>
4 typedef struct _Widget_Data Widget_Data;
8 Evas_Object *glview_image;
11 Elm_GLView_Resize_Policy scale_policy;
12 Elm_GLView_Render_Policy render_policy;
15 Evas_GL_Config config;
16 Evas_GL_Surface *surface;
17 Evas_GL_Context *context;
21 Elm_GLView_Func init_func;
22 Elm_GLView_Func del_func;
23 Elm_GLView_Func resize_func;
24 Elm_GLView_Func render_func;
26 Ecore_Idle_Enterer *render_idle_enterer;
28 Eina_Bool initialized;
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);
36 static const char SIG_FOCUSED[] = "focused";
37 static const char SIG_UNFOCUSED[] = "unfocused";
40 _del_hook(Evas_Object *obj)
42 Widget_Data *wd = elm_widget_data_get(obj);
45 // Call delete func if it's registered
48 evas_gl_make_current(wd->evasgl, wd->surface, wd->context);
52 if (wd->render_idle_enterer) ecore_idle_enterer_del(wd->render_idle_enterer);
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);
62 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
64 Widget_Data *wd = elm_widget_data_get(obj);
67 if (elm_widget_focus_get(obj))
69 evas_object_focus_set(wd->glview_image, EINA_TRUE);
70 evas_object_smart_callback_call(obj, SIG_FOCUSED, NULL);
74 evas_object_focus_set(wd->glview_image, EINA_FALSE);
75 evas_object_smart_callback_call(obj, SIG_UNFOCUSED, NULL);
80 _glview_update_surface(Evas_Object *obj)
82 Widget_Data *wd = elm_widget_data_get(obj);
87 evas_object_image_native_surface_set(wd->glview_image, NULL);
88 evas_gl_surface_destroy(wd->evasgl, wd->surface);
92 evas_object_image_size_set(wd->glview_image, wd->w, wd->h);
96 Evas_Native_Surface ns;
98 wd->surface = evas_gl_surface_create(wd->evasgl, &wd->config,
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);
107 _glview_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
109 Widget_Data *wd = elm_widget_data_get(data);
114 wd->resized = EINA_TRUE;
116 if (wd->scale_policy == ELM_GLVIEW_RESIZE_POLICY_RECREATE)
118 evas_object_geometry_get(wd->glview_image, NULL, NULL, &w, &h);
119 if ((w == 0) || (h == 0))
124 if ((wd->w == w) && (wd->h == h)) return;
127 _glview_update_surface(data);
131 evas_gl_make_current(wd->evasgl, wd->surface, wd->context);
132 wd->render_func(data);
139 _render_cb(void *obj)
141 Widget_Data *wd = elm_widget_data_get(obj);
142 if (!wd) return EINA_FALSE;
145 if (!evas_gl_make_current(wd->evasgl, wd->surface, wd->context))
147 wd->render_idle_enterer = NULL;
148 ERR("Failed doing make current.\n");
152 // Call the init function if it hasn't been called already
153 if (!wd->initialized)
155 if (wd->init_func) wd->init_func(obj);
156 wd->initialized = EINA_TRUE;
161 if (wd->resize_func) wd->resize_func(obj);
162 wd->resized = EINA_FALSE;
165 // Call the render function
166 if (wd->render_func) wd->render_func(obj);
168 // Depending on the policy return true or false
169 if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ON_DEMAND)
171 else if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ALWAYS)
173 // Return false so it only runs once
174 wd->render_idle_enterer = NULL;
179 ERR("Invalid Render Policy.\n");
180 wd->render_idle_enterer = NULL;
187 _set_render_policy_callback(Evas_Object *obj)
189 Widget_Data *wd = elm_widget_data_get(obj);
191 switch (wd->render_policy)
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)
197 ecore_idle_enterer_del(wd->render_idle_enterer);
198 wd->render_idle_enterer = NULL;
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);
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);
211 ERR("Invalid Render Policy.\n");
217 elm_glview_add(Evas_Object *parent)
222 Evas_GL_Config cfg = { EVAS_GL_RGB_8,
224 EVAS_GL_STENCIL_NONE };
226 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
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);
236 wd->evasgl = evas_gl_new(e);
239 ERR("Failed Creating an Evas GL Object.\n");
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);
251 // Initialize variables
253 wd->scale_policy = ELM_GLVIEW_RESIZE_POLICY_RECREATE;
254 wd->render_policy = ELM_GLVIEW_RENDER_POLICY_ON_DEMAND;
258 // Initialize it to (64,64) (It's an arbitrary value)
262 // Initialize the rest of the values
263 wd->init_func = NULL;
265 wd->render_func = NULL;
266 wd->render_idle_enterer = NULL;
267 wd->initialized = EINA_FALSE;
268 wd->resized = EINA_FALSE;
273 wd->context = evas_gl_context_create(wd->evasgl, NULL);
276 ERR("Error Creating an Evas_GL Context.\n");
284 elm_glview_gl_api_get(const Evas_Object *obj)
286 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
287 Widget_Data *wd = elm_widget_data_get(obj);
288 if (!wd) return NULL;
290 return evas_gl_api_get(wd->evasgl);
294 elm_glview_mode_set(Evas_Object *obj, Elm_GLView_Mode mode)
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,
300 EVAS_GL_STENCIL_NONE };
301 if (!wd) return EINA_FALSE;
304 if (mode & ELM_GLVIEW_ALPHA)
305 cfg.color_format = EVAS_GL_RGBA_8;
307 if (mode & ELM_GLVIEW_DEPTH)
308 cfg.depth_bits = EVAS_GL_DEPTH_BIT_24;
310 if (mode & ELM_GLVIEW_STENCIL)
311 cfg.stencil_bits = EVAS_GL_STENCIL_BIT_8;
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);
317 evas_object_image_alpha_set(wd->glview_image, EINA_FALSE);
322 elm_glview_changed_set(obj);
328 elm_glview_resize_policy_set(Evas_Object *obj, Elm_GLView_Resize_Policy policy)
330 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
331 Widget_Data *wd = elm_widget_data_get(obj);
332 if (!wd) return EINA_FALSE;
334 if (policy == wd->scale_policy) return EINA_TRUE;
337 case ELM_GLVIEW_RESIZE_POLICY_RECREATE:
338 case ELM_GLVIEW_RESIZE_POLICY_SCALE:
339 wd->scale_policy = policy;
340 _glview_update_surface(obj);
341 elm_glview_changed_set(obj);
344 ERR("Invalid Scale Policy.\n");
350 elm_glview_render_policy_set(Evas_Object *obj, Elm_GLView_Render_Policy policy)
352 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
353 Widget_Data *wd = elm_widget_data_get(obj);
354 if (!wd) return EINA_FALSE;
356 if ((policy != ELM_GLVIEW_RENDER_POLICY_ON_DEMAND) &&
357 (policy != ELM_GLVIEW_RENDER_POLICY_ALWAYS))
359 ERR("Invalid Render Policy.\n");
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);
370 elm_glview_size_set(Evas_Object *obj, int width, int height)
372 ELM_CHECK_WIDTYPE(obj, widtype);
373 Widget_Data *wd = elm_widget_data_get(obj);
376 if ((width == wd->w) && (height == wd->h)) return;
379 _glview_update_surface(obj);
380 elm_glview_changed_set(obj);
384 elm_glview_size_get(const Evas_Object *obj, int *width, int *height)
386 ELM_CHECK_WIDTYPE(obj, widtype);
387 Widget_Data *wd = elm_widget_data_get(obj);
390 if (width) *width = wd->w;
391 if (height) *height = wd->h;
395 elm_glview_init_func_set(Evas_Object *obj, Elm_GLView_Func func)
397 ELM_CHECK_WIDTYPE(obj, widtype);
398 Widget_Data *wd = elm_widget_data_get(obj);
401 wd->initialized = EINA_FALSE;
402 wd->init_func = func;
406 elm_glview_del_func_set(Evas_Object *obj, Elm_GLView_Func func)
408 ELM_CHECK_WIDTYPE(obj, widtype);
409 Widget_Data *wd = elm_widget_data_get(obj);
416 elm_glview_resize_func_set(Evas_Object *obj, Elm_GLView_Func func)
418 ELM_CHECK_WIDTYPE(obj, widtype);
419 Widget_Data *wd = elm_widget_data_get(obj);
422 ERR("Invalid Widget Object.\n");
426 wd->resize_func = func;
430 elm_glview_render_func_set(Evas_Object *obj, Elm_GLView_Func func)
432 ELM_CHECK_WIDTYPE(obj, widtype);
433 Widget_Data *wd = elm_widget_data_get(obj);
436 wd->render_func = func;
437 _set_render_policy_callback(obj);
441 elm_glview_changed_set(Evas_Object *obj)
443 ELM_CHECK_WIDTYPE(obj, widtype);
444 Widget_Data *wd = elm_widget_data_get(obj);
447 evas_object_image_pixels_dirty_set(wd->glview_image, EINA_TRUE);
448 if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ALWAYS)
450 if (!wd->render_idle_enterer)
451 wd->render_idle_enterer = ecore_idle_enterer_before_add((Ecore_Task_Cb)_render_cb, obj);
455 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/