Version 4.12.2 Added TileQueue.

This commit is contained in:
ClemensF 2019-06-10 17:20:21 +02:00
parent a7d3edf154
commit 716adff1d1
7 changed files with 113 additions and 66 deletions

View file

@ -21,7 +21,7 @@ namespace MapControl
{ {
public interface ITileImageLoader public interface ITileImageLoader
{ {
void LoadTilesAsync(MapTileLayer tileLayer); void LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName);
} }
/// <summary> /// <summary>
@ -391,7 +391,7 @@ namespace MapControl
Children.Add(tile.Image); Children.Add(tile.Image);
} }
TileImageLoader.LoadTilesAsync(this); TileImageLoader.LoadTilesAsync(Tiles, TileSource, SourceName);
} }
} }
} }

View file

@ -44,6 +44,11 @@ namespace MapControl
} }
} }
public override string ToString()
{
return string.Format("{0}/{1}/{2}", ZoomLevel, XIndex, Y);
}
private void FadeIn() private void FadeIn()
{ {
Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation { From = 0d, To = 1d, Duration = FadeDuration, FillBehavior = FillBehavior.Stop }); Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation { From = 0d, To = 1d, Duration = FadeDuration, FillBehavior = FillBehavior.Stop });

View file

@ -3,11 +3,8 @@
// Licensed under the Microsoft Public License (Ms-PL) // Licensed under the Microsoft Public License (Ms-PL)
using System; using System;
using System.Collections.Concurrent; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MapControl namespace MapControl
@ -45,84 +42,49 @@ namespace MapControl
/// </summary> /// </summary>
public static string CacheKeyFormat { get; set; } = "{0};{1};{2};{3}{4}"; public static string CacheKeyFormat { get; set; } = "{0};{1};{2};{3}{4}";
private readonly ConcurrentStack<Tile> pendingTiles = new ConcurrentStack<Tile>(); private readonly TileQueue pendingTiles = new TileQueue();
private int taskCount;
/// <summary> /// <summary>
/// Loads all pending tiles from the Tiles collection of a MapTileLayer by running up to MaxLoadTasks parallel Tasks. /// Loads all pending tiles from the tiles collection in up to MaxLoadTasks parallel Tasks.
/// If the TileSource's SourceName is non-empty and its UriFormat starts with "http", tile images are cached in the /// If the UriFormat of the TileSource starts with "http" and the sourceName string is non-empty,
/// TileImageLoader's Cache. /// tile images are cached in the TileImageLoader's Cache.
/// </summary> /// </summary>
public void LoadTilesAsync(MapTileLayer tileLayer) public void LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName)
{ {
pendingTiles.Clear(); pendingTiles.Clear();
var tileSource = tileLayer.TileSource; if (tileSource != null && pendingTiles.Enqueue(tiles))
var sourceName = tileLayer.SourceName;
var tiles = tileLayer.Tiles.Where(t => t.Pending);
if (tileSource != null && tiles.Any())
{ {
if (Cache == null || tileSource.UriFormat == null || !tileSource.UriFormat.StartsWith("http")) if (Cache != null &&
tileSource.UriFormat != null &&
tileSource.UriFormat.StartsWith("http") &&
!string.IsNullOrEmpty(sourceName))
{ {
sourceName = null; // do not use cache pendingTiles.RunDequeueTasks(MaxLoadTasks, tile => LoadTileImageAsync(tile, tileSource, sourceName));
} }
else
pendingTiles.PushRange(tiles.Reverse().ToArray());
var newTasks = Math.Min(pendingTiles.Count, MaxLoadTasks) - taskCount;
while (--newTasks >= 0)
{ {
Interlocked.Increment(ref taskCount); pendingTiles.RunDequeueTasks(MaxLoadTasks, tile => LoadTileImageAsync(tile, tileSource));
Task.Run(async () => // do not await
{
Tile tile;
while (pendingTiles.TryPop(out tile))
{
tile.Pending = false;
try
{
await LoadTileImageAsync(tile, tileSource, sourceName);
}
catch (Exception ex)
{
Debug.WriteLine("TileImageLoader: {0}/{1}/{2}: {3}", tile.ZoomLevel, tile.XIndex, tile.Y, ex.Message);
}
}
Interlocked.Decrement(ref taskCount);
});
} }
} }
} }
private async Task LoadTileImageAsync(Tile tile, TileSource tileSource, string sourceName) private async Task LoadTileImageAsync(Tile tile, TileSource tileSource, string sourceName)
{ {
if (string.IsNullOrEmpty(sourceName)) var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);
{
await LoadTileImageAsync(tile, tileSource);
}
else
{
var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);
if (uri != null) if (uri != null)
{
var extension = Path.GetExtension(uri.LocalPath);
if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
{ {
var extension = Path.GetExtension(uri.LocalPath); extension = ".jpg";
if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
{
extension = ".jpg";
}
var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);
await LoadTileImageAsync(tile, uri, cacheKey);
} }
var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);
await LoadTileImageAsync(tile, uri, cacheKey);
} }
} }

View file

@ -0,0 +1,75 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2019 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MapControl
{
public class TileQueue : ConcurrentStack<Tile>
{
private int taskCount;
public bool Enqueue(IEnumerable<Tile> tiles)
{
tiles = tiles.Where(tile => tile.Pending);
if (tiles.Any())
{
PushRange(tiles.Reverse().ToArray());
return true;
}
return false;
}
public bool TryDequeue(out Tile tile)
{
var success = TryPop(out tile);
if (success)
{
tile.Pending = false;
}
return success;
}
public void RunDequeueTasks(int maxTasks, Func<Tile, Task> tileFunc)
{
var newTasks = Math.Min(Count, maxTasks) - taskCount;
while (--newTasks >= 0)
{
Interlocked.Increment(ref taskCount);
Task.Run(() => DequeueTiles(tileFunc));
}
}
private async Task DequeueTiles(Func<Tile, Task> tileFunc)
{
Tile tile;
while (TryDequeue(out tile))
{
try
{
await tileFunc(tile);
}
catch (Exception ex)
{
Debug.WriteLine("TileQueue: {0}: {1}", tile, ex.Message);
}
}
Interlocked.Decrement(ref taskCount);
}
}
}

View file

@ -139,6 +139,9 @@
<Compile Include="..\Shared\TileImageLoader.cs"> <Compile Include="..\Shared\TileImageLoader.cs">
<Link>TileImageLoader.cs</Link> <Link>TileImageLoader.cs</Link>
</Compile> </Compile>
<Compile Include="..\Shared\TileQueue.cs">
<Link>TileQueue.cs</Link>
</Compile>
<Compile Include="..\Shared\TileSource.cs"> <Compile Include="..\Shared\TileSource.cs">
<Link>TileSource.cs</Link> <Link>TileSource.cs</Link>
</Compile> </Compile>

View file

@ -164,6 +164,9 @@
<Compile Include="..\Shared\TileImageLoader.cs"> <Compile Include="..\Shared\TileImageLoader.cs">
<Link>TileImageLoader.cs</Link> <Link>TileImageLoader.cs</Link>
</Compile> </Compile>
<Compile Include="..\Shared\TileQueue.cs">
<Link>TileQueue.cs</Link>
</Compile>
<Compile Include="..\Shared\TileSource.cs"> <Compile Include="..\Shared\TileSource.cs">
<Link>TileSource.cs</Link> <Link>TileSource.cs</Link>
</Compile> </Compile>

View file

@ -8,7 +8,6 @@ using System.Runtime.Caching;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Threading;
namespace MapControl namespace MapControl
{ {