getTexture
Will get the texture from cache or create it if it doesn't already exist
Only valid between Window.begin and Window.end
Parameters
- self:ImageSource
- ImageSource
Source
Implementation
pub fn getTexture(self: ImageSource) !Texture {
const key = self.hash();
const invalidate = switch (self) {
.imageFile => |f| f.invalidation,
.pixels => |px| px.invalidation,
.pixelsPMA => |px| px.invalidation,
// return texture directly
.texture => |tex| return tex,
};
if (dvui.textureGetCached(key)) |cached_texture| {
// if invalidate = always, we update the texture using updateImageSource for efficency, otherwise return the cached Texture
if (invalidate == .always) {
var tex_mut = cached_texture;
try tex_mut.updateImageSource(self);
if (cached_texture.ptr != tex_mut.ptr) {
// updateImageSource created a new texture, called
// destroyLater on old one, remove before adding new one
_ = dvui.currentWindow().texture_cache.remove(key);
dvui.textureAddToCache(key, tex_mut);
}
return tex_mut;
} else return cached_texture;
} else {
// cache was empty we create a new Texture
const new_texture = try Texture.fromImageSource(self);
dvui.textureAddToCache(key, new_texture);
return new_texture;
}
}