Merge branch '7.8'
[mesa.git] / src / gallium / state_trackers / egl / common / egl_g3d.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.8
4 *
5 * Copyright (C) 2009-2010 Chia-I Wu <olv@0xlab.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <assert.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include "pipe/p_screen.h"
29 #include "util/u_memory.h"
30 #include "util/u_rect.h"
31 #include "util/u_inlines.h"
32 #include "egldriver.h"
33 #include "eglcurrent.h"
34 #include "eglconfigutil.h"
35 #include "egllog.h"
36
37 #include "native.h"
38 #include "egl_g3d.h"
39 #include "egl_g3d_image.h"
40 #include "egl_st.h"
41
42 /**
43 * Validate the draw/read surfaces of the context.
44 */
45 static void
46 egl_g3d_validate_context(_EGLDisplay *dpy, _EGLContext *ctx)
47 {
48 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
49 struct pipe_screen *screen = gdpy->native->screen;
50 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
51 const uint st_att_map[NUM_NATIVE_ATTACHMENTS] = {
52 ST_SURFACE_FRONT_LEFT,
53 ST_SURFACE_BACK_LEFT,
54 ST_SURFACE_FRONT_RIGHT,
55 ST_SURFACE_BACK_RIGHT,
56 };
57 EGLint num_surfaces, s;
58
59 /* validate draw and/or read buffers */
60 num_surfaces = (gctx->base.ReadSurface == gctx->base.DrawSurface) ? 1 : 2;
61 for (s = 0; s < num_surfaces; s++) {
62 struct pipe_texture *textures[NUM_NATIVE_ATTACHMENTS];
63 struct egl_g3d_surface *gsurf;
64 struct egl_g3d_buffer *gbuf;
65 EGLint att;
66
67 if (s == 0) {
68 gsurf = egl_g3d_surface(gctx->base.DrawSurface);
69 gbuf = &gctx->draw;
70 }
71 else {
72 gsurf = egl_g3d_surface(gctx->base.ReadSurface);
73 gbuf = &gctx->read;
74 }
75
76 if (!gctx->force_validate) {
77 unsigned int seq_num;
78
79 gsurf->native->validate(gsurf->native, gbuf->attachment_mask,
80 &seq_num, NULL, NULL, NULL);
81 /* skip validation */
82 if (gsurf->sequence_number == seq_num)
83 continue;
84 }
85
86 pipe_surface_reference(&gsurf->render_surface, NULL);
87 memset(textures, 0, sizeof(textures));
88
89 gsurf->native->validate(gsurf->native, gbuf->attachment_mask,
90 &gsurf->sequence_number, textures,
91 &gsurf->base.Width, &gsurf->base.Height);
92 for (att = 0; att < NUM_NATIVE_ATTACHMENTS; att++) {
93 struct pipe_texture *pt = textures[att];
94 struct pipe_surface *ps;
95
96 if (native_attachment_mask_test(gbuf->attachment_mask, att) && pt) {
97 ps = screen->get_tex_surface(screen, pt, 0, 0, 0,
98 PIPE_BUFFER_USAGE_GPU_READ |
99 PIPE_BUFFER_USAGE_GPU_WRITE);
100 gctx->stapi->st_set_framebuffer_surface(gbuf->st_fb,
101 st_att_map[att], ps);
102
103 if (gsurf->render_att == att)
104 pipe_surface_reference(&gsurf->render_surface, ps);
105
106 pipe_surface_reference(&ps, NULL);
107 pipe_texture_reference(&pt, NULL);
108 }
109 }
110
111 gctx->stapi->st_resize_framebuffer(gbuf->st_fb,
112 gsurf->base.Width, gsurf->base.Height);
113 }
114
115 gctx->force_validate = EGL_FALSE;
116
117 }
118
119 /**
120 * Create a st_framebuffer.
121 */
122 static struct st_framebuffer *
123 create_framebuffer(_EGLContext *ctx, _EGLSurface *surf)
124 {
125 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
126 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
127 struct egl_g3d_config *gconf = egl_g3d_config(gsurf->base.Config);
128
129 return gctx->stapi->st_create_framebuffer(&gconf->native->mode,
130 gconf->native->color_format, gconf->native->depth_format,
131 gconf->native->stencil_format,
132 gsurf->base.Width, gsurf->base.Height, &gsurf->base);
133 }
134
135 /**
136 * Update the attachments of draw/read surfaces.
137 */
138 static void
139 egl_g3d_route_context(_EGLDisplay *dpy, _EGLContext *ctx)
140 {
141 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
142 EGLint s;
143
144 /* route draw and read buffers' attachments */
145 for (s = 0; s < 2; s++) {
146 struct egl_g3d_surface *gsurf;
147 struct egl_g3d_buffer *gbuf;
148
149 if (s == 0) {
150 gsurf = egl_g3d_surface(gctx->base.DrawSurface);
151 gbuf = &gctx->draw;
152 }
153 else {
154 gsurf = egl_g3d_surface(gctx->base.ReadSurface);
155 gbuf = &gctx->read;
156 }
157
158 gbuf->attachment_mask = (1 << gsurf->render_att);
159
160 /* FIXME OpenGL defaults to draw the front or back buffer when the
161 * context is single-buffered or double-buffered respectively. In EGL,
162 * however, the buffer to be drawn is determined by the surface, instead
163 * of the context. As a result, rendering to a pixmap surface with a
164 * double-buffered context does not work as expected.
165 *
166 * gctx->stapi->st_draw_front_buffer(gctx->st_ctx, natt ==
167 * NATIVE_ATTACHMENT_FRONT_LEFT);
168 */
169
170 /*
171 * FIXME If the back buffer is asked for here, and the front buffer is
172 * later needed by the client API (e.g. glDrawBuffer is called to draw
173 * the front buffer), it will create a new pipe texture and draw there.
174 * One fix is to ask for both buffers here, but it would be a waste if
175 * the front buffer is never used. A better fix is to add a callback to
176 * the pipe screen with context private (just like flush_frontbuffer).
177 */
178 }
179 }
180
181 /**
182 * Reallocate the context's framebuffers after draw/read surfaces change.
183 */
184 static EGLBoolean
185 egl_g3d_realloc_context(_EGLDisplay *dpy, _EGLContext *ctx)
186 {
187 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
188 struct egl_g3d_surface *gdraw = egl_g3d_surface(gctx->base.DrawSurface);
189 struct egl_g3d_surface *gread = egl_g3d_surface(gctx->base.ReadSurface);
190
191 /* unreference the old framebuffers */
192 if (gctx->draw.st_fb) {
193 EGLBoolean is_equal = (gctx->draw.st_fb == gctx->read.st_fb);
194 void *priv;
195
196 priv = gctx->stapi->st_framebuffer_private(gctx->draw.st_fb);
197 if (!gdraw || priv != (void *) &gdraw->base) {
198 gctx->stapi->st_unreference_framebuffer(gctx->draw.st_fb);
199 gctx->draw.st_fb = NULL;
200 gctx->draw.attachment_mask = 0x0;
201 }
202
203 if (is_equal) {
204 gctx->read.st_fb = NULL;
205 gctx->draw.attachment_mask = 0x0;
206 }
207 else {
208 priv = gctx->stapi->st_framebuffer_private(gctx->read.st_fb);
209 if (!gread || priv != (void *) &gread->base) {
210 gctx->stapi->st_unreference_framebuffer(gctx->read.st_fb);
211 gctx->read.st_fb = NULL;
212 gctx->draw.attachment_mask = 0x0;
213 }
214 }
215 }
216
217 if (!gdraw)
218 return EGL_TRUE;
219
220 /* create the draw fb */
221 if (!gctx->draw.st_fb) {
222 gctx->draw.st_fb = create_framebuffer(&gctx->base, &gdraw->base);
223 if (!gctx->draw.st_fb)
224 return EGL_FALSE;
225 }
226
227 /* create the read fb */
228 if (!gctx->read.st_fb) {
229 if (gread != gdraw) {
230 gctx->read.st_fb = create_framebuffer(&gctx->base, &gread->base);
231 if (!gctx->read.st_fb) {
232 gctx->stapi->st_unreference_framebuffer(gctx->draw.st_fb);
233 gctx->draw.st_fb = NULL;
234 return EGL_FALSE;
235 }
236 }
237 else {
238 /* there is no st_reference_framebuffer... */
239 gctx->read.st_fb = gctx->draw.st_fb;
240 }
241 }
242
243 egl_g3d_route_context(dpy, &gctx->base);
244 gctx->force_validate = EGL_TRUE;
245
246 return EGL_TRUE;
247 }
248
249 /**
250 * Return the state tracker for the given context.
251 */
252 static const struct egl_g3d_st *
253 egl_g3d_choose_st(_EGLDriver *drv, _EGLContext *ctx)
254 {
255 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
256 const struct egl_g3d_st *stapi;
257 EGLint idx = -1;
258
259 switch (ctx->ClientAPI) {
260 case EGL_OPENGL_ES_API:
261 switch (ctx->ClientVersion) {
262 case 1:
263 idx = EGL_G3D_ST_OPENGL_ES;
264 break;
265 case 2:
266 idx = EGL_G3D_ST_OPENGL_ES2;
267 break;
268 default:
269 _eglLog(_EGL_WARNING, "unknown client version %d",
270 ctx->ClientVersion);
271 break;
272 }
273 break;
274 case EGL_OPENVG_API:
275 idx = EGL_G3D_ST_OPENVG;
276 break;
277 case EGL_OPENGL_API:
278 idx = EGL_G3D_ST_OPENGL;
279 break;
280 default:
281 _eglLog(_EGL_WARNING, "unknown client API 0x%04x", ctx->ClientAPI);
282 break;
283 }
284
285 stapi = (idx >= 0) ? gdrv->stapis[idx] : NULL;
286 return stapi;
287 }
288
289 /**
290 * Initialize the state trackers.
291 */
292 static void
293 egl_g3d_init_st(_EGLDriver *drv)
294 {
295 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
296 EGLint i;
297
298 /* already initialized */
299 if (gdrv->api_mask)
300 return;
301
302 for (i = 0; i < NUM_EGL_G3D_STS; i++) {
303 gdrv->stapis[i] = egl_g3d_get_st(i);
304 if (gdrv->stapis[i])
305 gdrv->api_mask |= gdrv->stapis[i]->api_bit;
306 }
307
308 if (gdrv->api_mask)
309 _eglLog(_EGL_DEBUG, "Driver API mask: 0x%x", gdrv->api_mask);
310 else
311 _eglLog(_EGL_WARNING, "No supported client API");
312 }
313
314 /**
315 * Get the probe object of the display.
316 *
317 * Note that this function may be called before the display is initialized.
318 */
319 static struct native_probe *
320 egl_g3d_get_probe(_EGLDriver *drv, _EGLDisplay *dpy)
321 {
322 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
323 struct native_probe *nprobe;
324
325 nprobe = (struct native_probe *) _eglGetProbeCache(gdrv->probe_key);
326 if (!nprobe || nprobe->display != dpy->NativeDisplay) {
327 if (nprobe)
328 nprobe->destroy(nprobe);
329 nprobe = native_create_probe(dpy->NativeDisplay);
330 _eglSetProbeCache(gdrv->probe_key, (void *) nprobe);
331 }
332
333 return nprobe;
334 }
335
336 /**
337 * Destroy the probe object of the display. The display may be NULL.
338 *
339 * Note that this function may be called before the display is initialized.
340 */
341 static void
342 egl_g3d_destroy_probe(_EGLDriver *drv, _EGLDisplay *dpy)
343 {
344 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
345 struct native_probe *nprobe;
346
347 nprobe = (struct native_probe *) _eglGetProbeCache(gdrv->probe_key);
348 if (nprobe && (!dpy || nprobe->display == dpy->NativeDisplay)) {
349 nprobe->destroy(nprobe);
350 _eglSetProbeCache(gdrv->probe_key, NULL);
351 }
352 }
353
354 /**
355 * Return an API mask that consists of the state trackers that supports the
356 * given mode.
357 *
358 * FIXME add st_is_mode_supported()?
359 */
360 static EGLint
361 get_mode_api_mask(const __GLcontextModes *mode, EGLint api_mask)
362 {
363 EGLint check;
364
365 /* OpenGL ES 1.x and 2.x are checked together */
366 check = EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT;
367 if (api_mask & check) {
368 /* this is required by EGL, not by OpenGL ES */
369 if (mode->drawableType & GLX_WINDOW_BIT && !mode->doubleBufferMode)
370 api_mask &= ~check;
371 }
372
373 check = EGL_OPENVG_BIT;
374 if (api_mask & check) {
375 /* vega st needs the depth/stencil rb */
376 if (!mode->depthBits && !mode->stencilBits)
377 api_mask &= ~check;
378 }
379
380 return api_mask;
381 }
382
383 #ifdef EGL_MESA_screen_surface
384
385 static void
386 egl_g3d_add_screens(_EGLDriver *drv, _EGLDisplay *dpy)
387 {
388 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
389 const struct native_connector **native_connectors;
390 EGLint num_connectors, i;
391
392 native_connectors =
393 gdpy->native->modeset->get_connectors(gdpy->native, &num_connectors, NULL);
394 if (!num_connectors) {
395 if (native_connectors)
396 free(native_connectors);
397 return;
398 }
399
400 for (i = 0; i < num_connectors; i++) {
401 const struct native_connector *nconn = native_connectors[i];
402 struct egl_g3d_screen *gscr;
403 const struct native_mode **native_modes;
404 EGLint num_modes, j;
405
406 /* TODO support for hotplug */
407 native_modes =
408 gdpy->native->modeset->get_modes(gdpy->native, nconn, &num_modes);
409 if (!num_modes) {
410 if (native_modes)
411 free(native_modes);
412 continue;
413 }
414
415 gscr = CALLOC_STRUCT(egl_g3d_screen);
416 if (!gscr) {
417 free(native_modes);
418 continue;
419 }
420
421 _eglInitScreen(&gscr->base);
422
423 for (j = 0; j < num_modes; j++) {
424 const struct native_mode *nmode = native_modes[j];
425 _EGLMode *mode;
426
427 mode = _eglAddNewMode(&gscr->base, nmode->width, nmode->height,
428 nmode->refresh_rate, nmode->desc);
429 if (!mode)
430 break;
431 /* gscr->native_modes and gscr->base.Modes should be consistent */
432 assert(mode == &gscr->base.Modes[j]);
433 }
434
435 gscr->native = nconn;
436 gscr->native_modes = native_modes;
437
438 _eglAddScreen(dpy, &gscr->base);
439 }
440
441 free(native_connectors);
442 }
443
444 #endif /* EGL_MESA_screen_surface */
445
446 /**
447 * Add configs to display and return the next config ID.
448 */
449 static EGLint
450 egl_g3d_add_configs(_EGLDriver *drv, _EGLDisplay *dpy, EGLint id)
451 {
452 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
453 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
454 const struct native_config **native_configs;
455 int num_configs, i;
456
457 native_configs = gdpy->native->get_configs(gdpy->native,
458 &num_configs);
459 if (!num_configs) {
460 if (native_configs)
461 free(native_configs);
462 return id;
463 }
464
465 for (i = 0; i < num_configs; i++) {
466 EGLint api_mask;
467 struct egl_g3d_config *gconf;
468 EGLBoolean valid;
469
470 gconf = CALLOC_STRUCT(egl_g3d_config);
471 if (!gconf)
472 continue;
473
474 _eglInitConfig(&gconf->base, dpy, id);
475
476 api_mask = get_mode_api_mask(&native_configs[i]->mode, gdrv->api_mask);
477 if (!api_mask) {
478 _eglLog(_EGL_DEBUG, "no state tracker supports config 0x%x",
479 native_configs[i]->mode.visualID);
480 }
481
482 valid = _eglConfigFromContextModesRec(&gconf->base,
483 &native_configs[i]->mode, api_mask, api_mask);
484 if (valid) {
485 #ifdef EGL_MESA_screen_surface
486 /* check if scanout surface bit is set */
487 if (native_configs[i]->scanout_bit) {
488 EGLint val = GET_CONFIG_ATTRIB(&gconf->base, EGL_SURFACE_TYPE);
489 val |= EGL_SCREEN_BIT_MESA;
490 SET_CONFIG_ATTRIB(&gconf->base, EGL_SURFACE_TYPE, val);
491 }
492 #endif
493 valid = _eglValidateConfig(&gconf->base, EGL_FALSE);
494 }
495 if (!valid) {
496 _eglLog(_EGL_DEBUG, "skip invalid config 0x%x",
497 native_configs[i]->mode.visualID);
498 free(gconf);
499 continue;
500 }
501
502 gconf->native = native_configs[i];
503 _eglAddConfig(dpy, &gconf->base);
504 id++;
505 }
506
507 free(native_configs);
508 return id;
509 }
510
511 /**
512 * Re-validate the context.
513 */
514 static void
515 egl_g3d_update_buffer(struct pipe_screen *screen, void *context_private)
516 {
517 struct egl_g3d_context *gctx = egl_g3d_context(context_private);
518 egl_g3d_validate_context(gctx->base.Resource.Display, &gctx->base);
519 }
520
521 static void
522 egl_g3d_invalid_surface(struct native_display *ndpy,
523 struct native_surface *nsurf,
524 unsigned int seq_num)
525 {
526 /* XXX not thread safe? */
527 struct egl_g3d_surface *gsurf = egl_g3d_surface(nsurf->user_data);
528 struct egl_g3d_context *gctx = egl_g3d_context(gsurf->base.CurrentContext);
529
530 /* set force_validate to skip an unnecessary check */
531 if (gctx)
532 gctx->force_validate = TRUE;
533 }
534
535 static struct native_event_handler egl_g3d_native_event_handler = {
536 .invalid_surface = egl_g3d_invalid_surface
537 };
538
539 static EGLBoolean
540 egl_g3d_terminate(_EGLDriver *drv, _EGLDisplay *dpy)
541 {
542 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
543 EGLint i;
544
545 _eglReleaseDisplayResources(drv, dpy);
546 _eglCleanupDisplay(dpy);
547
548 if (dpy->Screens) {
549 for (i = 0; i < dpy->NumScreens; i++) {
550 struct egl_g3d_screen *gscr = egl_g3d_screen(dpy->Screens[i]);
551 free(gscr->native_modes);
552 free(gscr);
553 }
554 free(dpy->Screens);
555 }
556
557 if (gdpy->native)
558 gdpy->native->destroy(gdpy->native);
559
560 free(gdpy);
561 dpy->DriverData = NULL;
562
563 return EGL_TRUE;
564 }
565
566 static EGLBoolean
567 egl_g3d_initialize(_EGLDriver *drv, _EGLDisplay *dpy,
568 EGLint *major, EGLint *minor)
569 {
570 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
571 struct egl_g3d_display *gdpy;
572
573 /* the probe object is unlikely to be needed again */
574 egl_g3d_destroy_probe(drv, dpy);
575
576 gdpy = CALLOC_STRUCT(egl_g3d_display);
577 if (!gdpy) {
578 _eglError(EGL_BAD_ALLOC, "eglInitialize");
579 goto fail;
580 }
581 dpy->DriverData = gdpy;
582
583 gdpy->native = native_create_display(dpy->NativeDisplay,
584 &egl_g3d_native_event_handler);
585 if (!gdpy->native) {
586 _eglError(EGL_NOT_INITIALIZED, "eglInitialize(no usable display)");
587 goto fail;
588 }
589
590 gdpy->native->user_data = (void *) dpy;
591 gdpy->native->screen->update_buffer = egl_g3d_update_buffer;
592
593 egl_g3d_init_st(&gdrv->base);
594 dpy->ClientAPIsMask = gdrv->api_mask;
595
596 #ifdef EGL_MESA_screen_surface
597 /* enable MESA_screen_surface before adding (and validating) configs */
598 if (gdpy->native->modeset) {
599 dpy->Extensions.MESA_screen_surface = EGL_TRUE;
600 egl_g3d_add_screens(drv, dpy);
601 }
602 #endif
603
604 dpy->Extensions.KHR_image_base = EGL_TRUE;
605 if (gdpy->native->get_param(gdpy->native, NATIVE_PARAM_USE_NATIVE_BUFFER))
606 dpy->Extensions.KHR_image_pixmap = EGL_TRUE;
607
608 if (egl_g3d_add_configs(drv, dpy, 1) == 1) {
609 _eglError(EGL_NOT_INITIALIZED, "eglInitialize(unable to add configs)");
610 goto fail;
611 }
612
613 *major = 1;
614 *minor = 4;
615
616 return EGL_TRUE;
617
618 fail:
619 if (gdpy)
620 egl_g3d_terminate(drv, dpy);
621 return EGL_FALSE;
622 }
623
624 static _EGLContext *
625 egl_g3d_create_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
626 _EGLContext *share, const EGLint *attribs)
627 {
628 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
629 struct egl_g3d_context *gshare = egl_g3d_context(share);
630 struct egl_g3d_config *gconf = egl_g3d_config(conf);
631 struct egl_g3d_context *gctx;
632 const __GLcontextModes *mode;
633
634 gctx = CALLOC_STRUCT(egl_g3d_context);
635 if (!gctx) {
636 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
637 return NULL;
638 }
639
640 if (!_eglInitContext(&gctx->base, dpy, conf, attribs)) {
641 free(gctx);
642 return NULL;
643 }
644
645 gctx->stapi = egl_g3d_choose_st(drv, &gctx->base);
646 if (!gctx->stapi) {
647 free(gctx);
648 return NULL;
649 }
650
651 mode = &gconf->native->mode;
652
653 gctx->pipe = gdpy->native->screen->context_create(
654 gdpy->native->screen,
655 (void *) &gctx->base);
656
657 if (!gctx->pipe) {
658 free(gctx);
659 return NULL;
660 }
661
662 gctx->st_ctx = gctx->stapi->st_create_context(gctx->pipe, mode,
663 (gshare) ? gshare->st_ctx : NULL);
664 if (!gctx->st_ctx) {
665 gctx->pipe->destroy(gctx->pipe);
666 free(gctx);
667 return NULL;
668 }
669
670 return &gctx->base;
671 }
672
673 /**
674 * Destroy a context.
675 */
676 static void
677 destroy_context(_EGLDisplay *dpy, _EGLContext *ctx)
678 {
679 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
680
681 /* FIXME a context might live longer than its display */
682 if (!dpy->Initialized)
683 _eglLog(_EGL_FATAL, "destroy a context with an unitialized display");
684
685 egl_g3d_realloc_context(dpy, &gctx->base);
686 /* it will destroy the associated pipe context */
687 gctx->stapi->st_destroy_context(gctx->st_ctx);
688
689 free(gctx);
690 }
691
692 static EGLBoolean
693 egl_g3d_destroy_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
694 {
695 if (!_eglIsContextBound(ctx))
696 destroy_context(dpy, ctx);
697 return EGL_TRUE;
698 }
699
700 struct egl_g3d_create_surface_arg {
701 EGLint type;
702 union {
703 EGLNativeWindowType win;
704 EGLNativePixmapType pix;
705 } u;
706 };
707
708 static _EGLSurface *
709 egl_g3d_create_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
710 struct egl_g3d_create_surface_arg *arg,
711 const EGLint *attribs)
712 {
713 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
714 struct egl_g3d_config *gconf = egl_g3d_config(conf);
715 struct egl_g3d_surface *gsurf;
716 struct native_surface *nsurf;
717 const char *err;
718
719 switch (arg->type) {
720 case EGL_WINDOW_BIT:
721 err = "eglCreateWindowSurface";
722 break;
723 case EGL_PIXMAP_BIT:
724 err = "eglCreatePixmapSurface";
725 break;
726 case EGL_PBUFFER_BIT:
727 err = "eglCreatePBufferSurface";
728 break;
729 #ifdef EGL_MESA_screen_surface
730 case EGL_SCREEN_BIT_MESA:
731 err = "eglCreateScreenSurface";
732 break;
733 #endif
734 default:
735 err = "eglCreateUnknownSurface";
736 break;
737 }
738
739 gsurf = CALLOC_STRUCT(egl_g3d_surface);
740 if (!gsurf) {
741 _eglError(EGL_BAD_ALLOC, err);
742 return NULL;
743 }
744
745 if (!_eglInitSurface(&gsurf->base, dpy, arg->type, conf, attribs)) {
746 free(gsurf);
747 return NULL;
748 }
749
750 /* create the native surface */
751 switch (arg->type) {
752 case EGL_WINDOW_BIT:
753 nsurf = gdpy->native->create_window_surface(gdpy->native,
754 arg->u.win, gconf->native);
755 break;
756 case EGL_PIXMAP_BIT:
757 nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
758 arg->u.pix, gconf->native);
759 break;
760 case EGL_PBUFFER_BIT:
761 nsurf = gdpy->native->create_pbuffer_surface(gdpy->native,
762 gconf->native, gsurf->base.Width, gsurf->base.Height);
763 break;
764 #ifdef EGL_MESA_screen_surface
765 case EGL_SCREEN_BIT_MESA:
766 /* prefer back buffer (move to _eglInitSurface?) */
767 gsurf->base.RenderBuffer = EGL_BACK_BUFFER;
768 nsurf = gdpy->native->modeset->create_scanout_surface(gdpy->native,
769 gconf->native, gsurf->base.Width, gsurf->base.Height);
770 break;
771 #endif
772 default:
773 nsurf = NULL;
774 break;
775 }
776
777 if (!nsurf) {
778 free(gsurf);
779 return NULL;
780 }
781 /* initialize the geometry */
782 if (!nsurf->validate(nsurf, 0x0, &gsurf->sequence_number, NULL,
783 &gsurf->base.Width, &gsurf->base.Height)) {
784 nsurf->destroy(nsurf);
785 free(gsurf);
786 return NULL;
787 }
788
789 nsurf->user_data = &gsurf->base;
790 gsurf->native = nsurf;
791
792 gsurf->render_att = (gsurf->base.RenderBuffer == EGL_SINGLE_BUFFER) ?
793 NATIVE_ATTACHMENT_FRONT_LEFT : NATIVE_ATTACHMENT_BACK_LEFT;
794 if (!gconf->native->mode.doubleBufferMode)
795 gsurf->render_att = NATIVE_ATTACHMENT_FRONT_LEFT;
796
797 return &gsurf->base;
798 }
799
800 static _EGLSurface *
801 egl_g3d_create_window_surface(_EGLDriver *drv, _EGLDisplay *dpy,
802 _EGLConfig *conf, EGLNativeWindowType win,
803 const EGLint *attribs)
804 {
805 struct egl_g3d_create_surface_arg arg;
806
807 memset(&arg, 0, sizeof(arg));
808 arg.type = EGL_WINDOW_BIT;
809 arg.u.win = win;
810
811 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
812 }
813
814 static _EGLSurface *
815 egl_g3d_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy,
816 _EGLConfig *conf, EGLNativePixmapType pix,
817 const EGLint *attribs)
818 {
819 struct egl_g3d_create_surface_arg arg;
820
821 memset(&arg, 0, sizeof(arg));
822 arg.type = EGL_PIXMAP_BIT;
823 arg.u.pix = pix;
824
825 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
826 }
827
828 static _EGLSurface *
829 egl_g3d_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy,
830 _EGLConfig *conf, const EGLint *attribs)
831 {
832 struct egl_g3d_create_surface_arg arg;
833
834 memset(&arg, 0, sizeof(arg));
835 arg.type = EGL_PBUFFER_BIT;
836
837 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
838 }
839
840 /**
841 * Destroy a surface.
842 */
843 static void
844 destroy_surface(_EGLDisplay *dpy, _EGLSurface *surf)
845 {
846 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
847
848 /* FIXME a surface might live longer than its display */
849 if (!dpy->Initialized)
850 _eglLog(_EGL_FATAL, "destroy a surface with an unitialized display");
851
852 pipe_surface_reference(&gsurf->render_surface, NULL);
853 gsurf->native->destroy(gsurf->native);
854 free(gsurf);
855 }
856
857 static EGLBoolean
858 egl_g3d_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
859 {
860 if (!_eglIsSurfaceBound(surf))
861 destroy_surface(dpy, surf);
862 return EGL_TRUE;
863 }
864
865 static EGLBoolean
866 egl_g3d_make_current(_EGLDriver *drv, _EGLDisplay *dpy,
867 _EGLSurface *draw, _EGLSurface *read, _EGLContext *ctx)
868 {
869 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
870 struct egl_g3d_surface *gdraw = egl_g3d_surface(draw);
871 struct egl_g3d_context *old_gctx;
872 EGLBoolean ok = EGL_TRUE;
873
874 /* bind the new context and return the "orphaned" one */
875 if (!_eglBindContext(&ctx, &draw, &read))
876 return EGL_FALSE;
877 old_gctx = egl_g3d_context(ctx);
878
879 if (old_gctx) {
880 /* flush old context */
881 old_gctx->stapi->st_flush(old_gctx->st_ctx,
882 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
883
884 /*
885 * The old context is no longer current, and egl_g3d_realloc_context()
886 * should be called to destroy the framebuffers. However, it is possible
887 * that it will be made current again with the same draw/read surfaces.
888 * It might be better to keep it around.
889 */
890 }
891
892 if (gctx) {
893 ok = egl_g3d_realloc_context(dpy, &gctx->base);
894 if (ok) {
895 /* XXX: need to pass the winsys argument for
896 * flush_frontbuffer in the fourth parameter here:
897 */
898 ok = gctx->stapi->st_make_current(gctx->st_ctx,
899 gctx->draw.st_fb,
900 gctx->read.st_fb,
901 NULL);
902 if (ok) {
903 egl_g3d_validate_context(dpy, &gctx->base);
904 if (gdraw->base.Type == EGL_WINDOW_BIT) {
905 gctx->base.WindowRenderBuffer =
906 (gdraw->render_att == NATIVE_ATTACHMENT_FRONT_LEFT) ?
907 EGL_SINGLE_BUFFER : EGL_BACK_BUFFER;
908 }
909 }
910 }
911 }
912 else if (old_gctx) {
913 ok = old_gctx->stapi->st_make_current(NULL, NULL, NULL, NULL);
914 old_gctx->base.WindowRenderBuffer = EGL_NONE;
915 }
916
917 if (ctx && !_eglIsContextLinked(ctx))
918 destroy_context(dpy, ctx);
919 if (draw && !_eglIsSurfaceLinked(draw))
920 destroy_surface(dpy, draw);
921 if (read && read != draw && !_eglIsSurfaceLinked(read))
922 destroy_surface(dpy, read);
923
924 return ok;
925 }
926
927 static EGLBoolean
928 egl_g3d_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
929 {
930 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
931 _EGLContext *ctx = _eglGetCurrentContext();
932 struct egl_g3d_context *gctx = NULL;
933
934 /* no-op for pixmap or pbuffer surface */
935 if (gsurf->base.Type == EGL_PIXMAP_BIT ||
936 gsurf->base.Type == EGL_PBUFFER_BIT)
937 return EGL_TRUE;
938
939 /* or when the surface is single-buffered */
940 if (gsurf->render_att == NATIVE_ATTACHMENT_FRONT_LEFT)
941 return EGL_TRUE;
942
943 if (ctx && ctx->DrawSurface == surf)
944 gctx = egl_g3d_context(ctx);
945
946 /* flush if the surface is current */
947 if (gctx)
948 gctx->stapi->st_notify_swapbuffers(gctx->draw.st_fb);
949
950 return gsurf->native->swap_buffers(gsurf->native);
951 }
952
953 /**
954 * Find a config that supports the pixmap.
955 */
956 _EGLConfig *
957 egl_g3d_find_pixmap_config(_EGLDisplay *dpy, EGLNativePixmapType pix)
958 {
959 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
960 struct egl_g3d_config *gconf;
961 EGLint i;
962
963 for (i = 0; i < dpy->NumConfigs; i++) {
964 gconf = egl_g3d_config(dpy->Configs[i]);
965 if (gdpy->native->is_pixmap_supported(gdpy->native, pix, gconf->native))
966 break;
967 }
968
969 return (i < dpy->NumConfigs) ? &gconf->base : NULL;
970 }
971
972 /**
973 * Get the pipe surface of the given attachment of the native surface.
974 */
975 static struct pipe_surface *
976 get_pipe_surface(struct native_display *ndpy, struct native_surface *nsurf,
977 enum native_attachment natt)
978 {
979 struct pipe_texture *textures[NUM_NATIVE_ATTACHMENTS];
980 struct pipe_surface *psurf;
981
982 textures[natt] = NULL;
983 nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL);
984 if (!textures[natt])
985 return NULL;
986
987 psurf = ndpy->screen->get_tex_surface(ndpy->screen, textures[natt],
988 0, 0, 0, PIPE_BUFFER_USAGE_CPU_WRITE);
989 pipe_texture_reference(&textures[natt], NULL);
990
991 return psurf;
992 }
993
994 static EGLBoolean
995 egl_g3d_copy_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
996 EGLNativePixmapType target)
997 {
998 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
999 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
1000 _EGLContext *ctx = _eglGetCurrentContext();
1001 struct egl_g3d_config *gconf;
1002 struct native_surface *nsurf;
1003 struct pipe_screen *screen = gdpy->native->screen;
1004 struct pipe_surface *psurf;
1005
1006 if (!gsurf->render_surface)
1007 return EGL_TRUE;
1008
1009 gconf = egl_g3d_config(egl_g3d_find_pixmap_config(dpy, target));
1010 if (!gconf)
1011 return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
1012
1013 nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
1014 target, gconf->native);
1015 if (!nsurf)
1016 return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
1017
1018 /* flush if the surface is current */
1019 if (ctx && ctx->DrawSurface == &gsurf->base) {
1020 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
1021 gctx->stapi->st_flush(gctx->st_ctx,
1022 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
1023 }
1024
1025 psurf = get_pipe_surface(gdpy->native, nsurf, NATIVE_ATTACHMENT_FRONT_LEFT);
1026 if (psurf) {
1027 struct pipe_context pipe;
1028
1029 /**
1030 * XXX This is hacky. If we might allow the EGLDisplay to create a pipe
1031 * context of its own and use the blitter context for this.
1032 */
1033 memset(&pipe, 0, sizeof(pipe));
1034 pipe.screen = screen;
1035
1036 util_surface_copy(&pipe, FALSE, psurf, 0, 0,
1037 gsurf->render_surface, 0, 0, psurf->width, psurf->height);
1038
1039 pipe_surface_reference(&psurf, NULL);
1040 nsurf->flush_frontbuffer(nsurf);
1041 }
1042
1043 nsurf->destroy(nsurf);
1044
1045 return EGL_TRUE;
1046 }
1047
1048 static EGLBoolean
1049 egl_g3d_wait_client(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
1050 {
1051 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
1052 gctx->stapi->st_finish(gctx->st_ctx);
1053 return EGL_TRUE;
1054 }
1055
1056 static EGLBoolean
1057 egl_g3d_wait_native(_EGLDriver *drv, _EGLDisplay *dpy, EGLint engine)
1058 {
1059 _EGLContext *ctx = _eglGetCurrentContext();
1060
1061 if (engine != EGL_CORE_NATIVE_ENGINE)
1062 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
1063
1064 if (ctx && ctx->DrawSurface) {
1065 struct egl_g3d_surface *gsurf = egl_g3d_surface(ctx->DrawSurface);
1066 gsurf->native->wait(gsurf->native);
1067 }
1068
1069 return EGL_TRUE;
1070 }
1071
1072 static _EGLProc
1073 egl_g3d_get_proc_address(_EGLDriver *drv, const char *procname)
1074 {
1075 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
1076 _EGLProc proc;
1077 EGLint i;
1078
1079 /* in case this is called before a display is initialized */
1080 egl_g3d_init_st(&gdrv->base);
1081
1082 for (i = 0; i < NUM_EGL_G3D_STS; i++) {
1083 const struct egl_g3d_st *stapi = gdrv->stapis[i];
1084 if (stapi) {
1085 proc = (_EGLProc) stapi->st_get_proc_address(procname);
1086 if (proc)
1087 return proc;
1088 }
1089 }
1090
1091 return (_EGLProc) NULL;
1092 }
1093
1094 static EGLBoolean
1095 egl_g3d_bind_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
1096 _EGLSurface *surf, EGLint buffer)
1097 {
1098 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
1099 _EGLContext *es1 = _eglGetAPIContext(EGL_OPENGL_ES_API);
1100 struct egl_g3d_context *gctx;
1101 enum pipe_format target_format;
1102 int target;
1103
1104 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT)
1105 return _eglError(EGL_BAD_SURFACE, "eglBindTexImage");
1106 if (buffer != EGL_BACK_BUFFER)
1107 return _eglError(EGL_BAD_PARAMETER, "eglBindTexImage");
1108 if (gsurf->base.BoundToTexture)
1109 return _eglError(EGL_BAD_ACCESS, "eglBindTexImage");
1110
1111 switch (gsurf->base.TextureFormat) {
1112 case EGL_TEXTURE_RGB:
1113 target_format = PIPE_FORMAT_R8G8B8_UNORM;
1114 break;
1115 case EGL_TEXTURE_RGBA:
1116 target_format = PIPE_FORMAT_B8G8R8A8_UNORM;
1117 break;
1118 default:
1119 return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
1120 }
1121
1122 switch (gsurf->base.TextureTarget) {
1123 case EGL_TEXTURE_2D:
1124 target = ST_TEXTURE_2D;
1125 break;
1126 default:
1127 return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
1128 }
1129
1130 if (!es1)
1131 return EGL_TRUE;
1132 if (!gsurf->render_surface)
1133 return EGL_FALSE;
1134
1135 /* flush properly if the surface is bound */
1136 if (gsurf->base.CurrentContext) {
1137 gctx = egl_g3d_context(gsurf->base.CurrentContext);
1138 gctx->stapi->st_flush(gctx->st_ctx,
1139 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
1140 }
1141
1142 gctx = egl_g3d_context(es1);
1143 gctx->stapi->st_bind_texture_surface(gsurf->render_surface,
1144 target, gsurf->base.MipmapLevel, target_format);
1145
1146 gsurf->base.BoundToTexture = EGL_TRUE;
1147
1148 return EGL_TRUE;
1149 }
1150
1151 static EGLBoolean
1152 egl_g3d_release_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
1153 _EGLSurface *surf, EGLint buffer)
1154 {
1155 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
1156
1157 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT ||
1158 !gsurf->base.BoundToTexture)
1159 return _eglError(EGL_BAD_SURFACE, "eglReleaseTexImage");
1160 if (buffer != EGL_BACK_BUFFER)
1161 return _eglError(EGL_BAD_PARAMETER, "eglReleaseTexImage");
1162
1163 if (gsurf->render_surface) {
1164 _EGLContext *ctx = _eglGetAPIContext(EGL_OPENGL_ES_API);
1165 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
1166
1167 /* what if the context the surface binds to is no longer current? */
1168 if (gctx)
1169 gctx->stapi->st_unbind_texture_surface(gsurf->render_surface,
1170 ST_TEXTURE_2D, gsurf->base.MipmapLevel);
1171 }
1172
1173 gsurf->base.BoundToTexture = EGL_FALSE;
1174
1175 return EGL_TRUE;
1176 }
1177
1178 #ifdef EGL_MESA_screen_surface
1179
1180 static _EGLSurface *
1181 egl_g3d_create_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
1182 _EGLConfig *conf, const EGLint *attribs)
1183 {
1184 struct egl_g3d_create_surface_arg arg;
1185
1186 memset(&arg, 0, sizeof(arg));
1187 arg.type = EGL_SCREEN_BIT_MESA;
1188
1189 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
1190 }
1191
1192 static EGLBoolean
1193 egl_g3d_show_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
1194 _EGLScreen *scr, _EGLSurface *surf,
1195 _EGLMode *mode)
1196 {
1197 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
1198 struct egl_g3d_screen *gscr = egl_g3d_screen(scr);
1199 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
1200 struct native_surface *nsurf;
1201 const struct native_mode *nmode;
1202 EGLBoolean changed;
1203
1204 if (gsurf) {
1205 EGLint idx;
1206
1207 if (!mode)
1208 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA");
1209 if (gsurf->base.Type != EGL_SCREEN_BIT_MESA)
1210 return _eglError(EGL_BAD_SURFACE, "eglShowScreenSurfaceMESA");
1211 if (gsurf->base.Width < mode->Width || gsurf->base.Height < mode->Height)
1212 return _eglError(EGL_BAD_MATCH,
1213 "eglShowSurfaceMESA(surface smaller than mode size)");
1214
1215 /* find the index of the mode */
1216 for (idx = 0; idx < gscr->base.NumModes; idx++)
1217 if (mode == &gscr->base.Modes[idx])
1218 break;
1219 if (idx >= gscr->base.NumModes) {
1220 return _eglError(EGL_BAD_MODE_MESA,
1221 "eglShowSurfaceMESA(unknown mode)");
1222 }
1223
1224 nsurf = gsurf->native;
1225 nmode = gscr->native_modes[idx];
1226 }
1227 else {
1228 if (mode)
1229 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA");
1230
1231 /* disable the screen */
1232 nsurf = NULL;
1233 nmode = NULL;
1234 }
1235
1236 /* TODO surface panning by CRTC choosing */
1237 changed = gdpy->native->modeset->program(gdpy->native, 0, nsurf,
1238 gscr->base.OriginX, gscr->base.OriginY, &gscr->native, 1, nmode);
1239 if (changed) {
1240 gscr->base.CurrentSurface = &gsurf->base;
1241 gscr->base.CurrentMode = mode;
1242 }
1243
1244 return changed;
1245 }
1246
1247 #endif /* EGL_MESA_screen_surface */
1248
1249 static EGLint
1250 egl_g3d_probe(_EGLDriver *drv, _EGLDisplay *dpy)
1251 {
1252 struct native_probe *nprobe;
1253 enum native_probe_result res;
1254 EGLint score;
1255
1256 nprobe = egl_g3d_get_probe(drv, dpy);
1257 res = native_get_probe_result(nprobe);
1258
1259 switch (res) {
1260 case NATIVE_PROBE_UNKNOWN:
1261 default:
1262 score = 0;
1263 break;
1264 case NATIVE_PROBE_FALLBACK:
1265 score = 40;
1266 break;
1267 case NATIVE_PROBE_SUPPORTED:
1268 score = 50;
1269 break;
1270 case NATIVE_PROBE_EXACT:
1271 score = 100;
1272 break;
1273 }
1274
1275 return score;
1276 }
1277
1278 static void
1279 egl_g3d_unload(_EGLDriver *drv)
1280 {
1281 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
1282
1283 egl_g3d_destroy_probe(drv, NULL);
1284 free(gdrv);
1285 }
1286
1287 _EGLDriver *
1288 _eglMain(const char *args)
1289 {
1290 static char driver_name[64];
1291 struct egl_g3d_driver *gdrv;
1292
1293 snprintf(driver_name, sizeof(driver_name),
1294 "Gallium/%s", native_get_name());
1295
1296 gdrv = CALLOC_STRUCT(egl_g3d_driver);
1297 if (!gdrv)
1298 return NULL;
1299
1300 _eglInitDriverFallbacks(&gdrv->base);
1301
1302 gdrv->base.API.Initialize = egl_g3d_initialize;
1303 gdrv->base.API.Terminate = egl_g3d_terminate;
1304 gdrv->base.API.CreateContext = egl_g3d_create_context;
1305 gdrv->base.API.DestroyContext = egl_g3d_destroy_context;
1306 gdrv->base.API.CreateWindowSurface = egl_g3d_create_window_surface;
1307 gdrv->base.API.CreatePixmapSurface = egl_g3d_create_pixmap_surface;
1308 gdrv->base.API.CreatePbufferSurface = egl_g3d_create_pbuffer_surface;
1309 gdrv->base.API.DestroySurface = egl_g3d_destroy_surface;
1310 gdrv->base.API.MakeCurrent = egl_g3d_make_current;
1311 gdrv->base.API.SwapBuffers = egl_g3d_swap_buffers;
1312 gdrv->base.API.CopyBuffers = egl_g3d_copy_buffers;
1313 gdrv->base.API.WaitClient = egl_g3d_wait_client;
1314 gdrv->base.API.WaitNative = egl_g3d_wait_native;
1315 gdrv->base.API.GetProcAddress = egl_g3d_get_proc_address;
1316
1317 gdrv->base.API.BindTexImage = egl_g3d_bind_tex_image;
1318 gdrv->base.API.ReleaseTexImage = egl_g3d_release_tex_image;
1319
1320 gdrv->base.API.CreateImageKHR = egl_g3d_create_image;
1321 gdrv->base.API.DestroyImageKHR = egl_g3d_destroy_image;
1322
1323 #ifdef EGL_MESA_screen_surface
1324 gdrv->base.API.CreateScreenSurfaceMESA = egl_g3d_create_screen_surface;
1325 gdrv->base.API.ShowScreenSurfaceMESA = egl_g3d_show_screen_surface;
1326 #endif
1327
1328 gdrv->base.Name = driver_name;
1329 gdrv->base.Probe = egl_g3d_probe;
1330 gdrv->base.Unload = egl_g3d_unload;
1331
1332 /* the key is " EGL G3D" */
1333 gdrv->probe_key = 0x0E61063D;
1334
1335 return &gdrv->base;
1336 }