Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix material caching for canvases #2843

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Source/Scene/Material.js
Original file line number Diff line number Diff line change
Expand Up @@ -949,13 +949,21 @@ define([
_textures : {},

addTexture : function(path, texture) {
if (path instanceof HTMLCanvasElement) {
path = path.toDataURL();
}

this._textures[path] = {
texture : texture,
count : 1
};
},

getTexture : function(path) {
if (path instanceof HTMLCanvasElement) {
path = path.toDataURL();
}

var entry = this._textures[path];

if (defined(entry)) {
Expand All @@ -967,10 +975,14 @@ define([
},

releaseTexture : function(path) {
if (path instanceof HTMLCanvasElement) {
path = path.toDataURL();
}

var entry = this._textures[path];
if (defined(entry) && --entry.count === 0) {
entry.texture = entry.texture && entry.texture.destroy();
this._textures[path] = undefined;
delete this._textures[path];
}
}
};
Expand Down
49 changes: 49 additions & 0 deletions Specs/Scene/MaterialSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,4 +778,53 @@ defineSuite([
expect(material.isDestroyed()).toEqual(true);
expect(diffuseMap.isDestroyed()).toEqual(true);
});

it('_textureCache works with canvas elements', function() {
var mockTexture = {
destroy : function() {
}
};

var canvas = document.createElement('canvas');
canvas.height = 1;
canvas.width = 1;
var context = canvas.getContext('2d');
context.rect(0,0,1,1);
context.fillStyle = 'red';
context.fill();

var mockTexture2 = {
destroy : function() {
}
};

var canvas2 = document.createElement('canvas');
canvas2.height = 1;
canvas2.width = 1;
context = canvas.getContext('2d');
context.rect(0,0,1,1);
context.fillStyle = 'yellow';
context.fill();

expect(Object.keys(Material._textureCache._textures).length).toEqual(0);

Material._textureCache.addTexture(canvas, mockTexture);
expect(Object.keys(Material._textureCache._textures).length).toEqual(1);

Material._textureCache.addTexture(canvas2, mockTexture2);
expect(Object.keys(Material._textureCache._textures).length).toEqual(2);

expect(Material._textureCache.getTexture(canvas)).toBe(mockTexture);
expect(Material._textureCache.getTexture(canvas2)).toBe(mockTexture2);

Material._textureCache.releaseTexture(canvas2);
Material._textureCache.releaseTexture(canvas2);
expect(Object.keys(Material._textureCache._textures).length).toEqual(1);

Material._textureCache.releaseTexture(canvas);
Material._textureCache.releaseTexture(canvas);
expect(Object.keys(Material._textureCache._textures).length).toEqual(0);

});

}, 'WebGL');