util/u_debug: add description field to debug_named_value
[mesa.git] / src / gallium / auxiliary / util / u_debug.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright (c) 2008 VMware, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29
30 #include "pipe/p_config.h"
31
32 #include "pipe/p_compiler.h"
33 #include "os/os_stream.h"
34 #include "util/u_debug.h"
35 #include "pipe/p_format.h"
36 #include "pipe/p_state.h"
37 #include "util/u_inlines.h"
38 #include "util/u_format.h"
39 #include "util/u_memory.h"
40 #include "util/u_string.h"
41 #include "util/u_math.h"
42 #include "util/u_tile.h"
43 #include "util/u_prim.h"
44
45 #include <limits.h> /* CHAR_BIT */
46
47 void _debug_vprintf(const char *format, va_list ap)
48 {
49 /* We buffer until we find a newline. */
50 static char buf[4096] = {'\0'};
51 size_t len = strlen(buf);
52 int ret = util_vsnprintf(buf + len, sizeof(buf) - len, format, ap);
53 if(ret > (int)(sizeof(buf) - len - 1) || util_strchr(buf + len, '\n')) {
54 os_log_message(buf);
55 buf[0] = '\0';
56 }
57 }
58
59
60 #ifdef DEBUG
61 void debug_print_blob( const char *name,
62 const void *blob,
63 unsigned size )
64 {
65 const unsigned *ublob = (const unsigned *)blob;
66 unsigned i;
67
68 debug_printf("%s (%d dwords%s)\n", name, size/4,
69 size%4 ? "... plus a few bytes" : "");
70
71 for (i = 0; i < size/4; i++) {
72 debug_printf("%d:\t%08x\n", i, ublob[i]);
73 }
74 }
75 #endif
76
77
78 static boolean
79 debug_get_option_should_print(void)
80 {
81 static boolean first = TRUE;
82 static boolean value = FALSE;
83
84 if (!first)
85 return value;
86
87 /* Oh hey this will call into this function,
88 * but its cool since we set first to false
89 */
90 first = FALSE;
91 value = debug_get_bool_option("GALLIUM_PRINT_OPTIONS", TRUE);
92 /* XXX should we print this option? Currently it wont */
93 return value;
94 }
95
96 const char *
97 debug_get_option(const char *name, const char *dfault)
98 {
99 const char *result;
100
101 result = os_get_option(name);
102 if(!result)
103 result = dfault;
104
105 if (debug_get_option_should_print())
106 debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? result : "(null)");
107
108 return result;
109 }
110
111 boolean
112 debug_get_bool_option(const char *name, boolean dfault)
113 {
114 const char *str = os_get_option(name);
115 boolean result;
116
117 if(str == NULL)
118 result = dfault;
119 else if(!util_strcmp(str, "n"))
120 result = FALSE;
121 else if(!util_strcmp(str, "no"))
122 result = FALSE;
123 else if(!util_strcmp(str, "0"))
124 result = FALSE;
125 else if(!util_strcmp(str, "f"))
126 result = FALSE;
127 else if(!util_strcmp(str, "F"))
128 result = FALSE;
129 else if(!util_strcmp(str, "false"))
130 result = FALSE;
131 else if(!util_strcmp(str, "FALSE"))
132 result = FALSE;
133 else
134 result = TRUE;
135
136 if (debug_get_option_should_print())
137 debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? "TRUE" : "FALSE");
138
139 return result;
140 }
141
142
143 long
144 debug_get_num_option(const char *name, long dfault)
145 {
146 long result;
147 const char *str;
148
149 str = os_get_option(name);
150 if(!str)
151 result = dfault;
152 else {
153 long sign;
154 char c;
155 c = *str++;
156 if(c == '-') {
157 sign = -1;
158 c = *str++;
159 }
160 else {
161 sign = 1;
162 }
163 result = 0;
164 while('0' <= c && c <= '9') {
165 result = result*10 + (c - '0');
166 c = *str++;
167 }
168 result *= sign;
169 }
170
171 if (debug_get_option_should_print())
172 debug_printf("%s: %s = %li\n", __FUNCTION__, name, result);
173
174 return result;
175 }
176
177 static INLINE int
178 max( int a,
179 int b )
180 {
181 return (a > b) ? a : b;
182 }
183
184 unsigned long
185 debug_get_flags_option(const char *name,
186 const struct debug_named_value *flags,
187 unsigned long dfault)
188 {
189 unsigned long result;
190 const char *str;
191 const struct debug_named_value *orig = flags;
192 int namealign = 0;
193
194 str = os_get_option(name);
195 if(!str)
196 result = dfault;
197 else if (!util_strcmp(str, "help")) {
198 result = dfault;
199 debug_printf("%s: help for %s:\n", __FUNCTION__, name);
200 for (; flags->name; ++flags)
201 namealign = max(namealign, strlen(flags->name));
202 for (flags = orig; flags->name; ++flags)
203 debug_printf("| %*s [0x%0*lx]%s%s\n", namealign, flags->name,
204 sizeof(unsigned long)*CHAR_BIT/4, flags->value,
205 flags->desc ? " " : "", flags->desc ? flags->desc : "");
206 }
207 else {
208 result = 0;
209 while( flags->name ) {
210 if (!util_strcmp(str, "all") || util_strstr(str, flags->name ))
211 result |= flags->value;
212 ++flags;
213 }
214 }
215
216 if (debug_get_option_should_print()) {
217 if (str) {
218 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__, name, result, str);
219 } else {
220 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__, name, result);
221 }
222 }
223
224 return result;
225 }
226
227
228 void _debug_assert_fail(const char *expr,
229 const char *file,
230 unsigned line,
231 const char *function)
232 {
233 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file, line, function, expr);
234 #if defined(PIPE_OS_WINDOWS) && !defined(PIPE_SUBSYSTEM_WINDOWS_USER)
235 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", FALSE))
236 #else
237 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE))
238 #endif
239 os_abort();
240 else
241 _debug_printf("continuing...\n");
242 }
243
244
245 const char *
246 debug_dump_enum(const struct debug_named_value *names,
247 unsigned long value)
248 {
249 static char rest[64];
250
251 while(names->name) {
252 if(names->value == value)
253 return names->name;
254 ++names;
255 }
256
257 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
258 return rest;
259 }
260
261
262 const char *
263 debug_dump_enum_noprefix(const struct debug_named_value *names,
264 const char *prefix,
265 unsigned long value)
266 {
267 static char rest[64];
268
269 while(names->name) {
270 if(names->value == value) {
271 const char *name = names->name;
272 while (*name == *prefix) {
273 name++;
274 prefix++;
275 }
276 return name;
277 }
278 ++names;
279 }
280
281
282
283 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
284 return rest;
285 }
286
287
288 const char *
289 debug_dump_flags(const struct debug_named_value *names,
290 unsigned long value)
291 {
292 static char output[4096];
293 static char rest[256];
294 int first = 1;
295
296 output[0] = '\0';
297
298 while(names->name) {
299 if((names->value & value) == names->value) {
300 if (!first)
301 util_strncat(output, "|", sizeof(output));
302 else
303 first = 0;
304 util_strncat(output, names->name, sizeof(output) - 1);
305 output[sizeof(output) - 1] = '\0';
306 value &= ~names->value;
307 }
308 ++names;
309 }
310
311 if (value) {
312 if (!first)
313 util_strncat(output, "|", sizeof(output));
314 else
315 first = 0;
316
317 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
318 util_strncat(output, rest, sizeof(output) - 1);
319 output[sizeof(output) - 1] = '\0';
320 }
321
322 if(first)
323 return "0";
324
325 return output;
326 }
327
328
329 #ifdef DEBUG
330 void debug_print_format(const char *msg, unsigned fmt )
331 {
332 debug_printf("%s: %s\n", msg, util_format_name(fmt));
333 }
334 #endif
335
336
337
338 static const struct debug_named_value pipe_prim_names[] = {
339 #ifdef DEBUG
340 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS),
341 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES),
342 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP),
343 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP),
344 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES),
345 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP),
346 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN),
347 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS),
348 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP),
349 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON),
350 #endif
351 DEBUG_NAMED_VALUE_END
352 };
353
354
355 const char *u_prim_name( unsigned prim )
356 {
357 return debug_dump_enum(pipe_prim_names, prim);
358 }
359
360
361
362
363 #ifdef DEBUG
364 /**
365 * Dump an image to a .raw or .ppm file (depends on OS).
366 * \param format PIPE_FORMAT_x
367 * \param cpp bytes per pixel
368 * \param width width in pixels
369 * \param height height in pixels
370 * \param stride row stride in bytes
371 */
372 void debug_dump_image(const char *prefix,
373 unsigned format, unsigned cpp,
374 unsigned width, unsigned height,
375 unsigned stride,
376 const void *data)
377 {
378 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
379 static unsigned no = 0;
380 char filename[256];
381 WCHAR wfilename[sizeof(filename)];
382 ULONG_PTR iFile = 0;
383 struct {
384 unsigned format;
385 unsigned cpp;
386 unsigned width;
387 unsigned height;
388 } header;
389 unsigned char *pMap = NULL;
390 unsigned i;
391
392 util_snprintf(filename, sizeof(filename), "\\??\\c:\\%03u%s.raw", ++no, prefix);
393 for(i = 0; i < sizeof(filename); ++i)
394 wfilename[i] = (WCHAR)filename[i];
395
396 pMap = (unsigned char *)EngMapFile(wfilename, sizeof(header) + height*width*cpp, &iFile);
397 if(!pMap)
398 return;
399
400 header.format = format;
401 header.cpp = cpp;
402 header.width = width;
403 header.height = height;
404 memcpy(pMap, &header, sizeof(header));
405 pMap += sizeof(header);
406
407 for(i = 0; i < height; ++i) {
408 memcpy(pMap, (unsigned char *)data + stride*i, cpp*width);
409 pMap += cpp*width;
410 }
411
412 EngUnmapFile(iFile);
413 #elif defined(PIPE_OS_UNIX)
414 /* write a ppm file */
415 char filename[256];
416 FILE *f;
417
418 util_snprintf(filename, sizeof(filename), "%s.ppm", prefix);
419
420 f = fopen(filename, "w");
421 if (f) {
422 int i, x, y;
423 int r, g, b;
424 const uint8_t *ptr = (uint8_t *) data;
425
426 /* XXX this is a hack */
427 switch (format) {
428 case PIPE_FORMAT_B8G8R8A8_UNORM:
429 r = 2;
430 g = 1;
431 b = 0;
432 break;
433 default:
434 r = 0;
435 g = 1;
436 b = 1;
437 }
438
439 fprintf(f, "P6\n");
440 fprintf(f, "# ppm-file created by osdemo.c\n");
441 fprintf(f, "%i %i\n", width, height);
442 fprintf(f, "255\n");
443 fclose(f);
444
445 f = fopen(filename, "ab"); /* reopen in binary append mode */
446 for (y = 0; y < height; y++) {
447 for (x = 0; x < width; x++) {
448 i = y * stride + x * cpp;
449 fputc(ptr[i + r], f); /* write red */
450 fputc(ptr[i + g], f); /* write green */
451 fputc(ptr[i + b], f); /* write blue */
452 }
453 }
454 fclose(f);
455 }
456 else {
457 fprintf(stderr, "Can't open %s for writing\n", filename);
458 }
459 #endif
460 }
461
462 void debug_dump_surface(struct pipe_context *pipe,
463 const char *prefix,
464 struct pipe_surface *surface)
465 {
466 struct pipe_resource *texture;
467 struct pipe_transfer *transfer;
468 void *data;
469
470 if (!surface)
471 return;
472
473 /* XXX: this doesn't necessarily work, as the driver may be using
474 * temporary storage for the surface which hasn't been propagated
475 * back into the texture. Need to nail down the semantics of views
476 * and transfers a bit better before we can say if extra work needs
477 * to be done here:
478 */
479 texture = surface->texture;
480
481 transfer = pipe_get_transfer(pipe, texture, surface->face,
482 surface->level, surface->zslice,
483 PIPE_TRANSFER_READ, 0, 0, surface->width,
484 surface->height);
485
486 data = pipe->transfer_map(pipe, transfer);
487 if(!data)
488 goto error;
489
490 debug_dump_image(prefix,
491 texture->format,
492 util_format_get_blocksize(texture->format),
493 util_format_get_nblocksx(texture->format, surface->width),
494 util_format_get_nblocksy(texture->format, surface->height),
495 transfer->stride,
496 data);
497
498 pipe->transfer_unmap(pipe, transfer);
499 error:
500 pipe->transfer_destroy(pipe, transfer);
501 }
502
503
504 void debug_dump_texture(struct pipe_context *pipe,
505 const char *prefix,
506 struct pipe_resource *texture)
507 {
508 struct pipe_surface *surface;
509 struct pipe_screen *screen;
510
511 if (!texture)
512 return;
513
514 screen = texture->screen;
515
516 /* XXX for now, just dump image for face=0, level=0 */
517 surface = screen->get_tex_surface(screen, texture, 0, 0, 0,
518 PIPE_BIND_SAMPLER_VIEW);
519 if (surface) {
520 debug_dump_surface(pipe, prefix, surface);
521 screen->tex_surface_destroy(surface);
522 }
523 }
524
525
526 #pragma pack(push,2)
527 struct bmp_file_header {
528 uint16_t bfType;
529 uint32_t bfSize;
530 uint16_t bfReserved1;
531 uint16_t bfReserved2;
532 uint32_t bfOffBits;
533 };
534 #pragma pack(pop)
535
536 struct bmp_info_header {
537 uint32_t biSize;
538 int32_t biWidth;
539 int32_t biHeight;
540 uint16_t biPlanes;
541 uint16_t biBitCount;
542 uint32_t biCompression;
543 uint32_t biSizeImage;
544 int32_t biXPelsPerMeter;
545 int32_t biYPelsPerMeter;
546 uint32_t biClrUsed;
547 uint32_t biClrImportant;
548 };
549
550 struct bmp_rgb_quad {
551 uint8_t rgbBlue;
552 uint8_t rgbGreen;
553 uint8_t rgbRed;
554 uint8_t rgbAlpha;
555 };
556
557 void
558 debug_dump_surface_bmp(struct pipe_context *pipe,
559 const char *filename,
560 struct pipe_surface *surface)
561 {
562 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
563 struct pipe_transfer *transfer;
564 struct pipe_resource *texture = surface->texture;
565
566 transfer = pipe_get_transfer(pipe, texture, surface->face,
567 surface->level, surface->zslice,
568 PIPE_TRANSFER_READ, 0, 0, surface->width,
569 surface->height);
570
571 debug_dump_transfer_bmp(pipe, filename, transfer);
572
573 pipe->transfer_destroy(pipe, transfer);
574 #endif
575 }
576
577 void
578 debug_dump_transfer_bmp(struct pipe_context *pipe,
579 const char *filename,
580 struct pipe_transfer *transfer)
581 {
582 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
583 float *rgba;
584
585 if (!transfer)
586 goto error1;
587
588 rgba = MALLOC(transfer->box.width *
589 transfer->box.height *
590 transfer->box.depth *
591 4*sizeof(float));
592 if(!rgba)
593 goto error1;
594
595 pipe_get_tile_rgba(pipe, transfer, 0, 0,
596 transfer->box.width, transfer->box.height,
597 rgba);
598
599 debug_dump_float_rgba_bmp(filename,
600 transfer->box.width, transfer->box.height,
601 rgba, transfer->box.width);
602
603 FREE(rgba);
604 error1:
605 ;
606 #endif
607 }
608
609 void
610 debug_dump_float_rgba_bmp(const char *filename,
611 unsigned width, unsigned height,
612 float *rgba, unsigned stride)
613 {
614 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
615 struct os_stream *stream;
616 struct bmp_file_header bmfh;
617 struct bmp_info_header bmih;
618 unsigned x, y;
619
620 if(!rgba)
621 goto error1;
622
623 bmfh.bfType = 0x4d42;
624 bmfh.bfSize = 14 + 40 + height*width*4;
625 bmfh.bfReserved1 = 0;
626 bmfh.bfReserved2 = 0;
627 bmfh.bfOffBits = 14 + 40;
628
629 bmih.biSize = 40;
630 bmih.biWidth = width;
631 bmih.biHeight = height;
632 bmih.biPlanes = 1;
633 bmih.biBitCount = 32;
634 bmih.biCompression = 0;
635 bmih.biSizeImage = height*width*4;
636 bmih.biXPelsPerMeter = 0;
637 bmih.biYPelsPerMeter = 0;
638 bmih.biClrUsed = 0;
639 bmih.biClrImportant = 0;
640
641 stream = os_file_stream_create(filename);
642 if(!stream)
643 goto error1;
644
645 os_stream_write(stream, &bmfh, 14);
646 os_stream_write(stream, &bmih, 40);
647
648 y = height;
649 while(y--) {
650 float *ptr = rgba + (stride * y * 4);
651 for(x = 0; x < width; ++x)
652 {
653 struct bmp_rgb_quad pixel;
654 pixel.rgbRed = float_to_ubyte(ptr[x*4 + 0]);
655 pixel.rgbGreen = float_to_ubyte(ptr[x*4 + 1]);
656 pixel.rgbBlue = float_to_ubyte(ptr[x*4 + 2]);
657 pixel.rgbAlpha = 255;
658 os_stream_write(stream, &pixel, 4);
659 }
660 }
661
662 os_stream_close(stream);
663 error1:
664 ;
665 #endif
666 }
667
668 #endif