swr: [rasterizer] Add string knob type
[mesa.git] / src / gallium / drivers / swr / rasterizer / core / knobs_init.h
1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * @file knobs_init.h
24 *
25 * @brief Dynamic Knobs Initialization for Core.
26 *
27 ******************************************************************************/
28 #pragma once
29
30 #include <core/knobs.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <stdio.h>
35
36 // Assume the type is compatible with a 32-bit integer
37 template <typename T>
38 static inline void ConvertEnvToKnob(const char* pOverride, T& knobValue)
39 {
40 uint32_t value = 0;
41 if (sscanf(pOverride, "%u", &value))
42 {
43 knobValue = static_cast<T>(value);
44 }
45 }
46
47 static inline void ConvertEnvToKnob(const char* pOverride, bool& knobValue)
48 {
49 size_t len = strlen(pOverride);
50 if (len == 1)
51 {
52 auto c = tolower(pOverride[0]);
53 if (c == 'y' || c == 't' || c == '1')
54 {
55 knobValue = true;
56 return;
57 }
58 if (c == 'n' || c == 'f' || c == '0')
59 {
60 knobValue = false;
61 return;
62 }
63 }
64
65 // Try converting to a number and casting to bool
66 uint32_t value = 0;
67 if (sscanf(pOverride, "%u", &value))
68 {
69 knobValue = value != 0;
70 return;
71 }
72 }
73
74 static inline void ConvertEnvToKnob(const char* pOverride, float& knobValue)
75 {
76 float value = knobValue;
77 if (sscanf(pOverride, "%f", &value))
78 {
79 knobValue = value;
80 }
81 }
82
83 static inline void ConvertEnvToKnob(const char* pOverride, std::string& knobValue)
84 {
85 knobValue = pOverride;
86 }
87
88 template <typename T>
89 static inline void InitKnob(T& knob)
90 {
91
92 // TODO, read registry first
93
94 // Second, read environment variables
95 const char* pOverride = getenv(knob.Name());
96
97 if (pOverride)
98 {
99 auto knobValue = knob.Value();
100 ConvertEnvToKnob(pOverride, knobValue);
101 knob.Value(knobValue);
102 }
103 }