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