home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mesa5.zip / mesa5src.zip / MesaDLL / imports.cpp < prev    next >
Text File  |  2002-12-08  |  17KB  |  730 lines

  1. /* $Id: imports.c,v 1.26 2002/12/01 13:59:11 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  5.0.1
  6.  *
  7.  * Copyright (C) 1999-2002  Brian Paul   All Rights Reserved.
  8.  *
  9.  * Permission is hereby granted, free of charge, to any person obtaining a
  10.  * copy of this software and associated documentation files (the "Software"),
  11.  * to deal in the Software without restriction, including without limitation
  12.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13.  * and/or sell copies of the Software, and to permit persons to whom the
  14.  * Software is furnished to do so, subject to the following conditions:
  15.  *
  16.  * The above copyright notice and this permission notice shall be included
  17.  * in all copies or substantial portions of the Software.
  18.  *
  19.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  */
  26.  
  27.  
  28. /*
  29.  * Imports are services which the device driver or window system or
  30.  * operating system provides to the core renderer.  The core renderer (Mesa)
  31.  * will call these functions in order to do memory allocation, simple I/O,
  32.  * etc.
  33.  *
  34.  * Some drivers will want to override/replace this file with something
  35.  * specialized, but that'll be rare.
  36.  *
  37.  * Eventually, I want to move roll the glheader.h file into this.
  38.  *
  39.  * The OpenGL SI's __GLimports structure allows per-context specification of
  40.  * replacements for the standard C lib functions.  In practice that's probably
  41.  * never needed; compile-time replacements are far more likely.
  42.  *
  43.  * The _mesa_foo() functions defined here don't in general take a context
  44.  * parameter.  I guess we can change that someday, if need be.
  45.  * So for now, the __GLimports stuff really isn't used.
  46.  */
  47.  
  48.  
  49. #include "glheader.h"
  50. #include "mtypes.h"
  51. #include "context.h"
  52. #include "imports.h"
  53.  
  54.  
  55. #define MAXSTRING 4000  /* for vsnprintf() */
  56.  
  57. #ifdef WIN32
  58. #define vsnprintf _vsnprintf
  59. #elif defined(__IBMC__) || defined(__IBMCPP__) || defined(VMS)
  60. extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
  61. #endif
  62.  
  63. #if defined(__OS2__)
  64.    FILE *fpDebugLog=NULL;
  65. #endif
  66.  
  67. /**********************************************************************/
  68. /* Wrappers for standard C library functions                          */
  69. /**********************************************************************/
  70.  
  71. /*
  72.  * Functions still needed:
  73.  * scanf
  74.  * qsort
  75.  * bsearch
  76.  * rand and RAND_MAX
  77.  */
  78.  
  79. void *
  80. _mesa_malloc(size_t bytes)
  81. {
  82. #if defined(XFree86LOADER) && defined(IN_MODULE)
  83.    return xf86malloc(bytes);
  84. #else
  85.    return malloc(bytes);
  86. #endif
  87. }
  88.  
  89.  
  90. void *
  91. _mesa_calloc(size_t bytes)
  92. {
  93. #if defined(XFree86LOADER) && defined(IN_MODULE)
  94.    return xf86calloc(1, bytes);
  95. #else
  96.    return calloc(1, bytes);
  97. #endif
  98. }
  99.  
  100.  
  101. void
  102. _mesa_free(void *ptr)
  103. {
  104. #if defined(XFree86LOADER) && defined(IN_MODULE)
  105.    xf86free(ptr);
  106. #else
  107.    free(ptr);
  108. #endif
  109. }
  110.  
  111.  
  112. void *
  113. _mesa_align_malloc(size_t bytes, unsigned long alignment)
  114. {
  115.    unsigned long ptr, buf;
  116.  
  117.    ASSERT( alignment > 0 );
  118.  
  119.    /* Allocate extra memory to accomodate rounding up the address for
  120.     * alignment and to record the real malloc address.
  121.     */
  122.    ptr = (unsigned long) _mesa_malloc(bytes + alignment + sizeof(void *));
  123.    if (!ptr)
  124.       return NULL;
  125.  
  126.    buf = (ptr + alignment + sizeof(void *)) & ~(unsigned long)(alignment - 1);
  127.    *(unsigned long *)(buf - sizeof(void *)) = ptr;
  128.  
  129. #ifdef DEBUG
  130.    /* mark the non-aligned area */
  131.    while ( ptr < buf - sizeof(void *) ) {
  132.       *(unsigned long *)ptr = 0xcdcdcdcd;
  133.       ptr += sizeof(unsigned long);
  134.    }
  135. #endif
  136.  
  137.    return (void *) buf;
  138. }
  139.  
  140.  
  141. void *
  142. _mesa_align_calloc(size_t bytes, unsigned long alignment)
  143. {
  144.    unsigned long ptr, buf;
  145.  
  146.    ASSERT( alignment > 0 );
  147.  
  148.    ptr = (unsigned long) _mesa_calloc(bytes + alignment + sizeof(void *));
  149.    if (!ptr)
  150.       return NULL;
  151.  
  152.    buf = (ptr + alignment + sizeof(void *)) & ~(unsigned long)(alignment - 1);
  153.    *(unsigned long *)(buf - sizeof(void *)) = ptr;
  154.  
  155. #ifdef DEBUG
  156.    /* mark the non-aligned area */
  157.    while ( ptr < buf - sizeof(void *) ) {
  158.       *(unsigned long *)ptr = 0xcdcdcdcd;
  159.       ptr += sizeof(unsigned long);
  160.    }
  161. #endif
  162.  
  163.    return (void *)buf;
  164. }
  165.  
  166.  
  167. void
  168. _mesa_align_free(void *ptr)
  169. {
  170. #if 0
  171.    _mesa_free( (void *)(*(unsigned long *)((unsigned long)ptr - sizeof(void *))) );
  172. #else
  173.    /* The actuall address to free is stuffed in the word immediately
  174.     * before the address the client sees.
  175.     */
  176.    void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
  177.    void *realAddr = *cubbyHole;
  178.    _mesa_free(realAddr);
  179. #endif
  180. }
  181.  
  182.  
  183. void *
  184. _mesa_memcpy(void *dest, const void *src, size_t n)
  185. {
  186. #if defined(XFree86LOADER) && defined(IN_MODULE)
  187.    return xf86memcpy(dest, src, n);
  188. #elif defined(SUNOS4)
  189.    return memcpy((char *) dest, (char *) src, (int) n);
  190. #else
  191.    return memcpy(dest, src, n);
  192. #endif
  193. }
  194.  
  195.  
  196. void
  197. _mesa_memset( void *dst, int val, size_t n )
  198. {
  199. #if defined(XFree86LOADER) && defined(IN_MODULE)
  200.    xf86memset( dst, val, n );
  201. #elif defined(SUNOS4)
  202.    memset( (char *) dst, (int) val, (int) n );
  203. #else
  204.    memset(dst, val, n);
  205. #endif
  206. }
  207.  
  208.  
  209. void
  210. _mesa_memset16( unsigned short *dst, unsigned short val, size_t n )
  211. {
  212.    while (n-- > 0)
  213.       *dst++ = val;
  214. }
  215.  
  216.  
  217. void
  218. _mesa_bzero( void *dst, size_t n )
  219. {
  220. #if defined(XFree86LOADER) && defined(IN_MODULE)
  221.    xf86memset( dst, 0, n );
  222. #elif defined(__FreeBSD__)
  223.    bzero( dst, n );
  224. #else
  225.    memset( dst, 0, n );
  226. #endif
  227. }
  228.  
  229.  
  230. double
  231. _mesa_sin(double a)
  232. {
  233. #if defined(XFree86LOADER) && defined(IN_MODULE)
  234.    return xf86sin(a);
  235. #else
  236.    return sin(a);
  237. #endif
  238. }
  239.  
  240.  
  241. double
  242. _mesa_cos(double a)
  243. {
  244. #if defined(XFree86LOADER) && defined(IN_MODULE)
  245.    return xf86cos(a);
  246. #else
  247.    return cos(a);
  248. #endif
  249. }
  250.  
  251.  
  252. double
  253. _mesa_sqrt(double x)
  254. {
  255. #if defined(XFree86LOADER) && defined(IN_MODULE)
  256.    return xf86sqrt(x);
  257. #else
  258.    return sqrt(x);
  259. #endif
  260. }
  261.  
  262.  
  263. double
  264. _mesa_pow(double x, double y)
  265. {
  266. #if defined(XFree86LOADER) && defined(IN_MODULE)
  267.    return xf86pow(x, y);
  268. #else
  269.    return pow(x, y);
  270. #endif
  271. }
  272.  
  273.  
  274. char *
  275. _mesa_getenv( const char *var )
  276. {
  277. #if defined(XFree86LOADER) && defined(IN_MODULE)
  278.    return xf86getenv(var);
  279. #else
  280.    return getenv(var);
  281. #endif
  282. }
  283.  
  284.  
  285. char *
  286. _mesa_strstr( const char *haystack, const char *needle )
  287. {
  288. #if defined(XFree86LOADER) && defined(IN_MODULE)
  289.    return xf86strstr(haystack, needle);
  290. #else
  291.    return strstr(haystack, needle);
  292. #endif
  293. }
  294.  
  295.  
  296. char *
  297. _mesa_strncat( char *dest, const char *src, size_t n )
  298. {
  299. #if defined(XFree86LOADER) && defined(IN_MODULE)
  300.    return xf86strncat(dest, src, n);
  301. #else
  302.    return strncat(dest, src, n);
  303. #endif
  304. }
  305.  
  306.  
  307. char *
  308. _mesa_strcpy( char *dest, const char *src )
  309. {
  310. #if defined(XFree86LOADER) && defined(IN_MODULE)
  311.    return xf86strcpy(dest, src);
  312. #else
  313.    return strcpy(dest, src);
  314. #endif
  315. }
  316.  
  317.  
  318. char *
  319. _mesa_strncpy( char *dest, const char *src, size_t n )
  320. {
  321. #if defined(XFree86LOADER) && defined(IN_MODULE)
  322.    return xf86strncpy(dest, src, n);
  323. #else
  324.    return strncpy(dest, src, n);
  325. #endif
  326. }
  327.  
  328.  
  329. size_t
  330. _mesa_strlen( const char *s )
  331. {
  332. #if defined(XFree86LOADER) && defined(IN_MODULE)
  333.    return xf86strlen(s);
  334. #else
  335.    return strlen(s);
  336. #endif
  337. }
  338.  
  339.  
  340. int
  341. _mesa_strcmp( const char *s1, const char *s2 )
  342. {
  343. #if defined(XFree86LOADER) && defined(IN_MODULE)
  344.    return xf86strcmp(s1, s2);
  345. #else
  346.    return strcmp(s1, s2);
  347. #endif
  348. }
  349.  
  350.  
  351. int
  352. _mesa_strncmp( const char *s1, const char *s2, size_t n )
  353. {
  354. #if defined(XFree86LOADER) && defined(IN_MODULE)
  355.    return xf86strncmp(s1, s2, n);
  356. #else
  357.    return strncmp(s1, s2, n);
  358. #endif
  359. }
  360.  
  361.  
  362. int
  363. _mesa_atoi(const char *s)
  364. {
  365. #if defined(XFree86LOADER) && defined(IN_MODULE)
  366.    return xf86atoi(s);
  367. #else
  368.    return atoi(s);
  369. #endif
  370. }
  371.  
  372.  
  373. int
  374. _mesa_sprintf( char *str, const char *fmt, ... )
  375. {
  376.    int r;
  377.    va_list args;
  378.    va_start( args, fmt );
  379.    va_end( args );
  380. #if defined(XFree86LOADER) && defined(IN_MODULE)
  381.    r = xf86vsprintf( str, fmt, args );
  382. #else
  383.    r = vsprintf( str, fmt, args );
  384. #endif
  385.    return r;
  386. }
  387.  
  388. #if defined(__OS2__)
  389. #include <time.h>
  390.  
  391. static void _Optlink _mesa_CloseDebugLog(void)
  392. { if(fpDebugLog)
  393.   fclose(fpDebugLog);
  394.   fpDebugLog = NULL;
  395. }
  396.  
  397. static int _mesa_OpenDebugLog(void)
  398. {
  399.   if(fpDebugLog == NULL)
  400.   {  struct tm *newtime;
  401.      time_t ltime;
  402.  
  403.     /* Get the time in seconds */
  404.      time(<ime);
  405.  
  406.     /* Convert it to the structure tm */
  407.     newtime = localtime(<ime);
  408.  
  409.       fpDebugLog = fopen("_aMesadebug.log","a");
  410.       fprintf(fpDebugLog, "Mesa Start debug log at %s",  asctime(newtime));
  411.       atexit(_mesa_CloseDebugLog);
  412.  }
  413.  return 0;
  414. }
  415.  
  416. #endif
  417.  
  418. void
  419. _mesa_printf( const char *fmtString, ... )
  420. {
  421.    char s[MAXSTRING];
  422.    va_list args;
  423.    va_start( args, fmtString );
  424.    vsnprintf(s, MAXSTRING, fmtString, args);
  425.    va_end( args );
  426. #if defined(XFree86LOADER) && defined(IN_MODULE)
  427.    xf86printf("%s", s);
  428. #else
  429.    printf("%s", s);
  430.  
  431. #if defined(__OS2__)
  432.       if(fpDebugLog == NULL) _mesa_OpenDebugLog();
  433.       fprintf(fpDebugLog, "%s\n", s);
  434. #endif
  435.  
  436. #endif
  437. }
  438.  
  439.  
  440. void
  441. _mesa_warning( GLcontext *ctx, const char *fmtString, ... )
  442. {
  443.    GLboolean debug;
  444.    char str[MAXSTRING];
  445.    va_list args;
  446.    (void) ctx;
  447.    va_start( args, fmtString );
  448.    (void) vsnprintf( str, MAXSTRING, fmtString, args );
  449.    va_end( args );
  450. #ifdef DEBUG
  451.    debug = GL_TRUE; /* always print warning */
  452. #else
  453.    debug = _mesa_getenv("MESA_DEBUG") ? GL_TRUE : GL_FALSE;
  454. #endif
  455.    if (debug) {
  456. #if defined(XFree86LOADER) && defined(IN_MODULE)
  457.       xf86fprintf(stderr, "Mesa warning: %s\n", str);
  458. #else
  459.       fprintf(stderr, "Mesa warning: %s\n", str);
  460.  
  461. #if defined(__OS2__)
  462.       if(fpDebugLog == NULL) _mesa_OpenDebugLog();
  463.       fprintf(fpDebugLog, "Mesa warning: %s\n", str);
  464. #endif
  465.  
  466. #endif
  467.  
  468.    }
  469. }
  470.  
  471.  
  472. /*
  473.  * This function is called when the Mesa user has stumbled into a code
  474.  * path which may not be implemented fully or correctly.
  475.  */
  476. void
  477. _mesa_problem( const GLcontext *ctx, const char *s )
  478. {
  479.    (void) ctx;
  480. #if defined(XFree86LOADER) && defined(IN_MODULE)
  481.    xf86fprintf(stderr, "Mesa implementation error: %s\n", s);
  482.    xf86fprintf(stderr, "Please report to the DRI project at dri.sourceforge.net\n");
  483. #else
  484.    fprintf(stderr, "Mesa implementation error: %s\n", s);
  485.    fprintf(stderr, "Please report to the Mesa bug database at www.mesa3d.org\n" );
  486.  
  487. #if defined(__OS2__)
  488.       if(fpDebugLog == NULL) _mesa_OpenDebugLog();
  489.       fprintf(fpDebugLog, "Mesa implementation error: %s\n", s);
  490.       fprintf(fpDebugLog, "Please report to the Mesa bug database at www.mesa3d.org\n" );
  491. #endif
  492.  
  493. #endif
  494. }
  495.  
  496.  
  497. /*
  498.  * If in debug mode, print error message to stdout.
  499.  * Also, record the error code by calling _mesa_record_error().
  500.  * Input:  ctx - the GL context
  501.  *         error - the error value
  502.  *         fmtString - printf-style format string, followed by optional args
  503.  */
  504. void
  505. _mesa_error( GLcontext *ctx, GLenum error, const char *fmtString, ... )
  506. {
  507.    const char *debugEnv;
  508.    GLboolean debug;
  509.  
  510.    debugEnv = _mesa_getenv("MESA_DEBUG");
  511.  
  512. #ifdef DEBUG
  513.    if (debugEnv && _mesa_strstr(debugEnv, "silent"))
  514.       debug = GL_FALSE;
  515.    else
  516.       debug = GL_TRUE;
  517. #else
  518.    if (debugEnv)
  519.       debug = GL_TRUE;
  520.    else
  521.       debug = GL_FALSE;
  522. #endif
  523.  
  524.    if (debug) {
  525.       va_list args;
  526.       char where[MAXSTRING];
  527.       const char *errstr;
  528.  
  529.       va_start( args, fmtString );
  530.       vsnprintf( where, MAXSTRING, fmtString, args );
  531.       va_end( args );
  532.  
  533.       switch (error) {
  534.         case GL_NO_ERROR:
  535.            errstr = "GL_NO_ERROR";
  536.            break;
  537.         case GL_INVALID_VALUE:
  538.            errstr = "GL_INVALID_VALUE";
  539.            break;
  540.         case GL_INVALID_ENUM:
  541.            errstr = "GL_INVALID_ENUM";
  542.            break;
  543.         case GL_INVALID_OPERATION:
  544.            errstr = "GL_INVALID_OPERATION";
  545.            break;
  546.         case GL_STACK_OVERFLOW:
  547.            errstr = "GL_STACK_OVERFLOW";
  548.            break;
  549.         case GL_STACK_UNDERFLOW:
  550.            errstr = "GL_STACK_UNDERFLOW";
  551.            break;
  552.         case GL_OUT_OF_MEMORY:
  553.            errstr = "GL_OUT_OF_MEMORY";
  554.            break;
  555.          case GL_TABLE_TOO_LARGE:
  556.             errstr = "GL_TABLE_TOO_LARGE";
  557.             break;
  558.         default:
  559.            errstr = "unknown";
  560.            break;
  561.       }
  562.       _mesa_debug(ctx, "Mesa user error: %s in %s\n", errstr, where);
  563.    }
  564.  
  565.    _mesa_record_error(ctx, error);
  566. }
  567.  
  568.  
  569. /*
  570.  * Call this to report debug information.  Uses stderr.
  571.  */
  572. void
  573. _mesa_debug( const GLcontext *ctx, const char *fmtString, ... )
  574. {
  575.    char s[MAXSTRING];
  576.    va_list args;
  577.    va_start(args, fmtString);
  578.    vsnprintf(s, MAXSTRING, fmtString, args);
  579.    va_end(args);
  580. #if defined(XFree86LOADER) && defined(IN_MODULE)
  581.    xf86fprintf(stderr, "Mesa: %s", s);
  582. #else
  583.  
  584.  
  585. #if defined(__OS2__)
  586.    fprintf(stderr, "Mesa: %s", s);
  587.    if(fpDebugLog == NULL) _mesa_OpenDebugLog();
  588.    fprintf(fpDebugLog, "%s", s);
  589.    fflush(fpDebugLog);
  590. #else
  591.    fprintf(stderr, "Mesa: %s", s);
  592. #endif
  593.  
  594. #endif
  595. }
  596.  
  597.  
  598.  
  599. /**********************************************************************/
  600. /* Default Imports Wrapper                                            */
  601. /**********************************************************************/
  602.  
  603. static void *
  604. default_malloc(__GLcontext *gc, size_t size)
  605. {
  606.    (void) gc;
  607.    return _mesa_malloc(size);
  608. }
  609.  
  610. static void *
  611. default_calloc(__GLcontext *gc, size_t numElem, size_t elemSize)
  612. {
  613.    (void) gc;
  614.    return _mesa_calloc(numElem * elemSize);
  615. }
  616.  
  617. static void *
  618. default_realloc(__GLcontext *gc, void *oldAddr, size_t newSize)
  619. {
  620.    (void) gc;
  621. #if defined(XFree86LOADER) && defined(IN_MODULE)
  622.    return xf86realloc(oldAddr, newSize);
  623. #else
  624.    return realloc(oldAddr, newSize);
  625. #endif
  626. }
  627.  
  628. static void
  629. default_free(__GLcontext *gc, void *addr)
  630. {
  631.    (void) gc;
  632.    _mesa_free(addr);
  633. }
  634.  
  635. static char * CAPI
  636. default_getenv( __GLcontext *gc, const char *var )
  637. {
  638.    (void) gc;
  639.    return _mesa_getenv(var);
  640. }
  641.  
  642. static void
  643. default_warning(__GLcontext *gc, char *str)
  644. {
  645.    _mesa_warning(gc, str);
  646. }
  647.  
  648. static void
  649. default_fatal(__GLcontext *gc, char *str)
  650. {
  651.    _mesa_problem(gc, str);
  652.    abort();
  653. }
  654.  
  655. static int CAPI
  656. default_atoi(__GLcontext *gc, const char *str)
  657. {
  658.    (void) gc;
  659.    return atoi(str);
  660. }
  661.  
  662. static int CAPI
  663. default_sprintf(__GLcontext *gc, char *str, const char *fmt, ...)
  664. {
  665.    int r;
  666.    va_list args;
  667.    va_start( args, fmt );
  668.    r = vsprintf( str, fmt, args );
  669.    va_end( args );
  670.    return r;
  671. }
  672.  
  673. static void * CAPI
  674. default_fopen(__GLcontext *gc, const char *path, const char *mode)
  675. {
  676.    return fopen(path, mode);
  677. }
  678.  
  679. static int CAPI
  680. default_fclose(__GLcontext *gc, void *stream)
  681. {
  682.    return fclose((FILE *) stream);
  683. }
  684.  
  685. static int CAPI
  686. default_fprintf(__GLcontext *gc, void *stream, const char *fmt, ...)
  687. {
  688.    int r;
  689.    va_list args;
  690.    va_start( args, fmt );
  691.    r = vfprintf( (FILE *) stream, fmt, args );
  692.    va_end( args );
  693.    return r;
  694. }
  695.  
  696. /* XXX this really is driver-specific and can't be here */
  697. static __GLdrawablePrivate *
  698. default_GetDrawablePrivate(__GLcontext *gc)
  699. {
  700.    return NULL;
  701. }
  702.  
  703.  
  704.  
  705.  
  706. /*
  707.  * Initialize a __GLimports object to point to the functions in
  708.  * this file.  This is to be called from device drivers.
  709.  * Input:  imports - the object to init
  710.  *         driverCtx - pointer to device driver-specific data
  711.  */
  712. void
  713. _mesa_init_default_imports(__GLimports *imports, void *driverCtx)
  714. {
  715.    imports->malloc = default_malloc;
  716.    imports->calloc = default_calloc;
  717.    imports->realloc = default_realloc;
  718.    imports->free = default_free;
  719.    imports->warning = default_warning;
  720.    imports->fatal = default_fatal;
  721.    imports->getenv = default_getenv; /* not used for now */
  722.    imports->atoi = default_atoi;
  723.    imports->sprintf = default_sprintf;
  724.    imports->fopen = default_fopen;
  725.    imports->fclose = default_fclose;
  726.    imports->fprintf = default_fprintf;
  727.    imports->getDrawablePrivate = default_GetDrawablePrivate;
  728.    imports->other = driverCtx;
  729. }
  730.