NetSurf
Data Structures | Macros | Typedefs | Functions | Variables
image_cache.c File Reference

Cache implementation for bitmap images decoded into frontend format. More...

#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "netsurf/inttypes.h"
#include "utils/utils.h"
#include "utils/log.h"
#include "netsurf/misc.h"
#include "netsurf/bitmap.h"
#include "content/llcache.h"
#include "content/content_protected.h"
#include "desktop/gui_internal.h"
#include "image/image_cache.h"
#include "image/image.h"
Include dependency graph for image_cache.c:

Go to the source code of this file.

Data Structures

struct  image_cache_entry_s
 Image cache entry. More...
 
struct  image_cache_s
 Current state of the cache. More...
 

Macros

#define FMTCHR(chr, fmt, var)
 
#define FMTPCHR(chr, fmt, var, div)
 

Typedefs

typedef unsigned int cache_age
 Age of an entry within the cache. More...
 

Functions

static struct image_cache_entry_simage_cache__findn (int entryn)
 Find a cache entry by index. More...
 
static struct image_cache_entry_simage_cache__find (const struct content *c)
 Find the cache entry for a content. More...
 
static void image_cache_stats_bitmap_add (struct image_cache_entry_s *centry)
 Update the image cache statistics with an entry. More...
 
static void image_cache__link (struct image_cache_entry_s *centry)
 
static void image_cache__unlink (struct image_cache_entry_s *centry)
 
static void image_cache__free_bitmap (struct image_cache_entry_s *centry)
 free bitmap from an image cache entry More...
 
static void image_cache__free_entry (struct image_cache_entry_s *centry)
 free image cache entry More...
 
static void image_cache__clean (struct image_cache_s *icache)
 Image cache cleaner. More...
 
static void image_cache__background_update (void *p)
 Cache background scheduled callback. More...
 
struct bitmapimage_cache_get_bitmap (const struct content *c)
 Obtain a bitmap from a content converting from source if neccessary. More...
 
bool image_cache_speculate (struct content *c)
 Decide if a content should be speculatively converted. More...
 
struct bitmapimage_cache_find_bitmap (struct content *c)
 Obtain a bitmap from a content with no conversion. More...
 
nserror image_cache_init (const struct image_cache_parameters *image_cache_parameters)
 Initialise the image cache. More...
 
nserror image_cache_fini (void)
 
nserror image_cache_add (struct content *content, struct bitmap *bitmap, image_cache_convert_fn *convert)
 adds an image content to be cached. More...
 
nserror image_cache_remove (struct content *content)
 
int image_cache_snsummaryf (char *string, size_t size, const char *fmt)
 Fill a buffer with information about the image cache using a format. More...
 
int image_cache_snentryf (char *string, size_t size, unsigned int entryn, const char *fmt)
 Fill a buffer with information about a cache entry using a format. More...
 
bool image_cache_redraw (struct content *c, struct content_redraw_data *data, const struct rect *clip, const struct redraw_context *ctx)
 Generic content redraw callback. More...
 
void image_cache_destroy (struct content *content)
 
void * image_cache_get_internal (const struct content *c, void *context)
 
bool image_cache_is_opaque (struct content *c)
 
content_type image_cache_content_type (void)
 

Variables

static struct image_cache_simage_cache = NULL
 image cache state More...
 

Detailed Description

Cache implementation for bitmap images decoded into frontend format.

Definition in file image_cache.c.

Macro Definition Documentation

◆ FMTCHR

#define FMTCHR (   chr,
  fmt,
  var 
)
Value:
case chr : \
slen += snprintf(string + slen, size - slen, "%"fmt, image_cache->var); break
static struct image_cache_s * image_cache
image cache state
Definition: image_cache.c:140

◆ FMTPCHR

#define FMTPCHR (   chr,
  fmt,
  var,
  div 
)
Value:
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
#define PRId64
Definition: inttypes.h:34

Typedef Documentation

◆ cache_age

typedef unsigned int cache_age

Age of an entry within the cache.

type deffed away so it can be readily changed later perhaps to a wallclock time structure.

Definition at line 48 of file image_cache.c.

Function Documentation

◆ image_cache__background_update()

static void image_cache__background_update ( void *  p)
static

Cache background scheduled callback.

Parameters
pThe image cache context.

Definition at line 327 of file image_cache.c.

References image_cache_parameters::bg_clean_time, image_cache_s::current_age, guit, image_cache__background_update(), image_cache__clean(), netsurf_table::misc, NSLOG, image_cache_s::params, and gui_misc_table::schedule.

Referenced by image_cache__background_update(), image_cache_fini(), and image_cache_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ image_cache__clean()

static void image_cache__clean ( struct image_cache_s icache)
static

Image cache cleaner.

Parameters
icacheThe image cache context.

Definition at line 304 of file image_cache.c.

References image_cache_parameters::bg_clean_time, image_cache_s::current_age, image_cache_s::entries, image_cache_parameters::hysteresis, image_cache__free_bitmap(), image_cache_parameters::limit, image_cache_entry_s::next, image_cache_s::params, image_cache_entry_s::redraw_age, and image_cache_s::total_bitmap_size.

Referenced by image_cache__background_update().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ image_cache__find()

static struct image_cache_entry_s * image_cache__find ( const struct content c)
static

Find the cache entry for a content.

Parameters
cThe content to get an entry for
Returns
The image cache entry or NULL if not found.

Definition at line 168 of file image_cache.c.

References image_cache_entry_s::content, image_cache_s::entries, image_cache, and image_cache_entry_s::next.

Referenced by image_cache_add(), image_cache_destroy(), image_cache_find_bitmap(), image_cache_get_bitmap(), image_cache_redraw(), and image_cache_remove().

Here is the caller graph for this function:

◆ image_cache__findn()

static struct image_cache_entry_s * image_cache__findn ( int  entryn)
static

Find a cache entry by index.

Parameters
entrynindex of cache entry
Returns
cache entry at index or NULL if not found.

Definition at line 149 of file image_cache.c.

References image_cache_s::entries, image_cache, and image_cache_entry_s::next.

Referenced by image_cache_snentryf().

Here is the caller graph for this function:

◆ image_cache__free_bitmap()

static void image_cache__free_bitmap ( struct image_cache_entry_s centry)
static

free bitmap from an image cache entry

Parameters
centryThe image cache entry to free bitmap from.

Definition at line 255 of file image_cache.c.

References image_cache_entry_s::bitmap, netsurf_table::bitmap, image_cache_entry_s::bitmap_age, image_cache_s::bitmap_count, image_cache_entry_s::bitmap_size, image_cache_s::current_age, gui_bitmap_table::destroy, guit, image_cache, NSLOG, image_cache_entry_s::redraw_count, image_cache_s::specultive_miss_count, and image_cache_s::total_bitmap_size.

Referenced by image_cache__clean(), and image_cache__free_entry().

Here is the caller graph for this function:

◆ image_cache__free_entry()

static void image_cache__free_entry ( struct image_cache_entry_s centry)
static

free image cache entry

Parameters
centryThe image cache entry to free.

Definition at line 282 of file image_cache.c.

References image_cache, image_cache__free_bitmap(), image_cache__unlink(), NSLOG, image_cache_entry_s::redraw_count, and image_cache_s::total_unrendered.

Referenced by image_cache_destroy(), image_cache_fini(), and image_cache_remove().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ image_cache__link()

static void image_cache__link ( struct image_cache_entry_s centry)
static

Definition at line 219 of file image_cache.c.

References image_cache_s::entries, image_cache, image_cache_entry_s::next, and image_cache_entry_s::prev.

Referenced by image_cache_add().

Here is the caller graph for this function:

◆ image_cache__unlink()

static void image_cache__unlink ( struct image_cache_entry_s centry)
static

Definition at line 229 of file image_cache.c.

References image_cache_s::entries, image_cache, image_cache_entry_s::next, and image_cache_entry_s::prev.

Referenced by image_cache__free_entry().

Here is the caller graph for this function:

◆ image_cache_add()

nserror image_cache_add ( struct content content,
struct bitmap bitmap,
image_cache_convert_fn convert 
)

adds an image content to be cached.

Parameters
contentThe content handle used as a key
bitmapA bitmap representing the already converted content or NULL.
convertA function pointer to convert the content into a bitmap or NULL.
Returns
A netsurf error code.

Definition at line 510 of file image_cache.c.

References image_cache_entry_s::bitmap, netsurf_table::bitmap, bitmap(), image_cache_entry_s::bitmap_size, image_cache_entry_s::content, image_cache_entry_s::convert, image_cache_s::current_age, gui_bitmap_table::destroy, image_cache_s::fail_count, guit, content::height, image_cache, image_cache__find(), image_cache__link(), image_cache_speculate(), image_cache_stats_bitmap_add(), NSERROR_NOMEM, NSERROR_OK, NSLOG, and content::width.

Referenced by nsjpeg_convert(), nsjpegxl_convert(), nspng_convert(), rsvg_convert(), and webp_convert().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ image_cache_content_type()

content_type image_cache_content_type ( void  )

Definition at line 873 of file image_cache.c.

References CONTENT_IMAGE.

◆ image_cache_destroy()

void image_cache_destroy ( struct content content)

Definition at line 841 of file image_cache.c.

References image_cache__find(), image_cache__free_entry(), and NSLOG.

Referenced by rsvg_destroy().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ image_cache_find_bitmap()

struct bitmap * image_cache_find_bitmap ( struct content c)

Obtain a bitmap from a content with no conversion.

Definition at line 402 of file image_cache.c.

References image_cache_entry_s::bitmap, and image_cache__find().

Here is the call graph for this function:

◆ image_cache_fini()

nserror image_cache_fini ( void  )

◆ image_cache_get_bitmap()

struct bitmap * image_cache_get_bitmap ( const struct content c)

Obtain a bitmap from a content converting from source if neccessary.

Definition at line 346 of file image_cache.c.

References image_cache_entry_s::bitmap, image_cache_entry_s::bitmap_size, image_cache_entry_s::content, image_cache_entry_s::convert, image_cache_s::fail_count, image_cache_s::fail_size, image_cache_s::hit_count, image_cache_s::hit_size, image_cache, image_cache__find(), image_cache_stats_bitmap_add(), image_cache_s::miss_count, and image_cache_s::miss_size.

Referenced by image_cache_get_internal(), and image_cache_is_opaque().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ image_cache_get_internal()

void * image_cache_get_internal ( const struct content c,
void *  context 
)

Definition at line 856 of file image_cache.c.

References image_cache_get_bitmap().

Here is the call graph for this function:

◆ image_cache_init()

nserror image_cache_init ( const struct image_cache_parameters image_cache_parameters)

Initialise the image cache.

Parameters
image_cache_parametersThe control parameters for the image cache

Definition at line 416 of file image_cache.c.

References image_cache_parameters::bg_clean_time, guit, image_cache_parameters::hysteresis, image_cache, image_cache__background_update(), image_cache_parameters::limit, netsurf_table::misc, NSERROR_NOMEM, NSERROR_OK, NSLOG, image_cache_s::params, PRIsizet, and gui_misc_table::schedule.

Referenced by netsurf_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ image_cache_is_opaque()

bool image_cache_is_opaque ( struct content c)

Definition at line 862 of file image_cache.c.

References netsurf_table::bitmap, gui_bitmap_table::get_opaque, guit, and image_cache_get_bitmap().

Here is the call graph for this function:

◆ image_cache_redraw()

bool image_cache_redraw ( struct content c,
struct content_redraw_data data,
const struct rect clip,
const struct redraw_context ctx 
)

Generic content redraw callback.

May be used by image content handlers as their redraw callback. Performs all neccissary cache lookups and conversions and calls the bitmap plot function in the redraw context.

Definition at line 798 of file image_cache.c.

References image_cache_entry_s::bitmap, image_cache_entry_s::bitmap_size, clip(), image_cache_entry_s::content, image_cache_entry_s::convert, image_cache_s::current_age, image_cache_s::fail_count, image_cache_s::fail_size, image_cache_s::hit_count, image_cache_s::hit_size, image_bitmap_plot(), image_cache, image_cache__find(), image_cache_stats_bitmap_add(), image_cache_s::miss_count, image_cache_s::miss_size, NSLOG, image_cache_entry_s::redraw_age, and image_cache_entry_s::redraw_count.

Here is the call graph for this function:

◆ image_cache_remove()

nserror image_cache_remove ( struct content content)

Definition at line 567 of file image_cache.c.

References image_cache__find(), image_cache__free_entry(), NSERROR_NOT_FOUND, NSERROR_OK, and NSLOG.

Here is the call graph for this function:

◆ image_cache_snentryf()

int image_cache_snentryf ( char *  string,
size_t  size,
unsigned int  entryn,
const char *  fmt 
)

Fill a buffer with information about a cache entry using a format.

The format string is copied into the output buffer with the following replaced: e - The entry number k - The content key r - The number of redraws of this bitmap c - The number of times this bitmap has been converted s - The size of the current bitmap allocation

Parameters
stringThe buffer in which to place the results.
sizeThe size of the string buffer.
entrynThe opaque entry number.
fmtThe format string.
Returns
The number of bytes written to string or -1 on error

Definition at line 692 of file image_cache.c.

References image_cache_entry_s::bitmap, image_cache_entry_s::bitmap_age, image_cache_entry_s::bitmap_size, image_cache_entry_s::content, image_cache_entry_s::conversion_count, image_cache_s::current_age, image_cache, image_cache__findn(), content::llcache, llcache, llcache_handle_get_url(), min, nsurl_access(), nsurl_get_component(), nsurl_has_component(), NSURL_HOST, PRIsizet, image_cache_entry_s::redraw_age, and image_cache_entry_s::redraw_count.

Here is the call graph for this function:

◆ image_cache_snsummaryf()

int image_cache_snsummaryf ( char *  string,
size_t  size,
const char *  fmt 
)

Fill a buffer with information about the image cache using a format.

The format string is copied into the output buffer with the following replaced:

a Configured cache limit size b Configured cache hysteresis size c Current caches total consumed size d Number of images currently in the cache e The age of the cache f The largest amount of space the cache has occupied since initialisation g The number of objetcs when the cache was at its largest h The largest number of images in the cache since initialisation i The size of the cache when the largest number of objects occoured j The total number of read operations performed on the cache k The total number of read operations satisfied from the cache without conversion. l The total number of read operations satisfied from the cache which required a conversion. m The total number of read operations which could not be sucessfully returned. ie. not available in cache and conversion failed. n The total size of read operations performed on the cache o The total size of read operations satisfied from the cache without conversion. q The total size of read operations satisfied from the cache which required a conversion. r The total size of read operations which could not be sucessfully returned. ie. not available in cache and conversion failed. s The number of images which were placed in the cache but never read. t The number of images that were converted on insertion into the cache which were subsequently never used. u The number of times an image was converted after the first v The number of images that had extra conversions performed. w Size of the image that was converted (read missed cache) highest number of times. x The number of times the image that was converted (read missed cache) highest number of times.

format modifiers: A p before the value modifies the replacement to be a percentage.

Parameters
stringThe buffer in which to place the results.
sizeThe size of the string buffer.
fmtThe format string.
Returns
The number of bytes written to string or -1 on error

Definition at line 585 of file image_cache.c.

References image_cache_s::fail_count, image_cache_s::fail_size, FMTCHR, FMTPCHR, image_cache_s::hit_count, image_cache_s::hit_size, image_cache, min, image_cache_s::miss_count, image_cache_s::miss_size, PRId64, and PRIsizet.

◆ image_cache_speculate()

bool image_cache_speculate ( struct content c)

Decide if a content should be speculatively converted.

This allows for image content handlers to ask the cache if a bitmap should be generated before it is added to the cache. This is the same decision logic used to decide to perform an immediate conversion when a content is initially added to the cache.

Parameters
cThe content to be considered.
Returns
true if a speculative conversion is desired false otherwise.

Definition at line 377 of file image_cache.c.

References image_cache, image_cache_parameters::limit, NSLOG, image_cache_s::params, content::size, SPECULATE_SMALL, image_cache_parameters::speculative_small, and image_cache_s::total_bitmap_size.

Referenced by image_cache_add(), and info_callback().

Here is the caller graph for this function:

◆ image_cache_stats_bitmap_add()

static void image_cache_stats_bitmap_add ( struct image_cache_entry_s centry)
static

Variable Documentation

◆ image_cache

struct image_cache_s* image_cache = NULL
static