-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImagePool.js
78 lines (67 loc) · 2.17 KB
/
ImagePool.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import Pool from './Pool.js'
import { GetChannelsFromPixelFormat,IsFloatFormat } from './PopWebImageApi.js'
const PopImage = Pop.Image;
function GetTypedArrayConstructor(Format)
{
if ( IsFloatFormat(Format) )
return Float32Array;
else
return Uint8Array;
}
const DummyBuffers = {}; // [200x100xRGBA] = typedarray
function GetDummyBuffer(Width,Height,Format)
{
const Key = `${Width}x${Height}x${Format}`;
if ( !DummyBuffers.hasOwnProperty(Key) )
{
const Constructor = GetTypedArrayConstructor(Format);
const Channels = GetChannelsFromPixelFormat(Format);
DummyBuffers[Key] = new Constructor( Width * Height * Channels );
}
return DummyBuffers[Key];
}
export class ImagePool extends Pool
{
constructor(Name,OnWarning=function(){})
{
OnWarning = OnWarning || function(){};
let Debug_AllocatedImageCounter = 0;
const OnDebug = function(){};//OnWarning;
function PopFromFreeList(FreeImages,Width,Height,Format)
{
for ( let i=0; i<FreeImages.length; i++ )
{
const FreeImage = FreeImages[i];
if ( !FreeImage )
{
Pop.Warning(`Null${FreeImage} image in image pool`);
continue;
}
if ( FreeImage.GetWidth() != Width )
continue;
if ( FreeImage.GetHeight() != Height )
continue;
if ( FreeImage.GetFormat() != Format )
continue;
OnDebug(`A) Found pool (${this.Name}) match ${Width},${Height},${Format} name=${FreeImage.Name}`);
FreeImages.splice(i,1);
return FreeImage;
}
OnWarning(`A) No pool(${this.Name}) image matching ${Width}x${Height}_${Format} (free=x${FreeImages.length})`);
return false;
}
function AllocImage(Width,Height,Format)
{
const Image = new PopImage(`ImagePool#${Debug_AllocatedImageCounter} ${Width}x${Height}_${Format} `);
Image.PoolAllocatedIndex = Debug_AllocatedImageCounter;
Debug_AllocatedImageCounter++;
// gr: don't allocate a pixel array, let the image object
// handle that. if we need pixels, and there isn't a buffer, it should
// allocate it itself (this is to handle Yuv_8_88 easily)
Image.WritePixels( Width, Height, null, Format );
return Image;
}
super( Name, AllocImage, OnWarning, PopFromFreeList );
}
}
export default ImagePool;