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