1b8e80941Smrg/*
2b8e80941Smrg * Copyright (C) 2018-2019 Lima Project
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice shall be included in
12b8e80941Smrg * all copies or substantial portions of the Software.
13b8e80941Smrg *
14b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b8e80941Smrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b8e80941Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b8e80941Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b8e80941Smrg * OTHER DEALINGS IN THE SOFTWARE.
21b8e80941Smrg *
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg#include <stdio.h>
25b8e80941Smrg#include <stdarg.h>
26b8e80941Smrg#include <time.h>
27b8e80941Smrg
28b8e80941Smrg#include <pipe/p_defines.h>
29b8e80941Smrg
30b8e80941Smrg#include "lima_util.h"
31b8e80941Smrg
32b8e80941SmrgFILE *lima_dump_command_stream = NULL;
33b8e80941Smrg
34b8e80941Smrgbool lima_get_absolute_timeout(uint64_t *timeout)
35b8e80941Smrg{
36b8e80941Smrg   struct timespec current;
37b8e80941Smrg   uint64_t current_ns;
38b8e80941Smrg
39b8e80941Smrg   if (*timeout == PIPE_TIMEOUT_INFINITE)
40b8e80941Smrg      return true;
41b8e80941Smrg
42b8e80941Smrg   if (clock_gettime(CLOCK_MONOTONIC, &current))
43b8e80941Smrg      return false;
44b8e80941Smrg
45b8e80941Smrg   current_ns = ((uint64_t)current.tv_sec) * 1000000000ull;
46b8e80941Smrg   current_ns += current.tv_nsec;
47b8e80941Smrg   *timeout += current_ns;
48b8e80941Smrg
49b8e80941Smrg   return true;
50b8e80941Smrg}
51b8e80941Smrg
52b8e80941Smrgvoid lima_dump_blob(FILE *fp, void *data, int size, bool is_float)
53b8e80941Smrg{
54b8e80941Smrg   for (int i = 0; i * 4 < size; i++) {
55b8e80941Smrg      if (i % 4 == 0) {
56b8e80941Smrg         if (i) fprintf(fp, "\n");
57b8e80941Smrg         fprintf(fp, "%04x:", i * 4);
58b8e80941Smrg      }
59b8e80941Smrg
60b8e80941Smrg      if (is_float)
61b8e80941Smrg         fprintf(fp, " %f", ((float *)data)[i]);
62b8e80941Smrg      else
63b8e80941Smrg         fprintf(fp, " 0x%08x", ((uint32_t *)data)[i]);
64b8e80941Smrg   }
65b8e80941Smrg   fprintf(fp, "\n");
66b8e80941Smrg}
67b8e80941Smrg
68b8e80941Smrgvoid
69b8e80941Smrglima_dump_command_stream_print(void *data, int size, bool is_float,
70b8e80941Smrg                               const char *fmt, ...)
71b8e80941Smrg{
72b8e80941Smrg   if (lima_dump_command_stream) {
73b8e80941Smrg      va_list ap;
74b8e80941Smrg      va_start(ap, fmt);
75b8e80941Smrg      vfprintf(lima_dump_command_stream, fmt, ap);
76b8e80941Smrg      va_end(ap);
77b8e80941Smrg
78b8e80941Smrg      lima_dump_blob(lima_dump_command_stream, data, size, is_float);
79b8e80941Smrg   }
80b8e80941Smrg}
81