egl_g3d_api.c revision d1e4117355f1db6ff87c837df6212bf6168e98a0
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 (configs && tmp_size) { 162 _eglSortConfigs((const _EGLConfig **) tmp_configs, tmp_size, 163 egl_g3d_compare_config, (void *) &criteria); 164 tmp_size = MIN2(tmp_size, size); 165 for (i = 0; i < tmp_size; i++) 166 configs[i] = _eglGetConfigHandle(tmp_configs[i]); 167 } 168 169 FREE(tmp_configs); 170 171 *num_configs = tmp_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 407 gsurf = create_pbuffer_surface(dpy, conf, attribs, 408 "eglCreatePbufferSurface"); 409 if (!gsurf) 410 return NULL; 411 412 gsurf->client_buffer_type = EGL_NONE; 413 414 return &gsurf->base; 415} 416 417static _EGLSurface * 418egl_g3d_create_pbuffer_from_client_buffer(_EGLDriver *drv, _EGLDisplay *dpy, 419 EGLenum buftype, 420 EGLClientBuffer buffer, 421 _EGLConfig *conf, 422 const EGLint *attribs) 423{ 424 struct egl_g3d_surface *gsurf; 425 struct pipe_resource *ptex = NULL; 426 EGLint pbuffer_attribs[32]; 427 EGLint count, i; 428 429 switch (buftype) { 430 case EGL_OPENVG_IMAGE: 431 break; 432 default: 433 _eglError(EGL_BAD_PARAMETER, "eglCreatePbufferFromClientBuffer"); 434 return NULL; 435 break; 436 } 437 438 /* parse the attributes first */ 439 count = 0; 440 for (i = 0; attribs && attribs[i] != EGL_NONE; i++) { 441 EGLint attr = attribs[i++]; 442 EGLint val = attribs[i]; 443 EGLint err = EGL_SUCCESS; 444 445 switch (attr) { 446 case EGL_TEXTURE_FORMAT: 447 case EGL_TEXTURE_TARGET: 448 case EGL_MIPMAP_TEXTURE: 449 pbuffer_attribs[count++] = attr; 450 pbuffer_attribs[count++] = val; 451 break; 452 default: 453 err = EGL_BAD_ATTRIBUTE; 454 break; 455 } 456 /* bail out */ 457 if (err != EGL_SUCCESS) { 458 _eglError(err, "eglCreatePbufferFromClientBuffer"); 459 return NULL; 460 } 461 } 462 463 pbuffer_attribs[count++] = EGL_NONE; 464 465 gsurf = create_pbuffer_surface(dpy, conf, pbuffer_attribs, 466 "eglCreatePbufferFromClientBuffer"); 467 if (!gsurf) 468 return NULL; 469 470 gsurf->client_buffer_type = buftype; 471 gsurf->client_buffer = buffer; 472 473 /* validate now so that it fails if the client buffer is invalid */ 474 if (!gsurf->stfbi->validate(gsurf->stfbi, 475 &gsurf->stvis.render_buffer, 1, &ptex)) { 476 egl_g3d_destroy_st_framebuffer(gsurf->stfbi); 477 FREE(gsurf); 478 return NULL; 479 } 480 pipe_resource_reference(&ptex, NULL); 481 482 return &gsurf->base; 483} 484 485/** 486 * Destroy a surface. 487 */ 488static void 489destroy_surface(_EGLDisplay *dpy, _EGLSurface *surf) 490{ 491 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf); 492 493 /* FIXME a surface might live longer than its display */ 494 if (!dpy->Initialized) 495 _eglLog(_EGL_FATAL, "destroy a surface with an unitialized display"); 496 497 pipe_resource_reference(&gsurf->render_texture, NULL); 498 egl_g3d_destroy_st_framebuffer(gsurf->stfbi); 499 if (gsurf->native) 500 gsurf->native->destroy(gsurf->native); 501 FREE(gsurf); 502} 503 504static EGLBoolean 505egl_g3d_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf) 506{ 507 if (_eglPutSurface(surf)) 508 destroy_surface(dpy, surf); 509 return EGL_TRUE; 510} 511 512static EGLBoolean 513egl_g3d_make_current(_EGLDriver *drv, _EGLDisplay *dpy, 514 _EGLSurface *draw, _EGLSurface *read, _EGLContext *ctx) 515{ 516 struct egl_g3d_context *gctx = egl_g3d_context(ctx); 517 struct egl_g3d_surface *gdraw = egl_g3d_surface(draw); 518 struct egl_g3d_surface *gread = egl_g3d_surface(read); 519 struct egl_g3d_context *old_gctx; 520 _EGLContext *old_ctx; 521 _EGLSurface *old_draw, *old_read; 522 EGLBoolean ok = EGL_TRUE; 523 524 /* make new bindings */ 525 if (!_eglBindContext(ctx, draw, read, &old_ctx, &old_draw, &old_read)) 526 return EGL_FALSE; 527 528 old_gctx = egl_g3d_context(old_ctx); 529 if (old_gctx) { 530 /* flush old context */ 531 old_gctx->stctxi->flush(old_gctx->stctxi, 532 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL); 533 } 534 535 if (gctx) { 536 ok = gctx->stapi->make_current(gctx->stapi, gctx->stctxi, 537 (gdraw) ? gdraw->stfbi : NULL, (gread) ? gread->stfbi : NULL); 538 if (ok) { 539 if (gdraw) { 540 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi, 541 gdraw->stfbi); 542 543 if (gdraw->base.Type == EGL_WINDOW_BIT) { 544 gctx->base.WindowRenderBuffer = 545 (gdraw->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT) ? 546 EGL_SINGLE_BUFFER : EGL_BACK_BUFFER; 547 } 548 } 549 if (gread && gread != gdraw) { 550 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi, 551 gread->stfbi); 552 } 553 } 554 } 555 else if (old_gctx) { 556 ok = old_gctx->stapi->make_current(old_gctx->stapi, NULL, NULL, NULL); 557 if (ok) 558 old_gctx->base.WindowRenderBuffer = EGL_NONE; 559 } 560 561 if (ok) { 562 if (_eglPutContext(old_ctx)) 563 destroy_context(dpy, old_ctx); 564 if (_eglPutSurface(old_draw)) 565 destroy_surface(dpy, old_draw); 566 if (_eglPutSurface(old_read)) 567 destroy_surface(dpy, old_read); 568 } 569 else { 570 /* undo the previous _eglBindContext */ 571 _eglBindContext(old_ctx, old_draw, old_read, &ctx, &draw, &read); 572 assert(&gctx->base == ctx && 573 &gdraw->base == draw && 574 &gread->base == read); 575 576 _eglPutSurface(draw); 577 _eglPutSurface(read); 578 _eglPutContext(ctx); 579 580 _eglPutSurface(old_draw); 581 _eglPutSurface(old_read); 582 _eglPutContext(old_ctx); 583 } 584 585 return ok; 586} 587 588static EGLBoolean 589egl_g3d_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf) 590{ 591 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf); 592 _EGLContext *ctx = _eglGetCurrentContext(); 593 struct egl_g3d_context *gctx = NULL; 594 595 /* no-op for pixmap or pbuffer surface */ 596 if (gsurf->base.Type == EGL_PIXMAP_BIT || 597 gsurf->base.Type == EGL_PBUFFER_BIT) 598 return EGL_TRUE; 599 600 /* or when the surface is single-buffered */ 601 if (gsurf->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT) 602 return EGL_TRUE; 603 604 if (ctx && ctx->DrawSurface == surf) 605 gctx = egl_g3d_context(ctx); 606 607 /* flush if the surface is current */ 608 if (gctx) { 609 gctx->stctxi->flush(gctx->stctxi, 610 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL); 611 } 612 613 return gsurf->native->present(gsurf->native, 614 NATIVE_ATTACHMENT_BACK_LEFT, 615 gsurf->base.SwapBehavior == EGL_BUFFER_PRESERVED, 616 gsurf->base.SwapInterval); 617} 618 619/** 620 * Get the pipe surface of the given attachment of the native surface. 621 */ 622static struct pipe_resource * 623get_pipe_resource(struct native_display *ndpy, struct native_surface *nsurf, 624 enum native_attachment natt) 625{ 626 struct pipe_resource *textures[NUM_NATIVE_ATTACHMENTS]; 627 628 textures[natt] = NULL; 629 nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL); 630 631 return textures[natt]; 632} 633 634static EGLBoolean 635egl_g3d_copy_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf, 636 EGLNativePixmapType target) 637{ 638 struct egl_g3d_display *gdpy = egl_g3d_display(dpy); 639 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf); 640 _EGLContext *ctx = _eglGetCurrentContext(); 641 struct native_surface *nsurf; 642 struct pipe_resource *ptex; 643 struct pipe_context *pipe; 644 645 if (!gsurf->render_texture) 646 return EGL_TRUE; 647 648 nsurf = gdpy->native->create_pixmap_surface(gdpy->native, target, NULL); 649 if (!nsurf) 650 return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers"); 651 652 /* flush if the surface is current */ 653 if (ctx && ctx->DrawSurface == &gsurf->base) { 654 struct egl_g3d_context *gctx = egl_g3d_context(ctx); 655 gctx->stctxi->flush(gctx->stctxi, 656 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL); 657 } 658 659 pipe = ndpy_get_copy_context(gdpy->native); 660 if (!pipe) 661 return EGL_FALSE; 662 663 ptex = get_pipe_resource(gdpy->native, nsurf, NATIVE_ATTACHMENT_FRONT_LEFT); 664 if (ptex) { 665 struct pipe_box src_box; 666 667 u_box_origin_2d(ptex->width0, ptex->height0, &src_box); 668 pipe->resource_copy_region(pipe, ptex, 0, 0, 0, 0, 669 gsurf->render_texture, 0, &src_box); 670 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL); 671 nsurf->present(nsurf, NATIVE_ATTACHMENT_FRONT_LEFT, FALSE, 0); 672 673 pipe_resource_reference(&ptex, NULL); 674 } 675 676 nsurf->destroy(nsurf); 677 678 return EGL_TRUE; 679} 680 681static EGLBoolean 682egl_g3d_wait_client(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx) 683{ 684 struct egl_g3d_display *gdpy = egl_g3d_display(dpy); 685 struct egl_g3d_context *gctx = egl_g3d_context(ctx); 686 struct pipe_screen *screen = gdpy->native->screen; 687 struct pipe_fence_handle *fence = NULL; 688 689 gctx->stctxi->flush(gctx->stctxi, 690 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, &fence); 691 if (fence) { 692 screen->fence_finish(screen, fence, 0); 693 screen->fence_reference(screen, &fence, NULL); 694 } 695 696 return EGL_TRUE; 697} 698 699static EGLBoolean 700egl_g3d_wait_native(_EGLDriver *drv, _EGLDisplay *dpy, EGLint engine) 701{ 702 _EGLContext *ctx = _eglGetCurrentContext(); 703 704 if (engine != EGL_CORE_NATIVE_ENGINE) 705 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative"); 706 707 if (ctx && ctx->DrawSurface) { 708 struct egl_g3d_surface *gsurf = egl_g3d_surface(ctx->DrawSurface); 709 710 if (gsurf->native) 711 gsurf->native->wait(gsurf->native); 712 } 713 714 return EGL_TRUE; 715} 716 717static EGLBoolean 718egl_g3d_bind_tex_image(_EGLDriver *drv, _EGLDisplay *dpy, 719 _EGLSurface *surf, EGLint buffer) 720{ 721 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf); 722 _EGLContext *es1 = _eglGetAPIContext(EGL_OPENGL_ES_API); 723 struct egl_g3d_context *gctx; 724 enum pipe_format internal_format; 725 enum st_texture_type target; 726 727 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT) 728 return _eglError(EGL_BAD_SURFACE, "eglBindTexImage"); 729 if (buffer != EGL_BACK_BUFFER) 730 return _eglError(EGL_BAD_PARAMETER, "eglBindTexImage"); 731 if (gsurf->base.BoundToTexture) 732 return _eglError(EGL_BAD_ACCESS, "eglBindTexImage"); 733 734 switch (gsurf->base.TextureFormat) { 735 case EGL_TEXTURE_RGB: 736 internal_format = PIPE_FORMAT_R8G8B8_UNORM; 737 break; 738 case EGL_TEXTURE_RGBA: 739 internal_format = PIPE_FORMAT_B8G8R8A8_UNORM; 740 break; 741 default: 742 return _eglError(EGL_BAD_MATCH, "eglBindTexImage"); 743 } 744 745 switch (gsurf->base.TextureTarget) { 746 case EGL_TEXTURE_2D: 747 target = ST_TEXTURE_2D; 748 break; 749 default: 750 return _eglError(EGL_BAD_MATCH, "eglBindTexImage"); 751 } 752 753 if (!es1) 754 return EGL_TRUE; 755 if (!gsurf->render_texture) 756 return EGL_FALSE; 757 758 /* flush properly if the surface is bound */ 759 if (gsurf->base.CurrentContext) { 760 gctx = egl_g3d_context(gsurf->base.CurrentContext); 761 gctx->stctxi->flush(gctx->stctxi, 762 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL); 763 } 764 765 gctx = egl_g3d_context(es1); 766 if (gctx->stctxi->teximage) { 767 if (!gctx->stctxi->teximage(gctx->stctxi, target, 768 gsurf->base.MipmapLevel, internal_format, 769 gsurf->render_texture, gsurf->base.MipmapTexture)) 770 return EGL_FALSE; 771 gsurf->base.BoundToTexture = EGL_TRUE; 772 } 773 774 return EGL_TRUE; 775} 776 777static EGLBoolean 778egl_g3d_release_tex_image(_EGLDriver *drv, _EGLDisplay *dpy, 779 _EGLSurface *surf, EGLint buffer) 780{ 781 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf); 782 783 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT || 784 !gsurf->base.BoundToTexture) 785 return _eglError(EGL_BAD_SURFACE, "eglReleaseTexImage"); 786 if (buffer != EGL_BACK_BUFFER) 787 return _eglError(EGL_BAD_PARAMETER, "eglReleaseTexImage"); 788 789 if (gsurf->render_texture) { 790 _EGLContext *ctx = _eglGetAPIContext(EGL_OPENGL_ES_API); 791 struct egl_g3d_context *gctx = egl_g3d_context(ctx); 792 793 /* what if the context the surface binds to is no longer current? */ 794 if (gctx) { 795 gctx->stctxi->teximage(gctx->stctxi, ST_TEXTURE_2D, 796 gsurf->base.MipmapLevel, PIPE_FORMAT_NONE, NULL, FALSE); 797 } 798 } 799 800 gsurf->base.BoundToTexture = EGL_FALSE; 801 802 return EGL_TRUE; 803} 804 805#ifdef EGL_MESA_screen_surface 806 807static _EGLSurface * 808egl_g3d_create_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy, 809 _EGLConfig *conf, const EGLint *attribs) 810{ 811 struct egl_g3d_create_surface_arg arg; 812 813 memset(&arg, 0, sizeof(arg)); 814 arg.type = EGL_SCREEN_BIT_MESA; 815 816 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs); 817} 818 819static EGLBoolean 820egl_g3d_show_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy, 821 _EGLScreen *scr, _EGLSurface *surf, 822 _EGLMode *mode) 823{ 824 struct egl_g3d_display *gdpy = egl_g3d_display(dpy); 825 struct egl_g3d_screen *gscr = egl_g3d_screen(scr); 826 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf); 827 struct native_surface *nsurf; 828 const struct native_mode *nmode; 829 EGLBoolean changed; 830 831 if (gsurf) { 832 EGLint idx; 833 834 if (!mode) 835 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA"); 836 if (gsurf->base.Type != EGL_SCREEN_BIT_MESA) 837 return _eglError(EGL_BAD_SURFACE, "eglShowScreenSurfaceMESA"); 838 if (gsurf->base.Width < mode->Width || gsurf->base.Height < mode->Height) 839 return _eglError(EGL_BAD_MATCH, 840 "eglShowSurfaceMESA(surface smaller than mode size)"); 841 842 /* find the index of the mode */ 843 for (idx = 0; idx < gscr->base.NumModes; idx++) 844 if (mode == &gscr->base.Modes[idx]) 845 break; 846 if (idx >= gscr->base.NumModes) { 847 return _eglError(EGL_BAD_MODE_MESA, 848 "eglShowSurfaceMESA(unknown mode)"); 849 } 850 851 nsurf = gsurf->native; 852 nmode = gscr->native_modes[idx]; 853 } 854 else { 855 if (mode) 856 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA"); 857 858 /* disable the screen */ 859 nsurf = NULL; 860 nmode = NULL; 861 } 862 863 /* TODO surface panning by CRTC choosing */ 864 changed = gdpy->native->modeset->program(gdpy->native, 0, nsurf, 865 gscr->base.OriginX, gscr->base.OriginY, &gscr->native, 1, nmode); 866 if (changed) { 867 gscr->base.CurrentSurface = &gsurf->base; 868 gscr->base.CurrentMode = mode; 869 } 870 871 return changed; 872} 873 874#endif /* EGL_MESA_screen_surface */ 875 876void 877egl_g3d_init_driver_api(_EGLDriver *drv) 878{ 879 _eglInitDriverFallbacks(drv); 880 881 drv->API.ChooseConfig = egl_g3d_choose_config; 882 883 drv->API.CreateContext = egl_g3d_create_context; 884 drv->API.DestroyContext = egl_g3d_destroy_context; 885 drv->API.CreateWindowSurface = egl_g3d_create_window_surface; 886 drv->API.CreatePixmapSurface = egl_g3d_create_pixmap_surface; 887 drv->API.CreatePbufferSurface = egl_g3d_create_pbuffer_surface; 888 drv->API.CreatePbufferFromClientBuffer = egl_g3d_create_pbuffer_from_client_buffer; 889 drv->API.DestroySurface = egl_g3d_destroy_surface; 890 drv->API.MakeCurrent = egl_g3d_make_current; 891 drv->API.SwapBuffers = egl_g3d_swap_buffers; 892 drv->API.CopyBuffers = egl_g3d_copy_buffers; 893 drv->API.WaitClient = egl_g3d_wait_client; 894 drv->API.WaitNative = egl_g3d_wait_native; 895 896 drv->API.BindTexImage = egl_g3d_bind_tex_image; 897 drv->API.ReleaseTexImage = egl_g3d_release_tex_image; 898 899 drv->API.CreateImageKHR = egl_g3d_create_image; 900 drv->API.DestroyImageKHR = egl_g3d_destroy_image; 901#ifdef EGL_MESA_drm_image 902 drv->API.CreateDRMImageMESA = egl_g3d_create_drm_image; 903 drv->API.ExportDRMImageMESA = egl_g3d_export_drm_image; 904#endif 905 906#ifdef EGL_KHR_reusable_sync 907 drv->API.CreateSyncKHR = egl_g3d_create_sync; 908 drv->API.DestroySyncKHR = egl_g3d_destroy_sync; 909 drv->API.ClientWaitSyncKHR = egl_g3d_client_wait_sync; 910 drv->API.SignalSyncKHR = egl_g3d_signal_sync; 911#endif 912 913#ifdef EGL_MESA_screen_surface 914 drv->API.CreateScreenSurfaceMESA = egl_g3d_create_screen_surface; 915 drv->API.ShowScreenSurfaceMESA = egl_g3d_show_screen_surface; 916#endif 917} 918