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
21 * \ingroup bke
22 *
23 * Contains management of #Main database itself.
24 */
25
26 #include <string.h>
27
28 #include "MEM_guardedalloc.h"
29
30 #include "BLI_blenlib.h"
31 #include "BLI_ghash.h"
32 #include "BLI_mempool.h"
33 #include "BLI_threads.h"
34
35 #include "DNA_ID.h"
36
37 #include "BKE_global.h"
38 #include "BKE_lib_id.h"
39 #include "BKE_lib_query.h"
40 #include "BKE_main.h"
41
42 #include "IMB_imbuf.h"
43 #include "IMB_imbuf_types.h"
44
BKE_main_new(void)45 Main *BKE_main_new(void)
46 {
47 Main *bmain = MEM_callocN(sizeof(Main), "new main");
48 bmain->lock = MEM_mallocN(sizeof(SpinLock), "main lock");
49 BLI_spin_init((SpinLock *)bmain->lock);
50 return bmain;
51 }
52
BKE_main_free(Main * mainvar)53 void BKE_main_free(Main *mainvar)
54 {
55 /* also call when reading a file, erase all, etc */
56 ListBase *lbarray[MAX_LIBARRAY];
57 int a;
58
59 /* Since we are removing whole main, no need to bother 'properly'
60 * (and slowly) removing each ID from it. */
61 const int free_flag = (LIB_ID_FREE_NO_MAIN | LIB_ID_FREE_NO_UI_USER |
62 LIB_ID_FREE_NO_USER_REFCOUNT | 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:
80 BKE_id_free_ex(mainvar, id, free_flag, false);
81 break;
82 case 1:
83 BKE_id_free_ex(mainvar, id, free_flag, false);
84 break;
85 case 2:
86 BKE_id_free_ex(mainvar, id, free_flag, false);
87 break;
88 case 3:
89 BKE_id_free_ex(mainvar, id, free_flag, false);
90 break;
91 case 4:
92 BKE_id_free_ex(mainvar, id, free_flag, false);
93 break;
94 case 5:
95 BKE_id_free_ex(mainvar, id, free_flag, false);
96 break;
97 case 6:
98 BKE_id_free_ex(mainvar, id, free_flag, false);
99 break;
100 case 7:
101 BKE_id_free_ex(mainvar, id, free_flag, false);
102 break;
103 case 8:
104 BKE_id_free_ex(mainvar, id, free_flag, false);
105 break;
106 case 9:
107 BKE_id_free_ex(mainvar, id, free_flag, false);
108 break;
109 case 10:
110 BKE_id_free_ex(mainvar, id, free_flag, false);
111 break;
112 case 11:
113 BKE_id_free_ex(mainvar, id, free_flag, false);
114 break;
115 case 12:
116 BKE_id_free_ex(mainvar, id, free_flag, false);
117 break;
118 case 13:
119 BKE_id_free_ex(mainvar, id, free_flag, false);
120 break;
121 case 14:
122 BKE_id_free_ex(mainvar, id, free_flag, false);
123 break;
124 case 15:
125 BKE_id_free_ex(mainvar, id, free_flag, false);
126 break;
127 case 16:
128 BKE_id_free_ex(mainvar, id, free_flag, false);
129 break;
130 case 17:
131 BKE_id_free_ex(mainvar, id, free_flag, false);
132 break;
133 case 18:
134 BKE_id_free_ex(mainvar, id, free_flag, false);
135 break;
136 case 19:
137 BKE_id_free_ex(mainvar, id, free_flag, false);
138 break;
139 case 20:
140 BKE_id_free_ex(mainvar, id, free_flag, false);
141 break;
142 case 21:
143 BKE_id_free_ex(mainvar, id, free_flag, false);
144 break;
145 case 22:
146 BKE_id_free_ex(mainvar, id, free_flag, false);
147 break;
148 case 23:
149 BKE_id_free_ex(mainvar, id, free_flag, false);
150 break;
151 case 24:
152 BKE_id_free_ex(mainvar, id, free_flag, false);
153 break;
154 case 25:
155 BKE_id_free_ex(mainvar, id, free_flag, false);
156 break;
157 case 26:
158 BKE_id_free_ex(mainvar, id, free_flag, false);
159 break;
160 case 27:
161 BKE_id_free_ex(mainvar, id, free_flag, false);
162 break;
163 case 28:
164 BKE_id_free_ex(mainvar, id, free_flag, false);
165 break;
166 case 29:
167 BKE_id_free_ex(mainvar, id, free_flag, false);
168 break;
169 case 30:
170 BKE_id_free_ex(mainvar, id, free_flag, false);
171 break;
172 case 31:
173 BKE_id_free_ex(mainvar, id, free_flag, false);
174 break;
175 case 32:
176 BKE_id_free_ex(mainvar, id, free_flag, false);
177 break;
178 case 33:
179 BKE_id_free_ex(mainvar, id, free_flag, false);
180 break;
181 case 34:
182 BKE_id_free_ex(mainvar, id, free_flag, false);
183 break;
184 default:
185 BLI_assert(0);
186 break;
187 }
188 #endif
189 }
190 BLI_listbase_clear(lb);
191 }
192
193 if (mainvar->relations) {
194 BKE_main_relations_free(mainvar);
195 }
196
197 BLI_spin_end((SpinLock *)mainvar->lock);
198 MEM_freeN(mainvar->lock);
199 MEM_freeN(mainvar);
200 }
201
BKE_main_lock(struct Main * bmain)202 void BKE_main_lock(struct Main *bmain)
203 {
204 BLI_spin_lock((SpinLock *)bmain->lock);
205 }
206
BKE_main_unlock(struct Main * bmain)207 void BKE_main_unlock(struct Main *bmain)
208 {
209 BLI_spin_unlock((SpinLock *)bmain->lock);
210 }
211
main_relations_create_idlink_cb(LibraryIDLinkCallbackData * cb_data)212 static int main_relations_create_idlink_cb(LibraryIDLinkCallbackData *cb_data)
213 {
214 MainIDRelations *rel = cb_data->user_data;
215 ID *id_self = cb_data->id_self;
216 ID **id_pointer = cb_data->id_pointer;
217 const int cb_flag = cb_data->cb_flag;
218
219 if (*id_pointer) {
220 MainIDRelationsEntry *entry, **entry_p;
221
222 entry = BLI_mempool_alloc(rel->entry_pool);
223 if (BLI_ghash_ensure_p(rel->id_user_to_used, id_self, (void ***)&entry_p)) {
224 entry->next = *entry_p;
225 }
226 else {
227 entry->next = NULL;
228 }
229 entry->id_pointer = id_pointer;
230 entry->usage_flag = cb_flag;
231 *entry_p = entry;
232
233 entry = BLI_mempool_alloc(rel->entry_pool);
234 if (BLI_ghash_ensure_p(rel->id_used_to_user, *id_pointer, (void ***)&entry_p)) {
235 entry->next = *entry_p;
236 }
237 else {
238 entry->next = NULL;
239 }
240 entry->id_pointer = (ID **)id_self;
241 entry->usage_flag = cb_flag;
242 *entry_p = entry;
243 }
244
245 return IDWALK_RET_NOP;
246 }
247
248 /** Generate the mappings between used IDs and their users, and vice-versa. */
BKE_main_relations_create(Main * bmain,const short flag)249 void BKE_main_relations_create(Main *bmain, const short flag)
250 {
251 if (bmain->relations != NULL) {
252 BKE_main_relations_free(bmain);
253 }
254
255 bmain->relations = MEM_mallocN(sizeof(*bmain->relations), __func__);
256 bmain->relations->id_used_to_user = BLI_ghash_new(
257 BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
258 bmain->relations->id_user_to_used = BLI_ghash_new(
259 BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
260 bmain->relations->entry_pool = BLI_mempool_create(
261 sizeof(MainIDRelationsEntry), 128, 128, BLI_MEMPOOL_NOP);
262
263 ID *id;
264 FOREACH_MAIN_ID_BEGIN (bmain, id) {
265 const int idwalk_flag = IDWALK_READONLY |
266 ((flag & MAINIDRELATIONS_INCLUDE_UI) != 0 ? IDWALK_INCLUDE_UI : 0);
267 BKE_library_foreach_ID_link(
268 NULL, id, main_relations_create_idlink_cb, bmain->relations, idwalk_flag);
269 }
270 FOREACH_MAIN_ID_END;
271
272 bmain->relations->flag = flag;
273 }
274
BKE_main_relations_free(Main * bmain)275 void BKE_main_relations_free(Main *bmain)
276 {
277 if (bmain->relations) {
278 if (bmain->relations->id_used_to_user) {
279 BLI_ghash_free(bmain->relations->id_used_to_user, NULL, NULL);
280 }
281 if (bmain->relations->id_user_to_used) {
282 BLI_ghash_free(bmain->relations->id_user_to_used, NULL, NULL);
283 }
284 BLI_mempool_destroy(bmain->relations->entry_pool);
285 MEM_freeN(bmain->relations);
286 bmain->relations = NULL;
287 }
288 }
289
290 /**
291 * Remove an ID from the relations (the two entries for that ID, not the ID from entries in other
292 * IDs' relationships).
293 *
294 * Does not free any allocated memory.
295 * Allows to use those relations as a way to mark an ID as already processed, without requiring any
296 * additional tagging or GSet.
297 * Obviously, relations should be freed after use then, since this will make them fully invalid.
298 */
BKE_main_relations_ID_remove(Main * bmain,ID * id)299 void BKE_main_relations_ID_remove(Main *bmain, ID *id)
300 {
301 if (bmain->relations) {
302 /* Note: we do not free the entries from the mempool, those will be dealt with when finally
303 * freeing the whole relations. */
304 if (bmain->relations->id_used_to_user) {
305 BLI_ghash_remove(bmain->relations->id_used_to_user, id, NULL, NULL);
306 }
307 if (bmain->relations->id_user_to_used) {
308 BLI_ghash_remove(bmain->relations->id_user_to_used, id, NULL, NULL);
309 }
310 }
311 }
312
313 /**
314 * Create a GSet storing all IDs present in given \a bmain, by their pointers.
315 *
316 * \param gset: If not NULL, given GSet will be extended with IDs from given \a bmain,
317 * instead of creating a new one.
318 */
BKE_main_gset_create(Main * bmain,GSet * gset)319 GSet *BKE_main_gset_create(Main *bmain, GSet *gset)
320 {
321 if (gset == NULL) {
322 gset = BLI_gset_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
323 }
324
325 ID *id;
326 FOREACH_MAIN_ID_BEGIN (bmain, id) {
327 BLI_gset_add(gset, id);
328 }
329 FOREACH_MAIN_ID_END;
330 return gset;
331 }
332
333 /**
334 * Generates a raw .blend file thumbnail data from given image.
335 *
336 * \param bmain: If not NULL, also store generated data in this Main.
337 * \param img: ImBuf image to generate thumbnail data from.
338 * \return The generated .blend file raw thumbnail data.
339 */
BKE_main_thumbnail_from_imbuf(Main * bmain,ImBuf * img)340 BlendThumbnail *BKE_main_thumbnail_from_imbuf(Main *bmain, ImBuf *img)
341 {
342 BlendThumbnail *data = NULL;
343
344 if (bmain) {
345 MEM_SAFE_FREE(bmain->blen_thumb);
346 }
347
348 if (img) {
349 const size_t sz = BLEN_THUMB_MEMSIZE(img->x, img->y);
350 data = MEM_mallocN(sz, __func__);
351
352 IMB_rect_from_float(img); /* Just in case... */
353 data->width = img->x;
354 data->height = img->y;
355 memcpy(data->rect, img->rect, sz - sizeof(*data));
356 }
357
358 if (bmain) {
359 bmain->blen_thumb = data;
360 }
361 return data;
362 }
363
364 /**
365 * Generates an image from raw .blend file thumbnail \a data.
366 *
367 * \param bmain: Use this bmain->blen_thumb data if given \a data is NULL.
368 * \param data: Raw .blend file thumbnail data.
369 * \return An ImBuf from given data, or NULL if invalid.
370 */
BKE_main_thumbnail_to_imbuf(Main * bmain,BlendThumbnail * data)371 ImBuf *BKE_main_thumbnail_to_imbuf(Main *bmain, BlendThumbnail *data)
372 {
373 ImBuf *img = NULL;
374
375 if (!data && bmain) {
376 data = bmain->blen_thumb;
377 }
378
379 if (data) {
380 /* Note: we cannot use IMB_allocFromBuffer(), since it tries to dupalloc passed buffer,
381 * which will fail here (we do not want to pass the first two ints!). */
382 img = IMB_allocImBuf(
383 (unsigned int)data->width, (unsigned int)data->height, 32, IB_rect | IB_metadata);
384 memcpy(img->rect, data->rect, BLEN_THUMB_MEMSIZE(data->width, data->height) - sizeof(*data));
385 }
386
387 return img;
388 }
389
390 /**
391 * Generates an empty (black) thumbnail for given Main.
392 */
BKE_main_thumbnail_create(struct Main * bmain)393 void BKE_main_thumbnail_create(struct Main *bmain)
394 {
395 MEM_SAFE_FREE(bmain->blen_thumb);
396
397 bmain->blen_thumb = MEM_callocN(BLEN_THUMB_MEMSIZE(BLEN_THUMB_SIZE, BLEN_THUMB_SIZE), __func__);
398 bmain->blen_thumb->width = BLEN_THUMB_SIZE;
399 bmain->blen_thumb->height = BLEN_THUMB_SIZE;
400 }
401
402 /**
403 * Return filepath of given \a main.
404 */
BKE_main_blendfile_path(const Main * bmain)405 const char *BKE_main_blendfile_path(const Main *bmain)
406 {
407 return bmain->name;
408 }
409
410 /**
411 * Return filepath of global main #G_MAIN.
412 *
413 * \warning Usage is not recommended,
414 * you should always try to get a valid Main pointer from context...
415 */
BKE_main_blendfile_path_from_global(void)416 const char *BKE_main_blendfile_path_from_global(void)
417 {
418 return BKE_main_blendfile_path(G_MAIN);
419 }
420
421 /**
422 * \return A pointer to the \a ListBase of given \a bmain for requested \a type ID type.
423 */
which_libbase(Main * bmain,short type)424 ListBase *which_libbase(Main *bmain, short type)
425 {
426 switch ((ID_Type)type) {
427 case ID_SCE:
428 return &(bmain->scenes);
429 case ID_LI:
430 return &(bmain->libraries);
431 case ID_OB:
432 return &(bmain->objects);
433 case ID_ME:
434 return &(bmain->meshes);
435 case ID_CU:
436 return &(bmain->curves);
437 case ID_MB:
438 return &(bmain->metaballs);
439 case ID_MA:
440 return &(bmain->materials);
441 case ID_TE:
442 return &(bmain->textures);
443 case ID_IM:
444 return &(bmain->images);
445 case ID_LT:
446 return &(bmain->lattices);
447 case ID_LA:
448 return &(bmain->lights);
449 case ID_CA:
450 return &(bmain->cameras);
451 case ID_IP:
452 return &(bmain->ipo);
453 case ID_KE:
454 return &(bmain->shapekeys);
455 case ID_WO:
456 return &(bmain->worlds);
457 case ID_SCR:
458 return &(bmain->screens);
459 case ID_VF:
460 return &(bmain->fonts);
461 case ID_TXT:
462 return &(bmain->texts);
463 case ID_SPK:
464 return &(bmain->speakers);
465 case ID_LP:
466 return &(bmain->lightprobes);
467 case ID_SO:
468 return &(bmain->sounds);
469 case ID_GR:
470 return &(bmain->collections);
471 case ID_AR:
472 return &(bmain->armatures);
473 case ID_AC:
474 return &(bmain->actions);
475 case ID_NT:
476 return &(bmain->nodetrees);
477 case ID_BR:
478 return &(bmain->brushes);
479 case ID_PA:
480 return &(bmain->particles);
481 case ID_WM:
482 return &(bmain->wm);
483 case ID_GD:
484 return &(bmain->gpencils);
485 case ID_MC:
486 return &(bmain->movieclips);
487 case ID_MSK:
488 return &(bmain->masks);
489 case ID_LS:
490 return &(bmain->linestyles);
491 case ID_PAL:
492 return &(bmain->palettes);
493 case ID_PC:
494 return &(bmain->paintcurves);
495 case ID_CF:
496 return &(bmain->cachefiles);
497 case ID_WS:
498 return &(bmain->workspaces);
499 case ID_HA:
500 return &(bmain->hairs);
501 case ID_PT:
502 return &(bmain->pointclouds);
503 case ID_VO:
504 return &(bmain->volumes);
505 case ID_SIM:
506 return &(bmain->simulations);
507 }
508 return NULL;
509 }
510
511 /**
512 * puts into array *lb pointers to all the #ListBase structs in main,
513 * and returns the number of them as the function result. This is useful for
514 * generic traversal of all the blocks in a Main (by traversing all the
515 * lists in turn), without worrying about block types.
516 *
517 * \note #MAX_LIBARRAY define should match this code */
set_listbasepointers(Main * bmain,ListBase ** lb)518 int set_listbasepointers(Main *bmain, ListBase **lb)
519 {
520 /* BACKWARDS! also watch order of free-ing! (mesh<->mat), first items freed last.
521 * This is important because freeing data decreases user-counts of other data-blocks,
522 * if this data is its self freed it can crash. */
523
524 /* Libraries may be accessed from pretty much any other ID. */
525 lb[INDEX_ID_LI] = &(bmain->libraries);
526
527 lb[INDEX_ID_IP] = &(bmain->ipo);
528
529 /* Moved here to avoid problems when freeing with animato (aligorith). */
530 lb[INDEX_ID_AC] = &(bmain->actions);
531
532 lb[INDEX_ID_KE] = &(bmain->shapekeys);
533
534 /* Referenced by gpencil, so needs to be before that to avoid crashes. */
535 lb[INDEX_ID_PAL] = &(bmain->palettes);
536
537 /* Referenced by nodes, objects, view, scene etc, before to free after. */
538 lb[INDEX_ID_GD] = &(bmain->gpencils);
539
540 lb[INDEX_ID_NT] = &(bmain->nodetrees);
541 lb[INDEX_ID_IM] = &(bmain->images);
542 lb[INDEX_ID_TE] = &(bmain->textures);
543 lb[INDEX_ID_MA] = &(bmain->materials);
544 lb[INDEX_ID_VF] = &(bmain->fonts);
545
546 /* Important!: When adding a new object type,
547 * the specific data should be inserted here. */
548
549 lb[INDEX_ID_AR] = &(bmain->armatures);
550
551 lb[INDEX_ID_CF] = &(bmain->cachefiles);
552 lb[INDEX_ID_ME] = &(bmain->meshes);
553 lb[INDEX_ID_CU] = &(bmain->curves);
554 lb[INDEX_ID_MB] = &(bmain->metaballs);
555 lb[INDEX_ID_HA] = &(bmain->hairs);
556 lb[INDEX_ID_PT] = &(bmain->pointclouds);
557 lb[INDEX_ID_VO] = &(bmain->volumes);
558
559 lb[INDEX_ID_LT] = &(bmain->lattices);
560 lb[INDEX_ID_LA] = &(bmain->lights);
561 lb[INDEX_ID_CA] = &(bmain->cameras);
562
563 lb[INDEX_ID_TXT] = &(bmain->texts);
564 lb[INDEX_ID_SO] = &(bmain->sounds);
565 lb[INDEX_ID_GR] = &(bmain->collections);
566 lb[INDEX_ID_PAL] = &(bmain->palettes);
567 lb[INDEX_ID_PC] = &(bmain->paintcurves);
568 lb[INDEX_ID_BR] = &(bmain->brushes);
569 lb[INDEX_ID_PA] = &(bmain->particles);
570 lb[INDEX_ID_SPK] = &(bmain->speakers);
571 lb[INDEX_ID_LP] = &(bmain->lightprobes);
572
573 lb[INDEX_ID_WO] = &(bmain->worlds);
574 lb[INDEX_ID_MC] = &(bmain->movieclips);
575 lb[INDEX_ID_SCR] = &(bmain->screens);
576 lb[INDEX_ID_OB] = &(bmain->objects);
577 lb[INDEX_ID_LS] = &(bmain->linestyles); /* referenced by scenes */
578 lb[INDEX_ID_SCE] = &(bmain->scenes);
579 lb[INDEX_ID_WS] = &(bmain->workspaces); /* before wm, so it's freed after it! */
580 lb[INDEX_ID_WM] = &(bmain->wm);
581 lb[INDEX_ID_MSK] = &(bmain->masks);
582 lb[INDEX_ID_SIM] = &(bmain->simulations);
583
584 lb[INDEX_ID_NULL] = NULL;
585
586 return (MAX_LIBARRAY - 1);
587 }
588