summaryrefslogtreecommitdiff
path: root/src/cairo-evas-gl-context.c
blob: 10d6808178d1bedb2b7b988ebb754e3a8b0b9e65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* cairo - a vector graphics library with display and print output
 *
 * Copyright © 2009 Eric Anholt
 * Copyright © 2009 Chris Wilson
 * Copyright © 2005 Red Hat, Inc
 * Copyright © 2014 Samsung Research America, Inc - Silicon Valley
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
   You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 *
 * The Original Code is the cairo graphics library.
 *
 * The Initial Developer of the Original Code is Red Hat, Inc.
 *
 * Contributor(s):
 *	Carl Worth <cworth@cworth.org>
 *	Chris Wilson <chris@chris-wilson.co.uk>
 *	Henry Song <henry.song@samsung.com>
 */

#include "cairoint.h"
#include "cairo-gl-private.h"
#include "cairo-error-private.h"
#if CAIRO_HAS_DLSYM
#include <dlfcn.h>
#endif

#include "cairo-evas-gl.h"

typedef struct _cairo_evas_gl_context {
    cairo_gl_context_t base;

    Evas_GL *evas_gl;
    Evas_GL_Surface *surface;
    Evas_GL_Context *context;

    Evas_GL_Surface *dummy_surface;
    Evas_GL_Surface *current_surface;

    Evas_GL_Context *queried_context;

    cairo_bool_t has_multithread_makecurrent;
} cairo_evas_gl_context_t;

typedef struct _cairo_evas_gl_surface {
    cairo_gl_surface_t base;

    Evas_GL_Surface *surface;
} cairo_evas_gl_surface_t;

/* as of efl-1.12.0, the following GL functions are supported, for other
   GL functions and extensions, we us dlsym to get.

   The following APIs are not supported by efl-1.12
   glDrawBuffer, glReadBuffer, glMapBuffer, glUnmapBuffer,
   glBlitFramebuffer{ANGLE}, glRenderbufferStorageMultisample{ANGLE},

   This functions are queried via dlsym.  cairo does not use map/unmap
   buffer.  For rest unsupported GL functions, they should not change
   evas_gl internal context states (I hope).
 */
static cairo_gl_generic_func_t
_cairo_evas_gl_get_proc_addr (void *data, const char *name)
{
    static Evas_GL_API *api;
    Evas_GL *gl = (Evas_GL *) data;
    int i;

    struct {
	size_t func;
	const char *name;
    } evas_gl_func_map[] = {
	/* core function, glReadBuffer and glDrawBuffer not supported */
	{ offsetof (Evas_GL_API, glActiveTexture	), "glActiveTexture" },
	{ offsetof (Evas_GL_API, glBindTexture		), "glBindTexture" },
	{ offsetof (Evas_GL_API, glBlendFunc		), "glBlendFunc" },
	{ offsetof (Evas_GL_API, glBlendFuncSeparate	), "glBlendFuncSeparate" },
	{ offsetof (Evas_GL_API, glClear		), "glClear" },
	{ offsetof (Evas_GL_API, glClearColor		), "glClearColor" },
	{ offsetof (Evas_GL_API, glClearStencil		), "glClearStencil" },
	{ offsetof (Evas_GL_API, glColorMask		), "glColorMask" },
	{ offsetof (Evas_GL_API, glDeleteTextures	), "glDeleteTextures" },
	{ offsetof (Evas_GL_API, glDepthMask		), "glDepthMask" },
	{ offsetof (Evas_GL_API, glDisable		), "glDisable" },
	{ offsetof (Evas_GL_API, glDrawArrays		), "glDrawArrays" },
	{ offsetof (Evas_GL_API, glDrawElements		), "glDrawElements" },
	{ offsetof (Evas_GL_API, glEnable		), "glEnable" },
	{ offsetof (Evas_GL_API, glGenTextures		), "glGenTextures" },
	{ offsetof (Evas_GL_API, glGetBooleanv		), "glGetBooleanv" },
	{ offsetof (Evas_GL_API, glGetError		), "glGetError" },
	{ offsetof (Evas_GL_API, glGetFloatv		), "glGetFloatv" },
	{ offsetof (Evas_GL_API, glGetIntegerv		), "glGetIntegerv" },
	{ offsetof (Evas_GL_API, glGetString		), "glGetString" },
	{ offsetof (Evas_GL_API, glPixelStorei		), "glPixelStorei" },
	{ offsetof (Evas_GL_API, glReadPixels		), "glReadPixels" },
	{ offsetof (Evas_GL_API, glScissor		), "glScissor" },
	{ offsetof (Evas_GL_API, glStencilFunc		), "glStencilFunc" },
	{ offsetof (Evas_GL_API, glStencilMask		), "glStencilMask" },
	{ offsetof (Evas_GL_API, glStencilOp		), "glStencilOp" },
	{ offsetof (Evas_GL_API, glTexImage2D		), "glTexImage2D" },
	{ offsetof (Evas_GL_API, glTexSubImage2D	), "glTexSubImage2D" },
	{ offsetof (Evas_GL_API, glTexParameteri	), "glTexParameteri" },
	{ offsetof (Evas_GL_API, glViewport		), "glViewport" },
	/* buffers functions, glMapBufferOES, glUnmapBufferOES not
	   suported
	 */
	{ offsetof (Evas_GL_API, glGenBuffers		), "glGenBuffers" },
	{ offsetof (Evas_GL_API, glBindBuffer		), "glBindBuffer" },
	{ offsetof (Evas_GL_API, glBufferData		), "glBufferData" },
	/* shader functions */
	{ offsetof (Evas_GL_API, glCreateShader		), "glCreateShader" },
	{ offsetof (Evas_GL_API, glShaderSource		), "glShaderSource" },
	{ offsetof (Evas_GL_API, glCompileShader	), "glCompileShader" },
	{ offsetof (Evas_GL_API, glGetShaderiv		), "glGetShaderiv" },
	{ offsetof (Evas_GL_API, glGetShaderInfoLog	), "glGetShaderInfoLog" },
	{ offsetof (Evas_GL_API, glDeleteShader		), "glDeleteShader" },
	/* program functions */
	{ offsetof (Evas_GL_API, glCreateProgram	), "glCreateProgram" },
	{ offsetof (Evas_GL_API, glAttachShader		), "glAttachShader" },
	{ offsetof (Evas_GL_API, glDeleteProgram	), "glDeleteProgram" },
	{ offsetof (Evas_GL_API, glLinkProgram		), "glLinkProgram" },
	{ offsetof (Evas_GL_API, glUseProgram		), "glUseProgram" },
	{ offsetof (Evas_GL_API, glGetProgramiv		), "glGetProgramiv" },
	{ offsetof (Evas_GL_API, glGetProgramInfoLog	), "glGetProgramInfoLog" },
	/* uniform functions */
	{ offsetof (Evas_GL_API, glGetUniformLocation	), "glGetUniformLocation" },
	{ offsetof (Evas_GL_API, glUniform1f		), "glUniform1f" },
	{ offsetof (Evas_GL_API, glUniform2f		), "glUniform2f" },
	{ offsetof (Evas_GL_API, glUniform3f		), "glUniform3f" },
	{ offsetof (Evas_GL_API, glUniform4f		), "glUniform4f" },
	{ offsetof (Evas_GL_API, glUniform1fv		), "glUniform1fv" },
	{ offsetof (Evas_GL_API, glUniformMatrix3fv	), "glUniformMatrix3fv" },
	{ offsetof (Evas_GL_API, glUniformMatrix4fv	), "glUniformMatrix4fv" },
	{ offsetof (Evas_GL_API, glUniform1i		), "glUniform1i" },
	/* attribute functions */
	{ offsetof (Evas_GL_API, glBindAttribLocation	), "glBindAttribLocation" },
	{ offsetof (Evas_GL_API, glVertexAttribPointer	), "glVertexAttribPointer" },
	{ offsetof (Evas_GL_API, glEnableVertexAttribArray), "glEnableVertexAttribArray" },
	{ offsetof (Evas_GL_API, glDisableVertexAttribArray), "glDisableVertexAttribArray" },
	/* fbo functions */
	{ offsetof (Evas_GL_API, glGenFramebuffers	), "glGenFramebuffers" },
	{ offsetof (Evas_GL_API, glBindFramebuffer	), "glBindFramebuffer" },
	{ offsetof (Evas_GL_API, glFramebufferTexture2D	), "glFramebufferTexture2D" },
	{ offsetof (Evas_GL_API, glCheckFramebufferStatus), "glCheckFramebufferStatus" },
	{ offsetof (Evas_GL_API, glDeleteFramebuffers	), "glDeleteFramebuffers" },
	{ offsetof (Evas_GL_API, glGenRenderbuffers	), "glGenRenderbuffers" },
	{ offsetof (Evas_GL_API, glBindRenderbuffer	), "glBindRenderbuffer" },
	{ offsetof (Evas_GL_API, glRenderbufferStorage	), "glRenderbufferStorage" },
	{ offsetof (Evas_GL_API, glFramebufferRenderbuffer), "glFramebufferRenderbuffer" },
	{ offsetof (Evas_GL_API, glDeleteRenderbuffers	), "glDeleteRenderbuffers" },
	/* multisampling functions, none supported in efl-1.12.0 */
	{ 0, NULL }
    };

    api = evas_gl_api_get (gl);

   if(api != NULL) {
     for (i = 0; evas_gl_func_map[i].name; i++) {
	     if (! strncmp (evas_gl_func_map[i].name, name, strlen(name)))
	       return *((cairo_gl_generic_func_t *) (((char *) &api->version) + evas_gl_func_map[i].func));
     }
   }

   return evas_gl_proc_address_get (gl, name);
}

static cairo_bool_t
_context_acquisition_changed_evas_gl_state (cairo_evas_gl_context_t *ctx,
					    Evas_GL_Surface *current_surface)
{
    return ctx->queried_context != ctx->context ||
	   ctx->current_surface != current_surface;
}

static Evas_GL_Surface *
_evas_gl_get_current_surface (cairo_evas_gl_context_t *ctx)
{
    if (ctx->base.current_target == NULL ||
        _cairo_gl_surface_is_texture (ctx->base.current_target)) {
	return  ctx->dummy_surface;
    }

    return ((cairo_evas_gl_surface_t *) ctx->base.current_target)->surface;
}

static void
_evas_gl_query_current_state (cairo_evas_gl_context_t *ctx)
{
    ctx->queried_context = evas_gl_current_context_get (ctx->evas_gl);
    ctx->current_surface = evas_gl_current_surface_get (ctx->evas_gl);
}

static void
_evas_gl_acquire (void *abstract_ctx)
{
    cairo_evas_gl_context_t *ctx = abstract_ctx;
    Evas_GL_Surface *current_surface = _evas_gl_get_current_surface (ctx);

    _evas_gl_query_current_state (ctx);
    if (!_context_acquisition_changed_evas_gl_state (ctx, current_surface))
	return;

    _cairo_gl_context_reset (&ctx->base);
    evas_gl_make_current (ctx->evas_gl, current_surface, ctx->context);

    ctx->current_surface = current_surface;
    //ctx->base.current_target = &ctx->dummy_surface->base;
}

static void
_evas_gl_release (void *abstract_ctx)
{
    cairo_evas_gl_context_t *ctx = abstract_ctx;
    if (!ctx->base.thread_aware || ctx->has_multithread_makecurrent ||
	!_context_acquisition_changed_evas_gl_state (ctx,
						 _evas_gl_get_current_surface (ctx))) {
	return;
    }

    _cairo_gl_composite_flush (&ctx->base);
    evas_gl_make_current (ctx->evas_gl, NULL, NULL);
    ctx->current_surface = NULL;
}

static void
_evas_gl_make_current (void *abstract_ctx,
	           cairo_gl_surface_t *abstract_surface)
{
    cairo_evas_gl_context_t *ctx = abstract_ctx;
    cairo_evas_gl_surface_t *surface = (cairo_evas_gl_surface_t *) abstract_surface;

    if (surface->surface != ctx->current_surface) {
	evas_gl_make_current (ctx->evas_gl, surface->surface, ctx->context);
	ctx->current_surface = surface->surface;
    }
}

static void
_evas_gl_swap_buffers (void *abstract_ctx,
		   cairo_gl_surface_t *abstract_surface)
{ }

static void
_evas_gl_destroy (void *abstract_ctx)
{
    cairo_evas_gl_context_t *ctx = abstract_ctx;

    evas_gl_make_current (ctx->evas_gl, NULL, NULL);
    if (ctx->dummy_surface)
	evas_gl_surface_destroy (ctx->evas_gl, ctx->dummy_surface);
}

cairo_device_t *
cairo_evas_gl_device_create (Evas_GL *evas_gl,
			     Evas_GL_Context *evas_context)
{
    Evas_GL_Config *evas_cfg;
    cairo_evas_gl_context_t *ctx;
    cairo_status_t status;

	if (! evas_gl ||! evas_context) {
		fprintf (stderr, "cairo_evas_gl_device_create(): evas_gl or evas_context is NULL\n");
	return _cairo_gl_context_create_in_error (CAIRO_STATUS_NULL_POINTER);
	}

    ctx = calloc (1, sizeof (cairo_evas_gl_context_t));
    if (unlikely (ctx == NULL))
	return _cairo_gl_context_create_in_error (CAIRO_STATUS_NO_MEMORY);

    ctx->evas_gl = evas_gl;
    ctx->context = evas_context;
    evas_cfg = evas_gl_config_new ();

    ctx->base.acquire = _evas_gl_acquire;
    ctx->base.release = _evas_gl_release;
    ctx->base.make_current = _evas_gl_make_current;
    ctx->base.swap_buffers = _evas_gl_swap_buffers;
    ctx->base.destroy = _evas_gl_destroy;

    /* We are about the change the current state of evas-gl, so we should
     * query the pre-existing surface now instead of later. */
    _evas_gl_query_current_state (ctx);

    ctx->dummy_surface = evas_gl_pbuffer_surface_create (ctx->evas_gl,
							 evas_cfg,
							 1, 1, NULL);
    //evas_gl_config_free (evas_cfg);

    if (ctx->dummy_surface == NULL) {
        free (ctx);
	return _cairo_gl_context_create_in_error (CAIRO_STATUS_NO_MEMORY);
    }

    if (!evas_gl_make_current (ctx->evas_gl, ctx->dummy_surface, evas_context)) {
	evas_gl_surface_destroy (ctx->evas_gl, ctx->dummy_surface);
	free (ctx);
	return _cairo_gl_context_create_in_error (CAIRO_STATUS_NO_MEMORY);
    }

    status = _cairo_gl_dispatch_init (&ctx->base.dispatch,
				      _cairo_evas_gl_get_proc_addr,
				      ctx->evas_gl);
    if (unlikely (status)) {
	evas_gl_surface_destroy (ctx->evas_gl, ctx->dummy_surface);
	free (ctx);
	return _cairo_gl_context_create_in_error (status);
    }

    status = _cairo_gl_context_init (&ctx->base);
    if (unlikely (status)) {
	evas_gl_surface_destroy (ctx->evas_gl, ctx->dummy_surface);
	free (ctx);
	return _cairo_gl_context_create_in_error (status);
    }

    if (strstr(evas_gl_string_query (ctx->evas_gl, EVAS_GL_EXTENSIONS),
				     "GLX_MESA_multithread_makecurrent"))
	ctx->has_multithread_makecurrent = TRUE;
    else
	ctx->has_multithread_makecurrent = FALSE;

    // reset vbo_size
    ctx->base.vbo_size = 16 * 1024;

    evas_gl_make_current (ctx->evas_gl, NULL, NULL);
    return &ctx->base.base;
}

cairo_surface_t *
cairo_gl_surface_create_for_evas_gl (cairo_device_t	*device,
				     Evas_GL_Surface	*evas_surface,
				     Evas_GL_Config     *evas_config,
				     int		 width,
				     int		 height)
{
    cairo_evas_gl_surface_t *surface;

	if ((! device)||(cairo_device_status(device)!= CAIRO_STATUS_SUCCESS)){
		fprintf (stderr, "cairo_gl_surface_create_for_evas_gl(): cairo device is NULL or not available\n");
	return _cairo_surface_create_in_error(CAIRO_STATUS_NULL_POINTER);
	}

    if (unlikely (device->status))
	return _cairo_surface_create_in_error (device->status);

	if (! evas_surface || ! evas_config) {
		fprintf (stderr, "cairo_gl_surface_create_for_evas_gl(): evas_surface or evas_config is NULL\n");
	return _cairo_surface_create_in_error (CAIRO_STATUS_NULL_POINTER);
	}

    if (device->backend->type != CAIRO_DEVICE_TYPE_GL)
	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));

    if (width <= 0 || height <= 0)
        return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));

    surface = calloc (1, sizeof (cairo_evas_gl_surface_t));
    if (unlikely (surface == NULL))
	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));

    _cairo_gl_surface_init (device, &surface->base,
			    CAIRO_CONTENT_COLOR_ALPHA, width, height);

    if (evas_config->stencil_bits != EVAS_GL_STENCIL_NONE)
	surface->base.supports_stencil = TRUE;

    /* does not matter num of samples */
    if (evas_config->multisample_bits != EVAS_GL_MULTISAMPLE_NONE) {
	surface->base.num_samples = 2;
	surface->base.supports_msaa = TRUE;
    }

    surface->base.stencil_and_msaa_caps_initialized = TRUE;

    surface->surface = evas_surface;

    return &surface->base.base;
}

static cairo_bool_t is_evas_gl_device (cairo_device_t *device)
{
    return (device->backend != NULL &&
	    device->backend->type == CAIRO_DEVICE_TYPE_GL);
}

static cairo_evas_gl_context_t *to_evas_gl_context (cairo_device_t *device)
{
    return (cairo_evas_gl_context_t *) device;
}

cairo_public Evas_GL *
cairo_evas_gl_device_get_gl (cairo_device_t *device)
{
	if ((! device)||(cairo_device_status(device)!= CAIRO_STATUS_SUCCESS)){
		fprintf(stderr,"\n cairo_evas_gl_device_get_gl(): cairo device is NULL or not available");
	_cairo_error_throw(CAIRO_STATUS_DEVICE_ERROR);
	return NULL;
	}

    if (! is_evas_gl_device (device)) {
	_cairo_error_throw (CAIRO_STATUS_DEVICE_TYPE_MISMATCH);
	return NULL;
    }

    return to_evas_gl_context (device)->evas_gl;
}

cairo_public Evas_GL_Context *
cairo_evas_gl_device_get_context (cairo_device_t *device)
{
	if ((! device)||(cairo_device_status(device)!= CAIRO_STATUS_SUCCESS)){
		fprintf(stderr,"\n cairo_evas_gl_device_get_context(): cairo device is NULL or not available");		
	_cairo_error_throw (CAIRO_STATUS_DEVICE_ERROR);
	return NULL;
	}

    if (! is_evas_gl_device (device)) {
	_cairo_error_throw (CAIRO_STATUS_DEVICE_TYPE_MISMATCH);
	return NULL;
    }

    return to_evas_gl_context (device)->context;
}