forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixelpipe_cache.c
More file actions
220 lines (197 loc) · 6.58 KB
/
Copy pathpixelpipe_cache.c
File metadata and controls
220 lines (197 loc) · 6.58 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
/*
This file is part of darktable,
copyright (c) 2009--2010 johannes hanika.
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 "develop/pixelpipe_cache.h"
#include "develop/pixelpipe_hb.h"
#include "libs/lib.h"
#include <stdlib.h>
// TODO: make cache global (needs to be thread safe then)
// plan:
// - look at mipmap_cache.c, for the full buffer allocs
// - do that, but for `large' and `regular' buffers (full + export/dr mode), so 2 caches
// (in fact, maybe 3, one for preview pipes?)
// - have at most 3 read locks all the time per pipe, get them at create time
// ping, pong, and priority buffer (focused plugin)
// - drop read by the time another is requested (with priority, drop that, or alternating ping and pong?)
int dt_dev_pixelpipe_cache_init(dt_dev_pixelpipe_cache_t *cache, int entries, int size)
{
cache->entries = entries;
cache->data = (void **)malloc(sizeof(void *)*entries);
cache->size = (size_t *)malloc(sizeof(size_t)*entries);
cache->hash = (uint64_t *)malloc(sizeof(uint64_t)*entries);
cache->used = (int32_t *)malloc(sizeof(int32_t)*entries);
memset(cache->data,0,sizeof(void *)*entries);
for(int k=0; k<entries; k++)
{
cache->data[k] = (void *)dt_alloc_align(16, size);
if(!cache->data[k])
goto alloc_memory_fail;
cache->size[k] = size;
#ifdef _DEBUG
memset(cache->data[k], 0x5d, size);
#endif
cache->hash[k] = -1;
cache->used[k] = 0;
}
cache->queries = cache->misses = 0;
return 1;
alloc_memory_fail:
for(int k=0; k<entries; k++)
{
if(cache->data[k])
free(cache->data[k]);
}
free(cache->data);
free(cache->size);
free(cache->hash);
free(cache->used);
return 0;
}
void dt_dev_pixelpipe_cache_cleanup(dt_dev_pixelpipe_cache_t *cache)
{
for(int k=0; k<cache->entries; k++) free(cache->data[k]);
free(cache->data);
free(cache->hash);
free(cache->used);
free(cache->size);
}
uint64_t dt_dev_pixelpipe_cache_hash(int imgid, const dt_iop_roi_t *roi, dt_dev_pixelpipe_t *pipe, int module)
{
// bernstein hash (djb2)
uint64_t hash = 5381 + imgid;
// go through all modules up to module and compute a weird hash using the operation and params.
GList *pieces = pipe->nodes;
for(int k=0; k<module&&pieces; k++)
{
dt_dev_pixelpipe_iop_t *piece = (dt_dev_pixelpipe_iop_t *)pieces->data;
dt_develop_t *dev = piece->module->dev;
if(!(dev->gui_module && (dev->gui_module->operation_tags_filter() & piece->module->operation_tags())))
{
hash = ((hash << 5) + hash) ^ piece->hash;
if(piece->module->request_color_pick)
{
if(darktable.lib->proxy.colorpicker.size)
{
const char *str = (const char *)piece->module->color_picker_box;
for(int i=0; i<sizeof(float)*4; i++) hash = ((hash << 5) + hash) ^ str[i];
}
else
{
const char *str = (const char *)piece->module->color_picker_point;
for(int i=0; i<sizeof(float)*2; i++) hash = ((hash << 5) + hash) ^ str[i];
}
}
}
pieces = g_list_next(pieces);
}
// also add scale, x and y:
const char *str = (const char *)roi;
for(int i=0; i<sizeof(dt_iop_roi_t); i++) hash = ((hash << 5) + hash) ^ str[i];
return hash;
}
int dt_dev_pixelpipe_cache_available(dt_dev_pixelpipe_cache_t *cache, const uint64_t hash)
{
// search for hash in cache
for(int k=0; k<cache->entries; k++) if(cache->hash[k] == hash) return 1;
return 0;
}
int dt_dev_pixelpipe_cache_get_important(dt_dev_pixelpipe_cache_t *cache, const uint64_t hash, const size_t size, void **data)
{
return dt_dev_pixelpipe_cache_get_weighted(cache, hash, size, data, -cache->entries);
}
int dt_dev_pixelpipe_cache_get(dt_dev_pixelpipe_cache_t *cache, const uint64_t hash, const size_t size, void **data)
{
return dt_dev_pixelpipe_cache_get_weighted(cache, hash, size, data, 0);
}
int dt_dev_pixelpipe_cache_get_weighted(dt_dev_pixelpipe_cache_t *cache, const uint64_t hash, const size_t size, void **data, int weight)
{
cache->queries ++;
*data = NULL;
int max_used = -1, max = 0;
size_t sz = 0;
for(int k=0; k<cache->entries; k++)
{
// search for hash in cache
if(cache->used[k] > max_used)
{
max_used = cache->used[k];
max = k;
}
cache->used[k]++; // age all entries
if(cache->hash[k] == hash)
{
*data = cache->data[k];
sz = cache->size[k];
cache->used[k] = weight; // this is the MRU entry
}
}
if(!*data || sz < size)
{
// kill LRU entry
// printf("[pixelpipe_cache_get] hash not found, returning slot %d/%d age %d\n", max, cache->entries, weight);
if(cache->size[max] < size)
{
free(cache->data[max]);
cache->data[max] = (void *)dt_alloc_align(16, size);
cache->size[max] = size;
}
*data = cache->data[max];
cache->hash[max] = hash;
cache->used[max] = weight;
cache->misses++;
return 1;
}
else return 0;
}
void dt_dev_pixelpipe_cache_flush(dt_dev_pixelpipe_cache_t *cache)
{
for(int k=0; k<cache->entries; k++)
{
cache->hash[k] = -1;
cache->used[k] = 0;
}
}
void dt_dev_pixelpipe_cache_reweight(dt_dev_pixelpipe_cache_t *cache, void *data)
{
for(int k=0; k<cache->entries; k++)
{
if(cache->data[k] == data)
{
cache->used[k] = -cache->entries;
}
}
}
void dt_dev_pixelpipe_cache_invalidate(dt_dev_pixelpipe_cache_t *cache, void *data)
{
for(int k=0; k<cache->entries; k++)
{
if(cache->data[k] == data)
{
cache->hash[k] = -1;
}
}
}
void dt_dev_pixelpipe_cache_print(dt_dev_pixelpipe_cache_t *cache)
{
for(int k=0; k<cache->entries; k++)
{
printf("pixelpipe cacheline %d ", k);
printf("used %d by %"PRIu64"", cache->used[k], cache->hash[k]);
printf("\n");
}
printf("cache hit rate so far: %.3f\n", (cache->queries - cache->misses)/(float)cache->queries);
}
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-space on;