Blender  V2.93
cachefile.c
Go to the documentation of this file.
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) 2016 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include <string.h>
25 
26 #include "DNA_anim_types.h"
27 #include "DNA_cachefile_types.h"
28 #include "DNA_constraint_types.h"
29 #include "DNA_object_types.h"
30 #include "DNA_scene_types.h"
31 
32 #include "BLI_fileops.h"
33 #include "BLI_ghash.h"
34 #include "BLI_listbase.h"
35 #include "BLI_path_util.h"
36 #include "BLI_string.h"
37 #include "BLI_threads.h"
38 #include "BLI_utildefines.h"
39 
40 #include "BLT_translation.h"
41 
42 #include "BKE_anim_data.h"
43 #include "BKE_cachefile.h"
44 #include "BKE_idtype.h"
45 #include "BKE_lib_id.h"
46 #include "BKE_main.h"
47 #include "BKE_modifier.h"
48 #include "BKE_scene.h"
49 
50 #include "DEG_depsgraph_query.h"
51 
52 #include "BLO_read_write.h"
53 
54 #ifdef WITH_ALEMBIC
55 # include "ABC_alembic.h"
56 #endif
57 
58 static void cachefile_handle_free(CacheFile *cache_file);
59 
60 static void cache_file_init_data(ID *id)
61 {
62  CacheFile *cache_file = (CacheFile *)id;
63 
64  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(cache_file, id));
65 
66  cache_file->scale = 1.0f;
68  BLI_strncpy(cache_file->velocity_name, ".velocities", sizeof(cache_file->velocity_name));
69 }
70 
71 static void cache_file_copy_data(Main *UNUSED(bmain),
72  ID *id_dst,
73  const ID *id_src,
74  const int UNUSED(flag))
75 {
76  CacheFile *cache_file_dst = (CacheFile *)id_dst;
77  const CacheFile *cache_file_src = (const CacheFile *)id_src;
78 
79  cache_file_dst->handle = NULL;
80  cache_file_dst->handle_readers = NULL;
81  BLI_duplicatelist(&cache_file_dst->object_paths, &cache_file_src->object_paths);
82 }
83 
84 static void cache_file_free_data(ID *id)
85 {
86  CacheFile *cache_file = (CacheFile *)id;
87  cachefile_handle_free(cache_file);
88  BLI_freelistN(&cache_file->object_paths);
89 }
90 
91 static void cache_file_blend_write(BlendWriter *writer, ID *id, const void *id_address)
92 {
93  CacheFile *cache_file = (CacheFile *)id;
94  if (cache_file->id.us > 0 || BLO_write_is_undo(writer)) {
95  /* Clean up, important in undo case to reduce false detection of changed datablocks. */
96  BLI_listbase_clear(&cache_file->object_paths);
97  cache_file->handle = NULL;
98  memset(cache_file->handle_filepath, 0, sizeof(cache_file->handle_filepath));
99  cache_file->handle_readers = NULL;
100 
101  BLO_write_id_struct(writer, CacheFile, id_address, &cache_file->id);
102  BKE_id_blend_write(writer, &cache_file->id);
103 
104  if (cache_file->adt) {
105  BKE_animdata_blend_write(writer, cache_file->adt);
106  }
107  }
108 }
109 
111 {
112  CacheFile *cache_file = (CacheFile *)id;
113  BLI_listbase_clear(&cache_file->object_paths);
114  cache_file->handle = NULL;
115  cache_file->handle_filepath[0] = '\0';
116  cache_file->handle_readers = NULL;
117 
118  /* relink animdata */
119  BLO_read_data_address(reader, &cache_file->adt);
120  BKE_animdata_blend_read_data(reader, cache_file->adt);
121 }
122 
124  .id_code = ID_CF,
125  .id_filter = FILTER_ID_CF,
126  .main_listbase_index = INDEX_ID_CF,
127  .struct_size = sizeof(CacheFile),
128  .name = "CacheFile",
129  .name_plural = "cache_files",
130  .translation_context = BLT_I18NCONTEXT_ID_CACHEFILE,
131  .flags = 0,
132 
134  .copy_data = cache_file_copy_data,
135  .free_data = cache_file_free_data,
136  .make_local = NULL,
137  .foreach_id = NULL,
138  .foreach_cache = NULL,
139  .owner_get = NULL,
140 
141  .blend_write = cache_file_blend_write,
142  .blend_read_data = cache_file_blend_read_data,
143  .blend_read_lib = NULL,
144  .blend_read_expand = NULL,
145 
146  .blend_read_undo_preserve = NULL,
147 
148  .lib_override_apply_post = NULL,
149 };
150 
151 /* TODO: make this per cache file to avoid global locks. */
152 static SpinLock spin;
153 
155 {
157 }
158 
160 {
161  BLI_spin_end(&spin);
162 }
163 
165  struct CacheReader **reader,
166  Object *object,
167  const char *object_path)
168 {
169 #ifdef WITH_ALEMBIC
170  BLI_assert(cache_file->id.tag & LIB_TAG_COPIED_ON_WRITE);
171 
172  if (cache_file->handle == NULL) {
173  return;
174  }
175 
176  /* Open Alembic cache reader. */
177  *reader = CacheReader_open_alembic_object(cache_file->handle, *reader, object, object_path);
178 
179  /* Multiple modifiers and constraints can call this function concurrently. */
181  if (*reader) {
182  /* Register in set so we can free it when the cache file changes. */
183  if (cache_file->handle_readers == NULL) {
184  cache_file->handle_readers = BLI_gset_ptr_new("CacheFile.handle_readers");
185  }
186  BLI_gset_reinsert(cache_file->handle_readers, reader, NULL);
187  }
188  else if (cache_file->handle_readers) {
189  /* Remove in case CacheReader_open_alembic_object free the existing reader. */
190  BLI_gset_remove(cache_file->handle_readers, reader, NULL);
191  }
193 #else
194  UNUSED_VARS(cache_file, reader, object, object_path);
195 #endif
196 }
197 
198 void BKE_cachefile_reader_free(CacheFile *cache_file, struct CacheReader **reader)
199 {
200 #ifdef WITH_ALEMBIC
201  if (*reader != NULL) {
202  if (cache_file) {
203  BLI_assert(cache_file->id.tag & LIB_TAG_COPIED_ON_WRITE);
204  }
205 
206  CacheReader_free(*reader);
207  *reader = NULL;
208 
209  /* Multiple modifiers and constraints can call this function concurrently. */
211  if (cache_file && cache_file->handle_readers) {
212  BLI_gset_remove(cache_file->handle_readers, reader, NULL);
213  }
215  }
216 #else
217  UNUSED_VARS(cache_file, reader);
218 #endif
219 }
220 
221 static void cachefile_handle_free(CacheFile *cache_file)
222 {
223 #ifdef WITH_ALEMBIC
224  /* Free readers in all modifiers and constraints that use the handle, before
225  * we free the handle itself. */
227  if (cache_file->handle_readers) {
228  GSetIterator gs_iter;
229  GSET_ITER (gs_iter, cache_file->handle_readers) {
230  struct CacheReader **reader = BLI_gsetIterator_getKey(&gs_iter);
231  if (*reader != NULL) {
232  CacheReader_free(*reader);
233  *reader = NULL;
234  }
235  }
236 
237  BLI_gset_free(cache_file->handle_readers, NULL);
238  cache_file->handle_readers = NULL;
239  }
241 
242  /* Free handle. */
243  if (cache_file->handle) {
244  ABC_free_handle(cache_file->handle);
245  cache_file->handle = NULL;
246  }
247 
248  cache_file->handle_filepath[0] = '\0';
249 #else
250  UNUSED_VARS(cache_file);
251 #endif
252 }
253 
254 void *BKE_cachefile_add(Main *bmain, const char *name)
255 {
256  CacheFile *cache_file = BKE_id_new(bmain, ID_CF, name);
257 
258  return cache_file;
259 }
260 
262 {
263  /* To force reload, free the handle and tag depsgraph to load it again. */
264  CacheFile *cache_file_eval = (CacheFile *)DEG_get_evaluated_id(depsgraph, &cache_file->id);
265  if (cache_file_eval) {
266  cachefile_handle_free(cache_file_eval);
267  }
268 
270 }
271 
273 {
274  BLI_assert(cache_file->id.tag & LIB_TAG_COPIED_ON_WRITE);
275 
276  /* Compute filepath. */
277  char filepath[FILE_MAX];
278  if (!BKE_cachefile_filepath_get(bmain, depsgraph, cache_file, filepath)) {
279  return;
280  }
281 
282  /* Test if filepath change or if we can keep the existing handle. */
283  if (STREQ(cache_file->handle_filepath, filepath)) {
284  return;
285  }
286 
287  cachefile_handle_free(cache_file);
288  BLI_freelistN(&cache_file->object_paths);
289 
290 #ifdef WITH_ALEMBIC
291  cache_file->handle = ABC_create_handle(bmain, filepath, &cache_file->object_paths);
292  BLI_strncpy(cache_file->handle_filepath, filepath, FILE_MAX);
293 #endif
294 
295  if (DEG_is_active(depsgraph)) {
296  /* Flush object paths back to original datablock for UI. */
297  CacheFile *cache_file_orig = (CacheFile *)DEG_get_original_id(&cache_file->id);
298  BLI_freelistN(&cache_file_orig->object_paths);
299  BLI_duplicatelist(&cache_file_orig->object_paths, &cache_file->object_paths);
300  }
301 }
302 
304  const Depsgraph *depsgraph,
305  const CacheFile *cache_file,
306  char r_filepath[FILE_MAX])
307 {
308  BLI_strncpy(r_filepath, cache_file->filepath, FILE_MAX);
309  BLI_path_abs(r_filepath, ID_BLEND_PATH(bmain, &cache_file->id));
310 
311  int fframe;
312  int frame_len;
313 
314  if (cache_file->is_sequence && BLI_path_frame_get(r_filepath, &fframe, &frame_len)) {
316  const float ctime = BKE_scene_frame_get(scene);
317  const float fps = (((double)scene->r.frs_sec) / (double)scene->r.frs_sec_base);
318  const float frame = BKE_cachefile_time_offset(cache_file, ctime, fps);
319 
320  char ext[32];
321  BLI_path_frame_strip(r_filepath, ext);
322  BLI_path_frame(r_filepath, frame, frame_len);
323  BLI_path_extension_ensure(r_filepath, FILE_MAX, ext);
324 
325  /* TODO(kevin): store sequence range? */
326  return BLI_exists(r_filepath);
327  }
328 
329  return true;
330 }
331 
332 float BKE_cachefile_time_offset(const CacheFile *cache_file, const float time, const float fps)
333 {
334  const float time_offset = cache_file->frame_offset / fps;
335  const float frame = (cache_file->override_frame ? cache_file->frame : time);
336  return cache_file->is_sequence ? frame : frame / fps - time_offset;
337 }
struct CacheReader * CacheReader_open_alembic_object(struct AbcArchiveHandle *handle, struct CacheReader *reader, struct Object *object, const char *object_path)
AbcArchiveHandle * ABC_create_handle(struct Main *bmain, const char *filename, struct ListBase *object_paths)
void CacheReader_free(struct CacheReader *reader)
void ABC_free_handle(AbcArchiveHandle *handle)
void BKE_animdata_blend_read_data(struct BlendDataReader *reader, struct AnimData *adt)
Definition: anim_data.c:1574
void BKE_animdata_blend_write(struct BlendWriter *writer, struct AnimData *adt)
Definition: anim_data.c:1552
void BKE_id_blend_write(struct BlendWriter *writer, struct ID *id)
Definition: lib_id.c:2395
void * BKE_id_new(struct Main *bmain, const short type, const char *name)
Definition: lib_id.c:1177
float BKE_scene_frame_get(const struct Scene *scene)
#define BLI_assert(a)
Definition: BLI_assert.h:58
File and directory operations.
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:349
bool BLI_gset_reinsert(GSet *gh, void *key, GSetKeyFreeFP keyfreefp)
Definition: BLI_ghash.c:1195
GSet * BLI_gset_ptr_new(const char *info)
#define GSET_ITER(gs_iter_, gset_)
Definition: BLI_ghash.h:268
void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
Definition: BLI_ghash.c:1253
BLI_INLINE void * BLI_gsetIterator_getKey(GSetIterator *gsi)
Definition: BLI_ghash.h:255
bool BLI_gset_remove(GSet *gs, const void *key, GSetKeyFreeFP keyfreefp)
Definition: BLI_ghash.c:1211
void void void void void BLI_duplicatelist(struct ListBase *dst, const struct ListBase *src) ATTR_NONNULL(1
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
Definition: BLI_listbase.h:128
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
bool BLI_path_frame(char *path, int frame, int digits) ATTR_NONNULL()
Definition: path_util.c:802
bool BLI_path_frame_get(char *path, int *r_frame, int *numdigits) ATTR_NONNULL()
Definition: path_util.c:854
void BLI_path_frame_strip(char *path, char *ext) ATTR_NONNULL()
Definition: path_util.c:906
#define FILE_MAX
bool BLI_path_extension_ensure(char *path, size_t maxlen, const char *ext) ATTR_NONNULL()
Definition: path_util.c:1601
bool BLI_path_abs(char *path, const char *basepath) ATTR_NONNULL()
Definition: path_util.c:1016
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
pthread_spinlock_t SpinLock
Definition: BLI_threads.h:111
void BLI_spin_init(SpinLock *spin)
Definition: threads.cc:447
void BLI_spin_unlock(SpinLock *spin)
Definition: threads.cc:480
void BLI_spin_lock(SpinLock *spin)
Definition: threads.cc:461
void BLI_spin_end(SpinLock *spin)
Definition: threads.cc:495
#define UNUSED_VARS(...)
#define UNUSED(x)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define STREQ(a, b)
#define BLO_read_data_address(reader, ptr_p)
#define BLO_write_id_struct(writer, struct_name, id_address, id)
bool BLO_write_is_undo(BlendWriter *writer)
Definition: writefile.c:1412
#define BLT_I18NCONTEXT_ID_CACHEFILE
typedef double(DMatrix)[4][4]
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
bool DEG_is_active(const struct Depsgraph *depsgraph)
Definition: depsgraph.cc:331
void DEG_id_tag_update(struct ID *id, int flag)
struct ID * DEG_get_evaluated_id(const struct Depsgraph *depsgraph, struct ID *id)
struct ID * DEG_get_original_id(struct ID *id)
struct Scene * DEG_get_evaluated_scene(const struct Depsgraph *graph)
#define FILTER_ID_CF
Definition: DNA_ID.h:733
@ ID_RECALC_COPY_ON_WRITE
Definition: DNA_ID.h:654
@ LIB_TAG_COPIED_ON_WRITE
Definition: DNA_ID.h:565
#define ID_BLEND_PATH(_bmain, _id)
Definition: DNA_ID.h:419
@ INDEX_ID_CF
Definition: DNA_ID.h:818
@ ID_CF
Definition: DNA_ID_enums.h:90
@ CACHEFILE_VELOCITY_UNIT_SECOND
struct CacheFile CacheFile
Object is a sort of wrapper for general info.
static void init_data(ModifierData *md)
static float frame_len(const Frame *frame)
Definition: MOD_skin.c:392
float BKE_cachefile_time_offset(const CacheFile *cache_file, const float time, const float fps)
Definition: cachefile.c:332
static void cachefile_handle_free(CacheFile *cache_file)
Definition: cachefile.c:221
static void cache_file_free_data(ID *id)
Definition: cachefile.c:84
void BKE_cachefiles_init(void)
Definition: cachefile.c:154
static void cache_file_init_data(ID *id)
Definition: cachefile.c:60
static SpinLock spin
Definition: cachefile.c:152
static void cache_file_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition: cachefile.c:91
static void cache_file_blend_read_data(BlendDataReader *reader, ID *id)
Definition: cachefile.c:110
void BKE_cachefile_reader_free(CacheFile *cache_file, struct CacheReader **reader)
Definition: cachefile.c:198
void * BKE_cachefile_add(Main *bmain, const char *name)
Definition: cachefile.c:254
bool BKE_cachefile_filepath_get(const Main *bmain, const Depsgraph *depsgraph, const CacheFile *cache_file, char r_filepath[FILE_MAX])
Definition: cachefile.c:303
void BKE_cachefile_reload(Depsgraph *depsgraph, CacheFile *cache_file)
Definition: cachefile.c:261
void BKE_cachefile_eval(Main *bmain, Depsgraph *depsgraph, CacheFile *cache_file)
Definition: cachefile.c:272
void BKE_cachefile_reader_open(CacheFile *cache_file, struct CacheReader **reader, Object *object, const char *object_path)
Definition: cachefile.c:164
static void cache_file_copy_data(Main *UNUSED(bmain), ID *id_dst, const ID *id_src, const int UNUSED(flag))
Definition: cachefile.c:71
void BKE_cachefiles_exit(void)
Definition: cachefile.c:159
IDTypeInfo IDType_ID_CF
Definition: cachefile.c:123
double time
Scene scene
const Depsgraph * depsgraph
struct AnimData * adt
ListBase object_paths
char handle_filepath[1024]
struct GSet * handle_readers
char filepath[1024]
char velocity_name[64]
struct AbcArchiveHandle * handle
The CacheReader struct is only used for anonymous pointers, to interface between C and C++ code....
Definition: abc_util.h:30
short id_code
Definition: BKE_idtype.h:120
Definition: DNA_ID.h:273
int tag
Definition: DNA_ID.h:292
int us
Definition: DNA_ID.h:293
Definition: BKE_main.h:116
float frs_sec_base
struct RenderData r