Merge remote branch 'origin/master' into pipe-video
[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 #include "util/u_surface.h"
45
46 #include <limits.h> /* CHAR_BIT */
47 #include <ctype.h> /* isalnum */
48
49 void _debug_vprintf(const char *format, va_list ap)
50 {
51 #if defined(PIPE_OS_WINDOWS) || defined(PIPE_OS_EMBEDDED)
52 /* We buffer until we find a newline. */
53 static char buf[4096] = {'\0'};
54 size_t len = strlen(buf);
55 int ret = util_vsnprintf(buf + len, sizeof(buf) - len, format, ap);
56 if(ret > (int)(sizeof(buf) - len - 1) || util_strchr(buf + len, '\n')) {
57 os_log_message(buf);
58 buf[0] = '\0';
59 }
60 #else
61 /* Just print as-is to stderr */
62 vfprintf(stderr, format, ap);
63 #endif
64 }
65
66
67 #ifdef DEBUG
68 void debug_print_blob( const char *name,
69 const void *blob,
70 unsigned size )
71 {
72 const unsigned *ublob = (const unsigned *)blob;
73 unsigned i;
74
75 debug_printf("%s (%d dwords%s)\n", name, size/4,
76 size%4 ? "... plus a few bytes" : "");
77
78 for (i = 0; i < size/4; i++) {
79 debug_printf("%d:\t%08x\n", i, ublob[i]);
80 }
81 }
82 #endif
83
84
85 static boolean
86 debug_get_option_should_print(void)
87 {
88 static boolean first = TRUE;
89 static boolean value = FALSE;
90
91 if (!first)
92 return value;
93
94 /* Oh hey this will call into this function,
95 * but its cool since we set first to false
96 */
97 first = FALSE;
98 value = debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE);
99 /* XXX should we print this option? Currently it wont */
100 return value;
101 }
102
103 const char *
104 debug_get_option(const char *name, const char *dfault)
105 {
106 const char *result;
107
108 result = os_get_option(name);
109 if(!result)
110 result = dfault;
111
112 if (debug_get_option_should_print())
113 debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? result : "(null)");
114
115 return result;
116 }
117
118 boolean
119 debug_get_bool_option(const char *name, boolean dfault)
120 {
121 const char *str = os_get_option(name);
122 boolean result;
123
124 if(str == NULL)
125 result = dfault;
126 else if(!util_strcmp(str, "n"))
127 result = FALSE;
128 else if(!util_strcmp(str, "no"))
129 result = FALSE;
130 else if(!util_strcmp(str, "0"))
131 result = FALSE;
132 else if(!util_strcmp(str, "f"))
133 result = FALSE;
134 else if(!util_strcmp(str, "F"))
135 result = FALSE;
136 else if(!util_strcmp(str, "false"))
137 result = FALSE;
138 else if(!util_strcmp(str, "FALSE"))
139 result = FALSE;
140 else
141 result = TRUE;
142
143 if (debug_get_option_should_print())
144 debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? "TRUE" : "FALSE");
145
146 return result;
147 }
148
149
150 long
151 debug_get_num_option(const char *name, long dfault)
152 {
153 long result;
154 const char *str;
155
156 str = os_get_option(name);
157 if(!str)
158 result = dfault;
159 else {
160 long sign;
161 char c;
162 c = *str++;
163 if(c == '-') {
164 sign = -1;
165 c = *str++;
166 }
167 else {
168 sign = 1;
169 }
170 result = 0;
171 while('0' <= c && c <= '9') {
172 result = result*10 + (c - '0');
173 c = *str++;
174 }
175 result *= sign;
176 }
177
178 if (debug_get_option_should_print())
179 debug_printf("%s: %s = %li\n", __FUNCTION__, name, result);
180
181 return result;
182 }
183
184 static boolean str_has_option(const char *str, const char *name)
185 {
186 /* Empty string. */
187 if (!*str) {
188 return FALSE;
189 }
190
191 /* OPTION=all */
192 if (!util_strcmp(str, "all")) {
193 return TRUE;
194 }
195
196 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
197 {
198 const char *start = str;
199 unsigned name_len = strlen(name);
200
201 /* 'start' is the beginning of the currently-parsed word,
202 * we increment 'str' each iteration.
203 * if we find either the end of string or a non-alphanumeric character,
204 * we compare 'start' up to 'str-1' with 'name'. */
205
206 while (1) {
207 if (!*str || !isalnum(*str)) {
208 if (str-start == name_len &&
209 !memcmp(start, name, name_len)) {
210 return TRUE;
211 }
212
213 if (!*str) {
214 return FALSE;
215 }
216
217 start = str+1;
218 }
219
220 str++;
221 }
222 }
223
224 return FALSE;
225 }
226
227 unsigned long
228 debug_get_flags_option(const char *name,
229 const struct debug_named_value *flags,
230 unsigned long dfault)
231 {
232 unsigned long result;
233 const char *str;
234 const struct debug_named_value *orig = flags;
235 int namealign = 0;
236
237 str = os_get_option(name);
238 if(!str)
239 result = dfault;
240 else if (!util_strcmp(str, "help")) {
241 result = dfault;
242 _debug_printf("%s: help for %s:\n", __FUNCTION__, name);
243 for (; flags->name; ++flags)
244 namealign = MAX2(namealign, strlen(flags->name));
245 for (flags = orig; flags->name; ++flags)
246 _debug_printf("| %*s [0x%0*lx]%s%s\n", namealign, flags->name,
247 (int)sizeof(unsigned long)*CHAR_BIT/4, flags->value,
248 flags->desc ? " " : "", flags->desc ? flags->desc : "");
249 }
250 else {
251 result = 0;
252 while( flags->name ) {
253 if (str_has_option(str, flags->name))
254 result |= flags->value;
255 ++flags;
256 }
257 }
258
259 if (debug_get_option_should_print()) {
260 if (str) {
261 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__, name, result, str);
262 } else {
263 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__, name, result);
264 }
265 }
266
267 return result;
268 }
269
270
271 void _debug_assert_fail(const char *expr,
272 const char *file,
273 unsigned line,
274 const char *function)
275 {
276 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file, line, function, expr);
277 #if defined(PIPE_OS_WINDOWS) && !defined(PIPE_SUBSYSTEM_WINDOWS_USER)
278 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", FALSE))
279 #else
280 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE))
281 #endif
282 os_abort();
283 else
284 _debug_printf("continuing...\n");
285 }
286
287
288 const char *
289 debug_dump_enum(const struct debug_named_value *names,
290 unsigned long value)
291 {
292 static char rest[64];
293
294 while(names->name) {
295 if(names->value == value)
296 return names->name;
297 ++names;
298 }
299
300 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
301 return rest;
302 }
303
304
305 const char *
306 debug_dump_enum_noprefix(const struct debug_named_value *names,
307 const char *prefix,
308 unsigned long value)
309 {
310 static char rest[64];
311
312 while(names->name) {
313 if(names->value == value) {
314 const char *name = names->name;
315 while (*name == *prefix) {
316 name++;
317 prefix++;
318 }
319 return name;
320 }
321 ++names;
322 }
323
324
325
326 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
327 return rest;
328 }
329
330
331 const char *
332 debug_dump_flags(const struct debug_named_value *names,
333 unsigned long value)
334 {
335 static char output[4096];
336 static char rest[256];
337 int first = 1;
338
339 output[0] = '\0';
340
341 while(names->name) {
342 if((names->value & value) == names->value) {
343 if (!first)
344 util_strncat(output, "|", sizeof(output));
345 else
346 first = 0;
347 util_strncat(output, names->name, sizeof(output) - 1);
348 output[sizeof(output) - 1] = '\0';
349 value &= ~names->value;
350 }
351 ++names;
352 }
353
354 if (value) {
355 if (!first)
356 util_strncat(output, "|", sizeof(output));
357 else
358 first = 0;
359
360 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
361 util_strncat(output, rest, sizeof(output) - 1);
362 output[sizeof(output) - 1] = '\0';
363 }
364
365 if(first)
366 return "0";
367
368 return output;
369 }
370
371
372 #ifdef DEBUG
373 void debug_print_format(const char *msg, unsigned fmt )
374 {
375 debug_printf("%s: %s\n", msg, util_format_name(fmt));
376 }
377 #endif
378
379
380
381 static const struct debug_named_value pipe_prim_names[] = {
382 #ifdef DEBUG
383 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS),
384 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES),
385 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP),
386 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP),
387 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES),
388 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP),
389 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN),
390 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS),
391 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP),
392 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON),
393 #endif
394 DEBUG_NAMED_VALUE_END
395 };
396
397
398 const char *u_prim_name( unsigned prim )
399 {
400 return debug_dump_enum(pipe_prim_names, prim);
401 }
402
403
404
405 #ifdef DEBUG
406 int fl_indent = 0;
407 const char* fl_function[1024];
408
409 int debug_funclog_enter(const char* f, const int line, const char* file)
410 {
411 int i;
412
413 for (i = 0; i < fl_indent; i++)
414 debug_printf(" ");
415 debug_printf("%s\n", f);
416
417 assert(fl_indent < 1023);
418 fl_function[fl_indent++] = f;
419
420 return 0;
421 }
422
423 void debug_funclog_exit(const char* f, const int line, const char* file)
424 {
425 --fl_indent;
426 assert(fl_indent >= 0);
427 assert(fl_function[fl_indent] == f);
428 }
429
430 void debug_funclog_enter_exit(const char* f, const int line, const char* file)
431 {
432 int i;
433 for (i = 0; i < fl_indent; i++)
434 debug_printf(" ");
435 debug_printf("%s\n", f);
436 }
437 #endif
438
439
440
441 #ifdef DEBUG
442 /**
443 * Dump an image to a .raw or .ppm file (depends on OS).
444 * \param format PIPE_FORMAT_x
445 * \param cpp bytes per pixel
446 * \param width width in pixels
447 * \param height height in pixels
448 * \param stride row stride in bytes
449 */
450 void debug_dump_image(const char *prefix,
451 unsigned format, unsigned cpp,
452 unsigned width, unsigned height,
453 unsigned stride,
454 const void *data)
455 {
456 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
457 static unsigned no = 0;
458 char filename[256];
459 WCHAR wfilename[sizeof(filename)];
460 ULONG_PTR iFile = 0;
461 struct {
462 unsigned format;
463 unsigned cpp;
464 unsigned width;
465 unsigned height;
466 } header;
467 unsigned char *pMap = NULL;
468 unsigned i;
469
470 util_snprintf(filename, sizeof(filename), "\\??\\c:\\%03u%s.raw", ++no, prefix);
471 for(i = 0; i < sizeof(filename); ++i)
472 wfilename[i] = (WCHAR)filename[i];
473
474 pMap = (unsigned char *)EngMapFile(wfilename, sizeof(header) + height*width*cpp, &iFile);
475 if(!pMap)
476 return;
477
478 header.format = format;
479 header.cpp = cpp;
480 header.width = width;
481 header.height = height;
482 memcpy(pMap, &header, sizeof(header));
483 pMap += sizeof(header);
484
485 for(i = 0; i < height; ++i) {
486 memcpy(pMap, (unsigned char *)data + stride*i, cpp*width);
487 pMap += cpp*width;
488 }
489
490 EngUnmapFile(iFile);
491 #elif defined(PIPE_OS_UNIX)
492 /* write a ppm file */
493 char filename[256];
494 FILE *f;
495
496 util_snprintf(filename, sizeof(filename), "%s.ppm", prefix);
497
498 f = fopen(filename, "w");
499 if (f) {
500 int i, x, y;
501 int r, g, b;
502 const uint8_t *ptr = (uint8_t *) data;
503
504 /* XXX this is a hack */
505 switch (format) {
506 case PIPE_FORMAT_B8G8R8A8_UNORM:
507 r = 2;
508 g = 1;
509 b = 0;
510 break;
511 default:
512 r = 0;
513 g = 1;
514 b = 1;
515 }
516
517 fprintf(f, "P6\n");
518 fprintf(f, "# ppm-file created by osdemo.c\n");
519 fprintf(f, "%i %i\n", width, height);
520 fprintf(f, "255\n");
521 fclose(f);
522
523 f = fopen(filename, "ab"); /* reopen in binary append mode */
524 for (y = 0; y < height; y++) {
525 for (x = 0; x < width; x++) {
526 i = y * stride + x * cpp;
527 fputc(ptr[i + r], f); /* write red */
528 fputc(ptr[i + g], f); /* write green */
529 fputc(ptr[i + b], f); /* write blue */
530 }
531 }
532 fclose(f);
533 }
534 else {
535 fprintf(stderr, "Can't open %s for writing\n", filename);
536 }
537 #endif
538 }
539
540 /* FIXME: dump resources, not surfaces... */
541 void debug_dump_surface(struct pipe_context *pipe,
542 const char *prefix,
543 struct pipe_surface *surface)
544 {
545 struct pipe_resource *texture;
546 struct pipe_transfer *transfer;
547 void *data;
548
549 if (!surface)
550 return;
551
552 /* XXX: this doesn't necessarily work, as the driver may be using
553 * temporary storage for the surface which hasn't been propagated
554 * back into the texture. Need to nail down the semantics of views
555 * and transfers a bit better before we can say if extra work needs
556 * to be done here:
557 */
558 texture = surface->texture;
559
560 transfer = pipe_get_transfer(pipe, texture, surface->u.tex.level,
561 surface->u.tex.first_layer,
562 PIPE_TRANSFER_READ,
563 0, 0, surface->width, surface->height);
564
565 data = pipe->transfer_map(pipe, transfer);
566 if(!data)
567 goto error;
568
569 debug_dump_image(prefix,
570 texture->format,
571 util_format_get_blocksize(texture->format),
572 util_format_get_nblocksx(texture->format, surface->width),
573 util_format_get_nblocksy(texture->format, surface->height),
574 transfer->stride,
575 data);
576
577 pipe->transfer_unmap(pipe, transfer);
578 error:
579 pipe->transfer_destroy(pipe, transfer);
580 }
581
582
583 void debug_dump_texture(struct pipe_context *pipe,
584 const char *prefix,
585 struct pipe_resource *texture)
586 {
587 struct pipe_surface *surface, surf_tmpl;
588
589 if (!texture)
590 return;
591
592 /* XXX for now, just dump image for layer=0, level=0 */
593 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
594 u_surface_default_template(&surf_tmpl, texture, 0 /* no bind flag - not a surface */);
595 surface = pipe->create_surface(pipe, texture, &surf_tmpl);
596 if (surface) {
597 debug_dump_surface(pipe, prefix, surface);
598 pipe->surface_destroy(pipe, surface);
599 }
600 }
601
602
603 #pragma pack(push,2)
604 struct bmp_file_header {
605 uint16_t bfType;
606 uint32_t bfSize;
607 uint16_t bfReserved1;
608 uint16_t bfReserved2;
609 uint32_t bfOffBits;
610 };
611 #pragma pack(pop)
612
613 struct bmp_info_header {
614 uint32_t biSize;
615 int32_t biWidth;
616 int32_t biHeight;
617 uint16_t biPlanes;
618 uint16_t biBitCount;
619 uint32_t biCompression;
620 uint32_t biSizeImage;
621 int32_t biXPelsPerMeter;
622 int32_t biYPelsPerMeter;
623 uint32_t biClrUsed;
624 uint32_t biClrImportant;
625 };
626
627 struct bmp_rgb_quad {
628 uint8_t rgbBlue;
629 uint8_t rgbGreen;
630 uint8_t rgbRed;
631 uint8_t rgbAlpha;
632 };
633
634 void
635 debug_dump_surface_bmp(struct pipe_context *pipe,
636 const char *filename,
637 struct pipe_surface *surface)
638 {
639 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
640 struct pipe_transfer *transfer;
641 struct pipe_resource *texture = surface->texture;
642
643 transfer = pipe_get_transfer(pipe, texture, surface->u.tex.level,
644 surface->u.tex.first_layer, PIPE_TRANSFER_READ,
645 0, 0, surface->width, surface->height);
646
647 debug_dump_transfer_bmp(pipe, filename, transfer);
648
649 pipe->transfer_destroy(pipe, transfer);
650 #endif
651 }
652
653 void
654 debug_dump_transfer_bmp(struct pipe_context *pipe,
655 const char *filename,
656 struct pipe_transfer *transfer)
657 {
658 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
659 float *rgba;
660
661 if (!transfer)
662 goto error1;
663
664 rgba = MALLOC(transfer->box.width *
665 transfer->box.height *
666 transfer->box.depth *
667 4*sizeof(float));
668 if(!rgba)
669 goto error1;
670
671 pipe_get_tile_rgba(pipe, transfer, 0, 0,
672 transfer->box.width, transfer->box.height,
673 rgba);
674
675 debug_dump_float_rgba_bmp(filename,
676 transfer->box.width, transfer->box.height,
677 rgba, transfer->box.width);
678
679 FREE(rgba);
680 error1:
681 ;
682 #endif
683 }
684
685 void
686 debug_dump_float_rgba_bmp(const char *filename,
687 unsigned width, unsigned height,
688 float *rgba, unsigned stride)
689 {
690 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
691 struct os_stream *stream;
692 struct bmp_file_header bmfh;
693 struct bmp_info_header bmih;
694 unsigned x, y;
695
696 if(!rgba)
697 goto error1;
698
699 bmfh.bfType = 0x4d42;
700 bmfh.bfSize = 14 + 40 + height*width*4;
701 bmfh.bfReserved1 = 0;
702 bmfh.bfReserved2 = 0;
703 bmfh.bfOffBits = 14 + 40;
704
705 bmih.biSize = 40;
706 bmih.biWidth = width;
707 bmih.biHeight = height;
708 bmih.biPlanes = 1;
709 bmih.biBitCount = 32;
710 bmih.biCompression = 0;
711 bmih.biSizeImage = height*width*4;
712 bmih.biXPelsPerMeter = 0;
713 bmih.biYPelsPerMeter = 0;
714 bmih.biClrUsed = 0;
715 bmih.biClrImportant = 0;
716
717 stream = os_file_stream_create(filename);
718 if(!stream)
719 goto error1;
720
721 os_stream_write(stream, &bmfh, 14);
722 os_stream_write(stream, &bmih, 40);
723
724 y = height;
725 while(y--) {
726 float *ptr = rgba + (stride * y * 4);
727 for(x = 0; x < width; ++x)
728 {
729 struct bmp_rgb_quad pixel;
730 pixel.rgbRed = float_to_ubyte(ptr[x*4 + 0]);
731 pixel.rgbGreen = float_to_ubyte(ptr[x*4 + 1]);
732 pixel.rgbBlue = float_to_ubyte(ptr[x*4 + 2]);
733 pixel.rgbAlpha = 255;
734 os_stream_write(stream, &pixel, 4);
735 }
736 }
737
738 os_stream_close(stream);
739 error1:
740 ;
741 #endif
742 }
743
744 #endif