From 0d2a24b9a0b19a7a2344d10e16cee711c4ca5494 Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Sat, 30 May 2020 16:22:58 +0100 Subject: [PATCH] util: Add a index fifo --- doc/changes/auxiliary/mr.359.1.md | 2 + src/xrt/auxiliary/util/u_index_fifo.h | 76 +++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 doc/changes/auxiliary/mr.359.1.md create mode 100644 src/xrt/auxiliary/util/u_index_fifo.h diff --git a/doc/changes/auxiliary/mr.359.1.md b/doc/changes/auxiliary/mr.359.1.md new file mode 100644 index 000000000..f43a5b2fd --- /dev/null +++ b/doc/changes/auxiliary/mr.359.1.md @@ -0,0 +1,2 @@ +util: Add a very simple fifo for indices, this is used to keep track of +swapchain in order of age (oldness). diff --git a/src/xrt/auxiliary/util/u_index_fifo.h b/src/xrt/auxiliary/util/u_index_fifo.h new file mode 100644 index 000000000..037d31844 --- /dev/null +++ b/src/xrt/auxiliary/util/u_index_fifo.h @@ -0,0 +1,76 @@ +// Copyright 2020, Collabora, Ltd. +// SPDX-License-Identifier: BSL-1.0 +/*! + * @file + * @brief A FIFO for indices. + * @author Jakob Bornecrantz + * @ingroup aux_util + */ + +#pragma once + + +#define U_MAX_FIFO_INDICES 16 + +struct u_index_fifo +{ + uint32_t indices[U_MAX_FIFO_INDICES]; + size_t start; + size_t end; +}; + +static inline int +u_index_fifo_is_empty(struct u_index_fifo *uif) +{ + if (uif->start == uif->end) { + return 1; + } else { + return 0; + } +} + +static inline int +u_index_fifo_is_full(struct u_index_fifo *uif) +{ + if (((uif->end + 1) % U_MAX_FIFO_INDICES) == uif->start) { + return 1; + } else { + return 0; + } +} + +static inline int +u_index_fifo_peek(struct u_index_fifo *uif, uint32_t *out_index) +{ + if (u_index_fifo_is_empty(uif)) { + return -1; + } + + *out_index = uif->indices[uif->start]; + return 0; +} + +static inline int +u_index_fifo_pop(struct u_index_fifo *uif, uint32_t *out_index) +{ + if (u_index_fifo_is_empty(uif)) { + return -1; + } + + *out_index = uif->indices[uif->start]; + uif->start = (uif->start + 1) % U_MAX_FIFO_INDICES; + return 0; +} + +static inline int +u_index_fifo_push(struct u_index_fifo *uif, uint32_t index) +{ + if (u_index_fifo_is_full(uif)) { + return -1; + } + + uif->indices[uif->end] = index; + uif->end = (uif->end + 1) % U_MAX_FIFO_INDICES; + + return 0; +}