1 #include <Elementary.h>
4 typedef struct _Widget_Data Widget_Data;
9 Evas_Object *glview_image;
11 Evas_Object *glview_image;
12 >>>>>>> remotes/origin/upstream
15 Elm_GLView_Resize_Policy scale_policy;
16 Elm_GLView_Render_Policy render_policy;
19 Evas_GL_Config *config;
20 Evas_GL_Surface *surface;
21 Evas_GL_Context *context;
25 Elm_GLView_Func_Cb init_func;
26 Elm_GLView_Func_Cb del_func;
27 Elm_GLView_Func_Cb resize_func;
28 Elm_GLView_Func_Cb render_func;
30 Ecore_Idle_Enterer *render_idle_enterer;
32 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 // Call delete func if it's registered
52 evas_gl_make_current(wd->evasgl, wd->surface, wd->context);
56 if (wd->render_idle_enterer) ecore_idle_enterer_del(wd->render_idle_enterer);
58 if (wd->surface) evas_gl_surface_destroy(wd->evasgl, wd->surface);
59 if (wd->context) evas_gl_context_destroy(wd->evasgl, wd->context);
60 if (wd->config) evas_gl_config_free(wd->config);
61 if (wd->evasgl) evas_gl_free(wd->evasgl);
67 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
69 Widget_Data *wd = elm_widget_data_get(obj);
72 if (elm_widget_focus_get(obj))
74 evas_object_focus_set(wd->glview_image, EINA_TRUE);
75 evas_object_smart_callback_call(obj, SIG_FOCUSED, NULL);
79 evas_object_focus_set(wd->glview_image, EINA_FALSE);
80 evas_object_smart_callback_call(obj, SIG_UNFOCUSED, NULL);
85 _glview_update_surface(Evas_Object *obj)
87 Widget_Data *wd = elm_widget_data_get(obj);
92 evas_object_image_native_surface_set(wd->glview_image, NULL);
93 evas_gl_surface_destroy(wd->evasgl, wd->surface);
97 evas_object_image_size_set(wd->glview_image, wd->w, wd->h);
101 Evas_Native_Surface ns;
103 wd->surface = evas_gl_surface_create(wd->evasgl, wd->config,
105 evas_gl_native_surface_get(wd->evasgl, wd->surface, &ns);
106 evas_object_image_native_surface_set(wd->glview_image, &ns);
107 elm_glview_changed_set(obj);
112 _glview_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
114 Widget_Data *wd = elm_widget_data_get(data);
119 wd->resized = EINA_TRUE;
121 if (wd->scale_policy == ELM_GLVIEW_RESIZE_POLICY_RECREATE)
123 evas_object_geometry_get(wd->glview_image, NULL, NULL, &w, &h);
124 if ((w == 0) || (h == 0))
129 if ((wd->w == w) && (wd->h == h)) return;
132 _glview_update_surface(data);
137 evas_gl_make_current(wd->evasgl, wd->surface, wd->context);
138 wd->render_func(data);
142 >>>>>>> remotes/origin/upstream
147 _render_cb(void *obj)
149 Widget_Data *wd = elm_widget_data_get(obj);
150 if (!wd) return EINA_FALSE;
153 if (!evas_gl_make_current(wd->evasgl, wd->surface, wd->context))
155 wd->render_idle_enterer = NULL;
156 ERR("Failed doing make current.\n");
160 // Call the init function if it hasn't been called already
161 if (!wd->initialized)
163 if (wd->init_func) wd->init_func(obj);
164 wd->initialized = EINA_TRUE;
169 if (wd->resize_func) wd->resize_func(obj);
170 wd->resized = EINA_FALSE;
173 // Call the render function
174 if (wd->render_func) wd->render_func(obj);
176 // Depending on the policy return true or false
177 if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ON_DEMAND)
179 else if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ALWAYS)
181 // Return false so it only runs once
182 wd->render_idle_enterer = NULL;
187 ERR("Invalid Render Policy.\n");
188 wd->render_idle_enterer = NULL;
195 _set_render_policy_callback(Evas_Object *obj)
197 Widget_Data *wd = elm_widget_data_get(obj);
199 switch (wd->render_policy)
201 case ELM_GLVIEW_RENDER_POLICY_ON_DEMAND:
202 // Delete idle_enterer if it for some reason is around
203 if (wd->render_idle_enterer)
205 ecore_idle_enterer_del(wd->render_idle_enterer);
206 wd->render_idle_enterer = NULL;
209 // Set pixel getter callback
210 evas_object_image_pixels_get_callback_set
211 (wd->glview_image, (Evas_Object_Image_Pixels_Get_Cb)_render_cb, obj);
213 case ELM_GLVIEW_RENDER_POLICY_ALWAYS:
214 // Unset the pixel getter callback if set already
215 evas_object_image_pixels_get_callback_set(wd->glview_image, NULL, NULL);
219 ERR("Invalid Render Policy.\n");
225 elm_glview_add(Evas_Object *parent)
231 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
233 ELM_SET_WIDTYPE(widtype, "glview");
234 elm_widget_type_set(obj, "glview");
235 elm_widget_sub_object_add(parent, obj);
236 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
237 elm_widget_data_set(obj, wd);
238 elm_widget_del_hook_set(obj, _del_hook);
241 wd->evasgl = evas_gl_new(e);
244 ERR("Failed Creating an Evas GL Object.\n");
248 // Create a default config
249 wd->config = evas_gl_config_new();
252 ERR("Failed Creating a Config Object.\n");
253 evas_gl_free(wd->evasgl);
256 wd->config->color_format = EVAS_GL_RGB_888;
258 // Create image to render Evas_GL Surface
259 wd->glview_image = evas_object_image_filled_add(e);
260 evas_object_image_size_set(wd->glview_image, 1, 1);
261 evas_object_event_callback_add(wd->glview_image, EVAS_CALLBACK_RESIZE,
262 _glview_resize, obj);
263 elm_widget_resize_object_set(obj, wd->glview_image);
264 evas_object_show(wd->glview_image);
266 // Initialize variables
268 wd->scale_policy = ELM_GLVIEW_RESIZE_POLICY_RECREATE;
269 wd->render_policy = ELM_GLVIEW_RENDER_POLICY_ON_DEMAND;
272 // Initialize it to (64,64) (It's an arbitrary value)
276 // Initialize the rest of the values
277 wd->init_func = NULL;
279 wd->render_func = NULL;
280 wd->render_idle_enterer = NULL;
281 wd->initialized = EINA_FALSE;
282 wd->resized = EINA_FALSE;
287 wd->context = evas_gl_context_create(wd->evasgl, NULL);
290 ERR("Error Creating an Evas_GL Context.\n");
298 elm_glview_gl_api_get(const Evas_Object *obj)
300 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
301 Widget_Data *wd = elm_widget_data_get(obj);
302 if (!wd) return NULL;
304 return evas_gl_api_get(wd->evasgl);
308 elm_glview_mode_set(Evas_Object *obj, Elm_GLView_Mode mode)
310 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
311 Widget_Data *wd = elm_widget_data_get(obj);
312 if (!wd) return EINA_FALSE;
315 if (mode & ELM_GLVIEW_ALPHA)
316 wd->config->color_format = EVAS_GL_RGBA_8888;
318 wd->config->color_format = EVAS_GL_RGB_888;
320 if (mode & ELM_GLVIEW_DEPTH)
321 wd->config->depth_bits = EVAS_GL_DEPTH_BIT_24;
323 wd->config->depth_bits = EVAS_GL_DEPTH_NONE;
325 if (mode & ELM_GLVIEW_STENCIL)
326 wd->config->stencil_bits = EVAS_GL_STENCIL_BIT_8;
328 wd->config->stencil_bits = EVAS_GL_STENCIL_NONE;
330 if (mode & ELM_GLVIEW_DIRECT)
331 wd->config->options_bits = EVAS_GL_OPTIONS_DIRECT;
333 wd->config->options_bits = EVAS_GL_OPTIONS_NONE;
335 // Check for Alpha Channel and enable it
336 if (mode & ELM_GLVIEW_ALPHA)
337 evas_object_image_alpha_set(wd->glview_image, EINA_TRUE);
339 evas_object_image_alpha_set(wd->glview_image, EINA_FALSE);
343 elm_glview_changed_set(obj);
349 elm_glview_resize_policy_set(Evas_Object *obj, Elm_GLView_Resize_Policy policy)
351 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
352 Widget_Data *wd = elm_widget_data_get(obj);
353 if (!wd) return EINA_FALSE;
355 if (policy == wd->scale_policy) return EINA_TRUE;
358 case ELM_GLVIEW_RESIZE_POLICY_RECREATE:
359 case ELM_GLVIEW_RESIZE_POLICY_SCALE:
360 wd->scale_policy = policy;
361 _glview_update_surface(obj);
362 elm_glview_changed_set(obj);
365 ERR("Invalid Scale Policy.\n");
371 elm_glview_render_policy_set(Evas_Object *obj, Elm_GLView_Render_Policy policy)
373 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
374 Widget_Data *wd = elm_widget_data_get(obj);
375 if (!wd) return EINA_FALSE;
377 if ((policy != ELM_GLVIEW_RENDER_POLICY_ON_DEMAND) &&
378 (policy != ELM_GLVIEW_RENDER_POLICY_ALWAYS))
380 ERR("Invalid Render Policy.\n");
383 if (wd->render_policy == policy) return EINA_TRUE;
384 wd->render_policy = policy;
385 _set_render_policy_callback(obj);
386 _glview_update_surface(obj);
392 elm_glview_size_set(Evas_Object *obj, int width, int height)
394 elm_glview_size_set(Evas_Object *obj, int w, int h)
395 >>>>>>> remotes/origin/upstream
397 ELM_CHECK_WIDTYPE(obj, widtype);
398 Widget_Data *wd = elm_widget_data_get(obj);
402 if ((width == wd->w) && (height == wd->h)) return;
406 if ((w == wd->w) && (h == wd->h)) return;
409 >>>>>>> remotes/origin/upstream
410 _glview_update_surface(obj);
411 elm_glview_changed_set(obj);
416 elm_glview_size_get(const Evas_Object *obj, int *width, int *height)
418 elm_glview_size_get(const Evas_Object *obj, int *w, int *h)
419 >>>>>>> remotes/origin/upstream
421 ELM_CHECK_WIDTYPE(obj, widtype);
422 Widget_Data *wd = elm_widget_data_get(obj);
426 if (width) *width = wd->w;
427 if (height) *height = wd->h;
431 >>>>>>> remotes/origin/upstream
435 elm_glview_init_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
437 ELM_CHECK_WIDTYPE(obj, widtype);
438 Widget_Data *wd = elm_widget_data_get(obj);
441 wd->initialized = EINA_FALSE;
442 wd->init_func = func;
446 elm_glview_del_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
448 ELM_CHECK_WIDTYPE(obj, widtype);
449 Widget_Data *wd = elm_widget_data_get(obj);
456 elm_glview_resize_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
458 ELM_CHECK_WIDTYPE(obj, widtype);
459 Widget_Data *wd = elm_widget_data_get(obj);
462 ERR("Invalid Widget Object.\n");
466 wd->resize_func = func;
470 elm_glview_render_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
472 ELM_CHECK_WIDTYPE(obj, widtype);
473 Widget_Data *wd = elm_widget_data_get(obj);
476 wd->render_func = func;
477 _set_render_policy_callback(obj);
481 elm_glview_changed_set(Evas_Object *obj)
483 ELM_CHECK_WIDTYPE(obj, widtype);
484 Widget_Data *wd = elm_widget_data_get(obj);
487 evas_object_image_pixels_dirty_set(wd->glview_image, EINA_TRUE);
488 if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ALWAYS)
490 if (!wd->render_idle_enterer)
491 wd->render_idle_enterer = ecore_idle_enterer_before_add((Ecore_Task_Cb)_render_cb, obj);
495 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/