stw_context.c revision 14e2dc9c66b332b2527201e65a4dbe2ded968669
1/************************************************************************** 2 * 3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28#include <windows.h> 29 30#include "pipe/p_compiler.h" 31#include "pipe/p_context.h" 32#include "pipe/p_state.h" 33#include "util/u_memory.h" 34#include "state_tracker/st_api.h" 35 36#include "stw_icd.h" 37#include "stw_device.h" 38#include "stw_winsys.h" 39#include "stw_framebuffer.h" 40#include "stw_pixelformat.h" 41#include "stw_context.h" 42#include "stw_tls.h" 43 44 45static INLINE struct stw_context * 46stw_current_context(void) 47{ 48 struct st_context_iface *st; 49 50 st = (stw_dev) ? stw_dev->stapi->get_current(stw_dev->stapi) : NULL; 51 52 return (struct stw_context *) ((st) ? st->st_manager_private : NULL); 53} 54 55BOOL APIENTRY 56DrvCopyContext( 57 DHGLRC dhrcSource, 58 DHGLRC dhrcDest, 59 UINT fuMask ) 60{ 61 struct stw_context *src; 62 struct stw_context *dst; 63 BOOL ret = FALSE; 64 65 if (!stw_dev) 66 return FALSE; 67 68 pipe_mutex_lock( stw_dev->ctx_mutex ); 69 70 src = stw_lookup_context_locked( dhrcSource ); 71 dst = stw_lookup_context_locked( dhrcDest ); 72 73 if (src && dst) { 74 /* FIXME */ 75 assert(0); 76 (void) src; 77 (void) dst; 78 (void) fuMask; 79 } 80 81 pipe_mutex_unlock( stw_dev->ctx_mutex ); 82 83 return ret; 84} 85 86BOOL APIENTRY 87DrvShareLists( 88 DHGLRC dhglrc1, 89 DHGLRC dhglrc2 ) 90{ 91 struct stw_context *ctx1; 92 struct stw_context *ctx2; 93 BOOL ret = FALSE; 94 95 if (!stw_dev) 96 return FALSE; 97 98 pipe_mutex_lock( stw_dev->ctx_mutex ); 99 100 ctx1 = stw_lookup_context_locked( dhglrc1 ); 101 ctx2 = stw_lookup_context_locked( dhglrc2 ); 102 103 if (ctx1 && ctx2 && ctx2->st->share) 104 ret = ctx2->st->share(ctx2->st, ctx1->st); 105 106 pipe_mutex_unlock( stw_dev->ctx_mutex ); 107 108 return ret; 109} 110 111DHGLRC APIENTRY 112DrvCreateContext( 113 HDC hdc ) 114{ 115 return DrvCreateLayerContext( hdc, 0 ); 116} 117 118DHGLRC APIENTRY 119DrvCreateLayerContext( 120 HDC hdc, 121 INT iLayerPlane ) 122{ 123 int iPixelFormat; 124 const struct stw_pixelformat_info *pfi; 125 struct st_context_attribs attribs; 126 struct stw_context *ctx = NULL; 127 128 if(!stw_dev) 129 return 0; 130 131 if (iLayerPlane != 0) 132 return 0; 133 134 iPixelFormat = GetPixelFormat(hdc); 135 if(!iPixelFormat) 136 return 0; 137 138 pfi = stw_pixelformat_get_info( iPixelFormat - 1 ); 139 140 ctx = CALLOC_STRUCT( stw_context ); 141 if (ctx == NULL) 142 goto no_ctx; 143 144 ctx->hdc = hdc; 145 ctx->iPixelFormat = iPixelFormat; 146 147 memset(&attribs, 0, sizeof(attribs)); 148 attribs.profile = ST_PROFILE_DEFAULT; 149 attribs.visual = pfi->stvis; 150 151 ctx->st = stw_dev->stapi->create_context(stw_dev->stapi, 152 stw_dev->smapi, &attribs, NULL); 153 if (ctx->st == NULL) 154 goto no_st_ctx; 155 156 ctx->st->st_manager_private = (void *) ctx; 157 158 pipe_mutex_lock( stw_dev->ctx_mutex ); 159 ctx->dhglrc = handle_table_add(stw_dev->ctx_table, ctx); 160 pipe_mutex_unlock( stw_dev->ctx_mutex ); 161 if (!ctx->dhglrc) 162 goto no_hglrc; 163 164 return ctx->dhglrc; 165 166no_hglrc: 167 ctx->st->destroy(ctx->st); 168no_st_ctx: 169 FREE(ctx); 170no_ctx: 171 return 0; 172} 173 174BOOL APIENTRY 175DrvDeleteContext( 176 DHGLRC dhglrc ) 177{ 178 struct stw_context *ctx ; 179 BOOL ret = FALSE; 180 181 if (!stw_dev) 182 return FALSE; 183 184 pipe_mutex_lock( stw_dev->ctx_mutex ); 185 ctx = stw_lookup_context_locked(dhglrc); 186 handle_table_remove(stw_dev->ctx_table, dhglrc); 187 pipe_mutex_unlock( stw_dev->ctx_mutex ); 188 189 if (ctx) { 190 struct stw_context *curctx = stw_current_context(); 191 192 /* Unbind current if deleting current context. */ 193 if (curctx == ctx) 194 stw_dev->stapi->make_current(stw_dev->stapi, NULL, NULL, NULL); 195 196 ctx->st->destroy(ctx->st); 197 FREE(ctx); 198 199 ret = TRUE; 200 } 201 202 return ret; 203} 204 205BOOL APIENTRY 206DrvReleaseContext( 207 DHGLRC dhglrc ) 208{ 209 struct stw_context *ctx; 210 211 if (!stw_dev) 212 return FALSE; 213 214 pipe_mutex_lock( stw_dev->ctx_mutex ); 215 ctx = stw_lookup_context_locked( dhglrc ); 216 pipe_mutex_unlock( stw_dev->ctx_mutex ); 217 218 if (!ctx) 219 return FALSE; 220 221 /* The expectation is that ctx is the same context which is 222 * current for this thread. We should check that and return False 223 * if not the case. 224 */ 225 if (ctx != stw_current_context()) 226 return FALSE; 227 228 if (stw_make_current( NULL, 0 ) == FALSE) 229 return FALSE; 230 231 return TRUE; 232} 233 234 235DHGLRC 236stw_get_current_context( void ) 237{ 238 struct stw_context *ctx; 239 240 ctx = stw_current_context(); 241 if(!ctx) 242 return 0; 243 244 return ctx->dhglrc; 245} 246 247HDC 248stw_get_current_dc( void ) 249{ 250 struct stw_context *ctx; 251 252 ctx = stw_current_context(); 253 if(!ctx) 254 return NULL; 255 256 return ctx->hdc; 257} 258 259BOOL 260stw_make_current( 261 HDC hdc, 262 DHGLRC dhglrc ) 263{ 264 struct stw_context *curctx = NULL; 265 struct stw_context *ctx = NULL; 266 struct stw_framebuffer *fb = NULL; 267 BOOL ret = FALSE; 268 269 if (!stw_dev) 270 return FALSE; 271 272 curctx = stw_current_context(); 273 if (curctx != NULL) { 274 if (curctx->dhglrc == dhglrc) { 275 if (curctx->hdc == hdc) { 276 /* Return if already current. */ 277 return TRUE; 278 } 279 } else { 280 curctx->st->flush(curctx->st, PIPE_FLUSH_RENDER_CACHE, NULL); 281 } 282 } 283 284 if (dhglrc) { 285 pipe_mutex_lock( stw_dev->ctx_mutex ); 286 ctx = stw_lookup_context_locked( dhglrc ); 287 pipe_mutex_unlock( stw_dev->ctx_mutex ); 288 if (!ctx) { 289 goto fail; 290 } 291 292 fb = stw_framebuffer_from_hdc( hdc ); 293 if (fb) { 294 stw_framebuffer_update(fb); 295 } 296 else { 297 /* Applications should call SetPixelFormat before creating a context, 298 * but not all do, and the opengl32 runtime seems to use a default pixel 299 * format in some cases, so we must create a framebuffer for those here 300 */ 301 int iPixelFormat = GetPixelFormat(hdc); 302 if (iPixelFormat) 303 fb = stw_framebuffer_create( hdc, iPixelFormat ); 304 if (!fb) 305 goto fail; 306 } 307 308 if (fb->iPixelFormat != ctx->iPixelFormat) { 309 SetLastError(ERROR_INVALID_PIXEL_FORMAT); 310 goto fail; 311 } 312 313 /* Bind the new framebuffer */ 314 ctx->hdc = hdc; 315 316 ret = stw_dev->stapi->make_current(stw_dev->stapi, ctx->st, fb->stfb, fb->stfb); 317 stw_framebuffer_reference(&ctx->current_framebuffer, fb); 318 } else { 319 ret = stw_dev->stapi->make_current(stw_dev->stapi, NULL, NULL, NULL); 320 } 321 322fail: 323 324 if (fb) { 325 stw_framebuffer_release(fb); 326 } 327 328 /* On failure, make the thread's current rendering context not current 329 * before returning */ 330 if (!ret) { 331 stw_dev->stapi->make_current(stw_dev->stapi, NULL, NULL, NULL); 332 ctx = NULL; 333 } 334 335 /* Unreference the previous framebuffer if any. It must be done after 336 * make_current, as it can be referenced inside. 337 */ 338 if (curctx && curctx != ctx) { 339 stw_framebuffer_reference(&curctx->current_framebuffer, NULL); 340 } 341 342 return ret; 343} 344 345/** 346 * Flush the current context if it is bound to the framebuffer. 347 */ 348void 349stw_flush_current_locked( struct stw_framebuffer *fb ) 350{ 351 struct stw_context *ctx = stw_current_context(); 352 353 if (ctx && ctx->current_framebuffer == fb) { 354 ctx->st->flush(ctx->st, 355 PIPE_FLUSH_RENDER_CACHE | 356 PIPE_FLUSH_SWAPBUFFERS | 357 PIPE_FLUSH_FRAME, 358 NULL); 359 } 360} 361 362/** 363 * Notify the current context that the framebuffer has become invalid. 364 */ 365void 366stw_notify_current_locked( struct stw_framebuffer *fb ) 367{ 368 struct stw_context *ctx = stw_current_context(); 369 370 if (ctx && ctx->current_framebuffer == fb) 371 ctx->st->notify_invalid_framebuffer(ctx->st, fb->stfb); 372} 373 374/** 375 * Although WGL allows different dispatch entrypoints per context 376 */ 377static const GLCLTPROCTABLE cpt = 378{ 379 OPENGL_VERSION_110_ENTRIES, 380 { 381 &glNewList, 382 &glEndList, 383 &glCallList, 384 &glCallLists, 385 &glDeleteLists, 386 &glGenLists, 387 &glListBase, 388 &glBegin, 389 &glBitmap, 390 &glColor3b, 391 &glColor3bv, 392 &glColor3d, 393 &glColor3dv, 394 &glColor3f, 395 &glColor3fv, 396 &glColor3i, 397 &glColor3iv, 398 &glColor3s, 399 &glColor3sv, 400 &glColor3ub, 401 &glColor3ubv, 402 &glColor3ui, 403 &glColor3uiv, 404 &glColor3us, 405 &glColor3usv, 406 &glColor4b, 407 &glColor4bv, 408 &glColor4d, 409 &glColor4dv, 410 &glColor4f, 411 &glColor4fv, 412 &glColor4i, 413 &glColor4iv, 414 &glColor4s, 415 &glColor4sv, 416 &glColor4ub, 417 &glColor4ubv, 418 &glColor4ui, 419 &glColor4uiv, 420 &glColor4us, 421 &glColor4usv, 422 &glEdgeFlag, 423 &glEdgeFlagv, 424 &glEnd, 425 &glIndexd, 426 &glIndexdv, 427 &glIndexf, 428 &glIndexfv, 429 &glIndexi, 430 &glIndexiv, 431 &glIndexs, 432 &glIndexsv, 433 &glNormal3b, 434 &glNormal3bv, 435 &glNormal3d, 436 &glNormal3dv, 437 &glNormal3f, 438 &glNormal3fv, 439 &glNormal3i, 440 &glNormal3iv, 441 &glNormal3s, 442 &glNormal3sv, 443 &glRasterPos2d, 444 &glRasterPos2dv, 445 &glRasterPos2f, 446 &glRasterPos2fv, 447 &glRasterPos2i, 448 &glRasterPos2iv, 449 &glRasterPos2s, 450 &glRasterPos2sv, 451 &glRasterPos3d, 452 &glRasterPos3dv, 453 &glRasterPos3f, 454 &glRasterPos3fv, 455 &glRasterPos3i, 456 &glRasterPos3iv, 457 &glRasterPos3s, 458 &glRasterPos3sv, 459 &glRasterPos4d, 460 &glRasterPos4dv, 461 &glRasterPos4f, 462 &glRasterPos4fv, 463 &glRasterPos4i, 464 &glRasterPos4iv, 465 &glRasterPos4s, 466 &glRasterPos4sv, 467 &glRectd, 468 &glRectdv, 469 &glRectf, 470 &glRectfv, 471 &glRecti, 472 &glRectiv, 473 &glRects, 474 &glRectsv, 475 &glTexCoord1d, 476 &glTexCoord1dv, 477 &glTexCoord1f, 478 &glTexCoord1fv, 479 &glTexCoord1i, 480 &glTexCoord1iv, 481 &glTexCoord1s, 482 &glTexCoord1sv, 483 &glTexCoord2d, 484 &glTexCoord2dv, 485 &glTexCoord2f, 486 &glTexCoord2fv, 487 &glTexCoord2i, 488 &glTexCoord2iv, 489 &glTexCoord2s, 490 &glTexCoord2sv, 491 &glTexCoord3d, 492 &glTexCoord3dv, 493 &glTexCoord3f, 494 &glTexCoord3fv, 495 &glTexCoord3i, 496 &glTexCoord3iv, 497 &glTexCoord3s, 498 &glTexCoord3sv, 499 &glTexCoord4d, 500 &glTexCoord4dv, 501 &glTexCoord4f, 502 &glTexCoord4fv, 503 &glTexCoord4i, 504 &glTexCoord4iv, 505 &glTexCoord4s, 506 &glTexCoord4sv, 507 &glVertex2d, 508 &glVertex2dv, 509 &glVertex2f, 510 &glVertex2fv, 511 &glVertex2i, 512 &glVertex2iv, 513 &glVertex2s, 514 &glVertex2sv, 515 &glVertex3d, 516 &glVertex3dv, 517 &glVertex3f, 518 &glVertex3fv, 519 &glVertex3i, 520 &glVertex3iv, 521 &glVertex3s, 522 &glVertex3sv, 523 &glVertex4d, 524 &glVertex4dv, 525 &glVertex4f, 526 &glVertex4fv, 527 &glVertex4i, 528 &glVertex4iv, 529 &glVertex4s, 530 &glVertex4sv, 531 &glClipPlane, 532 &glColorMaterial, 533 &glCullFace, 534 &glFogf, 535 &glFogfv, 536 &glFogi, 537 &glFogiv, 538 &glFrontFace, 539 &glHint, 540 &glLightf, 541 &glLightfv, 542 &glLighti, 543 &glLightiv, 544 &glLightModelf, 545 &glLightModelfv, 546 &glLightModeli, 547 &glLightModeliv, 548 &glLineStipple, 549 &glLineWidth, 550 &glMaterialf, 551 &glMaterialfv, 552 &glMateriali, 553 &glMaterialiv, 554 &glPointSize, 555 &glPolygonMode, 556 &glPolygonStipple, 557 &glScissor, 558 &glShadeModel, 559 &glTexParameterf, 560 &glTexParameterfv, 561 &glTexParameteri, 562 &glTexParameteriv, 563 &glTexImage1D, 564 &glTexImage2D, 565 &glTexEnvf, 566 &glTexEnvfv, 567 &glTexEnvi, 568 &glTexEnviv, 569 &glTexGend, 570 &glTexGendv, 571 &glTexGenf, 572 &glTexGenfv, 573 &glTexGeni, 574 &glTexGeniv, 575 &glFeedbackBuffer, 576 &glSelectBuffer, 577 &glRenderMode, 578 &glInitNames, 579 &glLoadName, 580 &glPassThrough, 581 &glPopName, 582 &glPushName, 583 &glDrawBuffer, 584 &glClear, 585 &glClearAccum, 586 &glClearIndex, 587 &glClearColor, 588 &glClearStencil, 589 &glClearDepth, 590 &glStencilMask, 591 &glColorMask, 592 &glDepthMask, 593 &glIndexMask, 594 &glAccum, 595 &glDisable, 596 &glEnable, 597 &glFinish, 598 &glFlush, 599 &glPopAttrib, 600 &glPushAttrib, 601 &glMap1d, 602 &glMap1f, 603 &glMap2d, 604 &glMap2f, 605 &glMapGrid1d, 606 &glMapGrid1f, 607 &glMapGrid2d, 608 &glMapGrid2f, 609 &glEvalCoord1d, 610 &glEvalCoord1dv, 611 &glEvalCoord1f, 612 &glEvalCoord1fv, 613 &glEvalCoord2d, 614 &glEvalCoord2dv, 615 &glEvalCoord2f, 616 &glEvalCoord2fv, 617 &glEvalMesh1, 618 &glEvalPoint1, 619 &glEvalMesh2, 620 &glEvalPoint2, 621 &glAlphaFunc, 622 &glBlendFunc, 623 &glLogicOp, 624 &glStencilFunc, 625 &glStencilOp, 626 &glDepthFunc, 627 &glPixelZoom, 628 &glPixelTransferf, 629 &glPixelTransferi, 630 &glPixelStoref, 631 &glPixelStorei, 632 &glPixelMapfv, 633 &glPixelMapuiv, 634 &glPixelMapusv, 635 &glReadBuffer, 636 &glCopyPixels, 637 &glReadPixels, 638 &glDrawPixels, 639 &glGetBooleanv, 640 &glGetClipPlane, 641 &glGetDoublev, 642 &glGetError, 643 &glGetFloatv, 644 &glGetIntegerv, 645 &glGetLightfv, 646 &glGetLightiv, 647 &glGetMapdv, 648 &glGetMapfv, 649 &glGetMapiv, 650 &glGetMaterialfv, 651 &glGetMaterialiv, 652 &glGetPixelMapfv, 653 &glGetPixelMapuiv, 654 &glGetPixelMapusv, 655 &glGetPolygonStipple, 656 &glGetString, 657 &glGetTexEnvfv, 658 &glGetTexEnviv, 659 &glGetTexGendv, 660 &glGetTexGenfv, 661 &glGetTexGeniv, 662 &glGetTexImage, 663 &glGetTexParameterfv, 664 &glGetTexParameteriv, 665 &glGetTexLevelParameterfv, 666 &glGetTexLevelParameteriv, 667 &glIsEnabled, 668 &glIsList, 669 &glDepthRange, 670 &glFrustum, 671 &glLoadIdentity, 672 &glLoadMatrixf, 673 &glLoadMatrixd, 674 &glMatrixMode, 675 &glMultMatrixf, 676 &glMultMatrixd, 677 &glOrtho, 678 &glPopMatrix, 679 &glPushMatrix, 680 &glRotated, 681 &glRotatef, 682 &glScaled, 683 &glScalef, 684 &glTranslated, 685 &glTranslatef, 686 &glViewport, 687 &glArrayElement, 688 &glBindTexture, 689 &glColorPointer, 690 &glDisableClientState, 691 &glDrawArrays, 692 &glDrawElements, 693 &glEdgeFlagPointer, 694 &glEnableClientState, 695 &glIndexPointer, 696 &glIndexub, 697 &glIndexubv, 698 &glInterleavedArrays, 699 &glNormalPointer, 700 &glPolygonOffset, 701 &glTexCoordPointer, 702 &glVertexPointer, 703 &glAreTexturesResident, 704 &glCopyTexImage1D, 705 &glCopyTexImage2D, 706 &glCopyTexSubImage1D, 707 &glCopyTexSubImage2D, 708 &glDeleteTextures, 709 &glGenTextures, 710 &glGetPointerv, 711 &glIsTexture, 712 &glPrioritizeTextures, 713 &glTexSubImage1D, 714 &glTexSubImage2D, 715 &glPopClientAttrib, 716 &glPushClientAttrib 717 } 718}; 719 720PGLCLTPROCTABLE APIENTRY 721DrvSetContext( 722 HDC hdc, 723 DHGLRC dhglrc, 724 PFN_SETPROCTABLE pfnSetProcTable ) 725{ 726 PGLCLTPROCTABLE r = (PGLCLTPROCTABLE)&cpt; 727 728 if (!stw_make_current( hdc, dhglrc )) 729 r = NULL; 730 731 return r; 732} 733