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 #include "util/box.h"
33 #include "pipe/p_state.h"
34
35 #include "lima_util.h"
36 #include "lima_parser.h"
37 #include "lima_screen.h"
38
39 struct lima_dump {
40 FILE *fp;
41 int id;
42 };
43
44 static void
lima_dump_blob(FILE * fp,void * data,int size,bool is_float)45 lima_dump_blob(FILE *fp, void *data, int size, bool is_float)
46 {
47 fprintf(fp, "{\n");
48 for (int i = 0; i * 4 < size; i++) {
49 if (i % 4 == 0)
50 fprintf(fp, "\t");
51
52 if (is_float)
53 fprintf(fp, "%f, ", ((float *)data)[i]);
54 else
55 fprintf(fp, "0x%08x, ", ((uint32_t *)data)[i]);
56
57 if ((i % 4 == 3) || (i == size / 4 - 1)) {
58 fprintf(fp, "/* 0x%08x */", MAX2((i - 3) * 4, 0));
59 if (i) fprintf(fp, "\n");
60 }
61 }
62 fprintf(fp, "}\n");
63 }
64
65 void
lima_dump_shader(struct lima_dump * dump,void * data,int size,bool is_frag)66 lima_dump_shader(struct lima_dump *dump, void *data, int size, bool is_frag)
67 {
68 if (dump)
69 lima_parse_shader(dump->fp, (uint32_t *)data, size, is_frag);
70 }
71
72 void
lima_dump_vs_command_stream_print(struct lima_dump * dump,void * data,int size,uint32_t start)73 lima_dump_vs_command_stream_print(struct lima_dump *dump, void *data,
74 int size, uint32_t start)
75 {
76 if (dump)
77 lima_parse_vs(dump->fp, (uint32_t *)data, size, start);
78 }
79
80 void
lima_dump_plbu_command_stream_print(struct lima_dump * dump,void * data,int size,uint32_t start)81 lima_dump_plbu_command_stream_print(struct lima_dump *dump, void *data,
82 int size, uint32_t start)
83 {
84 if (dump)
85 lima_parse_plbu(dump->fp, (uint32_t *)data, size, start);
86 }
87
88 void
lima_dump_rsw_command_stream_print(struct lima_dump * dump,void * data,int size,uint32_t start)89 lima_dump_rsw_command_stream_print(struct lima_dump *dump, void *data,
90 int size, uint32_t start)
91 {
92 if (dump)
93 lima_parse_render_state(dump->fp, (uint32_t *)data, size, start);
94 }
95
96 void
lima_dump_texture_descriptor(struct lima_dump * dump,void * data,int size,uint32_t start,uint32_t offset)97 lima_dump_texture_descriptor(struct lima_dump *dump, void *data,
98 int size, uint32_t start, uint32_t offset)
99 {
100 if (dump)
101 lima_parse_texture_descriptor(dump->fp, (uint32_t *)data, size, start, offset);
102 }
103
104 struct lima_dump *
lima_dump_create(void)105 lima_dump_create(void)
106 {
107 static int dump_id = 0;
108
109 if (!(lima_debug & LIMA_DEBUG_DUMP))
110 return NULL;
111
112 struct lima_dump *ret = MALLOC_STRUCT(lima_dump);
113 if (!ret)
114 return NULL;
115
116 ret->id = dump_id++;
117
118 char buffer[PATH_MAX];
119 const char *dump_command = debug_get_option("LIMA_DUMP_FILE", "lima.dump");
120 snprintf(buffer, sizeof(buffer), "%s.staging.%04d", dump_command, ret->id);
121
122 ret->fp = fopen(buffer, "w");
123 if (!ret->fp) {
124 fprintf(stderr, "lima: failed to open command stream log file %s\n", buffer);
125 FREE(ret);
126 return NULL;
127 }
128
129 return ret;
130 }
131
132 void
lima_dump_free(struct lima_dump * dump)133 lima_dump_free(struct lima_dump *dump)
134 {
135 static int frame_count = 0;
136
137 if (!dump)
138 return;
139
140 fclose(dump->fp);
141
142 /* we only know the frame count when flush, not on dump create, so first dump to a
143 * stage file, then move to the final file with frame count as surfix when flush.
144 */
145
146 char stage_name[PATH_MAX];
147 char final_name[PATH_MAX];
148 const char *dump_command = debug_get_option("LIMA_DUMP_FILE", "lima.dump");
149 snprintf(stage_name, sizeof(stage_name), "%s.staging.%04d", dump_command, dump->id);
150 snprintf(final_name, sizeof(final_name), "%s.%04d", dump_command, frame_count++);
151
152 if (rename(stage_name, final_name))
153 fprintf(stderr, "lima: failed to rename log %s to %s\n", stage_name, final_name);
154
155 FREE(dump);
156 }
157
158 void
_lima_dump_command_stream_print(struct lima_dump * dump,void * data,int size,bool is_float,const char * fmt,...)159 _lima_dump_command_stream_print(struct lima_dump *dump, void *data,
160 int size, bool is_float, const char *fmt, ...)
161 {
162 va_list ap;
163 va_start(ap, fmt);
164 vfprintf(dump->fp, fmt, ap);
165 va_end(ap);
166
167 lima_dump_blob(dump->fp, data, size, is_float);
168 }
169
170 void
lima_damage_rect_union(struct pipe_scissor_state * rect,unsigned minx,unsigned maxx,unsigned miny,unsigned maxy)171 lima_damage_rect_union(struct pipe_scissor_state *rect,
172 unsigned minx, unsigned maxx,
173 unsigned miny, unsigned maxy)
174 {
175 rect->minx = MIN2(rect->minx, minx);
176 rect->miny = MIN2(rect->miny, miny);
177 rect->maxx = MAX2(rect->maxx, maxx);
178 rect->maxy = MAX2(rect->maxy, maxy);
179 }
180