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