2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * Off-Screen Mesa rendering / Rendering into client memory space
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.
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/mipmap.h"
45 #include "main/mtypes.h"
46 #include "main/renderbuffer.h"
47 #include "swrast/swrast.h"
48 #include "swrast_setup/swrast_setup.h"
49 #include "swrast/s_context.h"
50 #include "swrast/s_lines.h"
51 #include "swrast/s_renderbuffer.h"
52 #include "swrast/s_triangle.h"
54 #include "tnl/t_context.h"
55 #include "tnl/t_pipeline.h"
56 #include "drivers/common/driverfuncs.h"
57 #include "drivers/common/meta.h"
61 #define OSMESA_RENDERBUFFER_CLASS 0x053
65 * OSMesa rendering context, derived from core Mesa struct gl_context.
69 struct gl_context mesa; /*< Base class - this must be first */
70 struct gl_config *gl_visual; /*< Describes the buffers */
71 struct swrast_renderbuffer *srb; /*< The user's colorbuffer */
72 struct gl_framebuffer *gl_buffer; /*< The framebuffer, containing user's rb */
73 GLenum format; /*< User-specified context format */
74 GLint userRowLength; /*< user-specified number of pixels per row */
75 GLint rInd, gInd, bInd, aInd;/*< index offsets for RGBA formats */
76 GLvoid *rowaddr[SWRAST_MAX_HEIGHT]; /*< address of first pixel in each image row */
77 GLboolean yup; /*< TRUE -> Y increases upward */
78 /*< FALSE -> Y increases downward */
83 static INLINE OSMesaContext
84 OSMESA_CONTEXT(struct gl_context *ctx)
86 /* Just cast, since we're using structure containment */
87 return (OSMesaContext) ctx;
91 /**********************************************************************/
92 /*** Private Device Driver Functions ***/
93 /**********************************************************************/
96 static const GLubyte *
97 get_string( struct gl_context *ctx, GLenum name )
103 return (const GLubyte *) "Mesa OffScreen32";
104 #elif CHAN_BITS == 16
105 return (const GLubyte *) "Mesa OffScreen16";
107 return (const GLubyte *) "Mesa OffScreen";
116 osmesa_update_state( struct gl_context *ctx, GLuint new_state )
118 /* easy - just propogate */
119 _swrast_InvalidateState( ctx, new_state );
120 _swsetup_InvalidateState( ctx, new_state );
121 _tnl_InvalidateState( ctx, new_state );
122 _vbo_InvalidateState( ctx, new_state );
128 * Macros for optimized line/triangle rendering.
129 * Only for 8-bit channel, RGBA, BGRA, ARGB formats.
132 #define PACK_RGBA(DST, R, G, B, A) \
134 (DST)[osmesa->rInd] = R; \
135 (DST)[osmesa->gInd] = G; \
136 (DST)[osmesa->bInd] = B; \
137 (DST)[osmesa->aInd] = A; \
140 #define PIXELADDR4(X,Y) ((GLchan *) osmesa->rowaddr[Y] + 4 * (X))
144 * Draw a flat-shaded, RGB line into an osmesa buffer.
146 #define NAME flat_rgba_line
149 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
150 const GLchan *color = vert1->color;
154 GLchan *p = PIXELADDR4(X, Y); \
155 PACK_RGBA(p, color[0], color[1], color[2], color[3]); \
158 #include "swrast/s_linetemp.h"
163 * Draw a flat-shaded, Z-less, RGB line into an osmesa buffer.
165 #define NAME flat_rgba_z_line
168 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
170 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
171 const GLchan *color = vert1->color;
176 GLchan *p = PIXELADDR4(X, Y); \
177 PACK_RGBA(p, color[RCOMP], color[GCOMP], \
178 color[BCOMP], color[ACOMP]); \
183 #include "swrast/s_linetemp.h"
188 * Analyze context state to see if we can provide a fast line drawing
189 * function. Otherwise, return NULL.
191 static swrast_line_func
192 osmesa_choose_line_function( struct gl_context *ctx )
194 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
195 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
197 if (ctx->RenderMode != GL_RENDER) return NULL;
198 if (ctx->Line.SmoothFlag) return NULL;
199 if (ctx->Texture._EnabledUnits) return NULL;
200 if (ctx->Light.ShadeModel != GL_FLAT) return NULL;
201 if (ctx->Line.Width != 1.0F) return NULL;
202 if (ctx->Line.StippleFlag) return NULL;
203 if (ctx->Line.SmoothFlag) return NULL;
204 if (osmesa->format != OSMESA_RGBA &&
205 osmesa->format != OSMESA_BGRA &&
206 osmesa->format != OSMESA_ARGB) return NULL;
208 if (swrast->_RasterMask==DEPTH_BIT
209 && ctx->Depth.Func==GL_LESS
210 && ctx->Depth.Mask==GL_TRUE
211 && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS) {
212 return (swrast_line_func) flat_rgba_z_line;
215 if (swrast->_RasterMask == 0) {
216 return (swrast_line_func) flat_rgba_line;
219 return (swrast_line_func) NULL;
223 /**********************************************************************/
224 /***** Optimized triangle rendering *****/
225 /**********************************************************************/
229 * Smooth-shaded, z-less triangle, RGBA color.
231 #define NAME smooth_rgba_z_triangle
233 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
235 #define INTERP_ALPHA 1
237 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
238 #define RENDER_SPAN( span ) { \
240 GLchan *img = PIXELADDR4(span.x, span.y); \
241 for (i = 0; i < span.end; i++, img += 4) { \
242 const GLuint z = FixedToDepth(span.z); \
244 PACK_RGBA(img, FixedToChan(span.red), \
245 FixedToChan(span.green), FixedToChan(span.blue), \
246 FixedToChan(span.alpha)); \
249 span.red += span.redStep; \
250 span.green += span.greenStep; \
251 span.blue += span.blueStep; \
252 span.alpha += span.alphaStep; \
253 span.z += span.zStep; \
256 #include "swrast/s_tritemp.h"
261 * Flat-shaded, z-less triangle, RGBA color.
263 #define NAME flat_rgba_z_triangle
265 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
267 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx); \
269 PACK_RGBA((GLchan *) &pixel, v2->color[0], v2->color[1], \
270 v2->color[2], v2->color[3]);
272 #define RENDER_SPAN( span ) { \
274 GLuint *img = (GLuint *) PIXELADDR4(span.x, span.y); \
275 for (i = 0; i < span.end; i++) { \
276 const GLuint z = FixedToDepth(span.z); \
281 span.z += span.zStep; \
285 #include "swrast/s_tritemp.h"
290 * Return pointer to an optimized triangle function if possible.
292 static swrast_tri_func
293 osmesa_choose_triangle_function( struct gl_context *ctx )
295 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
296 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
298 if (ctx->RenderMode != GL_RENDER) return (swrast_tri_func) NULL;
299 if (ctx->Polygon.SmoothFlag) return (swrast_tri_func) NULL;
300 if (ctx->Polygon.StippleFlag) return (swrast_tri_func) NULL;
301 if (ctx->Texture._EnabledUnits) return (swrast_tri_func) NULL;
302 if (osmesa->format != OSMESA_RGBA &&
303 osmesa->format != OSMESA_BGRA &&
304 osmesa->format != OSMESA_ARGB) return (swrast_tri_func) NULL;
305 if (ctx->Polygon.CullFlag &&
306 ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)
307 return (swrast_tri_func) NULL;
309 if (swrast->_RasterMask == DEPTH_BIT &&
310 ctx->Depth.Func == GL_LESS &&
311 ctx->Depth.Mask == GL_TRUE &&
312 ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS) {
313 if (ctx->Light.ShadeModel == GL_SMOOTH) {
314 return (swrast_tri_func) smooth_rgba_z_triangle;
317 return (swrast_tri_func) flat_rgba_z_triangle;
320 return (swrast_tri_func) NULL;
325 /* Override for the swrast triangle-selection function. Try to use one
326 * of our internal triangle functions, otherwise fall back to the
327 * standard swrast functions.
330 osmesa_choose_triangle( struct gl_context *ctx )
332 SWcontext *swrast = SWRAST_CONTEXT(ctx);
334 swrast->Triangle = osmesa_choose_triangle_function( ctx );
335 if (!swrast->Triangle)
336 _swrast_choose_triangle( ctx );
340 osmesa_choose_line( struct gl_context *ctx )
342 SWcontext *swrast = SWRAST_CONTEXT(ctx);
344 swrast->Line = osmesa_choose_line_function( ctx );
346 _swrast_choose_line( ctx );
352 * Recompute the values of the context's rowaddr array.
355 compute_row_addresses( OSMesaContext osmesa )
357 GLint bytesPerRow, i;
358 GLubyte *origin = (GLubyte *) osmesa->srb->Buffer;
359 GLint rowlength; /* in pixels */
360 GLint height = osmesa->srb->Base.Height;
362 if (osmesa->userRowLength)
363 rowlength = osmesa->userRowLength;
365 rowlength = osmesa->srb->Base.Width;
367 bytesPerRow = rowlength * _mesa_get_format_bytes(osmesa->srb->Base.Format);
370 /* Y=0 is bottom line of window */
371 for (i = 0; i < height; i++) {
372 osmesa->rowaddr[i] = (GLvoid *) ((GLubyte *) origin + i * bytesPerRow);
376 /* Y=0 is top line of window */
377 for (i = 0; i < height; i++) {
378 GLint j = height - i - 1;
379 osmesa->rowaddr[i] = (GLvoid *) ((GLubyte *) origin + j * bytesPerRow);
387 * Don't use _mesa_delete_renderbuffer since we can't free rb->Buffer.
390 osmesa_delete_renderbuffer(struct gl_renderbuffer *rb)
397 * Allocate renderbuffer storage. We don't actually allocate any storage
398 * since we're using a user-provided buffer.
399 * Just set up all the gl_renderbuffer methods.
402 osmesa_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
403 GLenum internalFormat, GLuint width, GLuint height)
405 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
407 /* Note: we can ignoring internalFormat for "window-system" renderbuffers */
408 (void) internalFormat;
410 /* Given the user-provided format and type, figure out which MESA_FORMAT_x
412 * XXX There aren't Mesa formats for all the possible combinations here!
413 * XXX Specifically, there's only RGBA-order 16-bit/channel and float
415 * XXX The 8-bit/channel formats should all be OK.
417 if (osmesa->format == OSMESA_RGBA) {
418 if (osmesa->DataType == GL_UNSIGNED_BYTE) {
419 if (_mesa_little_endian())
420 rb->Format = MESA_FORMAT_RGBA8888_REV;
422 rb->Format = MESA_FORMAT_RGBA8888;
424 else if (osmesa->DataType == GL_UNSIGNED_SHORT) {
425 rb->Format = MESA_FORMAT_RGBA_16;
428 rb->Format = MESA_FORMAT_RGBA_FLOAT32;
431 else if (osmesa->format == OSMESA_BGRA) {
432 if (osmesa->DataType == GL_UNSIGNED_BYTE) {
433 if (_mesa_little_endian())
434 rb->Format = MESA_FORMAT_ARGB8888;
436 rb->Format = MESA_FORMAT_ARGB8888_REV;
438 else if (osmesa->DataType == GL_UNSIGNED_SHORT) {
439 _mesa_warning(ctx, "Unsupported OSMesa format BGRA/GLushort");
440 rb->Format = MESA_FORMAT_RGBA_16; /* not exactly right */
443 _mesa_warning(ctx, "Unsupported OSMesa format BGRA/GLfloat");
444 rb->Format = MESA_FORMAT_RGBA_FLOAT32; /* not exactly right */
447 else if (osmesa->format == OSMESA_ARGB) {
448 if (osmesa->DataType == GL_UNSIGNED_BYTE) {
449 if (_mesa_little_endian())
450 rb->Format = MESA_FORMAT_ARGB8888_REV;
452 rb->Format = MESA_FORMAT_ARGB8888;
454 else if (osmesa->DataType == GL_UNSIGNED_SHORT) {
455 _mesa_warning(ctx, "Unsupported OSMesa format ARGB/GLushort");
456 rb->Format = MESA_FORMAT_RGBA_16; /* not exactly right */
459 _mesa_warning(ctx, "Unsupported OSMesa format ARGB/GLfloat");
460 rb->Format = MESA_FORMAT_RGBA_FLOAT32; /* not exactly right */
463 else if (osmesa->format == OSMESA_RGB) {
464 if (osmesa->DataType == GL_UNSIGNED_BYTE) {
465 rb->Format = MESA_FORMAT_RGB888;
467 else if (osmesa->DataType == GL_UNSIGNED_SHORT) {
468 _mesa_warning(ctx, "Unsupported OSMesa format RGB/GLushort");
469 rb->Format = MESA_FORMAT_RGBA_16; /* not exactly right */
472 _mesa_warning(ctx, "Unsupported OSMesa format RGB/GLfloat");
473 rb->Format = MESA_FORMAT_RGBA_FLOAT32; /* not exactly right */
476 else if (osmesa->format == OSMESA_BGR) {
477 if (osmesa->DataType == GL_UNSIGNED_BYTE) {
478 rb->Format = MESA_FORMAT_BGR888;
480 else if (osmesa->DataType == GL_UNSIGNED_SHORT) {
481 _mesa_warning(ctx, "Unsupported OSMesa format BGR/GLushort");
482 rb->Format = MESA_FORMAT_RGBA_16; /* not exactly right */
485 _mesa_warning(ctx, "Unsupported OSMesa format BGR/GLfloat");
486 rb->Format = MESA_FORMAT_RGBA_FLOAT32; /* not exactly right */
489 else if (osmesa->format == OSMESA_RGB_565) {
490 ASSERT(osmesa->DataType == GL_UNSIGNED_BYTE);
491 rb->Format = MESA_FORMAT_RGB565;
494 _mesa_problem(ctx, "bad pixel format in osmesa renderbuffer_storage");
500 compute_row_addresses( osmesa );
507 * Allocate a new renderbuffer to describe the user-provided color buffer.
509 static struct swrast_renderbuffer *
510 new_osmesa_renderbuffer(struct gl_context *ctx, GLenum format, GLenum type)
512 const GLuint name = 0;
513 struct swrast_renderbuffer *srb = CALLOC_STRUCT(swrast_renderbuffer);
516 _mesa_init_renderbuffer(&srb->Base, name);
518 srb->Base.ClassID = OSMESA_RENDERBUFFER_CLASS;
519 srb->Base.RefCount = 1;
520 srb->Base.Delete = osmesa_delete_renderbuffer;
521 srb->Base.AllocStorage = osmesa_renderbuffer_storage;
523 srb->Base.InternalFormat = GL_RGBA;
524 srb->Base._BaseFormat = GL_RGBA;
534 osmesa_MapRenderbuffer(struct gl_context *ctx,
535 struct gl_renderbuffer *rb,
536 GLuint x, GLuint y, GLuint w, GLuint h,
538 GLubyte **mapOut, GLint *rowStrideOut)
540 const OSMesaContext osmesa = OSMESA_CONTEXT(ctx);
542 if (rb->ClassID == OSMESA_RENDERBUFFER_CLASS) {
543 /* this is an OSMesa renderbuffer which wraps user memory */
544 struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
545 const GLuint bpp = _mesa_get_format_bytes(rb->Format);
546 GLint rowStride; /* in bytes */
548 if (osmesa->userRowLength)
549 rowStride = osmesa->userRowLength * bpp;
551 rowStride = rb->Width * bpp;
554 /* Y=0 is top line of window */
555 y = rb->Height - y - 1;
556 *rowStrideOut = -rowStride;
559 *rowStrideOut = rowStride;
562 *mapOut = (GLubyte *) srb->Buffer + y * rowStride + x * bpp;
565 _swrast_map_soft_renderbuffer(ctx, rb, x, y, w, h, mode,
566 mapOut, rowStrideOut);
572 osmesa_UnmapRenderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
574 if (rb->ClassID == OSMESA_RENDERBUFFER_CLASS) {
578 _swrast_unmap_soft_renderbuffer(ctx, rb);
583 /**********************************************************************/
584 /***** Public Functions *****/
585 /**********************************************************************/
589 * Create an Off-Screen Mesa rendering context. The only attribute needed is
590 * an RGBA vs Color-Index mode flag.
592 * Input: format - Must be GL_RGBA
593 * sharelist - specifies another OSMesaContext with which to share
594 * display lists. NULL indicates no sharing.
595 * Return: an OSMesaContext or 0 if error
597 GLAPI OSMesaContext GLAPIENTRY
598 OSMesaCreateContext( GLenum format, OSMesaContext sharelist )
600 return OSMesaCreateContextExt(format, DEFAULT_SOFTWARE_DEPTH_BITS,
609 * Create context and specify size of ancillary buffers.
611 GLAPI OSMesaContext GLAPIENTRY
612 OSMesaCreateContextExt( GLenum format, GLint depthBits, GLint stencilBits,
613 GLint accumBits, OSMesaContext sharelist )
615 OSMesaContext osmesa;
616 struct dd_function_table functions;
617 GLint rind, gind, bind, aind;
618 GLint redBits = 0, greenBits = 0, blueBits = 0, alphaBits =0;
620 rind = gind = bind = aind = 0;
621 if (format==OSMESA_RGBA) {
623 greenBits = CHAN_BITS;
624 blueBits = CHAN_BITS;
625 alphaBits = CHAN_BITS;
631 else if (format==OSMESA_BGRA) {
633 greenBits = CHAN_BITS;
634 blueBits = CHAN_BITS;
635 alphaBits = CHAN_BITS;
641 else if (format==OSMESA_ARGB) {
643 greenBits = CHAN_BITS;
644 blueBits = CHAN_BITS;
645 alphaBits = CHAN_BITS;
651 else if (format==OSMESA_RGB) {
653 greenBits = CHAN_BITS;
654 blueBits = CHAN_BITS;
660 else if (format==OSMESA_BGR) {
662 greenBits = CHAN_BITS;
663 blueBits = CHAN_BITS;
669 #if CHAN_TYPE == GL_UNSIGNED_BYTE
670 else if (format==OSMESA_RGB_565) {
675 rind = 0; /* not used */
684 osmesa = (OSMesaContext) CALLOC_STRUCT(osmesa_context);
686 osmesa->gl_visual = _mesa_create_visual( GL_FALSE, /* double buffer */
687 GL_FALSE, /* stereo */
697 alphaBits ? accumBits : 0,
700 if (!osmesa->gl_visual) {
705 /* Initialize device driver function table */
706 _mesa_init_driver_functions(&functions);
707 /* override with our functions */
708 functions.GetString = get_string;
709 functions.UpdateState = osmesa_update_state;
710 functions.GetBufferSize = NULL;
712 if (!_mesa_initialize_context(&osmesa->mesa,
715 sharelist ? &sharelist->mesa
716 : (struct gl_context *) NULL,
717 &functions, (void *) osmesa)) {
718 _mesa_destroy_visual( osmesa->gl_visual );
723 _mesa_enable_sw_extensions(&(osmesa->mesa));
724 _mesa_enable_1_3_extensions(&(osmesa->mesa));
725 _mesa_enable_1_4_extensions(&(osmesa->mesa));
726 _mesa_enable_1_5_extensions(&(osmesa->mesa));
727 _mesa_enable_2_0_extensions(&(osmesa->mesa));
728 _mesa_enable_2_1_extensions(&(osmesa->mesa));
730 osmesa->gl_buffer = _mesa_create_framebuffer(osmesa->gl_visual);
731 if (!osmesa->gl_buffer) {
732 _mesa_destroy_visual( osmesa->gl_visual );
733 _mesa_free_context_data( &osmesa->mesa );
738 /* Create depth/stencil/accum buffers. We'll create the color
739 * buffer later in OSMesaMakeCurrent().
741 _swrast_add_soft_renderbuffers(osmesa->gl_buffer,
742 GL_FALSE, /* color */
743 osmesa->gl_visual->haveDepthBuffer,
744 osmesa->gl_visual->haveStencilBuffer,
745 osmesa->gl_visual->haveAccumBuffer,
746 GL_FALSE, /* alpha */
747 GL_FALSE /* aux */ );
749 osmesa->format = format;
750 osmesa->userRowLength = 0;
751 osmesa->yup = GL_TRUE;
757 _mesa_meta_init(&osmesa->mesa);
759 /* Initialize the software rasterizer and helper modules. */
761 struct gl_context *ctx = &osmesa->mesa;
765 if (!_swrast_CreateContext( ctx ) ||
766 !_vbo_CreateContext( ctx ) ||
767 !_tnl_CreateContext( ctx ) ||
768 !_swsetup_CreateContext( ctx )) {
769 _mesa_destroy_visual(osmesa->gl_visual);
770 _mesa_free_context_data(ctx);
775 _swsetup_Wakeup( ctx );
777 /* use default TCL pipeline */
778 tnl = TNL_CONTEXT(ctx);
779 tnl->Driver.RunPipeline = _tnl_run_pipeline;
781 ctx->Driver.MapRenderbuffer = osmesa_MapRenderbuffer;
782 ctx->Driver.UnmapRenderbuffer = osmesa_UnmapRenderbuffer;
784 ctx->Driver.GenerateMipmap = _mesa_generate_mipmap;
786 /* Extend the software rasterizer with our optimized line and triangle
789 swrast = SWRAST_CONTEXT( ctx );
790 swrast->choose_line = osmesa_choose_line;
791 swrast->choose_triangle = osmesa_choose_triangle;
799 * Destroy an Off-Screen Mesa rendering context.
801 * \param osmesa the context to destroy
803 GLAPI void GLAPIENTRY
804 OSMesaDestroyContext( OSMesaContext osmesa )
808 _mesa_reference_renderbuffer((struct gl_renderbuffer **) &osmesa->srb, NULL);
810 _mesa_meta_free( &osmesa->mesa );
812 _swsetup_DestroyContext( &osmesa->mesa );
813 _tnl_DestroyContext( &osmesa->mesa );
814 _vbo_DestroyContext( &osmesa->mesa );
815 _swrast_DestroyContext( &osmesa->mesa );
817 _mesa_destroy_visual( osmesa->gl_visual );
818 _mesa_reference_framebuffer( &osmesa->gl_buffer, NULL );
820 _mesa_free_context_data( &osmesa->mesa );
827 * Bind an OSMesaContext to an image buffer. The image buffer is just a
828 * block of memory which the client provides. Its size must be at least
829 * as large as width*height*sizeof(type). Its address should be a multiple
830 * of 4 if using RGBA mode.
832 * Image data is stored in the order of glDrawPixels: row-major order
833 * with the lower-left image pixel stored in the first array position
834 * (ie. bottom-to-top).
836 * If the context's viewport hasn't been initialized yet, it will now be
837 * initialized to (0,0,width,height).
839 * Input: osmesa - the rendering context
840 * buffer - the image buffer memory
841 * type - data type for pixel components
842 * Normally, only GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5
843 * are supported. But if Mesa's been compiled with CHAN_BITS==16
844 * then type may be GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE. And if
845 * Mesa's been build with CHAN_BITS==32 then type may be GL_FLOAT,
846 * GL_UNSIGNED_SHORT or GL_UNSIGNED_BYTE.
847 * width, height - size of image buffer in pixels, at least 1
848 * Return: GL_TRUE if success, GL_FALSE if error because of invalid osmesa,
849 * invalid buffer address, invalid type, width<1, height<1,
850 * width>internal limit or height>internal limit.
852 GLAPI GLboolean GLAPIENTRY
853 OSMesaMakeCurrent( OSMesaContext osmesa, void *buffer, GLenum type,
854 GLsizei width, GLsizei height )
856 if (!osmesa || !buffer ||
857 width < 1 || height < 1 ||
858 width > SWRAST_MAX_WIDTH || height > SWRAST_MAX_HEIGHT) {
862 if (osmesa->format == OSMESA_RGB_565 && type != GL_UNSIGNED_SHORT_5_6_5) {
867 if (!(type == GL_UNSIGNED_BYTE ||
868 (type == GL_UNSIGNED_SHORT && CHAN_BITS >= 16) ||
869 (type == GL_FLOAT && CHAN_BITS == 32))) {
870 /* i.e. is sizeof(type) * 8 > CHAN_BITS? */
875 osmesa_update_state( &osmesa->mesa, 0 );
877 /* Call this periodically to detect when the user has begun using
878 * GL rendering from multiple threads.
880 _glapi_check_multithread();
883 /* Create a front/left color buffer which wraps the user-provided buffer.
884 * There is no back color buffer.
885 * If the user tries to use a 8, 16 or 32-bit/channel buffer that
886 * doesn't match what Mesa was compiled for (CHAN_BITS) the
887 * _mesa_add_renderbuffer() function will create a "wrapper" renderbuffer
888 * that converts rendering from CHAN_BITS to the user-requested channel
892 osmesa->srb = new_osmesa_renderbuffer(&osmesa->mesa, osmesa->format, type);
893 _mesa_remove_renderbuffer(osmesa->gl_buffer, BUFFER_FRONT_LEFT);
894 _mesa_add_renderbuffer(osmesa->gl_buffer, BUFFER_FRONT_LEFT,
896 assert(osmesa->srb->Base.RefCount == 2);
899 osmesa->DataType = type;
901 /* Set renderbuffer fields. Set width/height = 0 to force
902 * osmesa_renderbuffer_storage() being called by _mesa_resize_framebuffer()
904 osmesa->srb->Buffer = buffer;
905 osmesa->srb->Base.Width = osmesa->srb->Base.Height = 0;
907 /* Set the framebuffer's size. This causes the
908 * osmesa_renderbuffer_storage() function to get called.
910 _mesa_resize_framebuffer(&osmesa->mesa, osmesa->gl_buffer, width, height);
911 osmesa->gl_buffer->Initialized = GL_TRUE; /* XXX TEMPORARY? */
913 _mesa_make_current( &osmesa->mesa, osmesa->gl_buffer, osmesa->gl_buffer );
915 /* Remove renderbuffer attachment, then re-add. This installs the
916 * renderbuffer adaptor/wrapper if needed (for bpp conversion).
918 _mesa_remove_renderbuffer(osmesa->gl_buffer, BUFFER_FRONT_LEFT);
919 _mesa_add_renderbuffer(osmesa->gl_buffer, BUFFER_FRONT_LEFT,
923 /* this updates the visual's red/green/blue/alphaBits fields */
924 _mesa_update_framebuffer_visual(&osmesa->mesa, osmesa->gl_buffer);
926 /* update the framebuffer size */
927 _mesa_resize_framebuffer(&osmesa->mesa, osmesa->gl_buffer, width, height);
934 GLAPI OSMesaContext GLAPIENTRY
935 OSMesaGetCurrentContext( void )
937 struct gl_context *ctx = _mesa_get_current_context();
939 return (OSMesaContext) ctx;
946 GLAPI void GLAPIENTRY
947 OSMesaPixelStore( GLint pname, GLint value )
949 OSMesaContext osmesa = OSMesaGetCurrentContext();
952 case OSMESA_ROW_LENGTH:
954 _mesa_error( &osmesa->mesa, GL_INVALID_VALUE,
955 "OSMesaPixelStore(value)" );
958 osmesa->userRowLength = value;
961 osmesa->yup = value ? GL_TRUE : GL_FALSE;
964 _mesa_error( &osmesa->mesa, GL_INVALID_ENUM, "OSMesaPixelStore(pname)" );
968 compute_row_addresses( osmesa );
972 GLAPI void GLAPIENTRY
973 OSMesaGetIntegerv( GLint pname, GLint *value )
975 OSMesaContext osmesa = OSMesaGetCurrentContext();
979 if (osmesa->gl_buffer)
980 *value = osmesa->gl_buffer->Width;
985 if (osmesa->gl_buffer)
986 *value = osmesa->gl_buffer->Height;
991 *value = osmesa->format;
994 /* current color buffer's data type */
995 *value = osmesa->DataType;
997 case OSMESA_ROW_LENGTH:
998 *value = osmesa->userRowLength;
1001 *value = osmesa->yup;
1003 case OSMESA_MAX_WIDTH:
1004 *value = SWRAST_MAX_WIDTH;
1006 case OSMESA_MAX_HEIGHT:
1007 *value = SWRAST_MAX_HEIGHT;
1010 _mesa_error(&osmesa->mesa, GL_INVALID_ENUM, "OSMesaGetIntergerv(pname)");
1017 * Return the depth buffer associated with an OSMesa context.
1018 * Input: c - the OSMesa context
1019 * Output: width, height - size of buffer in pixels
1020 * bytesPerValue - bytes per depth value (2 or 4)
1021 * buffer - pointer to depth buffer values
1022 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1024 GLAPI GLboolean GLAPIENTRY
1025 OSMesaGetDepthBuffer( OSMesaContext c, GLint *width, GLint *height,
1026 GLint *bytesPerValue, void **buffer )
1028 struct swrast_renderbuffer *srb = NULL;
1031 srb = swrast_renderbuffer(c->gl_buffer->
1032 Attachment[BUFFER_DEPTH].Renderbuffer);
1034 if (!srb || !srb->Buffer) {
1042 *width = srb->Base.Width;
1043 *height = srb->Base.Height;
1044 if (c->gl_visual->depthBits <= 16)
1045 *bytesPerValue = sizeof(GLushort);
1047 *bytesPerValue = sizeof(GLuint);
1048 *buffer = (void *) srb->Buffer;
1055 * Return the color buffer associated with an OSMesa context.
1056 * Input: c - the OSMesa context
1057 * Output: width, height - size of buffer in pixels
1058 * format - the pixel format (OSMESA_FORMAT)
1059 * buffer - pointer to color buffer values
1060 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1062 GLAPI GLboolean GLAPIENTRY
1063 OSMesaGetColorBuffer( OSMesaContext osmesa, GLint *width,
1064 GLint *height, GLint *format, void **buffer )
1066 if (osmesa->srb && osmesa->srb->Buffer) {
1067 *width = osmesa->srb->Base.Width;
1068 *height = osmesa->srb->Base.Height;
1069 *format = osmesa->format;
1070 *buffer = (void *) osmesa->srb->Buffer;
1083 struct name_function
1086 OSMESAproc Function;
1089 static struct name_function functions[] = {
1090 { "OSMesaCreateContext", (OSMESAproc) OSMesaCreateContext },
1091 { "OSMesaCreateContextExt", (OSMESAproc) OSMesaCreateContextExt },
1092 { "OSMesaDestroyContext", (OSMESAproc) OSMesaDestroyContext },
1093 { "OSMesaMakeCurrent", (OSMESAproc) OSMesaMakeCurrent },
1094 { "OSMesaGetCurrentContext", (OSMESAproc) OSMesaGetCurrentContext },
1095 { "OSMesaPixelsStore", (OSMESAproc) OSMesaPixelStore },
1096 { "OSMesaGetIntegerv", (OSMESAproc) OSMesaGetIntegerv },
1097 { "OSMesaGetDepthBuffer", (OSMESAproc) OSMesaGetDepthBuffer },
1098 { "OSMesaGetColorBuffer", (OSMESAproc) OSMesaGetColorBuffer },
1099 { "OSMesaGetProcAddress", (OSMESAproc) OSMesaGetProcAddress },
1100 { "OSMesaColorClamp", (OSMESAproc) OSMesaColorClamp },
1105 GLAPI OSMESAproc GLAPIENTRY
1106 OSMesaGetProcAddress( const char *funcName )
1109 for (i = 0; functions[i].Name; i++) {
1110 if (strcmp(functions[i].Name, funcName) == 0)
1111 return functions[i].Function;
1113 return _glapi_get_proc_address(funcName);
1117 GLAPI void GLAPIENTRY
1118 OSMesaColorClamp(GLboolean enable)
1120 OSMesaContext osmesa = OSMesaGetCurrentContext();
1122 if (enable == GL_TRUE) {
1123 osmesa->mesa.Color.ClampFragmentColor = GL_TRUE;
1126 osmesa->mesa.Color.ClampFragmentColor = GL_FIXED_ONLY_ARB;
1132 * When GLX_INDIRECT_RENDERING is defined, some symbols are missing in
1133 * libglapi.a. We need to define them here.
1135 #ifdef GLX_INDIRECT_RENDERING
1137 #define GL_GLEXT_PROTOTYPES
1139 #include "glapi/glapi.h"
1140 #include "glapi/glapitable.h"
1142 #if defined(USE_MGL_NAMESPACE)
1143 #define NAME(func) mgl##func
1145 #define NAME(func) gl##func
1148 #define DISPATCH(FUNC, ARGS, MESSAGE) \
1149 GET_DISPATCH()->FUNC ARGS
1151 #define RETURN_DISPATCH(FUNC, ARGS, MESSAGE) \
1152 return GET_DISPATCH()->FUNC ARGS
1154 /* skip normal ones */
1155 #define _GLAPI_SKIP_NORMAL_ENTRY_POINTS
1156 #include "glapi/glapitemp.h"
1158 #endif /* GLX_INDIRECT_RENDERING */