egl_g3d_api.c revision 0fb2dcc98ff46299094c308b7b4e0cde9e38d5c0
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.9 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 OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 * DEALINGS IN THE SOFTWARE. 24 */ 25 26#include "egldriver.h" 27#include "eglcurrent.h" 28#include "egllog.h" 29 30#include "pipe/p_screen.h" 31#include "util/u_memory.h" 32#include "util/u_inlines.h" 33#include "util/u_box.h" 34 35#include "egl_g3d.h" 36#include "egl_g3d_api.h" 37#include "egl_g3d_image.h" 38#include "egl_g3d_sync.h" 39#include "egl_g3d_st.h" 40#include "egl_g3d_loader.h" 41#include "native.h" 42 43/** 44 * Return the state tracker for the given context. 45 */ 46static struct st_api * 47egl_g3d_choose_st(_EGLDriver *drv, _EGLContext *ctx, 48 enum st_profile_type *profile) 49{ 50 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv); 51 struct st_api *stapi; 52 EGLint api = -1; 53 54 *profile = ST_PROFILE_DEFAULT; 55 56 switch (ctx->ClientAPI) { 57 case EGL_OPENGL_ES_API: 58 switch (ctx->ClientVersion) { 59 case 1: 60 api = ST_API_OPENGL; 61 *profile = ST_PROFILE_OPENGL_ES1; 62 break; 63 case 2: 64 api = ST_API_OPENGL; 65 *profile = ST_PROFILE_OPENGL_ES2; 66 break; 67 default: 68 _eglLog(_EGL_WARNING, "unknown client version %d", 69 ctx->ClientVersion); 70 break; 71 } 72 break; 73 case EGL_OPENVG_API: 74 api = ST_API_OPENVG; 75 break; 76 case EGL_OPENGL_API: 77 api = ST_API_OPENGL; 78 break; 79 default: 80 _eglLog(_EGL_WARNING, "unknown client API 0x%04x", ctx->ClientAPI); 81 break; 82 } 83 84 switch (api) { 85 case ST_API_OPENGL: 86 stapi = gdrv->loader->guess_gl_api(*profile); 87 break; 88 case ST_API_OPENVG: 89 stapi = gdrv->loader->get_st_api(api); 90 break; 91 default: 92 stapi = NULL; 93 break; 94 } 95 if (stapi && !(stapi->profile_mask & (1 << *profile))) 96 stapi = NULL; 97 98 return stapi; 99} 100 101static int 102egl_g3d_compare_config(const _EGLConfig *conf1, const _EGLConfig *conf2, 103 void *priv_data) 104{ 105 const _EGLConfig *criteria = (const _EGLConfig *) priv_data; 106 107 /* EGL_NATIVE_VISUAL_TYPE ignored? */ 108 return _eglCompareConfigs(conf1, conf2, criteria, EGL_TRUE); 109} 110 111static EGLBoolean 112egl_g3d_match_config(const _EGLConfig *conf, const _EGLConfig *criteria) 113{ 114 if (!_eglMatchConfig(conf, criteria)) 115 return EGL_FALSE; 116 117 if (criteria->MatchNativePixmap != EGL_NONE && 118 criteria->MatchNativePixmap != EGL_DONT_CARE) { 119 struct egl_g3d_display *gdpy = egl_g3d_display(conf->Display); 120 struct egl_g3d_config *gconf = egl_g3d_config(conf); 121 EGLNativePixmapType pix = 122 (EGLNativePixmapType) criteria->MatchNativePixmap; 123 124 if (!gdpy->native->is_pixmap_supported(gdpy->native, pix, gconf->native)) 125 return EGL_FALSE; 126 } 127 128 return EGL_TRUE; 129} 130 131static EGLBoolean 132egl_g3d_choose_config(_EGLDriver *drv, _EGLDisplay *dpy, const EGLint *attribs, 133 EGLConfig *configs, EGLint size, EGLint *num_configs) 134{ 135 _EGLConfig **tmp_configs, criteria; 136 EGLint tmp_size, i; 137 138 if (!num_configs) 139 return _eglError(EGL_BAD_PARAMETER, "eglChooseConfigs"); 140 141 if (!_eglParseConfigAttribList(&criteria, dpy, attribs)) 142 return _eglError(EGL_BAD_ATTRIBUTE, "eglChooseConfig"); 143 144 /* get the number of matched configs */ 145 tmp_size = _eglFilterArray(dpy->Configs, NULL, 0, 146 (_EGLArrayForEach) egl_g3d_match_config, (void *) &criteria); 147 if (!tmp_size) { 148 *num_configs = tmp_size; 149 return EGL_TRUE; 150 } 151 152 tmp_configs = MALLOC(sizeof(tmp_configs[0]) * tmp_size); 153 if (!tmp_configs) 154 return _eglError(EGL_BAD_ALLOC, "eglChooseConfig(out of memory)"); 155 156 /* get the matched configs */ 157 _eglFilterArray(dpy->Configs, (void **) tmp_configs, tmp_size, 158 (_EGLArrayForEach) egl_g3d_match_config, (void *) &criteria); 159 160 /* perform sorting of configs */ 161 if (tmp_configs && tmp_size) { 162 _eglSortConfigs((const _EGLConfig **) tmp_configs, tmp_size, 163 egl_g3d_compare_config, (void *) &criteria); 164 size = MIN2(tmp_size, size); 165 for (i = 0; i < size; i++) 166 configs[i] = _eglGetConfigHandle(tmp_configs[i]); 167 } 168 169 FREE(tmp_configs); 170 171 *num_configs = size; 172 173 return EGL_TRUE; 174} 175 176static _EGLContext * 177egl_g3d_create_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, 178 _EGLContext *share, const EGLint *attribs) 179{ 180 struct egl_g3d_display *gdpy = egl_g3d_display(dpy); 181 struct egl_g3d_context *gshare = egl_g3d_context(share); 182 struct egl_g3d_config *gconf = egl_g3d_config(conf); 183 struct egl_g3d_context *gctx; 184 struct st_context_attribs stattribs; 185 186 gctx = CALLOC_STRUCT(egl_g3d_context); 187 if (!gctx) { 188 _eglError(EGL_BAD_ALLOC, "eglCreateContext"); 189 return NULL; 190 } 191 192 if (!_eglInitContext(&gctx->base, dpy, conf, attribs)) { 193 FREE(gctx); 194 return NULL; 195 } 196 197 memset(&stattribs, 0, sizeof(stattribs)); 198 if (gconf) 199 stattribs.visual = gconf->stvis; 200 201 gctx->stapi = egl_g3d_choose_st(drv, &gctx->base, &stattribs.profile); 202 if (!gctx->stapi) { 203 FREE(gctx); 204 return NULL; 205 } 206 207 gctx->stctxi = gctx->stapi->create_context(gctx->stapi, gdpy->smapi, 208 &stattribs, (gshare) ? gshare->stctxi : NULL); 209 if (!gctx->stctxi) { 210 FREE(gctx); 211 return NULL; 212 } 213 214 gctx->stctxi->st_manager_private = (void *) &gctx->base; 215 216 return &gctx->base; 217} 218 219/** 220 * Destroy a context. 221 */ 222static void 223destroy_context(_EGLDisplay *dpy, _EGLContext *ctx) 224{ 225 struct egl_g3d_context *gctx = egl_g3d_context(ctx); 226 227 /* FIXME a context might live longer than its display */ 228 if (!dpy->Initialized) 229 _eglLog(_EGL_FATAL, "destroy a context with an unitialized display"); 230 231 gctx->stctxi->destroy(gctx->stctxi); 232 233 FREE(gctx); 234} 235 236static EGLBoolean 237egl_g3d_destroy_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx) 238{ 239 if (_eglPutContext(ctx)) 240 destroy_context(dpy, ctx); 241 return EGL_TRUE; 242} 243 244struct egl_g3d_create_surface_arg { 245 EGLint type; 246 union { 247 EGLNativeWindowType win; 248 EGLNativePixmapType pix; 249 } u; 250}; 251 252static _EGLSurface * 253egl_g3d_create_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, 254 struct egl_g3d_create_surface_arg *arg, 255 const EGLint *attribs) 256{ 257 struct egl_g3d_display *gdpy = egl_g3d_display(dpy); 258 struct egl_g3d_config *gconf = egl_g3d_config(conf); 259 struct egl_g3d_surface *gsurf; 260 struct native_surface *nsurf; 261 const char *err; 262 263 switch (arg->type) { 264 case EGL_WINDOW_BIT: 265 err = "eglCreateWindowSurface"; 266 break; 267 case EGL_PIXMAP_BIT: 268 err = "eglCreatePixmapSurface"; 269 break; 270#ifdef EGL_MESA_screen_surface 271 case EGL_SCREEN_BIT_MESA: 272 err = "eglCreateScreenSurface"; 273 break; 274#endif 275 default: 276 err = "eglCreateUnknownSurface"; 277 break; 278 } 279 280 gsurf = CALLOC_STRUCT(egl_g3d_surface); 281 if (!gsurf) { 282 _eglError(EGL_BAD_ALLOC, err); 283 return NULL; 284 } 285 286 if (!_eglInitSurface(&gsurf->base, dpy, arg->type, conf, attribs)) { 287 FREE(gsurf); 288 return NULL; 289 } 290 291 /* create the native surface */ 292 switch (arg->type) { 293 case EGL_WINDOW_BIT: 294 nsurf = gdpy->native->create_window_surface(gdpy->native, 295 arg->u.win, gconf->native); 296 break; 297 case EGL_PIXMAP_BIT: 298 nsurf = gdpy->native->create_pixmap_surface(gdpy->native, 299 arg->u.pix, gconf->native); 300 break; 301#ifdef EGL_MESA_screen_surface 302 case EGL_SCREEN_BIT_MESA: 303 /* prefer back buffer (move to _eglInitSurface?) */ 304 gsurf->base.RenderBuffer = EGL_BACK_BUFFER; 305 nsurf = gdpy->native->modeset->create_scanout_surface(gdpy->native, 306 gconf->native, gsurf->base.Width, gsurf->base.Height); 307 break; 308#endif 309 default: 310 nsurf = NULL; 311 break; 312 } 313 314 if (!nsurf) { 315 FREE(gsurf); 316 return NULL; 317 } 318 /* initialize the geometry */ 319 if (!nsurf->validate(nsurf, 0x0, &gsurf->sequence_number, NULL, 320 &gsurf->base.Width, &gsurf->base.Height)) { 321 nsurf->destroy(nsurf); 322 FREE(gsurf); 323 return NULL; 324 } 325 326 gsurf->stvis = gconf->stvis; 327 if (gsurf->base.RenderBuffer == EGL_SINGLE_BUFFER && 328 gconf->stvis.buffer_mask & ST_ATTACHMENT_FRONT_LEFT_MASK) 329 gsurf->stvis.render_buffer = ST_ATTACHMENT_FRONT_LEFT; 330 331 gsurf->stfbi = egl_g3d_create_st_framebuffer(&gsurf->base); 332 if (!gsurf->stfbi) { 333 nsurf->destroy(nsurf); 334 FREE(gsurf); 335 return NULL; 336 } 337 338 nsurf->user_data = &gsurf->base; 339 gsurf->native = nsurf; 340 341 return &gsurf->base; 342} 343 344static _EGLSurface * 345egl_g3d_create_window_surface(_EGLDriver *drv, _EGLDisplay *dpy, 346 _EGLConfig *conf, EGLNativeWindowType win, 347 const EGLint *attribs) 348{ 349 struct egl_g3d_create_surface_arg arg; 350 351 memset(&arg, 0, sizeof(arg)); 352 arg.type = EGL_WINDOW_BIT; 353 arg.u.win = win; 354 355 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs); 356} 357 358static _EGLSurface * 359egl_g3d_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy, 360 _EGLConfig *conf, EGLNativePixmapType pix, 361 const EGLint *attribs) 362{ 363 struct egl_g3d_create_surface_arg arg; 364 365 memset(&arg, 0, sizeof(arg)); 366 arg.type = EGL_PIXMAP_BIT; 367 arg.u.pix = pix; 368 369 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs); 370} 371 372static struct egl_g3d_surface * 373create_pbuffer_surface(_EGLDisplay *dpy, _EGLConfig *conf, 374 const EGLint *attribs, const char *func) 375{ 376 struct egl_g3d_config *gconf = egl_g3d_config(conf); 377 struct egl_g3d_surface *gsurf; 378 379 gsurf = CALLOC_STRUCT(egl_g3d_surface); 380 if (!gsurf) { 381 _eglError(EGL_BAD_ALLOC, func); 382 return NULL; 383 } 384 385 if (!_eglInitSurface(&gsurf->base, dpy, EGL_PBUFFER_BIT, conf, attribs)) { 386 FREE(gsurf); 387 return NULL; 388 } 389 390 gsurf->stvis = gconf->stvis; 391 392 gsurf->stfbi = egl_g3d_create_st_framebuffer(&gsurf->base); 393 if (!gsurf->stfbi) { 394 FREE(gsurf); 395 return NULL; 396 } 397 398 return gsurf; 399} 400 401static _EGLSurface * 402egl_g3d_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy, 403 _EGLConfig *conf, const EGLint *attribs) 404{ 405 struct egl_g3d_surface *gsurf; 406 struct pipe_resource *ptex = NULL; 407 408 gsurf = create_pbuffer_surface(dpy, conf, attribs, 409 "eglCreatePbufferSurface"); 410 if (!gsurf) 411 return NULL; 412 413 gsurf->client_buffer_type = EGL_NONE; 414 415 if (!gsurf->stfbi->validate(gsurf->stfbi, 416 &gsurf->stvis.render_buffer, 1, &ptex)) { 417 egl_g3d_destroy_st_framebuffer(gsurf->stfbi); 418 FREE(gsurf); 419 return NULL; 420 } 421 422 return &gsurf->base; 423} 424 425static _EGLSurface * 426egl_g3d_create_pbuffer_from_client_buffer(_EGLDriver *drv, _EGLDisplay *dpy, 427 EGLenum buftype, 428 EGLClientBuffer buffer, 429 _EGLConfig *conf, 430 const EGLint *attribs) 431{ 432 struct egl_g3d_surface *gsurf; 433 struct pipe_resource *ptex = NULL; 434 EGLint pbuffer_attribs[32]; 435 EGLint count, i; 436 437 switch (buftype) { 438 case EGL_OPENVG_IMAGE: 439 break; 440 default: 441 _eglError(EGL_BAD_PARAMETER, "eglCreatePbufferFromClientBuffer"); 442 return NULL; 443 break; 444 } 445 446 /* parse the attributes first */ 447 count = 0; 448 for (i = 0; attribs && attribs[i] != EGL_NONE; i++) { 449 EGLint attr = attribs[i++]; 450 EGLint val = attribs[i]; 451 EGLint err = EGL_SUCCESS; 452 453 switch (attr) { 454 case EGL_TEXTURE_FORMAT: 455 case EGL_TEXTURE_TARGET: 456 case EGL_MIPMAP_TEXTURE: 457 pbuffer_attribs[count++] = attr; 458 pbuffer_attribs[count++] = val; 459 break; 460 default: 461 err = EGL_BAD_ATTRIBUTE; 462 break; 463 } 464 /* bail out */ 465 if (err != EGL_SUCCESS) { 466 _eglError(err, "eglCreatePbufferFromClientBuffer"); 467 return NULL; 468 } 469 } 470 471 pbuffer_attribs[count++] = EGL_NONE; 472 473 gsurf = create_pbuffer_surface(dpy, conf, pbuffer_attribs, 474 "eglCreatePbufferFromClientBuffer"); 475 if (!gsurf) 476 return NULL; 477 478 gsurf->client_buffer_type = buftype; 479 gsurf->client_buffer = buffer; 480 481 if (!gsurf->stfbi->validate(gsurf->stfbi, 482 &gsurf->stvis.render_buffer, 1, &ptex)) { 483 egl_g3d_destroy_st_framebuffer(gsurf->stfbi); 484 FREE(gsurf); 485 return NULL; 486 } 487 488 return &gsurf->base; 489} 490 491/** 492 * Destroy a surface. 493 */ 494static void 495destroy_surface(_EGLDisplay *dpy, _EGLSurface *surf) 496{ 497 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf); 498 499 /* FIXME a surface might live longer than its display */ 500 if (!dpy->Initialized) 501 _eglLog(_EGL_FATAL, "destroy a surface with an unitialized display"); 502 503 pipe_resource_reference(&gsurf->render_texture, NULL); 504 egl_g3d_destroy_st_framebuffer(gsurf->stfbi); 505 if (gsurf->native) 506 gsurf->native->destroy(gsurf->native); 507 FREE(gsurf); 508} 509 510static EGLBoolean 511egl_g3d_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf) 512{ 513 if (_eglPutSurface(surf)) 514 destroy_surface(dpy, surf); 515 return EGL_TRUE; 516} 517 518static EGLBoolean 519egl_g3d_make_current(_EGLDriver *drv, _EGLDisplay *dpy, 520 _EGLSurface *draw, _EGLSurface *read, _EGLContext *ctx) 521{ 522 struct egl_g3d_context *gctx = egl_g3d_context(ctx); 523 struct egl_g3d_surface *gdraw = egl_g3d_surface(draw); 524 struct egl_g3d_surface *gread = egl_g3d_surface(read); 525 struct egl_g3d_context *old_gctx; 526 _EGLContext *old_ctx; 527 _EGLSurface *old_draw, *old_read; 528 EGLBoolean ok = EGL_TRUE; 529 530 /* make new bindings */ 531 if (!_eglBindContext(ctx, draw, read, &old_ctx, &old_draw, &old_read)) 532 return EGL_FALSE; 533 534 old_gctx = egl_g3d_context(old_ctx); 535 if (old_gctx) { 536 /* flush old context */ 537 old_gctx->stctxi->flush(old_gctx->stctxi, 538 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL); 539 } 540 541 if (gctx) { 542 ok = gctx->stapi->make_current(gctx->stapi, gctx->stctxi, 543 (gdraw) ? gdraw->stfbi : NULL, (gread) ? gread->stfbi : NULL); 544 if (ok) { 545 if (gdraw) { 546 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi, 547 gdraw->stfbi); 548 549 if (gdraw->base.Type == EGL_WINDOW_BIT) { 550 gctx->base.WindowRenderBuffer = 551 (gdraw->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT) ? 552 EGL_SINGLE_BUFFER : EGL_BACK_BUFFER; 553 } 554 } 555 if (gread && gread != gdraw) { 556 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi, 557 gread->stfbi); 558 } 559 } 560 } 561 else if (old_gctx) { 562 ok = old_gctx->stapi->make_current(old_gctx->stapi, NULL, NULL, NULL); 563 if (ok) 564 old_gctx->base.WindowRenderBuffer = EGL_NONE; 565 } 566 567 if (ok) { 568 if (_eglPutContext(old_ctx)) 569 destroy_context(dpy, old_ctx); 570 if (_eglPutSurface(old_draw)) 571 destroy_surface(dpy, old_draw); 572 if (_eglPutSurface(old_read)) 573 destroy_surface(dpy, old_read); 574 } 575 else { 576 /* undo the previous _eglBindContext */ 577 _eglBindContext(old_ctx, old_draw, old_read, &ctx, &draw, &read); 578 assert(&gctx->base == ctx && 579 &gdraw->base == draw && 580 &gread->base == read); 581 582 _eglPutSurface(draw); 583 _eglPutSurface(read); 584 _eglPutContext(ctx); 585 586 _eglPutSurface(old_draw); 587 _eglPutSurface(old_read); 588 _eglPutContext(old_ctx); 589 } 590 591 return ok; 592} 593 594static EGLBoolean 595egl_g3d_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf) 596{ 597 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf); 598 _EGLContext *ctx = _eglGetCurrentContext(); 599 struct egl_g3d_context *gctx = NULL; 600 601 /* no-op for pixmap or pbuffer surface */ 602 if (gsurf->base.Type == EGL_PIXMAP_BIT || 603 gsurf->base.Type == EGL_PBUFFER_BIT) 604 return EGL_TRUE; 605 606 /* or when the surface is single-buffered */ 607 if (gsurf->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT) 608 return EGL_TRUE; 609 610 if (ctx && ctx->DrawSurface == surf) 611 gctx = egl_g3d_context(ctx); 612 613 /* flush if the surface is current */ 614 if (gctx) { 615 gctx->stctxi->flush(gctx->stctxi, 616 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL); 617 } 618 619 return gsurf->native->present(gsurf->native, 620 NATIVE_ATTACHMENT_BACK_LEFT, 621 gsurf->base.SwapBehavior == EGL_BUFFER_PRESERVED, 622 gsurf->base.SwapInterval); 623} 624 625/** 626 * Get the pipe surface of the given attachment of the native surface. 627 */ 628static struct pipe_resource * 629get_pipe_resource(struct native_display *ndpy, struct native_surface *nsurf, 630 enum native_attachment natt) 631{ 632 struct pipe_resource *textures[NUM_NATIVE_ATTACHMENTS]; 633 634 textures[natt] = NULL; 635 nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL); 636 637 return textures[natt]; 638} 639 640static EGLBoolean 641egl_g3d_copy_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf, 642 EGLNativePixmapType target) 643{ 644 struct egl_g3d_display *gdpy = egl_g3d_display(dpy); 645 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf); 646 _EGLContext *ctx = _eglGetCurrentContext(); 647 struct native_surface *nsurf; 648 struct pipe_resource *ptex; 649 650 if (!gsurf->render_texture) 651 return EGL_TRUE; 652 653 nsurf = gdpy->native->create_pixmap_surface(gdpy->native, target, NULL); 654 if (!nsurf) 655 return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers"); 656 657 /* flush if the surface is current */ 658 if (ctx && ctx->DrawSurface == &gsurf->base) { 659 struct egl_g3d_context *gctx = egl_g3d_context(ctx); 660 gctx->stctxi->flush(gctx->stctxi, 661 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL); 662 } 663 664 /* create a pipe context to copy surfaces */ 665 if (!gdpy->pipe) { 666 gdpy->pipe = 667 gdpy->native->screen->context_create(gdpy->native->screen, NULL); 668 if (!gdpy->pipe) 669 return EGL_FALSE; 670 } 671 672 ptex = get_pipe_resource(gdpy->native, nsurf, NATIVE_ATTACHMENT_FRONT_LEFT); 673 if (ptex) { 674 struct pipe_resource *psrc = gsurf->render_texture; 675 struct pipe_box src_box; 676 u_box_origin_2d(ptex->width0, ptex->height0, &src_box); 677 if (psrc) { 678 gdpy->pipe->resource_copy_region(gdpy->pipe, ptex, 0, 0, 0, 0, 679 gsurf->render_texture, 0, &src_box); 680 nsurf->present(nsurf, NATIVE_ATTACHMENT_FRONT_LEFT, FALSE, 0); 681 } 682 683 pipe_resource_reference(&ptex, NULL); 684 } 685 686 nsurf->destroy(nsurf); 687 688 return EGL_TRUE; 689} 690 691static EGLBoolean 692egl_g3d_wait_client(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx) 693{ 694 struct egl_g3d_display *gdpy = egl_g3d_display(dpy); 695 struct egl_g3d_context *gctx = egl_g3d_context(ctx); 696 struct pipe_screen *screen = gdpy->native->screen; 697 struct pipe_fence_handle *fence = NULL; 698 699 gctx->stctxi->flush(gctx->stctxi, 700 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, &fence); 701 if (fence) { 702 screen->fence_finish(screen, fence, 0); 703 screen->fence_reference(screen, &fence, NULL); 704 } 705 706 return EGL_TRUE; 707} 708 709static EGLBoolean 710egl_g3d_wait_native(_EGLDriver *drv, _EGLDisplay *dpy, EGLint engine) 711{ 712 _EGLContext *ctx = _eglGetCurrentContext(); 713 714 if (engine != EGL_CORE_NATIVE_ENGINE) 715 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative"); 716 717 if (ctx && ctx->DrawSurface) { 718 struct egl_g3d_surface *gsurf = egl_g3d_surface(ctx->DrawSurface); 719 720 if (gsurf->native) 721 gsurf->native->wait(gsurf->native); 722 } 723 724 return EGL_TRUE; 725} 726 727static EGLBoolean 728egl_g3d_bind_tex_image(_EGLDriver *drv, _EGLDisplay *dpy, 729 _EGLSurface *surf, EGLint buffer) 730{ 731 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf); 732 _EGLContext *es1 = _eglGetAPIContext(EGL_OPENGL_ES_API); 733 struct egl_g3d_context *gctx; 734 enum pipe_format internal_format; 735 enum st_texture_type target; 736 737 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT) 738 return _eglError(EGL_BAD_SURFACE, "eglBindTexImage"); 739 if (buffer != EGL_BACK_BUFFER) 740 return _eglError(EGL_BAD_PARAMETER, "eglBindTexImage"); 741 if (gsurf->base.BoundToTexture) 742 return _eglError(EGL_BAD_ACCESS, "eglBindTexImage"); 743 744 switch (gsurf->base.TextureFormat) { 745 case EGL_TEXTURE_RGB: 746 internal_format = PIPE_FORMAT_R8G8B8_UNORM; 747 break; 748 case EGL_TEXTURE_RGBA: 749 internal_format = PIPE_FORMAT_B8G8R8A8_UNORM; 750 break; 751 default: 752 return _eglError(EGL_BAD_MATCH, "eglBindTexImage"); 753 } 754 755 switch (gsurf->base.TextureTarget) { 756 case EGL_TEXTURE_2D: 757 target = ST_TEXTURE_2D; 758 break; 759 default: 760 return _eglError(EGL_BAD_MATCH, "eglBindTexImage"); 761 } 762 763 if (!es1) 764 return EGL_TRUE; 765 if (!gsurf->render_texture) 766 return EGL_FALSE; 767 768 /* flush properly if the surface is bound */ 769 if (gsurf->base.CurrentContext) { 770 gctx = egl_g3d_context(gsurf->base.CurrentContext); 771 gctx->stctxi->flush(gctx->stctxi, 772 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL); 773 } 774 775 gctx = egl_g3d_context(es1); 776 if (gctx->stctxi->teximage) { 777 if (!gctx->stctxi->teximage(gctx->stctxi, target, 778 gsurf->base.MipmapLevel, internal_format, 779 gsurf->render_texture, gsurf->base.MipmapTexture)) 780 return EGL_FALSE; 781 gsurf->base.BoundToTexture = EGL_TRUE; 782 } 783 784 return EGL_TRUE; 785} 786 787static EGLBoolean 788egl_g3d_release_tex_image(_EGLDriver *drv, _EGLDisplay *dpy, 789 _EGLSurface *surf, EGLint buffer) 790{ 791 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf); 792 793 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT || 794 !gsurf->base.BoundToTexture) 795 return _eglError(EGL_BAD_SURFACE, "eglReleaseTexImage"); 796 if (buffer != EGL_BACK_BUFFER) 797 return _eglError(EGL_BAD_PARAMETER, "eglReleaseTexImage"); 798 799 if (gsurf->render_texture) { 800 _EGLContext *ctx = _eglGetAPIContext(EGL_OPENGL_ES_API); 801 struct egl_g3d_context *gctx = egl_g3d_context(ctx); 802 803 /* what if the context the surface binds to is no longer current? */ 804 if (gctx) { 805 gctx->stctxi->teximage(gctx->stctxi, ST_TEXTURE_2D, 806 gsurf->base.MipmapLevel, PIPE_FORMAT_NONE, NULL, FALSE); 807 } 808 } 809 810 gsurf->base.BoundToTexture = EGL_FALSE; 811 812 return EGL_TRUE; 813} 814 815#ifdef EGL_MESA_screen_surface 816 817static _EGLSurface * 818egl_g3d_create_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy, 819 _EGLConfig *conf, const EGLint *attribs) 820{ 821 struct egl_g3d_create_surface_arg arg; 822 823 memset(&arg, 0, sizeof(arg)); 824 arg.type = EGL_SCREEN_BIT_MESA; 825 826 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs); 827} 828 829static EGLBoolean 830egl_g3d_show_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy, 831 _EGLScreen *scr, _EGLSurface *surf, 832 _EGLMode *mode) 833{ 834 struct egl_g3d_display *gdpy = egl_g3d_display(dpy); 835 struct egl_g3d_screen *gscr = egl_g3d_screen(scr); 836 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf); 837 struct native_surface *nsurf; 838 const struct native_mode *nmode; 839 EGLBoolean changed; 840 841 if (gsurf) { 842 EGLint idx; 843 844 if (!mode) 845 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA"); 846 if (gsurf->base.Type != EGL_SCREEN_BIT_MESA) 847 return _eglError(EGL_BAD_SURFACE, "eglShowScreenSurfaceMESA"); 848 if (gsurf->base.Width < mode->Width || gsurf->base.Height < mode->Height) 849 return _eglError(EGL_BAD_MATCH, 850 "eglShowSurfaceMESA(surface smaller than mode size)"); 851 852 /* find the index of the mode */ 853 for (idx = 0; idx < gscr->base.NumModes; idx++) 854 if (mode == &gscr->base.Modes[idx]) 855 break; 856 if (idx >= gscr->base.NumModes) { 857 return _eglError(EGL_BAD_MODE_MESA, 858 "eglShowSurfaceMESA(unknown mode)"); 859 } 860 861 nsurf = gsurf->native; 862 nmode = gscr->native_modes[idx]; 863 } 864 else { 865 if (mode) 866 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA"); 867 868 /* disable the screen */ 869 nsurf = NULL; 870 nmode = NULL; 871 } 872 873 /* TODO surface panning by CRTC choosing */ 874 changed = gdpy->native->modeset->program(gdpy->native, 0, nsurf, 875 gscr->base.OriginX, gscr->base.OriginY, &gscr->native, 1, nmode); 876 if (changed) { 877 gscr->base.CurrentSurface = &gsurf->base; 878 gscr->base.CurrentMode = mode; 879 } 880 881 return changed; 882} 883 884#endif /* EGL_MESA_screen_surface */ 885 886void 887egl_g3d_init_driver_api(_EGLDriver *drv) 888{ 889 _eglInitDriverFallbacks(drv); 890 891 drv->API.ChooseConfig = egl_g3d_choose_config; 892 893 drv->API.CreateContext = egl_g3d_create_context; 894 drv->API.DestroyContext = egl_g3d_destroy_context; 895 drv->API.CreateWindowSurface = egl_g3d_create_window_surface; 896 drv->API.CreatePixmapSurface = egl_g3d_create_pixmap_surface; 897 drv->API.CreatePbufferSurface = egl_g3d_create_pbuffer_surface; 898 drv->API.CreatePbufferFromClientBuffer = egl_g3d_create_pbuffer_from_client_buffer; 899 drv->API.DestroySurface = egl_g3d_destroy_surface; 900 drv->API.MakeCurrent = egl_g3d_make_current; 901 drv->API.SwapBuffers = egl_g3d_swap_buffers; 902 drv->API.CopyBuffers = egl_g3d_copy_buffers; 903 drv->API.WaitClient = egl_g3d_wait_client; 904 drv->API.WaitNative = egl_g3d_wait_native; 905 906 drv->API.BindTexImage = egl_g3d_bind_tex_image; 907 drv->API.ReleaseTexImage = egl_g3d_release_tex_image; 908 909 drv->API.CreateImageKHR = egl_g3d_create_image; 910 drv->API.DestroyImageKHR = egl_g3d_destroy_image; 911#ifdef EGL_MESA_drm_image 912 drv->API.CreateDRMImageMESA = egl_g3d_create_drm_image; 913 drv->API.ExportDRMImageMESA = egl_g3d_export_drm_image; 914#endif 915 916#ifdef EGL_KHR_reusable_sync 917 drv->API.CreateSyncKHR = egl_g3d_create_sync; 918 drv->API.DestroySyncKHR = egl_g3d_destroy_sync; 919 drv->API.ClientWaitSyncKHR = egl_g3d_client_wait_sync; 920 drv->API.SignalSyncKHR = egl_g3d_signal_sync; 921#endif 922 923#ifdef EGL_MESA_screen_surface 924 drv->API.CreateScreenSurfaceMESA = egl_g3d_create_screen_surface; 925 drv->API.ShowScreenSurfaceMESA = egl_g3d_show_screen_surface; 926#endif 927} 928