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