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