render_multi.c
author Tero Marttila <terom@fixme.fi>
Sun, 08 Jun 2008 23:10:36 +0300
changeset 16 50995bbe442a
parent 15 e7f0697814dc
child 17 8e8b56b0e0f5
permissions -rw-r--r--
render_multi works a bit more efficiently now

committer: Tero Marttila <terom@fixme.fi>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>

#include "common.h"
#include "render_internal.h"
#include "render_multi.h"
#include "render_remote.h"
#include "remote_node.h"
#include "render_png.h"

/*
// the states we can go through
enum render_multi_state {
    STATE_INIT,                 // the render_multi is in the process of being initialized
    STATE_SENDING,              // we're still waiting for some of the requests to be sent
    STATE_RENDER,               // we're handling data now!
    STATE_DATA_DONE,            // we're finished with all the data
    STATE_PNG_DONE,             // PNG data's done
    STATE_FAILED,               // we failed
};

enum render_multi_sub_state {
    STATE_INIT,                 // the render_multi_sub is in the process of being initialized
    STATE_SENDING,              // we're waiting for the requests to be sent
    STATE_FILL_ROW,             // we're filling the row with data
    STATE_ROW_FULL,             // our row is full
    STATE_DATA_DONE,            // we're finished with all the data
    STATE_PNG_DONE,             // PNG data's done
    STATE_FAILED,               // we failed

};
*/

struct render_multi {
    // these are used as arguments to render_remote
    struct render_multi_sub {
        // our offset in the list
        int index;

        // the render_remote_ctx
        struct render_remote *render_remote;
        
        // a pointer to ourself
        struct render_multi *self;

        // _render_multi_sent called for this?
        int render_sent;

        // our offset into the row, static
        size_t row_offset;

        // how wide our slice is, static
        size_t slice_width;

        // how many bytes we have already written into the current row
        size_t col;

        // _render_multi_done called for this?
        int render_done;

    } remote_renders[RENDER_MULTI_NODES_MAX];
    
    // how many remote_renders we have
    int remote_render_count;

    // is this still alive?
    int alive;

    // how many remote renders have been succesfully cb_sent?
    int renders_sent;

    // how many of the renders are done?
    int renders_done;

    // have we called cb_sent?
    int have_sent;

    // the png thing
    struct render_png *png_info;

    // has render_png_done returned?
    int png_done;

    // our pixel data row
    unsigned char *rowbuf;

    // buffer render_png output in this
    struct evbuffer *out_buf;

    // our own callbacks that we call
    void (*cb_sent)(void *arg);
    void (*cb_data)(struct evbuffer *buf, void *arg);
    void (*cb_done)(void *arg);
    void (*cb_fail)(void *arg);

    void *cb_arg;
};

#define FAIL_PARTIAL 0x01
#define FAIL_SILENT 0x02

// prototypes
static void _render_multi_do_free (struct render_multi *ctx);
static void _render_multi_do_sent (struct render_multi *ctx);
static void _render_multi_do_png_data (struct render_multi *ctx);
static void _render_multi_do_png_done (struct render_multi *ctx);
static void _render_multi_do_done (struct render_multi *ctx);
static void _render_multi_do_fail (struct render_multi *ctx, int flags);

/*
 * Actually free the request. Should be de-initialized (either _render_multi_error, or _render_done when this is called
 */
static void _render_multi_do_free (struct render_multi *ctx) {
    assert(ctx && ctx->alive == 0);
    
    if (ctx->rowbuf) {
        free(ctx->rowbuf);
        ctx->rowbuf = NULL;
    }

    if (ctx->out_buf) {
        evbuffer_free(ctx->out_buf);
        ctx->out_buf = NULL;
    }

    free(ctx);
}

// the request has been sent
static void _render_multi_do_sent (struct render_multi *ctx) {
    int i;

    // check that all the remote_renders have indeed been sent
    for (i = 0; i < ctx->remote_render_count; i++) {
        assert(ctx->remote_renders[i].render_sent);
    }

    // assert the callbacks are still valid
    assert(ctx->cb_sent && ctx->cb_fail && ctx->cb_done);

    // call cb_sent and then invalidate it
    ctx->cb_sent(ctx->cb_arg);
    ctx->cb_sent = NULL;
    ctx->have_sent = 1;
    
    // we're going to always have the PNG header data buffered at this point, so give that to the user right away
    assert(evbuffer_get_length(ctx->out_buf) > 0);
    _render_multi_do_png_data(ctx);
}

// possibly call cb_data, and if the renders are all done and the buffer is empty, cb_done
static void _render_multi_do_png_data (struct render_multi *ctx) {
    // at first we have to wait until we've called cb_sent
    if (!ctx->have_sent)
        return;

    // got any PNG data in there?
    if (evbuffer_get_length(ctx->out_buf))
        ctx->cb_data(ctx->out_buf, ctx->cb_arg);
    
    // was that the last piece of PNG data?
    if (ctx->png_done && evbuffer_get_length(ctx->out_buf) == 0) {
        // PNG data done!
        _render_multi_do_png_done(ctx);

    }
}

// the PNG rendering completed succesfully
static void _render_multi_do_png_done (struct render_multi *ctx) {
    // check that ctx is still valid
    assert(ctx->alive);

    // mark as not alive
    ctx->alive = 0;

    // call cb_done
    ctx->cb_done(ctx->cb_arg);

    // don't free ourself, our user does that (probably did already)
    return;
}

// the request completed normally, flush the png data and return
// _render_multi_png_done takes care of calling cb_done, not us!
static void _render_multi_do_done (struct render_multi *ctx) {
    assert(ctx->alive && ctx->png_info);

    int i;
    
    // check that all the remote_renders are indeed complete
    for (i = 0; i < ctx->remote_render_count; i++) {
        assert(ctx->remote_renders[i].render_remote == NULL);
        assert(ctx->remote_renders[i].col == 0);
    }
    
    // we need to make sure this render_png_done call only happens once (render_multi_do_fail, state bugs)
    if (render_png_done(ctx->png_info)) {
        ctx->png_info = NULL;

        ERROR("render_png_done");
    }
    
    ctx->png_info = NULL;

    // mark the png as done now
    ctx->png_done = 1;

    // if that all the data handled now, we're done
    _render_multi_do_png_data(ctx);

    // don't free ourself, our user does that (probably already did, via render_png_done)
    return;

error:    
    /* render_png_done failed, probably because we didn't have enough data */
    _render_multi_do_fail(ctx, FAIL_PARTIAL);
}

// the request completed abnormally. Flags:
//  FAIL_SILENT     - don't call cb_fail
//  FAIL_PARTIAL    - assume png_info may be NULL
static void _render_multi_do_fail (struct render_multi *ctx, int flags) {
    int i;

    // check that ctx is still valid
    assert(ctx->alive || flags & FAIL_PARTIAL);

    // mark as not alive
    ctx->alive = 0;

    // cancel any in-progress remote renders
    for (i = 0; i < ctx->remote_render_count; i++)
        if (ctx->remote_renders[i].render_remote) {
            render_remote_cancel(ctx->remote_renders[i].render_remote);
            ctx->remote_renders[i].render_remote = NULL;
        }
    
    if (!(flags & FAIL_PARTIAL) || ctx->png_info) {
        // abort the render_png
        render_png_abort(ctx->png_info);
        ctx->png_info = NULL;
    }
    
    // check that both callbacks are still valid
    assert(ctx->cb_fail && ctx->cb_done);
    
    if (!(flags & FAIL_SILENT)) {
        // call cb_fail and then invalidate it
        ctx->cb_fail(ctx->cb_arg);
    }

    ctx->cb_fail = NULL;

    // don't free ourself, our user does that
}

/*
 * One of the remote render commands has succesfully been sent.
 *
 * Once all of these commands have been sent, invoke our cb_sent.
 */
static void _render_multi_sent (void *arg) {
    struct render_multi_sub *ctx = arg;
    
    // mark these as sent
    ctx->render_sent = 1;
    ctx->self->renders_sent++;

    // have all render_sub_ctxs been sent?
    if (ctx->self->renders_sent == ctx->self->remote_render_count) {
        // tell our user
        _render_multi_do_sent(ctx->self);
    }
}

/*
 * One render node failed, abort the whole thing
 */
static void _render_multi_fail (void *arg) {
    struct render_multi_sub *ctx = arg;

    // free this ctx's remote render
    render_remote_free(ctx->render_remote);
    ctx->render_remote = NULL;
    
    // cancel the rest + cb_fail
    _render_multi_do_fail(ctx->self, 0);
}

/*
 * Got new data for some remote render
 */
static void _render_multi_data_raw (int fd, short event, void *arg) {
    struct render_multi_sub *ctx = arg;
    int ret;

    assert(ctx->col <= ctx->slice_width);   // check it isn't out of range

    // if our slice is full, we don't want to receive any more data
    if (ctx->col == ctx->slice_width)
        return;

    // read new data into our slice
    ret = read(fd, 
        ctx->self->rowbuf + ctx->row_offset + ctx->col, // our fixed offset + partial row offset
        ctx->slice_width - ctx->col                     // how many bytes left in the window
    );
    
    // errors/EOF?
    if (ret == -1) {
        if (errno == EAGAIN) {
            // false alarm
            goto reschedule;

        } else
            ERROR("read");

    } else if (ret == 0) {
        // mark it as done
        ctx->render_done = 1;
        ctx->self->renders_done++;
       
        // this ctx's remote render is done
        render_remote_done(ctx->render_remote);
        ctx->render_remote = NULL;

        // is the data incomplete?
        if (!(ctx->col == ctx->slice_width || ctx->col == 0))
            ERROR("incomplete data for slice %d: %zu/%zu bytes", ctx->index, ctx->col, ctx->slice_width);

        // are all of them done?
        if (ctx->self->renders_done == ctx->self->remote_render_count) {
            // finish it off
            _render_multi_do_done(ctx->self);

        } // else, just wait for the rest to complete
        
        // do *NOT* reschedule ourself, ctx->render_remote is invalid anyways (as is ctx!)
        return;
    }
    
    // ok, we received some data normally
    ctx->col += ret;

    // is our slice full now?
    if (ctx->col == ctx->slice_width) {
        // is the row complete now?
        int i;
        for (i = 0; i < ctx->self->remote_render_count; i++) {
            if (ctx->self->remote_renders[i].col < ctx->self->remote_renders[i].slice_width)
                break;
        }
        
        if (i == ctx->self->remote_render_count) {
            // pass the data to render_png, this results in calls to _render_multi_png_data
            if (render_png_row(ctx->self->png_info, ctx->self->rowbuf))
                ERROR("render_png_row");

            // clear the col values and reschedule the reads in case they were paused
            for (i = 0; i < ctx->self->remote_render_count; i++) {
                ctx->self->remote_renders[i].col = 0;
                render_remote_reschedule(ctx->self->remote_renders[i].render_remote);
            }
        }
    }

    // ok, reschedule ourselves

reschedule:
    // reschedule a new call once we get more data
    render_remote_reschedule(ctx->render_remote);

    return;

error:
    _render_multi_do_fail(ctx->self, 0);
}

/*
 * We fed a row of pixels into render_png, and this PNG data came out.
 *
 * We need to pass it back to our caller
 */
static int _render_multi_png_data (const unsigned char *data, size_t length, void *arg) {
    struct render_multi *ctx = arg;

    // XXX: need a better user-API to avoid these data copies
    if (evbuffer_add(ctx->out_buf, data, length))
        ERROR("evbuffer_add");
    
    // handle cb_data/cb_done
    _render_multi_do_png_data(ctx);
    
    // ok
    return 0;

error:
    // don't do any failing here, this will return control to a _render_* function that will handle it
    return -1;
}

#define ROUND_DOWN(dividend, divisor) ((dividend) / (divisor))
#define ROUND_UP(dividend, divisor) (((dividend) / (divisor)) + ((dividend) % (divisor)))

#define HALF(a, b) (( a + b) / 2)

struct render_multi *render_multi (
        struct render *render,               // what to render
        struct remote_pool *pool_info,    // what render pool to use
        void (*cb_sent)(void *arg),
        void (*cb_data)(struct evbuffer *buf, void *arg),
        void (*cb_done)(void *arg),
        void (*cb_fail)(void *arg),
        void *cb_arg
) {
    struct render_multi *ctx = NULL;
    struct render r_left, r_right;
    
    // alloc the render_multi
    ctx = calloc(1, sizeof(struct render_multi));

    if (!ctx)
        ERROR("calloc");

    // init the remote_render
    // for now, just split it in half into two render_ts
    ctx->remote_renders[0].index = 0;
    ctx->remote_renders[0].self = ctx;
    ctx->remote_renders[0].slice_width = render->img_w / 2;
    ctx->remote_renders[0].row_offset = 0;

    ctx->remote_renders[1].index = 1;
    ctx->remote_renders[1].self = ctx;
    ctx->remote_renders[1].slice_width = render->img_w / 2 + render->img_w % 2;
    ctx->remote_renders[1].row_offset = render->img_w / 2;

    ctx->remote_render_count = 2;

    assert(RENDER_MULTI_NODES_MAX >= 2);

    if (
            render_init(&r_left, RENDER_RAW)
         || render_init(&r_right, RENDER_RAW)
         || render_set_size(&r_left, ctx->remote_renders[0].slice_width, render->img_h)
         || render_set_size(&r_right, ctx->remote_renders[1].slice_width, render->img_h)
         || render_region_raw(&r_left, render->x1, render->y1, HALF(render->x1, render->x2), render->y2)
         || render_region_raw(&r_right, HALF(render->x1, render->x2), render->y1, render->x2, render->y2)
    )
        ERROR("render_{init,set_size,set_region_raw}");
            
    // store the provided callback functions
    ctx->cb_sent = cb_sent;
    ctx->cb_data = cb_data;
    ctx->cb_done = cb_done;
    ctx->cb_fail = cb_fail;
    ctx->cb_arg = cb_arg;

    // our rowbuf
    if (!(ctx->rowbuf = malloc(render->img_w)))
        ERROR("malloc");

    // store our render_png callbacks, must be before png_info
    if (render_io_custom(render, &_render_multi_png_data, NULL, ctx))
        ERROR("render_io_custom");

    // evbuffer, must be before png_info
    if (!(ctx->out_buf = evbuffer_new()))
        ERROR("evbuffer_new");

    // png info
    if (!(ctx->png_info = render_png_init(render)))
        ERROR("render_png_init");

    // pull two nodes from the pool
    struct remote_node *node_left, *node_right;

    if (
            !(node_left = remote_pool_get(pool_info))
         || !(node_right = remote_pool_get(pool_info))
    )
        ERROR("remote_pool_get");
    
    // the two render_remote calls
    if (
            !(ctx->remote_renders[0].render_remote = render_remote_rawio(&r_left, node_left, 
                &_render_multi_sent, &_render_multi_fail, &_render_multi_data_raw, &ctx->remote_renders[0]))
         || !(ctx->remote_renders[1].render_remote = render_remote_rawio(&r_right, node_right,
                &_render_multi_sent, &_render_multi_fail, &_render_multi_data_raw, &ctx->remote_renders[1]))
    )
        ERROR("render_remote");

    // we are now alive
    ctx->alive = 1;
    
    // I guess that's a succesfull start now
    return ctx;

error:
    _render_multi_do_fail(ctx, FAIL_SILENT | FAIL_PARTIAL);

    return NULL;
}


void render_multi_set_recv (struct render_multi *ctx, size_t recv_threshold, size_t unread_buffer) {

}

int render_multi_flush (struct render_multi *ctx) {
    _render_multi_do_png_data(ctx);

    return 0;
}

void render_multi_cancel (struct render_multi *ctx) {
    _render_multi_do_fail(ctx, FAIL_SILENT);
}

void render_multi_free (struct render_multi *ctx) {
    _render_multi_do_free(ctx);
}