1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 #include "pipe/p_config.h"
34 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
39 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
41 #ifndef WIN32_LEAN_AND_MEAN
42 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
55 #include "pipe/p_compiler.h"
56 #include "pipe/p_util.h"
57 #include "pipe/p_debug.h"
58 #include "pipe/p_format.h"
59 #include "util/u_string.h"
62 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
64 _EngDebugPrint(const char *format
, ...)
68 EngDebugPrint("", (PCHAR
)format
, ap
);
74 void _debug_vprintf(const char *format
, va_list ap
)
76 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
78 /* EngDebugPrint does not handle float point arguments, so we need to use
79 * our own vsnprintf implementation. It is also very slow, so buffer until
80 * we find a newline. */
81 static char buf
[512 + 1] = {'\0'};
82 size_t len
= strlen(buf
);
83 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
84 if(ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
85 _EngDebugPrint("%s", buf
);
89 /* TODO: Implement debug print for WINCE */
91 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
92 /* EngDebugPrint does not handle float point arguments, so we need to use
93 * our own vsnprintf implementation. It is also very slow, so buffer until
94 * we find a newline. */
95 static char buf
[512 + 1] = {'\0'};
96 size_t len
= strlen(buf
);
97 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
98 if(ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
99 OutputDebugStringA(buf
);
103 vfprintf(stderr
, format
, ap
);
109 void debug_print_blob( const char *name
,
113 const unsigned *ublob
= (const unsigned *)blob
;
116 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
117 size
%4 ? "... plus a few bytes" : "");
119 for (i
= 0; i
< size
/4; i
++) {
120 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
126 void _debug_break(void)
128 #if defined(PIPE_ARCH_X86) && defined(PIPE_CC_GCC)
130 #elif defined(PIPE_ARCH_X86) && defined(PIPE_CC_MSVC)
132 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
140 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
142 find(const char *start
, const char *end
, char c
)
145 for(p
= start
; !end
|| p
!= end
; ++p
) {
155 compare(const char *start
, const char *end
, const char *s
)
158 for(p
= start
, q
= s
; p
!= end
&& *q
!= '\0'; ++p
, ++q
) {
162 return p
== end
&& *q
== '\0';
166 copy(char *dst
, const char *start
, const char *end
, size_t n
)
170 for(p
= start
, q
= dst
, n
= n
- 1; p
!= end
&& n
; ++p
, ++q
, --n
)
177 static INLINE
const char *
178 _debug_get_option(const char *name
)
180 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
181 /* EngMapFile creates the file if it does not exists, so it must either be
182 * disabled on release versions (or put in a less conspicuous place). */
184 const char *result
= NULL
;
186 const void *pMap
= NULL
;
187 const char *sol
, *eol
, *sep
;
188 static char output
[1024];
190 pMap
= EngMapFile(L
"\\??\\c:\\gallium.cfg", 0, &iFile
);
192 sol
= (const char *)pMap
;
194 /* TODO: handle LF line endings */
195 eol
= find(sol
, NULL
, '\r');
196 if(!eol
|| eol
== sol
)
198 sep
= find(sol
, eol
, '=');
201 if(compare(sol
, sep
, name
)) {
202 copy(output
, sep
+ 1, eol
, sizeof(output
));
214 #elif defined(PIPE_SUBSYSTEM_WINDOWS_CE)
215 /* TODO: implement */
223 debug_get_option(const char *name
, const char *dfault
)
227 result
= _debug_get_option(name
);
231 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? result
: "(null)");
237 debug_get_bool_option(const char *name
, boolean dfault
)
239 const char *str
= _debug_get_option(name
);
244 else if(!util_strcmp(str
, "n"))
246 else if(!util_strcmp(str
, "no"))
248 else if(!util_strcmp(str
, "0"))
250 else if(!util_strcmp(str
, "f"))
252 else if(!util_strcmp(str
, "false"))
257 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? "TRUE" : "FALSE");
264 debug_get_num_option(const char *name
, long dfault
)
269 str
= _debug_get_option(name
);
284 while('0' <= c
&& c
<= '9') {
285 result
= result
*10 + (c
- '0');
291 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
298 debug_get_flags_option(const char *name
,
299 const struct debug_named_value
*flags
,
300 unsigned long dfault
)
302 unsigned long result
;
305 str
= _debug_get_option(name
);
310 while( flags
->name
) {
311 if (!util_strcmp(str
, "all") || util_strstr(str
, flags
->name
))
312 result
|= flags
->value
;
317 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__
, name
, result
);
323 void _debug_assert_fail(const char *expr
,
326 const char *function
)
328 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file
, line
, function
, expr
);
329 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE
))
332 _debug_printf("continuing...\n");
337 debug_dump_enum(const struct debug_named_value
*names
,
340 static char rest
[256];
343 if(names
->value
== value
)
348 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
354 debug_dump_flags(const struct debug_named_value
*names
,
357 static char output
[4096];
358 static char rest
[256];
364 if((names
->value
& value
) == names
->value
) {
366 util_strncat(output
, "|", sizeof(output
));
369 util_strncat(output
, names
->name
, sizeof(output
));
370 value
&= ~names
->value
;
377 util_strncat(output
, "|", sizeof(output
));
381 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
382 util_strncat(output
, rest
, sizeof(output
));
392 static const struct debug_named_value pipe_format_names
[] = {
394 DEBUG_NAMED_VALUE(PIPE_FORMAT_NONE
),
395 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8R8G8B8_UNORM
),
396 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8R8G8B8_UNORM
),
397 DEBUG_NAMED_VALUE(PIPE_FORMAT_B8G8R8A8_UNORM
),
398 DEBUG_NAMED_VALUE(PIPE_FORMAT_B8G8R8X8_UNORM
),
399 DEBUG_NAMED_VALUE(PIPE_FORMAT_A1R5G5B5_UNORM
),
400 DEBUG_NAMED_VALUE(PIPE_FORMAT_A4R4G4B4_UNORM
),
401 DEBUG_NAMED_VALUE(PIPE_FORMAT_R5G6B5_UNORM
),
402 DEBUG_NAMED_VALUE(PIPE_FORMAT_A2B10G10R10_UNORM
),
403 DEBUG_NAMED_VALUE(PIPE_FORMAT_L8_UNORM
),
404 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8_UNORM
),
405 DEBUG_NAMED_VALUE(PIPE_FORMAT_I8_UNORM
),
406 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8L8_UNORM
),
407 DEBUG_NAMED_VALUE(PIPE_FORMAT_L16_UNORM
),
408 DEBUG_NAMED_VALUE(PIPE_FORMAT_YCBCR
),
409 DEBUG_NAMED_VALUE(PIPE_FORMAT_YCBCR_REV
),
410 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z16_UNORM
),
411 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z32_UNORM
),
412 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z32_FLOAT
),
413 DEBUG_NAMED_VALUE(PIPE_FORMAT_S8Z24_UNORM
),
414 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z24S8_UNORM
),
415 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8Z24_UNORM
),
416 DEBUG_NAMED_VALUE(PIPE_FORMAT_Z24X8_UNORM
),
417 DEBUG_NAMED_VALUE(PIPE_FORMAT_S8_UNORM
),
418 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64_FLOAT
),
419 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64G64_FLOAT
),
420 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64G64B64_FLOAT
),
421 DEBUG_NAMED_VALUE(PIPE_FORMAT_R64G64B64A64_FLOAT
),
422 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_FLOAT
),
423 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_FLOAT
),
424 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_FLOAT
),
425 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_FLOAT
),
426 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_UNORM
),
427 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_UNORM
),
428 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_UNORM
),
429 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_UNORM
),
430 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_USCALED
),
431 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_USCALED
),
432 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_USCALED
),
433 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_USCALED
),
434 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_SNORM
),
435 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_SNORM
),
436 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_SNORM
),
437 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_SNORM
),
438 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32_SSCALED
),
439 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32_SSCALED
),
440 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32_SSCALED
),
441 DEBUG_NAMED_VALUE(PIPE_FORMAT_R32G32B32A32_SSCALED
),
442 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_UNORM
),
443 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_UNORM
),
444 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_UNORM
),
445 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_UNORM
),
446 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_USCALED
),
447 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_USCALED
),
448 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_USCALED
),
449 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_USCALED
),
450 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_SNORM
),
451 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_SNORM
),
452 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_SNORM
),
453 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_SNORM
),
454 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16_SSCALED
),
455 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16_SSCALED
),
456 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16_SSCALED
),
457 DEBUG_NAMED_VALUE(PIPE_FORMAT_R16G16B16A16_SSCALED
),
458 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_UNORM
),
459 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_UNORM
),
460 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_UNORM
),
461 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_UNORM
),
462 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_UNORM
),
463 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_USCALED
),
464 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_USCALED
),
465 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_USCALED
),
466 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_USCALED
),
467 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_USCALED
),
468 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_SNORM
),
469 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_SNORM
),
470 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_SNORM
),
471 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_SNORM
),
472 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_SNORM
),
473 DEBUG_NAMED_VALUE(PIPE_FORMAT_B6G5R5_SNORM
),
474 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8B8G8R8_SNORM
),
475 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8B8G8R8_SNORM
),
476 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8_SSCALED
),
477 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8_SSCALED
),
478 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_SSCALED
),
479 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_SSCALED
),
480 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_SSCALED
),
481 DEBUG_NAMED_VALUE(PIPE_FORMAT_L8_SRGB
),
482 DEBUG_NAMED_VALUE(PIPE_FORMAT_A8_L8_SRGB
),
483 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8_SRGB
),
484 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8A8_SRGB
),
485 DEBUG_NAMED_VALUE(PIPE_FORMAT_R8G8B8X8_SRGB
),
486 DEBUG_NAMED_VALUE(PIPE_FORMAT_X8UB8UG8SR8S_NORM
),
487 DEBUG_NAMED_VALUE(PIPE_FORMAT_B6UG5SR5S_NORM
),
488 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT1_RGB
),
489 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT1_RGBA
),
490 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT3_RGBA
),
491 DEBUG_NAMED_VALUE(PIPE_FORMAT_DXT5_RGBA
),
493 DEBUG_NAMED_VALUE_END
497 void debug_print_format(const char *msg
, unsigned fmt
)
499 debug_printf("%s: %s\n", msg
, debug_dump_enum(pipe_format_names
, fmt
));
503 char *pf_sprint_name( char *str
, enum pipe_format format
)
505 strcpy( str
, debug_dump_enum(pipe_format_names
, format
) );
511 void debug_dump_image(const char *prefix
,
512 unsigned format
, unsigned cpp
,
513 unsigned width
, unsigned height
,
517 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
518 static unsigned no
= 0;
520 WCHAR wfilename
[sizeof(filename
)];
528 unsigned char *pMap
= NULL
;
531 util_snprintf(filename
, sizeof(filename
), "\\??\\c:\\%03u%s.raw", ++no
, prefix
);
532 for(i
= 0; i
< sizeof(filename
); ++i
)
533 wfilename
[i
] = (WCHAR
)filename
[i
];
535 pMap
= (unsigned char *)EngMapFile(wfilename
, sizeof(header
) + cpp
*width
*height
, &iFile
);
539 header
.format
= format
;
541 header
.width
= width
;
542 header
.height
= height
;
543 memcpy(pMap
, &header
, sizeof(header
));
544 pMap
+= sizeof(header
);
546 for(i
= 0; i
< height
; ++i
) {
547 memcpy(pMap
, (unsigned char *)data
+ cpp
*pitch
*i
, cpp
*width
);