Path: blob/21.2-virgl/src/vulkan/overlay-layer/overlay_params.c
7178 views
/*1* Copyright © 2019 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include <stdio.h>24#include <string.h>25#include <stdlib.h>26#include <string.h>27#include <errno.h>2829#include "overlay_params.h"3031#include "util/os_socket.h"3233static enum overlay_param_position34parse_position(const char *str)35{36if (!str || !strcmp(str, "top-left"))37return LAYER_POSITION_TOP_LEFT;38if (!strcmp(str, "top-right"))39return LAYER_POSITION_TOP_RIGHT;40if (!strcmp(str, "bottom-left"))41return LAYER_POSITION_BOTTOM_LEFT;42if (!strcmp(str, "bottom-right"))43return LAYER_POSITION_BOTTOM_RIGHT;44return LAYER_POSITION_TOP_LEFT;45}4647static FILE *48parse_output_file(const char *str)49{50return fopen(str, "w+");51}5253static int54parse_control(const char *str)55{56int ret = os_socket_listen_abstract(str, 1);57if (ret < 0) {58fprintf(stderr, "ERROR: Couldn't create socket pipe at '%s'\n", str);59fprintf(stderr, "ERROR: '%s'\n", strerror(errno));60return ret;61}6263os_socket_block(ret, false);6465return ret;66}6768static uint32_t69parse_fps_sampling_period(const char *str)70{71return strtol(str, NULL, 0) * 1000;72}7374static bool75parse_no_display(const char *str)76{77return strtol(str, NULL, 0) != 0;78}7980static unsigned81parse_unsigned(const char *str)82{83return strtol(str, NULL, 0);84}8586#define parse_width(s) parse_unsigned(s)87#define parse_height(s) parse_unsigned(s)8889static bool90parse_help(const char *str)91{92fprintf(stderr, "Layer params using VK_LAYER_MESA_OVERLAY_CONFIG=\n");93#define OVERLAY_PARAM_BOOL(name) \94fprintf(stderr, "\t%s=0|1\n", #name);95#define OVERLAY_PARAM_CUSTOM(name)96OVERLAY_PARAMS97#undef OVERLAY_PARAM_BOOL98#undef OVERLAY_PARAM_CUSTOM99fprintf(stderr, "\tposition=top-left|top-right|bottom-left|bottom-right\n");100fprintf(stderr, "\tfps_sampling_period=number-of-milliseconds\n");101fprintf(stderr, "\tno_display=0|1\n");102fprintf(stderr, "\toutput_file=/path/to/output.txt\n");103fprintf(stderr, "\twidth=width-in-pixels\n");104fprintf(stderr, "\theight=height-in-pixels\n");105106return true;107}108109static bool is_delimiter(char c)110{111return c == 0 || c == ',' || c == ':' || c == ';' || c == '=';112}113114static int115parse_string(const char *s, char *out_param, char *out_value)116{117int i = 0;118119for (; !is_delimiter(*s); s++, out_param++, i++)120*out_param = *s;121122*out_param = 0;123124if (*s == '=') {125s++;126i++;127for (; !is_delimiter(*s); s++, out_value++, i++)128*out_value = *s;129} else130*(out_value++) = '1';131*out_value = 0;132133if (*s && is_delimiter(*s)) {134s++;135i++;136}137138if (*s && !i) {139fprintf(stderr, "mesa-overlay: syntax error: unexpected '%c' (%i) while "140"parsing a string\n", *s, *s);141fflush(stderr);142}143144return i;145}146147const char *overlay_param_names[] = {148#define OVERLAY_PARAM_BOOL(name) #name,149#define OVERLAY_PARAM_CUSTOM(name)150OVERLAY_PARAMS151#undef OVERLAY_PARAM_BOOL152#undef OVERLAY_PARAM_CUSTOM153};154155void156parse_overlay_env(struct overlay_params *params,157const char *env)158{159uint32_t num;160char key[256], value[256];161162memset(params, 0, sizeof(*params));163164/* Visible by default */165params->enabled[OVERLAY_PARAM_ENABLED_fps] = true;166params->enabled[OVERLAY_PARAM_ENABLED_frame_timing] = true;167params->enabled[OVERLAY_PARAM_ENABLED_device] = true;168params->enabled[OVERLAY_PARAM_ENABLED_format] = true;169params->fps_sampling_period = 500000; /* 500ms */170params->width = params->height = 300;171params->control = -1;172173if (!env)174return;175176while ((num = parse_string(env, key, value)) != 0) {177env += num;178179#define OVERLAY_PARAM_BOOL(name) \180if (!strcmp(#name, key)) { \181params->enabled[OVERLAY_PARAM_ENABLED_##name] = \182strtol(value, NULL, 0); \183continue; \184}185#define OVERLAY_PARAM_CUSTOM(name) \186if (!strcmp(#name, key)) { \187params->name = parse_##name(value); \188continue; \189}190OVERLAY_PARAMS191#undef OVERLAY_PARAM_BOOL192#undef OVERLAY_PARAM_CUSTOM193fprintf(stderr, "Unknown option '%s'\n", key);194}195}196197198