44
44
#include "IMB_divers.h"
45
45
#include "IMB_allocimbuf.h"
46
#include "MEM_CacheLimiterC-Api.h"
47
48
static unsigned int dfltcmap[16] = {
48
49
0x00000000, 0xffffffff, 0x777777ff, 0xccccccff,
54
55
void imb_freeplanesImBuf(struct ImBuf * ibuf)
57
if (ibuf==NULL) return;
58
if (ibuf->mall & IB_planes) free(ibuf->planes);
59
if (ibuf->mall & IB_planes) MEM_freeN(ibuf->planes);
61
62
ibuf->mall &= ~IB_planes;
65
void imb_freerectfloatImBuf(struct ImBuf * ibuf)
67
if (ibuf==NULL) return;
69
if (ibuf->rect_float) {
70
if (ibuf->mall & IB_rectfloat) {
71
MEM_freeN(ibuf->rect_float);
72
ibuf->rect_float=NULL;
76
ibuf->rect_float= NULL;
77
ibuf->mall &= ~IB_rectfloat;
65
80
void imb_freerectImBuf(struct ImBuf * ibuf)
69
if (ibuf->mall & IB_rect) free(ibuf->rect);
82
if (ibuf==NULL) return;
85
if (ibuf->mall & IB_rect) {
86
MEM_freeN(ibuf->rect);
72
91
ibuf->mall &= ~IB_rect;
75
94
static void freeencodedbufferImBuf(struct ImBuf * ibuf)
96
if (ibuf==NULL) return;
78
97
if (ibuf->encodedbuffer){
79
if (ibuf->mall & IB_mem) free(ibuf->encodedbuffer);
98
if (ibuf->mall & IB_mem) MEM_freeN(ibuf->encodedbuffer);
81
100
ibuf->encodedbuffer = 0;
82
101
ibuf->encodedbuffersize = 0;
87
106
void IMB_freezbufImBuf(struct ImBuf * ibuf)
108
if (ibuf==NULL) return;
91
if (ibuf->mall & IB_zbuf) free(ibuf->zbuf);
110
if (ibuf->mall & IB_zbuf) MEM_freeN(ibuf->zbuf);
94
113
ibuf->mall &= ~IB_zbuf;
116
void IMB_freezbuffloatImBuf(struct ImBuf * ibuf)
118
if (ibuf==NULL) return;
119
if (ibuf->zbuf_float){
120
if (ibuf->mall & IB_zbuffloat) MEM_freeN(ibuf->zbuf_float);
122
ibuf->zbuf_float= NULL;
123
ibuf->mall &= ~IB_zbuffloat;
97
126
void IMB_freecmapImBuf(struct ImBuf * ibuf)
99
if (ibuf == 0) return;
128
if (ibuf==NULL) return;
101
if (ibuf->mall & IB_cmap) free(ibuf->cmap);
130
if (ibuf->mall & IB_cmap) MEM_freeN(ibuf->cmap);
104
133
ibuf->mall &= ~IB_cmap;
108
136
void IMB_freeImBuf(struct ImBuf * ibuf)
111
imb_freeplanesImBuf(ibuf);
112
imb_freerectImBuf(ibuf);
113
IMB_freezbufImBuf(ibuf);
114
IMB_freecmapImBuf(ibuf);
115
freeencodedbufferImBuf(ibuf);
139
if (ibuf->refcounter > 0) {
142
imb_freeplanesImBuf(ibuf);
143
imb_freerectImBuf(ibuf);
144
imb_freerectfloatImBuf(ibuf);
145
IMB_freezbufImBuf(ibuf);
146
IMB_freezbuffloatImBuf(ibuf);
147
IMB_freecmapImBuf(ibuf);
148
freeencodedbufferImBuf(ibuf);
149
IMB_cache_limiter_unmanage(ibuf);
155
void IMB_refImBuf(struct ImBuf * ibuf)
120
160
short addzbufImBuf(struct ImBuf * ibuf)
124
if (ibuf==0) return(FALSE);
164
if (ibuf==NULL) return(FALSE);
125
166
IMB_freezbufImBuf(ibuf);
127
168
size = ibuf->x * ibuf->y * sizeof(unsigned int);
128
if ( (ibuf->zbuf = MEM_mallocN(size, "addzbufImBuf")) ){
169
if ( (ibuf->zbuf = MEM_mapallocN(size, "addzbufImBuf")) ){
129
170
ibuf->mall |= IB_zbuf;
171
ibuf->flags |= IB_zbuf;
178
short addzbuffloatImBuf(struct ImBuf * ibuf)
182
if (ibuf==NULL) return(FALSE);
184
IMB_freezbuffloatImBuf(ibuf);
186
size = ibuf->x * ibuf->y * sizeof(float);
187
if ( (ibuf->zbuf_float = MEM_mapallocN(size, "addzbuffloatImBuf")) ){
188
ibuf->mall |= IB_zbuffloat;
189
ibuf->flags |= IB_zbuffloat;
137
197
short imb_addencodedbufferImBuf(struct ImBuf * ibuf)
139
if (ibuf==0) return(FALSE);
199
if (ibuf==NULL) return(FALSE);
141
201
freeencodedbufferImBuf(ibuf);
159
220
unsigned int newsize, encodedsize;
162
if (ibuf==0) return(FALSE);
223
if (ibuf==NULL) return(FALSE);
164
225
if (ibuf->encodedbuffersize < ibuf->encodedsize) {
165
226
printf("imb_enlargeencodedbufferImBuf: error in parameters\n");
186
247
ibuf->encodedsize = encodedsize;
187
248
ibuf->encodedbuffer = newbuffer;
188
249
ibuf->mall |= IB_mem;
250
ibuf->flags |= IB_mem;
255
short imb_addrectfloatImBuf(struct ImBuf * ibuf)
259
if (ibuf==NULL) return(FALSE);
261
imb_freerectfloatImBuf(ibuf);
263
size = ibuf->x * ibuf->y;
264
size = size * 4 * sizeof(float);
266
if ( (ibuf->rect_float = MEM_mapallocN(size, "imb_addrectfloatImBuf")) ){
267
ibuf->mall |= IB_rectfloat;
268
ibuf->flags |= IB_rectfloat;
275
/* question; why also add zbuf? */
194
276
short imb_addrectImBuf(struct ImBuf * ibuf)
198
if (ibuf==0) return(FALSE);
280
if (ibuf==NULL) return(FALSE);
199
281
imb_freerectImBuf(ibuf);
201
size = ibuf->x * ibuf->y * sizeof(unsigned int);
202
if ( (ibuf->rect = MEM_mallocN(size, "imb_addrectImBuf")) ){
283
size = ibuf->x * ibuf->y;
284
size = size * sizeof(unsigned int);
286
if ( (ibuf->rect = MEM_mapallocN(size, "imb_addrectImBuf")) ){
203
287
ibuf->mall |= IB_rect;
288
ibuf->flags |= IB_rect;
204
289
if (ibuf->depth > 32) return (addzbufImBuf(ibuf));
205
290
else return (TRUE);
261
347
ibuf->mall |= IB_planes;
348
ibuf->flags |= IB_planes;
267
struct ImBuf *IMB_allocImBuf(short x,short y,uchar d,unsigned int flags,uchar bitmap)
354
struct ImBuf *IMB_allocImBuf(short x, short y, uchar d, unsigned int flags, uchar bitmap)
269
356
struct ImBuf *ibuf;
272
359
if (bitmap) flags |= IB_planes;
280
367
if (flags & IB_rect){
281
368
if (imb_addrectImBuf(ibuf)==FALSE){
282
369
IMB_freeImBuf(ibuf);
374
if (flags & IB_rectfloat){
375
if (imb_addrectfloatImBuf(ibuf)==FALSE){
287
381
if (flags & IB_zbuf){
288
382
if (addzbufImBuf(ibuf)==FALSE){
289
383
IMB_freeImBuf(ibuf);
388
if (flags & IB_zbuffloat){
389
if (addzbuffloatImBuf(ibuf)==FALSE){
294
395
if (flags & IB_planes){
295
396
if (imb_addplanesImBuf(ibuf)==FALSE){
296
397
IMB_freeImBuf(ibuf);
405
/* does no zbuffers? */
304
406
struct ImBuf *IMB_dupImBuf(struct ImBuf *ibuf1)
306
408
struct ImBuf *ibuf2, tbuf;
310
if (ibuf1 == 0) return (0);
412
if (ibuf1 == NULL) return NULL;
312
414
if (ibuf1->rect) flags |= IB_rect;
415
if (ibuf1->rect_float) flags |= IB_rectfloat;
313
416
if (ibuf1->planes) flags |= IB_planes;
317
420
if (ibuf1->flags & IB_fields) y *= 2;
319
422
ibuf2 = IMB_allocImBuf(x, y, ibuf1->depth, flags, 0);
320
if (ibuf2 == 0) return (0);
322
if (flags & IB_rect) memcpy(ibuf2->rect,ibuf1->rect,x * y * sizeof(int));
323
if (flags & IB_planes) memcpy(*(ibuf2->planes),*(ibuf1->planes),ibuf1->depth * ibuf1->skipx * y * sizeof(int));
423
if (ibuf2 == NULL) return NULL;
426
memcpy(ibuf2->rect, ibuf1->rect, x * y * sizeof(int));
428
if (flags & IB_rectfloat)
429
memcpy(ibuf2->rect_float, ibuf1->rect_float, 4 * x * y * sizeof(float));
431
if (flags & IB_planes)
432
memcpy(*(ibuf2->planes),*(ibuf1->planes),ibuf1->depth * ibuf1->skipx * y * sizeof(int));
325
434
if (ibuf1->encodedbuffer) {
326
435
ibuf2->encodedbuffersize = ibuf1->encodedbuffersize;
327
436
if (imb_addencodedbufferImBuf(ibuf2) == FALSE) {
328
437
IMB_freeImBuf(ibuf2);
332
441
memcpy(ibuf2->encodedbuffer, ibuf1->encodedbuffer, ibuf1->encodedsize);
444
/* silly trick to copy the entire contents of ibuf1 struct over to ibuf */
338
// pointers goedzetten
339
448
tbuf.rect = ibuf2->rect;
449
tbuf.rect_float = ibuf2->rect_float;
340
450
tbuf.planes = ibuf2->planes;
341
451
tbuf.cmap = ibuf2->cmap;
342
452
tbuf.encodedbuffer = ibuf2->encodedbuffer;
454
tbuf.zbuf_float= NULL;
344
// malloc flag goed zetten
345
457
tbuf.mall = ibuf2->mall;
349
462
if (ibuf1->cmap){
470
/* support for cache limiting */
472
static void imbuf_cache_destructor(void * data)
474
struct ImBuf * ibuf = (struct ImBuf*) data;
476
imb_freeplanesImBuf(ibuf);
477
imb_freerectImBuf(ibuf);
478
imb_freerectfloatImBuf(ibuf);
479
IMB_freezbufImBuf(ibuf);
480
IMB_freezbuffloatImBuf(ibuf);
481
IMB_freecmapImBuf(ibuf);
482
freeencodedbufferImBuf(ibuf);
487
static MEM_CacheLimiterC ** get_imbuf_cache_limiter()
489
static MEM_CacheLimiterC * c = 0;
491
c = new_MEM_CacheLimiter(imbuf_cache_destructor);
496
void IMB_free_cache_limiter()
498
delete_MEM_CacheLimiter(*get_imbuf_cache_limiter());
499
*get_imbuf_cache_limiter() = 0;
502
void IMB_cache_limiter_insert(struct ImBuf * i)
505
i->c_handle = MEM_CacheLimiter_insert(
506
*get_imbuf_cache_limiter(), i);
507
MEM_CacheLimiter_ref(i->c_handle);
508
MEM_CacheLimiter_enforce_limits(
509
*get_imbuf_cache_limiter());
510
MEM_CacheLimiter_unref(i->c_handle);
514
void IMB_cache_limiter_unmanage(struct ImBuf * i)
517
MEM_CacheLimiter_unmanage(i->c_handle);
522
void IMB_cache_limiter_touch(struct ImBuf * i)
525
MEM_CacheLimiter_touch(i->c_handle);
529
void IMB_cache_limiter_ref(struct ImBuf * i)
532
MEM_CacheLimiter_ref(i->c_handle);
536
void IMB_cache_limiter_unref(struct ImBuf * i)
539
MEM_CacheLimiter_unref(i->c_handle);
543
int IMB_cache_limiter_get_refcount(struct ImBuf * i)
546
return MEM_CacheLimiter_get_refcount(i->c_handle);