hash
Pass the return value of this to dvui.textureInvalidate to
remove the texture from the cache.
When providing a texture directly with ImageSource.texture,
this function will always return 0 as it doesn't interact with
the texture cache.
Parameters
- self:ImageSource
- ImageSource
Source
Implementation
pub fn hash(self: ImageSource) u64 {
// short circuit for texture
switch (self) {
.texture => return 0,
else => {},
}
var h = dvui.fnv.init();
// .always hashes ptr (for uniqueness) and image dimensions so we can update the texture if dimensions stay the same
const img_dimensions = self.size() catch Size{ .w = 0, .h = 0 };
var dim: [2]u32 = .{ @trunc(img_dimensions.w), @trunc(img_dimensions.h) };
const img_dim_bytes = std.mem.asBytes(&dim); // hashing u32 here instead of float because of unstable bit representation in floating point numbers
switch (self) {
.imageFile => |file| {
switch (file.invalidation) {
.ptr => h.update(std.mem.asBytes(&file.bytes.ptr)),
.bytes => h.update(file.bytes),
.always => {
h.update(std.mem.asBytes(&file.bytes.ptr));
h.update(img_dim_bytes);
},
}
h.update(std.mem.asBytes(&@intFromEnum(file.interpolation)));
},
.pixelsPMA => |pixels| {
switch (pixels.invalidation) {
.ptr, .always => h.update(std.mem.asBytes(&pixels.rgba.ptr)),
.bytes => h.update(@ptrCast(pixels.rgba)),
}
h.update(std.mem.asBytes(&@intFromEnum(pixels.interpolation)));
h.update(img_dim_bytes);
},
.pixels => |pixels| {
switch (pixels.invalidation) {
.ptr, .always => h.update(std.mem.asBytes(&pixels.rgba.ptr)),
.bytes => h.update(std.mem.sliceAsBytes(pixels.rgba)),
}
h.update(std.mem.asBytes(&@intFromEnum(pixels.interpolation)));
h.update(img_dim_bytes);
},
.texture => unreachable,
}
return h.final();
}