#ifndef DISK_CACHE_H
#define DISK_CACHE_H
#ifdef HAVE_DLFCN_H
#include <dlfcn.h>
#include <stdio.h>
#include "util/build_id.h"
#endif
#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/stat.h>
#include "util/mesa-sha1.h"
#ifdef __cplusplus
extern "C" {
#endif
#define CACHE_KEY_SIZE 20
#define CACHE_DIR_NAME "mesa_shader_cache"
typedef uint8_t cache_key[CACHE_KEY_SIZE];
#define CACHE_ITEM_TYPE_UNKNOWN 0x0
#define CACHE_ITEM_TYPE_GLSL 0x1
typedef void
(*disk_cache_put_cb) (const void *key, signed long keySize,
const void *value, signed long valueSize);
typedef signed long
(*disk_cache_get_cb) (const void *key, signed long keySize,
void *value, signed long valueSize);
struct cache_item_metadata {
uint32_t type;
cache_key *keys;
uint32_t num_keys;
};
struct disk_cache;
static inline char *
disk_cache_format_hex_id(char *buf, const uint8_t *hex_id, unsigned size)
{
static const char hex_digits[] = "0123456789abcdef";
unsigned i;
for (i = 0; i < size; i += 2) {
buf[i] = hex_digits[hex_id[i >> 1] >> 4];
buf[i + 1] = hex_digits[hex_id[i >> 1] & 0x0f];
}
buf[i] = '\0';
return buf;
}
#ifdef HAVE_DLADDR
static inline bool
disk_cache_get_function_timestamp(void *ptr, uint32_t* timestamp)
{
Dl_info info;
struct stat st;
if (!dladdr(ptr, &info) || !info.dli_fname) {
return false;
}
if (stat(info.dli_fname, &st)) {
return false;
}
if (!st.st_mtime) {
fprintf(stderr, "Mesa: The provided filesystem timestamp for the cache "
"is bogus! Disabling On-disk cache.\n");
return false;
}
*timestamp = st.st_mtime;
return true;
}
static inline bool
disk_cache_get_function_identifier(void *ptr, struct mesa_sha1 *ctx)
{
uint32_t timestamp;
#ifdef HAVE_DL_ITERATE_PHDR
const struct build_id_note *note = NULL;
if ((note = build_id_find_nhdr_for_addr(ptr))) {
_mesa_sha1_update(ctx, build_id_data(note), build_id_length(note));
} else
#endif
if (disk_cache_get_function_timestamp(ptr, ×tamp)) {
_mesa_sha1_update(ctx, ×tamp, sizeof(timestamp));
} else
return false;
return true;
}
#endif
#ifdef ENABLE_SHADER_CACHE
struct disk_cache *
disk_cache_create(const char *gpu_name, const char *timestamp,
uint64_t driver_flags);
void
disk_cache_destroy(struct disk_cache *cache);
void
disk_cache_wait_for_idle(struct disk_cache *cache);
void
disk_cache_remove(struct disk_cache *cache, const cache_key key);
void
disk_cache_put(struct disk_cache *cache, const cache_key key,
const void *data, size_t size,
struct cache_item_metadata *cache_item_metadata);
void *
disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size);
void
disk_cache_put_key(struct disk_cache *cache, const cache_key key);
bool
disk_cache_has_key(struct disk_cache *cache, const cache_key key);
void
disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
cache_key key);
void
disk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put,
disk_cache_get_cb get);
#else
static inline struct disk_cache *
disk_cache_create(const char *gpu_name, const char *timestamp,
uint64_t driver_flags)
{
return NULL;
}
static inline void
disk_cache_destroy(struct disk_cache *cache) {
return;
}
static inline void
disk_cache_put(struct disk_cache *cache, const cache_key key,
const void *data, size_t size,
struct cache_item_metadata *cache_item_metadata)
{
return;
}
static inline void
disk_cache_remove(struct disk_cache *cache, const cache_key key)
{
return;
}
static inline uint8_t *
disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size)
{
return NULL;
}
static inline void
disk_cache_put_key(struct disk_cache *cache, const cache_key key)
{
return;
}
static inline bool
disk_cache_has_key(struct disk_cache *cache, const cache_key key)
{
return false;
}
static inline void
disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
const cache_key key)
{
return;
}
static inline void
disk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put,
disk_cache_get_cb get)
{
return;
}
#endif
#ifdef __cplusplus
}
#endif
#endif