osmesa.c revision 2873555a76a7358db053c3a7b121b489f8df9bb1
1/* 2 * Mesa 3-D graphics library 3 * Version: 6.5.3 4 * 5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. 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 26/* 27 * Off-Screen Mesa rendering / Rendering into client memory space 28 * 29 * Note on thread safety: this driver is thread safe. All 30 * functions are reentrant. The notion of current context is 31 * managed by the core _mesa_make_current() and _mesa_get_current_context() 32 * functions. Those functions are thread-safe. 33 */ 34 35 36#include "main/glheader.h" 37#include "GL/osmesa.h" 38#include "main/context.h" 39#include "main/extensions.h" 40#include "main/formats.h" 41#include "main/framebuffer.h" 42#include "main/imports.h" 43#include "main/macros.h" 44#include "main/mtypes.h" 45#include "main/renderbuffer.h" 46#include "swrast/swrast.h" 47#include "swrast_setup/swrast_setup.h" 48#include "swrast/s_context.h" 49#include "swrast/s_lines.h" 50#include "swrast/s_renderbuffer.h" 51#include "swrast/s_triangle.h" 52#include "tnl/tnl.h" 53#include "tnl/t_context.h" 54#include "tnl/t_pipeline.h" 55#include "drivers/common/driverfuncs.h" 56#include "drivers/common/meta.h" 57#include "vbo/vbo.h" 58 59 60#define OSMESA_RENDERBUFFER_CLASS 0x053 61 62 63/** 64 * OSMesa rendering context, derived from core Mesa struct gl_context. 65 */ 66struct osmesa_context 67{ 68 struct gl_context mesa; /*< Base class - this must be first */ 69 struct gl_config *gl_visual; /*< Describes the buffers */ 70 struct gl_renderbuffer *rb; /*< The user's colorbuffer */ 71 struct gl_framebuffer *gl_buffer; /*< The framebuffer, containing user's rb */ 72 GLenum format; /*< User-specified context format */ 73 GLint userRowLength; /*< user-specified number of pixels per row */ 74 GLint rInd, gInd, bInd, aInd;/*< index offsets for RGBA formats */ 75 GLvoid *rowaddr[MAX_HEIGHT]; /*< address of first pixel in each image row */ 76 GLboolean yup; /*< TRUE -> Y increases upward */ 77 /*< FALSE -> Y increases downward */ 78}; 79 80 81static INLINE OSMesaContext 82OSMESA_CONTEXT(struct gl_context *ctx) 83{ 84 /* Just cast, since we're using structure containment */ 85 return (OSMesaContext) ctx; 86} 87 88 89/**********************************************************************/ 90/*** Private Device Driver Functions ***/ 91/**********************************************************************/ 92 93 94static const GLubyte * 95get_string( struct gl_context *ctx, GLenum name ) 96{ 97 (void) ctx; 98 switch (name) { 99 case GL_RENDERER: 100#if CHAN_BITS == 32 101 return (const GLubyte *) "Mesa OffScreen32"; 102#elif CHAN_BITS == 16 103 return (const GLubyte *) "Mesa OffScreen16"; 104#else 105 return (const GLubyte *) "Mesa OffScreen"; 106#endif 107 default: 108 return NULL; 109 } 110} 111 112 113static void 114osmesa_update_state( struct gl_context *ctx, GLuint new_state ) 115{ 116 /* easy - just propogate */ 117 _swrast_InvalidateState( ctx, new_state ); 118 _swsetup_InvalidateState( ctx, new_state ); 119 _tnl_InvalidateState( ctx, new_state ); 120 _vbo_InvalidateState( ctx, new_state ); 121} 122 123 124 125/** 126 * Macros for optimized line/triangle rendering. 127 * Only for 8-bit channel, RGBA, BGRA, ARGB formats. 128 */ 129 130#define PACK_RGBA(DST, R, G, B, A) \ 131do { \ 132 (DST)[osmesa->rInd] = R; \ 133 (DST)[osmesa->gInd] = G; \ 134 (DST)[osmesa->bInd] = B; \ 135 (DST)[osmesa->aInd] = A; \ 136} while (0) 137 138#define PIXELADDR4(X,Y) ((GLchan *) osmesa->rowaddr[Y] + 4 * (X)) 139 140 141/** 142 * Draw a flat-shaded, RGB line into an osmesa buffer. 143 */ 144#define NAME flat_rgba_line 145#define CLIP_HACK 1 146#define SETUP_CODE \ 147 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \ 148 const GLchan *color = vert1->color; 149 150#define PLOT(X, Y) \ 151do { \ 152 GLchan *p = PIXELADDR4(X, Y); \ 153 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \ 154} while (0) 155 156#include "swrast/s_linetemp.h" 157 158 159 160/** 161 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer. 162 */ 163#define NAME flat_rgba_z_line 164#define CLIP_HACK 1 165#define INTERP_Z 1 166#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE 167#define SETUP_CODE \ 168 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \ 169 const GLchan *color = vert1->color; 170 171#define PLOT(X, Y) \ 172do { \ 173 if (Z < *zPtr) { \ 174 GLchan *p = PIXELADDR4(X, Y); \ 175 PACK_RGBA(p, color[RCOMP], color[GCOMP], \ 176 color[BCOMP], color[ACOMP]); \ 177 *zPtr = Z; \ 178 } \ 179} while (0) 180 181#include "swrast/s_linetemp.h" 182 183 184 185/** 186 * Analyze context state to see if we can provide a fast line drawing 187 * function. Otherwise, return NULL. 188 */ 189static swrast_line_func 190osmesa_choose_line_function( struct gl_context *ctx ) 191{ 192 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); 193 const SWcontext *swrast = SWRAST_CONTEXT(ctx); 194 195 if (osmesa->rb->DataType != GL_UNSIGNED_BYTE) 196 return NULL; 197 198 if (ctx->RenderMode != GL_RENDER) return NULL; 199 if (ctx->Line.SmoothFlag) return NULL; 200 if (ctx->Texture._EnabledUnits) return NULL; 201 if (ctx->Light.ShadeModel != GL_FLAT) return NULL; 202 if (ctx->Line.Width != 1.0F) return NULL; 203 if (ctx->Line.StippleFlag) return NULL; 204 if (ctx->Line.SmoothFlag) return NULL; 205 if (osmesa->format != OSMESA_RGBA && 206 osmesa->format != OSMESA_BGRA && 207 osmesa->format != OSMESA_ARGB) return NULL; 208 209 if (swrast->_RasterMask==DEPTH_BIT 210 && ctx->Depth.Func==GL_LESS 211 && ctx->Depth.Mask==GL_TRUE 212 && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS) { 213 return (swrast_line_func) flat_rgba_z_line; 214 } 215 216 if (swrast->_RasterMask == 0) { 217 return (swrast_line_func) flat_rgba_line; 218 } 219 220 return (swrast_line_func) NULL; 221} 222 223 224/**********************************************************************/ 225/***** Optimized triangle rendering *****/ 226/**********************************************************************/ 227 228 229/* 230 * Smooth-shaded, z-less triangle, RGBA color. 231 */ 232#define NAME smooth_rgba_z_triangle 233#define INTERP_Z 1 234#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE 235#define INTERP_RGB 1 236#define INTERP_ALPHA 1 237#define SETUP_CODE \ 238 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); 239#define RENDER_SPAN( span ) { \ 240 GLuint i; \ 241 GLchan *img = PIXELADDR4(span.x, span.y); \ 242 for (i = 0; i < span.end; i++, img += 4) { \ 243 const GLuint z = FixedToDepth(span.z); \ 244 if (z < zRow[i]) { \ 245 PACK_RGBA(img, FixedToChan(span.red), \ 246 FixedToChan(span.green), FixedToChan(span.blue), \ 247 FixedToChan(span.alpha)); \ 248 zRow[i] = z; \ 249 } \ 250 span.red += span.redStep; \ 251 span.green += span.greenStep; \ 252 span.blue += span.blueStep; \ 253 span.alpha += span.alphaStep; \ 254 span.z += span.zStep; \ 255 } \ 256} 257#include "swrast/s_tritemp.h" 258 259 260 261/* 262 * Flat-shaded, z-less triangle, RGBA color. 263 */ 264#define NAME flat_rgba_z_triangle 265#define INTERP_Z 1 266#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE 267#define SETUP_CODE \ 268 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \ 269 GLuint pixel; \ 270 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \ 271 v2->color[2], v2->color[3]); 272 273#define RENDER_SPAN( span ) { \ 274 GLuint i; \ 275 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \ 276 for (i = 0; i < span.end; i++) { \ 277 const GLuint z = FixedToDepth(span.z); \ 278 if (z < zRow[i]) { \ 279 img[i] = pixel; \ 280 zRow[i] = z; \ 281 } \ 282 span.z += span.zStep; \ 283 } \ 284} 285 286#include "swrast/s_tritemp.h" 287 288 289 290/** 291 * Return pointer to an optimized triangle function if possible. 292 */ 293static swrast_tri_func 294osmesa_choose_triangle_function( struct gl_context *ctx ) 295{ 296 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); 297 const SWcontext *swrast = SWRAST_CONTEXT(ctx); 298 299 if (osmesa->rb->DataType != GL_UNSIGNED_BYTE) 300 return (swrast_tri_func) NULL; 301 302 if (ctx->RenderMode != GL_RENDER) return (swrast_tri_func) NULL; 303 if (ctx->Polygon.SmoothFlag) return (swrast_tri_func) NULL; 304 if (ctx->Polygon.StippleFlag) return (swrast_tri_func) NULL; 305 if (ctx->Texture._EnabledUnits) return (swrast_tri_func) NULL; 306 if (osmesa->format != OSMESA_RGBA && 307 osmesa->format != OSMESA_BGRA && 308 osmesa->format != OSMESA_ARGB) return (swrast_tri_func) NULL; 309 if (ctx->Polygon.CullFlag && 310 ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) 311 return (swrast_tri_func) NULL; 312 313 if (swrast->_RasterMask == DEPTH_BIT && 314 ctx->Depth.Func == GL_LESS && 315 ctx->Depth.Mask == GL_TRUE && 316 ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS) { 317 if (ctx->Light.ShadeModel == GL_SMOOTH) { 318 return (swrast_tri_func) smooth_rgba_z_triangle; 319 } 320 else { 321 return (swrast_tri_func) flat_rgba_z_triangle; 322 } 323 } 324 return (swrast_tri_func) NULL; 325} 326 327 328 329/* Override for the swrast triangle-selection function. Try to use one 330 * of our internal triangle functions, otherwise fall back to the 331 * standard swrast functions. 332 */ 333static void 334osmesa_choose_triangle( struct gl_context *ctx ) 335{ 336 SWcontext *swrast = SWRAST_CONTEXT(ctx); 337 338 swrast->Triangle = osmesa_choose_triangle_function( ctx ); 339 if (!swrast->Triangle) 340 _swrast_choose_triangle( ctx ); 341} 342 343static void 344osmesa_choose_line( struct gl_context *ctx ) 345{ 346 SWcontext *swrast = SWRAST_CONTEXT(ctx); 347 348 swrast->Line = osmesa_choose_line_function( ctx ); 349 if (!swrast->Line) 350 _swrast_choose_line( ctx ); 351} 352 353 354 355/** 356 * Recompute the values of the context's rowaddr array. 357 */ 358static void 359compute_row_addresses( OSMesaContext osmesa ) 360{ 361 GLint bytesPerRow, i; 362 GLubyte *origin = (GLubyte *) osmesa->rb->Data; 363 GLint rowlength; /* in pixels */ 364 GLint height = osmesa->rb->Height; 365 366 if (osmesa->userRowLength) 367 rowlength = osmesa->userRowLength; 368 else 369 rowlength = osmesa->rb->Width; 370 371 bytesPerRow = rowlength * _mesa_get_format_bytes(osmesa->rb->Format); 372 373 if (osmesa->yup) { 374 /* Y=0 is bottom line of window */ 375 for (i = 0; i < height; i++) { 376 osmesa->rowaddr[i] = (GLvoid *) ((GLubyte *) origin + i * bytesPerRow); 377 } 378 } 379 else { 380 /* Y=0 is top line of window */ 381 for (i = 0; i < height; i++) { 382 GLint j = height - i - 1; 383 osmesa->rowaddr[i] = (GLvoid *) ((GLubyte *) origin + j * bytesPerRow); 384 } 385 } 386} 387 388 389 390/** 391 * Don't use _mesa_delete_renderbuffer since we can't free rb->Data. 392 */ 393static void 394osmesa_delete_renderbuffer(struct gl_renderbuffer *rb) 395{ 396 free(rb); 397} 398 399 400/** 401 * Allocate renderbuffer storage. We don't actually allocate any storage 402 * since we're using a user-provided buffer. 403 * Just set up all the gl_renderbuffer methods. 404 */ 405static GLboolean 406osmesa_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb, 407 GLenum internalFormat, GLuint width, GLuint height) 408{ 409 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); 410 411 /* Note: we can ignoring internalFormat for "window-system" renderbuffers */ 412 (void) internalFormat; 413 414 /* Given the user-provided format and type, figure out which MESA_FORMAT_x 415 * to use. 416 * XXX There aren't Mesa formats for all the possible combinations here! 417 * XXX Specifically, there's only RGBA-order 16-bit/channel and float 418 * XXX formats. 419 * XXX The 8-bit/channel formats should all be OK. 420 */ 421 if (osmesa->format == OSMESA_RGBA) { 422 if (rb->DataType == GL_UNSIGNED_BYTE) { 423 if (_mesa_little_endian()) 424 rb->Format = MESA_FORMAT_RGBA8888_REV; 425 else 426 rb->Format = MESA_FORMAT_RGBA8888; 427 } 428 else if (rb->DataType == GL_UNSIGNED_SHORT) { 429 rb->Format = MESA_FORMAT_RGBA_16; 430 } 431 else { 432 rb->Format = MESA_FORMAT_RGBA_FLOAT32; 433 } 434 } 435 else if (osmesa->format == OSMESA_BGRA) { 436 if (rb->DataType == GL_UNSIGNED_BYTE) { 437 if (_mesa_little_endian()) 438 rb->Format = MESA_FORMAT_ARGB8888; 439 else 440 rb->Format = MESA_FORMAT_ARGB8888_REV; 441 } 442 else if (rb->DataType == GL_UNSIGNED_SHORT) { 443 _mesa_warning(ctx, "Unsupported OSMesa format BGRA/GLushort"); 444 rb->Format = MESA_FORMAT_RGBA_16; /* not exactly right */ 445 } 446 else { 447 _mesa_warning(ctx, "Unsupported OSMesa format BGRA/GLfloat"); 448 rb->Format = MESA_FORMAT_RGBA_FLOAT32; /* not exactly right */ 449 } 450 } 451 else if (osmesa->format == OSMESA_ARGB) { 452 if (rb->DataType == GL_UNSIGNED_BYTE) { 453 if (_mesa_little_endian()) 454 rb->Format = MESA_FORMAT_ARGB8888_REV; 455 else 456 rb->Format = MESA_FORMAT_ARGB8888; 457 } 458 else if (rb->DataType == GL_UNSIGNED_SHORT) { 459 _mesa_warning(ctx, "Unsupported OSMesa format ARGB/GLushort"); 460 rb->Format = MESA_FORMAT_RGBA_16; /* not exactly right */ 461 } 462 else { 463 _mesa_warning(ctx, "Unsupported OSMesa format ARGB/GLfloat"); 464 rb->Format = MESA_FORMAT_RGBA_FLOAT32; /* not exactly right */ 465 } 466 } 467 else if (osmesa->format == OSMESA_RGB) { 468 if (rb->DataType == GL_UNSIGNED_BYTE) { 469 rb->Format = MESA_FORMAT_RGB888; 470 } 471 else if (rb->DataType == GL_UNSIGNED_SHORT) { 472 _mesa_warning(ctx, "Unsupported OSMesa format RGB/GLushort"); 473 rb->Format = MESA_FORMAT_RGBA_16; /* not exactly right */ 474 } 475 else { 476 _mesa_warning(ctx, "Unsupported OSMesa format RGB/GLfloat"); 477 rb->Format = MESA_FORMAT_RGBA_FLOAT32; /* not exactly right */ 478 } 479 } 480 else if (osmesa->format == OSMESA_BGR) { 481 if (rb->DataType == GL_UNSIGNED_BYTE) { 482 rb->Format = MESA_FORMAT_BGR888; 483 } 484 else if (rb->DataType == GL_UNSIGNED_SHORT) { 485 _mesa_warning(ctx, "Unsupported OSMesa format BGR/GLushort"); 486 rb->Format = MESA_FORMAT_RGBA_16; /* not exactly right */ 487 } 488 else { 489 _mesa_warning(ctx, "Unsupported OSMesa format BGR/GLfloat"); 490 rb->Format = MESA_FORMAT_RGBA_FLOAT32; /* not exactly right */ 491 } 492 } 493 else if (osmesa->format == OSMESA_RGB_565) { 494 ASSERT(rb->DataType == GL_UNSIGNED_BYTE); 495 rb->Format = MESA_FORMAT_RGB565; 496 } 497 else { 498 _mesa_problem(ctx, "bad pixel format in osmesa renderbuffer_storage"); 499 } 500 501 rb->Width = width; 502 rb->Height = height; 503 504 compute_row_addresses( osmesa ); 505 506 return GL_TRUE; 507} 508 509 510/** 511 * Allocate a new renderbuffer to describe the user-provided color buffer. 512 */ 513static struct gl_renderbuffer * 514new_osmesa_renderbuffer(struct gl_context *ctx, GLenum format, GLenum type) 515{ 516 const GLuint name = 0; 517 struct gl_renderbuffer *rb = _mesa_new_renderbuffer(ctx, name); 518 if (rb) { 519 rb->RefCount = 1; 520 rb->Delete = osmesa_delete_renderbuffer; 521 rb->AllocStorage = osmesa_renderbuffer_storage; 522 rb->ClassID = OSMESA_RENDERBUFFER_CLASS; 523 524 rb->InternalFormat = GL_RGBA; 525 rb->_BaseFormat = GL_RGBA; 526 rb->DataType = type; 527 } 528 return rb; 529} 530 531 532 533static void 534osmesa_MapRenderbuffer(struct gl_context *ctx, 535 struct gl_renderbuffer *rb, 536 GLuint x, GLuint y, GLuint w, GLuint h, 537 GLbitfield mode, 538 GLubyte **mapOut, GLint *rowStrideOut) 539{ 540 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); 541 542 if (rb->ClassID == OSMESA_RENDERBUFFER_CLASS) { 543 /* this is an OSMesa renderbuffer which wraps user memory */ 544 const GLuint bpp = _mesa_get_format_bytes(rb->Format); 545 GLint rowStride; /* in bytes */ 546 547 if (osmesa->userRowLength) 548 rowStride = osmesa->userRowLength * bpp; 549 else 550 rowStride = rb->Width * bpp; 551 552 if (!osmesa->yup) { 553 /* Y=0 is top line of window */ 554 y = rb->Height - y - 1; 555 *rowStrideOut = -rowStride; 556 } 557 else { 558 *rowStrideOut = rowStride; 559 } 560 561 *mapOut = (GLubyte *) rb->Data + y * rowStride + x * bpp; 562 } 563 else { 564 _swrast_map_soft_renderbuffer(ctx, rb, x, y, w, h, mode, 565 mapOut, rowStrideOut); 566 } 567} 568 569 570static void 571osmesa_UnmapRenderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb) 572{ 573 if (rb->ClassID == OSMESA_RENDERBUFFER_CLASS) { 574 /* no-op */ 575 } 576 else { 577 _swrast_unmap_soft_renderbuffer(ctx, rb); 578 } 579} 580 581 582/**********************************************************************/ 583/***** Public Functions *****/ 584/**********************************************************************/ 585 586 587/** 588 * Create an Off-Screen Mesa rendering context. The only attribute needed is 589 * an RGBA vs Color-Index mode flag. 590 * 591 * Input: format - Must be GL_RGBA 592 * sharelist - specifies another OSMesaContext with which to share 593 * display lists. NULL indicates no sharing. 594 * Return: an OSMesaContext or 0 if error 595 */ 596GLAPI OSMesaContext GLAPIENTRY 597OSMesaCreateContext( GLenum format, OSMesaContext sharelist ) 598{ 599 return OSMesaCreateContextExt(format, DEFAULT_SOFTWARE_DEPTH_BITS, 600 8, 0, sharelist); 601} 602 603 604 605/** 606 * New in Mesa 3.5 607 * 608 * Create context and specify size of ancillary buffers. 609 */ 610GLAPI OSMesaContext GLAPIENTRY 611OSMesaCreateContextExt( GLenum format, GLint depthBits, GLint stencilBits, 612 GLint accumBits, OSMesaContext sharelist ) 613{ 614 OSMesaContext osmesa; 615 struct dd_function_table functions; 616 GLint rind, gind, bind, aind; 617 GLint redBits = 0, greenBits = 0, blueBits = 0, alphaBits =0; 618 619 rind = gind = bind = aind = 0; 620 if (format==OSMESA_RGBA) { 621 redBits = CHAN_BITS; 622 greenBits = CHAN_BITS; 623 blueBits = CHAN_BITS; 624 alphaBits = CHAN_BITS; 625 rind = 0; 626 gind = 1; 627 bind = 2; 628 aind = 3; 629 } 630 else if (format==OSMESA_BGRA) { 631 redBits = CHAN_BITS; 632 greenBits = CHAN_BITS; 633 blueBits = CHAN_BITS; 634 alphaBits = CHAN_BITS; 635 bind = 0; 636 gind = 1; 637 rind = 2; 638 aind = 3; 639 } 640 else if (format==OSMESA_ARGB) { 641 redBits = CHAN_BITS; 642 greenBits = CHAN_BITS; 643 blueBits = CHAN_BITS; 644 alphaBits = CHAN_BITS; 645 aind = 0; 646 rind = 1; 647 gind = 2; 648 bind = 3; 649 } 650 else if (format==OSMESA_RGB) { 651 redBits = CHAN_BITS; 652 greenBits = CHAN_BITS; 653 blueBits = CHAN_BITS; 654 alphaBits = 0; 655 rind = 0; 656 gind = 1; 657 bind = 2; 658 } 659 else if (format==OSMESA_BGR) { 660 redBits = CHAN_BITS; 661 greenBits = CHAN_BITS; 662 blueBits = CHAN_BITS; 663 alphaBits = 0; 664 rind = 2; 665 gind = 1; 666 bind = 0; 667 } 668#if CHAN_TYPE == GL_UNSIGNED_BYTE 669 else if (format==OSMESA_RGB_565) { 670 redBits = 5; 671 greenBits = 6; 672 blueBits = 5; 673 alphaBits = 0; 674 rind = 0; /* not used */ 675 gind = 0; 676 bind = 0; 677 } 678#endif 679 else { 680 return NULL; 681 } 682 683 osmesa = (OSMesaContext) CALLOC_STRUCT(osmesa_context); 684 if (osmesa) { 685 osmesa->gl_visual = _mesa_create_visual( GL_FALSE, /* double buffer */ 686 GL_FALSE, /* stereo */ 687 redBits, 688 greenBits, 689 blueBits, 690 alphaBits, 691 depthBits, 692 stencilBits, 693 accumBits, 694 accumBits, 695 accumBits, 696 alphaBits ? accumBits : 0, 697 1 /* num samples */ 698 ); 699 if (!osmesa->gl_visual) { 700 free(osmesa); 701 return NULL; 702 } 703 704 /* Initialize device driver function table */ 705 _mesa_init_driver_functions(&functions); 706 /* override with our functions */ 707 functions.GetString = get_string; 708 functions.UpdateState = osmesa_update_state; 709 functions.GetBufferSize = NULL; 710 711 if (!_mesa_initialize_context(&osmesa->mesa, 712 API_OPENGL, 713 osmesa->gl_visual, 714 sharelist ? &sharelist->mesa 715 : (struct gl_context *) NULL, 716 &functions, (void *) osmesa)) { 717 _mesa_destroy_visual( osmesa->gl_visual ); 718 free(osmesa); 719 return NULL; 720 } 721 722 _mesa_enable_sw_extensions(&(osmesa->mesa)); 723 _mesa_enable_1_3_extensions(&(osmesa->mesa)); 724 _mesa_enable_1_4_extensions(&(osmesa->mesa)); 725 _mesa_enable_1_5_extensions(&(osmesa->mesa)); 726 _mesa_enable_2_0_extensions(&(osmesa->mesa)); 727 _mesa_enable_2_1_extensions(&(osmesa->mesa)); 728 729 osmesa->gl_buffer = _mesa_create_framebuffer(osmesa->gl_visual); 730 if (!osmesa->gl_buffer) { 731 _mesa_destroy_visual( osmesa->gl_visual ); 732 _mesa_free_context_data( &osmesa->mesa ); 733 free(osmesa); 734 return NULL; 735 } 736 737 /* Create depth/stencil/accum buffers. We'll create the color 738 * buffer later in OSMesaMakeCurrent(). 739 */ 740 _swrast_add_soft_renderbuffers(osmesa->gl_buffer, 741 GL_FALSE, /* color */ 742 osmesa->gl_visual->haveDepthBuffer, 743 osmesa->gl_visual->haveStencilBuffer, 744 osmesa->gl_visual->haveAccumBuffer, 745 GL_FALSE, /* alpha */ 746 GL_FALSE /* aux */ ); 747 748 osmesa->format = format; 749 osmesa->userRowLength = 0; 750 osmesa->yup = GL_TRUE; 751 osmesa->rInd = rind; 752 osmesa->gInd = gind; 753 osmesa->bInd = bind; 754 osmesa->aInd = aind; 755 756 _mesa_meta_init(&osmesa->mesa); 757 758 /* Initialize the software rasterizer and helper modules. */ 759 { 760 struct gl_context *ctx = &osmesa->mesa; 761 SWcontext *swrast; 762 TNLcontext *tnl; 763 764 if (!_swrast_CreateContext( ctx ) || 765 !_vbo_CreateContext( ctx ) || 766 !_tnl_CreateContext( ctx ) || 767 !_swsetup_CreateContext( ctx )) { 768 _mesa_destroy_visual(osmesa->gl_visual); 769 _mesa_free_context_data(ctx); 770 free(osmesa); 771 return NULL; 772 } 773 774 _swsetup_Wakeup( ctx ); 775 776 /* use default TCL pipeline */ 777 tnl = TNL_CONTEXT(ctx); 778 tnl->Driver.RunPipeline = _tnl_run_pipeline; 779 780 ctx->Driver.MapRenderbuffer = osmesa_MapRenderbuffer; 781 ctx->Driver.UnmapRenderbuffer = osmesa_UnmapRenderbuffer; 782 783 /* Extend the software rasterizer with our optimized line and triangle 784 * drawing functions. 785 */ 786 swrast = SWRAST_CONTEXT( ctx ); 787 swrast->choose_line = osmesa_choose_line; 788 swrast->choose_triangle = osmesa_choose_triangle; 789 } 790 } 791 return osmesa; 792} 793 794 795/** 796 * Destroy an Off-Screen Mesa rendering context. 797 * 798 * \param osmesa the context to destroy 799 */ 800GLAPI void GLAPIENTRY 801OSMesaDestroyContext( OSMesaContext osmesa ) 802{ 803 if (osmesa) { 804 if (osmesa->rb) 805 _mesa_reference_renderbuffer(&osmesa->rb, NULL); 806 807 _mesa_meta_free( &osmesa->mesa ); 808 809 _swsetup_DestroyContext( &osmesa->mesa ); 810 _tnl_DestroyContext( &osmesa->mesa ); 811 _vbo_DestroyContext( &osmesa->mesa ); 812 _swrast_DestroyContext( &osmesa->mesa ); 813 814 _mesa_destroy_visual( osmesa->gl_visual ); 815 _mesa_reference_framebuffer( &osmesa->gl_buffer, NULL ); 816 817 _mesa_free_context_data( &osmesa->mesa ); 818 free( osmesa ); 819 } 820} 821 822 823/** 824 * Bind an OSMesaContext to an image buffer. The image buffer is just a 825 * block of memory which the client provides. Its size must be at least 826 * as large as width*height*sizeof(type). Its address should be a multiple 827 * of 4 if using RGBA mode. 828 * 829 * Image data is stored in the order of glDrawPixels: row-major order 830 * with the lower-left image pixel stored in the first array position 831 * (ie. bottom-to-top). 832 * 833 * If the context's viewport hasn't been initialized yet, it will now be 834 * initialized to (0,0,width,height). 835 * 836 * Input: osmesa - the rendering context 837 * buffer - the image buffer memory 838 * type - data type for pixel components 839 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5 840 * are supported. But if Mesa's been compiled with CHAN_BITS==16 841 * then type may be GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE. And if 842 * Mesa's been build with CHAN_BITS==32 then type may be GL_FLOAT, 843 * GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE. 844 * width, height - size of image buffer in pixels, at least 1 845 * Return: GL_TRUE if success, GL_FALSE if error because of invalid osmesa, 846 * invalid buffer address, invalid type, width<1, height<1, 847 * width>internal limit or height>internal limit. 848 */ 849GLAPI GLboolean GLAPIENTRY 850OSMesaMakeCurrent( OSMesaContext osmesa, void *buffer, GLenum type, 851 GLsizei width, GLsizei height ) 852{ 853 if (!osmesa || !buffer || 854 width < 1 || height < 1 || 855 width > MAX_WIDTH || height > MAX_HEIGHT) { 856 return GL_FALSE; 857 } 858 859 if (osmesa->format == OSMESA_RGB_565 && type != GL_UNSIGNED_SHORT_5_6_5) { 860 return GL_FALSE; 861 } 862 863#if 0 864 if (!(type == GL_UNSIGNED_BYTE || 865 (type == GL_UNSIGNED_SHORT && CHAN_BITS >= 16) || 866 (type == GL_FLOAT && CHAN_BITS == 32))) { 867 /* i.e. is sizeof(type) * 8 > CHAN_BITS? */ 868 return GL_FALSE; 869 } 870#endif 871 872 osmesa_update_state( &osmesa->mesa, 0 ); 873 874 /* Call this periodically to detect when the user has begun using 875 * GL rendering from multiple threads. 876 */ 877 _glapi_check_multithread(); 878 879 880 /* Create a front/left color buffer which wraps the user-provided buffer. 881 * There is no back color buffer. 882 * If the user tries to use a 8, 16 or 32-bit/channel buffer that 883 * doesn't match what Mesa was compiled for (CHAN_BITS) the 884 * _mesa_add_renderbuffer() function will create a "wrapper" renderbuffer 885 * that converts rendering from CHAN_BITS to the user-requested channel 886 * size. 887 */ 888 if (!osmesa->rb) { 889 osmesa->rb = new_osmesa_renderbuffer(&osmesa->mesa, osmesa->format, type); 890 _mesa_remove_renderbuffer(osmesa->gl_buffer, BUFFER_FRONT_LEFT); 891 _mesa_add_renderbuffer(osmesa->gl_buffer, BUFFER_FRONT_LEFT, osmesa->rb); 892 assert(osmesa->rb->RefCount == 2); 893 } 894 895 /* Set renderbuffer fields. Set width/height = 0 to force 896 * osmesa_renderbuffer_storage() being called by _mesa_resize_framebuffer() 897 */ 898 osmesa->rb->Data = buffer; 899 osmesa->rb->Width = osmesa->rb->Height = 0; 900 901 /* Set the framebuffer's size. This causes the 902 * osmesa_renderbuffer_storage() function to get called. 903 */ 904 _mesa_resize_framebuffer(&osmesa->mesa, osmesa->gl_buffer, width, height); 905 osmesa->gl_buffer->Initialized = GL_TRUE; /* XXX TEMPORARY? */ 906 907 _mesa_make_current( &osmesa->mesa, osmesa->gl_buffer, osmesa->gl_buffer ); 908 909 /* Remove renderbuffer attachment, then re-add. This installs the 910 * renderbuffer adaptor/wrapper if needed (for bpp conversion). 911 */ 912 _mesa_remove_renderbuffer(osmesa->gl_buffer, BUFFER_FRONT_LEFT); 913 _mesa_add_renderbuffer(osmesa->gl_buffer, BUFFER_FRONT_LEFT, osmesa->rb); 914 915 916 /* this updates the visual's red/green/blue/alphaBits fields */ 917 _mesa_update_framebuffer_visual(&osmesa->mesa, osmesa->gl_buffer); 918 919 /* update the framebuffer size */ 920 _mesa_resize_framebuffer(&osmesa->mesa, osmesa->gl_buffer, width, height); 921 922 return GL_TRUE; 923} 924 925 926 927GLAPI OSMesaContext GLAPIENTRY 928OSMesaGetCurrentContext( void ) 929{ 930 struct gl_context *ctx = _mesa_get_current_context(); 931 if (ctx) 932 return (OSMesaContext) ctx; 933 else 934 return NULL; 935} 936 937 938 939GLAPI void GLAPIENTRY 940OSMesaPixelStore( GLint pname, GLint value ) 941{ 942 OSMesaContext osmesa = OSMesaGetCurrentContext(); 943 944 switch (pname) { 945 case OSMESA_ROW_LENGTH: 946 if (value<0) { 947 _mesa_error( &osmesa->mesa, GL_INVALID_VALUE, 948 "OSMesaPixelStore(value)" ); 949 return; 950 } 951 osmesa->userRowLength = value; 952 break; 953 case OSMESA_Y_UP: 954 osmesa->yup = value ? GL_TRUE : GL_FALSE; 955 break; 956 default: 957 _mesa_error( &osmesa->mesa, GL_INVALID_ENUM, "OSMesaPixelStore(pname)" ); 958 return; 959 } 960 961 compute_row_addresses( osmesa ); 962} 963 964 965GLAPI void GLAPIENTRY 966OSMesaGetIntegerv( GLint pname, GLint *value ) 967{ 968 OSMesaContext osmesa = OSMesaGetCurrentContext(); 969 970 switch (pname) { 971 case OSMESA_WIDTH: 972 if (osmesa->gl_buffer) 973 *value = osmesa->gl_buffer->Width; 974 else 975 *value = 0; 976 return; 977 case OSMESA_HEIGHT: 978 if (osmesa->gl_buffer) 979 *value = osmesa->gl_buffer->Height; 980 else 981 *value = 0; 982 return; 983 case OSMESA_FORMAT: 984 *value = osmesa->format; 985 return; 986 case OSMESA_TYPE: 987 /* current color buffer's data type */ 988 if (osmesa->rb) { 989 *value = osmesa->rb->DataType; 990 } 991 else { 992 *value = 0; 993 } 994 return; 995 case OSMESA_ROW_LENGTH: 996 *value = osmesa->userRowLength; 997 return; 998 case OSMESA_Y_UP: 999 *value = osmesa->yup; 1000 return; 1001 case OSMESA_MAX_WIDTH: 1002 *value = MAX_WIDTH; 1003 return; 1004 case OSMESA_MAX_HEIGHT: 1005 *value = MAX_HEIGHT; 1006 return; 1007 default: 1008 _mesa_error(&osmesa->mesa, GL_INVALID_ENUM, "OSMesaGetIntergerv(pname)"); 1009 return; 1010 } 1011} 1012 1013 1014/** 1015 * Return the depth buffer associated with an OSMesa context. 1016 * Input: c - the OSMesa context 1017 * Output: width, height - size of buffer in pixels 1018 * bytesPerValue - bytes per depth value (2 or 4) 1019 * buffer - pointer to depth buffer values 1020 * Return: GL_TRUE or GL_FALSE to indicate success or failure. 1021 */ 1022GLAPI GLboolean GLAPIENTRY 1023OSMesaGetDepthBuffer( OSMesaContext c, GLint *width, GLint *height, 1024 GLint *bytesPerValue, void **buffer ) 1025{ 1026 struct gl_renderbuffer *rb = NULL; 1027 1028 if (c->gl_buffer) 1029 rb = c->gl_buffer->Attachment[BUFFER_DEPTH].Renderbuffer; 1030 1031 if (!rb || !rb->Data) { 1032 *width = 0; 1033 *height = 0; 1034 *bytesPerValue = 0; 1035 *buffer = 0; 1036 return GL_FALSE; 1037 } 1038 else { 1039 *width = rb->Width; 1040 *height = rb->Height; 1041 if (c->gl_visual->depthBits <= 16) 1042 *bytesPerValue = sizeof(GLushort); 1043 else 1044 *bytesPerValue = sizeof(GLuint); 1045 *buffer = rb->Data; 1046 return GL_TRUE; 1047 } 1048} 1049 1050 1051/** 1052 * Return the color buffer associated with an OSMesa context. 1053 * Input: c - the OSMesa context 1054 * Output: width, height - size of buffer in pixels 1055 * format - the pixel format (OSMESA_FORMAT) 1056 * buffer - pointer to color buffer values 1057 * Return: GL_TRUE or GL_FALSE to indicate success or failure. 1058 */ 1059GLAPI GLboolean GLAPIENTRY 1060OSMesaGetColorBuffer( OSMesaContext osmesa, GLint *width, 1061 GLint *height, GLint *format, void **buffer ) 1062{ 1063 if (osmesa->rb && osmesa->rb->Data) { 1064 *width = osmesa->rb->Width; 1065 *height = osmesa->rb->Height; 1066 *format = osmesa->format; 1067 *buffer = osmesa->rb->Data; 1068 return GL_TRUE; 1069 } 1070 else { 1071 *width = 0; 1072 *height = 0; 1073 *format = 0; 1074 *buffer = 0; 1075 return GL_FALSE; 1076 } 1077} 1078 1079 1080struct name_function 1081{ 1082 const char *Name; 1083 OSMESAproc Function; 1084}; 1085 1086static struct name_function functions[] = { 1087 { "OSMesaCreateContext", (OSMESAproc) OSMesaCreateContext }, 1088 { "OSMesaCreateContextExt", (OSMESAproc) OSMesaCreateContextExt }, 1089 { "OSMesaDestroyContext", (OSMESAproc) OSMesaDestroyContext }, 1090 { "OSMesaMakeCurrent", (OSMESAproc) OSMesaMakeCurrent }, 1091 { "OSMesaGetCurrentContext", (OSMESAproc) OSMesaGetCurrentContext }, 1092 { "OSMesaPixelsStore", (OSMESAproc) OSMesaPixelStore }, 1093 { "OSMesaGetIntegerv", (OSMESAproc) OSMesaGetIntegerv }, 1094 { "OSMesaGetDepthBuffer", (OSMESAproc) OSMesaGetDepthBuffer }, 1095 { "OSMesaGetColorBuffer", (OSMESAproc) OSMesaGetColorBuffer }, 1096 { "OSMesaGetProcAddress", (OSMESAproc) OSMesaGetProcAddress }, 1097 { "OSMesaColorClamp", (OSMESAproc) OSMesaColorClamp }, 1098 { NULL, NULL } 1099}; 1100 1101 1102GLAPI OSMESAproc GLAPIENTRY 1103OSMesaGetProcAddress( const char *funcName ) 1104{ 1105 int i; 1106 for (i = 0; functions[i].Name; i++) { 1107 if (strcmp(functions[i].Name, funcName) == 0) 1108 return functions[i].Function; 1109 } 1110 return _glapi_get_proc_address(funcName); 1111} 1112 1113 1114GLAPI void GLAPIENTRY 1115OSMesaColorClamp(GLboolean enable) 1116{ 1117 OSMesaContext osmesa = OSMesaGetCurrentContext(); 1118 1119 if (enable == GL_TRUE) { 1120 osmesa->mesa.Color.ClampFragmentColor = GL_TRUE; 1121 } 1122 else { 1123 osmesa->mesa.Color.ClampFragmentColor = GL_FIXED_ONLY_ARB; 1124 } 1125} 1126 1127 1128/** 1129 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in 1130 * libglapi.a. We need to define them here. 1131 */ 1132#ifdef GLX_INDIRECT_RENDERING 1133 1134#define GL_GLEXT_PROTOTYPES 1135#include "GL/gl.h" 1136#include "glapi/glapi.h" 1137#include "glapi/glapitable.h" 1138 1139#if defined(USE_MGL_NAMESPACE) 1140#define NAME(func) mgl##func 1141#else 1142#define NAME(func) gl##func 1143#endif 1144 1145#define DISPATCH(FUNC, ARGS, MESSAGE) \ 1146 GET_DISPATCH()->FUNC ARGS 1147 1148#define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \ 1149 return GET_DISPATCH()->FUNC ARGS 1150 1151/* skip normal ones */ 1152#define _GLAPI_SKIP_NORMAL_ENTRY_POINTS 1153#include "glapi/glapitemp.h" 1154 1155#endif /* GLX_INDIRECT_RENDERING */ 1156