(task_image) Cleanups

This commit is contained in:
twinaphex 2017-05-09 20:01:42 +02:00
parent da1ab24c57
commit 7ed57ee77d
3 changed files with 78 additions and 66 deletions

View File

@ -16,7 +16,6 @@
#include <string.h>
#include <errno.h>
#include <file/nbio.h>
#include <formats/image.h>
#include <compat/strl.h>
#include <retro_assert.h>
#include <retro_miscellaneous.h>
@ -76,24 +75,13 @@ void task_file_load_handler(retro_task_t *task)
case NBIO_STATUS_INIT:
if (nbio && !string_is_empty(nbio->path))
{
const char *fullpath = nbio->path;
struct nbio_t *handle = nbio_open(fullpath, NBIO_READ);
struct nbio_t *handle = nbio_open(nbio->path, NBIO_READ);
if (handle)
{
nbio->handle = handle;
nbio->status = NBIO_STATUS_TRANSFER;
if (strstr(fullpath, file_path_str(FILE_PATH_PNG_EXTENSION)))
nbio->image_type = IMAGE_TYPE_PNG;
else if (strstr(fullpath, file_path_str(FILE_PATH_JPEG_EXTENSION))
|| strstr(fullpath, file_path_str(FILE_PATH_JPG_EXTENSION)))
nbio->image_type = IMAGE_TYPE_JPEG;
else if (strstr(fullpath, file_path_str(FILE_PATH_BMP_EXTENSION)))
nbio->image_type = IMAGE_TYPE_BMP;
else if (strstr(fullpath, file_path_str(FILE_PATH_TGA_EXTENSION)))
nbio->image_type = IMAGE_TYPE_TGA;
nbio_begin_read(handle);
return;
}
@ -116,16 +104,17 @@ void task_file_load_handler(retro_task_t *task)
break;
}
switch (nbio->image_type)
switch (nbio->type)
{
case IMAGE_TYPE_PNG:
case IMAGE_TYPE_JPEG:
case IMAGE_TYPE_TGA:
case IMAGE_TYPE_BMP:
case NBIO_TYPE_PNG:
case NBIO_TYPE_JPEG:
case NBIO_TYPE_TGA:
case NBIO_TYPE_BMP:
if (!task_image_load_handler(task))
task_set_finished(task, true);
break;
case 0:
case NBIO_TYPE_NONE:
default:
if (nbio->is_finished)
task_set_finished(task, true);
break;

View File

@ -43,6 +43,7 @@ enum image_status_enum
struct nbio_image_handle
{
enum image_type_enum type;
struct texture_image ti;
bool is_blocking;
bool is_blocking_on_processing;
@ -88,29 +89,14 @@ static int cb_image_menu_upload_generic(void *data, size_t len)
return 0;
}
static int task_image_iterate_transfer_parse(nbio_handle_t *nbio)
{
struct nbio_image_handle *image = (struct nbio_image_handle*)nbio->data;
if (image->handle && image->cb)
{
size_t len = 0;
image->cb(nbio, len);
}
return 0;
}
static int task_image_process(
nbio_handle_t *nbio,
struct nbio_image_handle *image,
unsigned *width,
unsigned *height)
{
struct nbio_image_handle *image = (struct nbio_image_handle*)nbio->data;
int retval = image_transfer_process(
image->handle,
nbio->image_type,
image->type,
&image->ti.pixels, image->size, width, height);
if (retval == IMAGE_PROCESS_ERROR)
@ -132,7 +118,7 @@ static int cb_image_menu_generic(nbio_handle_t *nbio)
if (!image)
return -1;
retval = task_image_process(nbio, &width, &height);
retval = task_image_process(image, &width, &height);
switch (retval)
{
@ -162,20 +148,19 @@ static int cb_image_menu_thumbnail(void *data, size_t len)
return 0;
}
static int task_image_iterate_process_transfer(nbio_handle_t *nbio)
static int task_image_iterate_process_transfer(struct nbio_image_handle *image)
{
unsigned i;
int retval = 0;
unsigned width = 0;
unsigned height = 0;
struct nbio_image_handle *image = (struct nbio_image_handle*)nbio->data;
if (!image)
return -1;
for (i = 0; i < image->processing_pos_increment; i++)
{
retval = task_image_process(nbio,
retval = task_image_process(image,
&width, &height);
if (retval != IMAGE_PROCESS_NEXT)
break;
@ -188,10 +173,9 @@ static int task_image_iterate_process_transfer(nbio_handle_t *nbio)
return -1;
}
static int task_image_iterate_transfer(nbio_handle_t *nbio)
static int task_image_iterate_transfer(struct nbio_image_handle *image)
{
unsigned i;
struct nbio_image_handle *image = (struct nbio_image_handle*)nbio->data;
if (!image)
goto error;
@ -201,7 +185,7 @@ static int task_image_iterate_transfer(nbio_handle_t *nbio)
for (i = 0; i < image->pos_increment; i++)
{
if (!image_transfer_iterate(image->handle, nbio->image_type))
if (!image_transfer_iterate(image->handle, image->type))
goto error;
}
@ -211,14 +195,9 @@ error:
return -1;
}
static void task_image_load_free_internal(nbio_handle_t *nbio)
static void task_image_load_free_internal(struct nbio_image_handle *image)
{
struct nbio_image_handle *image = (struct nbio_image_handle*)nbio->data;
if (!image)
return;
image_transfer_free(image->handle, nbio->image_type);
image_transfer_free(image->handle, image->type);
image->handle = NULL;
image->cb = NULL;
@ -232,14 +211,14 @@ static int cb_nbio_generic(nbio_handle_t *nbio, size_t *len)
if (!ptr || !image || !image->handle)
goto error;
image_transfer_set_buffer_ptr(image->handle, nbio->image_type, ptr);
image_transfer_set_buffer_ptr(image->handle, image->type, ptr);
image->size = *len;
image->pos_increment = (*len / 2) ? ((unsigned)(*len / 2)) : 1;
image->processing_pos_increment = (*len / 4) ?
((unsigned)(*len / 4)) : 1;
if (!image_transfer_start(image->handle, nbio->image_type))
if (!image_transfer_start(image->handle, image->type))
goto error;
image->is_blocking = false;
@ -249,7 +228,8 @@ static int cb_nbio_generic(nbio_handle_t *nbio, size_t *len)
return 0;
error:
task_image_load_free_internal(nbio);
if (image)
task_image_load_free_internal(image);
if (nbio->data)
free(nbio->data);
nbio->data = NULL;
@ -258,19 +238,18 @@ error:
static int cb_nbio_image_menu_thumbnail(void *data, size_t len)
{
struct nbio_image_handle *image = NULL;
void *handle = NULL;
nbio_handle_t *nbio = (nbio_handle_t*)data;
void *handle = NULL;
nbio_handle_t *nbio = (nbio_handle_t*)data;
struct nbio_image_handle *image = nbio ?
(struct nbio_image_handle*)nbio->data : NULL;
if (!nbio)
goto error;
handle = image_transfer_new(nbio->image_type);
handle = image_transfer_new(image->type);
if (!handle)
goto error;
image = (struct nbio_image_handle*)nbio->data;
image->handle = handle;
image->size = len;
@ -292,21 +271,29 @@ bool task_image_load_handler(retro_task_t *task)
switch (image->status)
{
case IMAGE_STATUS_PROCESS_TRANSFER:
if (task_image_iterate_process_transfer(nbio) == -1)
if (task_image_iterate_process_transfer(image) == -1)
image->status = IMAGE_STATUS_PROCESS_TRANSFER_PARSE;
break;
case IMAGE_STATUS_TRANSFER_PARSE:
task_image_iterate_transfer_parse(nbio);
if (image->handle && image->cb)
{
size_t len = 0;
image->cb(nbio, len);
}
if (image->is_blocking_on_processing)
image->status = IMAGE_STATUS_PROCESS_TRANSFER;
break;
case IMAGE_STATUS_TRANSFER:
if (!image->is_blocking)
if (task_image_iterate_transfer(nbio) == -1)
if (task_image_iterate_transfer(image) == -1)
image->status = IMAGE_STATUS_TRANSFER_PARSE;
break;
case IMAGE_STATUS_PROCESS_TRANSFER_PARSE:
task_image_iterate_transfer_parse(nbio);
if (image->handle && image->cb)
{
size_t len = 0;
image->cb(nbio, len);
}
if (!image->is_finished)
break;
case IMAGE_STATUS_TRANSFER_PARSE_FREE:
@ -356,6 +343,31 @@ bool task_push_image_load(const char *fullpath, retro_task_callback_t cb, void *
if (!image)
goto error;
nbio->type = NBIO_TYPE_NONE;
image->type = IMAGE_TYPE_NONE;
if (strstr(fullpath, file_path_str(FILE_PATH_PNG_EXTENSION)))
{
nbio->type = NBIO_TYPE_PNG;
image->type = IMAGE_TYPE_PNG;
}
else if (strstr(fullpath, file_path_str(FILE_PATH_JPEG_EXTENSION))
|| strstr(fullpath, file_path_str(FILE_PATH_JPG_EXTENSION)))
{
nbio->type = NBIO_TYPE_JPEG;
image->type = IMAGE_TYPE_JPEG;
}
else if (strstr(fullpath, file_path_str(FILE_PATH_BMP_EXTENSION)))
{
nbio->type = NBIO_TYPE_BMP;
image->type = IMAGE_TYPE_BMP;
}
else if (strstr(fullpath, file_path_str(FILE_PATH_TGA_EXTENSION)))
{
nbio->type = NBIO_TYPE_TGA;
image->type = IMAGE_TYPE_TGA;
}
image->status = IMAGE_STATUS_TRANSFER;
nbio->data = (struct nbio_image_handle*)image;
@ -393,7 +405,10 @@ void task_image_load_free(retro_task_t *task)
if (nbio)
{
task_image_load_free_internal(nbio);
struct nbio_image_handle *image = (struct nbio_image_handle*)nbio->data;
if (image)
task_image_load_free_internal(image);
if (nbio->data)
free(nbio->data);
nbio_free(nbio->handle);

View File

@ -24,7 +24,6 @@
#include <queues/message_queue.h>
#include <queues/task_queue.h>
#include <formats/image.h>
#ifdef HAVE_CONFIG_H
#include "../config.h"
@ -62,9 +61,18 @@ enum nbio_status_flags
NBIO_FLAG_IMAGE_SUPPORTS_RGBA
};
enum nbio_type
{
NBIO_TYPE_NONE = 0,
NBIO_TYPE_JPEG,
NBIO_TYPE_PNG,
NBIO_TYPE_TGA,
NBIO_TYPE_BMP
};
typedef struct nbio_handle
{
enum image_type_enum image_type;
enum nbio_type type;
void *data;
bool is_finished;
transfer_cb_t cb;