1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifdef ENABLE_SHADER_CACHE
25
26 #include <ctype.h>
27 #include <ftw.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <sys/file.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/mman.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <dirent.h>
38 #include <inttypes.h>
39
40 #include "util/compress.h"
41 #include "util/crc32.h"
42 #include "util/u_debug.h"
43 #include "util/rand_xor.h"
44 #include "util/u_atomic.h"
45 #include "util/mesa-sha1.h"
46 #include "util/perf/cpu_trace.h"
47 #include "util/ralloc.h"
48 #include "util/compiler.h"
49
50 #include "disk_cache.h"
51 #include "disk_cache_os.h"
52
53 /* The cache version should be bumped whenever a change is made to the
54 * structure of cache entries or the index. This will give any 3rd party
55 * applications reading the cache entries a chance to adjust to the changes.
56 *
57 * - The cache version is checked internally when reading a cache entry. If we
58 * ever have a mismatch we are in big trouble as this means we had a cache
59 * collision. In case of such an event please check the skys for giant
60 * asteroids and that the entire Mesa team hasn't been eaten by wolves.
61 *
62 * - There is no strict requirement that cache versions be backwards
63 * compatible but effort should be taken to limit disruption where possible.
64 */
65 #define CACHE_VERSION 1
66
67 #define DRV_KEY_CPY(_dst, _src, _src_size) \
68 do { \
69 memcpy(_dst, _src, _src_size); \
70 _dst += _src_size; \
71 } while (0);
72
73 static bool
disk_cache_init_queue(struct disk_cache * cache)74 disk_cache_init_queue(struct disk_cache *cache)
75 {
76 if (util_queue_is_initialized(&cache->cache_queue))
77 return true;
78
79 /* 4 threads were chosen below because just about all modern CPUs currently
80 * available that run Mesa have *at least* 4 cores. For these CPUs allowing
81 * more threads can result in the queue being processed faster, thus
82 * avoiding excessive memory use due to a backlog of cache entrys building
83 * up in the queue. Since we set the UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY
84 * flag this should have little negative impact on low core systems.
85 *
86 * The queue will resize automatically when it's full, so adding new jobs
87 * doesn't stall.
88 */
89 return util_queue_init(&cache->cache_queue, "disk$", 32, 4,
90 UTIL_QUEUE_INIT_RESIZE_IF_FULL |
91 UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY |
92 UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY, NULL);
93 }
94
95 static struct disk_cache *
disk_cache_type_create(const char * gpu_name,const char * driver_id,uint64_t driver_flags,enum disk_cache_type cache_type)96 disk_cache_type_create(const char *gpu_name,
97 const char *driver_id,
98 uint64_t driver_flags,
99 enum disk_cache_type cache_type)
100 {
101 void *local;
102 struct disk_cache *cache = NULL;
103 char *max_size_str;
104 uint64_t max_size;
105
106 uint8_t cache_version = CACHE_VERSION;
107 size_t cv_size = sizeof(cache_version);
108
109 /* A ralloc context for transient data during this invocation. */
110 local = ralloc_context(NULL);
111 if (local == NULL)
112 goto fail;
113
114 cache = rzalloc(NULL, struct disk_cache);
115 if (cache == NULL)
116 goto fail;
117
118 /* Assume failure. */
119 cache->path_init_failed = true;
120 cache->type = DISK_CACHE_NONE;
121
122 if (!disk_cache_enabled())
123 goto path_fail;
124
125 char *path = disk_cache_generate_cache_dir(local, gpu_name, driver_id,
126 cache_type);
127 if (!path)
128 goto path_fail;
129
130 cache->path = ralloc_strdup(cache, path);
131 if (cache->path == NULL)
132 goto path_fail;
133
134 /* Cache tests that want to have a disabled cache compression are using
135 * the "make_check_uncompressed" for the driver_id name. Hence here we
136 * disable disk cache compression when mesa's build tests require it.
137 */
138 if (strcmp(driver_id, "make_check_uncompressed") == 0)
139 cache->compression_disabled = true;
140
141 if (cache_type == DISK_CACHE_SINGLE_FILE) {
142 if (!disk_cache_load_cache_index_foz(local, cache))
143 goto path_fail;
144 } else if (cache_type == DISK_CACHE_DATABASE) {
145 if (!disk_cache_db_load_cache_index(local, cache))
146 goto path_fail;
147 }
148
149 if (!getenv("MESA_SHADER_CACHE_DIR") && !getenv("MESA_GLSL_CACHE_DIR"))
150 disk_cache_touch_cache_user_marker(cache->path);
151
152 cache->type = cache_type;
153
154 cache->stats.enabled = debug_get_bool_option("MESA_SHADER_CACHE_SHOW_STATS",
155 false);
156
157 if (!disk_cache_mmap_cache_index(local, cache, path))
158 goto path_fail;
159
160 max_size = 0;
161
162 max_size_str = getenv("MESA_SHADER_CACHE_MAX_SIZE");
163
164 if (!max_size_str) {
165 max_size_str = getenv("MESA_GLSL_CACHE_MAX_SIZE");
166 if (max_size_str)
167 fprintf(stderr,
168 "*** MESA_GLSL_CACHE_MAX_SIZE is deprecated; "
169 "use MESA_SHADER_CACHE_MAX_SIZE instead ***\n");
170 }
171
172 #ifdef MESA_SHADER_CACHE_MAX_SIZE
173 if( !max_size_str ) {
174 max_size_str = MESA_SHADER_CACHE_MAX_SIZE;
175 }
176 #endif
177
178 if (max_size_str) {
179 char *end;
180 max_size = strtoul(max_size_str, &end, 10);
181 if (end == max_size_str) {
182 max_size = 0;
183 } else {
184 switch (*end) {
185 case 'K':
186 case 'k':
187 max_size *= 1024;
188 break;
189 case 'M':
190 case 'm':
191 max_size *= 1024*1024;
192 break;
193 case '\0':
194 case 'G':
195 case 'g':
196 default:
197 max_size *= 1024*1024*1024;
198 break;
199 }
200 }
201 }
202
203 /* Default to 1GB for maximum cache size. */
204 if (max_size == 0) {
205 max_size = 1024*1024*1024;
206 }
207
208 cache->max_size = max_size;
209
210 if (cache->type == DISK_CACHE_DATABASE)
211 mesa_cache_db_multipart_set_size_limit(&cache->cache_db, cache->max_size);
212
213 if (!disk_cache_init_queue(cache))
214 goto fail;
215
216 cache->path_init_failed = false;
217
218 path_fail:
219
220 cache->driver_keys_blob_size = cv_size;
221
222 /* Create driver id keys */
223 size_t id_size = strlen(driver_id) + 1;
224 size_t gpu_name_size = strlen(gpu_name) + 1;
225 cache->driver_keys_blob_size += id_size;
226 cache->driver_keys_blob_size += gpu_name_size;
227
228 /* We sometimes store entire structs that contains a pointers in the cache,
229 * use pointer size as a key to avoid hard to debug issues.
230 */
231 uint8_t ptr_size = sizeof(void *);
232 size_t ptr_size_size = sizeof(ptr_size);
233 cache->driver_keys_blob_size += ptr_size_size;
234
235 size_t driver_flags_size = sizeof(driver_flags);
236 cache->driver_keys_blob_size += driver_flags_size;
237
238 cache->driver_keys_blob =
239 ralloc_size(cache, cache->driver_keys_blob_size);
240 if (!cache->driver_keys_blob)
241 goto fail;
242
243 uint8_t *drv_key_blob = cache->driver_keys_blob;
244 DRV_KEY_CPY(drv_key_blob, &cache_version, cv_size)
245 DRV_KEY_CPY(drv_key_blob, driver_id, id_size)
246 DRV_KEY_CPY(drv_key_blob, gpu_name, gpu_name_size)
247 DRV_KEY_CPY(drv_key_blob, &ptr_size, ptr_size_size)
248 DRV_KEY_CPY(drv_key_blob, &driver_flags, driver_flags_size)
249
250 /* Seed our rand function */
251 s_rand_xorshift128plus(cache->seed_xorshift128plus, true);
252
253 ralloc_free(local);
254
255 return cache;
256
257 fail:
258 if (cache)
259 ralloc_free(cache);
260 ralloc_free(local);
261
262 return NULL;
263 }
264
265 struct disk_cache *
disk_cache_create(const char * gpu_name,const char * driver_id,uint64_t driver_flags)266 disk_cache_create(const char *gpu_name, const char *driver_id,
267 uint64_t driver_flags)
268 {
269 enum disk_cache_type cache_type;
270 struct disk_cache *cache;
271
272 if (debug_get_bool_option("MESA_DISK_CACHE_SINGLE_FILE", false))
273 cache_type = DISK_CACHE_SINGLE_FILE;
274 else if (debug_get_bool_option("MESA_DISK_CACHE_DATABASE", false))
275 cache_type = DISK_CACHE_DATABASE;
276 else
277 cache_type = DISK_CACHE_MULTI_FILE;
278
279 /* Create main writable cache. */
280 cache = disk_cache_type_create(gpu_name, driver_id, driver_flags,
281 cache_type);
282 if (!cache)
283 return NULL;
284
285 /* If MESA_DISK_CACHE_SINGLE_FILE is unset and MESA_DISK_CACHE_COMBINE_RW_WITH_RO_FOZ
286 * is set, then enable additional Fossilize RO caches together with the RW
287 * cache. At first we will check cache entry presence in the RO caches and
288 * if entry isn't found there, then we'll fall back to the RW cache.
289 */
290 if (cache_type != DISK_CACHE_SINGLE_FILE && !cache->path_init_failed &&
291 debug_get_bool_option("MESA_DISK_CACHE_COMBINE_RW_WITH_RO_FOZ", false)) {
292
293 /* Create read-only cache used for sharing prebuilt shaders.
294 * If cache entry will be found in this cache, then the main cache
295 * will be bypassed.
296 */
297 cache->foz_ro_cache = disk_cache_type_create(gpu_name, driver_id,
298 driver_flags,
299 DISK_CACHE_SINGLE_FILE);
300 }
301
302 return cache;
303 }
304
305 void
disk_cache_destroy(struct disk_cache * cache)306 disk_cache_destroy(struct disk_cache *cache)
307 {
308 if (unlikely(cache && cache->stats.enabled)) {
309 printf("disk shader cache: hits = %u, misses = %u\n",
310 cache->stats.hits,
311 cache->stats.misses);
312 }
313
314 if (cache && util_queue_is_initialized(&cache->cache_queue)) {
315 util_queue_finish(&cache->cache_queue);
316 util_queue_destroy(&cache->cache_queue);
317
318 if (cache->foz_ro_cache)
319 disk_cache_destroy(cache->foz_ro_cache);
320
321 if (cache->type == DISK_CACHE_SINGLE_FILE)
322 foz_destroy(&cache->foz_db);
323
324 if (cache->type == DISK_CACHE_DATABASE)
325 mesa_cache_db_multipart_close(&cache->cache_db);
326
327 disk_cache_destroy_mmap(cache);
328 }
329
330 ralloc_free(cache);
331 }
332
333 void
disk_cache_wait_for_idle(struct disk_cache * cache)334 disk_cache_wait_for_idle(struct disk_cache *cache)
335 {
336 util_queue_finish(&cache->cache_queue);
337 }
338
339 void
disk_cache_remove(struct disk_cache * cache,const cache_key key)340 disk_cache_remove(struct disk_cache *cache, const cache_key key)
341 {
342 if (cache->type == DISK_CACHE_DATABASE) {
343 mesa_cache_db_multipart_entry_remove(&cache->cache_db, key);
344 return;
345 }
346
347 char *filename = disk_cache_get_cache_filename(cache, key);
348 if (filename == NULL) {
349 return;
350 }
351
352 disk_cache_evict_item(cache, filename);
353 }
354
355 static struct disk_cache_put_job *
create_put_job(struct disk_cache * cache,const cache_key key,void * data,size_t size,struct cache_item_metadata * cache_item_metadata,bool take_ownership)356 create_put_job(struct disk_cache *cache, const cache_key key,
357 void *data, size_t size,
358 struct cache_item_metadata *cache_item_metadata,
359 bool take_ownership)
360 {
361 struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *)
362 malloc(sizeof(struct disk_cache_put_job) + (take_ownership ? 0 : size));
363
364 if (dc_job) {
365 dc_job->cache = cache;
366 memcpy(dc_job->key, key, sizeof(cache_key));
367 if (take_ownership) {
368 dc_job->data = data;
369 } else {
370 dc_job->data = dc_job + 1;
371 memcpy(dc_job->data, data, size);
372 }
373 dc_job->size = size;
374
375 /* Copy the cache item metadata */
376 if (cache_item_metadata) {
377 dc_job->cache_item_metadata.type = cache_item_metadata->type;
378 if (cache_item_metadata->type == CACHE_ITEM_TYPE_GLSL) {
379 dc_job->cache_item_metadata.num_keys =
380 cache_item_metadata->num_keys;
381 dc_job->cache_item_metadata.keys = (cache_key *)
382 malloc(cache_item_metadata->num_keys * sizeof(cache_key));
383
384 if (!dc_job->cache_item_metadata.keys)
385 goto fail;
386
387 memcpy(dc_job->cache_item_metadata.keys,
388 cache_item_metadata->keys,
389 sizeof(cache_key) * cache_item_metadata->num_keys);
390 }
391 } else {
392 dc_job->cache_item_metadata.type = CACHE_ITEM_TYPE_UNKNOWN;
393 dc_job->cache_item_metadata.keys = NULL;
394 }
395 }
396
397 return dc_job;
398
399 fail:
400 free(dc_job);
401
402 return NULL;
403 }
404
405 static void
destroy_put_job(void * job,void * gdata,int thread_index)406 destroy_put_job(void *job, void *gdata, int thread_index)
407 {
408 if (job) {
409 struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job;
410 free(dc_job->cache_item_metadata.keys);
411 free(job);
412 }
413 }
414
415 static void
destroy_put_job_nocopy(void * job,void * gdata,int thread_index)416 destroy_put_job_nocopy(void *job, void *gdata, int thread_index)
417 {
418 struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job;
419 free(dc_job->data);
420 destroy_put_job(job, gdata, thread_index);
421 }
422
423 static void
424 blob_put_compressed(struct disk_cache *cache, const cache_key key,
425 const void *data, size_t size);
426
427 static void
cache_put(void * job,void * gdata,int thread_index)428 cache_put(void *job, void *gdata, int thread_index)
429 {
430 assert(job);
431
432 unsigned i = 0;
433 char *filename = NULL;
434 struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job;
435
436 if (dc_job->cache->blob_put_cb) {
437 blob_put_compressed(dc_job->cache, dc_job->key, dc_job->data, dc_job->size);
438 } else if (dc_job->cache->type == DISK_CACHE_SINGLE_FILE) {
439 disk_cache_write_item_to_disk_foz(dc_job);
440 } else if (dc_job->cache->type == DISK_CACHE_DATABASE) {
441 disk_cache_db_write_item_to_disk(dc_job);
442 } else if (dc_job->cache->type == DISK_CACHE_MULTI_FILE) {
443 filename = disk_cache_get_cache_filename(dc_job->cache, dc_job->key);
444 if (filename == NULL)
445 goto done;
446
447 /* If the cache is too large, evict something else first. */
448 while (p_atomic_read_relaxed(&dc_job->cache->size->value) + dc_job->size > dc_job->cache->max_size &&
449 i < 8) {
450 disk_cache_evict_lru_item(dc_job->cache);
451 i++;
452 }
453
454 disk_cache_write_item_to_disk(dc_job, filename);
455
456 done:
457 free(filename);
458 }
459 }
460
461 struct blob_cache_entry {
462 uint32_t uncompressed_size;
463 uint8_t compressed_data[];
464 };
465
466 static void
blob_put_compressed(struct disk_cache * cache,const cache_key key,const void * data,size_t size)467 blob_put_compressed(struct disk_cache *cache, const cache_key key,
468 const void *data, size_t size)
469 {
470 MESA_TRACE_FUNC();
471
472 size_t max_buf = util_compress_max_compressed_len(size);
473 struct blob_cache_entry *entry = malloc(max_buf + sizeof(*entry));
474 if (!entry)
475 goto out;
476
477 entry->uncompressed_size = size;
478
479 size_t compressed_size =
480 util_compress_deflate(data, size, entry->compressed_data, max_buf);
481 if (!compressed_size)
482 goto out;
483
484 unsigned entry_size = compressed_size + sizeof(*entry);
485 // The curly brackets are here to only trace the blob_put_cb call
486 {
487 MESA_TRACE_SCOPE("blob_put");
488 cache->blob_put_cb(key, CACHE_KEY_SIZE, entry, entry_size);
489 }
490
491 out:
492 free(entry);
493 }
494
495 static void *
blob_get_compressed(struct disk_cache * cache,const cache_key key,size_t * size)496 blob_get_compressed(struct disk_cache *cache, const cache_key key,
497 size_t *size)
498 {
499 MESA_TRACE_FUNC();
500
501 /* This is what Android EGL defines as the maxValueSize in egl_cache_t
502 * class implementation.
503 */
504 const signed long max_blob_size = 64 * 1024;
505 struct blob_cache_entry *entry = malloc(max_blob_size);
506 if (!entry)
507 return NULL;
508
509 signed long entry_size;
510 // The curly brackets are here to only trace the blob_get_cb call
511 {
512 MESA_TRACE_SCOPE("blob_get");
513 entry_size = cache->blob_get_cb(key, CACHE_KEY_SIZE, entry, max_blob_size);
514 }
515
516 if (!entry_size) {
517 free(entry);
518 return NULL;
519 }
520
521 void *data = malloc(entry->uncompressed_size);
522 if (!data) {
523 free(entry);
524 return NULL;
525 }
526
527 unsigned compressed_size = entry_size - sizeof(*entry);
528 bool ret = util_compress_inflate(entry->compressed_data, compressed_size,
529 data, entry->uncompressed_size);
530 if (!ret) {
531 free(data);
532 free(entry);
533 return NULL;
534 }
535
536 if (size)
537 *size = entry->uncompressed_size;
538
539 free(entry);
540
541 return data;
542 }
543
544 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)545 disk_cache_put(struct disk_cache *cache, const cache_key key,
546 const void *data, size_t size,
547 struct cache_item_metadata *cache_item_metadata)
548 {
549 if (!util_queue_is_initialized(&cache->cache_queue))
550 return;
551
552 struct disk_cache_put_job *dc_job =
553 create_put_job(cache, key, (void*)data, size, cache_item_metadata, false);
554
555 if (dc_job) {
556 util_queue_fence_init(&dc_job->fence);
557 util_queue_add_job(&cache->cache_queue, dc_job, &dc_job->fence,
558 cache_put, destroy_put_job, dc_job->size);
559 }
560 }
561
562 void
disk_cache_put_nocopy(struct disk_cache * cache,const cache_key key,void * data,size_t size,struct cache_item_metadata * cache_item_metadata)563 disk_cache_put_nocopy(struct disk_cache *cache, const cache_key key,
564 void *data, size_t size,
565 struct cache_item_metadata *cache_item_metadata)
566 {
567 if (!util_queue_is_initialized(&cache->cache_queue)) {
568 free(data);
569 return;
570 }
571
572 struct disk_cache_put_job *dc_job =
573 create_put_job(cache, key, data, size, cache_item_metadata, true);
574
575 if (dc_job) {
576 util_queue_fence_init(&dc_job->fence);
577 util_queue_add_job(&cache->cache_queue, dc_job, &dc_job->fence,
578 cache_put, destroy_put_job_nocopy, dc_job->size);
579 }
580 }
581
582 void *
disk_cache_get(struct disk_cache * cache,const cache_key key,size_t * size)583 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size)
584 {
585 void *buf = NULL;
586
587 if (size)
588 *size = 0;
589
590 if (cache->foz_ro_cache)
591 buf = disk_cache_load_item_foz(cache->foz_ro_cache, key, size);
592
593 if (!buf) {
594 if (cache->blob_get_cb) {
595 buf = blob_get_compressed(cache, key, size);
596 } else if (cache->type == DISK_CACHE_SINGLE_FILE) {
597 buf = disk_cache_load_item_foz(cache, key, size);
598 } else if (cache->type == DISK_CACHE_DATABASE) {
599 buf = disk_cache_db_load_item(cache, key, size);
600 } else if (cache->type == DISK_CACHE_MULTI_FILE) {
601 char *filename = disk_cache_get_cache_filename(cache, key);
602 if (filename)
603 buf = disk_cache_load_item(cache, filename, size);
604 }
605 }
606
607 if (unlikely(cache->stats.enabled)) {
608 if (buf)
609 p_atomic_inc(&cache->stats.hits);
610 else
611 p_atomic_inc(&cache->stats.misses);
612 }
613
614 return buf;
615 }
616
617 void
disk_cache_put_key(struct disk_cache * cache,const cache_key key)618 disk_cache_put_key(struct disk_cache *cache, const cache_key key)
619 {
620 const uint32_t *key_chunk = (const uint32_t *) key;
621 int i = CPU_TO_LE32(*key_chunk) & CACHE_INDEX_KEY_MASK;
622 unsigned char *entry;
623
624 if (cache->blob_put_cb) {
625 cache->blob_put_cb(key, CACHE_KEY_SIZE, key_chunk, sizeof(uint32_t));
626 return;
627 }
628
629 if (cache->path_init_failed)
630 return;
631
632 entry = &cache->stored_keys[i * CACHE_KEY_SIZE];
633
634 memcpy(entry, key, CACHE_KEY_SIZE);
635 }
636
637 /* This function lets us test whether a given key was previously
638 * stored in the cache with disk_cache_put_key(). The implement is
639 * efficient by not using syscalls or hitting the disk. It's not
640 * race-free, but the races are benign. If we race with someone else
641 * calling disk_cache_put_key, then that's just an extra cache miss and an
642 * extra recompile.
643 */
644 bool
disk_cache_has_key(struct disk_cache * cache,const cache_key key)645 disk_cache_has_key(struct disk_cache *cache, const cache_key key)
646 {
647 const uint32_t *key_chunk = (const uint32_t *) key;
648 int i = CPU_TO_LE32(*key_chunk) & CACHE_INDEX_KEY_MASK;
649 unsigned char *entry;
650
651 if (cache->blob_get_cb) {
652 uint32_t blob;
653 return cache->blob_get_cb(key, CACHE_KEY_SIZE, &blob, sizeof(uint32_t));
654 }
655
656 if (cache->path_init_failed)
657 return false;
658
659 entry = &cache->stored_keys[i * CACHE_KEY_SIZE];
660
661 return memcmp(entry, key, CACHE_KEY_SIZE) == 0;
662 }
663
664 void
disk_cache_compute_key(struct disk_cache * cache,const void * data,size_t size,cache_key key)665 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
666 cache_key key)
667 {
668 struct mesa_sha1 ctx;
669
670 _mesa_sha1_init(&ctx);
671 _mesa_sha1_update(&ctx, cache->driver_keys_blob,
672 cache->driver_keys_blob_size);
673 _mesa_sha1_update(&ctx, data, size);
674 _mesa_sha1_final(&ctx, key);
675 }
676
677 void
disk_cache_set_callbacks(struct disk_cache * cache,disk_cache_put_cb put,disk_cache_get_cb get)678 disk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put,
679 disk_cache_get_cb get)
680 {
681 cache->blob_put_cb = put;
682 cache->blob_get_cb = get;
683 disk_cache_init_queue(cache);
684 }
685
686 #endif /* ENABLE_SHADER_CACHE */
687