Path: blob/21.2-virgl/src/gallium/drivers/lima/lima_util.c
4565 views
/*1* Copyright (C) 2018-2019 Lima Project2*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 shall be included in11* all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR17* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19* OTHER DEALINGS IN THE SOFTWARE.20*21*/2223#include <stdio.h>24#include <stdarg.h>25#include <time.h>2627#include <pipe/p_defines.h>2829#include "util/u_debug.h"30#include "util/u_memory.h"3132#include "lima_util.h"33#include "lima_parser.h"34#include "lima_screen.h"3536struct lima_dump {37FILE *fp;38int id;39};4041bool lima_get_absolute_timeout(uint64_t *timeout)42{43struct timespec current;44uint64_t current_ns;4546if (*timeout == PIPE_TIMEOUT_INFINITE)47return true;4849if (clock_gettime(CLOCK_MONOTONIC, ¤t))50return false;5152current_ns = ((uint64_t)current.tv_sec) * 1000000000ull;53current_ns += current.tv_nsec;54*timeout += current_ns;5556return true;57}5859static void60lima_dump_blob(FILE *fp, void *data, int size, bool is_float)61{62fprintf(fp, "{\n");63for (int i = 0; i * 4 < size; i++) {64if (i % 4 == 0)65fprintf(fp, "\t");6667if (is_float)68fprintf(fp, "%f, ", ((float *)data)[i]);69else70fprintf(fp, "0x%08x, ", ((uint32_t *)data)[i]);7172if ((i % 4 == 3) || (i == size / 4 - 1)) {73fprintf(fp, "/* 0x%08x */", MAX2((i - 3) * 4, 0));74if (i) fprintf(fp, "\n");75}76}77fprintf(fp, "}\n");78}7980void81lima_dump_vs_command_stream_print(struct lima_dump *dump, void *data,82int size, uint32_t start)83{84if (dump)85lima_parse_vs(dump->fp, (uint32_t *)data, size, start);86}8788void89lima_dump_plbu_command_stream_print(struct lima_dump *dump, void *data,90int size, uint32_t start)91{92if (dump)93lima_parse_plbu(dump->fp, (uint32_t *)data, size, start);94}9596void97lima_dump_rsw_command_stream_print(struct lima_dump *dump, void *data,98int size, uint32_t start)99{100if (dump)101lima_parse_render_state(dump->fp, (uint32_t *)data, size, start);102}103104void105lima_dump_texture_descriptor(struct lima_dump *dump, void *data,106int size, uint32_t start, uint32_t offset)107{108if (dump)109lima_parse_texture_descriptor(dump->fp, (uint32_t *)data, size, start, offset);110}111112struct lima_dump *113lima_dump_create(void)114{115static int dump_id = 0;116117if (!(lima_debug & LIMA_DEBUG_DUMP))118return NULL;119120struct lima_dump *ret = MALLOC_STRUCT(lima_dump);121if (!ret)122return NULL;123124ret->id = dump_id++;125126char buffer[PATH_MAX];127const char *dump_command = debug_get_option("LIMA_DUMP_FILE", "lima.dump");128snprintf(buffer, sizeof(buffer), "%s.staging.%04d", dump_command, ret->id);129130ret->fp = fopen(buffer, "w");131if (!ret->fp) {132fprintf(stderr, "lima: failed to open command stream log file %s\n", buffer);133FREE(ret);134return NULL;135}136137return ret;138}139140void141lima_dump_free(struct lima_dump *dump)142{143static int frame_count = 0;144145if (!dump)146return;147148fclose(dump->fp);149150/* we only know the frame count when flush, not on dump create, so first dump to a151* stage file, then move to the final file with frame count as surfix when flush.152*/153154char stage_name[PATH_MAX];155char final_name[PATH_MAX];156const char *dump_command = debug_get_option("LIMA_DUMP_FILE", "lima.dump");157snprintf(stage_name, sizeof(stage_name), "%s.staging.%04d", dump_command, dump->id);158snprintf(final_name, sizeof(final_name), "%s.%04d", dump_command, frame_count++);159160if (rename(stage_name, final_name))161fprintf(stderr, "lima: failed to rename log %s to %s\n", stage_name, final_name);162163FREE(dump);164}165166void167_lima_dump_command_stream_print(struct lima_dump *dump, void *data,168int size, bool is_float, const char *fmt, ...)169{170va_list ap;171va_start(ap, fmt);172vfprintf(dump->fp, fmt, ap);173va_end(ap);174175lima_dump_blob(dump->fp, data, size, is_float);176}177178179