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