Merging r50245 through r50247 from trunk into soc-2011-tomato
[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                         IMB_colormanage_cache_free(ibuf);
167
168                         if (ibuf->dds_data.data != NULL) {
169                                 free(ibuf->dds_data.data); /* dds_data.data is allocated by DirectDrawSurface::readData(), so don't use MEM_freeN! */
170                         }
171                         MEM_freeN(ibuf);
172                 }
173         }
174 }
175
176 void IMB_refImBuf(ImBuf *ibuf)
177 {
178         ibuf->refcounter++;
179 }
180
181 ImBuf *IMB_makeSingleUser(ImBuf *ibuf)
182 {
183         ImBuf *rval;
184
185         if (!ibuf || ibuf->refcounter == 0) { return ibuf; }
186
187         rval = IMB_dupImBuf(ibuf);
188
189         IMB_freeImBuf(ibuf);
190
191         return rval;
192 }
193
194 short addzbufImBuf(ImBuf *ibuf)
195 {
196         int size;
197         
198         if (ibuf == NULL) return FALSE;
199         
200         IMB_freezbufImBuf(ibuf);
201         
202         size = ibuf->x * ibuf->y * sizeof(unsigned int);
203         if ((ibuf->zbuf = MEM_mapallocN(size, "addzbufImBuf"))) {
204                 ibuf->mall |= IB_zbuf;
205                 ibuf->flags |= IB_zbuf;
206                 return TRUE;
207         }
208         
209         return FALSE;
210 }
211
212 short addzbuffloatImBuf(ImBuf *ibuf)
213 {
214         int size;
215         
216         if (ibuf == NULL) return FALSE;
217         
218         IMB_freezbuffloatImBuf(ibuf);
219         
220         size = ibuf->x * ibuf->y * sizeof(float);
221         if ((ibuf->zbuf_float = MEM_mapallocN(size, "addzbuffloatImBuf"))) {
222                 ibuf->mall |= IB_zbuffloat;
223                 ibuf->flags |= IB_zbuffloat;
224                 return TRUE;
225         }
226         
227         return FALSE;
228 }
229
230
231 short imb_addencodedbufferImBuf(ImBuf *ibuf)
232 {
233         if (ibuf == NULL) return FALSE;
234
235         freeencodedbufferImBuf(ibuf);
236
237         if (ibuf->encodedbuffersize == 0)
238                 ibuf->encodedbuffersize = 10000;
239
240         ibuf->encodedsize = 0;
241
242         if ((ibuf->encodedbuffer = MEM_mallocN(ibuf->encodedbuffersize, "addencodedbufferImBuf"))) {
243                 ibuf->mall |= IB_mem;
244                 ibuf->flags |= IB_mem;
245                 return TRUE;
246         }
247
248         return FALSE;
249 }
250
251
252 short imb_enlargeencodedbufferImBuf(ImBuf *ibuf)
253 {
254         unsigned int newsize, encodedsize;
255         void *newbuffer;
256
257         if (ibuf == NULL) return FALSE;
258
259         if (ibuf->encodedbuffersize < ibuf->encodedsize) {
260                 printf("imb_enlargeencodedbufferImBuf: error in parameters\n");
261                 return FALSE;
262         }
263
264         newsize = 2 * ibuf->encodedbuffersize;
265         if (newsize < 10000) newsize = 10000;
266
267         newbuffer = MEM_mallocN(newsize, "enlargeencodedbufferImBuf");
268         if (newbuffer == NULL) return FALSE;
269
270         if (ibuf->encodedbuffer) {
271                 memcpy(newbuffer, ibuf->encodedbuffer, ibuf->encodedsize);
272         }
273         else {
274                 ibuf->encodedsize = 0;
275         }
276
277         encodedsize = ibuf->encodedsize;
278
279         freeencodedbufferImBuf(ibuf);
280
281         ibuf->encodedbuffersize = newsize;
282         ibuf->encodedsize = encodedsize;
283         ibuf->encodedbuffer = newbuffer;
284         ibuf->mall |= IB_mem;
285         ibuf->flags |= IB_mem;
286
287         return TRUE;
288 }
289
290 short imb_addrectfloatImBuf(ImBuf *ibuf)
291 {
292         int size;
293         
294         if (ibuf == NULL) return FALSE;
295         
296         if (ibuf->rect_float)
297                 imb_freerectfloatImBuf(ibuf);  /* frees mipmap too, hrm */
298         
299         size = ibuf->x * ibuf->y;
300         size = size * 4 * sizeof(float);
301         ibuf->channels = 4;
302         
303         if ((ibuf->rect_float = MEM_mapallocN(size, "imb_addrectfloatImBuf"))) {
304                 ibuf->mall |= IB_rectfloat;
305                 ibuf->flags |= IB_rectfloat;
306                 return TRUE;
307         }
308         
309         return FALSE;
310 }
311
312 /* question; why also add zbuf? */
313 short imb_addrectImBuf(ImBuf *ibuf)
314 {
315         int size;
316
317         if (ibuf == NULL) return FALSE;
318         
319         /* don't call imb_freerectImBuf, it frees mipmaps, this call is used only too give float buffers display */
320         if (ibuf->rect && (ibuf->mall & IB_rect))
321                 MEM_freeN(ibuf->rect);
322         ibuf->rect = NULL;
323         
324         size = ibuf->x * ibuf->y;
325         size = size * sizeof(unsigned int);
326
327         if ((ibuf->rect = MEM_mapallocN(size, "imb_addrectImBuf"))) {
328                 ibuf->mall |= IB_rect;
329                 ibuf->flags |= IB_rect;
330                 if (ibuf->planes > 32) return (addzbufImBuf(ibuf));
331                 else return TRUE;
332         }
333
334         return FALSE;
335 }
336
337 short imb_addtilesImBuf(ImBuf *ibuf)
338 {
339         if (ibuf == NULL) return FALSE;
340
341         if (!ibuf->tiles)
342                 if ((ibuf->tiles = MEM_callocN(sizeof(unsigned int *) * ibuf->xtiles * ibuf->ytiles, "imb_tiles")))
343                         ibuf->mall |= IB_tiles;
344
345         return (ibuf->tiles != NULL);
346 }
347
348 ImBuf *IMB_allocImBuf(unsigned int x, unsigned int y, uchar planes, unsigned int flags)
349 {
350         ImBuf *ibuf;
351
352         ibuf = MEM_callocN(sizeof(ImBuf), "ImBuf_struct");
353
354         if (ibuf) {
355                 ibuf->x = x;
356                 ibuf->y = y;
357                 ibuf->planes = planes;
358                 ibuf->ftype = TGA;
359                 ibuf->channels = 4;  /* float option, is set to other values when buffers get assigned */
360                 ibuf->ppm[0] = ibuf->ppm[1] = IMB_DPI_DEFAULT / 0.0254; /* IMB_DPI_DEFAULT -> pixels-per-meter */
361                 
362                 if (flags & IB_rect) {
363                         if (imb_addrectImBuf(ibuf) == FALSE) {
364                                 IMB_freeImBuf(ibuf);
365                                 return NULL;
366                         }
367                 }
368                 
369                 if (flags & IB_rectfloat) {
370                         if (imb_addrectfloatImBuf(ibuf) == FALSE) {
371                                 IMB_freeImBuf(ibuf);
372                                 return NULL;
373                         }
374                 }
375                 
376                 if (flags & IB_zbuf) {
377                         if (addzbufImBuf(ibuf) == FALSE) {
378                                 IMB_freeImBuf(ibuf);
379                                 return NULL;
380                         }
381                 }
382                 
383                 if (flags & IB_zbuffloat) {
384                         if (addzbuffloatImBuf(ibuf) == FALSE) {
385                                 IMB_freeImBuf(ibuf);
386                                 return NULL;
387                         }
388                 }
389         }
390         return (ibuf);
391 }
392
393 /* does no zbuffers? */
394 ImBuf *IMB_dupImBuf(ImBuf *ibuf1)
395 {
396         ImBuf *ibuf2, tbuf;
397         int flags = 0;
398         int a, x, y;
399         
400         if (ibuf1 == NULL) return NULL;
401
402         if (ibuf1->rect) flags |= IB_rect;
403         if (ibuf1->rect_float) flags |= IB_rectfloat;
404
405         x = ibuf1->x;
406         y = ibuf1->y;
407         if (ibuf1->flags & IB_fields) y *= 2;
408         
409         ibuf2 = IMB_allocImBuf(x, y, ibuf1->planes, flags);
410         if (ibuf2 == NULL) return NULL;
411
412         if (flags & IB_rect)
413                 memcpy(ibuf2->rect, ibuf1->rect, x * y * sizeof(int));
414         
415         if (flags & IB_rectfloat)
416                 memcpy(ibuf2->rect_float, ibuf1->rect_float, ibuf1->channels * x * y * sizeof(float));
417
418         if (ibuf1->encodedbuffer) {
419                 ibuf2->encodedbuffersize = ibuf1->encodedbuffersize;
420                 if (imb_addencodedbufferImBuf(ibuf2) == FALSE) {
421                         IMB_freeImBuf(ibuf2);
422                         return NULL;
423                 }
424
425                 memcpy(ibuf2->encodedbuffer, ibuf1->encodedbuffer, ibuf1->encodedsize);
426         }
427
428         /* silly trick to copy the entire contents of ibuf1 struct over to ibuf */
429         tbuf = *ibuf1;
430         
431         /* fix pointers */
432         tbuf.rect          = ibuf2->rect;
433         tbuf.rect_float    = ibuf2->rect_float;
434         tbuf.encodedbuffer = ibuf2->encodedbuffer;
435         tbuf.zbuf          = NULL;
436         tbuf.zbuf_float    = NULL;
437         for (a = 0; a < IB_MIPMAP_LEVELS; a++)
438                 tbuf.mipmap[a] = NULL;
439         tbuf.dds_data.data = NULL;
440         
441         /* set malloc flag */
442         tbuf.mall               = ibuf2->mall;
443         tbuf.c_handle           = NULL;
444         tbuf.refcounter         = 0;
445
446         /* for now don't duplicate metadata */
447         tbuf.metadata = NULL;
448
449         tbuf.display_buffer_flags = NULL;
450         tbuf.colormanage_cache = NULL;
451
452         *ibuf2 = tbuf;
453
454         return(ibuf2);
455 }
456
457 #if 0 /* remove? - campbell */
458 /* support for cache limiting */
459
460 static void imbuf_cache_destructor(void *data)
461 {
462         ImBuf *ibuf = (ImBuf *) data;
463
464         imb_freerectImBuf(ibuf);
465         imb_freerectfloatImBuf(ibuf);
466         IMB_freezbufImBuf(ibuf);
467         IMB_freezbuffloatImBuf(ibuf);
468         freeencodedbufferImBuf(ibuf);
469
470         ibuf->c_handle = NULL;
471 }
472
473
474 static MEM_CacheLimiterC **get_imbuf_cache_limiter(void)
475 {
476         static MEM_CacheLimiterC *c = NULL;
477
478         if (!c)
479                 c = new_MEM_CacheLimiter(imbuf_cache_destructor, NULL);
480
481         return &c;
482 }
483 #endif