93ee5a42fe5e86f0f7b49ab4c38c0ad9ada8d921
[blender.git] / source / blender / imbuf / intern / allocimbuf.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/imbuf/intern/allocimbuf.c
29  *  \ingroup imbuf
30  */
31
32
33 /* It's become a bit messy... Basically, only the IMB_ prefixed files
34  * should remain. */
35
36 #include <stddef.h>
37
38 #include "IMB_imbuf.h"
39 #include "IMB_imbuf_types.h"
40
41 #include "IMB_allocimbuf.h"
42 #include "IMB_filetype.h"
43 #include "IMB_metadata.h"
44 #include "IMB_colormanagement.h"
45
46 #include "imbuf.h"
47
48 #include "MEM_guardedalloc.h"
49 #include "MEM_CacheLimiterC-Api.h"
50
51 void imb_freemipmapImBuf(ImBuf *ibuf)
52 {
53         int a;
54         
55         for (a = 1; a < ibuf->miptot; a++) {
56                 if (ibuf->mipmap[a - 1])
57                         IMB_freeImBuf(ibuf->mipmap[a - 1]);
58                 ibuf->mipmap[a - 1] = NULL;
59         }
60
61         ibuf->miptot = 0;
62 }
63
64 /* any free rect frees mipmaps to be sure, creation is in render on first request */
65 void imb_freerectfloatImBuf(ImBuf *ibuf)
66 {
67         if (ibuf == NULL) return;
68         
69         if (ibuf->rect_float && (ibuf->mall & IB_rectfloat)) {
70                 MEM_freeN(ibuf->rect_float);
71                 ibuf->rect_float = NULL;
72         }
73
74         imb_freemipmapImBuf(ibuf);
75         
76         ibuf->rect_float = NULL;
77         ibuf->mall &= ~IB_rectfloat;
78 }
79
80 /* any free rect frees mipmaps to be sure, creation is in render on first request */
81 void imb_freerectImBuf(ImBuf *ibuf)
82 {
83         if (ibuf == NULL) return;
84
85         if (ibuf->rect && (ibuf->mall & IB_rect))
86                 MEM_freeN(ibuf->rect);
87         ibuf->rect = NULL;
88         
89         imb_freemipmapImBuf(ibuf);
90
91         ibuf->mall &= ~IB_rect;
92 }
93
94 void imb_freetilesImBuf(ImBuf *ibuf)
95 {
96         int tx, ty;
97
98         if (ibuf == NULL) return;
99
100         if (ibuf->tiles && (ibuf->mall & IB_tiles)) {
101                 for (ty = 0; ty < ibuf->ytiles; ty++) {
102                         for (tx = 0; tx < ibuf->xtiles; tx++) {
103                                 if (ibuf->tiles[ibuf->xtiles * ty + tx]) {
104                                         imb_tile_cache_tile_free(ibuf, tx, ty);
105                                         MEM_freeN(ibuf->tiles[ibuf->xtiles * ty + tx]);
106                                 }
107                         }
108                 }
109
110                 MEM_freeN(ibuf->tiles);
111         }
112
113         ibuf->tiles = NULL;
114         ibuf->mall &= ~IB_tiles;
115 }
116
117 static void freeencodedbufferImBuf(ImBuf *ibuf)
118 {
119         if (ibuf == NULL) return;
120
121         if (ibuf->encodedbuffer && (ibuf->mall & IB_mem))
122                 MEM_freeN(ibuf->encodedbuffer);
123
124         ibuf->encodedbuffer = NULL;
125         ibuf->encodedbuffersize = 0;
126         ibuf->encodedsize = 0;
127         ibuf->mall &= ~IB_mem;
128 }
129
130 void IMB_freezbufImBuf(ImBuf *ibuf)
131 {
132         if (ibuf == NULL) return;
133
134         if (ibuf->zbuf && (ibuf->mall & IB_zbuf))
135                 MEM_freeN(ibuf->zbuf);
136
137         ibuf->zbuf = NULL;
138         ibuf->mall &= ~IB_zbuf;
139 }
140
141 void IMB_freezbuffloatImBuf(ImBuf *ibuf)
142 {
143         if (ibuf == NULL) return;
144
145         if (ibuf->zbuf_float && (ibuf->mall & IB_zbuffloat))
146                 MEM_freeN(ibuf->zbuf_float);
147
148         ibuf->zbuf_float = NULL;
149         ibuf->mall &= ~IB_zbuffloat;
150 }
151
152 void IMB_freeImBuf(ImBuf *ibuf)
153 {
154         if (ibuf) {
155                 if (ibuf->refcounter > 0) {
156                         ibuf->refcounter--;
157                 }
158                 else {
159                         imb_freerectImBuf(ibuf);
160                         imb_freerectfloatImBuf(ibuf);
161                         imb_freetilesImBuf(ibuf);
162                         IMB_freezbufImBuf(ibuf);
163                         IMB_freezbuffloatImBuf(ibuf);
164                         freeencodedbufferImBuf(ibuf);
165                         IMB_metadata_free(ibuf);
166                         MEM_freeN(ibuf);
167                 }
168         }
169 }
170
171 void IMB_refImBuf(ImBuf *ibuf)
172 {
173         ibuf->refcounter++;
174 }
175
176 ImBuf *IMB_makeSingleUser(ImBuf *ibuf)
177 {
178         ImBuf *rval;
179
180         if (!ibuf || ibuf->refcounter == 0) { return ibuf; }
181
182         rval = IMB_dupImBuf(ibuf);
183
184         IMB_freeImBuf(ibuf);
185
186         return rval;
187 }
188
189 short addzbufImBuf(ImBuf *ibuf)
190 {
191         int size;
192         
193         if (ibuf == NULL) return FALSE;
194         
195         IMB_freezbufImBuf(ibuf);
196         
197         size = ibuf->x * ibuf->y * sizeof(unsigned int);
198         if ((ibuf->zbuf = MEM_mapallocN(size, "addzbufImBuf"))) {
199                 ibuf->mall |= IB_zbuf;
200                 ibuf->flags |= IB_zbuf;
201                 return TRUE;
202         }
203         
204         return FALSE;
205 }
206
207 short addzbuffloatImBuf(ImBuf *ibuf)
208 {
209         int size;
210         
211         if (ibuf == NULL) return FALSE;
212         
213         IMB_freezbuffloatImBuf(ibuf);
214         
215         size = ibuf->x * ibuf->y * sizeof(float);
216         if ((ibuf->zbuf_float = MEM_mapallocN(size, "addzbuffloatImBuf"))) {
217                 ibuf->mall |= IB_zbuffloat;
218                 ibuf->flags |= IB_zbuffloat;
219                 return TRUE;
220         }
221         
222         return FALSE;
223 }
224
225
226 short imb_addencodedbufferImBuf(ImBuf *ibuf)
227 {
228         if (ibuf == NULL) return FALSE;
229
230         freeencodedbufferImBuf(ibuf);
231
232         if (ibuf->encodedbuffersize == 0)
233                 ibuf->encodedbuffersize = 10000;
234
235         ibuf->encodedsize = 0;
236
237         if ((ibuf->encodedbuffer = MEM_mallocN(ibuf->encodedbuffersize, "addencodedbufferImBuf"))) {
238                 ibuf->mall |= IB_mem;
239                 ibuf->flags |= IB_mem;
240                 return TRUE;
241         }
242
243         return FALSE;
244 }
245
246
247 short imb_enlargeencodedbufferImBuf(ImBuf *ibuf)
248 {
249         unsigned int newsize, encodedsize;
250         void *newbuffer;
251
252         if (ibuf == NULL) return FALSE;
253
254         if (ibuf->encodedbuffersize < ibuf->encodedsize) {
255                 printf("imb_enlargeencodedbufferImBuf: error in parameters\n");
256                 return FALSE;
257         }
258
259         newsize = 2 * ibuf->encodedbuffersize;
260         if (newsize < 10000) newsize = 10000;
261
262         newbuffer = MEM_mallocN(newsize, "enlargeencodedbufferImBuf");
263         if (newbuffer == NULL) return FALSE;
264
265         if (ibuf->encodedbuffer) {
266                 memcpy(newbuffer, ibuf->encodedbuffer, ibuf->encodedsize);
267         }
268         else {
269                 ibuf->encodedsize = 0;
270         }
271
272         encodedsize = ibuf->encodedsize;
273
274         freeencodedbufferImBuf(ibuf);
275
276         ibuf->encodedbuffersize = newsize;
277         ibuf->encodedsize = encodedsize;
278         ibuf->encodedbuffer = newbuffer;
279         ibuf->mall |= IB_mem;
280         ibuf->flags |= IB_mem;
281
282         return TRUE;
283 }
284
285 short imb_addrectfloatImBuf(ImBuf *ibuf)
286 {
287         int size;
288         
289         if (ibuf == NULL) return FALSE;
290         
291         if (ibuf->rect_float)
292                 imb_freerectfloatImBuf(ibuf);  /* frees mipmap too, hrm */
293         
294         size = ibuf->x * ibuf->y;
295         size = size * 4 * sizeof(float);
296         ibuf->channels = 4;
297         
298         if ((ibuf->rect_float = MEM_mapallocN(size, "imb_addrectfloatImBuf"))) {
299                 ibuf->mall |= IB_rectfloat;
300                 ibuf->flags |= IB_rectfloat;
301                 return TRUE;
302         }
303         
304         return FALSE;
305 }
306
307 /* question; why also add zbuf? */
308 short imb_addrectImBuf(ImBuf *ibuf)
309 {
310         int size;
311
312         if (ibuf == NULL) return FALSE;
313         
314         /* don't call imb_freerectImBuf, it frees mipmaps, this call is used only too give float buffers display */
315         if (ibuf->rect && (ibuf->mall & IB_rect))
316                 MEM_freeN(ibuf->rect);
317         ibuf->rect = NULL;
318         
319         size = ibuf->x * ibuf->y;
320         size = size * sizeof(unsigned int);
321
322         if ((ibuf->rect = MEM_mapallocN(size, "imb_addrectImBuf"))) {
323                 ibuf->mall |= IB_rect;
324                 ibuf->flags |= IB_rect;
325                 if (ibuf->planes > 32) return (addzbufImBuf(ibuf));
326                 else return TRUE;
327         }
328
329         return FALSE;
330 }
331
332 short imb_addtilesImBuf(ImBuf *ibuf)
333 {
334         if (ibuf == NULL) return FALSE;
335
336         if (!ibuf->tiles)
337                 if ((ibuf->tiles = MEM_callocN(sizeof(unsigned int *) * ibuf->xtiles * ibuf->ytiles, "imb_tiles")))
338                         ibuf->mall |= IB_tiles;
339
340         return (ibuf->tiles != NULL);
341 }
342
343 ImBuf *IMB_allocImBuf(unsigned int x, unsigned int y, uchar planes, unsigned int flags)
344 {
345         ImBuf *ibuf;
346
347         ibuf = MEM_callocN(sizeof(ImBuf), "ImBuf_struct");
348
349         if (ibuf) {
350                 ibuf->x = x;
351                 ibuf->y = y;
352                 ibuf->planes = planes;
353                 ibuf->ftype = TGA;
354                 ibuf->channels = 4;  /* float option, is set to other values when buffers get assigned */
355                 ibuf->ppm[0] = ibuf->ppm[1] = IMB_DPI_DEFAULT / 0.0254; /* IMB_DPI_DEFAULT -> pixels-per-meter */
356                 
357                 if (flags & IB_rect) {
358                         if (imb_addrectImBuf(ibuf) == FALSE) {
359                                 IMB_freeImBuf(ibuf);
360                                 return NULL;
361                         }
362                 }
363                 
364                 if (flags & IB_rectfloat) {
365                         if (imb_addrectfloatImBuf(ibuf) == FALSE) {
366                                 IMB_freeImBuf(ibuf);
367                                 return NULL;
368                         }
369                 }
370                 
371                 if (flags & IB_zbuf) {
372                         if (addzbufImBuf(ibuf) == FALSE) {
373                                 IMB_freeImBuf(ibuf);
374                                 return NULL;
375                         }
376                 }
377                 
378                 if (flags & IB_zbuffloat) {
379                         if (addzbuffloatImBuf(ibuf) == FALSE) {
380                                 IMB_freeImBuf(ibuf);
381                                 return NULL;
382                         }
383                 }
384         }
385         return (ibuf);
386 }
387
388 /* does no zbuffers? */
389 ImBuf *IMB_dupImBuf(ImBuf *ibuf1)
390 {
391         ImBuf *ibuf2, tbuf;
392         int flags = 0;
393         int a, x, y;
394         
395         if (ibuf1 == NULL) return NULL;
396
397         if (ibuf1->rect) flags |= IB_rect;
398         if (ibuf1->rect_float) flags |= IB_rectfloat;
399
400         x = ibuf1->x;
401         y = ibuf1->y;
402         if (ibuf1->flags & IB_fields) y *= 2;
403         
404         ibuf2 = IMB_allocImBuf(x, y, ibuf1->planes, flags);
405         if (ibuf2 == NULL) return NULL;
406
407         if (flags & IB_rect)
408                 memcpy(ibuf2->rect, ibuf1->rect, x * y * sizeof(int));
409         
410         if (flags & IB_rectfloat)
411                 memcpy(ibuf2->rect_float, ibuf1->rect_float, ibuf1->channels * x * y * sizeof(float));
412
413         if (ibuf1->encodedbuffer) {
414                 ibuf2->encodedbuffersize = ibuf1->encodedbuffersize;
415                 if (imb_addencodedbufferImBuf(ibuf2) == FALSE) {
416                         IMB_freeImBuf(ibuf2);
417                         return NULL;
418                 }
419
420                 memcpy(ibuf2->encodedbuffer, ibuf1->encodedbuffer, ibuf1->encodedsize);
421         }
422
423         /* silly trick to copy the entire contents of ibuf1 struct over to ibuf */
424         tbuf = *ibuf1;
425         
426         // fix pointers 
427         tbuf.rect          = ibuf2->rect;
428         tbuf.rect_float    = ibuf2->rect_float;
429         tbuf.encodedbuffer = ibuf2->encodedbuffer;
430         tbuf.zbuf          = NULL;
431         tbuf.zbuf_float    = NULL;
432         for (a = 0; a < IB_MIPMAP_LEVELS; a++)
433                 tbuf.mipmap[a] = NULL;
434         
435         // set malloc flag
436         tbuf.mall               = ibuf2->mall;
437         tbuf.c_handle           = NULL;
438         tbuf.refcounter         = 0;
439
440         // for now don't duplicate metadata
441         tbuf.metadata = NULL;
442
443         *ibuf2 = tbuf;
444
445         IMB_display_buffer_invalidate(ibuf2);
446         ibuf2->colormanage_flags &= ~ IMB_COLORMANAGED;
447
448         return(ibuf2);
449 }
450
451 #if 0 /* remove? - campbell */
452 /* support for cache limiting */
453
454 static void imbuf_cache_destructor(void *data)
455 {
456         ImBuf *ibuf = (ImBuf *) data;
457
458         imb_freerectImBuf(ibuf);
459         imb_freerectfloatImBuf(ibuf);
460         IMB_freezbufImBuf(ibuf);
461         IMB_freezbuffloatImBuf(ibuf);
462         freeencodedbufferImBuf(ibuf);
463
464         ibuf->c_handle = NULL;
465 }
466
467
468 static MEM_CacheLimiterC **get_imbuf_cache_limiter(void)
469 {
470         static MEM_CacheLimiterC *c = NULL;
471
472         if (!c)
473                 c = new_MEM_CacheLimiter(imbuf_cache_destructor, NULL);
474
475         return &c;
476 }
477 #endif