1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
4 * Copyright (c) 2008 VMware, Inc.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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 VMWARE 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.
27 **************************************************************************/
30 #include "pipe/p_config.h"
32 #include "pipe/p_compiler.h"
33 #include "util/u_debug.h"
34 #include "pipe/p_format.h"
35 #include "pipe/p_state.h"
36 #include "util/u_inlines.h"
37 #include "util/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_string.h"
40 #include "util/u_math.h"
41 #include "util/u_tile.h"
42 #include "util/u_prim.h"
43 #include "util/u_surface.h"
46 #include <limits.h> /* CHAR_BIT */
47 #include <ctype.h> /* isalnum */
55 void _debug_vprintf(const char *format
, va_list ap
)
57 static char buf
[4096] = {'\0'};
58 #if defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_EMBEDDED)
59 /* We buffer until we find a newline. */
60 size_t len
= strlen(buf
);
61 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
62 if(ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
67 util_vsnprintf(buf
, sizeof(buf
), format
, ap
);
74 debug_disable_error_message_boxes(void)
77 /* When Windows' error message boxes are disabled for this process (as is
78 * typically the case when running tests in an automated fashion) we disable
79 * CRT message boxes too.
81 UINT uMode
= SetErrorMode(0);
83 if (uMode
& SEM_FAILCRITICALERRORS
) {
84 /* Disable assertion failure message box.
85 * http://msdn.microsoft.com/en-us/library/sas1dkb2.aspx
87 _set_error_mode(_OUT_TO_STDERR
);
89 /* Disable abort message box.
90 * http://msdn.microsoft.com/en-us/library/e631wekh.aspx
92 _set_abort_behavior(0, _WRITE_ABORT_MSG
| _CALL_REPORTFAULT
);
100 void debug_print_blob( const char *name
,
104 const unsigned *ublob
= (const unsigned *)blob
;
107 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
108 size
%4 ? "... plus a few bytes" : "");
110 for (i
= 0; i
< size
/4; i
++) {
111 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
118 debug_get_option_should_print(void)
120 static boolean first
= TRUE
;
121 static boolean value
= FALSE
;
126 /* Oh hey this will call into this function,
127 * but its cool since we set first to false
130 value
= debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE
);
131 /* XXX should we print this option? Currently it wont */
136 debug_get_option(const char *name
, const char *dfault
)
140 result
= os_get_option(name
);
144 if (debug_get_option_should_print())
145 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? result
: "(null)");
151 debug_get_bool_option(const char *name
, boolean dfault
)
153 const char *str
= os_get_option(name
);
158 else if(!util_strcmp(str
, "n"))
160 else if(!util_strcmp(str
, "no"))
162 else if(!util_strcmp(str
, "0"))
164 else if(!util_strcmp(str
, "f"))
166 else if(!util_strcmp(str
, "F"))
168 else if(!util_strcmp(str
, "false"))
170 else if(!util_strcmp(str
, "FALSE"))
175 if (debug_get_option_should_print())
176 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? "TRUE" : "FALSE");
183 debug_get_num_option(const char *name
, long dfault
)
188 str
= os_get_option(name
);
203 while('0' <= c
&& c
<= '9') {
204 result
= result
*10 + (c
- '0');
210 if (debug_get_option_should_print())
211 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
216 static boolean
str_has_option(const char *str
, const char *name
)
224 if (!util_strcmp(str
, "all")) {
228 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
230 const char *start
= str
;
231 unsigned name_len
= strlen(name
);
233 /* 'start' is the beginning of the currently-parsed word,
234 * we increment 'str' each iteration.
235 * if we find either the end of string or a non-alphanumeric character,
236 * we compare 'start' up to 'str-1' with 'name'. */
239 if (!*str
|| !(isalnum(*str
) || *str
== '_')) {
240 if (str
-start
== name_len
&&
241 !memcmp(start
, name
, name_len
)) {
260 debug_get_flags_option(const char *name
,
261 const struct debug_named_value
*flags
,
262 unsigned long dfault
)
264 unsigned long result
;
266 const struct debug_named_value
*orig
= flags
;
267 unsigned namealign
= 0;
269 str
= os_get_option(name
);
272 else if (!util_strcmp(str
, "help")) {
274 _debug_printf("%s: help for %s:\n", __FUNCTION__
, name
);
275 for (; flags
->name
; ++flags
)
276 namealign
= MAX2(namealign
, strlen(flags
->name
));
277 for (flags
= orig
; flags
->name
; ++flags
)
278 _debug_printf("| %*s [0x%0*lx]%s%s\n", namealign
, flags
->name
,
279 (int)sizeof(unsigned long)*CHAR_BIT
/4, flags
->value
,
280 flags
->desc
? " " : "", flags
->desc
? flags
->desc
: "");
284 while( flags
->name
) {
285 if (str_has_option(str
, flags
->name
))
286 result
|= flags
->value
;
291 if (debug_get_option_should_print()) {
293 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__
, name
, result
, str
);
295 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__
, name
, result
);
303 void _debug_assert_fail(const char *expr
,
306 const char *function
)
308 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file
, line
, function
, expr
);
314 debug_dump_enum(const struct debug_named_value
*names
,
317 static char rest
[64];
320 if(names
->value
== value
)
325 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
331 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
335 static char rest
[64];
338 if(names
->value
== value
) {
339 const char *name
= names
->name
;
340 while (*name
== *prefix
) {
351 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
357 debug_dump_flags(const struct debug_named_value
*names
,
360 static char output
[4096];
361 static char rest
[256];
367 if((names
->value
& value
) == names
->value
) {
369 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
372 util_strncat(output
, names
->name
, sizeof(output
) - strlen(output
) - 1);
373 output
[sizeof(output
) - 1] = '\0';
374 value
&= ~names
->value
;
381 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
385 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
386 util_strncat(output
, rest
, sizeof(output
) - strlen(output
) - 1);
387 output
[sizeof(output
) - 1] = '\0';
398 void debug_print_format(const char *msg
, unsigned fmt
)
400 debug_printf("%s: %s\n", msg
, util_format_name(fmt
));
405 /** Return string name of given primitive type */
407 u_prim_name(unsigned prim
)
409 static const struct debug_named_value names
[] = {
410 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS
),
411 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES
),
412 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP
),
413 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP
),
414 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES
),
415 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP
),
416 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN
),
417 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS
),
418 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP
),
419 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON
),
420 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES_ADJACENCY
),
421 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP_ADJACENCY
),
422 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES_ADJACENCY
),
423 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
),
424 DEBUG_NAMED_VALUE_END
426 return debug_dump_enum(names
, prim
);
433 const char* fl_function
[1024];
435 int debug_funclog_enter(const char* f
, const int line
, const char* file
)
439 for (i
= 0; i
< fl_indent
; i
++)
441 debug_printf("%s\n", f
);
443 assert(fl_indent
< 1023);
444 fl_function
[fl_indent
++] = f
;
449 void debug_funclog_exit(const char* f
, const int line
, const char* file
)
452 assert(fl_indent
>= 0);
453 assert(fl_function
[fl_indent
] == f
);
456 void debug_funclog_enter_exit(const char* f
, const int line
, const char* file
)
459 for (i
= 0; i
< fl_indent
; i
++)
461 debug_printf("%s\n", f
);
469 * Dump an image to .ppm file.
470 * \param format PIPE_FORMAT_x
471 * \param cpp bytes per pixel
472 * \param width width in pixels
473 * \param height height in pixels
474 * \param stride row stride in bytes
476 void debug_dump_image(const char *prefix
,
477 enum pipe_format format
, unsigned cpp
,
478 unsigned width
, unsigned height
,
482 /* write a ppm file */
487 util_snprintf(filename
, sizeof(filename
), "%s.ppm", prefix
);
489 rgb8
= MALLOC(height
* width
* 3);
494 util_format_translate(
495 PIPE_FORMAT_R8G8B8_UNORM
,
500 0, 0, width
, height
);
502 /* Must be opened in binary mode or DOS line ending causes data
503 * to be read with one byte offset.
505 f
= fopen(filename
, "wb");
508 fprintf(f
, "# ppm-file created by gallium\n");
509 fprintf(f
, "%i %i\n", width
, height
);
511 fwrite(rgb8
, 1, height
* width
* 3, f
);
515 fprintf(stderr
, "Can't open %s for writing\n", filename
);
521 /* FIXME: dump resources, not surfaces... */
522 void debug_dump_surface(struct pipe_context
*pipe
,
524 struct pipe_surface
*surface
)
526 struct pipe_resource
*texture
;
527 struct pipe_transfer
*transfer
;
533 /* XXX: this doesn't necessarily work, as the driver may be using
534 * temporary storage for the surface which hasn't been propagated
535 * back into the texture. Need to nail down the semantics of views
536 * and transfers a bit better before we can say if extra work needs
539 texture
= surface
->texture
;
541 data
= pipe_transfer_map(pipe
, texture
, surface
->u
.tex
.level
,
542 surface
->u
.tex
.first_layer
,
544 0, 0, surface
->width
, surface
->height
, &transfer
);
548 debug_dump_image(prefix
,
550 util_format_get_blocksize(texture
->format
),
551 util_format_get_nblocksx(texture
->format
, surface
->width
),
552 util_format_get_nblocksy(texture
->format
, surface
->height
),
556 pipe
->transfer_unmap(pipe
, transfer
);
560 void debug_dump_texture(struct pipe_context
*pipe
,
562 struct pipe_resource
*texture
)
564 struct pipe_surface
*surface
, surf_tmpl
;
569 /* XXX for now, just dump image for layer=0, level=0 */
570 u_surface_default_template(&surf_tmpl
, texture
);
571 surface
= pipe
->create_surface(pipe
, texture
, &surf_tmpl
);
573 debug_dump_surface(pipe
, prefix
, surface
);
574 pipe
->surface_destroy(pipe
, surface
);
580 struct bmp_file_header
{
583 uint16_t bfReserved1
;
584 uint16_t bfReserved2
;
589 struct bmp_info_header
{
595 uint32_t biCompression
;
596 uint32_t biSizeImage
;
597 int32_t biXPelsPerMeter
;
598 int32_t biYPelsPerMeter
;
600 uint32_t biClrImportant
;
603 struct bmp_rgb_quad
{
611 debug_dump_surface_bmp(struct pipe_context
*pipe
,
612 const char *filename
,
613 struct pipe_surface
*surface
)
615 struct pipe_transfer
*transfer
;
616 struct pipe_resource
*texture
= surface
->texture
;
619 ptr
= pipe_transfer_map(pipe
, texture
, surface
->u
.tex
.level
,
620 surface
->u
.tex
.first_layer
, PIPE_TRANSFER_READ
,
621 0, 0, surface
->width
, surface
->height
, &transfer
);
623 debug_dump_transfer_bmp(pipe
, filename
, transfer
, ptr
);
625 pipe
->transfer_unmap(pipe
, transfer
);
629 debug_dump_transfer_bmp(struct pipe_context
*pipe
,
630 const char *filename
,
631 struct pipe_transfer
*transfer
, void *ptr
)
638 rgba
= MALLOC(transfer
->box
.width
*
639 transfer
->box
.height
*
640 transfer
->box
.depth
*
645 pipe_get_tile_rgba(transfer
, ptr
, 0, 0,
646 transfer
->box
.width
, transfer
->box
.height
,
649 debug_dump_float_rgba_bmp(filename
,
650 transfer
->box
.width
, transfer
->box
.height
,
651 rgba
, transfer
->box
.width
);
659 debug_dump_float_rgba_bmp(const char *filename
,
660 unsigned width
, unsigned height
,
661 float *rgba
, unsigned stride
)
664 struct bmp_file_header bmfh
;
665 struct bmp_info_header bmih
;
671 bmfh
.bfType
= 0x4d42;
672 bmfh
.bfSize
= 14 + 40 + height
*width
*4;
673 bmfh
.bfReserved1
= 0;
674 bmfh
.bfReserved2
= 0;
675 bmfh
.bfOffBits
= 14 + 40;
678 bmih
.biWidth
= width
;
679 bmih
.biHeight
= height
;
681 bmih
.biBitCount
= 32;
682 bmih
.biCompression
= 0;
683 bmih
.biSizeImage
= height
*width
*4;
684 bmih
.biXPelsPerMeter
= 0;
685 bmih
.biYPelsPerMeter
= 0;
687 bmih
.biClrImportant
= 0;
689 stream
= fopen(filename
, "wb");
693 fwrite(&bmfh
, 14, 1, stream
);
694 fwrite(&bmih
, 40, 1, stream
);
698 float *ptr
= rgba
+ (stride
* y
* 4);
699 for(x
= 0; x
< width
; ++x
)
701 struct bmp_rgb_quad pixel
;
702 pixel
.rgbRed
= float_to_ubyte(ptr
[x
*4 + 0]);
703 pixel
.rgbGreen
= float_to_ubyte(ptr
[x
*4 + 1]);
704 pixel
.rgbBlue
= float_to_ubyte(ptr
[x
*4 + 2]);
705 pixel
.rgbAlpha
= float_to_ubyte(ptr
[x
*4 + 3]);
706 fwrite(&pixel
, 1, 4, stream
);
717 * Print PIPE_TRANSFER_x flags with a message.
720 debug_print_transfer_flags(const char *msg
, unsigned usage
)
722 static const struct debug_named_value names
[] = {
723 DEBUG_NAMED_VALUE(PIPE_TRANSFER_READ
),
724 DEBUG_NAMED_VALUE(PIPE_TRANSFER_WRITE
),
725 DEBUG_NAMED_VALUE(PIPE_TRANSFER_MAP_DIRECTLY
),
726 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DISCARD_RANGE
),
727 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DONTBLOCK
),
728 DEBUG_NAMED_VALUE(PIPE_TRANSFER_UNSYNCHRONIZED
),
729 DEBUG_NAMED_VALUE(PIPE_TRANSFER_FLUSH_EXPLICIT
),
730 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
),
731 DEBUG_NAMED_VALUE(PIPE_TRANSFER_PERSISTENT
),
732 DEBUG_NAMED_VALUE(PIPE_TRANSFER_COHERENT
),
733 DEBUG_NAMED_VALUE_END
736 debug_printf("%s: %s\n", msg
, debug_dump_flags(names
, usage
));