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