|
Blender V2.27
|
00001 /* 00002 * allocimbuf.c 00003 * 00004 * $Id: allocimbuf.c 34761 2011-02-10 15:47:55Z ton $ 00005 * 00006 * ***** BEGIN GPL LICENSE BLOCK ***** 00007 * 00008 * This program is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU General Public License 00010 * as published by the Free Software Foundation; either version 2 00011 * of the License, or (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; if not, write to the Free Software Foundation, 00020 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00021 * 00022 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00023 * All rights reserved. 00024 * 00025 * The Original Code is: all of this file. 00026 * 00027 * Contributor(s): none yet. 00028 * 00029 * ***** END GPL LICENSE BLOCK ***** 00030 */ 00031 00032 /* It's become a bit messy... Basically, only the IMB_ prefixed files 00033 * should remain. */ 00034 00035 #include <stddef.h> 00036 00037 #include "IMB_imbuf.h" 00038 #include "IMB_imbuf_types.h" 00039 00040 #include "IMB_allocimbuf.h" 00041 #include "IMB_filetype.h" 00042 #include "IMB_metadata.h" 00043 00044 #include "imbuf.h" 00045 00046 #include "MEM_CacheLimiterC-Api.h" 00047 #include "MEM_guardedalloc.h" 00048 00049 void imb_freemipmapImBuf(ImBuf *ibuf) 00050 { 00051 int a; 00052 00053 for(a=1; a<ibuf->miptot; a++) { 00054 if(ibuf->mipmap[a-1]) 00055 IMB_freeImBuf(ibuf->mipmap[a-1]); 00056 ibuf->mipmap[a-1]= NULL; 00057 } 00058 00059 ibuf->miptot= 0; 00060 } 00061 00062 /* any free rect frees mipmaps to be sure, creation is in render on first request */ 00063 void imb_freerectfloatImBuf(ImBuf *ibuf) 00064 { 00065 if(ibuf==NULL) return; 00066 00067 if(ibuf->rect_float && (ibuf->mall & IB_rectfloat)) { 00068 MEM_freeN(ibuf->rect_float); 00069 ibuf->rect_float=NULL; 00070 } 00071 00072 imb_freemipmapImBuf(ibuf); 00073 00074 ibuf->rect_float= NULL; 00075 ibuf->mall &= ~IB_rectfloat; 00076 } 00077 00078 /* any free rect frees mipmaps to be sure, creation is in render on first request */ 00079 void imb_freerectImBuf(ImBuf *ibuf) 00080 { 00081 if(ibuf==NULL) return; 00082 00083 if(ibuf->crect) 00084 MEM_freeN(ibuf->crect); 00085 00086 if(ibuf->rect && (ibuf->mall & IB_rect)) 00087 MEM_freeN(ibuf->rect); 00088 00089 imb_freemipmapImBuf(ibuf); 00090 00091 ibuf->rect= NULL; 00092 ibuf->crect= NULL; 00093 ibuf->mall &= ~IB_rect; 00094 } 00095 00096 void imb_freetilesImBuf(ImBuf *ibuf) 00097 { 00098 int tx, ty; 00099 00100 if(ibuf==NULL) return; 00101 00102 if(ibuf->tiles && (ibuf->mall & IB_tiles)) { 00103 for(ty=0; ty<ibuf->ytiles; ty++) { 00104 for(tx=0; tx<ibuf->xtiles; tx++) { 00105 if(ibuf->tiles[ibuf->xtiles*ty + tx]) { 00106 imb_tile_cache_tile_free(ibuf, tx, ty); 00107 MEM_freeN(ibuf->tiles[ibuf->xtiles*ty + tx]); 00108 } 00109 } 00110 } 00111 00112 MEM_freeN(ibuf->tiles); 00113 } 00114 00115 ibuf->tiles= NULL; 00116 ibuf->mall &= ~IB_tiles; 00117 } 00118 00119 static void freeencodedbufferImBuf(ImBuf *ibuf) 00120 { 00121 if(ibuf==NULL) return; 00122 00123 if(ibuf->encodedbuffer && (ibuf->mall & IB_mem)) 00124 MEM_freeN(ibuf->encodedbuffer); 00125 00126 ibuf->encodedbuffer = 0; 00127 ibuf->encodedbuffersize = 0; 00128 ibuf->encodedsize = 0; 00129 ibuf->mall &= ~IB_mem; 00130 } 00131 00132 void IMB_freezbufImBuf(ImBuf *ibuf) 00133 { 00134 if(ibuf==NULL) return; 00135 00136 if(ibuf->zbuf && (ibuf->mall & IB_zbuf)) 00137 MEM_freeN(ibuf->zbuf); 00138 00139 ibuf->zbuf= NULL; 00140 ibuf->mall &= ~IB_zbuf; 00141 } 00142 00143 void IMB_freezbuffloatImBuf(ImBuf *ibuf) 00144 { 00145 if(ibuf==NULL) return; 00146 00147 if(ibuf->zbuf_float && (ibuf->mall & IB_zbuffloat)) 00148 MEM_freeN(ibuf->zbuf_float); 00149 00150 ibuf->zbuf_float= NULL; 00151 ibuf->mall &= ~IB_zbuffloat; 00152 } 00153 00154 void IMB_freeImBuf(ImBuf *ibuf) 00155 { 00156 if(ibuf) { 00157 if(ibuf->refcounter > 0) { 00158 ibuf->refcounter--; 00159 } 00160 else { 00161 imb_freerectImBuf(ibuf); 00162 imb_freerectfloatImBuf(ibuf); 00163 imb_freetilesImBuf(ibuf); 00164 IMB_freezbufImBuf(ibuf); 00165 IMB_freezbuffloatImBuf(ibuf); 00166 freeencodedbufferImBuf(ibuf); 00167 IMB_cache_limiter_unmanage(ibuf); 00168 IMB_metadata_free(ibuf); 00169 MEM_freeN(ibuf); 00170 } 00171 } 00172 } 00173 00174 void IMB_refImBuf(ImBuf *ibuf) 00175 { 00176 ibuf->refcounter++; 00177 } 00178 00179 short addzbufImBuf(ImBuf *ibuf) 00180 { 00181 int size; 00182 00183 if(ibuf==NULL) return FALSE; 00184 00185 IMB_freezbufImBuf(ibuf); 00186 00187 size = ibuf->x *ibuf->y *sizeof(unsigned int); 00188 if((ibuf->zbuf = MEM_mapallocN(size, "addzbufImBuf"))) { 00189 ibuf->mall |= IB_zbuf; 00190 ibuf->flags |= IB_zbuf; 00191 return TRUE; 00192 } 00193 00194 return FALSE; 00195 } 00196 00197 short addzbuffloatImBuf(ImBuf *ibuf) 00198 { 00199 int size; 00200 00201 if(ibuf==NULL) return FALSE; 00202 00203 IMB_freezbuffloatImBuf(ibuf); 00204 00205 size = ibuf->x *ibuf->y *sizeof(float); 00206 if((ibuf->zbuf_float = MEM_mapallocN(size, "addzbuffloatImBuf"))) { 00207 ibuf->mall |= IB_zbuffloat; 00208 ibuf->flags |= IB_zbuffloat; 00209 return TRUE; 00210 } 00211 00212 return FALSE; 00213 } 00214 00215 00216 short imb_addencodedbufferImBuf(ImBuf *ibuf) 00217 { 00218 if(ibuf==NULL) return FALSE; 00219 00220 freeencodedbufferImBuf(ibuf); 00221 00222 if(ibuf->encodedbuffersize == 0) 00223 ibuf->encodedbuffersize = 10000; 00224 00225 ibuf->encodedsize = 0; 00226 00227 if((ibuf->encodedbuffer = MEM_mallocN(ibuf->encodedbuffersize, "addencodedbufferImBuf"))) { 00228 ibuf->mall |= IB_mem; 00229 ibuf->flags |= IB_mem; 00230 return TRUE; 00231 } 00232 00233 return FALSE; 00234 } 00235 00236 00237 short imb_enlargeencodedbufferImBuf(ImBuf *ibuf) 00238 { 00239 unsigned int newsize, encodedsize; 00240 void *newbuffer; 00241 00242 if(ibuf==NULL) return FALSE; 00243 00244 if(ibuf->encodedbuffersize < ibuf->encodedsize) { 00245 printf("imb_enlargeencodedbufferImBuf: error in parameters\n"); 00246 return FALSE; 00247 } 00248 00249 newsize = 2 *ibuf->encodedbuffersize; 00250 if(newsize < 10000) newsize = 10000; 00251 00252 newbuffer = MEM_mallocN(newsize, "enlargeencodedbufferImBuf"); 00253 if(newbuffer == NULL) return FALSE; 00254 00255 if(ibuf->encodedbuffer) { 00256 memcpy(newbuffer, ibuf->encodedbuffer, ibuf->encodedsize); 00257 } else { 00258 ibuf->encodedsize = 0; 00259 } 00260 00261 encodedsize = ibuf->encodedsize; 00262 00263 freeencodedbufferImBuf(ibuf); 00264 00265 ibuf->encodedbuffersize = newsize; 00266 ibuf->encodedsize = encodedsize; 00267 ibuf->encodedbuffer = newbuffer; 00268 ibuf->mall |= IB_mem; 00269 ibuf->flags |= IB_mem; 00270 00271 return TRUE; 00272 } 00273 00274 short imb_addrectfloatImBuf(ImBuf *ibuf) 00275 { 00276 int size; 00277 00278 if(ibuf==NULL) return FALSE; 00279 00280 if(ibuf->rect_float) 00281 imb_freerectfloatImBuf(ibuf); /* frees mipmap too, hrm */ 00282 00283 size = ibuf->x *ibuf->y; 00284 size = size *4 *sizeof(float); 00285 ibuf->channels= 4; 00286 00287 if((ibuf->rect_float = MEM_mapallocN(size, "imb_addrectfloatImBuf"))) { 00288 ibuf->mall |= IB_rectfloat; 00289 ibuf->flags |= IB_rectfloat; 00290 return TRUE; 00291 } 00292 00293 return FALSE; 00294 } 00295 00296 /* question; why also add zbuf? */ 00297 short imb_addrectImBuf(ImBuf *ibuf) 00298 { 00299 int size; 00300 00301 if(ibuf==NULL) return FALSE; 00302 00303 /* don't call imb_freerectImBuf, it frees mipmaps, this call is used only too give float buffers display */ 00304 if(ibuf->rect && (ibuf->mall & IB_rect)) 00305 MEM_freeN(ibuf->rect); 00306 ibuf->rect= NULL; 00307 00308 size = ibuf->x*ibuf->y; 00309 size = size*sizeof(unsigned int); 00310 00311 if((ibuf->rect = MEM_mapallocN(size, "imb_addrectImBuf"))) { 00312 ibuf->mall |= IB_rect; 00313 ibuf->flags |= IB_rect; 00314 if(ibuf->depth > 32) return (addzbufImBuf(ibuf)); 00315 else return TRUE; 00316 } 00317 00318 return FALSE; 00319 } 00320 00321 short imb_addtilesImBuf(ImBuf *ibuf) 00322 { 00323 if(ibuf==NULL) return FALSE; 00324 00325 if(!ibuf->tiles) 00326 if((ibuf->tiles = MEM_callocN(sizeof(unsigned int*)*ibuf->xtiles*ibuf->ytiles, "imb_tiles"))) 00327 ibuf->mall |= IB_tiles; 00328 00329 return (ibuf->tiles != NULL); 00330 } 00331 00332 ImBuf *IMB_allocImBuf(unsigned int x, unsigned int y, uchar d, unsigned int flags) 00333 { 00334 ImBuf *ibuf; 00335 00336 ibuf = MEM_callocN(sizeof(ImBuf), "ImBuf_struct"); 00337 00338 if(ibuf) { 00339 ibuf->x= x; 00340 ibuf->y= y; 00341 ibuf->depth= d; 00342 ibuf->ftype= TGA; 00343 ibuf->channels= 4; /* float option, is set to other values when buffers get assigned */ 00344 00345 if(flags & IB_rect) { 00346 if(imb_addrectImBuf(ibuf)==FALSE) { 00347 IMB_freeImBuf(ibuf); 00348 return NULL; 00349 } 00350 } 00351 00352 if(flags & IB_rectfloat) { 00353 if(imb_addrectfloatImBuf(ibuf)==FALSE) { 00354 IMB_freeImBuf(ibuf); 00355 return NULL; 00356 } 00357 } 00358 00359 if(flags & IB_zbuf) { 00360 if(addzbufImBuf(ibuf)==FALSE) { 00361 IMB_freeImBuf(ibuf); 00362 return NULL; 00363 } 00364 } 00365 00366 if(flags & IB_zbuffloat) { 00367 if(addzbuffloatImBuf(ibuf)==FALSE) { 00368 IMB_freeImBuf(ibuf); 00369 return NULL; 00370 } 00371 } 00372 } 00373 return (ibuf); 00374 } 00375 00376 /* does no zbuffers? */ 00377 ImBuf *IMB_dupImBuf(ImBuf *ibuf1) 00378 { 00379 ImBuf *ibuf2, tbuf; 00380 int flags = 0; 00381 int a, x, y; 00382 00383 if(ibuf1 == NULL) return NULL; 00384 00385 if(ibuf1->rect) flags |= IB_rect; 00386 if(ibuf1->rect_float) flags |= IB_rectfloat; 00387 00388 x = ibuf1->x; 00389 y = ibuf1->y; 00390 if(ibuf1->flags & IB_fields) y *= 2; 00391 00392 ibuf2 = IMB_allocImBuf(x, y, ibuf1->depth, flags); 00393 if(ibuf2 == NULL) return NULL; 00394 00395 if(flags & IB_rect) 00396 memcpy(ibuf2->rect, ibuf1->rect, x *y *sizeof(int)); 00397 00398 if(flags & IB_rectfloat) 00399 memcpy(ibuf2->rect_float, ibuf1->rect_float, ibuf1->channels *x *y *sizeof(float)); 00400 00401 if(ibuf1->encodedbuffer) { 00402 ibuf2->encodedbuffersize = ibuf1->encodedbuffersize; 00403 if(imb_addencodedbufferImBuf(ibuf2) == FALSE) { 00404 IMB_freeImBuf(ibuf2); 00405 return NULL; 00406 } 00407 00408 memcpy(ibuf2->encodedbuffer, ibuf1->encodedbuffer, ibuf1->encodedsize); 00409 } 00410 00411 /* silly trick to copy the entire contents of ibuf1 struct over to ibuf */ 00412 tbuf = *ibuf1; 00413 00414 // fix pointers 00415 tbuf.rect = ibuf2->rect; 00416 tbuf.rect_float = ibuf2->rect_float; 00417 tbuf.encodedbuffer = ibuf2->encodedbuffer; 00418 tbuf.zbuf= NULL; 00419 tbuf.zbuf_float= NULL; 00420 for(a=0; a<IB_MIPMAP_LEVELS; a++) 00421 tbuf.mipmap[a]= NULL; 00422 00423 // set malloc flag 00424 tbuf.mall = ibuf2->mall; 00425 tbuf.c_handle = 0; 00426 tbuf.refcounter = 0; 00427 00428 // for now don't duplicate metadata 00429 tbuf.metadata = 0; 00430 00431 *ibuf2 = tbuf; 00432 00433 return(ibuf2); 00434 } 00435 00436 /* support for cache limiting */ 00437 00438 static void imbuf_cache_destructor(void *data) 00439 { 00440 ImBuf *ibuf = (ImBuf*) data; 00441 00442 imb_freerectImBuf(ibuf); 00443 imb_freerectfloatImBuf(ibuf); 00444 IMB_freezbufImBuf(ibuf); 00445 IMB_freezbuffloatImBuf(ibuf); 00446 freeencodedbufferImBuf(ibuf); 00447 00448 ibuf->c_handle = 0; 00449 } 00450 00451 static MEM_CacheLimiterC **get_imbuf_cache_limiter(void) 00452 { 00453 static MEM_CacheLimiterC *c = 0; 00454 00455 if(!c) 00456 c = new_MEM_CacheLimiter(imbuf_cache_destructor); 00457 00458 return &c; 00459 } 00460 00461 void IMB_free_cache_limiter() 00462 { 00463 delete_MEM_CacheLimiter(*get_imbuf_cache_limiter()); 00464 *get_imbuf_cache_limiter() = 0; 00465 } 00466 00467 void IMB_cache_limiter_insert(ImBuf *i) 00468 { 00469 if(!i->c_handle) { 00470 i->c_handle = MEM_CacheLimiter_insert( 00471 *get_imbuf_cache_limiter(), i); 00472 MEM_CacheLimiter_ref(i->c_handle); 00473 MEM_CacheLimiter_enforce_limits( 00474 *get_imbuf_cache_limiter()); 00475 MEM_CacheLimiter_unref(i->c_handle); 00476 } 00477 } 00478 00479 void IMB_cache_limiter_unmanage(ImBuf *i) 00480 { 00481 if(i->c_handle) { 00482 MEM_CacheLimiter_unmanage(i->c_handle); 00483 i->c_handle = 0; 00484 } 00485 } 00486 00487 void IMB_cache_limiter_touch(ImBuf *i) 00488 { 00489 if(i->c_handle) 00490 MEM_CacheLimiter_touch(i->c_handle); 00491 } 00492 00493 void IMB_cache_limiter_ref(ImBuf *i) 00494 { 00495 if(i->c_handle) 00496 MEM_CacheLimiter_ref(i->c_handle); 00497 } 00498 00499 void IMB_cache_limiter_unref(ImBuf *i) 00500 { 00501 if(i->c_handle) 00502 MEM_CacheLimiter_unref(i->c_handle); 00503 } 00504 00505 int IMB_cache_limiter_get_refcount(ImBuf *i) 00506 { 00507 if(i->c_handle) 00508 return MEM_CacheLimiter_get_refcount(i->c_handle); 00509 00510 return 0; 00511 } 00512
1.7.3