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