Skip to content

Commit

Permalink
Merge pull request #1019 from ahocevar/chrome-optimized
Browse files Browse the repository at this point in the history
Deal with recent Chrome optimizations to avoid failing TileManager and tests
  • Loading branch information
ahocevar authored and Bart van den Eijnden committed Jul 9, 2013
1 parent d020e8f commit 49dd5a4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 52 deletions.
8 changes: 7 additions & 1 deletion lib/OpenLayers/Tile/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,17 @@ OpenLayers.Tile.Image = OpenLayers.Class(OpenLayers.Tile, {
* Creates the content for the frame on the tile.
*/
initImage: function() {
if (!this.url && !this.imgDiv) {
// fast path out - if there is no tile url and no previous image
this.isLoading = false;
return;
}
this.events.triggerEvent('beforeload');
this.layer.div.appendChild(this.getTile());
this.events.triggerEvent(this._loadEvent);
var img = this.getImage();
if (this.url && OpenLayers.Util.isEquivalentUrl(img.src, this.url)) {
var src = img.getAttribute('src') || '';
if (this.url && OpenLayers.Util.isEquivalentUrl(src, this.url)) {
this._loadTimeout = window.setTimeout(
OpenLayers.Function.bind(this.onImageLoad, this), 0
);
Expand Down
41 changes: 18 additions & 23 deletions lib/OpenLayers/TileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ OpenLayers.TileManager = OpenLayers.Class({
for (j=layer.grid[i].length-1; j>=0; --j) {
tile = layer.grid[i][j];
this.addTile({tile: tile});
if (tile.url) {
if (tile.url && !tile.imgDiv) {
this.manageTileCache({object: tile});
}
}
Expand Down Expand Up @@ -255,9 +255,6 @@ OpenLayers.TileManager = OpenLayers.Class({
for (j=layer.grid[i].length-1; j>=0; --j) {
tile = layer.grid[i][j];
this.unloadTile({object: tile});
if (tile.url) {
this.manageTileCache({object: tile});
}
}
}
}
Expand Down Expand Up @@ -390,25 +387,23 @@ OpenLayers.TileManager = OpenLayers.Class({
manageTileCache: function(evt) {
var tile = evt.object;
var img = this.tileCache[tile.url];
// only use image from cache if it is not on a layer already
if (img && (!img.parentNode ||
OpenLayers.Element.hasClass(img.parentNode, 'olBackBuffer'))) {
if (tile.layer.backBuffer) {
if (tile.layer.backBuffer === img.parentNode) {
// cached image is on the target layer's backbuffer already,
// so nothing to do here
return;
}
img.style.opacity = 0;
img.style.visibility = 'hidden';
}
// Only backbuffer tiles have an id, so we don't want one here
img.id = null;
tile.setImage(img);
// LRU - move tile to the end of the array to mark it as the most
// recently used
OpenLayers.Util.removeItem(this.tileCacheIndex, tile.url);
this.tileCacheIndex.push(tile.url);
if (img) {
// if image is on its layer's backbuffer, remove it from backbuffer
if (img.parentNode &&
OpenLayers.Element.hasClass(img.parentNode, 'olBackBuffer')) {
img.parentNode.removeChild(img);
img.id = null;
}
// only use image from cache if it is not on a layer already
if (!img.parentNode) {
img.style.visibility = 'hidden';
img.style.opacity = 0;
tile.setImage(img);
// LRU - move tile to the end of the array to mark it as the most
// recently used
OpenLayers.Util.removeItem(this.tileCacheIndex, tile.url);
this.tileCacheIndex.push(tile.url);
}
}
},

Expand Down
41 changes: 20 additions & 21 deletions tests/Layer/Grid.html
Original file line number Diff line number Diff line change
Expand Up @@ -1135,27 +1135,26 @@
map.addLayer(layer);
map.zoomToMaxExtent();

t.delay_call(1, function() {
layer.mergeNewParams({foo: 'bar'});
var tile = layer.grid[1][1];
t.ok(OpenLayers.Element.hasClass(tile.imgDiv, 'olTileReplacing'), 'tile is marked for being replaced');
t.ok(document.getElementById(tile.id + '_bb'), 'backbuffer created for tile');
// simulate a css declaration where '.olTileReplacing' sets display
// to none.
tile.imgDiv.style.display = 'none';
tile.onImageLoad();
t.ok(!OpenLayers.Element.hasClass(tile.imgDiv, 'olTileReplacing'), 'tile replaced, no longer marked');
t.ok(!document.getElementById(tile.id + '_bb'), 'backbuffer removed for tile');

layer.mergeNewParams({foo: 'baz'});
tile = layer.grid[1][1];
// simulate a css declaration where '.olTileReplacing' does not set
// display to none.
tile.imgDiv.style.display = 'block';
tile.onImageLoad();
t.ok(!OpenLayers.Element.hasClass(tile.imgDiv, 'olTileReplacing'), 'tile replaced, no longer marked');
t.ok(document.getElementById(tile.id + '_bb'), 'backbuffer not removed for visible tile');
});
layer.grid[1][1].onImageLoad();
layer.mergeNewParams({foo: 'bar'});
var tile = layer.grid[1][1];
t.ok(OpenLayers.Element.hasClass(tile.imgDiv, 'olTileReplacing'), 'tile is marked for being replaced');
t.ok(document.getElementById(tile.id + '_bb'), 'backbuffer created for tile');
// simulate a css declaration where '.olTileReplacing' sets display
// to none.
tile.imgDiv.style.display = 'none';
tile.onImageLoad();
t.ok(!OpenLayers.Element.hasClass(tile.imgDiv, 'olTileReplacing'), 'tile replaced, no longer marked');
t.ok(!document.getElementById(tile.id + '_bb'), 'backbuffer removed for tile');

layer.mergeNewParams({foo: 'baz'});
tile = layer.grid[1][1];
// simulate a css declaration where '.olTileReplacing' does not set
// display to none.
tile.imgDiv.style.display = 'block';
tile.onImageLoad();
t.ok(!OpenLayers.Element.hasClass(tile.imgDiv, 'olTileReplacing'), 'tile replaced, no longer marked');
t.ok(document.getElementById(tile.id + '_bb'), 'backbuffer not removed for visible tile');
}

function test_backbuffer_replace_singleTile(t) {
Expand Down
9 changes: 3 additions & 6 deletions tests/Tile/Image.html
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,9 @@

t.delay_call(5, function() {
var tile = layer.grid[0][0];
if (tile.isLoading) {
t.ok(false, "test_getCanvasContext timeout");
} else {
t.ok(tile.getCanvasContext() instanceof CanvasRenderingContext2D,
"getCanvasContext() returns CanvasRenderingContext2D instance");
}
tile.onImageLoad();
t.ok(tile.getCanvasContext() instanceof CanvasRenderingContext2D,
"getCanvasContext() returns CanvasRenderingContext2D instance");
map.destroy();
});
}
Expand Down
2 changes: 1 addition & 1 deletion tests/list-tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@
<li>Tile/Image.html</li>
<li>Tile/Image/IFrame.html</li>
<li>Tile/UTFGrid.html</li>
<li>TileManager.html?visible</li>
<li>TileManager.html</li>
<li>Tween.html</li>
<li>Kinetic.html</li>
<li>Util.html?visible</li>
Expand Down

0 comments on commit 49dd5a4

Please sign in to comment.