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