i965: improve debug logging
[mesa.git] / src / mesa / drivers / dri / intel / intel_regions.c
index 56ca6d2fd274079cc5268981f8d4430b69745135..534e75efe1f78600a6c90a49e305eb86e01ba60b 100644 (file)
  * last moment.
  */
 
+#include <sys/ioctl.h>
+#include <errno.h>
+
 #include "intel_context.h"
 #include "intel_regions.h"
 #include "intel_blit.h"
 #include "intel_buffer_objects.h"
-#include "dri_bufmgr.h"
-#include "intel_bufmgr_ttm.h"
+#include "intel_bufmgr.h"
 #include "intel_batchbuffer.h"
+#include "intel_chipset.h"
 
 #define FILE_DEBUG_FLAG DEBUG_REGION
 
+/* This should be set to the maximum backtrace size desired.
+ * Set it to 0 to disable backtrace debugging.
+ */
+#define DEBUG_BACKTRACE_SIZE 0
+
+#if DEBUG_BACKTRACE_SIZE == 0
+/* Use the standard debug output */
+#define _DBG(...) DBG(__VA_ARGS__)
+#else
+/* Use backtracing debug output */
+#define _DBG(...) {debug_backtrace(); DBG(__VA_ARGS__);}
+
+/* Backtracing debug support */
+#include <execinfo.h>
+
+static void
+debug_backtrace(void)
+{
+   void *trace[DEBUG_BACKTRACE_SIZE];
+   char **strings = NULL;
+   int traceSize;
+   register int i;
+
+   traceSize = backtrace(trace, DEBUG_BACKTRACE_SIZE);
+   strings = backtrace_symbols(trace, traceSize);
+   if (strings == NULL) {
+      DBG("no backtrace:");
+      return;
+   }
+
+   /* Spit out all the strings with a colon separator.  Ignore
+    * the first, since we don't really care about the call
+    * to debug_backtrace() itself.  Skip until the final "/" in
+    * the trace to avoid really long lines.
+    */
+   for (i = 1; i < traceSize; i++) {
+      char *p = strings[i], *slash = strings[i];
+      while (*p) {
+         if (*p++ == '/') {
+            slash = p;
+         }
+      }
+
+      DBG("%s:", slash);
+   }
+
+   /* Free up the memory, and we're done */
+   free(strings);
+}
+
+#endif
+
+
+
 /* XXX: Thread safety?
  */
 GLubyte *
 intel_region_map(struct intel_context *intel, struct intel_region *region)
 {
-   DBG("%s\n", __FUNCTION__);
+   _DBG("%s %p\n", __FUNCTION__, region);
    if (!region->map_refcount++) {
       if (region->pbo)
          intel_region_cow(intel, region);
@@ -69,34 +126,95 @@ intel_region_map(struct intel_context *intel, struct intel_region *region)
 void
 intel_region_unmap(struct intel_context *intel, struct intel_region *region)
 {
-   DBG("%s\n", __FUNCTION__);
+   _DBG("%s %p\n", __FUNCTION__, region);
    if (!--region->map_refcount) {
       dri_bo_unmap(region->buffer);
       region->map = NULL;
    }
 }
 
-struct intel_region *
-intel_region_alloc(struct intel_context *intel,
-                   GLuint cpp, GLuint pitch, GLuint height)
+static struct intel_region *
+intel_region_alloc_internal(struct intel_context *intel,
+                           GLuint cpp,
+                           GLuint width, GLuint height, GLuint pitch,
+                           dri_bo *buffer)
 {
-   struct intel_region *region = calloc(sizeof(*region), 1);
+   struct intel_region *region;
 
-   DBG("%s\n", __FUNCTION__);
+   if (buffer == NULL) {
+      _DBG("%s <-- NULL\n", __FUNCTION__);
+      return NULL;
+   }
 
+   region = calloc(sizeof(*region), 1);
    region->cpp = cpp;
+   region->width = width;
+   region->height = height;
    region->pitch = pitch;
-   region->height = height;     /* needed? */
    region->refcount = 1;
+   region->buffer = buffer;
+
+   /* Default to no tiling */
+   region->tiling = I915_TILING_NONE;
+   region->bit_6_swizzle = I915_BIT_6_SWIZZLE_NONE;
+
+   _DBG("%s <-- %p\n", __FUNCTION__, region);
+   return region;
+}
+
+struct intel_region *
+intel_region_alloc(struct intel_context *intel,
+                   GLuint cpp, GLuint width, GLuint height, GLuint pitch,
+                  GLboolean expect_accelerated_upload)
+{
+   dri_bo *buffer;
+
+   if (expect_accelerated_upload) {
+      buffer = drm_intel_bo_alloc_for_render(intel->bufmgr, "region",
+                                            pitch * cpp * height, 64);
+   } else {
+      buffer = drm_intel_bo_alloc(intel->bufmgr, "region",
+                                 pitch * cpp * height, 64);
+   }
+
+   return intel_region_alloc_internal(intel, cpp, width, height, pitch, buffer);
+}
+
+struct intel_region *
+intel_region_alloc_for_handle(struct intel_context *intel,
+                             GLuint cpp,
+                             GLuint width, GLuint height, GLuint pitch,
+                             GLuint handle, const char *name)
+{
+   struct intel_region *region;
+   dri_bo *buffer;
+   int ret;
+
+   buffer = intel_bo_gem_create_from_name(intel->bufmgr, name, handle);
+
+   region = intel_region_alloc_internal(intel, cpp,
+                                       width, height, pitch, buffer);
+   if (region == NULL)
+      return region;
+
+   ret = dri_bo_get_tiling(region->buffer, &region->tiling,
+                          &region->bit_6_swizzle);
+   if (ret != 0) {
+      fprintf(stderr, "Couldn't get tiling of buffer %d (%s): %s\n",
+             handle, name, strerror(-ret));
+      intel_region_release(&region);
+      return NULL;
+   }
 
-   region->buffer = dri_bo_alloc(intel->bufmgr, "region",
-                                pitch * cpp * height, 64, DRM_BO_FLAG_MEM_TT);
    return region;
 }
 
 void
 intel_region_reference(struct intel_region **dst, struct intel_region *src)
 {
+   if (src)
+      _DBG("%s %p %d\n", __FUNCTION__, src, src->refcount);
+
    assert(*dst == NULL);
    if (src) {
       src->refcount++;
@@ -105,32 +223,42 @@ intel_region_reference(struct intel_region **dst, struct intel_region *src)
 }
 
 void
-intel_region_release(struct intel_region **region)
+intel_region_release(struct intel_region **region_handle)
 {
-   if (!*region)
+   struct intel_region *region = *region_handle;
+
+   if (region == NULL) {
+      _DBG("%s NULL\n", __FUNCTION__);
       return;
+   }
 
-   DBG("%s %d\n", __FUNCTION__, (*region)->refcount - 1);
+   _DBG("%s %p %d\n", __FUNCTION__, region, region->refcount - 1);
 
-   ASSERT((*region)->refcount > 0);
-   (*region)->refcount--;
+   ASSERT(region->refcount > 0);
+   region->refcount--;
 
-   if ((*region)->refcount == 0) {
-      assert((*region)->map_refcount == 0);
+   if (region->refcount == 0) {
+      assert(region->map_refcount == 0);
 
-      if ((*region)->pbo)
-        (*region)->pbo->region = NULL;
-      (*region)->pbo = NULL;
-      dri_bo_unreference((*region)->buffer);
-      free(*region);
+      if (region->pbo)
+        region->pbo->region = NULL;
+      region->pbo = NULL;
+      dri_bo_unreference(region->buffer);
+
+      if (region->classic_map != NULL) {
+        drmUnmap(region->classic_map,
+                       region->pitch * region->cpp * region->height);
+      }
+
+      free(region);
    }
-   *region = NULL;
+   *region_handle = NULL;
 }
 
 /*
  * XXX Move this into core Mesa?
  */
-static void
+void
 _mesa_copy_rect(GLubyte * dst,
                 GLuint cpp,
                 GLuint dst_pitch,
@@ -178,7 +306,9 @@ intel_region_data(struct intel_context *intel,
                   const void *src, GLuint src_pitch,
                   GLuint srcx, GLuint srcy, GLuint width, GLuint height)
 {
-   DBG("%s\n", __FUNCTION__);
+   GLboolean locked = GL_FALSE;
+
+   _DBG("%s\n", __FUNCTION__);
 
    if (intel == NULL)
       return;
@@ -191,8 +321,10 @@ intel_region_data(struct intel_context *intel,
          intel_region_cow(intel, dst);
    }
 
-
-   LOCK_HARDWARE(intel);
+   if (!intel->locked) {
+      LOCK_HARDWARE(intel);
+      locked = GL_TRUE;
+   }
 
    _mesa_copy_rect(intel_region_map(intel, dst) + dst_offset,
                    dst->cpp,
@@ -201,7 +333,8 @@ intel_region_data(struct intel_context *intel,
 
    intel_region_unmap(intel, dst);
 
-   UNLOCK_HARDWARE(intel);
+   if (locked)
+      UNLOCK_HARDWARE(intel);
 
 }
 
@@ -217,7 +350,7 @@ intel_region_copy(struct intel_context *intel,
                   GLuint src_offset,
                   GLuint srcx, GLuint srcy, GLuint width, GLuint height)
 {
-   DBG("%s\n", __FUNCTION__);
+   _DBG("%s\n", __FUNCTION__);
 
    if (intel == NULL)
       return;
@@ -234,8 +367,8 @@ intel_region_copy(struct intel_context *intel,
 
    intelEmitCopyBlit(intel,
                      dst->cpp,
-                     src->pitch, src->buffer, src_offset, src->tiled,
-                     dst->pitch, dst->buffer, dst_offset, dst->tiled,
+                     src->pitch, src->buffer, src_offset, src->tiling,
+                     dst->pitch, dst->buffer, dst_offset, dst->tiling,
                      srcx, srcy, dstx, dsty, width, height,
                     GL_COPY);
 }
@@ -250,7 +383,7 @@ intel_region_fill(struct intel_context *intel,
                   GLuint dstx, GLuint dsty,
                   GLuint width, GLuint height, GLuint color)
 {
-   DBG("%s\n", __FUNCTION__);
+   _DBG("%s\n", __FUNCTION__);
 
    if (intel == NULL)
       return;   
@@ -265,7 +398,7 @@ intel_region_fill(struct intel_context *intel,
 
    intelEmitFillBlit(intel,
                      dst->cpp,
-                     dst->pitch, dst->buffer, dst_offset, dst->tiled,
+                     dst->pitch, dst->buffer, dst_offset, dst->tiling,
                      dstx, dsty, width, height, color);
 }
 
@@ -280,6 +413,8 @@ intel_region_attach_pbo(struct intel_context *intel,
    if (region->pbo == pbo)
       return;
 
+   _DBG("%s %p %p\n", __FUNCTION__, region, pbo);
+
    /* If there is already a pbo attached, break the cow tie now.
     * Don't call intel_region_release_pbo() as that would
     * unnecessarily allocate a new buffer we would have to immediately
@@ -309,6 +444,7 @@ void
 intel_region_release_pbo(struct intel_context *intel,
                          struct intel_region *region)
 {
+   _DBG("%s %p\n", __FUNCTION__, region);
    assert(region->buffer == region->pbo->buffer);
    region->pbo->region = NULL;
    region->pbo = NULL;
@@ -317,7 +453,7 @@ intel_region_release_pbo(struct intel_context *intel,
 
    region->buffer = dri_bo_alloc(intel->bufmgr, "region",
                                 region->pitch * region->cpp * region->height,
-                                64, DRM_BO_FLAG_MEM_TT);
+                                64);
 }
 
 /* Break the COW tie to the pbo.  Both the pbo and the region end up
@@ -336,28 +472,24 @@ intel_region_cow(struct intel_context *intel, struct intel_region *region)
 
    assert(region->cpp * region->pitch * region->height == pbo->Base.Size);
 
-   DBG("%s (%d bytes)\n", __FUNCTION__, pbo->Base.Size);
+   _DBG("%s %p (%d bytes)\n", __FUNCTION__, region, pbo->Base.Size);
 
    /* Now blit from the texture buffer to the new buffer: 
     */
 
-   intel_batchbuffer_flush(intel->batch);
-
    was_locked = intel->locked;
-   if (intel->locked)
+   if (!was_locked)
       LOCK_HARDWARE(intel);
 
    intelEmitCopyBlit(intel,
                     region->cpp,
-                    region->pitch, region->buffer, 0, region->tiled,
-                    region->pitch, pbo->buffer, 0, region->tiled,
+                    region->pitch, region->buffer, 0, region->tiling,
+                    region->pitch, pbo->buffer, 0, region->tiling,
                     0, 0, 0, 0,
                     region->pitch, region->height,
                     GL_COPY);
 
-   intel_batchbuffer_flush(intel->batch);
-
-   if (was_locked)
+   if (!was_locked)
       UNLOCK_HARDWARE(intel);
 }
 
@@ -379,34 +511,83 @@ static struct intel_region *
 intel_recreate_static(struct intel_context *intel,
                      const char *name,
                      struct intel_region *region,
-                     intelRegion *region_desc,
-                     GLuint mem_type)
+                     intelRegion *region_desc)
 {
    intelScreenPrivate *intelScreen = intel->intelScreen;
+   int ret;
 
    if (region == NULL) {
       region = calloc(sizeof(*region), 1);
       region->refcount = 1;
+      _DBG("%s creating new region %p\n", __FUNCTION__, region);
+   }
+   else {
+      _DBG("%s %p\n", __FUNCTION__, region);
    }
 
-   region->cpp = intelScreen->cpp;
-   region->pitch = region_desc->pitch / intelScreen->cpp;
-   region->height = intelScreen->height;     /* needed? */
-   region->tiled = region_desc->tiled;
+   if (intel->ctx.Visual.rgbBits == 24)
+      region->cpp = 4;
+   else
+      region->cpp = intel->ctx.Visual.rgbBits / 8;
+   region->pitch = intelScreen->pitch;
+   region->width = intelScreen->width;
+   region->height = intelScreen->height;
+
+   if (region->buffer != NULL) {
+      dri_bo_unreference(region->buffer);
+      region->buffer = NULL;
+   }
 
    if (intel->ttm) {
       assert(region_desc->bo_handle != -1);
-      region->buffer = intel_ttm_bo_create_from_handle(intel->bufmgr,
-                                                      name,
-                                                      region_desc->bo_handle);
+      region->buffer = intel_bo_gem_create_from_name(intel->bufmgr,
+                                                    name,
+                                                    region_desc->bo_handle);
+
+      ret = dri_bo_get_tiling(region->buffer, &region->tiling,
+                             &region->bit_6_swizzle);
+      if (ret != 0) {
+        fprintf(stderr, "Couldn't get tiling of buffer %d (%s): %s\n",
+                region_desc->bo_handle, name, strerror(-ret));
+        intel_region_release(&region);
+        return NULL;
+      }
    } else {
-      region->buffer = dri_bo_alloc_static(intel->bufmgr,
-                                          name,
-                                          region_desc->offset,
-                                          region_desc->pitch *
-                                          intelScreen->height,
-                                          region_desc->map,
-                                          DRM_BO_FLAG_MEM_TT);
+      if (region->classic_map != NULL) {
+        drmUnmap(region->classic_map,
+                 region->pitch * region->cpp * region->height);
+        region->classic_map = NULL;
+      }
+      ret = drmMap(intel->driFd, region_desc->handle,
+                  region->pitch * region->cpp * region->height,
+                  &region->classic_map);
+      if (ret != 0) {
+        fprintf(stderr, "Failed to drmMap %s buffer\n", name);
+        free(region);
+        return NULL;
+      }
+
+      region->buffer = intel_bo_fake_alloc_static(intel->bufmgr,
+                                                 name,
+                                                 region_desc->offset,
+                                                 region->pitch * region->cpp *
+                                                 region->height,
+                                                 region->classic_map);
+
+      /* The sarea just gives us a boolean for whether it's tiled or not,
+       * instead of which tiling mode it is.  Guess.
+       */
+      if (region_desc->tiled) {
+        if (IS_965(intel->intelScreen->deviceID) &&
+            region_desc == &intelScreen->depth)
+           region->tiling = I915_TILING_Y;
+        else
+           region->tiling = I915_TILING_X;
+      } else {
+        region->tiling = I915_TILING_NONE;
+      }
+
+      region->bit_6_swizzle = I915_BIT_6_SWIZZLE_NONE;
    }
 
    assert(region->buffer != NULL);
@@ -433,22 +614,12 @@ intel_recreate_static_regions(struct intel_context *intel)
    intel->front_region =
       intel_recreate_static(intel, "front",
                            intel->front_region,
-                           &intelScreen->front,
-                           DRM_BO_FLAG_MEM_TT);
+                           &intelScreen->front);
 
    intel->back_region =
       intel_recreate_static(intel, "back",
                            intel->back_region,
-                           &intelScreen->back,
-                           DRM_BO_FLAG_MEM_TT);
-
-   if (intelScreen->third.handle) {
-      intel->third_region =
-        intel_recreate_static(intel, "third",
-                              intel->third_region,
-                              &intelScreen->third,
-                              DRM_BO_FLAG_MEM_TT);
-   }
+                           &intelScreen->back);
 
    /* Still assumes front.cpp == depth.cpp.  We can kill this when we move to
     * private buffers.
@@ -456,6 +627,5 @@ intel_recreate_static_regions(struct intel_context *intel)
    intel->depth_region =
       intel_recreate_static(intel, "depth",
                            intel->depth_region,
-                           &intelScreen->depth,
-                           DRM_BO_FLAG_MEM_TT);
+                           &intelScreen->depth);
 }