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[5];
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 *deviceName,
137                          const char *applicationName, uint32_t applicationVersion,
138                          const char *engineName, uint32_t engineVersion);
139 /** \brief Destroy option info
140  *
141  * To be called in <driver>DestroyScreen */
142 void driDestroyOptionInfo(driOptionCache *info);
143 /** \brief Destroy option cache
144  *
145  * To be called in <driver>DestroyContext */
146 void driDestroyOptionCache(driOptionCache *cache);
147 
148 /** \brief Check if there exists a certain option */
149 unsigned char driCheckOption(const driOptionCache *cache, const char *name,
150                              driOptionType type);
151 
152 /** \brief Query a boolean option value */
153 unsigned char driQueryOptionb(const driOptionCache *cache, const char *name);
154 /** \brief Query an integer option value */
155 int driQueryOptioni(const driOptionCache *cache, const char *name);
156 /** \brief Query a floating-point option value */
157 float driQueryOptionf(const driOptionCache *cache, const char *name);
158 /** \brief Query a string option value */
159 char *driQueryOptionstr(const driOptionCache *cache, const char *name);
160 
161 /* Overrides for the unit tests to control drirc parsing. */
162 void driInjectDataDir(const char *dir);
163 void driInjectExecName(const char *exec);
164 
165 /**
166  * Returns a hash of the options for this application.
167  */
168 static inline void
driComputeOptionsSha1(const driOptionCache * cache,unsigned char * sha1)169 driComputeOptionsSha1(const driOptionCache *cache, unsigned char *sha1)
170 {
171    void *ctx = ralloc_context(NULL);
172    char *dri_options = ralloc_strdup(ctx, "");
173 
174    for (int i = 0; i < 1 << cache->tableSize; i++) {
175       if (cache->info[i].name == NULL)
176          continue;
177 
178       bool ret = false;
179       switch (cache->info[i].type) {
180       case DRI_BOOL:
181          ret = ralloc_asprintf_append(&dri_options, "%s:%u,",
182                                       cache->info[i].name,
183                                       cache->values[i]._bool);
184          break;
185       case DRI_INT:
186       case DRI_ENUM:
187          ret = ralloc_asprintf_append(&dri_options, "%s:%d,",
188                                       cache->info[i].name,
189                                       cache->values[i]._int);
190          break;
191       case DRI_FLOAT:
192          ret = ralloc_asprintf_append(&dri_options, "%s:%f,",
193                                       cache->info[i].name,
194                                       cache->values[i]._float);
195          break;
196       case DRI_STRING:
197          ret = ralloc_asprintf_append(&dri_options, "%s:%s,",
198                                       cache->info[i].name,
199                                       cache->values[i]._string);
200          break;
201       default:
202          unreachable("unsupported dri config type!");
203       }
204 
205       if (!ret) {
206          break;
207       }
208    }
209 
210    _mesa_sha1_compute(dri_options, strlen(dri_options), sha1);
211    ralloc_free(ctx);
212 }
213 
214 #ifdef __cplusplus
215 } /* extern C */
216 #endif
217 
218 #endif
219