graph: add new scheduler

Move the activation state to the graph
Replace an old scheduler with a new one that works with the new
activation states.
Remove the DISABLED port flags, we explicitly add and remove to
make ports enabled/disabled.
Find first compatible port for link
Remove the node based scheduler and use the spa one.
This commit is contained in:
Wim Taymans 2018-03-14 11:52:13 +01:00
parent c547baf952
commit 6eea9247ea
15 changed files with 293 additions and 328 deletions

View file

@ -26,136 +26,126 @@ extern "C" {
#include <spa/graph/graph.h>
#define SPA_GRAPH_STATE_IN 0
#define SPA_GRAPH_STATE_OUT 1
#define SPA_GRAPH_STATE_CHECK_IN 2
#define SPA_GRAPH_STATE_CHECK_OUT 3
struct spa_graph_data {
struct spa_graph *graph;
struct spa_list ready;
struct spa_graph_node *node;
};
static inline void spa_graph_data_init(struct spa_graph_data *data,
struct spa_graph *graph)
{
data->graph = graph;
spa_list_init(&data->ready);
data->node = NULL;
}
static inline void spa_graph_data_port_check(struct spa_graph_data *data, struct spa_graph_port *port)
static inline int spa_graph_impl_process(void *data, struct spa_graph_node *node)
{
struct spa_graph_node *node = port->node;
uint32_t required = node->required[SPA_DIRECTION_INPUT];
struct spa_graph_data *d = (struct spa_graph_data *) data;
struct spa_graph *g = d->graph;
int old = node->state->status, res = 0;
uint32_t val;
if (port->io->status == SPA_STATUS_HAVE_BUFFER)
node->ready[SPA_DIRECTION_INPUT]++;
spa_debug("node %p: pending %d required %d %d", node,
node->state->pending, node->state->required, old);
spa_debug("port %p node %p check %d %d %d", port, node,
port->io->status, node->ready[SPA_DIRECTION_INPUT], required);
if (required > 0 && node->ready[SPA_DIRECTION_INPUT] == required) {
node->state = SPA_GRAPH_STATE_IN;
if (node->ready_link.next == NULL)
spa_list_append(&data->ready, &node->ready_link);
} else if (node->ready_link.next) {
spa_list_remove(&node->ready_link);
node->ready_link.next = NULL;
if (node->state->pending == 0) {
spa_debug("node %p: nothing pending", node);
return node->state->status;
}
}
static inline bool spa_graph_data_iterate(struct spa_graph_data *data)
{
bool res;
int state;
struct spa_graph_port *p;
struct spa_graph_node *n;
res = !spa_list_is_empty(&data->ready);
if (res) {
n = spa_list_first(&data->ready, struct spa_graph_node, ready_link);
spa_list_remove(&n->ready_link);
n->ready_link.next = NULL;
spa_debug("node %p state %d", n, n->state);
switch (n->state) {
case SPA_GRAPH_STATE_IN:
state = spa_node_process_input(n->implementation);
if (state == SPA_STATUS_NEED_BUFFER)
n->state = SPA_GRAPH_STATE_CHECK_IN;
else if (state == SPA_STATUS_HAVE_BUFFER)
n->state = SPA_GRAPH_STATE_CHECK_OUT;
spa_debug("node %p processed input state %d", n, n->state);
if (n == data->node)
break;
spa_list_append(&data->ready, &n->ready_link);
break;
case SPA_GRAPH_STATE_OUT:
state = spa_node_process_output(n->implementation);
if (state == SPA_STATUS_NEED_BUFFER)
n->state = SPA_GRAPH_STATE_CHECK_IN;
else if (state == SPA_STATUS_HAVE_BUFFER)
n->state = SPA_GRAPH_STATE_CHECK_OUT;
spa_debug("node %p processed output state %d", n, n->state);
spa_list_append(&data->ready, &n->ready_link);
break;
case SPA_GRAPH_STATE_CHECK_IN:
n->ready[SPA_DIRECTION_INPUT] = 0;
spa_list_for_each(p, &n->ports[SPA_DIRECTION_INPUT], link) {
struct spa_graph_node *pn = p->peer->node;
if (p->io->status == SPA_STATUS_NEED_BUFFER) {
if (pn != data->node
|| pn->flags & SPA_GRAPH_NODE_FLAG_ASYNC) {
pn->state = SPA_GRAPH_STATE_OUT;
spa_list_append(&data->ready,
&pn->ready_link);
}
} else if (p->io->status == SPA_STATUS_OK)
n->ready[SPA_DIRECTION_INPUT]++;
}
case SPA_GRAPH_STATE_CHECK_OUT:
spa_list_for_each(p, &n->ports[SPA_DIRECTION_OUTPUT], link)
spa_graph_data_port_check(data, p->peer);
break;
default:
break;
val = __atomic_sub_fetch(&node->state->pending, 1, __ATOMIC_SEQ_CST);
if (val == 0) {
if (old == SPA_STATUS_NEED_BUFFER &&
node->implementation->process_input) {
res = spa_node_process_input(node->implementation);
}
res = !spa_list_is_empty(&data->ready);
}
return res;
else {
res = spa_node_process_output(node->implementation);
}
spa_debug("node %p: process %d", node, res);
if (res == SPA_STATUS_HAVE_BUFFER)
spa_graph_have_output(g, node);
node->state->status = res;
spa_debug("node %p: end %d", node, res);
}
return node->state->status;
}
static inline int spa_graph_impl_need_input(void *data, struct spa_graph_node *node)
{
struct spa_graph_data *d = (struct spa_graph_data *) data;
struct spa_list queue, pending;
struct spa_graph_node *n, *pn;
struct spa_graph_port *p, *pp;
spa_debug("node %p start pull", node);
node->state = SPA_GRAPH_STATE_CHECK_IN;
d->node = node;
if (node->ready_link.next == NULL)
spa_list_append(&d->ready, &node->ready_link);
while(spa_graph_data_iterate(d));
spa_list_init(&queue);
spa_list_init(&pending);
node->state->status = SPA_STATUS_NEED_BUFFER;
if (node->sched_link.next == NULL)
spa_list_append(&queue, &node->sched_link);
while (!spa_list_is_empty(&queue)) {
n = spa_list_first(&queue, struct spa_graph_node, sched_link);
spa_list_remove(&n->sched_link);
n->sched_link.next = NULL;
n->state->pending = n->state->required + 1;
spa_debug("node %p: add %d %d status %d", n,
n->state->pending, n->state->required,
n->state->status);
spa_list_prepend(&pending, &n->sched_link);
if (n->state->status == SPA_STATUS_HAVE_BUFFER)
continue;
spa_list_for_each(p, &n->ports[SPA_DIRECTION_INPUT], link) {
pp = p->peer;
if (pp == NULL)
continue;
pn = pp->node;
spa_debug("node %p: %p in io:%d state:%d %p", n, pn, pp->io->status,
pn->state->status, pn->sched_link.next);
if (pn->sched_link.next != NULL)
continue;
if (pp->io->status == SPA_STATUS_NEED_BUFFER) {
pn->state->status = spa_node_process_output(pn->implementation);
} else {
n->state->pending--;
}
spa_list_append(&queue, &pn->sched_link);
}
}
while (!spa_list_is_empty(&pending)) {
n = spa_list_first(&pending, struct spa_graph_node, sched_link);
spa_list_remove(&n->sched_link);
n->sched_link.next = NULL;
spa_debug("schedule node %p: %d", n, n->state->status);
spa_graph_process(d->graph, n);
}
return 0;
}
static inline int spa_graph_impl_have_output(void *data, struct spa_graph_node *node)
{
struct spa_graph_data *d = (struct spa_graph_data *) data;
spa_debug("node %p start push", node);
node->state = SPA_GRAPH_STATE_OUT;
d->node = node;
if (node->ready_link.next == NULL)
spa_list_append(&d->ready, &node->ready_link);
struct spa_graph_port *p;
while(spa_graph_data_iterate(d));
spa_debug("node %p start push", node);
spa_list_for_each(p, &node->ports[SPA_DIRECTION_OUTPUT], link)
spa_graph_process(d->graph, p->peer->node);
return 0;
}
@ -164,6 +154,7 @@ static const struct spa_graph_callbacks spa_graph_impl_default = {
SPA_VERSION_GRAPH_CALLBACKS,
.need_input = spa_graph_impl_need_input,
.have_output = spa_graph_impl_have_output,
.process = spa_graph_impl_process,
};

View file

@ -43,6 +43,9 @@ struct spa_graph_callbacks {
int (*need_input) (void *data, struct spa_graph_node *node);
int (*have_output) (void *data, struct spa_graph_node *node);
int (*process) (void *data, struct spa_graph_node *node);
int (*reuse_buffer) (void *data, struct spa_graph_node *node,
uint32_t port_id, uint32_t buffer_id);
};
struct spa_graph {
@ -53,19 +56,24 @@ struct spa_graph {
#define spa_graph_need_input(g,n) ((g)->callbacks->need_input((g)->callbacks_data, (n)))
#define spa_graph_have_output(g,n) ((g)->callbacks->have_output((g)->callbacks_data, (n)))
#define spa_graph_process(g,n) ((g)->callbacks->process((g)->callbacks_data, (n)))
#define spa_graph_reuse_buffer(g,n,p,i) ((g)->callbacks->reuse_buffer((g)->callbacks_data, (n),(p),(i)))
struct spa_graph_state {
int status; /**< status of the node */
uint32_t required; /**< required number of input nodes */
uint32_t pending; /**< number of input nodes pending */
};
struct spa_graph_node {
struct spa_list link; /**< link in graph nodes list */
struct spa_graph *graph; /**< owner graph */
struct spa_list ports[2]; /**< list of input and output ports */
struct spa_list ready_link; /**< link for scheduler */
#define SPA_GRAPH_NODE_FLAG_ASYNC (1 << 0)
uint32_t flags; /**< node flags */
uint32_t required[2]; /**< required number of ports */
uint32_t ready[2]; /**< number of ports with data */
int state; /**< state of the node */
struct spa_node *implementation;/**< node implementation */
struct spa_graph_state *state; /**< state of the node */
struct spa_list sched_link; /**< link for scheduler */
void *scheduler_data; /**< scheduler private data */
};
@ -74,7 +82,6 @@ struct spa_graph_port {
struct spa_graph_node *node; /**< owner node */
enum spa_direction direction; /**< port direction */
uint32_t port_id; /**< port id */
#define SPA_GRAPH_PORT_FLAG_DISABLED (1 << 0)
uint32_t flags; /**< port flags */
struct spa_io_buffers *io; /**< io area of the port */
struct spa_graph_port *peer; /**< peer */
@ -96,13 +103,14 @@ spa_graph_set_callbacks(struct spa_graph *graph,
}
static inline void
spa_graph_node_init(struct spa_graph_node *node)
spa_graph_node_init(struct spa_graph_node *node, struct spa_graph_state *state)
{
spa_list_init(&node->ports[SPA_DIRECTION_INPUT]);
spa_list_init(&node->ports[SPA_DIRECTION_OUTPUT]);
node->flags = 0;
node->required[SPA_DIRECTION_INPUT] = node->ready[SPA_DIRECTION_INPUT] = 0;
node->required[SPA_DIRECTION_OUTPUT] = node->ready[SPA_DIRECTION_OUTPUT] = 0;
node->state = state;
node->state->required = node->state->pending = 0;
node->state->status = SPA_STATUS_OK;
spa_debug("node %p init", node);
}
@ -118,8 +126,7 @@ spa_graph_node_add(struct spa_graph *graph,
struct spa_graph_node *node)
{
node->graph = graph;
node->state = SPA_STATUS_OK;
node->ready_link.next = NULL;
node->sched_link.next = NULL;
spa_list_append(&graph->nodes, &node->link);
spa_debug("node %p add", node);
}
@ -145,43 +152,53 @@ spa_graph_port_add(struct spa_graph_node *node,
spa_debug("port %p add to node %p", port, node);
port->node = node;
spa_list_append(&node->ports[port->direction], &port->link);
if (!(port->flags & SPA_PORT_INFO_FLAG_OPTIONAL))
node->required[port->direction]++;
}
static inline void spa_graph_node_remove(struct spa_graph_node *node)
{
spa_debug("node %p remove", node);
spa_list_remove(&node->link);
if (node->ready_link.next)
spa_list_remove(&node->ready_link);
if (node->sched_link.next)
spa_list_remove(&node->sched_link);
}
static inline void spa_graph_port_remove(struct spa_graph_port *port)
{
spa_debug("port %p remove", port);
spa_list_remove(&port->link);
if (!(port->flags & SPA_PORT_INFO_FLAG_OPTIONAL) &&
port->node->required[port->direction] > 0) {
port->node->required[port->direction]--;
}
port->node = NULL;
}
static inline void
spa_graph_port_link(struct spa_graph_port *out, struct spa_graph_port *in)
{
spa_debug("port %p link to %p", out, in);
spa_debug("port %p link to %p %p %p", out, in, in->node, in->node->state);
out->peer = in;
in->peer = out;
if (in->direction == SPA_DIRECTION_INPUT)
in->node->state->required++;
else
out->node->state->required++;
}
static inline void
spa_graph_port_unlink(struct spa_graph_port *port)
{
struct spa_graph_port *out, *in;
spa_debug("port %p unlink from %p", port, port->peer);
if (port->peer) {
port->peer->peer = NULL;
port->peer = NULL;
if (port->direction == SPA_DIRECTION_INPUT) {
in = port;
out = port->peer;
} else {
out = port;
in = port->peer;
}
if (out && in) {
in->node->state->required--;
out->peer = NULL;
in->peer = NULL;
}
}