summaryrefslogtreecommitdiff
path: root/image/image_cache.c
blob: ac367908178782838b48767d9bebbf9f920f7418 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
/*
 * Copyright 2011 Vincent Sanders <vince@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 <inttypes.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#include "utils/log.h"
#include "content/content_protected.h"
#include "desktop/gui_misc.h"
#include "desktop/gui_internal.h"

#include "image/image_cache.h"
#include "image/image.h"

/** Age of an entry within the cache
 *
 * type deffed away so it can be readily changed later perhaps to a
 * wallclock time structure.
 */
typedef unsigned int cache_age;

/** Image cache entry
 */
struct image_cache_entry_s {
	struct image_cache_entry_s *next; /* next cache entry in list */
	struct image_cache_entry_s *prev; /* previous cache entry in list */

	struct content *content; /** content is used as a key */
	struct bitmap *bitmap; /** associated bitmap entry */
	/** Conversion routine */
	image_cache_convert_fn *convert;

	/* Statistics for replacement algorithm */

	unsigned int redraw_count; /**< number of times object has been drawn */
	cache_age redraw_age; /**< Age of last redraw */
	size_t bitmap_size; /**< size if storage occupied by bitmap */
	cache_age bitmap_age; /**< Age of last conversion to a bitmap by cache*/

	int conversion_count; /**< Number of times image has been converted */
};

/** Current state of the cache.
 *
 * Global state of the cache. entries "age" is determined based on a
 * monotonically incrementing operation count. This avoids issues with
 * using wall clock time while allowing the LRU algorithm to work
 * sensibly.
 */
struct image_cache_s {
	/** Cache parameters */
	struct image_cache_parameters params;

	/** The "age" of the current operation */
	cache_age current_age;

	/* The objects the cache holds */
	struct image_cache_entry_s *entries;


	/* Statistics for management algorithm */

	/** total size of bitmaps currently allocated */
	size_t total_bitmap_size;

	/** Total count of bitmaps currently allocated */
	int bitmap_count;

	/** Maximum size of bitmaps allocated at any one time */
	size_t max_bitmap_size;
	/** The number of objects when maximum bitmap usage occoured */
	int max_bitmap_size_count;

	/** Maximum count of bitmaps allocated at any one time */
	int max_bitmap_count;
	/** The size of the bitmaps when the max count occoured */
	size_t max_bitmap_count_size;

	/** Bitmap was not available at plot time required conversion */
	int miss_count;
	uint64_t miss_size;
	/** Bitmap was available at plot time required no conversion */
	int hit_count;
	uint64_t hit_size;
	/** Bitmap was not available at plot time and required
	 * conversion which failed.
	 */
	int fail_count;
	uint64_t fail_size;

	/* Cache entry freed without ever being redrawn */
	int total_unrendered;
	/** Bitmap was available but never required - wasted conversions */
	int specultive_miss_count;

	/** Total number of additional (after the first) conversions */
	int total_extra_conversions;
	/** counts total number of images with more than one conversion */
	int total_extra_conversions_count;

	/** Bitmap with most conversions was converted this many times */
	int peak_conversions;
	/** Size of bitmap with most conversions */
	unsigned int peak_conversions_size;
};

/** image cache state */
static struct image_cache_s *image_cache = NULL;


/** Find the nth cache entry
 */
static struct image_cache_entry_s *image_cache__findn(int entryn)
{
	struct image_cache_entry_s *found;

	found = image_cache->entries;
	while ((found != NULL) && (entryn > 0)) {
		entryn--;
		found = found->next;
	}
	return found;
}

/** Find the cache entry for a content
 */
static struct image_cache_entry_s *image_cache__find(const struct content *c)
{
	struct image_cache_entry_s *found;

	found = image_cache->entries;
	while ((found != NULL) && (found->content != c)) {
		found = found->next;
	}
	return found;
}

static void image_cache_stats_bitmap_add(struct image_cache_entry_s *centry)
{
	centry->bitmap_age = image_cache->current_age;
	centry->conversion_count++;

	image_cache->total_bitmap_size += centry->bitmap_size;
	image_cache->bitmap_count++;

	if (image_cache->total_bitmap_size > image_cache->max_bitmap_size) {
		image_cache->max_bitmap_size = image_cache->total_bitmap_size;
		image_cache->max_bitmap_size_count = image_cache->bitmap_count;

	}

	if (image_cache->bitmap_count > image_cache->max_bitmap_count) {
		image_cache->max_bitmap_count = image_cache->bitmap_count;
		image_cache->max_bitmap_count_size = image_cache->total_bitmap_size;
	}

	if (centry->conversion_count == 2) {
		image_cache->total_extra_conversions_count++;
	}

	if (centry->conversion_count > 1) {
		image_cache->total_extra_conversions++;
	}

	if ((centry->conversion_count > image_cache->peak_conversions) ||
	    (centry->conversion_count == image_cache->peak_conversions &&
	     centry->bitmap_size > image_cache->peak_conversions_size)) {
		image_cache->peak_conversions = centry->conversion_count;
		image_cache->peak_conversions_size = centry->bitmap_size;
	}
}

static void image_cache__link(struct image_cache_entry_s *centry)
{
	centry->next = image_cache->entries;
	centry->prev = NULL;
	if (centry->next != NULL) {
		centry->next->prev = centry;
	}
	image_cache->entries = centry;
}

static void image_cache__unlink(struct image_cache_entry_s *centry)
{
	/* unlink entry */
	if (centry->prev == NULL) {
		/* first in list */
		if (centry->next != NULL) {
			centry->next->prev = centry->prev;
			image_cache->entries = centry->next;
		} else {
			/* empty list */
			image_cache->entries = NULL;
		}
	} else {
		centry->prev->next = centry->next;

		if (centry->next != NULL) {
			centry->next->prev = centry->prev;
		}
	}
}

static void image_cache__free_bitmap(struct image_cache_entry_s *centry)
{
	if (centry->bitmap != NULL) {
#ifdef IMAGE_CACHE_VERBOSE
		LOG(("Freeing bitmap %p size %d age %d redraw count %d",
		     centry->bitmap,
		     centry->bitmap_size,
		     image_cache->current_age - centry->bitmap_age,
		     centry->redraw_count));
#endif
		guit->bitmap->destroy(centry->bitmap);
		centry->bitmap = NULL;
		image_cache->total_bitmap_size -= centry->bitmap_size;
		image_cache->bitmap_count--;
		if (centry->redraw_count == 0) {
			image_cache->specultive_miss_count++;
		}
	}

}

/* free cache entry */
static void image_cache__free_entry(struct image_cache_entry_s *centry)
{
#ifdef IMAGE_CACHE_VERBOSE
	LOG(("freeing %p ", centry));
#endif

	if (centry->redraw_count == 0) {
		image_cache->total_unrendered++;
	}

	image_cache__free_bitmap(centry);

	image_cache__unlink(centry);

	free(centry);
}

/** Cache cleaner */
static void image_cache__clean(struct image_cache_s *icache)
{
	struct image_cache_entry_s *centry = icache->entries;

	while (centry != NULL) {
		if ((icache->current_age - centry->redraw_age) >
		    icache->params.bg_clean_time) {
			/* only consider older entries, avoids active entries */
			if ((icache->total_bitmap_size >
			     (icache->params.limit - icache->params.hysteresis)) &&
			    (rand() > (RAND_MAX / 2))) {
				image_cache__free_bitmap(centry);
			}
		}
		centry=centry->next;
	}
}

/** Cache background scheduled callback. */
static void image_cache__background_update(void *p)
{
	struct image_cache_s *icache = p;

	/* increment current cache age */
	icache->current_age += icache->params.bg_clean_time;

#ifdef IMAGE_CACHE_VERBOSE
	LOG(("Cache age %ds", icache->current_age / 1000));
#endif

	image_cache__clean(icache);

	guit->browser->schedule(icache->params.bg_clean_time,
				image_cache__background_update,
				icache);
}

/* exported interface documented in image_cache.h */
struct bitmap *image_cache_get_bitmap(const struct content *c)
{
	struct image_cache_entry_s *centry;

	centry = image_cache__find(c);
	if (centry == NULL) {
		return NULL;
	}

	if (centry->bitmap == NULL) {
		if (centry->convert != NULL) {
			centry->bitmap = centry->convert(centry->content);
		}

		if (centry->bitmap != NULL) {
			image_cache_stats_bitmap_add(centry);
			image_cache->miss_count++;
			image_cache->miss_size += centry->bitmap_size;
		} else {
			image_cache->fail_count++;
			image_cache->fail_size += centry->bitmap_size;
		}
	} else {
		image_cache->hit_count++;
		image_cache->hit_size += centry->bitmap_size;
	}

	return centry->bitmap;
}

/* exported interface documented in image_cache.h */
bool image_cache_speculate(struct content *c)
{
	bool decision = false;

	/* If the cache is below its target usage and the bitmap is
	 * small enough speculate.
	 */
	if ((image_cache->total_bitmap_size < image_cache->params.limit) &&
	    (c->size <= image_cache->params.speculative_small)) {
#ifdef IMAGE_CACHE_VERBOSE
		LOG(("content size (%d) is smaller than minimum (%d)", c->size, SPECULATE_SMALL));
#endif
		decision = true;
	}

#ifdef IMAGE_CACHE_VERBOSE
	LOG(("returning %d", decision));
#endif
	return decision;
}

/* exported interface documented in image_cache.h */
struct bitmap *image_cache_find_bitmap(struct content *c)
{
	struct image_cache_entry_s *centry;

	centry = image_cache__find(c);
	if (centry == NULL) {
		return NULL;
	}

	return centry->bitmap;
}

/* exported interface documented in image_cache.h */
nserror
image_cache_init(const struct image_cache_parameters *image_cache_parameters)
{
	image_cache = calloc(1, sizeof(struct image_cache_s));
	if (image_cache == NULL) {
		return NSERROR_NOMEM;
	}

	image_cache->params = *image_cache_parameters;

	guit->browser->schedule(image_cache->params.bg_clean_time,
				image_cache__background_update,
				image_cache);

	LOG(("Image cache initilised with a limit of %d hysteresis of %d",
	     image_cache->params.limit, image_cache->params.hysteresis));

	return NSERROR_OK;
}

/* exported interface documented in image_cache.h */
nserror image_cache_fini(void)
{
	unsigned int op_count;

	guit->browser->schedule(-1, image_cache__background_update, image_cache);

	LOG(("Size at finish %d (in %d)",
	     image_cache->total_bitmap_size,
	     image_cache->bitmap_count));

	while (image_cache->entries != NULL) {
		image_cache__free_entry(image_cache->entries);
	}

	op_count = image_cache->hit_count +
		image_cache->miss_count +
		image_cache->fail_count;

	LOG(("Age %ds", image_cache->current_age / 1000));
	LOG(("Peak size %d (in %d)",
	     image_cache->max_bitmap_size,
	     image_cache->max_bitmap_size_count ));
	LOG(("Peak image count %d (size %d)",
	     image_cache->max_bitmap_count,
	     image_cache->max_bitmap_count_size));

	if (op_count > 0) {
		uint64_t op_size;

		op_size = image_cache->hit_size +
			image_cache->miss_size +
			image_cache->fail_size;

		LOG(("Cache total/hit/miss/fail (counts) %d/%d/%d/%d (100%%/%d%%/%d%%/%d%%)",
		     op_count,
		     image_cache->hit_count,
		     image_cache->miss_count,
		     image_cache->fail_count,
		     (image_cache->hit_count * 100) / op_count,
		     (image_cache->miss_count * 100) / op_count,
		     (image_cache->fail_count * 100) / op_count));
		LOG(("Cache total/hit/miss/fail (size) %d/%d/%d/%d (100%%/%d%%/%d%%/%d%%)",
		     op_size,
		     image_cache->hit_size,
		     image_cache->miss_size,
		     image_cache->fail_size,
		     (image_cache->hit_size * 100) / op_size,
		     (image_cache->miss_size * 100) / op_size,
		     (image_cache->fail_size * 100) / op_size));
	}

	LOG(("Total images never rendered: %d (includes %d that were converted)",
	     image_cache->total_unrendered,
	     image_cache->specultive_miss_count));

	LOG(("Total number of excessive conversions: %d (from %d images converted more than once)",
	     image_cache->total_extra_conversions,
	     image_cache->total_extra_conversions_count));

	LOG(("Bitmap of size %d had most (%d) conversions",
	     image_cache->peak_conversions_size,
	     image_cache->peak_conversions));

	free(image_cache);

	return NSERROR_OK;
}

/* exported interface documented in image_cache.h */
nserror image_cache_add(struct content *content,
			struct bitmap *bitmap,
			image_cache_convert_fn *convert)
{
	struct image_cache_entry_s *centry;

	/* bump the cache age by a ms to ensure multiple items are not
	 * added at exactly the same time
	 */
	image_cache->current_age++;

	centry = image_cache__find(content);
	if (centry == NULL) {
		/* new cache entry, content not previously added */
		centry = calloc(1, sizeof(struct image_cache_entry_s));
		if (centry == NULL) {
			return NSERROR_NOMEM;
		}
		image_cache__link(centry);
		centry->content = content;

		centry->bitmap_size = content->width * content->height * 4;
	}

	LOG(("centry %p, content %p, bitmap %p", centry, content, bitmap));

	centry->convert = convert;

	/* set bitmap entry if one is passed, free extant one if present */
	if (bitmap != NULL) {
		if (centry->bitmap != NULL) {
			guit->bitmap->destroy(centry->bitmap);
		} else {
			image_cache_stats_bitmap_add(centry);
		}
		centry->bitmap = bitmap;
	} else {
		/* no bitmap, check to see if we should speculatively convert */
		if ((centry->convert != NULL) &&
		    (image_cache_speculate(content) == true)) {
			centry->bitmap = centry->convert(centry->content);

			if (centry->bitmap != NULL) {
				image_cache_stats_bitmap_add(centry);
			} else {
				image_cache->fail_count++;
			}
		}
	}



	return NSERROR_OK;
}

/* exported interface documented in image_cache.h */
nserror image_cache_remove(struct content *content)
{
	struct image_cache_entry_s *centry;

	/* get the cache entry */
	centry = image_cache__find(content);
	if (centry == NULL) {
		LOG(("Could not find cache entry for content (%p)", content));
		return NSERROR_NOT_FOUND;
	}

	image_cache__free_entry(centry);

	return NSERROR_OK;
}

/* exported interface documented in image_cache.h */
int image_cache_snsummaryf(char *string, size_t size, const char *fmt)
{
	size_t slen = 0; /* current output string length */
	int fmtc = 0; /* current index into format string */
	bool pct;
	unsigned int op_count;
	uint64_t op_size;

	op_count = image_cache->hit_count +
		image_cache->miss_count +
		image_cache->fail_count;

	op_size = image_cache->hit_size +
		image_cache->miss_size +
		image_cache->fail_size;

	while((slen < size) && (fmt[fmtc] != 0)) {
		if (fmt[fmtc] == '%') {
			fmtc++;

			/* check for percentage modifier */
			if (fmt[fmtc] == 'p') {
				fmtc++;
				pct = true;
			} else {
				pct = false;
			}

#define FMTCHR(chr,fmt,var) case chr : \
slen += snprintf(string + slen, size - slen, "%"fmt, image_cache->var); break

#define FMTPCHR(chr,fmt,var,div) \
case chr :					\
	if (pct) {							\
		if (div > 0) {						\
			slen += snprintf(string + slen, size - slen, "%"PRId64, (uint64_t)((image_cache->var * 100) / div)); \
		} else {						\
			slen += snprintf(string + slen, size - slen, "100"); \
		}							\
	} else {							\
		slen += snprintf(string + slen, size - slen, "%"fmt, image_cache->var); \
	} break


			switch (fmt[fmtc]) {
			case '%':
				string[slen] = '%';
				slen++;
				break;

			FMTCHR('a', SSIZET_FMT, params.limit);
			FMTCHR('b', SSIZET_FMT, params.hysteresis);
			FMTCHR('c', SSIZET_FMT, total_bitmap_size);
			FMTCHR('d', "d", bitmap_count);
			FMTCHR('e', "d", current_age / 1000);
			FMTCHR('f', SSIZET_FMT, max_bitmap_size);
			FMTCHR('g', "d", max_bitmap_size_count);
			FMTCHR('h', "d", max_bitmap_count);
			FMTCHR('i', SSIZET_FMT, max_bitmap_count_size);


			case 'j':
				slen += snprintf(string + slen, size - slen,
						 "%d", pct?100:op_count);
				break;

			FMTPCHR('k', "d", hit_count, op_count);
			FMTPCHR('l', "d", miss_count, op_count);
			FMTPCHR('m', "d", fail_count, op_count);

			case 'n':
				slen += snprintf(string + slen, size - slen,
						 "%"PRId64, pct?100:op_size);
				break;

			FMTPCHR('o', PRId64, hit_size, op_size);
			FMTPCHR('q', PRId64, miss_size, op_size);
			FMTPCHR('r', PRId64, fail_size, op_size);

			FMTCHR('s', "d", total_unrendered);
			FMTCHR('t', "d", specultive_miss_count);
			FMTCHR('u', "d", total_extra_conversions);
			FMTCHR('v', "d", total_extra_conversions_count);
			FMTCHR('w', "d", peak_conversions_size);
			FMTCHR('x', "d", peak_conversions);


			}
#undef FMTCHR
#undef FMTPCHR

			fmtc++;
		} else {
			string[slen] = fmt[fmtc];
			slen++;
			fmtc++;
		}
	}

	/* Ensure that we NUL-terminate the output */
	string[min(slen, size - 1)] = '\0';

	return slen;
}

/* exported interface documented in image_cache.h */
int image_cache_snentryf(char *string, size_t size, unsigned int entryn,
		const char *fmt)
{
	struct image_cache_entry_s *centry;
	size_t slen = 0; /* current output string length */
	int fmtc = 0; /* current index into format string */
	lwc_string *origin; /* current entry's origin */

	centry = image_cache__findn(entryn);
	if (centry == NULL)
		return -1;

	while((slen < size) && (fmt[fmtc] != 0)) {
		if (fmt[fmtc] == '%') {
			fmtc++;
			switch (fmt[fmtc]) {
			case 'e':
				slen += snprintf(string + slen, size - slen,
						"%d", entryn);
				break;

			case 'r':
				slen += snprintf(string + slen, size - slen,
						"%u", centry->redraw_count);
				break;

			case 'a':
				slen += snprintf(string + slen, size - slen,
						 "%.2f", (float)((image_cache->current_age -  centry->redraw_age)) / 1000);
				break;


			case 'c':
				slen += snprintf(string + slen, size - slen,
						"%d", centry->conversion_count);
				break;

			case 'g':
				slen += snprintf(string + slen, size - slen,
						"%.2f", (float)((image_cache->current_age -  centry->bitmap_age)) / 1000);
				break;

			case 'k':
				slen += snprintf(string + slen, size - slen,
						"%p", centry->content);
				break;

			case 'U':
				slen += snprintf(string + slen, size - slen,
				    		"%s", nsurl_access(llcache_handle_get_url(centry->content->llcache)));
				break;

			case 'o':
				if (nsurl_has_component(llcache_handle_get_url(
						centry->content->llcache),
						NSURL_HOST)) {
					origin = nsurl_get_component(
							llcache_handle_get_url(
							centry->content->
								llcache),
							NSURL_HOST);
					
					slen += snprintf(string + slen,
							size - slen, "%s",
				    			lwc_string_data(
				    				origin));

					lwc_string_unref(origin);
				} else {
					slen += snprintf(string + slen,
							size - slen, "%s",
				    			"localhost");
				}
				break;
			
			case 's':
				if (centry->bitmap != NULL) {
					slen += snprintf(string + slen,
							 size - slen,
							 "%"SSIZET_FMT,
							 centry->bitmap_size);
				} else {
					slen += snprintf(string + slen,
							 size - slen,
							 "0");
				}
				break;
			}
			fmtc++;
		} else {
			string[slen] = fmt[fmtc];
			slen++;
			fmtc++;
		}
	}

	/* Ensure that we NUL-terminate the output */
	string[min(slen, size - 1)] = '\0';

	return slen;
}


/* exported interface documented in image_cache.h */
bool image_cache_redraw(struct content *c,
			struct content_redraw_data *data,
			const struct rect *clip,
			const struct redraw_context *ctx)
{
	struct image_cache_entry_s *centry;

	/* get the cache entry */
	centry = image_cache__find(c);
	if (centry == NULL) {
		LOG(("Could not find cache entry for content (%p)", c));
		return false;
	}

	if (centry->bitmap == NULL) {
		if (centry->convert != NULL) {
			centry->bitmap = centry->convert(centry->content);
		}

		if (centry->bitmap != NULL) {
			image_cache_stats_bitmap_add(centry);
			image_cache->miss_count++;
			image_cache->miss_size += centry->bitmap_size;
		} else {
			image_cache->fail_count++;
			image_cache->fail_size += centry->bitmap_size;
			return false;
		}
	} else {
		image_cache->hit_count++;
		image_cache->hit_size += centry->bitmap_size;
	}


	/* update statistics */
	centry->redraw_count++;
	centry->redraw_age = image_cache->current_age;

	return image_bitmap_plot(centry->bitmap, data, clip, ctx);
}

void image_cache_destroy(struct content *content)
{
	struct image_cache_entry_s *centry;

	/* get the cache entry */
	centry = image_cache__find(content);
	if (centry == NULL) {
		LOG(("Could not find cache entry for content (%p)", content));
	} else {
		image_cache__free_entry(centry);
	}
}

void *image_cache_get_internal(const struct content *c, void *context)
{
	return image_cache_get_bitmap(c);
}

content_type image_cache_content_type(void)
{
	return CONTENT_IMAGE;
}