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