summaryrefslogtreecommitdiff
path: root/image/image.c
blob: d65ae8a6797c280d0f65af5b8987d8c849dcdc95 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright 2011 John-Mark Bell <jmb@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf 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 Foundation; version 2 of the License.
 *
 * NetSurf 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#include "utils/errors.h"

#include "image/image.h"
#include "image/image_cache.h"
#include "image/bmp.h"
#include "image/gif.h"
#include "image/ico.h"
#include "image/jpeg.h"
#include "image/mng.h"
#include "image/nssprite.h"
#include "image/png.h"
#include "image/rsvg.h"
#include "image/svg.h"
#include "image/webp.h"

#include "utils/config.h"

/** low water mark for speculative pre-conversion */

/* Experimenting by visiting every page from default page in order and
 * then netsurf homepage
 *
 * 0    : Cache hit/miss/speculative miss/fail 604/147/  0/0 (80%/19%/ 0%/ 0%)
 * 2048 : Cache hit/miss/speculative miss/fail 622/119/ 17/0 (82%/15%/ 2%/ 0%)
 * 4096 : Cache hit/miss/speculative miss/fail 656/109/ 25/0 (83%/13%/ 3%/ 0%)
 * 8192 : Cache hit/miss/speculative miss/fail 648/104/ 40/0 (81%/13%/ 5%/ 0%)
 * ALL  : Cache hit/miss/speculative miss/fail 775/  0/161/0 (82%/ 0%/17%/ 0%)
*/
#define SPECULATE_SMALL 4096

/* the time between cache clean runs in ms */
#define CACHE_CLEAN_TIME (10 * 1000)

/**
 * Initialise image content handlers
 *
 * \return NSERROR_OK on success, appropriate error otherwise.
 */
nserror image_init(void)
{
	nserror error;
	struct image_cache_parameters image_cache_parameters = {
		.bg_clean_time = CACHE_CLEAN_TIME,
		.limit = (8 * 1024 * 1024),
		.hysteresis = (2 * 1024 * 1024),
		.speculative_small = SPECULATE_SMALL
	};

	error = image_cache_init(&image_cache_parameters);
	if (error != NSERROR_OK)
		return error;

#ifdef WITH_BMP
	error = nsbmp_init();
	if (error != NSERROR_OK)
		return error;
#endif

#ifdef WITH_GIF
	error = nsgif_init();
	if (error != NSERROR_OK)
		return error;
#endif

#ifdef WITH_BMP
	error = nsico_init();
	if (error != NSERROR_OK)
		return error;
#endif

#ifdef WITH_JPEG
	error = nsjpeg_init();
	if (error != NSERROR_OK)
		return error;
#endif

#ifdef WITH_MNG
	error = nsmng_init();
	if (error != NSERROR_OK)
		return error;

	error = nsjpng_init();
	if (error != NSERROR_OK)
		return error;
#endif

#ifdef WITH_PNG
	/* Prefer libpng over libmng for pngs by registering later */
	error = nspng_init();
	if (error != NSERROR_OK)
		return error;
#endif

#ifdef WITH_NSSPRITE
	error = nssprite_init();
	if (error != NSERROR_OK)
		return error;
#endif

	/* Prefer rsvg over libsvgtiny for svgs */
#ifdef WITH_NS_SVG
	error = svg_init();
	if (error != NSERROR_OK)
		return error;
#endif
#ifdef WITH_RSVG
	error = nsrsvg_init();
	if (error != NSERROR_OK)
		return error;
#endif

#ifdef WITH_WEBP
	error = webp_init();
	if (error != NSERROR_OK)
		return error;
#endif /* WITH_WEBP */

	return NSERROR_OK;
}

/**
 * Finalise image content handlers
 */
void image_fini(void)
{
#ifdef WITH_BMP
	nsbmp_fini();
#endif

#ifdef WITH_GIF
	nsgif_fini();
#endif

#ifdef WITH_BMP
	nsico_fini();
#endif

#ifdef WITH_JPEG
	nsjpeg_fini();
#endif

#ifdef WITH_MNG
	nsmng_fini();
	nsjpng_fini();
#endif

#ifdef WITH_NSSPRITE
	nssprite_fini();
#endif

#ifdef WITH_PNG
	nspng_fini();
#endif

#ifdef WITH_RSVG
	nsrsvg_fini();
#endif

#ifdef WITH_NS_SVG
	svg_fini();
#endif

#ifdef WITH_WEBP
	webp_fini();
#endif 

	/* dump any remaining cache entries */
	image_cache_fini();
}