From 77b51d4e583da417a004391724ba945d3f7de19d Mon Sep 17 00:00:00 2001 From: Richard Antalik Date: Mon, 25 Jan 2021 04:28:13 +0100 Subject: VSE: Remove skip_disk_cache argument This argument was used to prevent infinite loop in lookups between disk and RAM cache. Code was refactored, so this can be handled internally in cache. Reviewed By: sybren, sergey Differential Revision: https://developer.blender.org/D9955 --- source/blender/sequencer/intern/image_cache.c | 180 ++++++++++++++------------ source/blender/sequencer/intern/image_cache.h | 9 +- source/blender/sequencer/intern/prefetch.c | 8 +- source/blender/sequencer/intern/render.c | 26 ++-- 4 files changed, 113 insertions(+), 110 deletions(-) (limited to 'source/blender/sequencer') diff --git a/source/blender/sequencer/intern/image_cache.c b/source/blender/sequencer/intern/image_cache.c index 344b5af26aa..299ebffe8a8 100644 --- a/source/blender/sequencer/intern/image_cache.c +++ b/source/blender/sequencer/intern/image_cache.c @@ -824,16 +824,59 @@ static void seq_cache_valfree(void *val) BLI_mempool_free(item->cache_owner->items_pool, item); } -static void seq_cache_put_ex(SeqCache *cache, SeqCacheKey *key, ImBuf *ibuf) +static int get_stored_types_flag(Scene *scene, SeqCacheKey *key) { + int flag; + if (key->seq->cache_flag & SEQ_CACHE_OVERRIDE) { + flag = key->seq->cache_flag; + } + else { + flag = scene->ed->cache_flag; + } + + /* SEQ_CACHE_STORE_FINAL_OUT can not be overridden by strip cache */ + flag |= (scene->ed->cache_flag & SEQ_CACHE_STORE_FINAL_OUT); + + return flag; +} + +static void seq_cache_put_ex(Scene *scene, SeqCacheKey *key, ImBuf *ibuf) +{ + SeqCache *cache = seq_cache_get_from_scene(scene); SeqCacheItem *item; item = BLI_mempool_alloc(cache->items_pool); item->cache_owner = cache; item->ibuf = ibuf; + const int stored_types_flag = get_stored_types_flag(scene, key); + + /* Item stored for later use. */ + if (stored_types_flag & key->type) { + key->is_temp_cache = false; + key->link_prev = cache->last_key; + } + + /* Store pointer to last cached key. */ + SeqCacheKey *temp_last_key = cache->last_key; + if (BLI_ghash_reinsert(cache->hash, key, item, seq_cache_keyfree, seq_cache_valfree)) { IMB_refImBuf(ibuf); - cache->last_key = key; + + if (!key->is_temp_cache) { + cache->last_key = key; + } + } + + /* Set last_key's reference to this key so we can look up chain backwards. + * Item is already put in cache, so cache->last_key points to current key. + */ + if (!key->is_temp_cache && temp_last_key) { + temp_last_key->link_next = cache->last_key; + } + + /* Reset linking. */ + if (key->type == SEQ_CACHE_STORE_FINAL_OUT) { + cache->last_key = NULL; } } @@ -1095,6 +1138,35 @@ static void seq_cache_create(Main *bmain, Scene *scene) BLI_mutex_unlock(&cache_create_lock); } +static void seq_cache_populate_key(SeqCacheKey *key, + const SeqRenderData *context, + Sequence *seq, + const float timeline_frame, + const int type) +{ + key->cache_owner = seq_cache_get_from_scene(context->scene); + key->seq = seq; + key->context = *context; + key->frame_index = seq_cache_timeline_frame_to_frame_index(seq, timeline_frame, type); + key->timeline_frame = timeline_frame; + key->type = type; + key->link_prev = NULL; + key->link_next = NULL; + key->is_temp_cache = true; + key->task_id = context->task_id; +} + +static SeqCacheKey *seq_cache_allocate_key(SeqCache *cache, + const SeqRenderData *context, + Sequence *seq, + const float timeline_frame, + const int type) +{ + SeqCacheKey *key = BLI_mempool_alloc(cache->keys_pool); + seq_cache_populate_key(key, context, seq, timeline_frame, type); + return key; +} + /* ***************************** API ****************************** */ void seq_cache_free_temp_cache(Scene *scene, short id, int timeline_frame) @@ -1243,8 +1315,7 @@ void seq_cache_cleanup_sequence(Scene *scene, struct ImBuf *seq_cache_get(const SeqRenderData *context, Sequence *seq, float timeline_frame, - int type, - bool skip_disk_cache) + int type) { if (context->skip_cache || context->is_proxy_render || !seq) { @@ -1274,11 +1345,7 @@ struct ImBuf *seq_cache_get(const SeqRenderData *context, /* Try RAM cache: */ if (cache && seq) { - key.seq = seq; - key.context = *context; - key.frame_index = seq_cache_timeline_frame_to_frame_index(seq, timeline_frame, type); - key.type = type; - + seq_cache_populate_key(&key, context, seq, timeline_frame, type); ibuf = seq_cache_get_ex(cache, &key); } seq_cache_unlock(scene); @@ -1288,7 +1355,7 @@ struct ImBuf *seq_cache_get(const SeqRenderData *context, } /* Try disk cache: */ - if (!skip_disk_cache && seq_disk_cache_is_enabled(context->bmain)) { + if (seq_disk_cache_is_enabled(context->bmain)) { if (cache->disk_cache == NULL) { seq_disk_cache_create(context->bmain, context->scene); } @@ -1296,25 +1363,23 @@ struct ImBuf *seq_cache_get(const SeqRenderData *context, BLI_mutex_lock(&cache->disk_cache->read_write_mutex); ibuf = seq_disk_cache_read_file(cache->disk_cache, &key); BLI_mutex_unlock(&cache->disk_cache->read_write_mutex); - if (ibuf) { - if (key.type == SEQ_CACHE_STORE_FINAL_OUT) { - seq_cache_put_if_possible(context, seq, timeline_frame, type, ibuf, true); - } - else { - seq_cache_put(context, seq, timeline_frame, type, ibuf, true); - } + + if (ibuf == NULL) { + return NULL; + } + + /* Store read image in RAM. Only recycle item for final type. */ + if (key.type != SEQ_CACHE_STORE_FINAL_OUT || seq_cache_recycle_item(scene)) { + SeqCacheKey *new_key = seq_cache_allocate_key(cache, context, seq, timeline_frame, type); + seq_cache_put_ex(scene, new_key, ibuf); } } return ibuf; } -bool seq_cache_put_if_possible(const SeqRenderData *context, - Sequence *seq, - float timeline_frame, - int type, - ImBuf *ibuf, - bool skip_disk_cache) +bool seq_cache_put_if_possible( + const SeqRenderData *context, Sequence *seq, float timeline_frame, int type, ImBuf *ibuf) { Scene *scene = context->scene; @@ -1329,7 +1394,7 @@ bool seq_cache_put_if_possible(const SeqRenderData *context, } if (seq_cache_recycle_item(scene)) { - seq_cache_put(context, seq, timeline_frame, type, ibuf, skip_disk_cache); + seq_cache_put(context, seq, timeline_frame, type, ibuf); return true; } @@ -1338,12 +1403,8 @@ bool seq_cache_put_if_possible(const SeqRenderData *context, return false; } -void seq_cache_put(const SeqRenderData *context, - Sequence *seq, - float timeline_frame, - int type, - ImBuf *i, - bool skip_disk_cache) +void seq_cache_put( + const SeqRenderData *context, Sequence *seq, float timeline_frame, int type, ImBuf *i) { if (i == NULL || context->skip_cache || context->is_proxy_render || !seq) { return; @@ -1359,7 +1420,7 @@ void seq_cache_put(const SeqRenderData *context, } /* Prevent reinserting, it breaks cache key linking. */ - ImBuf *test = seq_cache_get(context, seq, timeline_frame, type, true); + ImBuf *test = seq_cache_get(context, seq, timeline_frame, type); if (test) { IMB_freeImBuf(test); return; @@ -1370,63 +1431,12 @@ void seq_cache_put(const SeqRenderData *context, } seq_cache_lock(scene); - SeqCache *cache = seq_cache_get_from_scene(scene); - int flag; - - if (seq->cache_flag & SEQ_CACHE_OVERRIDE) { - flag = seq->cache_flag; - /* Final_out is invalid in context of sequence override. */ - flag -= seq->cache_flag & SEQ_CACHE_STORE_FINAL_OUT; - /* If global setting is enabled however, use it. */ - flag |= scene->ed->cache_flag & SEQ_CACHE_STORE_FINAL_OUT; - } - else { - flag = scene->ed->cache_flag; - } - - SeqCacheKey *key; - key = BLI_mempool_alloc(cache->keys_pool); - key->cache_owner = cache; - key->seq = seq; - key->context = *context; - key->frame_index = seq_cache_timeline_frame_to_frame_index(seq, timeline_frame, type); - key->timeline_frame = timeline_frame; - key->type = type; - key->link_prev = NULL; - key->link_next = NULL; - key->is_temp_cache = true; - key->task_id = context->task_id; - - /* Item stored for later use */ - if (flag & type) { - key->is_temp_cache = false; - key->link_prev = cache->last_key; - } - - SeqCacheKey *temp_last_key = cache->last_key; - seq_cache_put_ex(cache, key, i); - - /* Restore pointer to previous item as this one will be freed when stack is rendered. */ - if (key->is_temp_cache) { - cache->last_key = temp_last_key; - } - - /* Set last_key's reference to this key so we can look up chain backwards. - * Item is already put in cache, so cache->last_key points to current key. - */ - if (flag & type && temp_last_key) { - temp_last_key->link_next = cache->last_key; - } - - /* Reset linking. */ - if (key->type == SEQ_CACHE_STORE_FINAL_OUT) { - cache->last_key = NULL; - } - + SeqCacheKey *key = seq_cache_allocate_key(cache, context, seq, timeline_frame, type); + seq_cache_put_ex(scene, key, i); seq_cache_unlock(scene); - if (!key->is_temp_cache && !skip_disk_cache) { + if (!key->is_temp_cache) { if (seq_disk_cache_is_enabled(context->bmain)) { if (cache->disk_cache == NULL) { seq_disk_cache_create(context->bmain, context->scene); diff --git a/source/blender/sequencer/intern/image_cache.h b/source/blender/sequencer/intern/image_cache.h index 41e8c4d1d48..63c559caee9 100644 --- a/source/blender/sequencer/intern/image_cache.h +++ b/source/blender/sequencer/intern/image_cache.h @@ -40,20 +40,17 @@ struct Sequence; struct ImBuf *seq_cache_get(const struct SeqRenderData *context, struct Sequence *seq, float timeline_frame, - int type, - bool skip_disk_cache); + int type); void seq_cache_put(const struct SeqRenderData *context, struct Sequence *seq, float timeline_frame, int type, - struct ImBuf *i, - bool skip_disk_cache); + struct ImBuf *i); bool seq_cache_put_if_possible(const struct SeqRenderData *context, struct Sequence *seq, float timeline_frame, int type, - struct ImBuf *nval, - bool skip_disk_cache); + struct ImBuf *nval); bool seq_cache_recycle_item(struct Scene *scene); void seq_cache_free_temp_cache(struct Scene *scene, short id, int timeline_frame); void seq_cache_destruct(struct Scene *scene); diff --git a/source/blender/sequencer/intern/prefetch.c b/source/blender/sequencer/intern/prefetch.c index 6deea305224..e658b99236a 100644 --- a/source/blender/sequencer/intern/prefetch.c +++ b/source/blender/sequencer/intern/prefetch.c @@ -373,28 +373,28 @@ static bool seq_prefetch_do_skip_frame(Scene *scene) if (seq_arr[i]->type == SEQ_TYPE_SCENE && (seq_arr[i]->flag & SEQ_SCENE_STRIPS) == 0) { int cached_types = 0; - ibuf = seq_cache_get(ctx, seq_arr[i], cfra, SEQ_CACHE_STORE_FINAL_OUT, false); + ibuf = seq_cache_get(ctx, seq_arr[i], cfra, SEQ_CACHE_STORE_FINAL_OUT); if (ibuf != NULL) { cached_types |= SEQ_CACHE_STORE_FINAL_OUT; IMB_freeImBuf(ibuf); ibuf = NULL; } - ibuf = seq_cache_get(ctx, seq_arr[i], cfra, SEQ_CACHE_STORE_COMPOSITE, false); + ibuf = seq_cache_get(ctx, seq_arr[i], cfra, SEQ_CACHE_STORE_COMPOSITE); if (ibuf != NULL) { cached_types |= SEQ_CACHE_STORE_COMPOSITE; IMB_freeImBuf(ibuf); ibuf = NULL; } - ibuf = seq_cache_get(ctx, seq_arr[i], cfra, SEQ_CACHE_STORE_PREPROCESSED, false); + ibuf = seq_cache_get(ctx, seq_arr[i], cfra, SEQ_CACHE_STORE_PREPROCESSED); if (ibuf != NULL) { cached_types |= SEQ_CACHE_STORE_PREPROCESSED; IMB_freeImBuf(ibuf); ibuf = NULL; } - ibuf = seq_cache_get(ctx, seq_arr[i], cfra, SEQ_CACHE_STORE_RAW, false); + ibuf = seq_cache_get(ctx, seq_arr[i], cfra, SEQ_CACHE_STORE_RAW); if (ibuf != NULL) { cached_types |= SEQ_CACHE_STORE_RAW; IMB_freeImBuf(ibuf); diff --git a/source/blender/sequencer/intern/render.c b/source/blender/sequencer/intern/render.c index cd993ae45c1..db317ffe7d6 100644 --- a/source/blender/sequencer/intern/render.c +++ b/source/blender/sequencer/intern/render.c @@ -730,14 +730,14 @@ static ImBuf *seq_render_preprocess_ibuf(const SeqRenderData *context, /* Proxies and effect strips are not stored in cache. */ if (!is_proxy_image && (seq->type & SEQ_TYPE_EFFECT) == 0) { - seq_cache_put(context, seq, timeline_frame, SEQ_CACHE_STORE_RAW, ibuf, false); + seq_cache_put(context, seq, timeline_frame, SEQ_CACHE_STORE_RAW, ibuf); } if (use_preprocess) { ibuf = input_preprocess(context, seq, timeline_frame, ibuf); } - seq_cache_put(context, seq, timeline_frame, SEQ_CACHE_STORE_PREPROCESSED, ibuf, false); + seq_cache_put(context, seq, timeline_frame, SEQ_CACHE_STORE_PREPROCESSED, ibuf); return ibuf; } @@ -1605,8 +1605,7 @@ static ImBuf *seq_render_scene_strip(const SeqRenderData *context, } if (view_id != context->view_id) { - seq_cache_put( - &localcontext, seq, timeline_frame, SEQ_CACHE_STORE_RAW, ibufs_arr[view_id], false); + seq_cache_put(&localcontext, seq, timeline_frame, SEQ_CACHE_STORE_RAW, ibufs_arr[view_id]); } RE_ReleaseResultImage(re); @@ -1781,14 +1780,14 @@ ImBuf *seq_render_strip(const SeqRenderData *context, bool use_preprocess = false; bool is_proxy_image = false; - ibuf = seq_cache_get(context, seq, timeline_frame, SEQ_CACHE_STORE_PREPROCESSED, false); + ibuf = seq_cache_get(context, seq, timeline_frame, SEQ_CACHE_STORE_PREPROCESSED); if (ibuf != NULL) { return ibuf; } /* Proxies are not stored in cache. */ if (!SEQ_can_use_proxy(seq, SEQ_rendersize_to_proxysize(context->preview_render_size))) { - ibuf = seq_cache_get(context, seq, timeline_frame, SEQ_CACHE_STORE_RAW, false); + ibuf = seq_cache_get(context, seq, timeline_frame, SEQ_CACHE_STORE_RAW); } if (ibuf == NULL) { @@ -1895,7 +1894,7 @@ static ImBuf *seq_render_strip_stack(const SeqRenderData *context, int early_out; Sequence *seq = seq_arr[i]; - out = seq_cache_get(context, seq, timeline_frame, SEQ_CACHE_STORE_COMPOSITE, false); + out = seq_cache_get(context, seq, timeline_frame, SEQ_CACHE_STORE_COMPOSITE); if (out) { break; @@ -1924,8 +1923,7 @@ static ImBuf *seq_render_strip_stack(const SeqRenderData *context, out = seq_render_strip_stack_apply_effect(context, seq, timeline_frame, ibuf1, ibuf2); - seq_cache_put( - context, seq_arr[i], timeline_frame, SEQ_CACHE_STORE_COMPOSITE, out, false); + seq_cache_put(context, seq_arr[i], timeline_frame, SEQ_CACHE_STORE_COMPOSITE, out); IMB_freeImBuf(ibuf1); IMB_freeImBuf(ibuf2); @@ -1951,7 +1949,7 @@ static ImBuf *seq_render_strip_stack(const SeqRenderData *context, IMB_freeImBuf(ibuf2); } - seq_cache_put(context, seq_arr[i], timeline_frame, SEQ_CACHE_STORE_COMPOSITE, out, false); + seq_cache_put(context, seq_arr[i], timeline_frame, SEQ_CACHE_STORE_COMPOSITE, out); } return out; @@ -1990,8 +1988,7 @@ ImBuf *SEQ_render_give_ibuf(const SeqRenderData *context, float timeline_frame, count = seq_get_shown_sequences(seqbasep, timeline_frame, chanshown, seq_arr); if (count) { - out = seq_cache_get( - context, seq_arr[count - 1], timeline_frame, SEQ_CACHE_STORE_FINAL_OUT, false); + out = seq_cache_get(context, seq_arr[count - 1], timeline_frame, SEQ_CACHE_STORE_FINAL_OUT); } seq_cache_free_temp_cache(context->scene, context->task_id, timeline_frame); @@ -2001,12 +1998,11 @@ ImBuf *SEQ_render_give_ibuf(const SeqRenderData *context, float timeline_frame, out = seq_render_strip_stack(context, &state, seqbasep, timeline_frame, chanshown); if (context->is_prefetch_render) { - seq_cache_put( - context, seq_arr[count - 1], timeline_frame, SEQ_CACHE_STORE_FINAL_OUT, out, false); + seq_cache_put(context, seq_arr[count - 1], timeline_frame, SEQ_CACHE_STORE_FINAL_OUT, out); } else { seq_cache_put_if_possible( - context, seq_arr[count - 1], timeline_frame, SEQ_CACHE_STORE_FINAL_OUT, out, false); + context, seq_arr[count - 1], timeline_frame, SEQ_CACHE_STORE_FINAL_OUT, out); } BLI_mutex_unlock(&seq_render_mutex); } -- cgit v1.2.3