Blender  V3.3
blendfile_loading_base_test.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2019 Blender Foundation. */
4 
5 #include "MEM_guardedalloc.h"
6 
7 #include "BKE_appdir.h"
8 #include "BKE_blender.h"
9 #include "BKE_callbacks.h"
10 #include "BKE_context.h"
11 #include "BKE_global.h"
12 #include "BKE_idtype.h"
13 #include "BKE_image.h"
14 #include "BKE_main.h"
15 #include "BKE_mball_tessellate.h"
16 #include "BKE_modifier.h"
17 #include "BKE_node.h"
18 #include "BKE_scene.h"
19 #include "BKE_vfont.h"
20 
21 #include "BLI_path_util.h"
22 #include "BLI_threads.h"
23 
24 #include "BLO_readfile.h"
25 
26 #include "DEG_depsgraph.h"
27 #include "DEG_depsgraph_build.h"
28 
29 #include "DNA_genfile.h" /* for DNA_sdna_current_init() */
31 
32 #include "IMB_imbuf.h"
33 
34 #include "ED_datafiles.h"
35 
36 #include "RNA_define.h"
37 
38 #include "WM_api.h"
39 #include "wm.h"
40 
41 #include "GHOST_Path-api.h"
42 
43 #include "CLG_log.h"
44 
46 {
47  testing::Test::SetUpTestCase();
48 
49  /* Minimal code to make loading a blendfile and constructing a depsgraph not crash, copied from
50  * main() in creator.c. */
51  CLG_init();
53 
56 
59  IMB_init();
62  RNA_init();
66 
67  G.background = true;
68  G.factory_startup = true;
69 
70  /* Allocate a dummy window manager. The real window manager will try and load Python scripts from
71  * the release directory, which it won't be able to find. */
72  ASSERT_EQ(G.main->wm.first, nullptr);
73  G.main->wm.first = MEM_callocN(sizeof(wmWindowManager), __func__);
74 }
75 
77 {
78  if (G.main->wm.first != nullptr) {
79  MEM_freeN(G.main->wm.first);
80  G.main->wm.first = nullptr;
81  }
82 
83  /* Copied from WM_exit_ex() in wm_init_exit.c, and cherry-picked those lines that match the
84  * allocation/initialization done in SetUpTestCase(). */
86  RNA_exit();
87 
92 
94 
97  CLG_exit();
98 
99  testing::Test::TearDownTestCase();
100 }
101 
103 {
105  depsgraph_free();
106  blendfile_free();
107 
108  testing::Test::TearDown();
109 }
110 
112 {
113  const std::string &test_assets_dir = blender::tests::flags_test_asset_dir();
114  if (test_assets_dir.empty()) {
115  return false;
116  }
117 
118  char abspath[FILENAME_MAX];
119  BLI_path_join(abspath, sizeof(abspath), test_assets_dir.c_str(), filepath, NULL);
120 
121  BlendFileReadReport bf_reports = {nullptr};
122  bfile = BLO_read_from_file(abspath, BLO_READ_SKIP_NONE, &bf_reports);
123  if (bfile == nullptr) {
124  ADD_FAILURE() << "Unable to load file '" << filepath << "' from test assets dir '"
125  << test_assets_dir << "'";
126  return false;
127  }
128  return true;
129 }
130 
132 {
133  if (bfile == nullptr) {
134  return;
135  }
136 
137  wmWindowManager *wm = static_cast<wmWindowManager *>(bfile->main->wm.first);
138  if (wm != nullptr) {
139  wm_close_and_free(nullptr, wm);
140  }
142  bfile = nullptr;
143 }
144 
146 {
148  bfile->main, bfile->curscene, bfile->cur_view_layer, depsgraph_evaluation_mode);
151 }
152 
154 {
155  if (depsgraph == nullptr) {
156  return;
157  }
159  depsgraph = nullptr;
160 }
void BKE_appdir_init(void)
Definition: appdir.c:88
void BKE_appdir_exit(void)
Definition: appdir.c:96
void BKE_tempdir_session_purge(void)
Definition: appdir.c:1159
Blender util stuff.
void BKE_blender_atexit(void)
Definition: blender.c:421
void BKE_blender_globals_init(void)
Definition: blender.c:161
void BKE_blender_free(void)
Definition: blender.c:58
void BKE_callback_global_init(void)
Definition: callbacks.c:100
void BKE_idtype_init(void)
Definition: idtype.c:106
void BKE_mball_cubeTable_free(void)
void BKE_modifier_init(void)
void BKE_node_system_init(void)
Definition: node.cc:4930
void BKE_scene_graph_update_tagged(struct Depsgraph *depsgraph, struct Main *bmain)
Definition: scene.cc:2648
void BKE_vfont_builtin_register(const void *mem, int size)
Definition: vfont.c:228
size_t BLI_path_join(char *__restrict dst, size_t dst_len, const char *path_first,...) ATTR_NONNULL(1
void BLI_threadapi_init(void)
Definition: threads.cc:125
void BLI_threadapi_exit(void)
Definition: threads.cc:130
external readfile function prototypes.
@ BLO_READ_SKIP_NONE
Definition: BLO_readfile.h:121
BlendFileData * BLO_read_from_file(const char *filepath, eBLOReadSkip skip_flags, struct BlendFileReadReport *reports)
void BLO_blendfiledata_free(BlendFileData *bfd)
void CLG_exit(void)
Definition: clog.c:703
void CLG_init(void)
Definition: clog.c:696
Depsgraph * DEG_graph_new(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer, eEvaluationMode mode)
Definition: depsgraph.cc:267
eEvaluationMode
Definition: DEG_depsgraph.h:44
void DEG_free_node_types(void)
void DEG_graph_free(Depsgraph *graph)
Definition: depsgraph.cc:295
void DEG_register_node_types(void)
void DEG_graph_build_from_view_layer(struct Depsgraph *graph)
blenloader genfile private function prototypes
void DNA_sdna_current_free(void)
Definition: dna_genfile.c:572
void DNA_sdna_current_init(void)
Definition: dna_genfile.c:561
const char datatoc_bfont_pfb[]
int datatoc_bfont_pfb_size
GHOST_TSuccess GHOST_DisposeSystemPaths(void)
void IMB_init(void)
Definition: module.c:16
Read Guarded memory(de)allocation.
virtual void depsgraph_create(eEvaluationMode depsgraph_evaluation_mode)
bool blendfile_load(const char *filepath)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
#define G(x, y, z)
void RNA_init(void)
Definition: rna_access.c:65
void RNA_exit(void)
Definition: rna_access.c:89
struct Scene * curscene
Definition: BLO_readfile.h:64
struct ViewLayer * cur_view_layer
Definition: BLO_readfile.h:65
struct Main * main
Definition: BLO_readfile.h:56
void * first
Definition: DNA_listBase.h:31
ListBase wm
Definition: BKE_main.h:197
void wm_close_and_free(bContext *C, wmWindowManager *wm)
Definition: wm.c:554