summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/lima/lima_util.c
blob: dca9307c991c2456eacf896b1eb919082a379ac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright (C) 2018-2019 Lima Project
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include <stdio.h>
#include <stdarg.h>
#include <time.h>

#include <pipe/p_defines.h>

#include "util/u_debug.h"
#include "util/u_memory.h"

#include "lima_util.h"
#include "lima_parser.h"
#include "lima_screen.h"

struct lima_dump {
   FILE *fp;
   int id;
};

bool lima_get_absolute_timeout(uint64_t *timeout)
{
   struct timespec current;
   uint64_t current_ns;

   if (*timeout == PIPE_TIMEOUT_INFINITE)
      return true;

   if (clock_gettime(CLOCK_MONOTONIC, &current))
      return false;

   current_ns = ((uint64_t)current.tv_sec) * 1000000000ull;
   current_ns += current.tv_nsec;
   *timeout += current_ns;

   return true;
}

static void
lima_dump_blob(FILE *fp, void *data, int size, bool is_float)
{
   fprintf(fp, "{\n");
   for (int i = 0; i * 4 < size; i++) {
      if (i % 4 == 0)
         fprintf(fp, "\t");

      if (is_float)
         fprintf(fp, "%f, ", ((float *)data)[i]);
      else
         fprintf(fp, "0x%08x, ", ((uint32_t *)data)[i]);

      if ((i % 4 == 3) || (i == size / 4 - 1)) {
         fprintf(fp, "/* 0x%08x */", MAX2((i - 3) * 4, 0));
         if (i) fprintf(fp, "\n");
      }
   }
   fprintf(fp, "}\n");
}

void
lima_dump_vs_command_stream_print(struct lima_dump *dump, void *data,
                                  int size, uint32_t start)
{
   if (dump)
      lima_parse_vs(dump->fp, (uint32_t *)data, size, start);
}

void
lima_dump_plbu_command_stream_print(struct lima_dump *dump, void *data,
                                    int size, uint32_t start)
{
   if (dump)
      lima_parse_plbu(dump->fp, (uint32_t *)data, size, start);
}

void
lima_dump_rsw_command_stream_print(struct lima_dump *dump, void *data,
                                   int size, uint32_t start)
{
   if (dump)
      lima_parse_render_state(dump->fp, (uint32_t *)data, size, start);
}

void
lima_dump_texture_descriptor(struct lima_dump *dump, void *data,
                             int size, uint32_t start, uint32_t offset)
{
   if (dump)
      lima_parse_texture_descriptor(dump->fp, (uint32_t *)data, size, start, offset);
}

struct lima_dump *
lima_dump_create(void)
{
   static int dump_id = 0;

   if (!(lima_debug & LIMA_DEBUG_DUMP))
      return NULL;

   struct lima_dump *ret = MALLOC_STRUCT(lima_dump);
   if (!ret)
      return NULL;

   ret->id = dump_id++;

   char buffer[PATH_MAX];
   const char *dump_command = debug_get_option("LIMA_DUMP_FILE", "lima.dump");
   snprintf(buffer, sizeof(buffer), "%s.staging.%04d", dump_command, ret->id);

   ret->fp = fopen(buffer, "w");
   if (!ret->fp) {
      fprintf(stderr, "lima: failed to open command stream log file %s\n", buffer);
      FREE(ret);
      return NULL;
   }

   return ret;
}

void
lima_dump_free(struct lima_dump *dump)
{
   static int frame_count = 0;

   if (!dump)
      return;

   fclose(dump->fp);

   /* we only know the frame count when flush, not on dump create, so first dump to a
    * stage file, then move to the final file with frame count as surfix when flush.
    */

   char stage_name[PATH_MAX];
   char final_name[PATH_MAX];
   const char *dump_command = debug_get_option("LIMA_DUMP_FILE", "lima.dump");
   snprintf(stage_name, sizeof(stage_name), "%s.staging.%04d", dump_command, dump->id);
   snprintf(final_name, sizeof(final_name), "%s.%04d", dump_command, frame_count++);

   if (rename(stage_name, final_name))
      fprintf(stderr, "lima: failed to rename log %s to %s\n", stage_name, final_name);

   FREE(dump);
}

void
_lima_dump_command_stream_print(struct lima_dump *dump, void *data,
                                int size, bool is_float, const char *fmt, ...)
{
   va_list ap;
   va_start(ap, fmt);
   vfprintf(dump->fp, fmt, ap);
   va_end(ap);

   lima_dump_blob(dump->fp, data, size, is_float);
}