-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathmipmap_cache.c
More file actions
1606 lines (1454 loc) · 60.8 KB
/
mipmap_cache.c
File metadata and controls
1606 lines (1454 loc) · 60.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of darktable,
Copyright (C) 2011-2024 darktable developers.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/mipmap_cache.h"
#include "common/darktable.h"
#include "common/debug.h"
#include "common/exif.h"
#include "common/file_location.h"
#include "common/grealpath.h"
#include "common/image_cache.h"
#include "control/conf.h"
#include "control/jobs.h"
#include "develop/imageop_math.h"
#include "imageio/imageio_common.h"
#include "imageio/imageio_jpeg.h"
#include "imageio/imageio_module.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#if !defined(_WIN32)
#include <sys/statvfs.h>
#else
//statvfs does not exist in Windows, providing implementation
#include "win/statvfs.h"
#endif
#define DT_MIPMAP_CACHE_FILE_MAGIC 0xD71337
#define DT_MIPMAP_CACHE_FILE_VERSION 23
#define DT_MIPMAP_CACHE_DEFAULT_FILE_NAME "mipmaps"
// the number of pixels in the largest static image is the smallest number of pixels we
// can allocate for the mipmap buffer, since that gets filled in after we attempt to read
// from the image file on disk
#define MIN_IMG_PIXELS 540
typedef enum dt_mipmap_buffer_dsc_flags
{
DT_MIPMAP_BUFFER_DSC_FLAG_NONE = 0,
DT_MIPMAP_BUFFER_DSC_FLAG_GENERATE = 1 << 0,
DT_MIPMAP_BUFFER_DSC_FLAG_INVALIDATE = 1 << 1
} dt_mipmap_buffer_dsc_flags;
// the embedded Exif data to tag thumbnails as sRGB or AdobeRGB
static const uint8_t dt_mipmap_cache_exif_data_srgb[] = {
0x45, 0x78, 0x69, 0x66, 0x00, 0x00, 0x49, 0x49, 0x2a, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x69,
0x87, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
0x01, 0xa0, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const uint8_t dt_mipmap_cache_exif_data_adobergb[] = {
0x45, 0x78, 0x69, 0x66, 0x00, 0x00, 0x49, 0x49, 0x2a, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x69,
0x87, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
0x01, 0xa0, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const int dt_mipmap_cache_exif_data_srgb_length
= sizeof(dt_mipmap_cache_exif_data_srgb) / sizeof(*dt_mipmap_cache_exif_data_srgb);
static const int dt_mipmap_cache_exif_data_adobergb_length
= sizeof(dt_mipmap_cache_exif_data_adobergb) / sizeof(*dt_mipmap_cache_exif_data_adobergb);
// Define the static images. We make the definitions macros so that they can be expanded to either
// 4-channel 8 bits/channel integer images or 4-channel float images, depending on how __ and XX
// are defined at the time of use.
#define DEAD_IMAGE \
{ __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,XX,XX,XX,XX,XX,XX,__,__,__,__,__,__,__, \
__,__,__,__,__,__,XX,XX,XX,XX,XX,XX,XX,XX,__,__,__,__,__,__, \
__,__,__,__,__,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,__,__,__,__,__, \
__,__,__,__,__,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,__,__,__,__,__, \
__,__,__,__,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,__,__,__,__, \
__,__,__,__,XX,XX,XX,__,XX,XX,XX,XX,__,XX,XX,XX,__,__,__,__, \
__,__,__,XX,XX,XX,__,__,__,XX,XX,__,__,__,XX,XX,XX,__,__,__, \
__,__,__,XX,XX,XX,__,__,__,XX,XX,__,__,__,XX,XX,XX,__,__,__, \
__,__,XX,XX,XX,__,__,__,__,XX,XX,__,__,__,__,XX,XX,XX,__,__, \
__,__,XX,XX,XX,__,__,__,__,XX,XX,__,__,__,__,XX,XX,XX,__,__, \
__,XX,XX,XX,XX,XX,__,__,__,XX,XX,__,__,__,XX,XX,XX,XX,XX,__, \
__,XX,XX,XX,XX,XX,__,__,XX,XX,XX,XX,__,__,XX,XX,XX,XX,XX,__, \
__,XX,XX,XX,XX,XX,XX,__,XX,XX,XX,XX,__,XX,XX,XX,XX,XX,XX,__, \
__,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,__, \
__,__,XX,XX,XX,XX,XX,XX,XX,__,__,XX,XX,XX,XX,XX,XX,XX,__,__, \
__,__,XX,XX,XX,XX,XX,XX,XX,__,__,XX,XX,XX,XX,XX,XX,XX,__,__, \
__,__,__,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,__,__,__, \
__,__,__,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,__,__,__, \
__,__,__,XX,XX,__,XX,__,XX,__,XX,__,XX,__,XX,XX,__,__,__,__, \
__,__,__,XX,XX,__,__,__,__,__,__,__,__,__,XX,XX,__,__,__,__, \
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, \
__,__,__,__,XX,__,__,XX,__,XX,__,XX,__,__,XX,__,__,__,__,__, \
__,__,__,__,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,__,__,__,__,__, \
__,__,__,__,__,XX,XX,XX,XX,XX,XX,XX,XX,XX,__,__,__,__,__,__, \
__,__,__,__,__,__,XX,XX,XX,XX,XX,XX,XX,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__ }
#define UNSUPPORTED_IMAGE \
{ __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,XX,XX,XX,XX,__,__,__,__,__,__, \
__,__,__,__,__,XX,XX,XX,XX,XX,XX,XX,XX,__,__,__,__, \
__,__,__,__,XX,XX,XX,__,__,__,__,XX,XX,XX,__,__,__, \
__,__,__,XX,XX,__,__,__,__,__,__,__,__,XX,XX,__,__, \
__,__,XX,XX,__,__,__,__,__,__,__,__,__,XX,XX,__,__, \
__,__,XX,XX,__,__,__,__,__,__,__,__,__,__,XX,XX,__, \
__,XX,XX,__,__,__,__,__,__,__,__,__,__,__,XX,XX,__, \
__,XX,XX,__,__,__,__,__,__,__,__,__,__,__,XX,XX,__, \
__,XX,XX,__,__,__,__,__,__,__,__,__,__,__,XX,XX,__, \
__,__,__,__,__,__,__,__,__,__,__,__,__,XX,XX,__,__, \
__,__,__,__,__,__,__,__,__,__,__,__,__,XX,XX,__,__, \
__,__,__,__,__,__,__,__,__,__,__,__,XX,XX,__,__,__, \
__,__,__,__,__,__,__,__,__,__,__,XX,XX,__,__,__,__, \
__,__,__,__,__,__,__,__,__,__,XX,XX,__,__,__,__,__, \
__,__,__,__,__,__,__,__,__,XX,XX,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,__,XX,XX,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,XX,XX,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,XX,XX,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,XX,XX,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,XX,XX,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,XX,XX,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,XX,XX,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__ }
#define ERROR_IMAGE \
{ __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,__,__,__,XX,__,__,__,__,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,__,__,XX,XX,XX,__,__,__,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,__,__,XX,__,XX,__,__,__,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,__,XX,XX,__,XX,XX,__,__,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,__,XX,XX,__,XX,XX,__,__,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,XX,XX,__,__,__,XX,XX,__,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,__,XX,XX,__,__,__,XX,XX,__,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,XX,XX,XX,__,__,__,XX,XX,XX,__,__,__,__,__,__,__, \
__,__,__,__,__,__,__,XX,XX,XX,__,__,__,XX,XX,XX,__,__,__,__,__,__,__, \
__,__,__,__,__,__,XX,XX,XX,XX,__,__,__,XX,XX,XX,XX,__,__,__,__,__,__, \
__,__,__,__,__,__,XX,XX,XX,XX,__,__,__,XX,XX,XX,XX,__,__,__,__,__,__, \
__,__,__,__,__,XX,XX,XX,XX,XX,__,__,__,XX,XX,XX,XX,XX,__,__,__,__,__, \
__,__,__,__,__,XX,XX,XX,XX,XX,XX,__,XX,XX,XX,XX,XX,XX,__,__,__,__,__, \
__,__,__,__,XX,XX,XX,XX,XX,XX,XX,__,XX,XX,XX,XX,XX,XX,XX,__,__,__,__, \
__,__,__,__,XX,XX,XX,XX,XX,XX,XX,__,XX,XX,XX,XX,XX,XX,XX,__,__,__,__, \
__,__,__,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,__,__,__, \
__,__,__,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,__,__,__, \
__,__,XX,XX,XX,XX,XX,XX,XX,XX,XX,__,XX,XX,XX,XX,XX,XX,XX,XX,XX,__,__, \
__,__,XX,XX,XX,XX,XX,XX,XX,XX,__,__,__,XX,XX,XX,XX,XX,XX,XX,XX,__,__, \
__,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,__,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,__, \
__,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,__, \
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__ }
struct dt_mipmap_buffer_dsc
{
uint32_t width;
uint32_t height;
float iscale;
size_t size;
dt_mipmap_buffer_dsc_flags flags;
dt_colorspaces_color_profile_type_t color_space;
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
// do not touch!
// must be the last element.
// must be no less than 16bytes
char redzone[16];
#endif
/* NB: sizeof must be a multiple of 4*sizeof(float) */
} DT_ALIGNED_ARRAY __attribute__((packed));
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
static const size_t dt_mipmap_buffer_dsc_size __attribute__((unused))
= sizeof(struct dt_mipmap_buffer_dsc) - sizeof(((struct dt_mipmap_buffer_dsc *)0)->redzone);
#else
static const size_t dt_mipmap_buffer_dsc_size __attribute__((unused)) = sizeof(struct dt_mipmap_buffer_dsc);
#endif
// last resort mem alloc for dead images. sizeof(dt_mipmap_buffer_dsc) + max static image pixels (14x15)
// Must be aligned to cache line
static float DT_ALIGNED_ARRAY _mipmap_cache_static_dead_image[sizeof(struct dt_mipmap_buffer_dsc) / sizeof(float) + MIN_IMG_PIXELS * 4];
static inline void _dead_image_8(dt_mipmap_buffer_t *buf)
{
if(!buf->buf) return;
struct dt_mipmap_buffer_dsc *dsc = (struct dt_mipmap_buffer_dsc *)buf->buf - 1;
dsc->width = 20; dsc->height = 27;
dsc->iscale = 1.0f;
buf->color_space = dsc->color_space = DT_COLORSPACE_DISPLAY;
assert(dsc->size > 210 * sizeof(uint32_t));
#define __ 0
#define XX 0xffffffffu
static const uint32_t image[] = DEAD_IMAGE;
#undef __
#undef XX
memcpy(buf->buf, image, sizeof(image));
}
static inline void _dead_image_f(dt_mipmap_buffer_t *buf)
{
if(!buf->buf) return;
struct dt_mipmap_buffer_dsc *dsc = (struct dt_mipmap_buffer_dsc *)buf->buf - 1;
dsc->width = 20; dsc->height = 27;
dsc->iscale = 1.0f;
buf->color_space = dsc->color_space = DT_COLORSPACE_DISPLAY;
assert(dsc->size > 210 * 4 * sizeof(float));
#define XX 1.0f, 1.0f, 1.0f, 1.0f
#define __ 0.0f, 0.0f, 0.0f, 0.0f
static const float image[] = DEAD_IMAGE;
#undef XX
#undef __
memcpy(buf->buf, image, sizeof(image));
}
static inline void unsupp_image_8(dt_mipmap_buffer_t *buf)
{
if(!buf->buf) return;
struct dt_mipmap_buffer_dsc *dsc = (struct dt_mipmap_buffer_dsc *)buf->buf - 1;
dsc->width = 17; dsc->height = 25;
dsc->iscale = 1.0f;
buf->color_space = dsc->color_space = DT_COLORSPACE_DISPLAY;
assert(dsc->size >= dsc->width * dsc->height * sizeof(uint32_t));
#define __ 0
#define XX 0xffffffffu
static const uint32_t image[] = UNSUPPORTED_IMAGE;
#undef __
#undef XX
memcpy(buf->buf, image, sizeof(image));
}
static inline void unsupp_image_f(dt_mipmap_buffer_t *buf)
{
if(!buf->buf) return;
struct dt_mipmap_buffer_dsc *dsc = (struct dt_mipmap_buffer_dsc *)buf->buf - 1;
dsc->width = 17; dsc->height = 25;
dsc->iscale = 1.0f;
buf->color_space = dsc->color_space = DT_COLORSPACE_DISPLAY;
assert(dsc->size > dsc->width * dsc->height * 4 * sizeof(float));
#define XX 1.0f, 1.0f, 1.0f, 1.0f
#define __ 0.0f, 0.0f, 0.0f, 0.0f
static const float image[] = UNSUPPORTED_IMAGE;
#undef XX
#undef __
memcpy(buf->buf, image, sizeof(image));
}
static inline void error_image_8(dt_mipmap_buffer_t *buf)
{
if(!buf->buf) return;
struct dt_mipmap_buffer_dsc *dsc = (struct dt_mipmap_buffer_dsc *)buf->buf - 1;
dsc->width = dsc->height = 23;
dsc->iscale = 1.0f;
buf->color_space = dsc->color_space = DT_COLORSPACE_DISPLAY;
assert(dsc->size >= dsc->width * dsc->height * sizeof(uint32_t));
#define __ 0
#define XX 0xffffffffu
static const uint32_t image[] = ERROR_IMAGE;
#undef __
#undef XX
memcpy(buf->buf, image, sizeof(image));
}
//static inline
void error_image_f(dt_mipmap_buffer_t *buf)
{
if(!buf->buf) return;
struct dt_mipmap_buffer_dsc *dsc = (struct dt_mipmap_buffer_dsc *)buf->buf - 1;
dsc->width = dsc->height = 23;
dsc->iscale = 1.0f;
buf->color_space = dsc->color_space = DT_COLORSPACE_DISPLAY;
assert(dsc->size >= dsc->width * dsc->height * 4 * sizeof(float));
#define XX 1.0f, 1.0f, 1.0f, 1.0f
#define __ 0.0f, 0.0f, 0.0f, 0.0f
static const float image[] = ERROR_IMAGE;
#undef XX
#undef __
memcpy(buf->buf, image, sizeof(image));
}
static inline gboolean _is_static_image(void *buffer)
{
return (buffer == _mipmap_cache_static_dead_image);
}
#ifndef NDEBUG
static inline int32_t _buffer_is_broken(dt_mipmap_buffer_t *buf)
{
if(!buf->buf) return 0;
struct dt_mipmap_buffer_dsc *dsc = (struct dt_mipmap_buffer_dsc *)buf->buf - 1;
if(buf->width != dsc->width) return 1;
if(buf->height != dsc->height) return 2;
// somewhat loose bound:
if(buf->width * buf->height > dsc->size) return 3;
return 0;
}
#endif
static inline uint32_t get_key(const dt_imgid_t imgid, const dt_mipmap_size_t size)
{
// imgid can't be >= 2^28 (~250 million images)
// also ensure a valid key for bad imgid
return (((uint32_t)size) << 28) | ((imgid - 1) & 0xfffffff);
}
static inline uint32_t get_imgid(const uint32_t key)
{
return (key & 0xfffffff) + 1;
}
static inline dt_mipmap_size_t _get_size(const uint32_t key)
{
return (dt_mipmap_size_t)(key >> 28);
}
static int _mipmap_cache_get_filename(gchar *mipmapfilename, size_t size)
{
int r = -1;
char *abspath = NULL;
// Directory
char cachedir[PATH_MAX] = { 0 };
dt_loc_get_user_cache_dir(cachedir, sizeof(cachedir));
// Build the mipmap filename
const gchar *dbfilename = dt_database_get_path(darktable.db);
if(!strcmp(dbfilename, ":memory:"))
{
mipmapfilename[0] = '\0';
r = 0;
goto exit;
}
abspath = g_realpath(dbfilename);
if(!abspath) abspath = g_strdup(dbfilename);
GChecksum *chk = g_checksum_new(G_CHECKSUM_SHA1);
g_checksum_update(chk, (guchar *)abspath, strlen(abspath));
const gchar *filename = g_checksum_get_string(chk);
if(!filename || filename[0] == '\0')
snprintf(mipmapfilename, size, "%s/%s", cachedir, DT_MIPMAP_CACHE_DEFAULT_FILE_NAME);
else
snprintf(mipmapfilename, size, "%s/%s-%s", cachedir, DT_MIPMAP_CACHE_DEFAULT_FILE_NAME, filename);
g_checksum_free(chk);
r = 0;
exit:
g_free(abspath);
return r;
}
static void _init_f(dt_mipmap_buffer_t *mipmap_buf,
float *buf,
uint32_t *width,
uint32_t *height,
float *iscale,
const dt_imgid_t imgid);
static void _init_8(uint8_t *buf,
uint32_t *width,
uint32_t *height,
float *iscale,
dt_colorspaces_color_profile_type_t *color_space,
const dt_imgid_t imgid,
const dt_mipmap_size_t size);
// callback for the imageio core to allocate memory.
// only needed for _F and _FULL buffers, as they change size
// with the input image. will allocate img->width*img->height*img->bpp bytes.
void *dt_mipmap_cache_alloc(dt_mipmap_buffer_t *buf, const dt_image_t *img)
{
assert(buf->size == DT_MIPMAP_FULL);
dt_cache_entry_t *entry = buf->cache_entry;
struct dt_mipmap_buffer_dsc *dsc = (struct dt_mipmap_buffer_dsc *)entry->data;
const int wd = img->width;
const int ht = img->height;
const size_t bpp = dt_iop_buffer_dsc_to_bpp(&img->buf_dsc);
const size_t buffer_size = (size_t)wd * ht * bpp + sizeof(*dsc);
// buf might have been alloc'ed before,
// so only check size and re-alloc if necessary:
if(!buf->buf || _is_static_image((void *)dsc) || (entry->data_size < buffer_size))
{
if(!_is_static_image((void *)dsc)) dt_free_align(entry->data);
entry->data_size = 0;
entry->data = dt_alloc_aligned(buffer_size);
if(!entry->data)
{
// return fallback: at least alloc size for the error image (the largest static image):
entry->data = (void *)_mipmap_cache_static_dead_image;
// allocator holds the pointer. but let imageio client know that allocation failed:
return NULL;
}
entry->data_size = buffer_size;
// set buffer size only if we're making it larger.
dsc = (struct dt_mipmap_buffer_dsc *)entry->data;
}
dsc->size = buffer_size;
dsc->width = wd;
dsc->height = ht;
dsc->iscale = 1.0f;
dsc->color_space = DT_COLORSPACE_NONE;
dsc->flags = DT_MIPMAP_BUFFER_DSC_FLAG_GENERATE;
buf->buf = (uint8_t *)(dsc + 1);
// dt_print(DT_DEBUG_ALWAYS, "full buffer allocating img %u %d x %d = %u bytes (%p)\n", img->id, img->width,
// img->height, buffer_size, *buf);
assert(entry->data_size);
assert(dsc->size);
assert(dsc->size <= entry->data_size);
ASAN_POISON_MEMORY_REGION(entry->data, entry->data_size);
ASAN_UNPOISON_MEMORY_REGION(dsc + 1, buffer_size - sizeof(struct dt_mipmap_buffer_dsc));
// return pointer to start of payload
return dsc + 1;
}
// callback for the cache backend to initialize payload pointers
static void _mipmap_cache_allocate_dynamic(void *data, dt_cache_entry_t *entry)
{
dt_mipmap_cache_t *cache = (dt_mipmap_cache_t *)data;
// for full image buffers
struct dt_mipmap_buffer_dsc *dsc = entry->data;
const dt_mipmap_size_t mip = _get_size(entry->key);
// alloc mere minimum for the header + broken image buffer:
if(!dsc)
{
if(mip == DT_MIPMAP_8)
{
int imgfw= 0, imgfh= 0;
// be sure that we have the right size values
dt_image_get_final_size(get_imgid(entry->key), &imgfw, &imgfh);
size_t pixels = MAX((size_t)(imgfw + 4) * (imgfh + 4), MIN_IMG_PIXELS);
entry->data_size = sizeof(struct dt_mipmap_buffer_dsc) + 4 * pixels;
}
else if(mip <= DT_MIPMAP_F)
{
// these are fixed-size:
entry->data_size = MAX(cache->buffer_size[mip], 4 * MIN_IMG_PIXELS);
}
else
{
entry->data_size = sizeof(*dsc) + sizeof(float) * 4 * MIN_IMG_PIXELS;
}
entry->data = dt_alloc_aligned(entry->data_size);
// dt_print(DT_DEBUG_ALWAYS, "[mipmap cache] alloc dynamic for key %u %p\n", key, *buf);
if(!(entry->data))
{
dt_print(DT_DEBUG_ALWAYS, "[mipmap_cache] memory allocation failed!\n");
exit(1);
}
dsc = entry->data;
if(mip <= DT_MIPMAP_F)
{
dsc->width = cache->max_width[mip];
dsc->height = cache->max_height[mip];
dsc->iscale = 1.0f;
dsc->size = entry->data_size;
dsc->color_space = DT_COLORSPACE_NONE;
}
else
{
dsc->width = 0;
dsc->height = 0;
dsc->iscale = 0.0f;
dsc->color_space = DT_COLORSPACE_NONE;
dsc->size = entry->data_size;
}
}
assert(dsc->size >= sizeof(*dsc));
int loaded_from_disk = 0;
if(mip < DT_MIPMAP_F)
{
if(cache->cachedir[0] && ((dt_conf_get_bool("cache_disk_backend") && mip < DT_MIPMAP_8)
|| (dt_conf_get_bool("cache_disk_backend_full") && mip == DT_MIPMAP_8)))
{
// try and load from disk, if successful set flag
char filename[PATH_MAX] = {0};
snprintf(filename, sizeof(filename), "%s.d/%d/%" PRIu32 ".jpg", cache->cachedir, (int)mip,
get_imgid(entry->key));
FILE *f = g_fopen(filename, "rb");
if(f)
{
uint8_t *blob = 0;
fseek(f, 0, SEEK_END);
const long len = ftell(f);
if(len <= 0) goto read_error; // coverity madness
blob = (uint8_t *)dt_alloc_aligned(len);
if(!blob) goto read_error;
fseek(f, 0, SEEK_SET);
const int rd = fread(blob, sizeof(uint8_t), len, f);
if(rd != len) goto read_error;
dt_colorspaces_color_profile_type_t color_space;
dt_imageio_jpeg_t jpg;
if(dt_imageio_jpeg_decompress_header(blob, len, &jpg)
|| (jpg.width > cache->max_width[mip] || jpg.height > cache->max_height[mip])
|| ((color_space = dt_imageio_jpeg_read_color_space(&jpg)) == DT_COLORSPACE_NONE) // pointless test to keep it in the if clause
|| dt_imageio_jpeg_decompress(&jpg, (uint8_t *)entry->data + sizeof(*dsc)))
{
dt_print(DT_DEBUG_ALWAYS,
"[mipmap_cache] failed to decompress thumbnail for image %" PRIu32 " from `%s'!\n",
get_imgid(entry->key), filename);
goto read_error;
}
dt_print(DT_DEBUG_CACHE,
"[mipmap_cache] grab mip %d for ID=%d from disk cache\n", mip,
get_imgid(entry->key));
dsc->width = jpg.width;
dsc->height = jpg.height;
dsc->iscale = 1.0f;
dsc->color_space = color_space;
loaded_from_disk = 1;
if(0)
{
read_error:
g_unlink(filename);
}
dt_free_align(blob);
fclose(f);
}
}
}
if(!loaded_from_disk)
dsc->flags = DT_MIPMAP_BUFFER_DSC_FLAG_GENERATE;
else dsc->flags = 0;
// cost is just flat one for the buffer, as the buffers might have different sizes,
// to make sure quota is meaningful.
if(mip >= DT_MIPMAP_F)
entry->cost = 1;
else if(mip == DT_MIPMAP_8)
entry->cost = entry->data_size;
else
entry->cost = cache->buffer_size[mip];
}
static void _mipmap_cache_unlink_ondisk_thumbnail(void *data,
const dt_imgid_t imgid,
dt_mipmap_size_t mip)
{
dt_mipmap_cache_t *cache = (dt_mipmap_cache_t *)data;
// also remove jpg backing (always try to do that, in case user just temporarily switched it off,
// to avoid inconsistencies.
// if(dt_conf_get_bool("cache_disk_backend"))
if(cache->cachedir[0])
{
char filename[PATH_MAX] = { 0 };
snprintf(filename, sizeof(filename), "%s.d/%d/%"PRIu32".jpg", cache->cachedir, (int)mip, imgid);
g_unlink(filename);
}
}
static void _mipmap_cache_deallocate_dynamic(void *data, dt_cache_entry_t *entry)
{
dt_mipmap_cache_t *cache = (dt_mipmap_cache_t *)data;
const dt_mipmap_size_t mip = _get_size(entry->key);
if(mip < DT_MIPMAP_F)
{
struct dt_mipmap_buffer_dsc *dsc = (struct dt_mipmap_buffer_dsc *)entry->data;
// don't write skulls:
if(dsc->width > 8 && dsc->height > 8)
{
if(dsc->flags & DT_MIPMAP_BUFFER_DSC_FLAG_INVALIDATE)
{
_mipmap_cache_unlink_ondisk_thumbnail(data, get_imgid(entry->key), mip);
}
else if(cache->cachedir[0] && ((dt_conf_get_bool("cache_disk_backend") && mip < DT_MIPMAP_8)
|| (dt_conf_get_bool("cache_disk_backend_full") && mip == DT_MIPMAP_8)))
{
// serialize to disk
char filename[PATH_MAX] = {0};
snprintf(filename, sizeof(filename), "%s.d/%d", cache->cachedir, mip);
const int mkd = g_mkdir_with_parents(filename, 0750);
if(!mkd)
{
snprintf(filename, sizeof(filename), "%s.d/%d/%" PRIu32 ".jpg", cache->cachedir, (int)mip,
get_imgid(entry->key));
// Don't write existing files as both performance and quality (lossy jpg) suffer
FILE *f = NULL;
if(!g_file_test(filename, G_FILE_TEST_EXISTS) && (f = g_fopen(filename, "wb")))
{
// first check the disk isn't full
struct statvfs vfsbuf;
if(!statvfs(filename, &vfsbuf))
{
const int64_t free_mb = ((vfsbuf.f_frsize * vfsbuf.f_bavail) >> 20);
if(free_mb < 100)
{
dt_print(DT_DEBUG_ALWAYS,
"[mipmap_cache] aborting image write as only %" PRId64 " MB free to write %s\n",
free_mb, filename);
goto write_error;
}
}
else
{
dt_print(DT_DEBUG_ALWAYS,
"[mipmap_cache] aborting image write since couldn't determine free space available to write %s\n",
filename);
goto write_error;
}
const int cache_quality = dt_conf_get_int("database_cache_quality");
const uint8_t *exif = NULL;
int exif_len = 0;
if(dsc->color_space == DT_COLORSPACE_SRGB)
{
exif = dt_mipmap_cache_exif_data_srgb;
exif_len = dt_mipmap_cache_exif_data_srgb_length;
}
else if(dsc->color_space == DT_COLORSPACE_ADOBERGB)
{
exif = dt_mipmap_cache_exif_data_adobergb;
exif_len = dt_mipmap_cache_exif_data_adobergb_length;
}
if(dt_imageio_jpeg_write(filename, (uint8_t *)entry->data + sizeof(*dsc), dsc->width, dsc->height, MIN(100, MAX(10, cache_quality)), exif, exif_len))
{
write_error:
g_unlink(filename);
}
}
if(f) fclose(f);
}
}
}
}
dt_free_align(entry->data);
}
static uint32_t _nearest_power_of_two(const uint32_t value)
{
uint32_t rc = 1;
while(rc < value) rc <<= 1;
return rc;
}
void dt_mipmap_cache_init(dt_mipmap_cache_t *cache)
{
_mipmap_cache_get_filename(cache->cachedir, sizeof(cache->cachedir));
// make sure static memory is initialized
struct dt_mipmap_buffer_dsc *dsc = (struct dt_mipmap_buffer_dsc *)_mipmap_cache_static_dead_image;
_dead_image_f((dt_mipmap_buffer_t *)(dsc + 1));
// adjust numbers to be large enough to hold what mem limit suggests.
// we want at least 100MB, and consider 8G just still reasonable.
const size_t max_mem = CLAMPS(darktable.dtresources.mipmap_memory, 100u << 20, ((size_t)8) << 30);
// Fixed sizes for the thumbnail mip levels, selected for coverage of most screen sizes
int32_t mipsizes[DT_MIPMAP_F][2] = {
{ 180, 110 }, // mip0 - ~1/2 size previous one
{ 360, 225 }, // mip1 - 1/2 size previous one
{ 720, 450 }, // mip2 - 1/2 size previous one
{ 1440, 900 }, // mip3 - covers 720p and 1366x768
{ 1920, 1200 }, // mip4 - covers 1080p and 1600x1200
{ 2560, 1600 }, // mip5 - covers 2560x1440
{ 4096, 2560 }, // mip6 - covers 4K and UHD
{ 5120, 3200 }, // mip7 - covers 5120x2880 panels
{ 999999999, 999999999 }, // mip8 - used for full preview at full size
};
// Set mipf to mip2 size as at most the user will be using an 8K screen and
// have a preview that's ~4x smaller
const char *preview_downsample = dt_conf_get_string_const("preview_downsampling");
const float downsample = (!g_strcmp0(preview_downsample, "original")) ? 1.0f
: (!g_strcmp0(preview_downsample, "to 1/2")) ? 0.5f
: (!g_strcmp0(preview_downsample, "to 1/3")) ? 1/3.0f
: 0.25f;
cache->max_width[DT_MIPMAP_F] = mipsizes[DT_MIPMAP_2][0] * downsample;
cache->max_height[DT_MIPMAP_F] = mipsizes[DT_MIPMAP_2][1] * downsample;
for(int k = DT_MIPMAP_F-1; k >= 0; k--)
{
cache->max_width[k] = mipsizes[k][0];
cache->max_height[k] = mipsizes[k][1];
}
// header + buffer
for(int k = DT_MIPMAP_F-1; k >= 0; k--)
cache->buffer_size[k] = sizeof(struct dt_mipmap_buffer_dsc)
+ (size_t)cache->max_width[k] * cache->max_height[k] * 4;
// clear stats:
cache->mip_thumbs.stats_requests = 0;
cache->mip_thumbs.stats_near_match = 0;
cache->mip_thumbs.stats_misses = 0;
cache->mip_thumbs.stats_fetches = 0;
cache->mip_thumbs.stats_standin = 0;
cache->mip_f.stats_requests = 0;
cache->mip_f.stats_near_match = 0;
cache->mip_f.stats_misses = 0;
cache->mip_f.stats_fetches = 0;
cache->mip_f.stats_standin = 0;
cache->mip_full.stats_requests = 0;
cache->mip_full.stats_near_match = 0;
cache->mip_full.stats_misses = 0;
cache->mip_full.stats_fetches = 0;
cache->mip_full.stats_standin = 0;
dt_cache_init(&cache->mip_thumbs.cache, 0, max_mem);
dt_cache_set_allocate_callback(&cache->mip_thumbs.cache, _mipmap_cache_allocate_dynamic, cache);
dt_cache_set_cleanup_callback(&cache->mip_thumbs.cache, _mipmap_cache_deallocate_dynamic, cache);
// even with one thread you want two buffers. one for dr one for thumbs.
// Also have the nr of cache entries larger than worker threads
const int full_entries = 2 * dt_worker_threads();
const int32_t max_mem_bufs = _nearest_power_of_two(full_entries);
// for this buffer, because it can be very busy during import
dt_cache_init(&cache->mip_full.cache, 0, max_mem_bufs);
dt_cache_set_allocate_callback(&cache->mip_full.cache, _mipmap_cache_allocate_dynamic, cache);
dt_cache_set_cleanup_callback(&cache->mip_full.cache, _mipmap_cache_deallocate_dynamic, cache);
cache->buffer_size[DT_MIPMAP_FULL] = 0;
// same for mipf:
dt_cache_init(&cache->mip_f.cache, 0, max_mem_bufs);
dt_cache_set_allocate_callback(&cache->mip_f.cache, _mipmap_cache_allocate_dynamic, cache);
dt_cache_set_cleanup_callback(&cache->mip_f.cache, _mipmap_cache_deallocate_dynamic, cache);
cache->buffer_size[DT_MIPMAP_F] = sizeof(struct dt_mipmap_buffer_dsc)
+ 4 * sizeof(float) * cache->max_width[DT_MIPMAP_F]
* cache->max_height[DT_MIPMAP_F];
}
void dt_mipmap_cache_cleanup(dt_mipmap_cache_t *cache)
{
dt_cache_cleanup(&cache->mip_thumbs.cache);
dt_cache_cleanup(&cache->mip_full.cache);
dt_cache_cleanup(&cache->mip_f.cache);
}
void dt_mipmap_cache_print(dt_mipmap_cache_t *cache)
{
dt_print(DT_DEBUG_ALWAYS,"[mipmap_cache] thumbs fill %.2f/%.2f MB (%.2f%%)\n",
cache->mip_thumbs.cache.cost / (1024.0 * 1024.0),
cache->mip_thumbs.cache.cost_quota / (1024.0 * 1024.0),
100.0f * (float)cache->mip_thumbs.cache.cost / (float)cache->mip_thumbs.cache.cost_quota);
dt_print(DT_DEBUG_ALWAYS,"[mipmap_cache] float fill %"PRIu32"/%"PRIu32" slots (%.2f%%)\n",
(uint32_t)cache->mip_f.cache.cost, (uint32_t)cache->mip_f.cache.cost_quota,
100.0f * (float)cache->mip_f.cache.cost / (float)cache->mip_f.cache.cost_quota);
dt_print(DT_DEBUG_ALWAYS,"[mipmap_cache] full fill %"PRIu32"/%"PRIu32" slots (%.2f%%)\n",
(uint32_t)cache->mip_full.cache.cost, (uint32_t)cache->mip_full.cache.cost_quota,
100.0f * (float)cache->mip_full.cache.cost / (float)cache->mip_full.cache.cost_quota);
uint64_t sum = 0;
uint64_t sum_fetches = 0;
uint64_t sum_standins = 0;
sum += cache->mip_thumbs.stats_requests;
sum_fetches += cache->mip_thumbs.stats_fetches;
sum_standins += cache->mip_thumbs.stats_standin;
sum += cache->mip_f.stats_requests;
sum_fetches += cache->mip_f.stats_fetches;
sum_standins += cache->mip_f.stats_standin;
sum += cache->mip_full.stats_requests;
sum_fetches += cache->mip_full.stats_fetches;
sum_standins += cache->mip_full.stats_standin;
dt_print(DT_DEBUG_ALWAYS,"[mipmap_cache] level | near match | miss | stand-in | fetches | total rq\n");
dt_print(DT_DEBUG_ALWAYS,"[mipmap_cache] thumb | %6.2f%% | %6.2f%% | %6.2f%% | %6.2f%% | %6.2f%%\n",
100.0 * cache->mip_thumbs.stats_near_match / (float)cache->mip_thumbs.stats_requests,
100.0 * cache->mip_thumbs.stats_misses / (float)cache->mip_thumbs.stats_requests,
100.0 * cache->mip_thumbs.stats_standin / (float)sum_standins,
100.0 * cache->mip_thumbs.stats_fetches / (float)sum_fetches,
100.0 * cache->mip_thumbs.stats_requests / (float)sum);
dt_print(DT_DEBUG_ALWAYS,"[mipmap_cache] float | %6.2f%% | %6.2f%% | %6.2f%% | %6.2f%% | %6.2f%%\n",
100.0 * cache->mip_f.stats_near_match / (float)cache->mip_f.stats_requests,
100.0 * cache->mip_f.stats_misses / (float)cache->mip_f.stats_requests,
100.0 * cache->mip_f.stats_standin / (float)sum_standins,
100.0 * cache->mip_f.stats_fetches / (float)sum_fetches,
100.0 * cache->mip_f.stats_requests / (float)sum);
dt_print(DT_DEBUG_ALWAYS,"[mipmap_cache] full | %6.2f%% | %6.2f%% | %6.2f%% | %6.2f%% | %6.2f%%\n\n\n",
100.0 * cache->mip_full.stats_near_match / (float)cache->mip_full.stats_requests,
100.0 * cache->mip_full.stats_misses / (float)cache->mip_full.stats_requests,
100.0 * cache->mip_full.stats_standin / (float)sum_standins,
100.0 * cache->mip_full.stats_fetches / (float)sum_fetches,
100.0 * cache->mip_full.stats_requests / (float)sum);
}
static gboolean _raise_signal_mipmap_updated(gpointer user_data)
{
DT_DEBUG_CONTROL_SIGNAL_RAISE(darktable.signals, DT_SIGNAL_DEVELOP_MIPMAP_UPDATED, GPOINTER_TO_INT(user_data));
return FALSE; // only call once
}
static dt_mipmap_cache_one_t *_get_cache(dt_mipmap_cache_t *cache,
const dt_mipmap_size_t mip)
{
switch(mip)
{
case DT_MIPMAP_FULL:
return &cache->mip_full;
case DT_MIPMAP_F:
return &cache->mip_f;
default:
return &cache->mip_thumbs;
}
}
void dt_mipmap_cache_get_with_caller(
dt_mipmap_cache_t *cache,
dt_mipmap_buffer_t *buf,
const dt_imgid_t imgid,
const dt_mipmap_size_t mip,
const dt_mipmap_get_flags_t flags,
const char mode,
const char *file,
int line)
{
const uint32_t key = get_key(imgid, mip);
// buf may be null when called with DT_MIPMAP_PREFETCH for example
if(buf)
buf->loader_status = DT_IMAGEIO_OK;
if(flags == DT_MIPMAP_TESTLOCK)
{
// simple case: only get and lock if it's there.
dt_cache_entry_t *entry = dt_cache_testget(&_get_cache(cache, mip)->cache, key, mode);
buf->cache_entry = entry;
if(entry)
{
ASAN_UNPOISON_MEMORY_REGION(entry->data, dt_mipmap_buffer_dsc_size);
struct dt_mipmap_buffer_dsc *dsc = (struct dt_mipmap_buffer_dsc *)entry->data;
buf->width = dsc->width;
buf->height = dsc->height;
buf->iscale = dsc->iscale;
buf->color_space = dsc->color_space;
buf->imgid = imgid;
buf->size = mip;
// skip to next 8-byte alignment, for sse buffers.
buf->buf = (uint8_t *)(dsc + 1);
ASAN_UNPOISON_MEMORY_REGION(buf->buf, dsc->size - sizeof(struct dt_mipmap_buffer_dsc));
}
else
{
// set to NULL if failed.
buf->width = buf->height = 0;
buf->iscale = 0.0f;
buf->imgid = NO_IMGID;
buf->color_space = DT_COLORSPACE_NONE;
buf->size = DT_MIPMAP_NONE;
buf->buf = NULL;
}
}
else if(flags == DT_MIPMAP_PREFETCH)
{
// and opposite: prefetch without locking
if(mip > DT_MIPMAP_FULL || (int)mip < DT_MIPMAP_0)
return; // remove the (int) once we no longer have to support gcc < 4.8 :/
dt_control_add_job(darktable.control, DT_JOB_QUEUE_SYSTEM_FG, dt_image_load_job_create(imgid, mip));
}
else if(flags == DT_MIPMAP_PREFETCH_DISK)
{
// only prefetch if the disk cache exists:
if(!cache->cachedir[0]) return;
if(mip > DT_MIPMAP_FULL || (int)mip < DT_MIPMAP_0)
return; // remove the (int) once we no longer have to support gcc < 4.8 :/
char filename[PATH_MAX] = {0};
snprintf(filename, sizeof(filename), "%s.d/%d/%"PRIu32".jpg", cache->cachedir, (int)mip, key);
// don't attempt to load if disk cache doesn't exist
if(!g_file_test(filename, G_FILE_TEST_EXISTS)) return;
dt_control_add_job(darktable.control, DT_JOB_QUEUE_SYSTEM_FG, dt_image_load_job_create(imgid, mip));
}
else if(flags == DT_MIPMAP_BLOCKING)
{
// simple case: blocking get
dt_cache_entry_t *entry = dt_cache_get_with_caller(&_get_cache(cache, mip)->cache, key, mode, file, line);
ASAN_UNPOISON_MEMORY_REGION(entry->data, dt_mipmap_buffer_dsc_size);
struct dt_mipmap_buffer_dsc *dsc = (struct dt_mipmap_buffer_dsc *)entry->data;
buf->cache_entry = entry;
int mipmap_generated = 0;
if(dsc->flags & DT_MIPMAP_BUFFER_DSC_FLAG_GENERATE)
{
mipmap_generated = 1;
__sync_fetch_and_add(&(_get_cache(cache, mip)->stats_fetches), 1);
// dt_print(DT_DEBUG_ALWAYS, "[mipmap cache get] now initializing buffer for img %u mip %d!\n", imgid, mip);
// we're write locked here, as requested by the alloc callback.
// now fill it with data:
if(mip == DT_MIPMAP_FULL)
{
// load the image:
// make sure we access the r/w lock as shortly as possible!
dt_image_t DT_ALIGNED_ARRAY buffered_image;
const dt_image_t *cimg = dt_image_cache_get(darktable.image_cache, imgid, 'r');
buffered_image = *cimg;
// dt_image_t *img = dt_image_cache_write_get(darktable.image_cache, cimg);
// dt_image_cache_write_release(darktable.image_cache, img, DT_IMAGE_CACHE_RELAXED);
dt_image_cache_read_release(darktable.image_cache, cimg);
char filename[PATH_MAX] = { 0 };
gboolean from_cache = TRUE;
dt_image_full_path(buffered_image.id, filename, sizeof(filename), &from_cache);
buf->imgid = imgid;
buf->size = mip;
buf->buf = NULL;
buf->width = buf->height = 0;
buf->iscale = 0.0f;
buf->color_space = DT_COLORSPACE_NONE; // TODO: does the full buffer need to know this?
dt_imageio_retval_t ret = dt_imageio_open(&buffered_image, filename, buf); // TODO: color_space?
buf->loader_status = ret;
// might have been reallocated:
ASAN_UNPOISON_MEMORY_REGION(entry->data, dt_mipmap_buffer_dsc_size);
dsc = (struct dt_mipmap_buffer_dsc *)buf->cache_entry->data;
if(ret == DT_IMAGEIO_OK)
{
// swap back new image data:
dt_image_t *img = dt_image_cache_get(darktable.image_cache, imgid, 'w');
*img = buffered_image;
img->load_status = DT_IMAGEIO_OK;
// dt_print(DT_DEBUG_ALWAYS, "[mipmap read get] initializing full buffer img %u with %u %u -> %d %d (%p)\n",
// imgid, data[0], data[1], img->width, img->height, data);
// don't write xmp for this (we only changed db stuff):
dt_image_cache_write_release(darktable.image_cache, img, DT_IMAGE_CACHE_RELAXED);
}
else
{
// dt_print(DT_DEBUG_ALWAYS, "[mipmap read get] error loading image: %d\n", ret);
//
// we can only return a zero dimension buffer if the buffer has been allocated.
// in case dsc couldn't be allocated and points to the static buffer, it contains
// a dead/unsupported/error image already.
if(!_is_static_image((void *)dsc))
{
dsc->width = dsc->height = 0;
buf->iscale = 0.0f;
dsc->color_space = DT_COLORSPACE_NONE;
buf->color_space = DT_COLORSPACE_NONE;
}
// record the error code in the cache, so that later lookups know it actually failed
dt_image_t *img = dt_image_cache_get(darktable.image_cache, imgid, 'w');
img->load_status = ret;
// don't write xmp for this (we only changed db stuff):
dt_image_cache_write_release(darktable.image_cache, img, DT_IMAGE_CACHE_RELAXED);
}
}
else if(mip == DT_MIPMAP_F)
{
ASAN_UNPOISON_MEMORY_REGION(dsc + 1, dsc->size - sizeof(struct dt_mipmap_buffer_dsc));
_init_f(buf, (float *)(dsc + 1), &dsc->width, &dsc->height, &dsc->iscale, imgid);
}
else
{
// 8-bit thumbs
ASAN_UNPOISON_MEMORY_REGION(dsc + 1, dsc->size - sizeof(struct dt_mipmap_buffer_dsc));
_init_8((uint8_t *)(dsc + 1), &dsc->width, &dsc->height, &dsc->iscale, &buf->color_space, imgid, mip);
}
dsc->color_space = buf->color_space;
dsc->flags &= ~DT_MIPMAP_BUFFER_DSC_FLAG_GENERATE;
}
// image cache is leaving the write lock in place in case the image has been newly allocated.
// this leads to a slight increase in thread contention, so we opt for dropping the write lock
// and acquiring a read lock immediately after. since this opens a small window for other threads
// to get in between, we need to take some care to re-init cache entries and dsc.
// note that concurrencykit has rw locks that can be demoted from w->r without losing the lock in between.
if(mode == 'r')
{
entry->_lock_demoting = TRUE;