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