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