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);
133 _render_cb(void *obj)
135 Widget_Data *wd = elm_widget_data_get(obj);
136 if (!wd) return EINA_FALSE;
139 if (!evas_gl_make_current(wd->evasgl, wd->surface, wd->context))
141 wd->render_idle_enterer = NULL;
142 ERR("Failed doing make current.\n");
146 // Call the init function if it hasn't been called already
147 if (!wd->initialized)
149 if (wd->init_func) wd->init_func(obj);
150 wd->initialized = EINA_TRUE;
155 if (wd->resize_func) wd->resize_func(obj);
156 wd->resized = EINA_FALSE;
159 // Call the render function
160 if (wd->render_func) wd->render_func(obj);
162 // Depending on the policy return true or false
163 if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ON_DEMAND)
165 else if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ALWAYS)
167 // Return false so it only runs once
168 wd->render_idle_enterer = NULL;
173 ERR("Invalid Render Policy.\n");
174 wd->render_idle_enterer = NULL;
181 _set_render_policy_callback(Evas_Object *obj)
183 Widget_Data *wd = elm_widget_data_get(obj);
185 switch (wd->render_policy)
187 case ELM_GLVIEW_RENDER_POLICY_ON_DEMAND:
188 // Delete idle_enterer if it for some reason is around
189 if (wd->render_idle_enterer)
191 ecore_idle_enterer_del(wd->render_idle_enterer);
192 wd->render_idle_enterer = NULL;
195 // Set pixel getter callback
196 evas_object_image_pixels_get_callback_set
197 (wd->glview_image, (Evas_Object_Image_Pixels_Get_Cb)_render_cb, obj);
199 case ELM_GLVIEW_RENDER_POLICY_ALWAYS:
200 // Unset the pixel getter callback if set already
201 evas_object_image_pixels_get_callback_set(wd->glview_image, NULL, NULL);
205 ERR("Invalid Render Policy.\n");
211 elm_glview_add(Evas_Object *parent)
217 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
219 ELM_SET_WIDTYPE(widtype, "glview");
220 elm_widget_type_set(obj, "glview");
221 elm_widget_sub_object_add(parent, obj);
222 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
223 elm_widget_data_set(obj, wd);
224 elm_widget_del_hook_set(obj, _del_hook);
227 wd->evasgl = evas_gl_new(e);
230 ERR("Failed Creating an Evas GL Object.\n");
234 // Create a default config
235 wd->config = evas_gl_config_new();
238 ERR("Failed Creating a Config Object.\n");
239 evas_gl_free(wd->evasgl);
242 wd->config->color_format = EVAS_GL_RGB_888;
244 // Create image to render Evas_GL Surface
245 wd->glview_image = evas_object_image_filled_add(e);
246 evas_object_image_size_set(wd->glview_image, 1, 1);
247 evas_object_event_callback_add(wd->glview_image, EVAS_CALLBACK_RESIZE,
248 _glview_resize, obj);
249 elm_widget_resize_object_set(obj, wd->glview_image);
250 evas_object_show(wd->glview_image);
252 // Initialize variables
254 wd->scale_policy = ELM_GLVIEW_RESIZE_POLICY_RECREATE;
255 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 if (!wd) return EINA_FALSE;
301 if (mode & ELM_GLVIEW_ALPHA)
302 wd->config->color_format = EVAS_GL_RGBA_8888;
304 wd->config->color_format = EVAS_GL_RGB_888;
306 if (mode & ELM_GLVIEW_DEPTH)
307 wd->config->depth_bits = EVAS_GL_DEPTH_BIT_24;
309 wd->config->depth_bits = EVAS_GL_DEPTH_NONE;
311 if (mode & ELM_GLVIEW_STENCIL)
312 wd->config->stencil_bits = EVAS_GL_STENCIL_BIT_8;
314 wd->config->stencil_bits = EVAS_GL_STENCIL_NONE;
316 if (mode & ELM_GLVIEW_DIRECT)
317 wd->config->options_bits = EVAS_GL_OPTIONS_DIRECT;
319 wd->config->options_bits = EVAS_GL_OPTIONS_NONE;
321 // Check for Alpha Channel and enable it
322 if (mode & ELM_GLVIEW_ALPHA)
323 evas_object_image_alpha_set(wd->glview_image, EINA_TRUE);
325 evas_object_image_alpha_set(wd->glview_image, EINA_FALSE);
329 elm_glview_changed_set(obj);
335 elm_glview_resize_policy_set(Evas_Object *obj, Elm_GLView_Resize_Policy policy)
337 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
338 Widget_Data *wd = elm_widget_data_get(obj);
339 if (!wd) return EINA_FALSE;
341 if (policy == wd->scale_policy) return EINA_TRUE;
344 case ELM_GLVIEW_RESIZE_POLICY_RECREATE:
345 case ELM_GLVIEW_RESIZE_POLICY_SCALE:
346 wd->scale_policy = policy;
347 _glview_update_surface(obj);
348 elm_glview_changed_set(obj);
351 ERR("Invalid Scale Policy.\n");
357 elm_glview_render_policy_set(Evas_Object *obj, Elm_GLView_Render_Policy policy)
359 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
360 Widget_Data *wd = elm_widget_data_get(obj);
361 if (!wd) return EINA_FALSE;
363 if ((policy != ELM_GLVIEW_RENDER_POLICY_ON_DEMAND) &&
364 (policy != ELM_GLVIEW_RENDER_POLICY_ALWAYS))
366 ERR("Invalid Render Policy.\n");
369 if (wd->render_policy == policy) return EINA_TRUE;
370 wd->render_policy = policy;
371 _set_render_policy_callback(obj);
372 _glview_update_surface(obj);
377 elm_glview_size_set(Evas_Object *obj, int w, int h)
379 ELM_CHECK_WIDTYPE(obj, widtype);
380 Widget_Data *wd = elm_widget_data_get(obj);
383 if ((w == wd->w) && (h == wd->h)) return;
386 _glview_update_surface(obj);
387 elm_glview_changed_set(obj);
391 elm_glview_size_get(const Evas_Object *obj, int *w, int *h)
393 ELM_CHECK_WIDTYPE(obj, widtype);
394 Widget_Data *wd = elm_widget_data_get(obj);
402 elm_glview_init_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
404 ELM_CHECK_WIDTYPE(obj, widtype);
405 Widget_Data *wd = elm_widget_data_get(obj);
408 wd->initialized = EINA_FALSE;
409 wd->init_func = func;
413 elm_glview_del_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
415 ELM_CHECK_WIDTYPE(obj, widtype);
416 Widget_Data *wd = elm_widget_data_get(obj);
423 elm_glview_resize_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
425 ELM_CHECK_WIDTYPE(obj, widtype);
426 Widget_Data *wd = elm_widget_data_get(obj);
429 ERR("Invalid Widget Object.\n");
433 wd->resize_func = func;
437 elm_glview_render_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
439 ELM_CHECK_WIDTYPE(obj, widtype);
440 Widget_Data *wd = elm_widget_data_get(obj);
443 wd->render_func = func;
444 _set_render_policy_callback(obj);
448 elm_glview_changed_set(Evas_Object *obj)
450 ELM_CHECK_WIDTYPE(obj, widtype);
451 Widget_Data *wd = elm_widget_data_get(obj);
454 evas_object_image_pixels_dirty_set(wd->glview_image, EINA_TRUE);
455 if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ALWAYS)
457 if (!wd->render_idle_enterer)
458 wd->render_idle_enterer = ecore_idle_enterer_before_add((Ecore_Task_Cb)_render_cb, obj);
462 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/