Only create new atlas after testing all existing

This commit is contained in:
tsauerwein
2014-11-06 15:28:44 +01:00
parent 509fbaee1c
commit 5ba6ddcecf
2 changed files with 17 additions and 5 deletions

View File

@@ -72,7 +72,7 @@ ol.renderer.webgl.AtlasManager = function(opt_size, opt_maxSize, opt_space) {
*/ */
ol.renderer.webgl.AtlasManager.prototype.getInfo = function(hash) { ol.renderer.webgl.AtlasManager.prototype.getInfo = function(hash) {
var atlas, info; var atlas, info;
for (var i = 0, l = this.atlases_.length; i < l; i++) { for (var i = 0, ii = this.atlases_.length; i < ii; i++) {
atlas = this.atlases_[i]; atlas = this.atlases_[i];
info = atlas.get(hash); info = atlas.get(hash);
if (info !== null) { if (info !== null) {
@@ -105,18 +105,18 @@ ol.renderer.webgl.AtlasManager.prototype.add =
} }
var atlas, info; var atlas, info;
for (var i = 0, l = this.atlases_.length; i < l; i++) { for (var i = 0, ii = this.atlases_.length; i < ii; i++) {
atlas = this.atlases_[i]; atlas = this.atlases_[i];
info = atlas.add(hash, width, height, renderCallback, opt_this); info = atlas.add(hash, width, height, renderCallback, opt_this);
if (info !== null) { if (info !== null) {
return info; return info;
} else { } else if (info === null && i === ii - 1) {
// the entry could not be added to one of the existing atlases, // the entry could not be added to one of the existing atlases,
// create a new atlas that is twice as big and try to add to this one. // create a new atlas that is twice as big and try to add to this one.
this.currentSize_ = Math.min(this.currentSize_ * 2, this.maxSize_); this.currentSize_ = Math.min(this.currentSize_ * 2, this.maxSize_);
atlas = new ol.renderer.webgl.Atlas(this.currentSize_, this.space_); atlas = new ol.renderer.webgl.Atlas(this.currentSize_, this.space_);
this.atlases_.push(atlas); this.atlases_.push(atlas);
l++; ii++;
} }
} }
}; };
@@ -200,7 +200,7 @@ ol.renderer.webgl.Atlas.prototype.get = function(hash) {
ol.renderer.webgl.Atlas.prototype.add = ol.renderer.webgl.Atlas.prototype.add =
function(hash, width, height, renderCallback, opt_this) { function(hash, width, height, renderCallback, opt_this) {
var block; var block;
for (var i = 0, l = this.emptyBlocks_.length; i < l; i++) { for (var i = 0, ii = this.emptyBlocks_.length; i < ii; i++) {
block = this.emptyBlocks_[i]; block = this.emptyBlocks_[i];
if (block.width >= width + this.space_ && if (block.width >= width + this.space_ &&
block.height >= height + this.space_) { block.height >= height + this.space_) {

View File

@@ -207,12 +207,24 @@ describe('ol.renderer.webgl.AtlasManager', function() {
it('creates new atlases until one is large enough', function() { it('creates new atlases until one is large enough', function() {
var manager = new ol.renderer.webgl.AtlasManager(128); var manager = new ol.renderer.webgl.AtlasManager(128);
expect(manager.add(1, 100, 100, defaultRender)).to.be.ok(); expect(manager.add(1, 100, 100, defaultRender)).to.be.ok();
expect(manager.atlases_).to.have.length(1);
var info = manager.add(2, 500, 500, defaultRender); var info = manager.add(2, 500, 500, defaultRender);
expect(info).to.be.ok(); expect(info).to.be.ok();
expect(info.image.width).to.eql(512); expect(info.image.width).to.eql(512);
expect(manager.atlases_).to.have.length(3); expect(manager.atlases_).to.have.length(3);
}); });
it('checks all existing atlases and create a new if needed', function() {
var manager = new ol.renderer.webgl.AtlasManager(128);
expect(manager.add(1, 100, 100, defaultRender)).to.be.ok();
expect(manager.add(2, 100, 100, defaultRender)).to.be.ok();
expect(manager.atlases_).to.have.length(2);
var info = manager.add(3, 500, 500, defaultRender);
expect(info).to.be.ok();
expect(info.image.width).to.eql(512);
expect(manager.atlases_).to.have.length(3);
});
it('returns null if the size exceeds the maximum size', function() { it('returns null if the size exceeds the maximum size', function() {
var manager = new ol.renderer.webgl.AtlasManager(128); var manager = new ol.renderer.webgl.AtlasManager(128);
expect(manager.add(1, 100, 100, defaultRender)).to.be.ok(); expect(manager.add(1, 100, 100, defaultRender)).to.be.ok();