Home | History | Annotate | Download | only in util
      1 /*
      2  * XML DRI client-side driver configuration
      3  * Copyright (C) 2003 Felix Kuehling
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a
      6  * copy of this software and associated documentation files (the "Software"),
      7  * to deal in the Software without restriction, including without limitation
      8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      9  * and/or sell copies of the Software, and to permit persons to whom the
     10  * Software is furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice shall be included
     13  * in all copies or substantial portions of the Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18  * FELIX KUEHLING, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
     19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
     21  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     22  *
     23  */
     24 /**
     25  * \file xmlconfig.h
     26  * \brief Driver-independent client-side part of the XML configuration
     27  * \author Felix Kuehling
     28  */
     29 
     30 #ifndef __XMLCONFIG_H
     31 #define __XMLCONFIG_H
     32 
     33 #include "util/mesa-sha1.h"
     34 #include "util/ralloc.h"
     35 
     36 #define STRING_CONF_MAXLEN 25
     37 
     38 /** \brief Option data types */
     39 typedef enum driOptionType {
     40     DRI_BOOL, DRI_ENUM, DRI_INT, DRI_FLOAT, DRI_STRING
     41 } driOptionType;
     42 
     43 /** \brief Option value */
     44 typedef union driOptionValue {
     45     unsigned char _bool; /**< \brief Boolean */
     46     int _int;      /**< \brief Integer or Enum */
     47     float _float;  /**< \brief Floating-point */
     48     char *_string;   /**< \brief String */
     49 } driOptionValue;
     50 
     51 /** \brief Single range of valid values
     52  *
     53  * For empty ranges (a single value) start == end */
     54 typedef struct driOptionRange {
     55     driOptionValue start; /**< \brief Start */
     56     driOptionValue end;   /**< \brief End */
     57 } driOptionRange;
     58 
     59 /** \brief Information about an option */
     60 typedef struct driOptionInfo {
     61     char *name;             /**< \brief Name */
     62     driOptionType type;     /**< \brief Type */
     63     driOptionRange *ranges; /**< \brief Array of ranges */
     64     unsigned int nRanges;   /**< \brief Number of ranges */
     65 } driOptionInfo;
     66 
     67 /** \brief Option cache
     68  *
     69  * \li One in <driver>Screen caching option info and the default values
     70  * \li One in each <driver>Context with the actual values for that context */
     71 typedef struct driOptionCache {
     72     driOptionInfo *info;
     73   /**< \brief Array of option infos
     74    *
     75    * Points to the same array in the screen and all contexts */
     76     driOptionValue *values;
     77   /**< \brief Array of option values
     78    *
     79    * \li Default values in screen
     80    * \li Actual values in contexts
     81    */
     82     unsigned int tableSize;
     83   /**< \brief Size of the arrays
     84    *
     85    * In the current implementation it's not actually a size but log2(size).
     86    * The value is the same in the screen and all contexts. */
     87 } driOptionCache;
     88 
     89 /** \brief Parse XML option info from configOptions
     90  *
     91  * To be called in <driver>CreateScreen
     92  *
     93  * \param info    pointer to a driOptionCache that will store the option info
     94  * \param configOptions   XML document describing available configuration opts
     95  *
     96  * For the option information to be available to external configuration tools
     97  * it must be a public symbol __driConfigOptions. It is also passed as a
     98  * parameter to driParseOptionInfo in order to avoid driver-independent code
     99  * depending on symbols in driver-specific code. */
    100 void driParseOptionInfo (driOptionCache *info,
    101 			 const char *configOptions);
    102 /** \brief Initialize option cache from info and parse configuration files
    103  *
    104  * To be called in <driver>CreateContext. screenNum and driverName select
    105  * device sections. */
    106 void driParseConfigFiles (driOptionCache *cache, const driOptionCache *info,
    107 			  int screenNum, const char *driverName);
    108 /** \brief Destroy option info
    109  *
    110  * To be called in <driver>DestroyScreen */
    111 void driDestroyOptionInfo (driOptionCache *info);
    112 /** \brief Destroy option cache
    113  *
    114  * To be called in <driver>DestroyContext */
    115 void driDestroyOptionCache (driOptionCache *cache);
    116 
    117 /** \brief Check if there exists a certain option */
    118 unsigned char driCheckOption (const driOptionCache *cache, const char *name,
    119 			  driOptionType type);
    120 
    121 /** \brief Query a boolean option value */
    122 unsigned char driQueryOptionb (const driOptionCache *cache, const char *name);
    123 /** \brief Query an integer option value */
    124 int driQueryOptioni (const driOptionCache *cache, const char *name);
    125 /** \brief Query a floating-point option value */
    126 float driQueryOptionf (const driOptionCache *cache, const char *name);
    127 /** \brief Query a string option value */
    128 char *driQueryOptionstr (const driOptionCache *cache, const char *name);
    129 
    130 /**
    131  * Returns a hash of the options for this application.
    132  */
    133 static inline void
    134 driComputeOptionsSha1(const driOptionCache *cache, unsigned char *sha1)
    135 {
    136    void *ctx = ralloc_context(NULL);
    137    char *dri_options = ralloc_strdup(ctx, "");
    138 
    139    for (int i = 0; i < 1 << cache->tableSize; i++) {
    140       if (cache->info[i].name == NULL)
    141          continue;
    142 
    143       bool ret = false;
    144       switch (cache->info[i].type) {
    145       case DRI_BOOL:
    146          ret = ralloc_asprintf_append(&dri_options, "%s:%u,",
    147                                       cache->info[i].name,
    148                                       cache->values[i]._bool);
    149          break;
    150       case DRI_INT:
    151       case DRI_ENUM:
    152          ret = ralloc_asprintf_append(&dri_options, "%s:%d,",
    153                                       cache->info[i].name,
    154                                       cache->values[i]._int);
    155          break;
    156       case DRI_FLOAT:
    157          ret = ralloc_asprintf_append(&dri_options, "%s:%f,",
    158                                       cache->info[i].name,
    159                                       cache->values[i]._float);
    160          break;
    161       case DRI_STRING:
    162          ret = ralloc_asprintf_append(&dri_options, "%s:%s,",
    163                                       cache->info[i].name,
    164                                       cache->values[i]._string);
    165          break;
    166       default:
    167          unreachable("unsupported dri config type!");
    168       }
    169 
    170       if (!ret) {
    171          break;
    172       }
    173    }
    174 
    175    _mesa_sha1_compute(dri_options, strlen(dri_options), sha1);
    176    ralloc_free(ctx);
    177 }
    178 
    179 #endif
    180