1 #include <Elementary.h>
7 * A simple GLView widget that allows GL rendering.
9 * Signals that you can add callbacks for are:
11 * "clicked" - This is called when a user has clicked the image
13 typedef struct _Widget_Data Widget_Data;
17 Evas_Object *glview_image;
20 Elm_GLView_Resize_Policy scale_policy;
21 Elm_GLView_Render_Policy render_policy;
24 Evas_GL_Config config;
25 Evas_GL_Surface *surface;
26 Evas_GL_Context *context;
30 Elm_GLView_Func render_func;
31 Ecore_Idle_Enterer *render_idle_enterer;
33 Eina_Bool initialized;
36 static const char *widtype = NULL;
37 static void _del_hook(Evas_Object *obj);
38 static void _on_focus_hook(void *data, Evas_Object *obj);
40 static const char SIG_FOCUSED[] = "focused";
41 static const char SIG_UNFOCUSED[] = "unfocused";
44 _del_hook(Evas_Object *obj)
46 Widget_Data *wd = elm_widget_data_get(obj);
49 if (wd->render_idle_enterer) ecore_idle_enterer_del(wd->render_idle_enterer);
51 if (wd->surface) evas_gl_surface_destroy(wd->evasgl, wd->surface);
52 if (wd->context) evas_gl_context_destroy(wd->evasgl, wd->context);
53 if (wd->evasgl) evas_gl_free(wd->evasgl);
59 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
61 Widget_Data *wd = elm_widget_data_get(obj);
64 if (elm_widget_focus_get(obj))
66 evas_object_focus_set(wd->glview_image, EINA_TRUE);
67 evas_object_smart_callback_call(obj, SIG_FOCUSED, NULL);
71 evas_object_focus_set(wd->glview_image, EINA_FALSE);
72 evas_object_smart_callback_call(obj, SIG_UNFOCUSED, NULL);
77 _glview_update_surface(Evas_Object *obj)
79 Widget_Data *wd = elm_widget_data_get(obj);
84 evas_object_image_native_surface_set(wd->glview_image, NULL);
85 evas_gl_surface_destroy(wd->evasgl, wd->surface);
89 evas_object_image_size_set(wd->glview_image, wd->w, wd->h);
93 Evas_Native_Surface ns;
95 wd->surface = evas_gl_surface_create(wd->evasgl, &wd->config,
97 evas_gl_native_surface_get(wd->evasgl, wd->surface, &ns);
98 evas_object_image_native_surface_set(wd->glview_image, &ns);
99 elm_glview_changed_set(obj);
104 _glview_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
106 Widget_Data *wd = elm_widget_data_get(data);
110 if (wd->scale_policy == ELM_GLVIEW_RESIZE_POLICY_RECREATE)
112 evas_object_geometry_get(wd->glview_image, NULL, NULL, &w, &h);
113 if ((w == 0) || (h == 0))
118 if ((wd->w == w) && (wd->h == h)) return;
121 _glview_update_surface(data);
124 evas_gl_make_current(wd->evasgl, wd->surface, wd->context);
125 wd->render_func(data);
132 _render_cb(void *obj)
134 Widget_Data *wd = elm_widget_data_get(obj);
135 if (!wd) return EINA_FALSE;
138 if (!evas_gl_make_current(wd->evasgl, wd->surface, wd->context))
140 wd->render_idle_enterer = NULL;
144 // Call the render function
145 if (wd->render_func) wd->render_func(obj);
147 // Depending on the policy return true or false
148 if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ON_DEMAND)
150 else if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ALWAYS)
152 // Return false so it only runs once
153 wd->render_idle_enterer = NULL;
158 ERR("Invalid Render Policy.\n");
159 wd->render_idle_enterer = NULL;
166 _set_render_policy_callback(Evas_Object *obj)
168 Widget_Data *wd = elm_widget_data_get(obj);
170 switch (wd->render_policy)
172 case ELM_GLVIEW_RENDER_POLICY_ON_DEMAND:
173 // Delete idle_enterer if it for some reason is around
174 if (wd->render_idle_enterer)
176 ecore_idle_enterer_del(wd->render_idle_enterer);
177 wd->render_idle_enterer = NULL;
180 // Set pixel getter callback
181 evas_object_image_pixels_get_callback_set
182 (wd->glview_image, (Evas_Object_Image_Pixels_Get_Cb)_render_cb, obj);
184 case ELM_GLVIEW_RENDER_POLICY_ALWAYS:
186 // Unset the pixel getter callback if set already
187 evas_object_image_pixels_get_callback_set(wd->glview_image, NULL, NULL);
190 ERR("Invalid Render Policy.\n");
198 * Add a new glview to the parent
200 * @param parent The parent object
201 * @return The new object or NULL if it cannot be created
206 elm_glview_add(Evas_Object *parent)
211 Evas_GL_Config cfg = { EVAS_GL_RGB_8,
213 EVAS_GL_STENCIL_NONE };
215 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
217 ELM_SET_WIDTYPE(widtype, "glview");
218 elm_widget_type_set(obj, "glview");
219 elm_widget_sub_object_add(parent, obj);
220 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
221 elm_widget_data_set(obj, wd);
222 elm_widget_del_hook_set(obj, _del_hook);
225 wd->evasgl = evas_gl_new(e);
228 ERR("Failed Creating an Evas GL Object.\n");
232 // Create image to render Evas_GL Surface
233 wd->glview_image = evas_object_image_filled_add(e);
234 evas_object_image_size_set(wd->glview_image, 1, 1);
235 evas_object_event_callback_add(wd->glview_image, EVAS_CALLBACK_RESIZE,
236 _glview_resize, obj);
237 elm_widget_resize_object_set(obj, wd->glview_image);
238 evas_object_show(wd->glview_image);
240 // Initialize variables
242 wd->scale_policy = ELM_GLVIEW_RESIZE_POLICY_RECREATE;
243 wd->render_policy = ELM_GLVIEW_RENDER_POLICY_ON_DEMAND;
250 wd->render_idle_enterer = NULL;
255 wd->context = evas_gl_context_create(wd->evasgl, NULL);
258 ERR("Error Creating an Evas_GL Context.\n");
268 * Gets the gl api struct for gl rendering
270 * @param obj The glview object
271 * @return The api object or NULL if it cannot be created
276 elm_glview_gl_api_get(Evas_Object *obj)
278 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
279 Widget_Data *wd = elm_widget_data_get(obj);
281 if (!wd) return NULL;
283 return evas_gl_api_get(wd->evasgl);
288 * Set the mode of the GLView. Supports Three simple modes.
290 * @param obj The glview object
291 * @param mode The mode Options OR'ed enabling Alpha, Depth, Stencil.
292 * @return True if set properly.
297 elm_glview_mode_set(Evas_Object *obj, Elm_GLView_Mode mode)
299 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
300 Widget_Data *wd = elm_widget_data_get(obj);
301 Evas_GL_Config cfg = { EVAS_GL_RGBA_8,
303 EVAS_GL_STENCIL_NONE };
304 if (!wd) return EINA_FALSE;
307 if (mode & ELM_GLVIEW_ALPHA)
308 cfg.color_format = EVAS_GL_RGBA_8;
310 if (mode & ELM_GLVIEW_DEPTH)
311 cfg.depth_bits = EVAS_GL_DEPTH_BIT_24;
313 if (mode & ELM_GLVIEW_STENCIL)
314 cfg.stencil_bits = EVAS_GL_STENCIL_BIT_8;
316 // Check for Alpha Channel and enable it
317 if (mode & ELM_GLVIEW_ALPHA)
318 evas_object_image_alpha_set(wd->glview_image, EINA_TRUE);
320 evas_object_image_alpha_set(wd->glview_image, EINA_FALSE);
325 elm_glview_changed_set(obj);
331 * Set the scaling policy for the glview object.
333 * @param obj The glview object.
334 * @param policy The scaling policy.
336 * By default, the scaling policy is set to ELM_GLVIEW_RESIZE_POLICY_RECREATE.
337 * When resize is called it destroys the previous surface and recreates the newly
338 * specified size. If the policy is set to ELM_GLVIEW_RESIZE_POLICY_SCALE, however,
339 * glview only scales the image object and not the underlying GL Surface.
344 elm_glview_scale_policy_set(Evas_Object *obj, Elm_GLView_Resize_Policy policy)
346 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
347 Widget_Data *wd = elm_widget_data_get(obj);
348 if (!wd) return EINA_FALSE;
350 if (policy == wd->scale_policy) return EINA_TRUE;
353 case ELM_GLVIEW_RESIZE_POLICY_RECREATE:
354 case ELM_GLVIEW_RESIZE_POLICY_SCALE:
355 wd->scale_policy = policy;
358 ERR("Invalid Scale Policy.\n");
361 _glview_update_surface(obj);
362 elm_glview_changed_set(obj);
366 * Set the render policy for the glview object.
368 * @param obj The glview object.
369 * @param policy The render policy.
371 * By default, the render policy is set to ELM_GLVIEW_RENDER_POLICY_ON_DEMAND.
372 * This policy is set such that during the render loop, glview is only redrawn
373 * if it needs to be redrawn. (i.e. When it is visible) If the policy is set
374 * to ELM_GLVIEWW_RENDER_POLICY_ALWAYS, it redraws regardless of whether it is
375 * visible/need redrawing or not.
380 elm_glview_render_policy_set(Evas_Object *obj, Elm_GLView_Render_Policy policy)
382 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
383 Widget_Data *wd = elm_widget_data_get(obj);
384 if (!wd) return EINA_FALSE;
386 if ((policy != ELM_GLVIEW_RENDER_POLICY_ON_DEMAND) &&
387 (policy != ELM_GLVIEW_RENDER_POLICY_ALWAYS))
389 ERR("Invalid Render Policy.\n");
392 if (wd->render_policy == policy) return EINA_TRUE;
393 wd->render_policy = policy;
394 _set_render_policy_callback(obj);
395 _glview_update_surface(obj);
400 * Sets the size of the glview
402 * @param obj The glview object
403 * @param width width of the glview object
404 * @param height height of the glview object
409 elm_glview_size_set(Evas_Object *obj, int width, int height)
411 ELM_CHECK_WIDTYPE(obj, widtype);
412 Widget_Data *wd = elm_widget_data_get(obj);
415 if ((width == wd->w) && (height == wd->h)) return;
418 _glview_update_surface(obj);
419 elm_glview_changed_set(obj);
423 * Gets the size of the glview.
425 * @param obj The glview object
426 * @param width width of the glview object
427 * @param height height of the glview object
429 * Note that this function returns the actual image size of the glview.
430 * This means that when the scale policy is set to ELM_GLVIEW_RESIZE_POLICY_SCALE,
431 * it'll return the non-scaled size.
436 elm_glview_size_get(Evas_Object *obj, int *width, int *height)
438 ELM_CHECK_WIDTYPE(obj, widtype);
439 Widget_Data *wd = elm_widget_data_get(obj);
442 if (width) *width = wd->w;
443 if (height) *height = wd->h;
447 * Set the render function that runs in the main loop.
449 * @param obj The glview object.
450 * @param func The render function to be registered.
455 elm_glview_render_func_set(Evas_Object *obj, Elm_GLView_Func func)
457 ELM_CHECK_WIDTYPE(obj, widtype);
458 Widget_Data *wd = elm_widget_data_get(obj);
461 wd->render_func = func;
463 _set_render_policy_callback(obj);
468 * Notifies that there has been changes in the GLView.
470 * @param obj The glview object.
475 elm_glview_changed_set(Evas_Object *obj)
477 ELM_CHECK_WIDTYPE(obj, widtype);
478 Widget_Data *wd = elm_widget_data_get(obj);
482 evas_object_image_pixels_dirty_set(wd->glview_image, EINA_TRUE);
484 if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ALWAYS)
486 if (!wd->render_idle_enterer)
488 wd->render_idle_enterer = ecore_idle_enterer_before_add((Ecore_Task_Cb)_render_cb, obj);
493 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/