1/*
2 * Copyright (C) 2018-2019 Lima Project
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 shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23
24#include <stdio.h>
25#include <stdarg.h>
26#include <time.h>
27
28#include <pipe/p_defines.h>
29
30#include "util/u_debug.h"
31#include "util/u_memory.h"
32
33#include "lima_util.h"
34#include "lima_parser.h"
35#include "lima_screen.h"
36
37struct lima_dump {
38   FILE *fp;
39   int id;
40};
41
42bool lima_get_absolute_timeout(uint64_t *timeout)
43{
44   struct timespec current;
45   uint64_t current_ns;
46
47   if (*timeout == PIPE_TIMEOUT_INFINITE)
48      return true;
49
50   if (clock_gettime(CLOCK_MONOTONIC, &current))
51      return false;
52
53   current_ns = ((uint64_t)current.tv_sec) * 1000000000ull;
54   current_ns += current.tv_nsec;
55   *timeout += current_ns;
56
57   return true;
58}
59
60static void
61lima_dump_blob(FILE *fp, void *data, int size, bool is_float)
62{
63   fprintf(fp, "{\n");
64   for (int i = 0; i * 4 < size; i++) {
65      if (i % 4 == 0)
66         fprintf(fp, "\t");
67
68      if (is_float)
69         fprintf(fp, "%f, ", ((float *)data)[i]);
70      else
71         fprintf(fp, "0x%08x, ", ((uint32_t *)data)[i]);
72
73      if ((i % 4 == 3) || (i == size / 4 - 1)) {
74         fprintf(fp, "/* 0x%08x */", MAX2((i - 3) * 4, 0));
75         if (i) fprintf(fp, "\n");
76      }
77   }
78   fprintf(fp, "}\n");
79}
80
81void
82lima_dump_shader(struct lima_dump *dump, void *data, int size, bool is_frag)
83{
84   if (dump)
85      lima_parse_shader(dump->fp, (uint32_t *)data, size, is_frag);
86}
87
88void
89lima_dump_vs_command_stream_print(struct lima_dump *dump, void *data,
90                                  int size, uint32_t start)
91{
92   if (dump)
93      lima_parse_vs(dump->fp, (uint32_t *)data, size, start);
94}
95
96void
97lima_dump_plbu_command_stream_print(struct lima_dump *dump, void *data,
98                                    int size, uint32_t start)
99{
100   if (dump)
101      lima_parse_plbu(dump->fp, (uint32_t *)data, size, start);
102}
103
104void
105lima_dump_rsw_command_stream_print(struct lima_dump *dump, void *data,
106                                   int size, uint32_t start)
107{
108   if (dump)
109      lima_parse_render_state(dump->fp, (uint32_t *)data, size, start);
110}
111
112void
113lima_dump_texture_descriptor(struct lima_dump *dump, void *data,
114                             int size, uint32_t start, uint32_t offset)
115{
116   if (dump)
117      lima_parse_texture_descriptor(dump->fp, (uint32_t *)data, size, start, offset);
118}
119
120struct lima_dump *
121lima_dump_create(void)
122{
123   static int dump_id = 0;
124
125   if (!(lima_debug & LIMA_DEBUG_DUMP))
126      return NULL;
127
128   struct lima_dump *ret = MALLOC_STRUCT(lima_dump);
129   if (!ret)
130      return NULL;
131
132   ret->id = dump_id++;
133
134   char buffer[PATH_MAX];
135   const char *dump_command = debug_get_option("LIMA_DUMP_FILE", "lima.dump");
136   snprintf(buffer, sizeof(buffer), "%s.staging.%04d", dump_command, ret->id);
137
138   ret->fp = fopen(buffer, "w");
139   if (!ret->fp) {
140      fprintf(stderr, "lima: failed to open command stream log file %s\n", buffer);
141      FREE(ret);
142      return NULL;
143   }
144
145   return ret;
146}
147
148void
149lima_dump_free(struct lima_dump *dump)
150{
151   static int frame_count = 0;
152
153   if (!dump)
154      return;
155
156   fclose(dump->fp);
157
158   /* we only know the frame count when flush, not on dump create, so first dump to a
159    * stage file, then move to the final file with frame count as surfix when flush.
160    */
161
162   char stage_name[PATH_MAX];
163   char final_name[PATH_MAX];
164   const char *dump_command = debug_get_option("LIMA_DUMP_FILE", "lima.dump");
165   snprintf(stage_name, sizeof(stage_name), "%s.staging.%04d", dump_command, dump->id);
166   snprintf(final_name, sizeof(final_name), "%s.%04d", dump_command, frame_count++);
167
168   if (rename(stage_name, final_name))
169      fprintf(stderr, "lima: failed to rename log %s to %s\n", stage_name, final_name);
170
171   FREE(dump);
172}
173
174void
175_lima_dump_command_stream_print(struct lima_dump *dump, void *data,
176                                int size, bool is_float, const char *fmt, ...)
177{
178   va_list ap;
179   va_start(ap, fmt);
180   vfprintf(dump->fp, fmt, ap);
181   va_end(ap);
182
183   lima_dump_blob(dump->fp, data, size, is_float);
184}
185