util/u_debug: use MAX2 macro
[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
178 unsigned long
179 debug_get_flags_option(const char *name,
180 const struct debug_named_value *flags,
181 unsigned long dfault)
182 {
183 unsigned long result;
184 const char *str;
185 const struct debug_named_value *orig = flags;
186 int namealign = 0;
187
188 str = os_get_option(name);
189 if(!str)
190 result = dfault;
191 else if (!util_strcmp(str, "help")) {
192 result = dfault;
193 debug_printf("%s: help for %s:\n", __FUNCTION__, name);
194 for (; flags->name; ++flags)
195 namealign = MAX2(namealign, strlen(flags->name));
196 for (flags = orig; flags->name; ++flags)
197 debug_printf("| %*s [0x%0*lx]%s%s\n", namealign, flags->name,
198 sizeof(unsigned long)*CHAR_BIT/4, flags->value,
199 flags->desc ? " " : "", flags->desc ? flags->desc : "");
200 }
201 else {
202 result = 0;
203 while( flags->name ) {
204 if (!util_strcmp(str, "all") || util_strstr(str, flags->name ))
205 result |= flags->value;
206 ++flags;
207 }
208 }
209
210 if (debug_get_option_should_print()) {
211 if (str) {
212 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__, name, result, str);
213 } else {
214 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__, name, result);
215 }
216 }
217
218 return result;
219 }
220
221
222 void _debug_assert_fail(const char *expr,
223 const char *file,
224 unsigned line,
225 const char *function)
226 {
227 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file, line, function, expr);
228 #if defined(PIPE_OS_WINDOWS) && !defined(PIPE_SUBSYSTEM_WINDOWS_USER)
229 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", FALSE))
230 #else
231 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE))
232 #endif
233 os_abort();
234 else
235 _debug_printf("continuing...\n");
236 }
237
238
239 const char *
240 debug_dump_enum(const struct debug_named_value *names,
241 unsigned long value)
242 {
243 static char rest[64];
244
245 while(names->name) {
246 if(names->value == value)
247 return names->name;
248 ++names;
249 }
250
251 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
252 return rest;
253 }
254
255
256 const char *
257 debug_dump_enum_noprefix(const struct debug_named_value *names,
258 const char *prefix,
259 unsigned long value)
260 {
261 static char rest[64];
262
263 while(names->name) {
264 if(names->value == value) {
265 const char *name = names->name;
266 while (*name == *prefix) {
267 name++;
268 prefix++;
269 }
270 return name;
271 }
272 ++names;
273 }
274
275
276
277 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
278 return rest;
279 }
280
281
282 const char *
283 debug_dump_flags(const struct debug_named_value *names,
284 unsigned long value)
285 {
286 static char output[4096];
287 static char rest[256];
288 int first = 1;
289
290 output[0] = '\0';
291
292 while(names->name) {
293 if((names->value & value) == names->value) {
294 if (!first)
295 util_strncat(output, "|", sizeof(output));
296 else
297 first = 0;
298 util_strncat(output, names->name, sizeof(output) - 1);
299 output[sizeof(output) - 1] = '\0';
300 value &= ~names->value;
301 }
302 ++names;
303 }
304
305 if (value) {
306 if (!first)
307 util_strncat(output, "|", sizeof(output));
308 else
309 first = 0;
310
311 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
312 util_strncat(output, rest, sizeof(output) - 1);
313 output[sizeof(output) - 1] = '\0';
314 }
315
316 if(first)
317 return "0";
318
319 return output;
320 }
321
322
323 #ifdef DEBUG
324 void debug_print_format(const char *msg, unsigned fmt )
325 {
326 debug_printf("%s: %s\n", msg, util_format_name(fmt));
327 }
328 #endif
329
330
331
332 static const struct debug_named_value pipe_prim_names[] = {
333 #ifdef DEBUG
334 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS),
335 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES),
336 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP),
337 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP),
338 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES),
339 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP),
340 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN),
341 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS),
342 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP),
343 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON),
344 #endif
345 DEBUG_NAMED_VALUE_END
346 };
347
348
349 const char *u_prim_name( unsigned prim )
350 {
351 return debug_dump_enum(pipe_prim_names, prim);
352 }
353
354
355
356
357 #ifdef DEBUG
358 /**
359 * Dump an image to a .raw or .ppm file (depends on OS).
360 * \param format PIPE_FORMAT_x
361 * \param cpp bytes per pixel
362 * \param width width in pixels
363 * \param height height in pixels
364 * \param stride row stride in bytes
365 */
366 void debug_dump_image(const char *prefix,
367 unsigned format, unsigned cpp,
368 unsigned width, unsigned height,
369 unsigned stride,
370 const void *data)
371 {
372 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
373 static unsigned no = 0;
374 char filename[256];
375 WCHAR wfilename[sizeof(filename)];
376 ULONG_PTR iFile = 0;
377 struct {
378 unsigned format;
379 unsigned cpp;
380 unsigned width;
381 unsigned height;
382 } header;
383 unsigned char *pMap = NULL;
384 unsigned i;
385
386 util_snprintf(filename, sizeof(filename), "\\??\\c:\\%03u%s.raw", ++no, prefix);
387 for(i = 0; i < sizeof(filename); ++i)
388 wfilename[i] = (WCHAR)filename[i];
389
390 pMap = (unsigned char *)EngMapFile(wfilename, sizeof(header) + height*width*cpp, &iFile);
391 if(!pMap)
392 return;
393
394 header.format = format;
395 header.cpp = cpp;
396 header.width = width;
397 header.height = height;
398 memcpy(pMap, &header, sizeof(header));
399 pMap += sizeof(header);
400
401 for(i = 0; i < height; ++i) {
402 memcpy(pMap, (unsigned char *)data + stride*i, cpp*width);
403 pMap += cpp*width;
404 }
405
406 EngUnmapFile(iFile);
407 #elif defined(PIPE_OS_UNIX)
408 /* write a ppm file */
409 char filename[256];
410 FILE *f;
411
412 util_snprintf(filename, sizeof(filename), "%s.ppm", prefix);
413
414 f = fopen(filename, "w");
415 if (f) {
416 int i, x, y;
417 int r, g, b;
418 const uint8_t *ptr = (uint8_t *) data;
419
420 /* XXX this is a hack */
421 switch (format) {
422 case PIPE_FORMAT_B8G8R8A8_UNORM:
423 r = 2;
424 g = 1;
425 b = 0;
426 break;
427 default:
428 r = 0;
429 g = 1;
430 b = 1;
431 }
432
433 fprintf(f, "P6\n");
434 fprintf(f, "# ppm-file created by osdemo.c\n");
435 fprintf(f, "%i %i\n", width, height);
436 fprintf(f, "255\n");
437 fclose(f);
438
439 f = fopen(filename, "ab"); /* reopen in binary append mode */
440 for (y = 0; y < height; y++) {
441 for (x = 0; x < width; x++) {
442 i = y * stride + x * cpp;
443 fputc(ptr[i + r], f); /* write red */
444 fputc(ptr[i + g], f); /* write green */
445 fputc(ptr[i + b], f); /* write blue */
446 }
447 }
448 fclose(f);
449 }
450 else {
451 fprintf(stderr, "Can't open %s for writing\n", filename);
452 }
453 #endif
454 }
455
456 void debug_dump_surface(struct pipe_context *pipe,
457 const char *prefix,
458 struct pipe_surface *surface)
459 {
460 struct pipe_resource *texture;
461 struct pipe_transfer *transfer;
462 void *data;
463
464 if (!surface)
465 return;
466
467 /* XXX: this doesn't necessarily work, as the driver may be using
468 * temporary storage for the surface which hasn't been propagated
469 * back into the texture. Need to nail down the semantics of views
470 * and transfers a bit better before we can say if extra work needs
471 * to be done here:
472 */
473 texture = surface->texture;
474
475 transfer = pipe_get_transfer(pipe, texture, surface->face,
476 surface->level, surface->zslice,
477 PIPE_TRANSFER_READ, 0, 0, surface->width,
478 surface->height);
479
480 data = pipe->transfer_map(pipe, transfer);
481 if(!data)
482 goto error;
483
484 debug_dump_image(prefix,
485 texture->format,
486 util_format_get_blocksize(texture->format),
487 util_format_get_nblocksx(texture->format, surface->width),
488 util_format_get_nblocksy(texture->format, surface->height),
489 transfer->stride,
490 data);
491
492 pipe->transfer_unmap(pipe, transfer);
493 error:
494 pipe->transfer_destroy(pipe, transfer);
495 }
496
497
498 void debug_dump_texture(struct pipe_context *pipe,
499 const char *prefix,
500 struct pipe_resource *texture)
501 {
502 struct pipe_surface *surface;
503 struct pipe_screen *screen;
504
505 if (!texture)
506 return;
507
508 screen = texture->screen;
509
510 /* XXX for now, just dump image for face=0, level=0 */
511 surface = screen->get_tex_surface(screen, texture, 0, 0, 0,
512 PIPE_BIND_SAMPLER_VIEW);
513 if (surface) {
514 debug_dump_surface(pipe, prefix, surface);
515 screen->tex_surface_destroy(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->face,
561 surface->level, surface->zslice,
562 PIPE_TRANSFER_READ, 0, 0, surface->width,
563 surface->height);
564
565 debug_dump_transfer_bmp(pipe, filename, transfer);
566
567 pipe->transfer_destroy(pipe, transfer);
568 #endif
569 }
570
571 void
572 debug_dump_transfer_bmp(struct pipe_context *pipe,
573 const char *filename,
574 struct pipe_transfer *transfer)
575 {
576 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
577 float *rgba;
578
579 if (!transfer)
580 goto error1;
581
582 rgba = MALLOC(transfer->box.width *
583 transfer->box.height *
584 transfer->box.depth *
585 4*sizeof(float));
586 if(!rgba)
587 goto error1;
588
589 pipe_get_tile_rgba(pipe, transfer, 0, 0,
590 transfer->box.width, transfer->box.height,
591 rgba);
592
593 debug_dump_float_rgba_bmp(filename,
594 transfer->box.width, transfer->box.height,
595 rgba, transfer->box.width);
596
597 FREE(rgba);
598 error1:
599 ;
600 #endif
601 }
602
603 void
604 debug_dump_float_rgba_bmp(const char *filename,
605 unsigned width, unsigned height,
606 float *rgba, unsigned stride)
607 {
608 #ifndef PIPE_SUBSYSTEM_WINDOWS_MINIPORT
609 struct os_stream *stream;
610 struct bmp_file_header bmfh;
611 struct bmp_info_header bmih;
612 unsigned x, y;
613
614 if(!rgba)
615 goto error1;
616
617 bmfh.bfType = 0x4d42;
618 bmfh.bfSize = 14 + 40 + height*width*4;
619 bmfh.bfReserved1 = 0;
620 bmfh.bfReserved2 = 0;
621 bmfh.bfOffBits = 14 + 40;
622
623 bmih.biSize = 40;
624 bmih.biWidth = width;
625 bmih.biHeight = height;
626 bmih.biPlanes = 1;
627 bmih.biBitCount = 32;
628 bmih.biCompression = 0;
629 bmih.biSizeImage = height*width*4;
630 bmih.biXPelsPerMeter = 0;
631 bmih.biYPelsPerMeter = 0;
632 bmih.biClrUsed = 0;
633 bmih.biClrImportant = 0;
634
635 stream = os_file_stream_create(filename);
636 if(!stream)
637 goto error1;
638
639 os_stream_write(stream, &bmfh, 14);
640 os_stream_write(stream, &bmih, 40);
641
642 y = height;
643 while(y--) {
644 float *ptr = rgba + (stride * y * 4);
645 for(x = 0; x < width; ++x)
646 {
647 struct bmp_rgb_quad pixel;
648 pixel.rgbRed = float_to_ubyte(ptr[x*4 + 0]);
649 pixel.rgbGreen = float_to_ubyte(ptr[x*4 + 1]);
650 pixel.rgbBlue = float_to_ubyte(ptr[x*4 + 2]);
651 pixel.rgbAlpha = 255;
652 os_stream_write(stream, &pixel, 4);
653 }
654 }
655
656 os_stream_close(stream);
657 error1:
658 ;
659 #endif
660 }
661
662 #endif