2007-11-23 17:22:54 +00:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sub license, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial portions
|
|
|
|
* of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
|
|
|
|
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Implementation of fenced buffers.
|
|
|
|
*
|
2008-04-01 10:41:43 +09:00
|
|
|
* \author José Fonseca <jrfonseca-at-tungstengraphics-dot-com>
|
|
|
|
* \author Thomas Hellström <thomas-at-tungstengraphics-dot-com>
|
2007-11-23 17:22:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2008-02-18 20:02:42 +09:00
|
|
|
#include "pipe/p_compiler.h"
|
|
|
|
#include "pipe/p_debug.h"
|
|
|
|
#include "pipe/p_winsys.h"
|
|
|
|
#include "pipe/p_thread.h"
|
|
|
|
#include "pipe/p_util.h"
|
2008-02-19 15:07:53 +09:00
|
|
|
#include "util/u_double_list.h"
|
2007-11-23 17:22:54 +00:00
|
|
|
|
|
|
|
#include "pb_buffer.h"
|
|
|
|
#include "pb_buffer_fenced.h"
|
|
|
|
|
2008-04-01 10:41:43 +09:00
|
|
|
#ifndef WIN32
|
2008-01-20 19:36:23 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2007-11-23 17:22:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience macro (type safe).
|
|
|
|
*/
|
|
|
|
#define SUPER(__derived) (&(__derived)->base)
|
|
|
|
|
|
|
|
|
|
|
|
struct fenced_buffer_list
|
|
|
|
{
|
|
|
|
_glthread_Mutex mutex;
|
|
|
|
|
|
|
|
struct pipe_winsys *winsys;
|
|
|
|
|
|
|
|
size_t numDelayed;
|
|
|
|
|
|
|
|
struct list_head delayed;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper around a pipe buffer which adds fencing and reference counting.
|
|
|
|
*/
|
|
|
|
struct fenced_buffer
|
|
|
|
{
|
2008-01-25 20:53:31 +00:00
|
|
|
struct pb_buffer base;
|
2007-11-23 17:22:54 +00:00
|
|
|
|
2008-01-25 20:53:31 +00:00
|
|
|
struct pb_buffer *buffer;
|
2007-11-23 17:22:54 +00:00
|
|
|
|
|
|
|
struct pipe_fence_handle *fence;
|
|
|
|
|
|
|
|
struct list_head head;
|
|
|
|
struct fenced_buffer_list *list;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-01-25 20:53:31 +00:00
|
|
|
static INLINE struct fenced_buffer *
|
|
|
|
fenced_buffer(struct pb_buffer *buf)
|
2007-11-23 17:22:54 +00:00
|
|
|
{
|
|
|
|
assert(buf);
|
|
|
|
assert(buf->vtbl == &fenced_buffer_vtbl);
|
|
|
|
return (struct fenced_buffer *)buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-05 11:29:26 +09:00
|
|
|
static INLINE void
|
|
|
|
_fenced_buffer_add(struct fenced_buffer *fenced_buf)
|
|
|
|
{
|
|
|
|
struct fenced_buffer_list *fenced_list = fenced_buf->list;
|
|
|
|
|
|
|
|
assert(fenced_buf->fence);
|
|
|
|
assert(!fenced_buf->head.prev);
|
|
|
|
assert(!fenced_buf->head.next);
|
|
|
|
LIST_ADDTAIL(&fenced_buf->head, &fenced_list->delayed);
|
|
|
|
++fenced_list->numDelayed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Actually destroy the buffer.
|
|
|
|
*/
|
|
|
|
static INLINE void
|
|
|
|
_fenced_buffer_destroy(struct fenced_buffer *fenced_buf)
|
|
|
|
{
|
|
|
|
struct fenced_buffer_list *fenced_list = fenced_buf->list;
|
|
|
|
|
|
|
|
assert(!fenced_buf->base.base.refcount);
|
|
|
|
assert(!fenced_buf->fence);
|
|
|
|
pb_reference(&fenced_buf->buffer, NULL);
|
|
|
|
FREE(fenced_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static INLINE void
|
|
|
|
_fenced_buffer_remove(struct fenced_buffer *fenced_buf)
|
|
|
|
{
|
|
|
|
struct fenced_buffer_list *fenced_list = fenced_buf->list;
|
|
|
|
struct pipe_winsys *winsys = fenced_list->winsys;
|
|
|
|
|
|
|
|
assert(fenced_buf->fence);
|
|
|
|
|
|
|
|
assert(winsys->fence_signalled(winsys, fenced_buf->fence, 0) == 0);
|
|
|
|
winsys->fence_reference(winsys, &fenced_buf->fence, NULL);
|
|
|
|
|
|
|
|
assert(fenced_buf->head.prev);
|
|
|
|
assert(fenced_buf->head.next);
|
|
|
|
LIST_DEL(&fenced_buf->head);
|
|
|
|
#ifdef DEBUG
|
|
|
|
fenced_buf->head.prev = NULL;
|
|
|
|
fenced_buf->head.next = NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
assert(fenced_list->numDelayed);
|
|
|
|
--fenced_list->numDelayed;
|
|
|
|
|
|
|
|
if(!fenced_buf->base.base.refcount)
|
|
|
|
_fenced_buffer_destroy(fenced_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free as many fenced buffers from the list head as possible.
|
|
|
|
*/
|
2007-11-23 17:22:54 +00:00
|
|
|
static void
|
|
|
|
_fenced_buffer_list_check_free(struct fenced_buffer_list *fenced_list,
|
|
|
|
int wait)
|
|
|
|
{
|
|
|
|
struct pipe_winsys *winsys = fenced_list->winsys;
|
2008-04-05 11:29:26 +09:00
|
|
|
struct list_head *curr, *next;
|
|
|
|
struct fenced_buffer *fenced_buf;
|
|
|
|
struct pipe_fence_handle *prev_fence = NULL;
|
|
|
|
|
|
|
|
curr = fenced_list->delayed.next;
|
|
|
|
next = curr->next;
|
|
|
|
while(curr != &fenced_list->delayed) {
|
|
|
|
fenced_buf = LIST_ENTRY(struct fenced_buffer, curr, head);
|
|
|
|
|
|
|
|
if(fenced_buf->fence != prev_fence) {
|
|
|
|
int signaled;
|
|
|
|
if (wait)
|
|
|
|
signaled = winsys->fence_finish(winsys, fenced_buf->fence, 0);
|
|
|
|
else
|
|
|
|
signaled = winsys->fence_signalled(winsys, fenced_buf->fence, 0);
|
|
|
|
if (signaled != 0)
|
|
|
|
break;
|
|
|
|
prev_fence = fenced_buf->fence;
|
2007-11-23 17:22:54 +00:00
|
|
|
}
|
|
|
|
|
2008-04-05 11:29:26 +09:00
|
|
|
_fenced_buffer_remove(fenced_buf);
|
2007-11-23 17:22:54 +00:00
|
|
|
|
2008-04-05 11:29:26 +09:00
|
|
|
curr = next;
|
|
|
|
next = curr->next;
|
2008-01-25 20:53:31 +00:00
|
|
|
}
|
2007-11-23 17:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-01-25 20:53:31 +00:00
|
|
|
fenced_buffer_destroy(struct pb_buffer *buf)
|
2007-11-23 17:22:54 +00:00
|
|
|
{
|
|
|
|
struct fenced_buffer *fenced_buf = fenced_buffer(buf);
|
|
|
|
struct fenced_buffer_list *fenced_list = fenced_buf->list;
|
|
|
|
|
2008-04-05 11:29:26 +09:00
|
|
|
_glthread_LOCK_MUTEX(fenced_list->mutex);
|
|
|
|
assert(fenced_buf->base.base.refcount == 0);
|
2008-01-25 20:53:31 +00:00
|
|
|
if (fenced_buf->fence) {
|
2008-04-01 10:41:43 +09:00
|
|
|
struct pipe_winsys *winsys = fenced_list->winsys;
|
2008-04-05 11:29:26 +09:00
|
|
|
if(winsys->fence_signalled(winsys, fenced_buf->fence, 0) == 0) {
|
|
|
|
struct list_head *curr, *prev;
|
|
|
|
curr = &fenced_buf->head;
|
|
|
|
prev = curr->prev;
|
|
|
|
do {
|
|
|
|
fenced_buf = LIST_ENTRY(struct fenced_buffer, curr, head);
|
|
|
|
_fenced_buffer_remove(fenced_buf);
|
|
|
|
curr = prev;
|
|
|
|
prev = curr->prev;
|
|
|
|
} while (curr != &fenced_list->delayed);
|
|
|
|
}
|
2008-04-01 10:41:43 +09:00
|
|
|
else {
|
2008-04-05 11:29:26 +09:00
|
|
|
/* delay destruction */
|
2008-04-01 10:41:43 +09:00
|
|
|
}
|
2008-01-25 20:53:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-04-05 11:29:26 +09:00
|
|
|
_fenced_buffer_destroy(fenced_buf);
|
2007-11-23 17:22:54 +00:00
|
|
|
}
|
2008-04-05 11:29:26 +09:00
|
|
|
_glthread_UNLOCK_MUTEX(fenced_list->mutex);
|
2007-11-23 17:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *
|
2008-01-25 20:53:31 +00:00
|
|
|
fenced_buffer_map(struct pb_buffer *buf,
|
2007-11-23 17:22:54 +00:00
|
|
|
unsigned flags)
|
|
|
|
{
|
|
|
|
struct fenced_buffer *fenced_buf = fenced_buffer(buf);
|
2008-01-25 20:53:31 +00:00
|
|
|
return pb_map(fenced_buf->buffer, flags);
|
2007-11-23 17:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-01-25 20:53:31 +00:00
|
|
|
fenced_buffer_unmap(struct pb_buffer *buf)
|
2007-11-23 17:22:54 +00:00
|
|
|
{
|
|
|
|
struct fenced_buffer *fenced_buf = fenced_buffer(buf);
|
2008-01-25 20:53:31 +00:00
|
|
|
pb_unmap(fenced_buf->buffer);
|
2007-11-23 17:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-01-25 20:53:31 +00:00
|
|
|
fenced_buffer_get_base_buffer(struct pb_buffer *buf,
|
|
|
|
struct pb_buffer **base_buf,
|
2007-11-23 17:22:54 +00:00
|
|
|
unsigned *offset)
|
|
|
|
{
|
|
|
|
struct fenced_buffer *fenced_buf = fenced_buffer(buf);
|
2008-01-25 20:53:31 +00:00
|
|
|
pb_get_base_buffer(fenced_buf->buffer, base_buf, offset);
|
2007-11-23 17:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-25 20:53:31 +00:00
|
|
|
const struct pb_vtbl
|
2007-11-23 17:22:54 +00:00
|
|
|
fenced_buffer_vtbl = {
|
2008-01-25 20:53:31 +00:00
|
|
|
fenced_buffer_destroy,
|
2007-11-23 17:22:54 +00:00
|
|
|
fenced_buffer_map,
|
|
|
|
fenced_buffer_unmap,
|
|
|
|
fenced_buffer_get_base_buffer
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-01-25 20:53:31 +00:00
|
|
|
struct pb_buffer *
|
2007-11-23 17:22:54 +00:00
|
|
|
fenced_buffer_create(struct fenced_buffer_list *fenced_list,
|
2008-01-25 20:53:31 +00:00
|
|
|
struct pb_buffer *buffer)
|
2007-11-23 17:22:54 +00:00
|
|
|
{
|
|
|
|
struct fenced_buffer *buf;
|
|
|
|
|
2007-12-18 18:59:49 +00:00
|
|
|
if(!buffer)
|
|
|
|
return NULL;
|
|
|
|
|
2008-01-25 20:53:31 +00:00
|
|
|
buf = CALLOC_STRUCT(fenced_buffer);
|
2007-11-23 17:22:54 +00:00
|
|
|
if(!buf)
|
|
|
|
return NULL;
|
|
|
|
|
2008-01-27 19:22:25 +09:00
|
|
|
buf->base.base.refcount = 1;
|
|
|
|
buf->base.base.alignment = buffer->base.alignment;
|
|
|
|
buf->base.base.usage = buffer->base.usage;
|
|
|
|
buf->base.base.size = buffer->base.size;
|
|
|
|
|
2007-11-23 17:22:54 +00:00
|
|
|
buf->base.vtbl = &fenced_buffer_vtbl;
|
|
|
|
buf->buffer = buffer;
|
|
|
|
buf->list = fenced_list;
|
|
|
|
|
|
|
|
return &buf->base;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2008-01-25 20:53:31 +00:00
|
|
|
buffer_fence(struct pb_buffer *buf,
|
2007-11-23 17:22:54 +00:00
|
|
|
struct pipe_fence_handle *fence)
|
|
|
|
{
|
2008-01-25 20:53:31 +00:00
|
|
|
struct fenced_buffer *fenced_buf = fenced_buffer(buf);
|
|
|
|
struct fenced_buffer_list *fenced_list = fenced_buf->list;
|
|
|
|
struct pipe_winsys *winsys = fenced_list->winsys;
|
2007-11-23 17:22:54 +00:00
|
|
|
|
2008-01-25 20:53:31 +00:00
|
|
|
_glthread_LOCK_MUTEX(fenced_list->mutex);
|
2008-04-05 11:29:26 +09:00
|
|
|
if (fenced_buf->fence)
|
|
|
|
_fenced_buffer_remove(fenced_buf);
|
2008-01-25 20:53:31 +00:00
|
|
|
winsys->fence_reference(winsys, &fenced_buf->fence, fence);
|
2008-04-05 11:29:26 +09:00
|
|
|
if (fenced_buf->fence)
|
|
|
|
_fenced_buffer_add(fenced_buf);
|
2008-01-25 20:53:31 +00:00
|
|
|
_glthread_UNLOCK_MUTEX(fenced_list->mutex);
|
2007-11-23 17:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct fenced_buffer_list *
|
|
|
|
fenced_buffer_list_create(struct pipe_winsys *winsys)
|
|
|
|
{
|
|
|
|
struct fenced_buffer_list *fenced_list;
|
|
|
|
|
2008-01-23 15:47:10 +01:00
|
|
|
fenced_list = (struct fenced_buffer_list *)CALLOC(1, sizeof(*fenced_list));
|
2007-11-23 17:22:54 +00:00
|
|
|
if (!fenced_list)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
fenced_list->winsys = winsys;
|
|
|
|
|
|
|
|
LIST_INITHEAD(&fenced_list->delayed);
|
|
|
|
|
|
|
|
fenced_list->numDelayed = 0;
|
|
|
|
|
|
|
|
_glthread_INIT_MUTEX(fenced_list->mutex);
|
|
|
|
|
|
|
|
return fenced_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
fenced_buffer_list_check_free(struct fenced_buffer_list *fenced_list,
|
|
|
|
int wait)
|
|
|
|
{
|
|
|
|
_glthread_LOCK_MUTEX(fenced_list->mutex);
|
|
|
|
_fenced_buffer_list_check_free(fenced_list, wait);
|
|
|
|
_glthread_UNLOCK_MUTEX(fenced_list->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
fenced_buffer_list_destroy(struct fenced_buffer_list *fenced_list)
|
|
|
|
{
|
|
|
|
_glthread_LOCK_MUTEX(fenced_list->mutex);
|
|
|
|
|
|
|
|
/* Wait on outstanding fences */
|
|
|
|
while (fenced_list->numDelayed) {
|
|
|
|
_glthread_UNLOCK_MUTEX(fenced_list->mutex);
|
2008-04-01 10:41:43 +09:00
|
|
|
#ifndef WIN32
|
2007-11-23 17:22:54 +00:00
|
|
|
sched_yield();
|
2008-02-24 02:16:28 +09:00
|
|
|
#endif
|
2008-01-20 19:36:23 +01:00
|
|
|
_fenced_buffer_list_check_free(fenced_list, 1);
|
2007-11-23 17:22:54 +00:00
|
|
|
_glthread_LOCK_MUTEX(fenced_list->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
_glthread_UNLOCK_MUTEX(fenced_list->mutex);
|
|
|
|
|
2008-01-23 15:47:10 +01:00
|
|
|
FREE(fenced_list);
|
2007-11-23 17:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|