EvasGL: Fixed a few minor bugs.
[profile/ivi/evas.git] / src / lib / canvas / evas_gl.c
1 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/
2 #include "evas_common.h"
3 #include "evas_private.h"
4 #include "Evas_GL.h"
5
6 struct _Evas_GL
7 {
8    DATA32      magic;
9    Evas       *evas;
10
11    Eina_List  *contexts;
12    Eina_List  *surfaces;
13 };
14
15 struct _Evas_GL_Context
16 {
17    void    *data;
18 };
19
20 struct _Evas_GL_Surface
21 {
22    void    *data;
23 };
24
25 EAPI Evas_GL *
26 evas_gl_new(Evas *e)
27 {
28    Evas_GL *evas_gl;
29
30    MAGIC_CHECK(e, Evas, MAGIC_EVAS);
31    return NULL;
32    MAGIC_CHECK_END();
33
34    evas_gl = calloc(1, sizeof(Evas_GL));
35    if (!evas_gl) return NULL;
36
37    evas_gl->magic = MAGIC_EVAS_GL;
38    evas_gl->evas = e;
39
40    if (!evas_gl->evas->engine.func->gl_context_create)
41      {
42         ERR("Evas GL engine not available.");
43         free(evas_gl);
44         return NULL;
45      }
46
47    return evas_gl;
48 }
49
50 EAPI void
51 evas_gl_free(Evas_GL *evas_gl)
52 {
53    MAGIC_CHECK(evas_gl, Evas_GL, MAGIC_EVAS_GL);
54    return;
55    MAGIC_CHECK_END();
56
57
58    // Delete undeleted surfaces
59    while (evas_gl->surfaces)
60      evas_gl_surface_destroy(evas_gl, evas_gl->surfaces->data);
61
62    // Delete undeleted contexts
63    while (evas_gl->contexts)
64      evas_gl_context_destroy(evas_gl, evas_gl->contexts->data);
65
66    evas_gl->magic = 0;
67    free(evas_gl);
68 }
69
70 EAPI Evas_GL_Config *
71 evas_gl_config_new()
72 {
73    Evas_GL_Config *cfg;
74
75    cfg = calloc(1, sizeof(Evas_GL_Config));
76
77    if (!cfg) return NULL;
78
79    return cfg;
80 }
81
82 EAPI void
83 evas_gl_config_free(Evas_GL_Config *cfg)
84 {
85    if (cfg) free(cfg);
86 }
87
88 EAPI Evas_GL_Surface *
89 evas_gl_surface_create(Evas_GL *evas_gl, Evas_GL_Config *config, int width, int height)
90 {
91    Evas_GL_Surface *surf;
92
93    MAGIC_CHECK(evas_gl, Evas_GL, MAGIC_EVAS_GL);
94    return NULL;
95    MAGIC_CHECK_END();
96
97    if (!config)
98      {
99         ERR("Invalid Config Pointer!");
100         return NULL;
101      }
102
103    if ( (width <= 0) || (height <= 0))
104      {
105         ERR("Invalid surface dimensions: %d, %d", width, height);
106         return NULL;
107      }
108
109    surf = calloc(1, sizeof(Evas_GL_Surface));
110
111    if (!surf) return NULL;
112
113    surf->data = evas_gl->evas->engine.func->gl_surface_create(evas_gl->evas->engine.data.output, config, width, height);
114
115    if (!surf->data)
116      {
117         ERR("Failed creating a surface from the engine.");
118         free(surf);
119         return NULL;
120      }
121
122    // Keep track of the surface creations
123    evas_gl->surfaces = eina_list_prepend(evas_gl->surfaces, surf);
124
125    return surf;
126 }
127
128 EAPI void
129 evas_gl_surface_destroy(Evas_GL *evas_gl, Evas_GL_Surface *surf)
130 {
131    // Magic
132    MAGIC_CHECK(evas_gl, Evas_GL, MAGIC_EVAS_GL);
133    return;
134    MAGIC_CHECK_END();
135
136    if (!surf)
137      {
138         ERR("Trying to destroy a NULL surface pointer!");
139         return;
140      }
141
142    // Call Engine's Surface Destroy
143    evas_gl->evas->engine.func->gl_surface_destroy(evas_gl->evas->engine.data.output, surf->data);
144
145    // Remove it from the list
146    evas_gl->surfaces = eina_list_remove(evas_gl->surfaces, surf);
147
148    // Delete the object
149    free(surf);
150    surf = NULL;
151 }
152
153 EAPI Evas_GL_Context *
154 evas_gl_context_create(Evas_GL *evas_gl, Evas_GL_Context *share_ctx)
155 {
156    Evas_GL_Context *ctx;
157
158    // Magic
159    MAGIC_CHECK(evas_gl, Evas_GL, MAGIC_EVAS_GL);
160    return NULL;
161    MAGIC_CHECK_END();
162
163    // Allocate a context object
164    ctx = calloc(1, sizeof(Evas_GL_Context));
165    if (!ctx)
166      {
167         ERR("Unable to create a Evas_GL_Context object");
168         return NULL;
169      }
170
171    // Call engine->gl_create_context
172    if (share_ctx)
173      {
174         ctx->data = evas_gl->evas->engine.func->gl_context_create(evas_gl->evas->engine.data.output, share_ctx->data);
175      }
176    else
177      {
178         ctx->data = evas_gl->evas->engine.func->gl_context_create(evas_gl->evas->engine.data.output, NULL);
179      }
180
181    // Set a few variables
182    if (!ctx->data)
183      {
184         ERR("Failed creating a context from the engine.");
185         free(ctx);
186         return NULL;
187      }
188
189    // Keep track of the context creations
190    evas_gl->contexts = eina_list_prepend(evas_gl->contexts, ctx);
191
192    return ctx;
193
194 }
195
196 EAPI void
197 evas_gl_context_destroy(Evas_GL *evas_gl, Evas_GL_Context *ctx)
198 {
199
200    MAGIC_CHECK(evas_gl, Evas_GL, MAGIC_EVAS_GL);
201    return;
202    MAGIC_CHECK_END();
203
204    if (!ctx)
205      {
206         ERR("Trying to destroy a NULL context pointer!");
207         return;
208      }
209
210    // Call Engine's destroy
211    evas_gl->evas->engine.func->gl_context_destroy(evas_gl->evas->engine.data.output, ctx->data);
212
213    // Remove it from the list
214    evas_gl->contexts = eina_list_remove(evas_gl->contexts, ctx);
215
216    // Delete the object
217    free(ctx);
218    ctx = NULL;
219 }
220
221 EAPI Eina_Bool
222 evas_gl_make_current(Evas_GL *evas_gl, Evas_GL_Surface *surf, Evas_GL_Context *ctx)
223 {
224    Eina_Bool ret;
225
226    MAGIC_CHECK(evas_gl, Evas_GL, MAGIC_EVAS_GL);
227    return EINA_FALSE;
228    MAGIC_CHECK_END();
229    
230    if ((surf) && (ctx))
231      ret = (Eina_Bool)evas_gl->evas->engine.func->gl_make_current(evas_gl->evas->engine.data.output, surf->data, ctx->data);
232    else if ((!surf) && (!ctx))
233      ret = (Eina_Bool)evas_gl->evas->engine.func->gl_make_current(evas_gl->evas->engine.data.output, NULL, NULL);
234    else
235      {
236         ERR("Bad match between surface: %p and context: %p", surf, ctx);
237         return EINA_FALSE;
238      }
239
240    return ret;
241 }
242
243 EAPI const char *
244 evas_gl_string_query(Evas_GL *evas_gl, int name)
245 {
246    MAGIC_CHECK(evas_gl, Evas_GL, MAGIC_EVAS_GL);
247    return EINA_FALSE;
248    MAGIC_CHECK_END();
249
250    return (const char *)evas_gl->evas->engine.func->gl_string_query(evas_gl->evas->engine.data.output, name);
251 }
252
253 EAPI Evas_GL_Func
254 evas_gl_proc_address_get(Evas_GL *evas_gl, const char *name)
255 {
256    MAGIC_CHECK(evas_gl, Evas_GL, MAGIC_EVAS_GL);
257    return EINA_FALSE;
258    MAGIC_CHECK_END();
259
260    return (Evas_GL_Func)evas_gl->evas->engine.func->gl_proc_address_get(evas_gl->evas->engine.data.output, name);
261 }
262
263 EAPI Eina_Bool
264 evas_gl_native_surface_get(Evas_GL *evas_gl, Evas_GL_Surface *surf, Evas_Native_Surface *ns)
265 {
266    MAGIC_CHECK(evas_gl, Evas_GL, MAGIC_EVAS_GL);
267    return EINA_FALSE;
268    MAGIC_CHECK_END();
269
270    if ((!surf) || (!ns))
271      {
272         ERR("Invalid input parameters!");
273         return EINA_FALSE;
274      }
275
276    return (Eina_Bool)evas_gl->evas->engine.func->gl_native_surface_get(evas_gl->evas->engine.data.output, surf->data, ns);
277 }
278
279
280 EAPI Evas_GL_API *
281 evas_gl_api_get(Evas_GL *evas_gl)
282 {
283    MAGIC_CHECK(evas_gl, Evas_GL, MAGIC_EVAS_GL);
284    return NULL;
285    MAGIC_CHECK_END();
286
287    return (Evas_GL_API*)evas_gl->evas->engine.func->gl_api_get(evas_gl->evas->engine.data.output);
288
289 }