/
TexturePool.js
154 lines (140 loc) · 5.66 KB
/
TexturePool.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*global define*/
define([
'../Core/destroyObject',
'../Core/DeveloperError',
'../Renderer/PixelDatatype',
'../Renderer/PixelFormat',
'../Renderer/Texture'
], function(
destroyObject,
DeveloperError,
PixelDatatype,
PixelFormat,
Texture) {
"use strict";
var PooledTexture;
function createPooledTexture(texture, textureTypeKey, pool) {
if (typeof PooledTexture === 'undefined') {
// define the class only when needed, so we can use modern
// language features without breaking legacy browsers at setup time.
PooledTexture = function(texture, textureTypeKey, pool) {
this._texture = texture;
this._textureTypeKey = textureTypeKey;
this._pool = pool;
};
// pass through all methods to the underlying texture
Object.keys(Texture.prototype).forEach(function(methodName) {
PooledTexture.prototype[methodName] = function() {
var texture = this._texture;
return texture[methodName].apply(texture, arguments);
};
});
// except for destroy, which releases back into the pool
PooledTexture.prototype.destroy = function() {
var freeList = this._pool._free[this._textureTypeKey];
if (typeof freeList === 'undefined') {
freeList = this._pool._free[this._textureTypeKey] = [];
}
if (freeList.length >= 8) {
this._texture.destroy();
} else {
freeList.push(this);
}
};
}
return new PooledTexture(texture, textureTypeKey, pool);
}
/**
* A pool of textures. Textures created from the pool will be released back into the pool
* when destroy() is called, so future calls to create may re-use a released texture.
* <br/><br/>
* Texture pools are useful when textures are being created and destroyed repeatedly.
*
* @alias Texture2DPool
* @constructor
*
* @see Texture
*/
var TexturePool = function() {
this._free = {};
};
/**
* Create a texture. This function takes the same arguments as {@link Context#createTexture2D},
* but may return a pooled texture if there are any available. If a pooled texture is re-used,
* and no source is provided, the new texture will still retain its old contents.
*
* @memberof TexturePool
*
* @param {Context} context The context to use to create textures when needed.
*
* @exception {DeveloperError} description is required.
*
* @see Context#createTexture2D
*/
TexturePool.prototype.createTexture2D = function(context, description) {
if (!description) {
throw new DeveloperError('description is required.');
}
var source = description.source;
var width = typeof source !== 'undefined' ? source.width : description.width;
var height = typeof source !== 'undefined' ? source.height : description.height;
//coerce values to primitive numbers to make textureTypeKey smaller.
var pixelFormat = +(description.pixelFormat || PixelFormat.RGBA);
var pixelDatatype = +(description.pixelDatatype || PixelDatatype.UNSIGNED_BYTE);
var preMultiplyAlpha = +(description.preMultiplyAlpha || pixelFormat === PixelFormat.RGB || pixelFormat === PixelFormat.LUMINANCE);
var textureTypeKey = JSON.stringify([width, height, pixelFormat, pixelDatatype, preMultiplyAlpha]);
var freeList = this._free[textureTypeKey];
if (typeof freeList !== 'undefined' && freeList.length > 0) {
var texture = freeList.pop();
if (typeof source !== 'undefined') {
texture.copyFrom(source);
}
return texture;
}
return createPooledTexture(context.createTexture2D(description), textureTypeKey, this);
};
/**
* Returns true if this object was destroyed; otherwise, false.
* <br /><br />
* If this object was destroyed, it should not be used; calling any function other than
* <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
*
* @memberof TexturePool
*
* @return {Boolean} True if this object was destroyed; otherwise, false.
*
* @see TexturePool#destroy
*/
TexturePool.prototype.isDestroyed = function() {
return false;
};
/**
* Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
* release of WebGL resources, instead of relying on the garbage collector to destroy this object.
* <br /><br />
* Once an object is destroyed, it should not be used; calling any function other than
* <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
* assign the return value (<code>undefined</code>) to the object as done in the example.
*
* @memberof TexturePool
*
* @return {undefined}
*
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*
* @see TexturePool#isDestroyed
*
* @example
* pool = pool && pool.destroy();
*/
TexturePool.prototype.destroy = function() {
var free = this._free;
Object.keys(free).forEach(function(textureTypeKey) {
free[textureTypeKey].forEach(function(texture) {
texture._texture.destroy();
});
});
return destroyObject(this);
};
return TexturePool;
});