Cleanup: remove redundant doxygen \file argument
[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_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         ListBase *lbarray[MAX_LIBARRAY];
180         ID *id;
181         int a;
182
183         if (bmain->relations != NULL) {
184                 BKE_main_relations_free(bmain);
185         }
186
187         bmain->relations = MEM_mallocN(sizeof(*bmain->relations), __func__);
188         bmain->relations->id_used_to_user = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
189         bmain->relations->id_user_to_used = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
190         bmain->relations->entry_pool = BLI_mempool_create(sizeof(MainIDRelationsEntry), 128, 128, BLI_MEMPOOL_NOP);
191
192         for (a = set_listbasepointers(bmain, lbarray); a--; ) {
193                 for (id = lbarray[a]->first; id; id = id->next) {
194                         BKE_library_foreach_ID_link(NULL, id, main_relations_create_cb, bmain->relations, IDWALK_READONLY);
195                 }
196         }
197 }
198
199 void BKE_main_relations_free(Main *bmain)
200 {
201         if (bmain->relations) {
202                 if (bmain->relations->id_used_to_user) {
203                         BLI_ghash_free(bmain->relations->id_used_to_user, NULL, NULL);
204                 }
205                 if (bmain->relations->id_user_to_used) {
206                         BLI_ghash_free(bmain->relations->id_user_to_used, NULL, NULL);
207                 }
208                 BLI_mempool_destroy(bmain->relations->entry_pool);
209                 MEM_freeN(bmain->relations);
210                 bmain->relations = NULL;
211         }
212 }
213
214 /**
215  * Generates a raw .blend file thumbnail data from given image.
216  *
217  * \param bmain: If not NULL, also store generated data in this Main.
218  * \param img: ImBuf image to generate thumbnail data from.
219  * \return The generated .blend file raw thumbnail data.
220  */
221 BlendThumbnail *BKE_main_thumbnail_from_imbuf(Main *bmain, ImBuf *img)
222 {
223         BlendThumbnail *data = NULL;
224
225         if (bmain) {
226                 MEM_SAFE_FREE(bmain->blen_thumb);
227         }
228
229         if (img) {
230                 const size_t sz = BLEN_THUMB_MEMSIZE(img->x, img->y);
231                 data = MEM_mallocN(sz, __func__);
232
233                 IMB_rect_from_float(img);  /* Just in case... */
234                 data->width = img->x;
235                 data->height = img->y;
236                 memcpy(data->rect, img->rect, sz - sizeof(*data));
237         }
238
239         if (bmain) {
240                 bmain->blen_thumb = data;
241         }
242         return data;
243 }
244
245 /**
246  * Generates an image from raw .blend file thumbnail \a data.
247  *
248  * \param bmain: Use this bmain->blen_thumb data if given \a data is NULL.
249  * \param data: Raw .blend file thumbnail data.
250  * \return An ImBuf from given data, or NULL if invalid.
251  */
252 ImBuf *BKE_main_thumbnail_to_imbuf(Main *bmain, BlendThumbnail *data)
253 {
254         ImBuf *img = NULL;
255
256         if (!data && bmain) {
257                 data = bmain->blen_thumb;
258         }
259
260         if (data) {
261                 /* Note: we cannot use IMB_allocFromBuffer(), since it tries to dupalloc passed buffer, which will fail
262                  *       here (we do not want to pass the first two ints!). */
263                 img = IMB_allocImBuf((unsigned int)data->width, (unsigned int)data->height, 32, IB_rect | IB_metadata);
264                 memcpy(img->rect, data->rect, BLEN_THUMB_MEMSIZE(data->width, data->height) - sizeof(*data));
265         }
266
267         return img;
268 }
269
270 /**
271  * Generates an empty (black) thumbnail for given Main.
272  */
273 void BKE_main_thumbnail_create(struct Main *bmain)
274 {
275         MEM_SAFE_FREE(bmain->blen_thumb);
276
277         bmain->blen_thumb = MEM_callocN(BLEN_THUMB_MEMSIZE(BLEN_THUMB_SIZE, BLEN_THUMB_SIZE), __func__);
278         bmain->blen_thumb->width = BLEN_THUMB_SIZE;
279         bmain->blen_thumb->height = BLEN_THUMB_SIZE;
280 }
281
282 /**
283  * Return filepath of given \a main.
284  */
285 const char *BKE_main_blendfile_path(const Main *bmain)
286 {
287         return bmain->name;
288 }
289
290 /**
291  * Return filepath of global main (G_MAIN).
292  *
293  * \warning Usage is not recommended, you should always try to get a valid Main pointer from context...
294  */
295 const char *BKE_main_blendfile_path_from_global(void)
296 {
297         return BKE_main_blendfile_path(G_MAIN);
298 }
299
300 /**
301  * \return A pointer to the \a ListBase of given \a bmain for requested \a type ID type.
302  */
303 ListBase *which_libbase(Main *bmain, short type)
304 {
305         switch ((ID_Type)type) {
306                 case ID_SCE:
307                         return &(bmain->scene);
308                 case ID_LI:
309                         return &(bmain->library);
310                 case ID_OB:
311                         return &(bmain->object);
312                 case ID_ME:
313                         return &(bmain->mesh);
314                 case ID_CU:
315                         return &(bmain->curve);
316                 case ID_MB:
317                         return &(bmain->mball);
318                 case ID_MA:
319                         return &(bmain->mat);
320                 case ID_TE:
321                         return &(bmain->tex);
322                 case ID_IM:
323                         return &(bmain->image);
324                 case ID_LT:
325                         return &(bmain->latt);
326                 case ID_LA:
327                         return &(bmain->lamp);
328                 case ID_CA:
329                         return &(bmain->camera);
330                 case ID_IP:
331                         return &(bmain->ipo);
332                 case ID_KE:
333                         return &(bmain->key);
334                 case ID_WO:
335                         return &(bmain->world);
336                 case ID_SCR:
337                         return &(bmain->screen);
338                 case ID_VF:
339                         return &(bmain->vfont);
340                 case ID_TXT:
341                         return &(bmain->text);
342                 case ID_SPK:
343                         return &(bmain->speaker);
344                 case ID_LP:
345                         return &(bmain->lightprobe);
346                 case ID_SO:
347                         return &(bmain->sound);
348                 case ID_GR:
349                         return &(bmain->collection);
350                 case ID_AR:
351                         return &(bmain->armature);
352                 case ID_AC:
353                         return &(bmain->action);
354                 case ID_NT:
355                         return &(bmain->nodetree);
356                 case ID_BR:
357                         return &(bmain->brush);
358                 case ID_PA:
359                         return &(bmain->particle);
360                 case ID_WM:
361                         return &(bmain->wm);
362                 case ID_GD:
363                         return &(bmain->gpencil);
364                 case ID_MC:
365                         return &(bmain->movieclip);
366                 case ID_MSK:
367                         return &(bmain->mask);
368                 case ID_LS:
369                         return &(bmain->linestyle);
370                 case ID_PAL:
371                         return &(bmain->palettes);
372                 case ID_PC:
373                         return &(bmain->paintcurves);
374                 case ID_CF:
375                         return &(bmain->cachefiles);
376                 case ID_WS:
377                         return &(bmain->workspaces);
378         }
379         return NULL;
380 }
381
382 /**
383  * puts into array *lb pointers to all the ListBase structs in main,
384  * and returns the number of them as the function result. This is useful for
385  * generic traversal of all the blocks in a Main (by traversing all the
386  * lists in turn), without worrying about block types.
387  *
388  * \note MAX_LIBARRAY define should match this code */
389 int set_listbasepointers(Main *bmain, ListBase **lb)
390 {
391         /* BACKWARDS! also watch order of free-ing! (mesh<->mat), first items freed last.
392          * This is important because freeing data decreases usercounts of other datablocks,
393          * if this data is its self freed it can crash. */
394         lb[INDEX_ID_LI] = &(bmain->library);  /* Libraries may be accessed from pretty much any other ID... */
395         lb[INDEX_ID_IP] = &(bmain->ipo);
396         lb[INDEX_ID_AC] = &(bmain->action); /* moved here to avoid problems when freeing with animato (aligorith) */
397         lb[INDEX_ID_KE] = &(bmain->key);
398         lb[INDEX_ID_PAL] = &(bmain->palettes); /* referenced by gpencil, so needs to be before that to avoid crashes */
399         lb[INDEX_ID_GD] = &(bmain->gpencil); /* referenced by nodes, objects, view, scene etc, before to free after. */
400         lb[INDEX_ID_NT] = &(bmain->nodetree);
401         lb[INDEX_ID_IM] = &(bmain->image);
402         lb[INDEX_ID_TE] = &(bmain->tex);
403         lb[INDEX_ID_MA] = &(bmain->mat);
404         lb[INDEX_ID_VF] = &(bmain->vfont);
405
406         /* Important!: When adding a new object type,
407          * the specific data should be inserted here
408          */
409
410         lb[INDEX_ID_AR] = &(bmain->armature);
411
412         lb[INDEX_ID_CF] = &(bmain->cachefiles);
413         lb[INDEX_ID_ME] = &(bmain->mesh);
414         lb[INDEX_ID_CU] = &(bmain->curve);
415         lb[INDEX_ID_MB] = &(bmain->mball);
416
417         lb[INDEX_ID_LT] = &(bmain->latt);
418         lb[INDEX_ID_LA] = &(bmain->lamp);
419         lb[INDEX_ID_CA] = &(bmain->camera);
420
421         lb[INDEX_ID_TXT] = &(bmain->text);
422         lb[INDEX_ID_SO]  = &(bmain->sound);
423         lb[INDEX_ID_GR]  = &(bmain->collection);
424         lb[INDEX_ID_PAL] = &(bmain->palettes);
425         lb[INDEX_ID_PC]  = &(bmain->paintcurves);
426         lb[INDEX_ID_BR]  = &(bmain->brush);
427         lb[INDEX_ID_PA]  = &(bmain->particle);
428         lb[INDEX_ID_SPK] = &(bmain->speaker);
429         lb[INDEX_ID_LP]  = &(bmain->lightprobe);
430
431         lb[INDEX_ID_WO]  = &(bmain->world);
432         lb[INDEX_ID_MC]  = &(bmain->movieclip);
433         lb[INDEX_ID_SCR] = &(bmain->screen);
434         lb[INDEX_ID_OB]  = &(bmain->object);
435         lb[INDEX_ID_LS]  = &(bmain->linestyle); /* referenced by scenes */
436         lb[INDEX_ID_SCE] = &(bmain->scene);
437         lb[INDEX_ID_WS]  = &(bmain->workspaces); /* before wm, so it's freed after it! */
438         lb[INDEX_ID_WM]  = &(bmain->wm);
439         lb[INDEX_ID_MSK] = &(bmain->mask);
440
441         lb[INDEX_ID_NULL] = NULL;
442
443         return (MAX_LIBARRAY - 1);
444 }