2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2011-10-10 09:38:02 +00:00
|
|
|
* $Id$
|
2010-07-23 16:57:11 +00:00
|
|
|
*
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Peter Schlaile <peter [at] schlaile [dot] de> 2010
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
2011-02-27 20:40:57 +00:00
|
|
|
/** \file blender/blenkernel/intern/seqcache.c
|
|
|
|
* \ingroup bke
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-07-23 16:57:11 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
#include "MEM_CacheLimiterC-Api.h"
|
|
|
|
|
|
|
|
#include "DNA_sequence_types.h"
|
|
|
|
#include "BKE_sequencer.h"
|
2011-01-07 18:36:47 +00:00
|
|
|
#include "BLI_utildefines.h"
|
2010-07-23 16:57:11 +00:00
|
|
|
#include "BLI_ghash.h"
|
|
|
|
#include "BLI_mempool.h"
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#include "IMB_imbuf.h"
|
|
|
|
#include "IMB_imbuf_types.h"
|
|
|
|
|
|
|
|
typedef struct seqCacheKey
|
|
|
|
{
|
|
|
|
struct Sequence * seq;
|
2010-11-21 20:00:31 +00:00
|
|
|
SeqRenderData context;
|
2010-07-23 16:57:11 +00:00
|
|
|
float cfra;
|
|
|
|
seq_stripelem_ibuf_t type;
|
|
|
|
} seqCacheKey;
|
|
|
|
|
|
|
|
typedef struct seqCacheEntry
|
|
|
|
{
|
|
|
|
ImBuf * ibuf;
|
|
|
|
MEM_CacheLimiterHandleC * c_handle;
|
|
|
|
} seqCacheEntry;
|
|
|
|
|
2011-02-13 10:52:18 +00:00
|
|
|
static GHash * hash = NULL;
|
|
|
|
static MEM_CacheLimiterC * limitor = NULL;
|
|
|
|
static struct BLI_mempool * entrypool = NULL;
|
|
|
|
static struct BLI_mempool * keypool = NULL;
|
2010-07-23 16:57:11 +00:00
|
|
|
static int ibufs_in = 0;
|
|
|
|
static int ibufs_rem = 0;
|
|
|
|
|
2010-12-03 17:05:21 +00:00
|
|
|
static unsigned int HashHash(const void *key_)
|
2010-07-23 16:57:11 +00:00
|
|
|
{
|
2010-12-03 17:05:21 +00:00
|
|
|
const seqCacheKey *key = (seqCacheKey*) key_;
|
2010-11-21 20:00:31 +00:00
|
|
|
unsigned int rval = seq_hash_render_data(&key->context);
|
2010-07-23 16:57:11 +00:00
|
|
|
|
|
|
|
rval ^= *(unsigned int*) &key->cfra;
|
|
|
|
rval += key->type;
|
2010-09-12 14:46:41 +00:00
|
|
|
rval ^= ((intptr_t) key->seq) << 6;
|
2010-07-23 16:57:11 +00:00
|
|
|
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2010-12-03 17:05:21 +00:00
|
|
|
static int HashCmp(const void *a_, const void *b_)
|
2010-07-23 16:57:11 +00:00
|
|
|
{
|
2010-12-03 17:05:21 +00:00
|
|
|
const seqCacheKey * a = (seqCacheKey*) a_;
|
|
|
|
const seqCacheKey * b = (seqCacheKey*) b_;
|
2010-07-23 16:57:11 +00:00
|
|
|
|
|
|
|
if (a->seq < b->seq) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a->seq > b->seq) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a->cfra < b->cfra) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a->cfra > b->cfra) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a->type < b->type) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a->type > b->type) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-11-21 20:00:31 +00:00
|
|
|
return seq_cmp_render_data(&a->context, &b->context);
|
2010-07-23 16:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void HashKeyFree(void *key)
|
|
|
|
{
|
|
|
|
BLI_mempool_free(keypool, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HashValFree(void *val)
|
|
|
|
{
|
|
|
|
seqCacheEntry* e = (seqCacheEntry*) val;
|
|
|
|
|
|
|
|
if (e->ibuf) {
|
|
|
|
/* fprintf(stderr, "Removing: %p, cnt: %d\n", e->ibuf,
|
|
|
|
e->ibuf->refcounter); */
|
|
|
|
IMB_freeImBuf(e->ibuf);
|
|
|
|
MEM_CacheLimiter_unmanage(e->c_handle);
|
|
|
|
ibufs_rem++;
|
|
|
|
}
|
|
|
|
|
2011-02-13 10:52:18 +00:00
|
|
|
e->ibuf = NULL;
|
|
|
|
e->c_handle = NULL;
|
2010-07-23 16:57:11 +00:00
|
|
|
|
|
|
|
BLI_mempool_free(entrypool, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void IMB_seq_cache_destructor(void * p)
|
|
|
|
{
|
|
|
|
seqCacheEntry* e = (seqCacheEntry*) p;
|
|
|
|
|
|
|
|
if (e && e->ibuf) {
|
|
|
|
/* fprintf(stderr, "Removing: %p, cnt: %d\n", e->ibuf,
|
|
|
|
e->ibuf->refcounter); */
|
|
|
|
IMB_freeImBuf(e->ibuf);
|
|
|
|
ibufs_rem++;
|
|
|
|
|
2011-02-13 10:52:18 +00:00
|
|
|
e->ibuf = NULL;
|
|
|
|
e->c_handle = NULL;
|
2010-07-23 16:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-13 10:52:18 +00:00
|
|
|
void seq_stripelem_cache_init(void)
|
2010-07-23 16:57:11 +00:00
|
|
|
{
|
|
|
|
hash = BLI_ghash_new(HashHash, HashCmp, "seq stripelem cache hash");
|
|
|
|
limitor = new_MEM_CacheLimiter( IMB_seq_cache_destructor );
|
|
|
|
|
|
|
|
entrypool = BLI_mempool_create(sizeof(seqCacheEntry), 64, 64, 0);
|
|
|
|
keypool = BLI_mempool_create(sizeof(seqCacheKey), 64, 64, 0);
|
|
|
|
}
|
|
|
|
|
2011-02-13 10:52:18 +00:00
|
|
|
void seq_stripelem_cache_destruct(void)
|
2010-07-23 16:57:11 +00:00
|
|
|
{
|
|
|
|
if (!entrypool) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BLI_ghash_free(hash, HashKeyFree, HashValFree);
|
|
|
|
delete_MEM_CacheLimiter(limitor);
|
|
|
|
BLI_mempool_destroy(entrypool);
|
|
|
|
BLI_mempool_destroy(keypool);
|
|
|
|
}
|
|
|
|
|
2011-02-13 10:52:18 +00:00
|
|
|
void seq_stripelem_cache_cleanup(void)
|
2010-07-23 16:57:11 +00:00
|
|
|
{
|
|
|
|
if (!entrypool) {
|
|
|
|
seq_stripelem_cache_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fprintf(stderr, "Stats before cleanup: in: %d rem: %d\n",
|
|
|
|
ibufs_in, ibufs_rem); */
|
|
|
|
|
|
|
|
BLI_ghash_free(hash, HashKeyFree, HashValFree);
|
|
|
|
hash = BLI_ghash_new(HashHash, HashCmp, "seq stripelem cache hash");
|
|
|
|
|
|
|
|
/* fprintf(stderr, "Stats after cleanup: in: %d rem: %d\n",
|
|
|
|
ibufs_in, ibufs_rem); */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ImBuf * seq_stripelem_cache_get(
|
2010-11-21 20:00:31 +00:00
|
|
|
SeqRenderData context, struct Sequence * seq,
|
2010-07-23 16:57:11 +00:00
|
|
|
float cfra, seq_stripelem_ibuf_t type)
|
|
|
|
{
|
|
|
|
seqCacheKey key;
|
|
|
|
seqCacheEntry * e;
|
|
|
|
|
|
|
|
if (!seq) {
|
2011-02-13 10:52:18 +00:00
|
|
|
return NULL;
|
2010-07-23 16:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!entrypool) {
|
|
|
|
seq_stripelem_cache_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
key.seq = seq;
|
2010-11-21 20:00:31 +00:00
|
|
|
key.context = context;
|
2010-07-23 16:57:11 +00:00
|
|
|
key.cfra = cfra - seq->start;
|
|
|
|
key.type = type;
|
|
|
|
|
|
|
|
e = (seqCacheEntry*) BLI_ghash_lookup(hash, &key);
|
|
|
|
|
|
|
|
if (e && e->ibuf) {
|
|
|
|
IMB_refImBuf(e->ibuf);
|
|
|
|
|
|
|
|
MEM_CacheLimiter_touch(e->c_handle);
|
|
|
|
return e->ibuf;
|
|
|
|
}
|
2011-02-13 10:52:18 +00:00
|
|
|
return NULL;
|
2010-07-23 16:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void seq_stripelem_cache_put(
|
2010-11-21 20:00:31 +00:00
|
|
|
SeqRenderData context, struct Sequence * seq,
|
2010-07-23 16:57:11 +00:00
|
|
|
float cfra, seq_stripelem_ibuf_t type, struct ImBuf * i)
|
|
|
|
{
|
|
|
|
seqCacheKey * key;
|
|
|
|
seqCacheEntry * e;
|
|
|
|
|
|
|
|
if (!i) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ibufs_in++;
|
|
|
|
|
|
|
|
if (!entrypool) {
|
|
|
|
seq_stripelem_cache_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
key = (seqCacheKey*) BLI_mempool_alloc(keypool);
|
|
|
|
|
|
|
|
key->seq = seq;
|
2010-11-21 20:00:31 +00:00
|
|
|
key->context = context;
|
2010-07-23 16:57:11 +00:00
|
|
|
key->cfra = cfra - seq->start;
|
|
|
|
key->type = type;
|
|
|
|
|
2011-04-17 10:05:27 +00:00
|
|
|
IMB_refImBuf(i);
|
2010-07-23 16:57:11 +00:00
|
|
|
|
|
|
|
e = (seqCacheEntry*) BLI_mempool_alloc(entrypool);
|
|
|
|
|
|
|
|
e->ibuf = i;
|
2011-02-13 10:52:18 +00:00
|
|
|
e->c_handle = NULL;
|
2010-07-23 16:57:11 +00:00
|
|
|
|
|
|
|
BLI_ghash_remove(hash, key, HashKeyFree, HashValFree);
|
|
|
|
BLI_ghash_insert(hash, key, e);
|
|
|
|
|
|
|
|
e->c_handle = MEM_CacheLimiter_insert(limitor, e);
|
|
|
|
|
|
|
|
MEM_CacheLimiter_ref(e->c_handle);
|
|
|
|
MEM_CacheLimiter_enforce_limits(limitor);
|
|
|
|
MEM_CacheLimiter_unref(e->c_handle);
|
|
|
|
}
|