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 cache->type = cache_type;
150
151 cache->stats.enabled = debug_get_bool_option("MESA_SHADER_CACHE_SHOW_STATS",
152 false);
153
154 if (!disk_cache_mmap_cache_index(local, cache, path))
155 goto path_fail;
156
157 max_size = 0;
158
159 max_size_str = getenv("MESA_SHADER_CACHE_MAX_SIZE");
160
161 if (!max_size_str) {
162 max_size_str = getenv("MESA_GLSL_CACHE_MAX_SIZE");
163 if (max_size_str)
164 fprintf(stderr,
165 "*** MESA_GLSL_CACHE_MAX_SIZE is deprecated; "
166 "use MESA_SHADER_CACHE_MAX_SIZE instead ***\n");
167 }
168
169 #ifdef MESA_SHADER_CACHE_MAX_SIZE
170 if( !max_size_str ) {
171 max_size_str = MESA_SHADER_CACHE_MAX_SIZE;
172 }
173 #endif
174
175 if (max_size_str) {
176 char *end;
177 max_size = strtoul(max_size_str, &end, 10);
178 if (end == max_size_str) {
179 max_size = 0;
180 } else {
181 switch (*end) {
182 case 'K':
183 case 'k':
184 max_size *= 1024;
185 break;
186 case 'M':
187 case 'm':
188 max_size *= 1024*1024;
189 break;
190 case '\0':
191 case 'G':
192 case 'g':
193 default:
194 max_size *= 1024*1024*1024;
195 break;
196 }
197 }
198 }
199
200 /* Default to 1GB for maximum cache size. */
201 if (max_size == 0) {
202 max_size = 1024*1024*1024;
203 }
204
205 cache->max_size = max_size;
206
207 if (cache->type == DISK_CACHE_DATABASE)
208 mesa_cache_db_multipart_set_size_limit(&cache->cache_db, cache->max_size);
209
210 if (!disk_cache_init_queue(cache))
211 goto fail;
212
213 cache->path_init_failed = false;
214
215 path_fail:
216
217 cache->driver_keys_blob_size = cv_size;
218
219 /* Create driver id keys */
220 size_t id_size = strlen(driver_id) + 1;
221 size_t gpu_name_size = strlen(gpu_name) + 1;
222 cache->driver_keys_blob_size += id_size;
223 cache->driver_keys_blob_size += gpu_name_size;
224
225 /* We sometimes store entire structs that contains a pointers in the cache,
226 * use pointer size as a key to avoid hard to debug issues.
227 */
228 uint8_t ptr_size = sizeof(void *);
229 size_t ptr_size_size = sizeof(ptr_size);
230 cache->driver_keys_blob_size += ptr_size_size;
231
232 size_t driver_flags_size = sizeof(driver_flags);
233 cache->driver_keys_blob_size += driver_flags_size;
234
235 cache->driver_keys_blob =
236 ralloc_size(cache, cache->driver_keys_blob_size);
237 if (!cache->driver_keys_blob)
238 goto fail;
239
240 uint8_t *drv_key_blob = cache->driver_keys_blob;
241 DRV_KEY_CPY(drv_key_blob, &cache_version, cv_size)
242 DRV_KEY_CPY(drv_key_blob, driver_id, id_size)
243 DRV_KEY_CPY(drv_key_blob, gpu_name, gpu_name_size)
244 DRV_KEY_CPY(drv_key_blob, &ptr_size, ptr_size_size)
245 DRV_KEY_CPY(drv_key_blob, &driver_flags, driver_flags_size)
246
247 /* Seed our rand function */
248 s_rand_xorshift128plus(cache->seed_xorshift128plus, true);
249
250 ralloc_free(local);
251
252 return cache;
253
254 fail:
255 if (cache)
256 ralloc_free(cache);
257 ralloc_free(local);
258
259 return NULL;
260 }
261
262 struct disk_cache *
disk_cache_create(const char * gpu_name,const char * driver_id,uint64_t driver_flags)263 disk_cache_create(const char *gpu_name, const char *driver_id,
264 uint64_t driver_flags)
265 {
266 enum disk_cache_type cache_type;
267 struct disk_cache *cache;
268
269 if (debug_get_bool_option("MESA_DISK_CACHE_SINGLE_FILE", false))
270 cache_type = DISK_CACHE_SINGLE_FILE;
271 else if (debug_get_bool_option("MESA_DISK_CACHE_DATABASE", false))
272 cache_type = DISK_CACHE_DATABASE;
273 else
274 cache_type = DISK_CACHE_MULTI_FILE;
275
276 /* Create main writable cache. */
277 cache = disk_cache_type_create(gpu_name, driver_id, driver_flags,
278 cache_type);
279 if (!cache)
280 return NULL;
281
282 /* If MESA_DISK_CACHE_SINGLE_FILE is unset and MESA_DISK_CACHE_COMBINE_RW_WITH_RO_FOZ
283 * is set, then enable additional Fossilize RO caches together with the RW
284 * cache. At first we will check cache entry presence in the RO caches and
285 * if entry isn't found there, then we'll fall back to the RW cache.
286 */
287 if (cache_type != DISK_CACHE_SINGLE_FILE && !cache->path_init_failed &&
288 debug_get_bool_option("MESA_DISK_CACHE_COMBINE_RW_WITH_RO_FOZ", false)) {
289
290 /* Create read-only cache used for sharing prebuilt shaders.
291 * If cache entry will be found in this cache, then the main cache
292 * will be bypassed.
293 */
294 cache->foz_ro_cache = disk_cache_type_create(gpu_name, driver_id,
295 driver_flags,
296 DISK_CACHE_SINGLE_FILE);
297 }
298
299 return cache;
300 }
301
302 void
disk_cache_destroy(struct disk_cache * cache)303 disk_cache_destroy(struct disk_cache *cache)
304 {
305 if (unlikely(cache && cache->stats.enabled)) {
306 printf("disk shader cache: hits = %u, misses = %u\n",
307 cache->stats.hits,
308 cache->stats.misses);
309 }
310
311 if (cache && util_queue_is_initialized(&cache->cache_queue)) {
312 util_queue_finish(&cache->cache_queue);
313 util_queue_destroy(&cache->cache_queue);
314
315 if (cache->foz_ro_cache)
316 disk_cache_destroy(cache->foz_ro_cache);
317
318 if (cache->type == DISK_CACHE_SINGLE_FILE)
319 foz_destroy(&cache->foz_db);
320
321 if (cache->type == DISK_CACHE_DATABASE)
322 mesa_cache_db_multipart_close(&cache->cache_db);
323
324 disk_cache_destroy_mmap(cache);
325 }
326
327 ralloc_free(cache);
328 }
329
330 void
disk_cache_wait_for_idle(struct disk_cache * cache)331 disk_cache_wait_for_idle(struct disk_cache *cache)
332 {
333 util_queue_finish(&cache->cache_queue);
334 }
335
336 void
disk_cache_remove(struct disk_cache * cache,const cache_key key)337 disk_cache_remove(struct disk_cache *cache, const cache_key key)
338 {
339 if (cache->type == DISK_CACHE_DATABASE) {
340 mesa_cache_db_multipart_entry_remove(&cache->cache_db, key);
341 return;
342 }
343
344 char *filename = disk_cache_get_cache_filename(cache, key);
345 if (filename == NULL) {
346 return;
347 }
348
349 disk_cache_evict_item(cache, filename);
350 }
351
352 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)353 create_put_job(struct disk_cache *cache, const cache_key key,
354 void *data, size_t size,
355 struct cache_item_metadata *cache_item_metadata,
356 bool take_ownership)
357 {
358 struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *)
359 malloc(sizeof(struct disk_cache_put_job) + (take_ownership ? 0 : size));
360
361 if (dc_job) {
362 dc_job->cache = cache;
363 memcpy(dc_job->key, key, sizeof(cache_key));
364 if (take_ownership) {
365 dc_job->data = data;
366 } else {
367 dc_job->data = dc_job + 1;
368 memcpy(dc_job->data, data, size);
369 }
370 dc_job->size = size;
371
372 /* Copy the cache item metadata */
373 if (cache_item_metadata) {
374 dc_job->cache_item_metadata.type = cache_item_metadata->type;
375 if (cache_item_metadata->type == CACHE_ITEM_TYPE_GLSL) {
376 dc_job->cache_item_metadata.num_keys =
377 cache_item_metadata->num_keys;
378 dc_job->cache_item_metadata.keys = (cache_key *)
379 malloc(cache_item_metadata->num_keys * sizeof(cache_key));
380
381 if (!dc_job->cache_item_metadata.keys)
382 goto fail;
383
384 memcpy(dc_job->cache_item_metadata.keys,
385 cache_item_metadata->keys,
386 sizeof(cache_key) * cache_item_metadata->num_keys);
387 }
388 } else {
389 dc_job->cache_item_metadata.type = CACHE_ITEM_TYPE_UNKNOWN;
390 dc_job->cache_item_metadata.keys = NULL;
391 }
392 }
393
394 return dc_job;
395
396 fail:
397 free(dc_job);
398
399 return NULL;
400 }
401
402 static void
destroy_put_job(void * job,void * gdata,int thread_index)403 destroy_put_job(void *job, void *gdata, int thread_index)
404 {
405 if (job) {
406 struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job;
407 free(dc_job->cache_item_metadata.keys);
408 free(job);
409 }
410 }
411
412 static void
destroy_put_job_nocopy(void * job,void * gdata,int thread_index)413 destroy_put_job_nocopy(void *job, void *gdata, int thread_index)
414 {
415 struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job;
416 free(dc_job->data);
417 destroy_put_job(job, gdata, thread_index);
418 }
419
420 static void
421 blob_put_compressed(struct disk_cache *cache, const cache_key key,
422 const void *data, size_t size);
423
424 static void
cache_put(void * job,void * gdata,int thread_index)425 cache_put(void *job, void *gdata, int thread_index)
426 {
427 assert(job);
428
429 unsigned i = 0;
430 char *filename = NULL;
431 struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job;
432
433 if (dc_job->cache->blob_put_cb) {
434 blob_put_compressed(dc_job->cache, dc_job->key, dc_job->data, dc_job->size);
435 } else if (dc_job->cache->type == DISK_CACHE_SINGLE_FILE) {
436 disk_cache_write_item_to_disk_foz(dc_job);
437 } else if (dc_job->cache->type == DISK_CACHE_DATABASE) {
438 disk_cache_db_write_item_to_disk(dc_job);
439 } else if (dc_job->cache->type == DISK_CACHE_MULTI_FILE) {
440 filename = disk_cache_get_cache_filename(dc_job->cache, dc_job->key);
441 if (filename == NULL)
442 goto done;
443
444 /* If the cache is too large, evict something else first. */
445 while (*dc_job->cache->size + dc_job->size > dc_job->cache->max_size &&
446 i < 8) {
447 disk_cache_evict_lru_item(dc_job->cache);
448 i++;
449 }
450
451 disk_cache_write_item_to_disk(dc_job, filename);
452
453 done:
454 free(filename);
455 }
456 }
457
458 struct blob_cache_entry {
459 uint32_t uncompressed_size;
460 uint8_t compressed_data[];
461 };
462
463 static void
blob_put_compressed(struct disk_cache * cache,const cache_key key,const void * data,size_t size)464 blob_put_compressed(struct disk_cache *cache, const cache_key key,
465 const void *data, size_t size)
466 {
467 MESA_TRACE_FUNC();
468
469 size_t max_buf = util_compress_max_compressed_len(size);
470 struct blob_cache_entry *entry = malloc(max_buf + sizeof(*entry));
471 if (!entry)
472 goto out;
473
474 entry->uncompressed_size = size;
475
476 MESA_TRACE_BEGIN("deflate");
477 size_t compressed_size =
478 util_compress_deflate(data, size, entry->compressed_data, max_buf);
479 MESA_TRACE_END();
480 if (!compressed_size)
481 goto out;
482
483 unsigned entry_size = compressed_size + sizeof(*entry);
484 MESA_TRACE_BEGIN("blob_put");
485 cache->blob_put_cb(key, CACHE_KEY_SIZE, entry, entry_size);
486 MESA_TRACE_END();
487
488 out:
489 free(entry);
490 }
491
492 static void *
blob_get_compressed(struct disk_cache * cache,const cache_key key,size_t * size)493 blob_get_compressed(struct disk_cache *cache, const cache_key key,
494 size_t *size)
495 {
496 MESA_TRACE_FUNC();
497
498 /* This is what Android EGL defines as the maxValueSize in egl_cache_t
499 * class implementation.
500 */
501 const signed long max_blob_size = 64 * 1024;
502 struct blob_cache_entry *entry = malloc(max_blob_size);
503 if (!entry)
504 return NULL;
505
506 MESA_TRACE_BEGIN("blob_get");
507 signed long entry_size =
508 cache->blob_get_cb(key, CACHE_KEY_SIZE, entry, max_blob_size);
509 MESA_TRACE_END();
510
511 if (!entry_size) {
512 free(entry);
513 return NULL;
514 }
515
516 void *data = malloc(entry->uncompressed_size);
517 if (!data) {
518 free(entry);
519 return NULL;
520 }
521
522 unsigned compressed_size = entry_size - sizeof(*entry);
523 MESA_TRACE_BEGIN("inflate");
524 bool ret = util_compress_inflate(entry->compressed_data, compressed_size,
525 data, entry->uncompressed_size);
526 MESA_TRACE_END();
527 if (!ret) {
528 free(data);
529 free(entry);
530 return NULL;
531 }
532
533 if (size)
534 *size = entry->uncompressed_size;
535
536 free(entry);
537
538 return data;
539 }
540
541 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)542 disk_cache_put(struct disk_cache *cache, const cache_key key,
543 const void *data, size_t size,
544 struct cache_item_metadata *cache_item_metadata)
545 {
546 if (!util_queue_is_initialized(&cache->cache_queue))
547 return;
548
549 struct disk_cache_put_job *dc_job =
550 create_put_job(cache, key, (void*)data, size, cache_item_metadata, false);
551
552 if (dc_job) {
553 util_queue_fence_init(&dc_job->fence);
554 util_queue_add_job(&cache->cache_queue, dc_job, &dc_job->fence,
555 cache_put, destroy_put_job, dc_job->size);
556 }
557 }
558
559 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)560 disk_cache_put_nocopy(struct disk_cache *cache, const cache_key key,
561 void *data, size_t size,
562 struct cache_item_metadata *cache_item_metadata)
563 {
564 if (!util_queue_is_initialized(&cache->cache_queue)) {
565 free(data);
566 return;
567 }
568
569 struct disk_cache_put_job *dc_job =
570 create_put_job(cache, key, data, size, cache_item_metadata, true);
571
572 if (dc_job) {
573 util_queue_fence_init(&dc_job->fence);
574 util_queue_add_job(&cache->cache_queue, dc_job, &dc_job->fence,
575 cache_put, destroy_put_job_nocopy, dc_job->size);
576 }
577 }
578
579 void *
disk_cache_get(struct disk_cache * cache,const cache_key key,size_t * size)580 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size)
581 {
582 void *buf = NULL;
583
584 if (size)
585 *size = 0;
586
587 if (cache->foz_ro_cache)
588 buf = disk_cache_load_item_foz(cache->foz_ro_cache, key, size);
589
590 if (!buf) {
591 if (cache->blob_get_cb) {
592 buf = blob_get_compressed(cache, key, size);
593 } else if (cache->type == DISK_CACHE_SINGLE_FILE) {
594 buf = disk_cache_load_item_foz(cache, key, size);
595 } else if (cache->type == DISK_CACHE_DATABASE) {
596 buf = disk_cache_db_load_item(cache, key, size);
597 } else if (cache->type == DISK_CACHE_MULTI_FILE) {
598 char *filename = disk_cache_get_cache_filename(cache, key);
599 if (filename)
600 buf = disk_cache_load_item(cache, filename, size);
601 }
602 }
603
604 if (unlikely(cache->stats.enabled)) {
605 if (buf)
606 p_atomic_inc(&cache->stats.hits);
607 else
608 p_atomic_inc(&cache->stats.misses);
609 }
610
611 return buf;
612 }
613
614 void
disk_cache_put_key(struct disk_cache * cache,const cache_key key)615 disk_cache_put_key(struct disk_cache *cache, const cache_key key)
616 {
617 const uint32_t *key_chunk = (const uint32_t *) key;
618 int i = CPU_TO_LE32(*key_chunk) & CACHE_INDEX_KEY_MASK;
619 unsigned char *entry;
620
621 if (cache->blob_put_cb) {
622 cache->blob_put_cb(key, CACHE_KEY_SIZE, key_chunk, sizeof(uint32_t));
623 return;
624 }
625
626 if (cache->path_init_failed)
627 return;
628
629 entry = &cache->stored_keys[i * CACHE_KEY_SIZE];
630
631 memcpy(entry, key, CACHE_KEY_SIZE);
632 }
633
634 /* This function lets us test whether a given key was previously
635 * stored in the cache with disk_cache_put_key(). The implement is
636 * efficient by not using syscalls or hitting the disk. It's not
637 * race-free, but the races are benign. If we race with someone else
638 * calling disk_cache_put_key, then that's just an extra cache miss and an
639 * extra recompile.
640 */
641 bool
disk_cache_has_key(struct disk_cache * cache,const cache_key key)642 disk_cache_has_key(struct disk_cache *cache, const cache_key key)
643 {
644 const uint32_t *key_chunk = (const uint32_t *) key;
645 int i = CPU_TO_LE32(*key_chunk) & CACHE_INDEX_KEY_MASK;
646 unsigned char *entry;
647
648 if (cache->blob_get_cb) {
649 uint32_t blob;
650 return cache->blob_get_cb(key, CACHE_KEY_SIZE, &blob, sizeof(uint32_t));
651 }
652
653 if (cache->path_init_failed)
654 return false;
655
656 entry = &cache->stored_keys[i * CACHE_KEY_SIZE];
657
658 return memcmp(entry, key, CACHE_KEY_SIZE) == 0;
659 }
660
661 void
disk_cache_compute_key(struct disk_cache * cache,const void * data,size_t size,cache_key key)662 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
663 cache_key key)
664 {
665 struct mesa_sha1 ctx;
666
667 _mesa_sha1_init(&ctx);
668 _mesa_sha1_update(&ctx, cache->driver_keys_blob,
669 cache->driver_keys_blob_size);
670 _mesa_sha1_update(&ctx, data, size);
671 _mesa_sha1_final(&ctx, key);
672 }
673
674 void
disk_cache_set_callbacks(struct disk_cache * cache,disk_cache_put_cb put,disk_cache_get_cb get)675 disk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put,
676 disk_cache_get_cb get)
677 {
678 cache->blob_put_cb = put;
679 cache->blob_get_cb = get;
680 disk_cache_init_queue(cache);
681 }
682
683 #endif /* ENABLE_SHADER_CACHE */
684