RetroArch/tasks/task_image.c

467 lines
10 KiB
C
Raw Normal View History

2016-05-10 05:54:47 +00:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2016 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#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>
#include <lists/string_list.h>
#include <rhash.h>
#ifdef HAVE_RPNG
#include <formats/rpng.h>
#endif
2016-05-11 19:38:33 +00:00
#ifdef HAVE_RJPEG
#include <formats/rjpeg.h>
#endif
2016-05-10 05:54:47 +00:00
#ifdef HAVE_MENU
#include "../menu/menu_driver.h"
#endif
#include "tasks_internal.h"
#include "../verbosity.h"
2016-05-10 05:55:31 +00:00
enum image_status_enum
2016-05-10 05:54:47 +00:00
{
2016-05-10 05:55:31 +00:00
IMAGE_STATUS_POLL = 0,
IMAGE_STATUS_TRANSFER,
IMAGE_STATUS_TRANSFER_PARSE,
IMAGE_STATUS_PROCESS_TRANSFER,
IMAGE_STATUS_PROCESS_TRANSFER_PARSE,
IMAGE_STATUS_TRANSFER_PARSE_FREE
2016-05-10 05:54:47 +00:00
};
static int cb_image_menu_upload_generic(void *data, size_t len)
{
nbio_handle_t *nbio = (nbio_handle_t*)data;
unsigned r_shift, g_shift, b_shift, a_shift;
if (!nbio)
return -1;
if (nbio->image.processing_final_state == IMAGE_PROCESS_ERROR ||
nbio->image.processing_final_state == IMAGE_PROCESS_ERROR_END)
return -1;
video_texture_image_set_color_shifts(&r_shift, &g_shift, &b_shift,
&a_shift);
video_texture_image_color_convert(r_shift, g_shift, b_shift,
a_shift, &nbio->image.ti);
nbio->image.is_blocking_on_processing = false;
nbio->image.is_blocking = true;
nbio->image.is_finished = true;
nbio->is_finished = true;
return 0;
}
static int rarch_main_data_image_iterate_transfer_parse(nbio_handle_t *nbio)
{
if (nbio->image.handle && nbio->image.cb)
{
size_t len = 0;
nbio->image.cb(nbio, len);
}
return 0;
}
static int cb_nbio_default(void *data, size_t len)
{
nbio_handle_t *nbio = (nbio_handle_t*)data;
if (!data)
return -1;
(void)len;
nbio->is_finished = true;
return 0;
}
2016-05-12 11:13:14 +00:00
static int image_transfer_process(
void *data,
enum image_type_enum type,
uint32_t **buf,
unsigned *width, unsigned *height)
2016-05-10 06:23:49 +00:00
{
2016-05-12 11:13:14 +00:00
switch (type)
{
case IMAGE_TYPE_PNG:
2016-05-10 06:23:49 +00:00
#ifdef HAVE_RPNG
2016-05-12 11:13:14 +00:00
if (!rpng_is_valid((rpng_t*)data))
return IMAGE_PROCESS_ERROR;
2016-05-10 06:25:18 +00:00
2016-05-12 11:13:14 +00:00
return rpng_nbio_load_image_argb_process(
(rpng_t*)data,
buf,
width, height);
2016-05-10 06:23:49 +00:00
#endif
break;
case IMAGE_TYPE_JPEG:
2016-05-11 19:27:17 +00:00
#ifdef HAVE_RJPEG
#endif
break;
}
2016-05-10 06:23:49 +00:00
2016-05-12 11:13:14 +00:00
return 0;
}
static bool image_transfer_iterate(void *data, enum image_type_enum type)
{
switch (type)
{
case IMAGE_TYPE_PNG:
#ifdef HAVE_RPNG
if (!rpng_nbio_load_image_argb_iterate((rpng_t*)data))
return false;
#endif
break;
case IMAGE_TYPE_JPEG:
#ifdef HAVE_RJPEG
#endif
break;
}
return true;
}
static int rarch_main_data_image_process(
nbio_handle_t *nbio,
unsigned *width,
unsigned *height)
{
int retval = image_transfer_process(
(rpng_t*)nbio->image.handle,
nbio->image_type,
&nbio->image.ti.pixels, width, height);
if (retval == IMAGE_PROCESS_ERROR)
return IMAGE_PROCESS_ERROR;
2016-05-10 06:23:49 +00:00
nbio->image.ti.width = *width;
nbio->image.ti.height = *height;
2016-05-11 18:38:09 +00:00
return retval;
2016-05-10 06:23:49 +00:00
}
2016-05-11 01:55:09 +00:00
static int cb_image_menu_generic(nbio_handle_t *nbio)
2016-05-10 05:54:47 +00:00
{
2016-05-10 06:23:49 +00:00
int retval = 0;
2016-05-10 05:54:47 +00:00
unsigned width = 0, height = 0;
if (!nbio)
return -1;
2016-05-11 18:38:09 +00:00
retval = rarch_main_data_image_process(nbio,
&width, &height);
switch (retval)
2016-05-10 06:02:42 +00:00
{
case IMAGE_PROCESS_ERROR:
case IMAGE_PROCESS_ERROR_END:
return -1;
default:
break;
}
2016-05-10 05:54:47 +00:00
nbio->image.is_blocking_on_processing = true;
nbio->image.is_finished = false;
return 0;
}
static int cb_image_menu_thumbnail(void *data, size_t len)
{
nbio_handle_t *nbio = (nbio_handle_t*)data;
2016-05-11 01:55:09 +00:00
if (cb_image_menu_generic(nbio) != 0)
2016-05-10 05:54:47 +00:00
return -1;
nbio->image.cb = &cb_image_menu_upload_generic;
return 0;
}
2016-05-11 19:22:45 +00:00
static int rarch_main_data_image_iterate_process_transfer(nbio_handle_t *nbio)
{
unsigned i, width = 0, height = 0;
int retval = 0;
if (!nbio)
return -1;
for (i = 0; i < nbio->image.processing_pos_increment; i++)
{
retval = rarch_main_data_image_process(nbio,
&width, &height);
if (retval != IMAGE_PROCESS_NEXT)
break;
}
if (retval == IMAGE_PROCESS_NEXT)
return 0;
nbio->image.processing_final_state = retval;
return -1;
}
2016-05-12 11:13:14 +00:00
2016-05-10 05:54:47 +00:00
static int rarch_main_data_image_iterate_transfer(nbio_handle_t *nbio)
{
unsigned i;
if (!nbio)
2016-05-12 11:13:14 +00:00
goto error;
2016-05-10 05:54:47 +00:00
if (nbio->image.is_finished)
return 0;
for (i = 0; i < nbio->image.pos_increment; i++)
{
2016-05-12 11:13:14 +00:00
if (!image_transfer_iterate(nbio->image.handle, nbio->image_type))
goto error;
2016-05-10 05:54:47 +00:00
}
nbio->image.frame_count++;
return 0;
error:
return -1;
}
2016-05-10 06:09:12 +00:00
static void rarch_task_image_load_free_internal(nbio_handle_t *nbio)
{
nbio_image_handle_t *image = nbio ? &nbio->image : NULL;
switch (nbio->image_type)
{
case IMAGE_TYPE_PNG:
2016-05-10 06:09:12 +00:00
#ifdef HAVE_RPNG
rpng_nbio_load_image_free((rpng_t*)image->handle);
2016-05-10 06:09:12 +00:00
#endif
break;
case IMAGE_TYPE_JPEG:
2016-05-11 19:27:17 +00:00
#ifdef HAVE_RJPEG
#endif
break;
}
2016-05-10 06:09:12 +00:00
image->handle = NULL;
image->cb = NULL;
image->frame_count = 0;
}
2016-05-11 02:03:21 +00:00
static int cb_nbio_generic(nbio_handle_t *nbio, size_t *len)
2016-05-10 05:54:47 +00:00
{
void *ptr = NULL;
if (!nbio->image.handle)
2016-05-10 06:09:12 +00:00
goto error;
2016-05-10 05:54:47 +00:00
ptr = nbio_get_ptr(nbio->handle, len);
if (!ptr)
2016-05-10 06:09:12 +00:00
goto error;
2016-05-10 05:54:47 +00:00
switch (nbio->image_type)
{
case IMAGE_TYPE_PNG:
2016-05-11 01:51:26 +00:00
#ifdef HAVE_RPNG
rpng_set_buf_ptr((rpng_t*)nbio->image.handle, (uint8_t*)ptr);
2016-05-11 01:51:26 +00:00
#endif
break;
case IMAGE_TYPE_JPEG:
2016-05-11 19:27:17 +00:00
#ifdef HAVE_RJPEG
#endif
break;
}
2016-05-11 01:51:26 +00:00
2016-05-10 05:54:47 +00:00
nbio->image.pos_increment = (*len / 2) ? (*len / 2) : 1;
nbio->image.processing_pos_increment = (*len / 4) ? (*len / 4) : 1;
switch (nbio->image_type)
{
case IMAGE_TYPE_PNG:
2016-05-11 01:51:26 +00:00
#ifdef HAVE_RPNG
if (!rpng_nbio_load_image_argb_start((rpng_t*)nbio->image.handle))
goto error;
2016-05-11 01:51:26 +00:00
#endif
break;
case IMAGE_TYPE_JPEG:
2016-05-11 19:27:17 +00:00
#ifdef HAVE_RJPEG
#endif
break;
}
2016-05-10 05:54:47 +00:00
nbio->image.is_blocking = false;
nbio->image.is_finished = false;
nbio->is_finished = true;
return 0;
2016-05-10 06:09:12 +00:00
error:
rarch_task_image_load_free_internal(nbio);
return -1;
2016-05-10 05:54:47 +00:00
}
2016-05-11 01:56:31 +00:00
static int cb_nbio_image_menu_thumbnail(void *data, size_t len)
2016-05-10 05:54:47 +00:00
{
nbio_handle_t *nbio = (nbio_handle_t*)data;
if (!nbio || !data)
return -1;
switch (nbio->image_type)
{
case IMAGE_TYPE_PNG:
2016-05-11 01:51:26 +00:00
#ifdef HAVE_RPNG
nbio->image.handle = rpng_alloc();
2016-05-11 01:51:26 +00:00
#endif
break;
case IMAGE_TYPE_JPEG:
2016-05-11 19:27:17 +00:00
#ifdef HAVE_RJPEG
#endif
break;
}
2016-05-11 01:51:26 +00:00
2016-05-10 05:54:47 +00:00
nbio->image.cb = &cb_image_menu_thumbnail;
2016-05-11 02:03:21 +00:00
return cb_nbio_generic(nbio, &len);
2016-05-10 05:54:47 +00:00
}
bool rarch_task_image_load_handler(retro_task_t *task)
{
nbio_handle_t *nbio = (nbio_handle_t*)task->state;
nbio_image_handle_t *image = nbio ? &nbio->image : NULL;
switch (image->status)
{
2016-05-10 05:55:31 +00:00
case IMAGE_STATUS_PROCESS_TRANSFER:
2016-05-10 05:54:47 +00:00
if (rarch_main_data_image_iterate_process_transfer(nbio) == -1)
2016-05-10 05:55:31 +00:00
image->status = IMAGE_STATUS_PROCESS_TRANSFER_PARSE;
2016-05-10 05:54:47 +00:00
break;
2016-05-10 05:55:31 +00:00
case IMAGE_STATUS_TRANSFER_PARSE:
2016-05-10 05:54:47 +00:00
rarch_main_data_image_iterate_transfer_parse(nbio);
if (image->is_blocking_on_processing)
2016-05-10 05:55:31 +00:00
image->status = IMAGE_STATUS_PROCESS_TRANSFER;
2016-05-10 05:54:47 +00:00
break;
2016-05-10 05:55:31 +00:00
case IMAGE_STATUS_TRANSFER:
2016-05-10 05:54:47 +00:00
if (!image->is_blocking)
if (rarch_main_data_image_iterate_transfer(nbio) == -1)
2016-05-10 05:55:31 +00:00
image->status = IMAGE_STATUS_TRANSFER_PARSE;
2016-05-10 05:54:47 +00:00
break;
2016-05-10 05:55:31 +00:00
case IMAGE_STATUS_PROCESS_TRANSFER_PARSE:
2016-05-11 01:55:09 +00:00
rarch_main_data_image_iterate_transfer_parse(nbio);
2016-05-10 05:54:47 +00:00
if (!image->is_finished)
break;
2016-05-10 05:55:31 +00:00
case IMAGE_STATUS_TRANSFER_PARSE_FREE:
case IMAGE_STATUS_POLL:
2016-05-10 05:54:47 +00:00
default:
break;
}
if ( nbio->is_finished
&& nbio->image.is_finished
&& !task->cancelled)
{
task->task_data = malloc(sizeof(nbio->image.ti));
if (task->task_data)
memcpy(task->task_data, &nbio->image.ti, sizeof(nbio->image.ti));
return false;
}
return true;
}
bool rarch_task_push_image_load(const char *fullpath,
const char *type, retro_task_callback_t cb, void *user_data)
{
2016-05-10 06:28:48 +00:00
nbio_handle_t *nbio = NULL;
retro_task_t *t = NULL;
uint32_t cb_type_hash = djb2_calculate(type);
struct nbio_t *handle = nbio_open(fullpath, NBIO_READ);
2016-05-10 05:54:47 +00:00
if (!handle)
2016-05-10 06:28:48 +00:00
goto error;
2016-05-10 05:54:47 +00:00
nbio = (nbio_handle_t*)calloc(1, sizeof(*nbio));
if (!nbio)
2016-05-10 06:28:48 +00:00
goto error;
2016-05-10 05:54:47 +00:00
nbio->handle = handle;
nbio->is_finished = false;
nbio->cb = &cb_nbio_default;
nbio->status = NBIO_STATUS_TRANSFER;
2016-05-10 05:55:31 +00:00
nbio->image.status = IMAGE_STATUS_TRANSFER;
2016-05-10 05:54:47 +00:00
if (strstr(fullpath, ".png"))
nbio->image_type = IMAGE_TYPE_PNG;
2016-05-10 05:54:47 +00:00
else if (strstr(fullpath, ".jpeg") || strstr(fullpath, ".jpg"))
nbio->image_type = IMAGE_TYPE_JPEG;
switch (cb_type_hash)
2016-05-10 05:54:47 +00:00
{
case CB_MENU_WALLPAPER:
case CB_MENU_THUMBNAIL:
nbio->cb = &cb_nbio_image_menu_thumbnail;
break;
default:
break;
2016-05-10 05:54:47 +00:00
}
nbio_begin_read(handle);
t = (retro_task_t*)calloc(1, sizeof(*t));
if (!t)
2016-05-10 06:28:48 +00:00
goto error;
2016-05-10 05:54:47 +00:00
t->state = nbio;
t->handler = rarch_task_file_load_handler;
t->callback = cb;
t->user_data = user_data;
task_queue_ctl(TASK_QUEUE_CTL_PUSH, t);
2016-05-10 06:28:48 +00:00
2016-05-10 05:54:47 +00:00
return true;
2016-05-10 06:28:48 +00:00
error:
if (t)
free(t);
if (nbio)
free(nbio);
RARCH_ERR("[image load] Failed to open '%s': %s.\n",
fullpath, strerror(errno));
return false;
2016-05-10 05:54:47 +00:00
}
void rarch_task_image_load_free(retro_task_t *task)
{
nbio_handle_t *nbio = (nbio_handle_t*)task->state;
2016-05-10 06:09:12 +00:00
rarch_task_image_load_free_internal(nbio);
2016-05-10 05:54:47 +00:00
}
2016-05-10 06:09:12 +00:00