libresman

changeset 20:c6073bf9fd38

finally! async loading works. Now on to inotify...
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 12 Feb 2014 16:02:12 +0200
parents ee117b67b3e5
children fe0dbdfbe403
files examples/imgthumbs/imgthumbs.vcproj examples/imgthumbs/src/thumbs.c examples/imgthumbs/src/thumbs.h libresman-static.vcproj src/resman.c
diffstat 5 files changed, 47 insertions(+), 55 deletions(-) [+]
line diff
     1.1 --- a/examples/imgthumbs/imgthumbs.vcproj	Wed Feb 12 06:54:52 2014 +0200
     1.2 +++ b/examples/imgthumbs/imgthumbs.vcproj	Wed Feb 12 16:02:12 2014 +0200
     1.3 @@ -19,7 +19,7 @@
     1.4  		<Configuration
     1.5  			Name="Debug|Win32"
     1.6  			OutputDirectory="$(SolutionDir)$(ConfigurationName)"
     1.7 -			IntermediateDirectory="$(ConfigurationName)"
     1.8 +			IntermediateDirectory="$(ProjectDir)$(ConfigurationName)"
     1.9  			ConfigurationType="1"
    1.10  			InheritedPropertySheets="..\resman.vsprops"
    1.11  			CharacterSet="2"
    1.12 @@ -93,7 +93,7 @@
    1.13  		<Configuration
    1.14  			Name="Release|Win32"
    1.15  			OutputDirectory="$(SolutionDir)$(ConfigurationName)"
    1.16 -			IntermediateDirectory="$(ConfigurationName)"
    1.17 +			IntermediateDirectory="$(ProjectDir)$(ConfigurationName)"
    1.18  			ConfigurationType="1"
    1.19  			InheritedPropertySheets="..\resman.vsprops"
    1.20  			CharacterSet="2"
     2.1 --- a/examples/imgthumbs/src/thumbs.c	Wed Feb 12 06:54:52 2014 +0200
     2.2 +++ b/examples/imgthumbs/src/thumbs.c	Wed Feb 12 16:02:12 2014 +0200
     2.3 @@ -9,19 +9,12 @@
     2.4  #include "thumbs.h"
     2.5  #include "resman.h"
     2.6  
     2.7 -#undef DBG_SYNC
     2.8 -
     2.9 -#ifndef GL_COMPRESSED_RGB
    2.10 -#define GL_COMPRESSED_RGB	0x84ed
    2.11 -#endif
    2.12 -
    2.13 -struct resman *texman;
    2.14 -struct thumbnail *dbg;
    2.15 -
    2.16  static int load_res_texture(const char *fname, int id, void *cls);
    2.17  static int done_res_texture(int id, void *cls);
    2.18  static void free_res_texture(int id, void *cls);
    2.19  
    2.20 +struct resman *texman;
    2.21 +int dbg_load_async = 1;
    2.22  
    2.23  struct thumbnail *create_thumbs(const char *dirpath)
    2.24  {
    2.25 @@ -29,7 +22,6 @@
    2.26  	struct dirent *dent;
    2.27  	/* allocate dummy head node */
    2.28  	struct thumbnail *list = calloc(1, sizeof *list);
    2.29 -	dbg = list;
    2.30  
    2.31  	if(!texman) {
    2.32  		texman = resman_create();
    2.33 @@ -44,9 +36,7 @@
    2.34  	}
    2.35  
    2.36  	while((dent = readdir(dir))) {
    2.37 -#ifdef DBG_SYNC
    2.38  		struct img_pixmap img;
    2.39 -#endif
    2.40  		struct thumbnail *node;
    2.41  
    2.42  		if(!(node = malloc(sizeof *node))) {
    2.43 @@ -67,32 +57,34 @@
    2.44  
    2.45  		node->aspect = 1.0;
    2.46  
    2.47 -#ifndef DBG_SYNC
    2.48 -		resman_lookup(texman, node->fname, node);
    2.49 -#else
    2.50 -		img_init(&img);
    2.51 -		if(img_load(&img, node->fname) == -1) {
    2.52 +		if(dbg_load_async) {
    2.53 +			resman_lookup(texman, node->fname, node);
    2.54 +		} else {
    2.55 +			img_init(&img);
    2.56 +			if(img_load(&img, node->fname) == -1) {
    2.57 +				img_destroy(&img);
    2.58 +				free(node->fname);
    2.59 +				free(node);
    2.60 +				continue;
    2.61 +			}
    2.62 +
    2.63 +			printf("loaded image: %s\n", node->fname);
    2.64 +
    2.65 +			node->aspect = (float)img.width / (float)img.height;
    2.66 +
    2.67 +			glGenTextures(1, &node->tex);
    2.68 +			glBindTexture(GL_TEXTURE_2D, node->tex);
    2.69 +			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    2.70 +			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    2.71 +			glTexImage2D(GL_TEXTURE_2D, 0, img_glintfmt(&img), img.width, img.height, 0, img_glfmt(&img), img_gltype(&img), img.pixels);
    2.72  			img_destroy(&img);
    2.73 -			free(node->fname);
    2.74 -			free(node);
    2.75 -			continue;
    2.76 +
    2.77 +			node->prev = list;
    2.78 +			node->next = list->next;
    2.79 +			if(list->next) list->next->prev = node;
    2.80 +			list->next = node;
    2.81  		}
    2.82 -
    2.83 -		printf("loaded image: %s\n", node->fname);
    2.84 -
    2.85 -		node->aspect = (float)img.width / (float)img.height;
    2.86 -
    2.87 -		glGenTextures(1, &node->tex);
    2.88 -		glBindTexture(GL_TEXTURE_2D, node->tex);
    2.89 -		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    2.90 -		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    2.91 -		glTexImage2D(GL_TEXTURE_2D, 0, img_glintfmt(&img), img.width, img.height, 0, img_glfmt(&img), img_gltype(&img), img.pixels);
    2.92 -		img_destroy(&img);
    2.93 -#endif
    2.94 -
    2.95 -		node->next = list->next;
    2.96 -		node->prev = list;
    2.97 -		list->next = node;
    2.98 +		node->list = list;
    2.99  	}
   2.100  	closedir(dir);
   2.101  
   2.102 @@ -138,7 +130,6 @@
   2.103  
   2.104  	thumbs = thumbs->next;	/* skip dummy node */
   2.105  	while(thumbs) {
   2.106 -		printf("drawing thumb: %s\n", thumbs->fname);
   2.107  		glPushMatrix();
   2.108  		glTranslatef(x, y, 0);
   2.109  
   2.110 @@ -225,7 +216,7 @@
   2.111  
   2.112  static int done_res_texture(int id, void *cls)
   2.113  {
   2.114 -	struct thumbnail *rdata = resman_get_res_data(texman, id);
   2.115 +	struct thumbnail *thumb = resman_get_res_data(texman, id);
   2.116  	int load_result = resman_get_res_result(texman, id);
   2.117  
   2.118  	if(load_result == -1) {
   2.119 @@ -236,21 +227,27 @@
   2.120  		return -1;
   2.121  	}
   2.122  
   2.123 -	if(resman_get_res_result(texman, id) != 0 || !rdata) {
   2.124 +	if(resman_get_res_result(texman, id) != 0 || !thumb) {
   2.125  		fprintf(stderr, "failed to load resource %d (%s)\n", id, resman_get_res_name(texman, id));
   2.126  	} else {
   2.127  		printf("done loading resource %d (%s)\n", id, resman_get_res_name(texman, id));
   2.128  	}
   2.129  
   2.130 -	if(!rdata->tex) {
   2.131 -		glGenTextures(1, &rdata->tex);
   2.132 +	if(!thumb->tex) {
   2.133 +		glGenTextures(1, &thumb->tex);
   2.134  	}
   2.135 -	glBindTexture(GL_TEXTURE_2D, rdata->tex);
   2.136 +	glBindTexture(GL_TEXTURE_2D, thumb->tex);
   2.137  	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   2.138  	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   2.139 -	glTexImage2D(GL_TEXTURE_2D, 0, img_glintfmt(rdata->img),
   2.140 -			rdata->img->width, rdata->img->height, 0, img_glfmt(rdata->img),
   2.141 -			img_gltype(rdata->img), rdata->img->pixels);
   2.142 +	glTexImage2D(GL_TEXTURE_2D, 0, img_glintfmt(thumb->img),
   2.143 +			thumb->img->width, thumb->img->height, 0, img_glfmt(thumb->img),
   2.144 +			img_gltype(thumb->img), thumb->img->pixels);
   2.145 +
   2.146 +	/* and add it to the list of thumbnails */
   2.147 +	thumb->prev = thumb->list;
   2.148 +	thumb->next = thumb->list->next;
   2.149 +	if(thumb->list->next) thumb->list->next->prev = thumb;
   2.150 +	thumb->list->next = thumb;
   2.151  	return 0;
   2.152  }
   2.153  
     3.1 --- a/examples/imgthumbs/src/thumbs.h	Wed Feb 12 06:54:52 2014 +0200
     3.2 +++ b/examples/imgthumbs/src/thumbs.h	Wed Feb 12 16:02:12 2014 +0200
     3.3 @@ -14,6 +14,7 @@
     3.4  	struct img_pixmap *img;
     3.5  
     3.6  	struct thumbnail *next, *prev;
     3.7 +	struct thumbnail *list;	/* pointer to the list this thumbnail belongs to */
     3.8  };
     3.9  
    3.10  struct thumbnail *create_thumbs(const char *dirpath);
     4.1 --- a/libresman-static.vcproj	Wed Feb 12 06:54:52 2014 +0200
     4.2 +++ b/libresman-static.vcproj	Wed Feb 12 16:02:12 2014 +0200
     4.3 @@ -18,8 +18,8 @@
     4.4  	<Configurations>
     4.5  		<Configuration
     4.6  			Name="Debug|Win32"
     4.7 -			OutputDirectory="$(SolutionDir)$(ConfigurationName)-static"
     4.8 -			IntermediateDirectory="$(ConfigurationName)"
     4.9 +			OutputDirectory="$(SolutionDir)$(ConfigurationName)"
    4.10 +			IntermediateDirectory="$(ConfigurationName)-static"
    4.11  			ConfigurationType="4"
    4.12  			CharacterSet="1"
    4.13  			>
     5.1 --- a/src/resman.c	Wed Feb 12 06:54:52 2014 +0200
     5.2 +++ b/src/resman.c	Wed Feb 12 16:02:12 2014 +0200
     5.3 @@ -166,10 +166,8 @@
     5.4  			continue;
     5.5  		}
     5.6  
     5.7 -		printf("locking mutex %d\n", res->id);
     5.8  		pthread_mutex_lock(&res->done_lock);
     5.9  		if(!res->done_pending) {
    5.10 -			printf("  unlocking mutex %d\n", res->id);
    5.11  			pthread_mutex_unlock(&res->done_lock);
    5.12  			continue;
    5.13  		}
    5.14 @@ -178,12 +176,10 @@
    5.15  		res->done_pending = 0;
    5.16  		if(rman->done_func(i, rman->done_func_cls) == -1) {
    5.17  			/* done-func returned -1, so let's remove the resource */
    5.18 -			printf("  unlocking mutex %d\n", res->id);
    5.19  			pthread_mutex_unlock(&res->done_lock);
    5.20  			remove_resource(rman, i);
    5.21  			continue;
    5.22  		}
    5.23 -		printf("  unlocking mutex %d\n", res->id);
    5.24  		pthread_mutex_unlock(&res->done_lock);
    5.25  	}
    5.26  	return 0;
    5.27 @@ -309,9 +305,7 @@
    5.28  		return;
    5.29  	}
    5.30  
    5.31 -	printf("locking mutex %d\n", res->id);
    5.32  	pthread_mutex_lock(&res->done_lock);
    5.33  	res->done_pending = 1;
    5.34 -	printf("  unlocking mutex %d\n", res->id);
    5.35  	pthread_mutex_unlock(&res->done_lock);
    5.36  }