gallium: extend pipe_context::flush for it to accept an END_OF_FRAME flag
[mesa.git] / src / gallium / state_trackers / vdpau / presentation.c
index 397bd372577591253d08f8928402d10683790cec..3dd7f05375e7368b6d85166890c4fe29a636b00e 100644 (file)
@@ -26,9 +26,6 @@
  **************************************************************************/
 
 #include <stdio.h>
-#include <time.h>
-#include <sys/timeb.h>
-
 #include <vdpau/vdpau.h>
 
 #include "util/u_debug.h"
@@ -68,10 +65,13 @@ vlVdpPresentationQueueCreate(VdpDevice device,
    pq->device = dev;
    pq->drawable = pqt->drawable;
 
+   pipe_mutex_lock(dev->mutex);
    if (!vl_compositor_init_state(&pq->cstate, dev->context)) {
+      pipe_mutex_unlock(dev->mutex);
       ret = VDP_STATUS_ERROR;
       goto no_compositor;
    }
+   pipe_mutex_unlock(dev->mutex);
 
    *presentation_queue = vlAddDataHTAB(pq);
    if (*presentation_queue == 0) {
@@ -99,7 +99,9 @@ vlVdpPresentationQueueDestroy(VdpPresentationQueue presentation_queue)
    if (!pq)
       return VDP_STATUS_INVALID_HANDLE;
 
+   pipe_mutex_lock(pq->device->mutex);
    vl_compositor_cleanup_state(&pq->cstate);
+   pipe_mutex_unlock(pq->device->mutex);
 
    vlRemoveDataHTAB(presentation_queue);
    FREE(pq);
@@ -129,7 +131,9 @@ vlVdpPresentationQueueSetBackgroundColor(VdpPresentationQueue presentation_queue
    color.f[2] = background_color->blue;
    color.f[3] = background_color->alpha;
 
+   pipe_mutex_lock(pq->device->mutex);
    vl_compositor_set_clear_color(&pq->cstate, &color);
+   pipe_mutex_unlock(pq->device->mutex);
 
    return VDP_STATUS_OK;
 }
@@ -151,7 +155,9 @@ vlVdpPresentationQueueGetBackgroundColor(VdpPresentationQueue presentation_queue
    if (!pq)
       return VDP_STATUS_INVALID_HANDLE;
 
+   pipe_mutex_lock(pq->device->mutex);
    vl_compositor_get_clear_color(&pq->cstate, &color);
+   pipe_mutex_unlock(pq->device->mutex);
 
    background_color->red = color.f[0];
    background_color->green = color.f[1];
@@ -169,7 +175,6 @@ vlVdpPresentationQueueGetTime(VdpPresentationQueue presentation_queue,
                               VdpTime *current_time)
 {
    vlVdpPresentationQueue *pq;
-   struct timespec ts;
 
    if (!current_time)
       return VDP_STATUS_INVALID_POINTER;
@@ -178,8 +183,9 @@ vlVdpPresentationQueueGetTime(VdpPresentationQueue presentation_queue,
    if (!pq)
       return VDP_STATUS_INVALID_HANDLE;
 
-   clock_gettime(CLOCK_REALTIME, &ts);
-   *current_time = (uint64_t)ts.tv_sec * 1000000000LL + (uint64_t)ts.tv_nsec;
+   pipe_mutex_lock(pq->device->mutex);
+   *current_time = vl_screen_get_timestamp(pq->device->vscreen, pq->drawable);
+   pipe_mutex_unlock(pq->device->mutex);
 
    return VDP_STATUS_OK;
 }
@@ -211,26 +217,27 @@ vlVdpPresentationQueueDisplay(VdpPresentationQueue presentation_queue,
    if (!pq)
       return VDP_STATUS_INVALID_HANDLE;
 
+   surf = vlGetDataHTAB(surface);
+   if (!surf)
+      return VDP_STATUS_INVALID_HANDLE;
 
    pipe = pq->device->context;
    compositor = &pq->device->compositor;
    cstate = &pq->cstate;
 
+   pipe_mutex_lock(pq->device->mutex);
    tex = vl_screen_texture_from_drawable(pq->device->vscreen, pq->drawable);
-   if (!tex)
+   if (!tex) {
+      pipe_mutex_unlock(pq->device->mutex);
       return VDP_STATUS_INVALID_HANDLE;
+   }
 
    dirty_area = vl_screen_get_dirty_area(pq->device->vscreen);
 
    memset(&surf_templ, 0, sizeof(surf_templ));
    surf_templ.format = tex->format;
-   surf_templ.usage = PIPE_BIND_RENDER_TARGET;
    surf_draw = pipe->create_surface(pipe, tex, &surf_templ);
 
-   surf = vlGetDataHTAB(surface);
-   if (!surf)
-      return VDP_STATUS_INVALID_HANDLE;
-
    surf->timestamp = (vlVdpTime)earliest_presentation_time;
 
    dst_clip.x0 = 0;
@@ -260,6 +267,7 @@ vlVdpPresentationQueueDisplay(VdpPresentationQueue presentation_queue,
       vl_compositor_render(cstate, compositor, surf_draw, dirty_area);
    }
 
+   vl_screen_set_next_timestamp(pq->device->vscreen, earliest_presentation_time);
    pipe->screen->flush_frontbuffer
    (
       pipe->screen, tex, 0, 0,
@@ -267,7 +275,7 @@ vlVdpPresentationQueueDisplay(VdpPresentationQueue presentation_queue,
    );
 
    pipe->screen->fence_reference(pipe->screen, &surf->fence, NULL);
-   pipe->flush(pipe, &surf->fence);
+   pipe->flush(pipe, &surf->fence, 0);
 
    if (dump_window == -1) {
       dump_window = debug_get_num_option("VDPAU_DUMP", 0);
@@ -277,13 +285,17 @@ vlVdpPresentationQueueDisplay(VdpPresentationQueue presentation_queue,
       static unsigned int framenum = 0;
       char cmd[256];
 
-      sprintf(cmd, "xwd -id %d -out vdpau_frame_%08d.xwd", (int)pq->drawable, ++framenum);
-      if (system(cmd) != 0)
-         VDPAU_MSG(VDPAU_ERR, "[VDPAU] Dumping surface %d failed.\n", surface);
+      if (framenum) {
+         sprintf(cmd, "xwd -id %d -silent -out vdpau_frame_%08d.xwd", (int)pq->drawable, framenum);
+         if (system(cmd) != 0)
+            VDPAU_MSG(VDPAU_ERR, "[VDPAU] Dumping surface %d failed.\n", surface);
+      }
+      framenum++;
    }
 
    pipe_resource_reference(&tex, NULL);
    pipe_surface_reference(&surf_draw, NULL);
+   pipe_mutex_unlock(pq->device->mutex);
 
    return VDP_STATUS_OK;
 }
@@ -311,15 +323,14 @@ vlVdpPresentationQueueBlockUntilSurfaceIdle(VdpPresentationQueue presentation_qu
    if (!surf)
       return VDP_STATUS_INVALID_HANDLE;
 
+   pipe_mutex_lock(pq->device->mutex);
    if (surf->fence) {
       screen = pq->device->vscreen->pscreen;
       screen->fence_finish(screen, surf->fence, 0);
    }
+   pipe_mutex_unlock(pq->device->mutex);
 
-   // We actually need to query the timestamp of the last VSYNC event from the hardware
-   vlVdpPresentationQueueGetTime(presentation_queue, first_presentation_time);
-
-   return VDP_STATUS_OK;
+   return vlVdpPresentationQueueGetTime(presentation_queue, first_presentation_time);
 }
 
 /**
@@ -351,16 +362,19 @@ vlVdpPresentationQueueQuerySurfaceStatus(VdpPresentationQueue presentation_queue
    if (!surf->fence) {
       *status = VDP_PRESENTATION_QUEUE_STATUS_IDLE;
    } else {
+      pipe_mutex_lock(pq->device->mutex);
       screen = pq->device->vscreen->pscreen;
       if (screen->fence_signalled(screen, surf->fence)) {
          screen->fence_reference(screen, &surf->fence, NULL);
          *status = VDP_PRESENTATION_QUEUE_STATUS_VISIBLE;
+         pipe_mutex_unlock(pq->device->mutex);
 
          // We actually need to query the timestamp of the last VSYNC event from the hardware
          vlVdpPresentationQueueGetTime(presentation_queue, first_presentation_time);
          *first_presentation_time += 1;
       } else {
          *status = VDP_PRESENTATION_QUEUE_STATUS_QUEUED;
+         pipe_mutex_unlock(pq->device->mutex);
       }
    }