The open source OpenXR runtime

u/aeg: Add log functions

authored by

Mateo de Mayo and committed by
Jakob Bornecrantz
bf311f3c 63b477d2

+35 -18
+35 -18
src/xrt/auxiliary/util/u_autoexpgain.c
··· 9 9 10 10 #include "math/m_api.h" 11 11 #include "util/u_autoexpgain.h" 12 + #include "util/u_debug.h" 12 13 #include "util/u_format.h" 14 + #include "util/u_logging.h" 13 15 #include "util/u_misc.h" 14 16 #include "util/u_var.h" 15 17 ··· 17 19 #include <math.h> 18 20 #include <stdint.h> 19 21 20 - #define MIN(a, b) ((a) < (b) ? (a) : (b)) 21 - #define MAX(a, b) ((a) > (b) ? (a) : (b)) 22 - #define CLAMP(X, A, B) (MIN(MAX((X), (A)), (B))) 22 + DEBUG_GET_ONCE_LOG_OPTION(aeg_log, "AEG_LOG", U_LOGGING_WARN) 23 + 24 + #define AEG_TRACE(...) U_LOG_IFL_T(aeg->log_level, __VA_ARGS__) 25 + #define AEG_DEBUG(...) U_LOG_IFL_D(aeg->log_level, __VA_ARGS__) 26 + #define AEG_INFO(...) U_LOG_IFL_I(aeg->log_level, __VA_ARGS__) 27 + #define AEG_WARN(...) U_LOG_IFL_W(aeg->log_level, __VA_ARGS__) 28 + #define AEG_ERROR(...) U_LOG_IFL_E(aeg->log_level, __VA_ARGS__) 29 + #define AEG_ASSERT(predicate, ...) \ 30 + do { \ 31 + bool p = predicate; \ 32 + if (!p) { \ 33 + U_LOG(U_LOGGING_ERROR, __VA_ARGS__); \ 34 + assert(false && "AEG_ASSERT failed: " #predicate); \ 35 + exit(EXIT_FAILURE); \ 36 + } \ 37 + } while (false); 38 + #define AEG_ASSERT_(predicate) AEG_ASSERT(predicate, "Assertion failed " #predicate) 23 39 24 40 #define LEVELS 256 //!< Possible pixel intensity values, only 8-bit supported 25 41 #define INITIAL_BRIGHTNESS 0.5 26 - #define INITIAL_MAX_BRIGHTNES_STEP 0.05 //!< 0.1 is faster but introduces oscilations more often 42 + #define INITIAL_MAX_BRIGHTNESS_STEP 0.05 //!< 0.1 is faster but introduces oscillations more often 27 43 #define INITIAL_THRESHOLD 0.1 28 44 #define GRID_COLS 40 //!< Amount of columns for the histogram sample grid 29 45 ··· 31 47 struct u_autoexpgain 32 48 { 33 49 bool enable; //!< Whether to enable auto exposure and gain adjustment 50 + 51 + enum u_logging_level log_level; 34 52 35 53 //! Algorithm strategy that affects how score and brightness are computed 36 54 enum u_aeg_strategy strategy; ··· 97 115 steps = steps_dr; 98 116 steps_count = sizeof(steps_dr) / sizeof(struct step); 99 117 } else { 100 - assert(false && "unexpected branch taken"); 118 + AEG_ASSERT(false, "Unexpected strategy=%d", aeg->strategy); 101 119 } 102 120 103 121 // Other simpler tables that might work for WMR are: ··· 105 123 // {{0, 120, 16}, {0.2, 6000, 16}, {0.9, 6000, 255}, {1.0, 9000, 255}}; 106 124 107 125 // Assertions 108 - assert(steps_count >= 2 && "Exoected at least two steps"); 109 - assert(steps[0].b == 0 && "First step should be at b=0"); 110 - assert(steps[steps_count - 1].b == 1 && "Last step should be at b=1"); 111 - assert(brightness >= 0 && brightness <= 1); 126 + AEG_ASSERT(steps_count >= 2, "Expected at least two steps but %d found", steps_count); 127 + AEG_ASSERT(steps[0].b == 0, "First step should be at b=0"); 128 + AEG_ASSERT(steps[steps_count - 1].b == 1, "Last step should be at b=1"); 129 + AEG_ASSERT(brightness >= 0 && brightness <= 1, "Invalid brightness=%f", brightness); 112 130 113 131 // Compute the piecewise function result from `steps` 114 132 float exposure = 0; ··· 140 158 } 141 159 aeg->last_brightness = brightness; 142 160 143 - float exposure = -1; 144 - float gain = -1; 145 - brightness_to_expgain(aeg, brightness, &exposure, &gain); 146 - aeg->exposure = (uint16_t)exposure; 147 - aeg->gain = (uint8_t)gain; 161 + brightness_to_expgain(aeg, brightness, &aeg->exposure, &aeg->gain); 148 162 } 149 163 150 164 //! Returns a value in the range [-1, 1] describing how dark-bright the image ··· 183 197 184 198 float score = 0; 185 199 186 - // Score that tries to make the mean reach TARGET_MEAN. 200 + // Score that tries to make the mean reach a `target_mean`. 187 201 188 202 float target_mean = -1; 189 203 if (aeg->strategy == U_AEG_STRATEGY_TRACKING) { ··· 192 206 target_mean = LEVELS / 4; 193 207 } else if (aeg->strategy == U_AEG_STRATEGY_DYNAMIC_RANGE) { 194 208 target_mean = LEVELS / 2; 209 + } else { 210 + AEG_ASSERT(false, "Unexpected strategy=%d", aeg->strategy); 195 211 } 196 212 197 213 const float range_size = mean < target_mean ? target_mean : (LEVELS - target_mean); ··· 200 216 201 217 return score; 202 218 } 203 - 204 219 205 220 static void 206 221 update_brightness(struct u_autoexpgain *aeg, struct xrt_frame *xf) ··· 240 255 struct u_autoexpgain *aeg = U_TYPED_CALLOC(struct u_autoexpgain); 241 256 242 257 aeg->enable = enabled_from_start; 258 + aeg->log_level = debug_get_log_option_aeg_log(); 243 259 aeg->strategy = strategy; 244 260 aeg->strategy_combo.count = U_AEG_STRATEGY_COUNT; 245 261 aeg->strategy_combo.options = "Tracking\0Dynamic Range\0\0"; ··· 253 269 aeg->brightness.step = 0.002; 254 270 aeg->brightness.val = INITIAL_BRIGHTNESS; 255 271 aeg->last_brightness = INITIAL_BRIGHTNESS; 256 - aeg->max_brightness_step = INITIAL_MAX_BRIGHTNES_STEP; 272 + aeg->max_brightness_step = INITIAL_MAX_BRIGHTNESS_STEP; 257 273 258 274 aeg->threshold = INITIAL_THRESHOLD; 259 275 aeg->frame_counter = 0; ··· 267 283 void 268 284 u_autoexpgain_add_vars(struct u_autoexpgain *aeg, void *root) 269 285 { 270 - u_var_add_bool(root, &aeg->enable, "Enable AEG"); 286 + u_var_add_bool(root, &aeg->enable, "Update brightness automatically"); 271 287 u_var_add_u8(root, &aeg->update_every, "Update every X frames"); 272 288 u_var_add_combo(root, &aeg->strategy_combo, "Strategy"); 273 289 u_var_add_draggable_f32(root, &aeg->brightness, "Brightness"); ··· 275 291 u_var_add_f32(root, &aeg->max_brightness_step, "Max brightness step"); 276 292 u_var_add_ro_f32(root, &aeg->current_score, "Image score"); 277 293 u_var_add_histogram_f32(root, &aeg->histogram_ui, "Intensity histogram"); 294 + u_var_add_log_level(root, &aeg->log_level, "AEG log level"); 278 295 } 279 296 280 297 void