summaryrefslogtreecommitdiff
path: root/imgcache.c
blob: 21db09305c234d8334a8bd95782f05fa61648118 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* imgcache.c
 * Image cache
 * (c) 2002 Mikulas Patocka
 * This file is a part of the Links program, released under GPL.
 */

#include "cfg.h"

#ifdef G

#include "links.h"

static struct list_head image_cache = { &image_cache, &image_cache };

/* xyw_meaning either MEANING_DIMS or MEANING_AUTOSCALE. */
struct cached_image *find_cached_image(int bg, unsigned char *url, int xw, int
		yw, int xyw_meaning, int scale, int aspect)
{
	struct cached_image *i;
	if (xw>=0&&yw>=0&&xyw_meaning==MEANING_DIMS){
		/* The xw and yw is already scaled so that scale and
		 * aspect don't matter.
		 */
		foreach (i, image_cache) {
			if (i->background_color == bg
				&& !strcmp(i->url, url)
				&& i->wanted_xw==xw
				&& i->wanted_yw==yw
				&& i->wanted_xyw_meaning==xyw_meaning
				) goto hit;
		}
	}else{
		foreach (i, image_cache) {
			if (i->background_color == bg
				&& !strcmp(i->url, url)
				&& i->wanted_xw==xw
				&& i->wanted_yw==yw
				&& i->wanted_xyw_meaning==xyw_meaning 
				&& i->scale==scale
				&& i->aspect==aspect) goto hit;
		}
	}
	return NULL;

hit:		
	i->refcount++;
	del_from_list(i);
	add_to_list(image_cache, i);
	return i;
}

void add_image_to_cache(struct cached_image *ci)
{
	add_to_list(image_cache, ci);
}

static unsigned long image_size(struct cached_image *cimg)
{
	unsigned long siz = sizeof(struct cached_image);
	switch(cimg->state){
		case 0:
		case 1:
		case 2:
		case 3:
		case 8:
		case 9:
		case 10:
		case 11:			
		break;

		case 12:
		case 14:
		siz+=(unsigned long)cimg->width*cimg->height*cimg->buffer_bytes_per_pixel;
		if (cimg->bmp.user){
			case 13:
			case 15:	
			siz+=(unsigned long)cimg->bmp.x*cimg->bmp.y*(drv->depth&7);
		}
		break;

#ifdef DEBUG
		default:
		fprintf(stderr,"cimg->state=%d\n",cimg->state);
		internal("Invalid cimg->state in image_size\n");
		break;
#endif /* #ifdef DEBUG */
	}
	return siz;
}

static int shrink_image_cache(int u)
{
	struct cached_image *i;
	longlong si = 0;
	int r = 0;
	foreach(i, image_cache) if (!i->refcount) si += image_size(i);
	while ((si >= image_cache_size || u == SH_FREE_ALL || u == SH_FREE_SOMETHING) && !list_empty(image_cache)) {
		i = image_cache.prev;
		while (i->refcount) {
			i = i->prev;
			if ((void *)i == &image_cache) goto no;
		}
		r |= ST_SOMETHING_FREED;
		si -= image_size(i);
		del_from_list(i);
		img_destruct_cached_image(i);
		if (u == SH_FREE_SOMETHING) break;
	}
	no:
	return r | (list_empty(image_cache) ? ST_CACHE_EMPTY : 0);
}

unsigned long imgcache_info(int type)
{
	struct cached_image *i;
	unsigned long n = 0;
	foreach(i, image_cache) {
		switch (type) {
			case CI_BYTES:
				n += image_size(i);
				break;
			case CI_LOCKED:
				if (!i->refcount) break;
				/* fall through */
			case CI_FILES:
				n++;
				break;
			default:
				internal("imgcache_info: query %d", type);
		}
	}
	return n;
}

void init_imgcache(void)
{
	register_cache_upcall(shrink_image_cache, "imgcache");
}

#endif