initTexture method
Implementation
bool initTexture(Map<String, dynamic> textureProperties, Texture texture) {
bool forceUpload = false;
if (textureProperties["__webglInit"] != true) {
textureProperties["__webglInit"] = true;
texture.addEventListener('dispose', onTextureDispose);
// if (texture.isOpenGLTexture) {
// final _texture = texture as OpenGLTexture;
// textureProperties["__webglTexture"] = _texture.openGLTexture;
// } else {
// textureProperties["__webglTexture"] = gl.createTexture();
// }
// info.memory["textures"] = info.memory["textures"]! + 1;
}
// create Source <-> WebGLTextures mapping if necessary
var source = texture.source;
var webglTextures = _sources.get(source);
if (webglTextures == null) {
webglTextures = {};
_sources.set(source, webglTextures);
}
// check if there is already a WebGLTexture object for the given texture parameters
var textureCacheKey = getTextureCacheKey(texture);
if (textureCacheKey != textureProperties["__cacheKey"]) {
// if not, create a new instance of WebGLTexture
if (webglTextures[textureCacheKey] == null) {
// create new entry
webglTextures[textureCacheKey] = {
"texture": _gl.createTexture(),
"usedTimes": 0
};
info.memory["textures"] = info.memory["textures"]! + 1;
// when a new instance of WebGLTexture was created, a texture upload is required
// even if the image contents are identical
forceUpload = true;
}
webglTextures[textureCacheKey]["usedTimes"]++;
// every time the texture cache key changes, it's necessary to check if an instance of
// WebGLTexture can be deleted in order to avoid a memory leak.
var webglTexture = webglTextures[textureProperties["__cacheKey"]];
if (webglTexture != undefined) {
webglTextures[textureProperties["__cacheKey"]]["usedTimes"]--;
if (webglTexture["usedTimes"] == 0) {
deleteTexture(texture);
}
}
// store references to cache key and WebGLTexture object
textureProperties["__cacheKey"] = textureCacheKey;
textureProperties["__webglTexture"] =
webglTextures[textureCacheKey]["texture"];
}
return forceUpload;
}