From a705c6fe11e4683e2f6066ef3d53ed31264a2fc7 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Wed, 8 Jul 2015 15:10:08 -0600 Subject: [PATCH 1/4] Test two queues sharing tiles --- test/spec/ol/tilequeue.test.js | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/test/spec/ol/tilequeue.test.js b/test/spec/ol/tilequeue.test.js index 142ceb44af..ddee7bd997 100644 --- a/test/spec/ol/tilequeue.test.js +++ b/test/spec/ol/tilequeue.test.js @@ -13,6 +13,64 @@ describe('ol.TileQueue', function() { } } + var tileId = 0; + function createImageTile() { + ++tileId; + var tileCoord = [tileId, tileId, tileId]; + var state = ol.TileState.IDLE; + var src = 'data:image/gif;base64,R0lGODlhAQABAPAAAP8AAP///' + + 'yH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==#' + tileId; + + return new ol.ImageTile(tileCoord, state, src, null, + ol.source.Image.defaultImageLoadFunction); + } + + describe('#loadMoreTiles()', function() { + var noop = function() {}; + + it('works when tile queues share tiles', function(done) { + var q1 = new ol.TileQueue(noop, noop); + var q2 = new ol.TileQueue(noop, noop); + + var numTiles = 20; + for (var i = 0; i < numTiles; ++i) { + var tile = createImageTile(); + q1.enqueue([tile]); + q2.enqueue([tile]); + } + + var maxLoading = numTiles / 2; + + expect(q1.getTilesLoading()).to.equal(0); + expect(q2.getTilesLoading()).to.equal(0); + + // ask both to load + q1.loadMoreTiles(maxLoading, maxLoading); + q2.loadMoreTiles(maxLoading, maxLoading); + + // since tiles can only load once, we expect one queue to load them + expect(q1.getTilesLoading()).to.equal(maxLoading); + expect(q2.getTilesLoading()).to.equal(0); + + // ask both to load more + q1.loadMoreTiles(maxLoading, maxLoading); + q2.loadMoreTiles(maxLoading, maxLoading); + + // after the first is saturated, the second should start loading + expect(q1.getTilesLoading()).to.equal(maxLoading); + expect(q2.getTilesLoading()).to.equal(maxLoading); + + // let all tiles load + setTimeout(function() { + expect(q1.getTilesLoading()).to.equal(0); + expect(q2.getTilesLoading()).to.equal(0); + done(); + }, 20); + + }); + + }); + describe('heapify', function() { it('does convert an arbitrary array into a heap', function() { @@ -56,6 +114,9 @@ describe('ol.TileQueue', function() { }); }); +goog.require('ol.ImageTile'); goog.require('ol.Tile'); +goog.require('ol.TileState'); goog.require('ol.TileQueue'); +goog.require('ol.source.Image'); goog.require('ol.structs.PriorityQueue'); From b57cdb730c5b57450a2f9adcca44038994181165 Mon Sep 17 00:00:00 2001 From: Alessandro Isaacs Date: Fri, 10 Jul 2015 08:17:14 -0700 Subject: [PATCH 2/4] Only load tiles that are not yet loaded --- src/ol/tilequeue.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ol/tilequeue.js b/src/ol/tilequeue.js index 5da58713fc..a50d588783 100644 --- a/src/ol/tilequeue.js +++ b/src/ol/tilequeue.js @@ -94,9 +94,11 @@ ol.TileQueue.prototype.loadMoreTiles = function(maxTotalLoading, maxNewLoads) { var i, tile; for (i = 0; i < newLoads; ++i) { tile = /** @type {ol.Tile} */ (this.dequeue()[0]); - goog.events.listen(tile, goog.events.EventType.CHANGE, - this.handleTileChange, false, this); - tile.load(); + if (tile.getState() === ol.TileState.IDLE) { + goog.events.listen(tile, goog.events.EventType.CHANGE, + this.handleTileChange, false, this); + tile.load(); + ++this.tilesLoading_; + } } - this.tilesLoading_ += newLoads; }; From 5149889bd250a6230ff3811ce8fee6f5f4b20f6c Mon Sep 17 00:00:00 2001 From: Alessandro Isaacs Date: Fri, 10 Jul 2015 11:25:02 -0700 Subject: [PATCH 3/4] Improve the test --- test/spec/ol/tilequeue.test.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/spec/ol/tilequeue.test.js b/test/spec/ol/tilequeue.test.js index ddee7bd997..35aa488899 100644 --- a/test/spec/ol/tilequeue.test.js +++ b/test/spec/ol/tilequeue.test.js @@ -39,6 +39,10 @@ describe('ol.TileQueue', function() { q2.enqueue([tile]); } + // Initially, both have all tiles. + expect(q1.getCount()).to.equal(numTiles); + expect(q2.getCount()).to.equal(numTiles); + var maxLoading = numTiles / 2; expect(q1.getTilesLoading()).to.equal(0); @@ -52,10 +56,18 @@ describe('ol.TileQueue', function() { expect(q1.getTilesLoading()).to.equal(maxLoading); expect(q2.getTilesLoading()).to.equal(0); + // however, both queues will be reduced + expect(q1.getCount()).to.equal(numTiles - maxLoading); + expect(q2.getCount()).to.equal(numTiles - maxLoading); + // ask both to load more q1.loadMoreTiles(maxLoading, maxLoading); q2.loadMoreTiles(maxLoading, maxLoading); + // now the second queue will be empty + expect(q1.getCount()).to.equal(numTiles - maxLoading); + expect(q2.getCount()).to.equal(0); + // after the first is saturated, the second should start loading expect(q1.getTilesLoading()).to.equal(maxLoading); expect(q2.getTilesLoading()).to.equal(maxLoading); @@ -64,6 +76,14 @@ describe('ol.TileQueue', function() { setTimeout(function() { expect(q1.getTilesLoading()).to.equal(0); expect(q2.getTilesLoading()).to.equal(0); + + // load again, which will clear the first queue + q1.loadMoreTiles(maxLoading, maxLoading); + q2.loadMoreTiles(maxLoading, maxLoading); + + expect(q1.getCount()).to.equal(0); + expect(q2.getCount()).to.equal(0); + done(); }, 20); From 2142b538ac7b7aafdbae37d58613454f5f5baab5 Mon Sep 17 00:00:00 2001 From: Alessandro Isaacs Date: Fri, 10 Jul 2015 12:03:01 -0700 Subject: [PATCH 4/4] Greedify the queue loading strategy --- src/ol/tilequeue.js | 9 +++++---- test/spec/ol/tilequeue.test.js | 25 +++++++------------------ 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/ol/tilequeue.js b/src/ol/tilequeue.js index a50d588783..43a588a1aa 100644 --- a/src/ol/tilequeue.js +++ b/src/ol/tilequeue.js @@ -89,16 +89,17 @@ ol.TileQueue.prototype.handleTileChange = function(event) { * @param {number} maxNewLoads Maximum number of new tiles to load. */ ol.TileQueue.prototype.loadMoreTiles = function(maxTotalLoading, maxNewLoads) { - var newLoads = Math.min( - maxTotalLoading - this.getTilesLoading(), maxNewLoads, this.getCount()); - var i, tile; - for (i = 0; i < newLoads; ++i) { + var newLoads = 0; + var tile; + while (this.tilesLoading_ < maxTotalLoading && newLoads < maxNewLoads && + this.getCount() > 0) { tile = /** @type {ol.Tile} */ (this.dequeue()[0]); if (tile.getState() === ol.TileState.IDLE) { goog.events.listen(tile, goog.events.EventType.CHANGE, this.handleTileChange, false, this); tile.load(); ++this.tilesLoading_; + ++newLoads; } } }; diff --git a/test/spec/ol/tilequeue.test.js b/test/spec/ol/tilequeue.test.js index 35aa488899..5d9067bd36 100644 --- a/test/spec/ol/tilequeue.test.js +++ b/test/spec/ol/tilequeue.test.js @@ -45,6 +45,7 @@ describe('ol.TileQueue', function() { var maxLoading = numTiles / 2; + // and nothing is loading expect(q1.getTilesLoading()).to.equal(0); expect(q2.getTilesLoading()).to.equal(0); @@ -52,32 +53,20 @@ describe('ol.TileQueue', function() { q1.loadMoreTiles(maxLoading, maxLoading); q2.loadMoreTiles(maxLoading, maxLoading); - // since tiles can only load once, we expect one queue to load them - expect(q1.getTilesLoading()).to.equal(maxLoading); - expect(q2.getTilesLoading()).to.equal(0); - - // however, both queues will be reduced - expect(q1.getCount()).to.equal(numTiles - maxLoading); - expect(q2.getCount()).to.equal(numTiles - maxLoading); - - // ask both to load more - q1.loadMoreTiles(maxLoading, maxLoading); - q2.loadMoreTiles(maxLoading, maxLoading); - - // now the second queue will be empty - expect(q1.getCount()).to.equal(numTiles - maxLoading); - expect(q2.getCount()).to.equal(0); - - // after the first is saturated, the second should start loading + // both tiles will be loading the max expect(q1.getTilesLoading()).to.equal(maxLoading); expect(q2.getTilesLoading()).to.equal(maxLoading); + // the second queue will be empty now + expect(q1.getCount()).to.equal(numTiles - maxLoading); + expect(q2.getCount()).to.equal(0); + // let all tiles load setTimeout(function() { expect(q1.getTilesLoading()).to.equal(0); expect(q2.getTilesLoading()).to.equal(0); - // load again, which will clear the first queue + // ask both to load, this should clear q1 q1.loadMoreTiles(maxLoading, maxLoading); q2.loadMoreTiles(maxLoading, maxLoading);