Remove stale comment about glFlush().
[mesa.git] / src / mesa / drivers / dri / intel / intel_batchbuffer.c
index 4e698627a369f4a6f31e644160e5080572c1a21c..803ff5e90ee382c96e9d477ecd99c90cde2b81c0 100644 (file)
@@ -78,13 +78,26 @@ intel_batchbuffer_reset(struct intel_batchbuffer *batch)
       batch->buf = NULL;
    }
 
-   batch->buf = dri_bo_alloc(intel->intelScreen->bufmgr, "batchbuffer",
-                            intel->intelScreen->maxBatchSize, 4096,
+   if (!batch->buffer && intel->ttm == GL_TRUE)
+      batch->buffer = malloc (intel->maxBatchSize);
+
+   batch->buf = dri_bo_alloc(intel->bufmgr, "batchbuffer",
+                            intel->maxBatchSize, 4096,
                             DRM_BO_FLAG_MEM_LOCAL | DRM_BO_FLAG_CACHED | DRM_BO_FLAG_CACHED_MAPPED);
-   dri_bo_map(batch->buf, GL_TRUE);
-   batch->map = batch->buf->virtual;
-   batch->size = intel->intelScreen->maxBatchSize;
+   if (batch->buffer)
+      batch->map = batch->buffer;
+   else {
+      dri_bo_map(batch->buf, GL_TRUE);
+      batch->map = batch->buf->virtual;
+   }
+   batch->size = intel->maxBatchSize;
    batch->ptr = batch->map;
+   batch->dirty_state = ~0;
+   batch->cliprect_mode = IGNORE_CLIPRECTS;
+
+   /* account batchbuffer in aperture */
+   dri_bufmgr_check_aperture_space(batch->buf);
+
 }
 
 struct intel_batchbuffer *
@@ -93,7 +106,6 @@ intel_batchbuffer_alloc(struct intel_context *intel)
    struct intel_batchbuffer *batch = calloc(sizeof(*batch), 1);
 
    batch->intel = intel;
-   batch->last_fence = NULL;
    intel_batchbuffer_reset(batch);
 
    return batch;
@@ -102,14 +114,13 @@ intel_batchbuffer_alloc(struct intel_context *intel)
 void
 intel_batchbuffer_free(struct intel_batchbuffer *batch)
 {
-   if (batch->last_fence) {
-      dri_fence_wait(batch->last_fence);
-      dri_fence_unreference(batch->last_fence);
-      batch->last_fence = NULL;
-   }
-   if (batch->map) {
-      dri_bo_unmap(batch->buf);
-      batch->map = NULL;
+   if (batch->buffer)
+      free (batch->buffer);
+   else {
+      if (batch->map) {
+        dri_bo_unmap(batch->buf);
+        batch->map = NULL;
+      }
    }
    dri_bo_unreference(batch->buf);
    batch->buf = NULL;
@@ -122,39 +133,49 @@ intel_batchbuffer_free(struct intel_batchbuffer *batch)
  */
 static void
 do_flush_locked(struct intel_batchbuffer *batch,
-               GLuint used,
-               GLboolean ignore_cliprects, GLboolean allow_unlock)
+               GLuint used, GLboolean allow_unlock)
 {
    struct intel_context *intel = batch->intel;
-   void *start;
-   GLuint count;
+   int ret = 0;
 
-   start = dri_process_relocs(batch->buf, &count);
+   if (batch->buffer)
+      dri_bo_subdata (batch->buf, 0, used, batch->buffer);
+   else
+      dri_bo_unmap(batch->buf);
 
    batch->map = NULL;
    batch->ptr = NULL;
-   batch->flags = 0;
 
    /* Throw away non-effective packets.  Won't work once we have
     * hardware contexts which would preserve statechanges beyond a
     * single buffer.
     */
 
-   if (!(intel->numClipRects == 0 && !ignore_cliprects)) {
-      if (intel->intelScreen->ttm == GL_TRUE) {
-        intel_exec_ioctl(batch->intel,
-                         used, ignore_cliprects, allow_unlock,
-                         start, count, &batch->last_fence);
+   if (!(intel->numClipRects == 0 &&
+        batch->cliprect_mode == LOOP_CLIPRECTS)) {
+      if (intel->ttm == GL_TRUE) {
+        struct drm_i915_gem_execbuffer *execbuf;
+
+        execbuf = dri_process_relocs(batch->buf);
+        ret = intel_exec_ioctl(batch->intel,
+                               used,
+                               batch->cliprect_mode != LOOP_CLIPRECTS,
+                               allow_unlock,
+                               execbuf);
       } else {
-        intel_batch_ioctl(batch->intel,
-                          batch->buf->offset,
-                          used, ignore_cliprects, allow_unlock);
+        dri_process_relocs(batch->buf);
+        ret = intel_batch_ioctl(batch->intel,
+                                batch->buf->offset,
+                                used,
+                                batch->cliprect_mode != LOOP_CLIPRECTS,
+                                allow_unlock);
       }
    }
-      
-   dri_post_submit(batch->buf, &batch->last_fence);
 
-   if (intel->numClipRects == 0 && !ignore_cliprects) {
+   dri_post_submit(batch->buf);
+
+   if (intel->numClipRects == 0 &&
+       batch->cliprect_mode == LOOP_CLIPRECTS) {
       if (allow_unlock) {
         /* If we are not doing any actual user-visible rendering,
          * do a sched_yield to keep the app from pegging the cpu while
@@ -164,7 +185,6 @@ do_flush_locked(struct intel_batchbuffer *batch,
          sched_yield();
          LOCK_HARDWARE(intel);
       }
-      intel->vtbl.lost_hardware(intel);
    }
 
    if (INTEL_DEBUG & DEBUG_BATCH) {
@@ -172,11 +192,21 @@ do_flush_locked(struct intel_batchbuffer *batch,
       intel_decode(batch->buf->virtual, used / 4, batch->buf->offset,
                   intel->intelScreen->deviceID);
       dri_bo_unmap(batch->buf);
+
+      if (intel->vtbl.debug_batch != NULL)
+        intel->vtbl.debug_batch(intel);
    }
+
+   if (ret != 0) {
+      UNLOCK_HARDWARE(intel);
+      exit(1);
+   }
+   intel->vtbl.new_batch(intel);
 }
 
 void
-intel_batchbuffer_flush(struct intel_batchbuffer *batch)
+_intel_batchbuffer_flush(struct intel_batchbuffer *batch, const char *file,
+                        int line)
 {
    struct intel_context *intel = batch->intel;
    GLuint used = batch->ptr - batch->map;
@@ -185,37 +215,60 @@ intel_batchbuffer_flush(struct intel_batchbuffer *batch)
    if (used == 0)
       return;
 
-   /* Add the MI_BATCH_BUFFER_END.  Always add an MI_FLUSH - this is a
-    * performance drain that we would like to avoid.
-    */
-   if (used & 4) {
-      ((int *) batch->ptr)[0] = intel->vtbl.flush_cmd();
-      ((int *) batch->ptr)[1] = 0;
-      ((int *) batch->ptr)[2] = MI_BATCH_BUFFER_END;
-      used += 12;
+   if (INTEL_DEBUG & DEBUG_BATCH)
+      fprintf(stderr, "%s:%d: Batchbuffer flush with %db used\n", file, line,
+             used);
+
+   /* Emit a flush if the bufmgr doesn't do it for us. */
+   if (!intel->ttm) {
+      *(GLuint *) (batch->ptr) = intel->vtbl.flush_cmd();
+      batch->ptr += 4;
+      used = batch->ptr - batch->map;
    }
-   else {
-      ((int *) batch->ptr)[0] = intel->vtbl.flush_cmd();
-      ((int *) batch->ptr)[1] = MI_BATCH_BUFFER_END;
-      used += 8;
+
+   /* Round batchbuffer usage to 2 DWORDs. */
+
+   if ((used & 4) == 0) {
+      *(GLuint *) (batch->ptr) = 0; /* noop */
+      batch->ptr += 4;
+      used = batch->ptr - batch->map;
    }
 
+   /* Mark the end of the buffer. */
+   *(GLuint *) (batch->ptr) = MI_BATCH_BUFFER_END; /* noop */
+   batch->ptr += 4;
+   used = batch->ptr - batch->map;
+
+   /* Workaround for recursive batchbuffer flushing: If the window is
+    * moved, we can get into a case where we try to flush during a
+    * flush.  What happens is that when we try to grab the lock for
+    * the first flush, we detect that the window moved which then
+    * causes another flush (from the intel_draw_buffer() call in
+    * intelUpdatePageFlipping()).  To work around this we reset the
+    * batchbuffer tail pointer before trying to get the lock.  This
+    * prevent the nested buffer flush, but a better fix would be to
+    * avoid that in the first place. */
+   batch->ptr = batch->map;
+
    /* TODO: Just pass the relocation list and dma buffer up to the
     * kernel.
     */
    if (!was_locked)
       LOCK_HARDWARE(intel);
 
-   do_flush_locked(batch, used, !(batch->flags & INTEL_BATCH_CLIPRECTS),
-                  GL_FALSE);
-     
+   do_flush_locked(batch, used, GL_FALSE);
+
    if (!was_locked)
       UNLOCK_HARDWARE(intel);
 
    if (INTEL_DEBUG & DEBUG_SYNC) {
+      int irq;
+
       fprintf(stderr, "waiting for idle\n");
-      if (batch->last_fence != NULL)
-        dri_fence_wait(batch->last_fence);
+      LOCK_HARDWARE(intel);
+      irq = intelEmitIrqLocked(intel);
+      UNLOCK_HARDWARE(intel);
+      intelWaitIrq(intel, irq);
    }
 
    /* Reset the buffer:
@@ -223,34 +276,40 @@ intel_batchbuffer_flush(struct intel_batchbuffer *batch)
    intel_batchbuffer_reset(batch);
 }
 
-void
-intel_batchbuffer_finish(struct intel_batchbuffer *batch)
-{
-   intel_batchbuffer_flush(batch);
-   if (batch->last_fence != NULL)
-      dri_fence_wait(batch->last_fence);
-}
-
 
 /*  This is the only way buffers get added to the validate list.
  */
 GLboolean
 intel_batchbuffer_emit_reloc(struct intel_batchbuffer *batch,
                              dri_bo *buffer,
-                             GLuint flags, GLuint delta)
+                             uint32_t read_domains, uint32_t write_domain,
+                            uint32_t delta)
 {
-   dri_emit_reloc(batch->buf, flags, delta, batch->ptr - batch->map, buffer);
-   batch->ptr += 4;
+   int ret;
+
+   if (batch->ptr - batch->map > batch->buf->size)
+    _mesa_printf ("bad relocation ptr %p map %p offset %d size %d\n",
+                 batch->ptr, batch->map, batch->ptr - batch->map, batch->buf->size);
+   ret = dri_emit_reloc(batch->buf, read_domains, write_domain,
+                       delta, batch->ptr - batch->map, buffer);
+
+   /*
+    * Using the old buffer offset, write in what the right data would be, in case
+    * the buffer doesn't move and we can short-circuit the relocation processing
+    * in the kernel
+    */
+   intel_batchbuffer_emit_dword (batch, buffer->offset + delta);
 
    return GL_TRUE;
 }
 
 void
 intel_batchbuffer_data(struct intel_batchbuffer *batch,
-                       const void *data, GLuint bytes, GLuint flags)
+                       const void *data, GLuint bytes,
+                      enum cliprect_mode cliprect_mode)
 {
    assert((bytes & 3) == 0);
-   intel_batchbuffer_require_space(batch, bytes, flags);
+   intel_batchbuffer_require_space(batch, bytes, cliprect_mode);
    __memcpy(batch->ptr, data, bytes);
    batch->ptr += bytes;
 }