• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdint.h>
36 #include <string.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #define STRING_CONF_MAXLEN 1024
43 
44 /** \brief Option data types */
45 typedef enum driOptionType {
46    DRI_BOOL, DRI_ENUM, DRI_INT, DRI_FLOAT, DRI_STRING, DRI_SECTION
47 } driOptionType;
48 
49 /** \brief Option value */
50 typedef union driOptionValue {
51    unsigned char _bool; /**< \brief Boolean */
52    int _int;      /**< \brief Integer or Enum */
53    float _float;  /**< \brief Floating-point */
54    char *_string;   /**< \brief String */
55 } driOptionValue;
56 
57 /** \brief Single range of valid values
58  *
59  * For empty ranges (a single value) start == end */
60 typedef struct driOptionRange {
61    driOptionValue start; /**< \brief Start */
62    driOptionValue end;   /**< \brief End */
63 } driOptionRange;
64 
65 /** \brief Information about an option */
66 typedef struct driOptionInfo {
67    char *name;             /**< \brief Name */
68    driOptionType type;     /**< \brief Type */
69    driOptionRange range;   /**< \brief Valid range of the option (or 0:0) */
70 } driOptionInfo;
71 
72 /** \brief Option cache
73  *
74  * \li One in <driver>Screen caching option info and the default values
75  * \li One in each <driver>Context with the actual values for that context */
76 typedef struct driOptionCache {
77    driOptionInfo *info;
78    /**< \brief Array of option infos
79     *
80     * Points to the same array in the screen and all contexts */
81    driOptionValue *values;
82    /**< \brief Array of option values
83     *
84     * \li Default values in screen
85     * \li Actual values in contexts
86     */
87    unsigned int tableSize;
88    /**< \brief Size of the arrays
89     *
90     * In the current implementation it's not actually a size but log2(size).
91     * The value is the same in the screen and all contexts. */
92 } driOptionCache;
93 
94 typedef struct driEnumDescription {
95    int value;
96    const char *desc;
97 } driEnumDescription;
98 
99 /**
100  * Struct for a driver's definition of an option, its default value, and the
101  * text documenting it.
102  */
103 typedef struct driOptionDescription {
104    const char *desc;
105 
106    driOptionInfo info;
107    driOptionValue value;
108    driEnumDescription enums[4];
109 } driOptionDescription;
110 
111 /** Returns an XML string describing the options for the driver. */
112 char *
113 driGetOptionsXml(const driOptionDescription *configOptions, unsigned numOptions);
114 
115 /** \brief Parse driconf option array from configOptions
116  *
117  * To be called in <driver>CreateScreen
118  *
119  * \param info    pointer to a driOptionCache that will store the option info
120  * \param configOptions   Array of XML document describing available configuration opts
121  *
122  * For the option information to be available to external configuration tools
123  * it must be a public symbol __driConfigOptions. It is also passed as a
124  * parameter to driParseOptionInfo in order to avoid driver-independent code
125  * depending on symbols in driver-specific code. */
126 void driParseOptionInfo(driOptionCache *info,
127                         const driOptionDescription *configOptions,
128                         unsigned numOptions);
129 /** \brief Initialize option cache from info and parse configuration files
130  *
131  * To be called in <driver>CreateContext. screenNum, driverName,
132  * kernelDriverName, applicationName and engineName select device sections. */
133 void driParseConfigFiles(driOptionCache *cache, const driOptionCache *info,
134                          int screenNum, const char *driverName,
135                          const char *kernelDriverName,
136                          const char *applicationName, uint32_t applicationVersion,
137                          const char *engineName, uint32_t engineVersion);
138 /** \brief Destroy option info
139  *
140  * To be called in <driver>DestroyScreen */
141 void driDestroyOptionInfo(driOptionCache *info);
142 /** \brief Destroy option cache
143  *
144  * To be called in <driver>DestroyContext */
145 void driDestroyOptionCache(driOptionCache *cache);
146 
147 /** \brief Check if there exists a certain option */
148 unsigned char driCheckOption(const driOptionCache *cache, const char *name,
149                              driOptionType type);
150 
151 /** \brief Query a boolean option value */
152 unsigned char driQueryOptionb(const driOptionCache *cache, const char *name);
153 /** \brief Query an integer option value */
154 int driQueryOptioni(const driOptionCache *cache, const char *name);
155 /** \brief Query a floating-point option value */
156 float driQueryOptionf(const driOptionCache *cache, const char *name);
157 /** \brief Query a string option value */
158 char *driQueryOptionstr(const driOptionCache *cache, const char *name);
159 
160 /**
161  * Returns a hash of the options for this application.
162  */
163 static inline void
driComputeOptionsSha1(const driOptionCache * cache,unsigned char * sha1)164 driComputeOptionsSha1(const driOptionCache *cache, unsigned char *sha1)
165 {
166    void *ctx = ralloc_context(NULL);
167    char *dri_options = ralloc_strdup(ctx, "");
168 
169    for (int i = 0; i < 1 << cache->tableSize; i++) {
170       if (cache->info[i].name == NULL)
171          continue;
172 
173       bool ret = false;
174       switch (cache->info[i].type) {
175       case DRI_BOOL:
176          ret = ralloc_asprintf_append(&dri_options, "%s:%u,",
177                                       cache->info[i].name,
178                                       cache->values[i]._bool);
179          break;
180       case DRI_INT:
181       case DRI_ENUM:
182          ret = ralloc_asprintf_append(&dri_options, "%s:%d,",
183                                       cache->info[i].name,
184                                       cache->values[i]._int);
185          break;
186       case DRI_FLOAT:
187          ret = ralloc_asprintf_append(&dri_options, "%s:%f,",
188                                       cache->info[i].name,
189                                       cache->values[i]._float);
190          break;
191       case DRI_STRING:
192          ret = ralloc_asprintf_append(&dri_options, "%s:%s,",
193                                       cache->info[i].name,
194                                       cache->values[i]._string);
195          break;
196       default:
197          unreachable("unsupported dri config type!");
198       }
199 
200       if (!ret) {
201          break;
202       }
203    }
204 
205    _mesa_sha1_compute(dri_options, strlen(dri_options), sha1);
206    ralloc_free(ctx);
207 }
208 
209 #ifdef __cplusplus
210 } /* extern C */
211 #endif
212 
213 #endif
214