Elementary migration revision 70341
[framework/uifw/elementary.git] / src / lib / elm_glview.c
index 7a995d3..339f23f 100644 (file)
@@ -1,36 +1,32 @@
 #include <Elementary.h>
 #include "elm_priv.h"
 
-/**
- * @defgroup GLView
- *
- * A simple GLView widget that allows GL rendering.
- *
- * Signals that you can add callbacks for are:
- *
- * "clicked" - This is called when a user has clicked the image
- */
 typedef struct _Widget_Data Widget_Data;
 
 struct _Widget_Data
 {
-   Evas_Object            *glview_image;
+   Evas_Object              *glview_image;
 
-   Elm_GLView_Mode          mode;
+   Elm_GLView_Mode           mode;
    Elm_GLView_Resize_Policy  scale_policy;
-   Elm_GLView_Render_Policy render_policy;
+   Elm_GLView_Render_Policy  render_policy;
 
-   Evas_GL                 *evasgl;
-   Evas_GL_Config           config;
-   Evas_GL_Surface         *surface;
-   Evas_GL_Context         *context;
+   Evas_GL                  *evasgl;
+   Evas_GL_Config           *config;
+   Evas_GL_Surface          *surface;
+   Evas_GL_Context          *context;
 
-   Evas_Coord               w, h;
+   Evas_Coord                w, h;
 
-   Elm_GLView_Func          render_func;
-   Ecore_Idle_Enterer      *render_idle_enterer;
+   Elm_GLView_Func_Cb        init_func;
+   Elm_GLView_Func_Cb        del_func;
+   Elm_GLView_Func_Cb        resize_func;
+   Elm_GLView_Func_Cb        render_func;
 
-   Eina_Bool                initialized;
+   Ecore_Idle_Enterer       *render_idle_enterer;
+
+   Eina_Bool                 initialized;
+   Eina_Bool                 resized;
 };
 
 static const char *widtype = NULL;
@@ -46,10 +42,18 @@ _del_hook(Evas_Object *obj)
    Widget_Data *wd = elm_widget_data_get(obj);
    if (!wd) return;
 
+   // Call delete func if it's registered
+   if (wd->del_func)
+     {
+        evas_gl_make_current(wd->evasgl, wd->surface, wd->context);
+        wd->del_func(obj);
+     }
+
    if (wd->render_idle_enterer) ecore_idle_enterer_del(wd->render_idle_enterer);
 
    if (wd->surface) evas_gl_surface_destroy(wd->evasgl, wd->surface);
    if (wd->context) evas_gl_context_destroy(wd->evasgl, wd->context);
+   if (wd->config) evas_gl_config_free(wd->config);
    if (wd->evasgl) evas_gl_free(wd->evasgl);
 
    free(wd);
@@ -92,7 +96,7 @@ _glview_update_surface(Evas_Object *obj)
      {
         Evas_Native_Surface ns;
 
-        wd->surface = evas_gl_surface_create(wd->evasgl, &wd->config,
+        wd->surface = evas_gl_surface_create(wd->evasgl, wd->config,
                                              wd->w, wd->h);
         evas_gl_native_surface_get(wd->evasgl, wd->surface, &ns);
         evas_object_image_native_surface_set(wd->glview_image, &ns);
@@ -107,6 +111,9 @@ _glview_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void
    Evas_Coord w, h;
 
    if (!wd) return;
+
+   wd->resized = EINA_TRUE;
+
    if (wd->scale_policy == ELM_GLVIEW_RESIZE_POLICY_RECREATE)
      {
         evas_object_geometry_get(wd->glview_image, NULL, NULL, &w, &h);
@@ -119,15 +126,9 @@ _glview_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void
         wd->w = w;
         wd->h = h;
         _glview_update_surface(data);
-        if (wd->render_func)
-          {
-             evas_gl_make_current(wd->evasgl, wd->surface, wd->context);
-             wd->render_func(data);
-          }
      }
 }
 
-
 static Eina_Bool
 _render_cb(void *obj)
 {
@@ -138,15 +139,29 @@ _render_cb(void *obj)
    if (!evas_gl_make_current(wd->evasgl, wd->surface, wd->context))
      {
         wd->render_idle_enterer = NULL;
+        ERR("Failed doing make current.\n");
         return EINA_FALSE;
      }
 
+   // Call the init function if it hasn't been called already
+   if (!wd->initialized)
+     {
+        if (wd->init_func) wd->init_func(obj);
+        wd->initialized = EINA_TRUE;
+     }
+
+   if (wd->resized)
+     {
+        if (wd->resize_func) wd->resize_func(obj);
+        wd->resized = EINA_FALSE;
+     }
+
    // Call the render function
    if (wd->render_func) wd->render_func(obj);
 
    // Depending on the policy return true or false
    if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ON_DEMAND)
-      return EINA_TRUE;
+     return EINA_TRUE;
    else if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ALWAYS)
      {
         // Return false so it only runs once
@@ -170,47 +185,34 @@ _set_render_policy_callback(Evas_Object *obj)
    switch (wd->render_policy)
      {
       case ELM_GLVIEW_RENDER_POLICY_ON_DEMAND:
-        // Delete idle_enterer if it for some reason is around
-        if (wd->render_idle_enterer)
-          {
-             ecore_idle_enterer_del(wd->render_idle_enterer);
-             wd->render_idle_enterer = NULL;
-          }
-
-        // Set pixel getter callback
-        evas_object_image_pixels_get_callback_set
-           (wd->glview_image, (Evas_Object_Image_Pixels_Get_Cb)_render_cb, obj);
-        break;
+         // Delete idle_enterer if it for some reason is around
+         if (wd->render_idle_enterer)
+           {
+              ecore_idle_enterer_del(wd->render_idle_enterer);
+              wd->render_idle_enterer = NULL;
+           }
+
+         // Set pixel getter callback
+         evas_object_image_pixels_get_callback_set
+            (wd->glview_image, (Evas_Object_Image_Pixels_Get_Cb)_render_cb, obj);
+         break;
       case ELM_GLVIEW_RENDER_POLICY_ALWAYS:
+         // Unset the pixel getter callback if set already
+         evas_object_image_pixels_get_callback_set(wd->glview_image, NULL, NULL);
 
-        // Unset the pixel getter callback if set already
-        evas_object_image_pixels_get_callback_set(wd->glview_image, NULL, NULL);
-        break;
+         break;
       default:
-        ERR("Invalid Render Policy.\n");
-        return;
+         ERR("Invalid Render Policy.\n");
+         return;
      }
 }
 
-
-
-/**
- * Add a new glview to the parent
- *
- * @param parent The parent object
- * @return The new object or NULL if it cannot be created
- *
- * @ingroup GLView
- */
 EAPI Evas_Object *
 elm_glview_add(Evas_Object *parent)
 {
    Evas_Object *obj;
    Evas *e;
    Widget_Data *wd;
-   Evas_GL_Config cfg = { EVAS_GL_RGB_8,
-                          EVAS_GL_DEPTH_NONE,
-                          EVAS_GL_STENCIL_NONE };
 
    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
 
@@ -229,6 +231,16 @@ elm_glview_add(Evas_Object *parent)
         return NULL;
      }
 
+   // Create a default config
+   wd->config = evas_gl_config_new();
+   if (!wd->config)
+     {
+        ERR("Failed Creating a Config Object.\n");
+        evas_gl_free(wd->evasgl);
+        return NULL;
+     }
+   wd->config->color_format = EVAS_GL_RGB_888;
+
    // Create image to render Evas_GL Surface
    wd->glview_image = evas_object_image_filled_add(e);
    evas_object_image_size_set(wd->glview_image, 1, 1);
@@ -238,16 +250,22 @@ elm_glview_add(Evas_Object *parent)
    evas_object_show(wd->glview_image);
 
    // Initialize variables
-   wd->mode          = 0;
-   wd->scale_policy  = ELM_GLVIEW_RESIZE_POLICY_RECREATE;
-   wd->render_policy = ELM_GLVIEW_RENDER_POLICY_ON_DEMAND;
-   wd->config        = cfg;
-   wd->surface       = NULL;
-
-   wd->w             = 64;
-   wd->h             = 64;
-
+   wd->mode                = 0;
+   wd->scale_policy        = ELM_GLVIEW_RESIZE_POLICY_RECREATE;
+   wd->render_policy       = ELM_GLVIEW_RENDER_POLICY_ON_DEMAND;
+   wd->surface             = NULL;
+
+   // Initialize it to (64,64)  (It's an arbitrary value)
+   wd->w                   = 64;
+   wd->h                   = 64;
+
+   // Initialize the rest of the values
+   wd->init_func           = NULL;
+   wd->del_func            = NULL;
+   wd->render_func         = NULL;
    wd->render_idle_enterer = NULL;
+   wd->initialized         = EINA_FALSE;
+   wd->resized             = EINA_FALSE;
 
    // Create Context
    if (!wd->context)
@@ -259,89 +277,62 @@ elm_glview_add(Evas_Object *parent)
              return NULL;
           }
      }
-
    return obj;
-
 }
 
-/**
- * Gets the gl api struct for gl rendering
- *
- * @param obj The glview object
- * @return The api object or NULL if it cannot be created
- *
- * @ingroup GLView
- */
 EAPI Evas_GL_API *
 elm_glview_gl_api_get(const Evas_Object *obj)
 {
    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
    Widget_Data *wd = elm_widget_data_get(obj);
-
    if (!wd) return NULL;
 
    return evas_gl_api_get(wd->evasgl);
 }
 
-
-/**
- * Set the mode of the GLView. Supports Three simple modes.
- *
- * @param obj The glview object
- * @param mode The mode Options OR'ed enabling Alpha, Depth, Stencil.
- * @return True if set properly.
- *
- * @ingroup GLView
- */
 EAPI Eina_Bool
 elm_glview_mode_set(Evas_Object *obj, Elm_GLView_Mode mode)
 {
    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
    Widget_Data *wd = elm_widget_data_get(obj);
-   Evas_GL_Config cfg = { EVAS_GL_RGBA_8,
-                          EVAS_GL_DEPTH_NONE,
-                          EVAS_GL_STENCIL_NONE };
    if (!wd) return EINA_FALSE;
 
    // Set the configs
    if (mode & ELM_GLVIEW_ALPHA)
-      cfg.color_format = EVAS_GL_RGBA_8;
+      wd->config->color_format = EVAS_GL_RGBA_8888;
+   else
+      wd->config->color_format = EVAS_GL_RGB_888;
 
    if (mode & ELM_GLVIEW_DEPTH)
-      cfg.depth_bits = EVAS_GL_DEPTH_BIT_24;
+      wd->config->depth_bits = EVAS_GL_DEPTH_BIT_24;
+   else
+      wd->config->depth_bits = EVAS_GL_DEPTH_NONE;
 
    if (mode & ELM_GLVIEW_STENCIL)
-      cfg.stencil_bits = EVAS_GL_STENCIL_BIT_8;
+      wd->config->stencil_bits = EVAS_GL_STENCIL_BIT_8;
+   else
+      wd->config->stencil_bits = EVAS_GL_STENCIL_NONE;
+
+   if (mode & ELM_GLVIEW_DIRECT)
+      wd->config->options_bits = EVAS_GL_OPTIONS_DIRECT;
+   else
+      wd->config->options_bits = EVAS_GL_OPTIONS_NONE;
 
    // Check for Alpha Channel and enable it
    if (mode & ELM_GLVIEW_ALPHA)
-      evas_object_image_alpha_set(wd->glview_image, EINA_TRUE);
+     evas_object_image_alpha_set(wd->glview_image, EINA_TRUE);
    else
-      evas_object_image_alpha_set(wd->glview_image, EINA_FALSE);
+     evas_object_image_alpha_set(wd->glview_image, EINA_FALSE);
 
-   wd->mode   = mode;
-   wd->config = cfg;
+   wd->mode = mode;
 
    elm_glview_changed_set(obj);
 
    return EINA_TRUE;
 }
 
-/**
- * Set the scaling policy for the glview object.
- *
- * @param obj The glview object.
- * @param policy The scaling policy.
- *
- * By default, the scaling policy is set to ELM_GLVIEW_RESIZE_POLICY_RECREATE.
- * When resize is called it destroys the previous surface and recreates the newly
- * specified size. If the policy is set to ELM_GLVIEW_RESIZE_POLICY_SCALE, however,
- * glview only scales the image object and not the underlying GL Surface.
- *
- * @ingroup GLView
- */
 EAPI Eina_Bool
-elm_glview_scale_policy_set(Evas_Object *obj, Elm_GLView_Resize_Policy policy)
+elm_glview_resize_policy_set(Evas_Object *obj, Elm_GLView_Resize_Policy policy)
 {
    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
    Widget_Data *wd = elm_widget_data_get(obj);
@@ -352,30 +343,16 @@ elm_glview_scale_policy_set(Evas_Object *obj, Elm_GLView_Resize_Policy policy)
      {
       case ELM_GLVIEW_RESIZE_POLICY_RECREATE:
       case ELM_GLVIEW_RESIZE_POLICY_SCALE:
-        wd->scale_policy = policy;
-        return EINA_TRUE;
+         wd->scale_policy = policy;
+         _glview_update_surface(obj);
+         elm_glview_changed_set(obj);
+         return EINA_TRUE;
       default:
-        ERR("Invalid Scale Policy.\n");
-        return EINA_FALSE;
+         ERR("Invalid Scale Policy.\n");
+         return EINA_FALSE;
      }
-   _glview_update_surface(obj);
-   elm_glview_changed_set(obj);
 }
 
-/**
- * Set the render policy for the glview object.
- *
- * @param obj The glview object.
- * @param policy The render policy.
- *
- * By default, the render policy is set to ELM_GLVIEW_RENDER_POLICY_ON_DEMAND.
- * This policy is set such that during the render loop, glview is only redrawn
- * if it needs to be redrawn. (i.e. When it is visible) If the policy is set
- * to ELM_GLVIEWW_RENDER_POLICY_ALWAYS, it redraws regardless of whether it is
- * visible/need redrawing or not.
- *
- * @ingroup GLView
- */
 EAPI Eina_Bool
 elm_glview_render_policy_set(Evas_Object *obj, Elm_GLView_Render_Policy policy)
 {
@@ -396,98 +373,90 @@ elm_glview_render_policy_set(Evas_Object *obj, Elm_GLView_Render_Policy policy)
    return EINA_TRUE;
 }
 
-/**
- * Sets the size of the glview
- *
- * @param obj The glview object
- * @param width width of the glview object
- * @param height height of the glview object
- *
- * @ingroup GLView
- */
 EAPI void
-elm_glview_size_set(Evas_Object *obj, int width, int height)
+elm_glview_size_set(Evas_Object *obj, int w, int h)
 {
    ELM_CHECK_WIDTYPE(obj, widtype);
    Widget_Data *wd = elm_widget_data_get(obj);
    if (!wd) return;
 
-   if ((width == wd->w) && (height == wd->h)) return;
-   wd->w = width;
-   wd->h = height;
+   if ((w == wd->w) && (h == wd->h)) return;
+   wd->w = w;
+   wd->h = h;
    _glview_update_surface(obj);
    elm_glview_changed_set(obj);
 }
 
-/**
- * Gets the size of the glview.
- *
- * @param obj The glview object
- * @param width width of the glview object
- * @param height height of the glview object
- *
- * Note that this function returns the actual image size of the glview.
- * This means that when the scale policy is set to ELM_GLVIEW_RESIZE_POLICY_SCALE,
- * it'll return the non-scaled size.
- *
- * @ingroup GLView
- */
 EAPI void
-elm_glview_size_get(const Evas_Object *obj, int *width, int *height)
+elm_glview_size_get(const Evas_Object *obj, int *w, int *h)
 {
    ELM_CHECK_WIDTYPE(obj, widtype);
    Widget_Data *wd = elm_widget_data_get(obj);
    if (!wd) return;
 
-   if (width) *width = wd->w;
-   if (height) *height = wd->h;
+   if (w) *w = wd->w;
+   if (h) *h = wd->h;
 }
 
-/**
- * Set the render function that runs in the main loop.
- *
- * @param obj The glview object.
- * @param func The render function to be registered.
- *
- * @ingroup GLView
- */
 EAPI void
-elm_glview_render_func_set(Evas_Object *obj, Elm_GLView_Func func)
+elm_glview_init_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
 {
    ELM_CHECK_WIDTYPE(obj, widtype);
    Widget_Data *wd = elm_widget_data_get(obj);
    if (!wd) return;
 
-   wd->render_func = func;
+   wd->initialized = EINA_FALSE;
+   wd->init_func = func;
+}
 
-   _set_render_policy_callback(obj);
+EAPI void
+elm_glview_del_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
+{
+   ELM_CHECK_WIDTYPE(obj, widtype);
+   Widget_Data *wd = elm_widget_data_get(obj);
+    if (!wd) return;
+
+   wd->del_func = func;
 }
 
+EAPI void
+elm_glview_resize_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
+{
+   ELM_CHECK_WIDTYPE(obj, widtype);
+   Widget_Data *wd = elm_widget_data_get(obj);
+    if (!wd)
+     {
+        ERR("Invalid Widget Object.\n");
+        return;
+     }
+
+   wd->resize_func = func;
+}
 
-/**
- * Notifies that there has been changes in the GLView.
- *
- * @param obj The glview object.
- *
- * @ingroup GLView
- */
 EAPI void
-elm_glview_changed_set(Evas_Object *obj)
+elm_glview_render_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func)
 {
    ELM_CHECK_WIDTYPE(obj, widtype);
    Widget_Data *wd = elm_widget_data_get(obj);
+   if (!wd) return;
+
+   wd->render_func = func;
+   _set_render_policy_callback(obj);
+}
 
+EAPI void
+elm_glview_changed_set(Evas_Object *obj)
+{
+   ELM_CHECK_WIDTYPE(obj, widtype);
+   Widget_Data *wd = elm_widget_data_get(obj);
    if (!wd) return;
 
    evas_object_image_pixels_dirty_set(wd->glview_image, EINA_TRUE);
-
    if (wd->render_policy == ELM_GLVIEW_RENDER_POLICY_ALWAYS)
      {
         if (!wd->render_idle_enterer)
-
-           wd->render_idle_enterer = ecore_idle_enterer_before_add((Ecore_Task_Cb)_render_cb, obj);
+          wd->render_idle_enterer = ecore_idle_enterer_before_add((Ecore_Task_Cb)_render_cb, obj);
      }
-
 }
 
-/* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/
+/* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/