# core/imagetexturecache (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/imagetexturecache/

Image texture cache.
- The original uses `WeakMap<image, texture>`, so the texture disappears along with the source image when it is collected.
- Here it holds `Weak<Image>`, and sweep() removes textures whose source has disappeared.
- After uploading, the image's CPU pixels are released. (images set to be kept with Image.set_pixels_retained are left alone)
- When released pixels are needed again (Canvas2D drawImage, createPattern), the texture remembered by the image is read back from the GPU. (restore_image_pixels_from_texture)
  This is the same path Chrome takes when it reads back a texture to draw a GPU-resident image onto a CPU canvas. The GL context for reading back is set by Graphic when it is created.

## ImageTextureCache (struct) (Default)

Image texture cache. (one GPU texture per image, Graphic holds one and uploads on first draw)
- Holds images as `Weak`, so sweep deletes the texture when the image is gone. (Graphic calls it every 600 frames)

Example: Empty cache

```rust
use vanilla::core::imagetexturecache::ImageTextureCache;

let image_texture_cache = ImageTextureCache::new();
assert_eq!(image_texture_cache.get_texture_count(), 0);
```

Example: Uploading a texture and cleaning up after the image is gone

```rust
use std::path::Path;
use vanilla::{Graphic, Image};
use vanilla::core::imagetexturecache::ImageTextureCache;

fn upload(graphic: &Graphic, image_texture_cache: &mut ImageTextureCache) -> Result<(), String> {
	let image = Image::load(Path::new("examples/assets/atlas/icons.png"))?;
	let texture = image_texture_cache.get_texture(graphic.get_gl(), &image, true, false);
	assert!(texture.is_some());
	assert!(!image.has_pixels());
	drop(image);
	image_texture_cache.sweep(graphic.get_gl());
	assert_eq!(image_texture_cache.get_texture_count(), 0);
	Ok(())
}
```

- `pub fn new() -> ImageTextureCache` (constructor, since 0.3.2): Constructor.
  - returns ImageTextureCache: Empty cache.
- `pub fn get_texture(&mut self, gl: &GlContext, image: &Rc<Image>, is_smoothing: bool, is_mipmap: bool) -> Option<crate::core::graphic::GlTexture>` (method, since 0.3.2): Returns the texture of an image. (uploads it and releases the CPU pixels if missing, reapplies the filter if the filter settings changed)
  - An image whose pixels were released when another cache uploaded it is read back from that texture and uploaded.
  - If the pixel length is not width x height x 4 (and it cannot be read back either), it is not uploaded and None.
  - `gl` &GlContext: GL context to create the texture with.
  - `image` &Rc<Image>: Image to get the texture for. (releases the CPU pixels on the first upload, keeps them if set_pixels_retained(true))
  - `is_smoothing` bool: true for a LINEAR filter, false for NEAREST.
  - `is_mipmap` bool: true uses a mipmap filter when minifying. (mipmaps are always built on upload)
  - returns Option<crate::core::graphic::GlTexture>: Texture of the image. None if the GL texture cannot be created.
- `pub fn sweep(&mut self, gl: &GlContext)` (method, since 0.3.2): Deletes textures whose original image is gone.
  - `gl` &GlContext: GL context that created the textures.
- `pub fn get_texture_count(&self) -> usize` (method, since 0.3.2): Number of uploaded textures.
  - returns usize: Number of textures in the cache. (counted even if the image is gone, until sweep)
