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