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_Cb init_func;
22 Elm_GLView_Func_Cb del_func;
23 Elm_GLView_Func_Cb resize_func;
24 Elm_GLView_Func_Cb 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->config) evas_gl_config_free(wd->config);
57 if (wd->evasgl) evas_gl_free(wd->evasgl);
63 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
65 Widget_Data *wd = elm_widget_data_get(obj);
68 if (elm_widget_focus_get(obj))
70 evas_object_focus_set(wd->glview_image, EINA_TRUE);
71 evas_object_smart_callback_call(obj, SIG_FOCUSED, NULL);
75 evas_object_focus_set(wd->glview_image, EINA_FALSE);
76 evas_object_smart_callback_call(obj, SIG_UNFOCUSED, NULL);
81 _glview_update_surface(Evas_Object *obj)
83 Widget_Data *wd = elm_widget_data_get(obj);
88 evas_object_image_native_surface_set(wd->glview_image, NULL);
89 evas_gl_surface_destroy(wd->evasgl, wd->surface);
93 evas_object_image_size_set(wd->glview_image, wd->w, wd->h);
97 Evas_Native_Surface ns;
99 wd->surface = evas_gl_surface_create(wd->evasgl, wd->config,
101 evas_gl_native_surface_get(wd->evasgl, wd->surface, &ns);
102 evas_object_image_native_surface_set(wd->glview_image, &ns);
103 elm_glview_changed_set(obj);
108 _glview_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
110 Widget_Data *wd = elm_widget_data_get(data);
115 wd->resized = EINA_TRUE;
117 if (wd->scale_policy == ELM_GLVIEW_RESIZE_POLICY_RECREATE)
119 evas_object_geometry_get(wd->glview_image, NULL, NULL, &w, &h);
120 if ((w == 0) || (h == 0))
125 if ((wd->w == w) && (wd->h == h)) return;
128 _glview_update_surface(data);
132 evas_gl_make_current(wd->evasgl, wd->surface, wd->context);
133 wd->render_func(data);
140 _render_cb(void *obj)
142 Widget_Data *wd = elm_widget_data_get(obj);
143 if (!wd) return EINA_FALSE;
146 if (!evas_gl_make_current(wd->evasgl, wd->surface, wd->context))
148 wd->render_idle_enterer = NULL;
149 ERR("Failed doing make current.\n");
153 // Call the init function if it hasn't been called already
154 if (!wd->initialized)
156 if (wd->init_func) wd->init_func(obj);
157 wd->initialized = EINA_TRUE;
162 if (wd->resize_func) wd->resize_func(obj);
163 wd->resized = EINA_FALSE;
166 // Call the render function
167 if (wd->render_func) wd->render_func(obj);
169 // Depending on the policy return true or false
170 if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ON_DEMAND)
172 else if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ALWAYS)
174 // Return false so it only runs once
175 wd->render_idle_enterer = NULL;
180 ERR("Invalid Render Policy.\n");
181 wd->render_idle_enterer = NULL;
188 _set_render_policy_callback(Evas_Object *obj)
190 Widget_Data *wd = elm_widget_data_get(obj);
192 switch (wd->render_policy)
194 case ELM_GLVIEW_RENDER_POLICY_ON_DEMAND:
195 // Delete idle_enterer if it for some reason is around
196 if (wd->render_idle_enterer)
198 ecore_idle_enterer_del(wd->render_idle_enterer);
199 wd->render_idle_enterer = NULL;
202 // Set pixel getter callback
203 evas_object_image_pixels_get_callback_set
204 (wd->glview_image, (Evas_Object_Image_Pixels_Get_Cb)_render_cb, obj);
206 case ELM_GLVIEW_RENDER_POLICY_ALWAYS:
207 // Unset the pixel getter callback if set already
208 evas_object_image_pixels_get_callback_set(wd->glview_image, NULL, NULL);
212 ERR("Invalid Render Policy.\n");
218 elm_glview_add(Evas_Object *parent)
224 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
226 ELM_SET_WIDTYPE(widtype, "glview");
227 elm_widget_type_set(obj, "glview");
228 elm_widget_sub_object_add(parent, obj);
229 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
230 elm_widget_data_set(obj, wd);
231 elm_widget_del_hook_set(obj, _del_hook);
234 wd->evasgl = evas_gl_new(e);
237 ERR("Failed Creating an Evas GL Object.\n");
241 // Create a default config
242 wd->config = evas_gl_config_new();
245 ERR("Failed Creating a Config Object.\n");
246 evas_gl_free(wd->evasgl);
249 wd->config->color_format = EVAS_GL_RGB_888;
251 // Create image to render Evas_GL Surface
252 wd->glview_image = evas_object_image_filled_add(e);
253 evas_object_image_size_set(wd->glview_image, 1, 1);
254 evas_object_event_callback_add(wd->glview_image, EVAS_CALLBACK_RESIZE,
255 _glview_resize, obj);
256 elm_widget_resize_object_set(obj, wd->glview_image);
257 evas_object_show(wd->glview_image);
259 // Initialize variables
261 wd->scale_policy = ELM_GLVIEW_RESIZE_POLICY_RECREATE;
262 wd->render_policy = ELM_GLVIEW_RENDER_POLICY_ON_DEMAND;
265 // Initialize it to (64,64) (It's an arbitrary value)
269 // Initialize the rest of the values
270 wd->init_func = NULL;
272 wd->render_func = NULL;
273 wd->render_idle_enterer = NULL;
274 wd->initialized = EINA_FALSE;
275 wd->resized = EINA_FALSE;
280 wd->context = evas_gl_context_create(wd->evasgl, NULL);
283 ERR("Error Creating an Evas_GL Context.\n");
291 elm_glview_gl_api_get(const Evas_Object *obj)
293 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
294 Widget_Data *wd = elm_widget_data_get(obj);
295 if (!wd) return NULL;
297 return evas_gl_api_get(wd->evasgl);
301 elm_glview_mode_set(Evas_Object *obj, Elm_GLView_Mode mode)
303 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
304 Widget_Data *wd = elm_widget_data_get(obj);
305 if (!wd) return EINA_FALSE;
308 if (mode & ELM_GLVIEW_ALPHA)
309 wd->config->color_format = EVAS_GL_RGBA_8888;
311 wd->config->color_format = EVAS_GL_RGB_888;
313 if (mode & ELM_GLVIEW_DEPTH)
314 wd->config->depth_bits = EVAS_GL_DEPTH_BIT_24;
316 wd->config->depth_bits = EVAS_GL_DEPTH_NONE;
318 if (mode & ELM_GLVIEW_STENCIL)
319 wd->config->stencil_bits = EVAS_GL_STENCIL_BIT_8;
321 wd->config->stencil_bits = EVAS_GL_STENCIL_NONE;
323 if (mode & ELM_GLVIEW_DIRECT)
324 wd->config->options_bits = EVAS_GL_OPTIONS_DIRECT;
326 wd->config->options_bits = EVAS_GL_OPTIONS_NONE;
328 // Check for Alpha Channel and enable it
329 if (mode & ELM_GLVIEW_ALPHA)
330 evas_object_image_alpha_set(wd->glview_image, EINA_TRUE);
332 evas_object_image_alpha_set(wd->glview_image, EINA_FALSE);
336 elm_glview_changed_set(obj);
342 elm_glview_resize_policy_set(Evas_Object *obj, Elm_GLView_Resize_Policy policy)
344 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
345 Widget_Data *wd = elm_widget_data_get(obj);
346 if (!wd) return EINA_FALSE;
348 if (policy == wd->scale_policy) return EINA_TRUE;
351 case ELM_GLVIEW_RESIZE_POLICY_RECREATE:
352 case ELM_GLVIEW_RESIZE_POLICY_SCALE:
353 wd->scale_policy = policy;
354 _glview_update_surface(obj);
355 elm_glview_changed_set(obj);
358 ERR("Invalid Scale Policy.\n");
364 elm_glview_render_policy_set(Evas_Object *obj, Elm_GLView_Render_Policy policy)
366 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
367 Widget_Data *wd = elm_widget_data_get(obj);
368 if (!wd) return EINA_FALSE;
370 if ((policy != ELM_GLVIEW_RENDER_POLICY_ON_DEMAND) &&
371 (policy != ELM_GLVIEW_RENDER_POLICY_ALWAYS))
373 ERR("Invalid Render Policy.\n");
376 if (wd->render_policy == policy) return EINA_TRUE;
377 wd->render_policy = policy;
378 _set_render_policy_callback(obj);
379 _glview_update_surface(obj);
384 elm_glview_size_set(Evas_Object *obj, int w, int h)
386 ELM_CHECK_WIDTYPE(obj, widtype);
387 Widget_Data *wd = elm_widget_data_get(obj);
390 if ((w == wd->w) && (h == wd->h)) return;
393 _glview_update_surface(obj);
394 elm_glview_changed_set(obj);
398 elm_glview_size_get(const Evas_Object *obj, int *w, int *h)
400 ELM_CHECK_WIDTYPE(obj, widtype);
401 Widget_Data *wd = elm_widget_data_get(obj);
409 elm_glview_init_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
411 ELM_CHECK_WIDTYPE(obj, widtype);
412 Widget_Data *wd = elm_widget_data_get(obj);
415 wd->initialized = EINA_FALSE;
416 wd->init_func = func;
420 elm_glview_del_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
422 ELM_CHECK_WIDTYPE(obj, widtype);
423 Widget_Data *wd = elm_widget_data_get(obj);
430 elm_glview_resize_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
432 ELM_CHECK_WIDTYPE(obj, widtype);
433 Widget_Data *wd = elm_widget_data_get(obj);
436 ERR("Invalid Widget Object.\n");
440 wd->resize_func = func;
444 elm_glview_render_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
446 ELM_CHECK_WIDTYPE(obj, widtype);
447 Widget_Data *wd = elm_widget_data_get(obj);
450 wd->render_func = func;
451 _set_render_policy_callback(obj);
455 elm_glview_changed_set(Evas_Object *obj)
457 ELM_CHECK_WIDTYPE(obj, widtype);
458 Widget_Data *wd = elm_widget_data_get(obj);
461 evas_object_image_pixels_dirty_set(wd->glview_image, EINA_TRUE);
462 if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ALWAYS)
464 if (!wd->render_idle_enterer)
465 wd->render_idle_enterer = ecore_idle_enterer_before_add((Ecore_Task_Cb)_render_cb, obj);
469 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/