19f464c52Smaya/* 29f464c52Smaya * Copyright (C) 2018-2019 Lima Project 39f464c52Smaya * 49f464c52Smaya * Permission is hereby granted, free of charge, to any person obtaining a 59f464c52Smaya * copy of this software and associated documentation files (the "Software"), 69f464c52Smaya * to deal in the Software without restriction, including without limitation 79f464c52Smaya * the rights to use, copy, modify, merge, publish, distribute, sublicense, 89f464c52Smaya * and/or sell copies of the Software, and to permit persons to whom the 99f464c52Smaya * Software is furnished to do so, subject to the following conditions: 109f464c52Smaya * 119f464c52Smaya * The above copyright notice and this permission notice shall be included in 129f464c52Smaya * all copies or substantial portions of the Software. 139f464c52Smaya * 149f464c52Smaya * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 159f464c52Smaya * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 169f464c52Smaya * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 179f464c52Smaya * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 189f464c52Smaya * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 199f464c52Smaya * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 209f464c52Smaya * OTHER DEALINGS IN THE SOFTWARE. 219f464c52Smaya * 229f464c52Smaya */ 239f464c52Smaya 249f464c52Smaya#include <stdio.h> 259f464c52Smaya#include <stdarg.h> 269f464c52Smaya#include <time.h> 279f464c52Smaya 289f464c52Smaya#include <pipe/p_defines.h> 299f464c52Smaya 307ec681f3Smrg#include "util/u_debug.h" 317ec681f3Smrg#include "util/u_memory.h" 327ec681f3Smrg 339f464c52Smaya#include "lima_util.h" 347ec681f3Smrg#include "lima_parser.h" 357ec681f3Smrg#include "lima_screen.h" 369f464c52Smaya 377ec681f3Smrgstruct lima_dump { 387ec681f3Smrg FILE *fp; 397ec681f3Smrg int id; 407ec681f3Smrg}; 419f464c52Smaya 429f464c52Smayabool lima_get_absolute_timeout(uint64_t *timeout) 439f464c52Smaya{ 449f464c52Smaya struct timespec current; 459f464c52Smaya uint64_t current_ns; 469f464c52Smaya 479f464c52Smaya if (*timeout == PIPE_TIMEOUT_INFINITE) 489f464c52Smaya return true; 499f464c52Smaya 509f464c52Smaya if (clock_gettime(CLOCK_MONOTONIC, ¤t)) 519f464c52Smaya return false; 529f464c52Smaya 539f464c52Smaya current_ns = ((uint64_t)current.tv_sec) * 1000000000ull; 549f464c52Smaya current_ns += current.tv_nsec; 559f464c52Smaya *timeout += current_ns; 569f464c52Smaya 579f464c52Smaya return true; 589f464c52Smaya} 599f464c52Smaya 607ec681f3Smrgstatic void 617ec681f3Smrglima_dump_blob(FILE *fp, void *data, int size, bool is_float) 629f464c52Smaya{ 637ec681f3Smrg fprintf(fp, "{\n"); 649f464c52Smaya for (int i = 0; i * 4 < size; i++) { 657ec681f3Smrg if (i % 4 == 0) 667ec681f3Smrg fprintf(fp, "\t"); 679f464c52Smaya 689f464c52Smaya if (is_float) 697ec681f3Smrg fprintf(fp, "%f, ", ((float *)data)[i]); 709f464c52Smaya else 717ec681f3Smrg fprintf(fp, "0x%08x, ", ((uint32_t *)data)[i]); 727ec681f3Smrg 737ec681f3Smrg if ((i % 4 == 3) || (i == size / 4 - 1)) { 747ec681f3Smrg fprintf(fp, "/* 0x%08x */", MAX2((i - 3) * 4, 0)); 757ec681f3Smrg if (i) fprintf(fp, "\n"); 767ec681f3Smrg } 779f464c52Smaya } 787ec681f3Smrg fprintf(fp, "}\n"); 799f464c52Smaya} 809f464c52Smaya 819f464c52Smayavoid 827ec681f3Smrglima_dump_shader(struct lima_dump *dump, void *data, int size, bool is_frag) 839f464c52Smaya{ 847ec681f3Smrg if (dump) 857ec681f3Smrg lima_parse_shader(dump->fp, (uint32_t *)data, size, is_frag); 867ec681f3Smrg} 877ec681f3Smrg 887ec681f3Smrgvoid 897ec681f3Smrglima_dump_vs_command_stream_print(struct lima_dump *dump, void *data, 907ec681f3Smrg int size, uint32_t start) 917ec681f3Smrg{ 927ec681f3Smrg if (dump) 937ec681f3Smrg lima_parse_vs(dump->fp, (uint32_t *)data, size, start); 947ec681f3Smrg} 957ec681f3Smrg 967ec681f3Smrgvoid 977ec681f3Smrglima_dump_plbu_command_stream_print(struct lima_dump *dump, void *data, 987ec681f3Smrg int size, uint32_t start) 997ec681f3Smrg{ 1007ec681f3Smrg if (dump) 1017ec681f3Smrg lima_parse_plbu(dump->fp, (uint32_t *)data, size, start); 1027ec681f3Smrg} 1037ec681f3Smrg 1047ec681f3Smrgvoid 1057ec681f3Smrglima_dump_rsw_command_stream_print(struct lima_dump *dump, void *data, 1067ec681f3Smrg int size, uint32_t start) 1077ec681f3Smrg{ 1087ec681f3Smrg if (dump) 1097ec681f3Smrg lima_parse_render_state(dump->fp, (uint32_t *)data, size, start); 1107ec681f3Smrg} 1117ec681f3Smrg 1127ec681f3Smrgvoid 1137ec681f3Smrglima_dump_texture_descriptor(struct lima_dump *dump, void *data, 1147ec681f3Smrg int size, uint32_t start, uint32_t offset) 1157ec681f3Smrg{ 1167ec681f3Smrg if (dump) 1177ec681f3Smrg lima_parse_texture_descriptor(dump->fp, (uint32_t *)data, size, start, offset); 1187ec681f3Smrg} 1199f464c52Smaya 1207ec681f3Smrgstruct lima_dump * 1217ec681f3Smrglima_dump_create(void) 1227ec681f3Smrg{ 1237ec681f3Smrg static int dump_id = 0; 1247ec681f3Smrg 1257ec681f3Smrg if (!(lima_debug & LIMA_DEBUG_DUMP)) 1267ec681f3Smrg return NULL; 1277ec681f3Smrg 1287ec681f3Smrg struct lima_dump *ret = MALLOC_STRUCT(lima_dump); 1297ec681f3Smrg if (!ret) 1307ec681f3Smrg return NULL; 1317ec681f3Smrg 1327ec681f3Smrg ret->id = dump_id++; 1337ec681f3Smrg 1347ec681f3Smrg char buffer[PATH_MAX]; 1357ec681f3Smrg const char *dump_command = debug_get_option("LIMA_DUMP_FILE", "lima.dump"); 1367ec681f3Smrg snprintf(buffer, sizeof(buffer), "%s.staging.%04d", dump_command, ret->id); 1377ec681f3Smrg 1387ec681f3Smrg ret->fp = fopen(buffer, "w"); 1397ec681f3Smrg if (!ret->fp) { 1407ec681f3Smrg fprintf(stderr, "lima: failed to open command stream log file %s\n", buffer); 1417ec681f3Smrg FREE(ret); 1427ec681f3Smrg return NULL; 1439f464c52Smaya } 1447ec681f3Smrg 1457ec681f3Smrg return ret; 1467ec681f3Smrg} 1477ec681f3Smrg 1487ec681f3Smrgvoid 1497ec681f3Smrglima_dump_free(struct lima_dump *dump) 1507ec681f3Smrg{ 1517ec681f3Smrg static int frame_count = 0; 1527ec681f3Smrg 1537ec681f3Smrg if (!dump) 1547ec681f3Smrg return; 1557ec681f3Smrg 1567ec681f3Smrg fclose(dump->fp); 1577ec681f3Smrg 1587ec681f3Smrg /* we only know the frame count when flush, not on dump create, so first dump to a 1597ec681f3Smrg * stage file, then move to the final file with frame count as surfix when flush. 1607ec681f3Smrg */ 1617ec681f3Smrg 1627ec681f3Smrg char stage_name[PATH_MAX]; 1637ec681f3Smrg char final_name[PATH_MAX]; 1647ec681f3Smrg const char *dump_command = debug_get_option("LIMA_DUMP_FILE", "lima.dump"); 1657ec681f3Smrg snprintf(stage_name, sizeof(stage_name), "%s.staging.%04d", dump_command, dump->id); 1667ec681f3Smrg snprintf(final_name, sizeof(final_name), "%s.%04d", dump_command, frame_count++); 1677ec681f3Smrg 1687ec681f3Smrg if (rename(stage_name, final_name)) 1697ec681f3Smrg fprintf(stderr, "lima: failed to rename log %s to %s\n", stage_name, final_name); 1707ec681f3Smrg 1717ec681f3Smrg FREE(dump); 1727ec681f3Smrg} 1737ec681f3Smrg 1747ec681f3Smrgvoid 1757ec681f3Smrg_lima_dump_command_stream_print(struct lima_dump *dump, void *data, 1767ec681f3Smrg int size, bool is_float, const char *fmt, ...) 1777ec681f3Smrg{ 1787ec681f3Smrg va_list ap; 1797ec681f3Smrg va_start(ap, fmt); 1807ec681f3Smrg vfprintf(dump->fp, fmt, ap); 1817ec681f3Smrg va_end(ap); 1827ec681f3Smrg 1837ec681f3Smrg lima_dump_blob(dump->fp, data, size, is_float); 1849f464c52Smaya} 185