Cleanup: replace Main ID's foreach functions by macros.
[blender.git] / source / blender / blenkernel / intern / main.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19
20 /** \file \ingroup bke
21  *
22  * Contains management of #Main database itself.
23  */
24
25 #include <string.h>
26
27 #include "MEM_guardedalloc.h"
28
29 #include "BLI_blenlib.h"
30 #include "BLI_ghash.h"
31 #include "BLI_mempool.h"
32 #include "BLI_threads.h"
33
34 #include "DNA_ID.h"
35
36 #include "BKE_global.h"
37 #include "BKE_library.h"
38 #include "BKE_library_query.h"
39 #include "BKE_main.h"
40
41 #include "IMB_imbuf.h"
42 #include "IMB_imbuf_types.h"
43
44 Main *BKE_main_new(void)
45 {
46         Main *bmain = MEM_callocN(sizeof(Main), "new main");
47         bmain->lock = MEM_mallocN(sizeof(SpinLock), "main lock");
48         BLI_spin_init((SpinLock *)bmain->lock);
49         return bmain;
50 }
51
52 void BKE_main_free(Main *mainvar)
53 {
54         /* also call when reading a file, erase all, etc */
55         ListBase *lbarray[MAX_LIBARRAY];
56         int a;
57
58         /* Since we are removing whole main, no need to bother 'properly' (and slowly) removing each ID from it. */
59         const int free_flag = (LIB_ID_FREE_NO_MAIN |
60                                LIB_ID_FREE_NO_UI_USER |
61                                LIB_ID_FREE_NO_USER_REFCOUNT |
62                                LIB_ID_FREE_NO_DEG_TAG);
63
64         MEM_SAFE_FREE(mainvar->blen_thumb);
65
66         a = set_listbasepointers(mainvar, lbarray);
67         while (a--) {
68                 ListBase *lb = lbarray[a];
69                 ID *id, *id_next;
70
71                 for (id = lb->first; id != NULL; id = id_next) {
72                         id_next = id->next;
73 #if 1
74                         BKE_id_free_ex(mainvar, id, free_flag, false);
75 #else
76                         /* errors freeing ID's can be hard to track down,
77                          * enable this so valgrind will give the line number in its error log */
78                         switch (a) {
79                                 case   0: BKE_id_free_ex(mainvar, id, free_flag, false); break;
80                                 case   1: BKE_id_free_ex(mainvar, id, free_flag, false); break;
81                                 case   2: BKE_id_free_ex(mainvar, id, free_flag, false); break;
82                                 case   3: BKE_id_free_ex(mainvar, id, free_flag, false); break;
83                                 case   4: BKE_id_free_ex(mainvar, id, free_flag, false); break;
84                                 case   5: BKE_id_free_ex(mainvar, id, free_flag, false); break;
85                                 case   6: BKE_id_free_ex(mainvar, id, free_flag, false); break;
86                                 case   7: BKE_id_free_ex(mainvar, id, free_flag, false); break;
87                                 case   8: BKE_id_free_ex(mainvar, id, free_flag, false); break;
88                                 case   9: BKE_id_free_ex(mainvar, id, free_flag, false); break;
89                                 case  10: BKE_id_free_ex(mainvar, id, free_flag, false); break;
90                                 case  11: BKE_id_free_ex(mainvar, id, free_flag, false); break;
91                                 case  12: BKE_id_free_ex(mainvar, id, free_flag, false); break;
92                                 case  13: BKE_id_free_ex(mainvar, id, free_flag, false); break;
93                                 case  14: BKE_id_free_ex(mainvar, id, free_flag, false); break;
94                                 case  15: BKE_id_free_ex(mainvar, id, free_flag, false); break;
95                                 case  16: BKE_id_free_ex(mainvar, id, free_flag, false); break;
96                                 case  17: BKE_id_free_ex(mainvar, id, free_flag, false); break;
97                                 case  18: BKE_id_free_ex(mainvar, id, free_flag, false); break;
98                                 case  19: BKE_id_free_ex(mainvar, id, free_flag, false); break;
99                                 case  20: BKE_id_free_ex(mainvar, id, free_flag, false); break;
100                                 case  21: BKE_id_free_ex(mainvar, id, free_flag, false); break;
101                                 case  22: BKE_id_free_ex(mainvar, id, free_flag, false); break;
102                                 case  23: BKE_id_free_ex(mainvar, id, free_flag, false); break;
103                                 case  24: BKE_id_free_ex(mainvar, id, free_flag, false); break;
104                                 case  25: BKE_id_free_ex(mainvar, id, free_flag, false); break;
105                                 case  26: BKE_id_free_ex(mainvar, id, free_flag, false); break;
106                                 case  27: BKE_id_free_ex(mainvar, id, free_flag, false); break;
107                                 case  28: BKE_id_free_ex(mainvar, id, free_flag, false); break;
108                                 case  29: BKE_id_free_ex(mainvar, id, free_flag, false); break;
109                                 case  30: BKE_id_free_ex(mainvar, id, free_flag, false); break;
110                                 case  31: BKE_id_free_ex(mainvar, id, free_flag, false); break;
111                                 case  32: BKE_id_free_ex(mainvar, id, free_flag, false); break;
112                                 case  33: BKE_id_free_ex(mainvar, id, free_flag, false); break;
113                                 case  34: BKE_id_free_ex(mainvar, id, free_flag, false); break;
114                                 default:
115                                         BLI_assert(0);
116                                         break;
117                         }
118 #endif
119                 }
120                 BLI_listbase_clear(lb);
121         }
122
123         if (mainvar->relations) {
124                 BKE_main_relations_free(mainvar);
125         }
126
127         BLI_spin_end((SpinLock *)mainvar->lock);
128         MEM_freeN(mainvar->lock);
129         MEM_freeN(mainvar);
130 }
131
132 void BKE_main_lock(struct Main *bmain)
133 {
134         BLI_spin_lock((SpinLock *) bmain->lock);
135 }
136
137 void BKE_main_unlock(struct Main *bmain)
138 {
139         BLI_spin_unlock((SpinLock *) bmain->lock);
140 }
141
142
143 static int main_relations_create_idlink_cb(void *user_data, ID *id_self, ID **id_pointer, int cb_flag)
144 {
145         MainIDRelations *rel = user_data;
146
147         if (*id_pointer) {
148                 MainIDRelationsEntry *entry, **entry_p;
149
150                 entry = BLI_mempool_alloc(rel->entry_pool);
151                 if (BLI_ghash_ensure_p(rel->id_user_to_used, id_self, (void ***)&entry_p)) {
152                         entry->next = *entry_p;
153                 }
154                 else {
155                         entry->next = NULL;
156                 }
157                 entry->id_pointer = id_pointer;
158                 entry->usage_flag = cb_flag;
159                 *entry_p = entry;
160
161                 entry = BLI_mempool_alloc(rel->entry_pool);
162                 if (BLI_ghash_ensure_p(rel->id_used_to_user, *id_pointer, (void ***)&entry_p)) {
163                         entry->next = *entry_p;
164                 }
165                 else {
166                         entry->next = NULL;
167                 }
168                 entry->id_pointer = (ID **)id_self;
169                 entry->usage_flag = cb_flag;
170                 *entry_p = entry;
171         }
172
173         return IDWALK_RET_NOP;
174 }
175
176 /** Generate the mappings between used IDs and their users, and vice-versa. */
177 void BKE_main_relations_create(Main *bmain)
178 {
179         if (bmain->relations != NULL) {
180                 BKE_main_relations_free(bmain);
181         }
182
183         bmain->relations = MEM_mallocN(sizeof(*bmain->relations), __func__);
184         bmain->relations->id_used_to_user = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
185         bmain->relations->id_user_to_used = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
186         bmain->relations->entry_pool = BLI_mempool_create(sizeof(MainIDRelationsEntry), 128, 128, BLI_MEMPOOL_NOP);
187
188         ID *id;
189         FOREACH_MAIN_ID_BEGIN(bmain, id)
190         {
191                 BKE_library_foreach_ID_link(NULL, id, main_relations_create_idlink_cb, bmain->relations, IDWALK_READONLY);
192         }
193         FOREACH_MAIN_ID_END;
194 }
195
196 void BKE_main_relations_free(Main *bmain)
197 {
198         if (bmain->relations) {
199                 if (bmain->relations->id_used_to_user) {
200                         BLI_ghash_free(bmain->relations->id_used_to_user, NULL, NULL);
201                 }
202                 if (bmain->relations->id_user_to_used) {
203                         BLI_ghash_free(bmain->relations->id_user_to_used, NULL, NULL);
204                 }
205                 BLI_mempool_destroy(bmain->relations->entry_pool);
206                 MEM_freeN(bmain->relations);
207                 bmain->relations = NULL;
208         }
209 }
210
211 /**
212  * Create a GSet storing all IDs present in given \a bmain, by their pointers.
213  *
214  * \param gset: If not NULL, given GSet will be extended with IDs from given \a bmain, instead of creating a new one.
215  */
216 GSet *BKE_main_gset_create(Main *bmain, GSet *gset)
217 {
218         if (gset == NULL) {
219                 gset = BLI_gset_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
220         }
221
222         ID *id;
223         FOREACH_MAIN_ID_BEGIN(bmain, id)
224         {
225                 BLI_gset_add(gset, id);
226         }
227         FOREACH_MAIN_ID_END;
228         return gset;
229 }
230
231 /**
232  * Generates a raw .blend file thumbnail data from given image.
233  *
234  * \param bmain: If not NULL, also store generated data in this Main.
235  * \param img: ImBuf image to generate thumbnail data from.
236  * \return The generated .blend file raw thumbnail data.
237  */
238 BlendThumbnail *BKE_main_thumbnail_from_imbuf(Main *bmain, ImBuf *img)
239 {
240         BlendThumbnail *data = NULL;
241
242         if (bmain) {
243                 MEM_SAFE_FREE(bmain->blen_thumb);
244         }
245
246         if (img) {
247                 const size_t sz = BLEN_THUMB_MEMSIZE(img->x, img->y);
248                 data = MEM_mallocN(sz, __func__);
249
250                 IMB_rect_from_float(img);  /* Just in case... */
251                 data->width = img->x;
252                 data->height = img->y;
253                 memcpy(data->rect, img->rect, sz - sizeof(*data));
254         }
255
256         if (bmain) {
257                 bmain->blen_thumb = data;
258         }
259         return data;
260 }
261
262 /**
263  * Generates an image from raw .blend file thumbnail \a data.
264  *
265  * \param bmain: Use this bmain->blen_thumb data if given \a data is NULL.
266  * \param data: Raw .blend file thumbnail data.
267  * \return An ImBuf from given data, or NULL if invalid.
268  */
269 ImBuf *BKE_main_thumbnail_to_imbuf(Main *bmain, BlendThumbnail *data)
270 {
271         ImBuf *img = NULL;
272
273         if (!data && bmain) {
274                 data = bmain->blen_thumb;
275         }
276
277         if (data) {
278                 /* Note: we cannot use IMB_allocFromBuffer(), since it tries to dupalloc passed buffer, which will fail
279                  *       here (we do not want to pass the first two ints!). */
280                 img = IMB_allocImBuf((unsigned int)data->width, (unsigned int)data->height, 32, IB_rect | IB_metadata);
281                 memcpy(img->rect, data->rect, BLEN_THUMB_MEMSIZE(data->width, data->height) - sizeof(*data));
282         }
283
284         return img;
285 }
286
287 /**
288  * Generates an empty (black) thumbnail for given Main.
289  */
290 void BKE_main_thumbnail_create(struct Main *bmain)
291 {
292         MEM_SAFE_FREE(bmain->blen_thumb);
293
294         bmain->blen_thumb = MEM_callocN(BLEN_THUMB_MEMSIZE(BLEN_THUMB_SIZE, BLEN_THUMB_SIZE), __func__);
295         bmain->blen_thumb->width = BLEN_THUMB_SIZE;
296         bmain->blen_thumb->height = BLEN_THUMB_SIZE;
297 }
298
299 /**
300  * Return filepath of given \a main.
301  */
302 const char *BKE_main_blendfile_path(const Main *bmain)
303 {
304         return bmain->name;
305 }
306
307 /**
308  * Return filepath of global main #G_MAIN.
309  *
310  * \warning Usage is not recommended, you should always try to get a valid Main pointer from context...
311  */
312 const char *BKE_main_blendfile_path_from_global(void)
313 {
314         return BKE_main_blendfile_path(G_MAIN);
315 }
316
317 /**
318  * \return A pointer to the \a ListBase of given \a bmain for requested \a type ID type.
319  */
320 ListBase *which_libbase(Main *bmain, short type)
321 {
322         switch ((ID_Type)type) {
323                 case ID_SCE:
324                         return &(bmain->scene);
325                 case ID_LI:
326                         return &(bmain->library);
327                 case ID_OB:
328                         return &(bmain->object);
329                 case ID_ME:
330                         return &(bmain->mesh);
331                 case ID_CU:
332                         return &(bmain->curve);
333                 case ID_MB:
334                         return &(bmain->mball);
335                 case ID_MA:
336                         return &(bmain->mat);
337                 case ID_TE:
338                         return &(bmain->tex);
339                 case ID_IM:
340                         return &(bmain->image);
341                 case ID_LT:
342                         return &(bmain->latt);
343                 case ID_LA:
344                         return &(bmain->lamp);
345                 case ID_CA:
346                         return &(bmain->camera);
347                 case ID_IP:
348                         return &(bmain->ipo);
349                 case ID_KE:
350                         return &(bmain->key);
351                 case ID_WO:
352                         return &(bmain->world);
353                 case ID_SCR:
354                         return &(bmain->screen);
355                 case ID_VF:
356                         return &(bmain->vfont);
357                 case ID_TXT:
358                         return &(bmain->text);
359                 case ID_SPK:
360                         return &(bmain->speaker);
361                 case ID_LP:
362                         return &(bmain->lightprobe);
363                 case ID_SO:
364                         return &(bmain->sound);
365                 case ID_GR:
366                         return &(bmain->collection);
367                 case ID_AR:
368                         return &(bmain->armature);
369                 case ID_AC:
370                         return &(bmain->action);
371                 case ID_NT:
372                         return &(bmain->nodetree);
373                 case ID_BR:
374                         return &(bmain->brush);
375                 case ID_PA:
376                         return &(bmain->particle);
377                 case ID_WM:
378                         return &(bmain->wm);
379                 case ID_GD:
380                         return &(bmain->gpencil);
381                 case ID_MC:
382                         return &(bmain->movieclip);
383                 case ID_MSK:
384                         return &(bmain->mask);
385                 case ID_LS:
386                         return &(bmain->linestyle);
387                 case ID_PAL:
388                         return &(bmain->palettes);
389                 case ID_PC:
390                         return &(bmain->paintcurves);
391                 case ID_CF:
392                         return &(bmain->cachefiles);
393                 case ID_WS:
394                         return &(bmain->workspaces);
395         }
396         return NULL;
397 }
398
399 /**
400  * puts into array *lb pointers to all the #ListBase structs in main,
401  * and returns the number of them as the function result. This is useful for
402  * generic traversal of all the blocks in a Main (by traversing all the
403  * lists in turn), without worrying about block types.
404  *
405  * \note #MAX_LIBARRAY define should match this code */
406 int set_listbasepointers(Main *bmain, ListBase **lb)
407 {
408         /* BACKWARDS! also watch order of free-ing! (mesh<->mat), first items freed last.
409          * This is important because freeing data decreases usercounts of other datablocks,
410          * if this data is its self freed it can crash. */
411         lb[INDEX_ID_LI] = &(bmain->library);  /* Libraries may be accessed from pretty much any other ID... */
412         lb[INDEX_ID_IP] = &(bmain->ipo);
413         lb[INDEX_ID_AC] = &(bmain->action); /* moved here to avoid problems when freeing with animato (aligorith) */
414         lb[INDEX_ID_KE] = &(bmain->key);
415         lb[INDEX_ID_PAL] = &(bmain->palettes); /* referenced by gpencil, so needs to be before that to avoid crashes */
416         lb[INDEX_ID_GD] = &(bmain->gpencil); /* referenced by nodes, objects, view, scene etc, before to free after. */
417         lb[INDEX_ID_NT] = &(bmain->nodetree);
418         lb[INDEX_ID_IM] = &(bmain->image);
419         lb[INDEX_ID_TE] = &(bmain->tex);
420         lb[INDEX_ID_MA] = &(bmain->mat);
421         lb[INDEX_ID_VF] = &(bmain->vfont);
422
423         /* Important!: When adding a new object type,
424          * the specific data should be inserted here
425          */
426
427         lb[INDEX_ID_AR] = &(bmain->armature);
428
429         lb[INDEX_ID_CF] = &(bmain->cachefiles);
430         lb[INDEX_ID_ME] = &(bmain->mesh);
431         lb[INDEX_ID_CU] = &(bmain->curve);
432         lb[INDEX_ID_MB] = &(bmain->mball);
433
434         lb[INDEX_ID_LT] = &(bmain->latt);
435         lb[INDEX_ID_LA] = &(bmain->lamp);
436         lb[INDEX_ID_CA] = &(bmain->camera);
437
438         lb[INDEX_ID_TXT] = &(bmain->text);
439         lb[INDEX_ID_SO]  = &(bmain->sound);
440         lb[INDEX_ID_GR]  = &(bmain->collection);
441         lb[INDEX_ID_PAL] = &(bmain->palettes);
442         lb[INDEX_ID_PC]  = &(bmain->paintcurves);
443         lb[INDEX_ID_BR]  = &(bmain->brush);
444         lb[INDEX_ID_PA]  = &(bmain->particle);
445         lb[INDEX_ID_SPK] = &(bmain->speaker);
446         lb[INDEX_ID_LP]  = &(bmain->lightprobe);
447
448         lb[INDEX_ID_WO]  = &(bmain->world);
449         lb[INDEX_ID_MC]  = &(bmain->movieclip);
450         lb[INDEX_ID_SCR] = &(bmain->screen);
451         lb[INDEX_ID_OB]  = &(bmain->object);
452         lb[INDEX_ID_LS]  = &(bmain->linestyle); /* referenced by scenes */
453         lb[INDEX_ID_SCE] = &(bmain->scene);
454         lb[INDEX_ID_WS]  = &(bmain->workspaces); /* before wm, so it's freed after it! */
455         lb[INDEX_ID_WM]  = &(bmain->wm);
456         lb[INDEX_ID_MSK] = &(bmain->mask);
457
458         lb[INDEX_ID_NULL] = NULL;
459
460         return (MAX_LIBARRAY - 1);
461 }