Merge pull request #15 from djes/djes

Rewrite of cache mechanism
This commit is contained in:
djes
2017-06-15 16:42:15 +02:00
committed by GitHub
3 changed files with 605 additions and 466 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
PBMap.pb.bak
*.exe

413
PBMap.pb
View File

@@ -47,6 +47,7 @@ DeclareModule PBMap
#PB_MAP_TILE_CLEANUP = #PB_EventType_FirstCustomValue + 3
Declare InitPBMap(window)
Declare SetDebugLevel(level.i)
Declare SetOption(Option.s, Value.s)
Declare.s GetOption(Option.s)
Declare LoadOptions(PreferencesFile.s = "PBMap.prefs")
@@ -124,7 +125,10 @@ Module PBMap
URL.s
CacheFile.s
GetImageThread.i
RetryNb.i
Download.i
Time.i
Size.i
Mutex.i
EndStructure
Structure BoundingBox
@@ -152,8 +156,9 @@ Module PBMap
Structure ImgMemCach
nImage.i
Size.i
*Tile.Tile
TimeStackPosition.i
*TimeStackPtr
Alpha.i
EndStructure
@@ -197,6 +202,8 @@ Module PBMap
ShowPointer.i
TimerInterval.i
MaxMemCache.i ; in MiB
MaxThreads.i ; Maximum simultaneous web loading threads
MaxDownloadSlots.i ; Maximum simultaneous download slots
TileLifetime.i
Verbose.i ; Maximum debug informations
Warning.i ; Warning requesters
@@ -281,11 +288,16 @@ Module PBMap
MemCache.TileMemCach ; Images in memory cache
ThreadsNB.i ; Current web threads nb
Mode.i ; User mode : 0 (default)->hand (moving map) and select markers, 1->hand, 2->select only (moving objects), 3->drawing (todo)
Redraw.i
Dragging.i
Dirty.i ; To signal that drawing need a refresh
MemoryCacheAccessMutex.i ; Memorycache access variable mutual exclusion
DownloadSlots.i ; Actual nb of used download slots
List TracksList.Tracks() ; To display a GPX track
List Markers.Marker() ; To diplay marker
EditMarker.l
@@ -300,7 +312,7 @@ Module PBMap
;-*** Global variables
;-Show debug infos
Global MyDebugLevel = 3
Global MyDebugLevel = 5
Global PBMap.PBMap, Null.i, NullPtrMem.i, *NullPtr = @NullPtrMem
Global slash.s
@@ -344,9 +356,14 @@ Module PBMap
EndIf
EndProcedure
; Set the debug level allowing more or less debug infos
Procedure SetDebugLevel(level.i)
MyDebugLevel = level
EndProcedure
; Send debug infos to stdout (allowing mixed debug infos with curl or other libs)
Procedure MyDebug(msg.s, DbgLevel = 0)
If PBMap\Options\Verbose And DbgLevel >= MyDebugLevel
If PBMap\Options\Verbose And DbgLevel <= MyDebugLevel
PrintN(msg)
; Debug msg
EndIf
@@ -681,6 +698,10 @@ Module PBMap
PBMap\Options\HDDCachePath = Value
Case "maxmemcache"
PBMap\Options\MaxMemCache = Val(Value)
Case "maxthreads"
PBMap\Options\MaxThreads = Val(Value)
Case "maxdownloadslots"
PBMap\Options\MaxDownloadSlots = Val(Value)
Case "tilelifetime"
PBMap\Options\TileLifetime = Val(Value)
Case "verbose"
@@ -748,6 +769,10 @@ Module PBMap
ProcedureReturn \HDDCachePath
Case "maxmemcache"
ProcedureReturn StrU(\MaxMemCache)
Case "maxthreads"
ProcedureReturn StrU(\MaxThreads)
Case "maxdownloadslots"
ProcedureReturn StrU(\MaxDownloadSlots)
Case "tilelifetime"
ProcedureReturn StrU(\TileLifetime)
Case "verbose"
@@ -809,6 +834,8 @@ Module PBMap
PreferenceGroup("OPTIONS")
WritePreferenceInteger("WheelMouseRelative", \WheelMouseRelative)
WritePreferenceInteger("MaxMemCache", \MaxMemCache)
WritePreferenceInteger("MaxThreads", \MaxThreads)
WritePreferenceInteger("MaxDownloadSlots", \MaxDownloadSlots)
WritePreferenceInteger("TileLifetime", \TileLifetime)
WritePreferenceInteger("Verbose", \Verbose)
WritePreferenceInteger("Warning", \Warning)
@@ -873,6 +900,8 @@ Module PBMap
PreferenceGroup("OPTIONS")
\WheelMouseRelative = ReadPreferenceInteger("WheelMouseRelative", #True)
\MaxMemCache = ReadPreferenceInteger("MaxMemCache", 20480) ; 20 MiB, about 80 tiles in memory
\MaxThreads = ReadPreferenceInteger("MaxThreads", 40)
\MaxDownloadSlots = ReadPreferenceInteger("MaxDownloadSlots", 2)
\TileLifetime = ReadPreferenceInteger("TileLifetime", 1209600) ; about 2 weeks ;-1 = unlimited
\Verbose = ReadPreferenceInteger("Verbose", #False)
\Warning = ReadPreferenceInteger("Warning", #False)
@@ -891,7 +920,7 @@ Module PBMap
\ColourFocus = ReadPreferenceInteger("ColourFocus", RGBA(255, 255, 0, 255))
\ColourSelected = ReadPreferenceInteger("ColourSelected", RGBA(225, 225, 0, 255))
\ColourTrackDefault = ReadPreferenceInteger("ColourTrackDefault", RGBA(0, 255, 0, 150))
\TimerInterval = 20
\TimerInterval = 12
ClosePreferences()
EndWith
SetOptions()
@@ -1021,51 +1050,73 @@ Module PBMap
ProcedureReturn PBMap\Layers(Name)\Alpha
EndProcedure
;-*** These are threaded
;-***
; If cache size exceeds limit, try to delete the oldest tiles used (first in the time stack)
Procedure MemoryCacheManagement()
LockMutex(PBMap\MemoryCacheAccessMutex) ; Prevents thread to start or finish
Protected CacheSize = MapSize(PBMap\MemCache\Images()) * Pow(PBMap\TileSize, 2) * 4 ; Size of a tile = TileSize * TileSize * 4 bytes (RGBA)
Protected CacheLimit = PBMap\Options\MaxMemCache * 1024
MyDebug("Cache size : " + Str(CacheSize/1024) + " / CacheLimit : " + Str(CacheLimit/1024), 5)
If CacheSize > CacheLimit
MyDebug(" Cache full. Trying cache cleaning", 5)
ResetList(PBMap\MemCache\ImagesTimeStack())
; Try to free half the cache memory (one pass)
While NextElement(PBMap\MemCache\ImagesTimeStack()) And CacheSize > (CacheLimit / 2) ; /2 = half
Protected CacheMapKey.s = PBMap\MemCache\ImagesTimeStack()\MapKey
; Is the loading over
If PBMap\MemCache\Images(CacheMapKey)\Tile <= 0 ;TODO Should not verify this var directly
MyDebug(" Delete " + CacheMapKey, 5)
If PBMap\MemCache\Images(CacheMapKey)\nImage;IsImage(PBMap\MemCache\Images(CacheMapKey)\nImage)
FreeImage(PBMap\MemCache\Images(CacheMapKey)\nImage)
MyDebug(" and free image nb " + Str(PBMap\MemCache\Images(CacheMapKey)\nImage), 5)
PBMap\MemCache\Images(CacheMapKey)\nImage = 0
EndIf
DeleteMapElement(PBMap\MemCache\Images(), CacheMapKey)
DeleteElement(PBMap\MemCache\ImagesTimeStack(), 1)
; ElseIf PBMap\MemCache\Images(CacheMapKey)\Tile = 0
; MyDebug(" Delete " + CacheMapKey, 5)
; DeleteMapElement(PBMap\MemCache\Images(), CacheMapKey)
; DeleteElement(PBMap\MemCache\ImagesTimeStack(), 1)
; ElseIf PBMap\MemCache\Images(CacheMapKey)\Tile > 0
; ; If the thread is running, try to abort the download
; If PBMap\MemCache\Images(CacheMapKey)\Tile\Download
; AbortHTTP(PBMap\MemCache\Images(CacheMapKey)\Tile\Download) ; Could lead to error
; EndIf
EndIf
CacheSize = MapSize(PBMap\MemCache\Images()) * Pow(PBMap\TileSize, 2) * 4 ; Size of a tile = TileSize * TileSize * 4 bytes (RGBA)
Wend
MyDebug(" New cache size : " + Str(CacheSize/1024) + " / CacheLimit : " + Str(CacheLimit/1024), 5)
If CacheSize > CacheLimit
MyDebug(" Cache cleaning unsuccessfull, can't add new tiles.", 5)
EndIf
EndIf
UnlockMutex(PBMap\MemoryCacheAccessMutex)
EndProcedure
Procedure.i GetTileFromHDD(CacheFile.s)
Protected nImage.i, LifeTime.i, MaxLifeTime.i = PBMap\Options\TileLifetime
If FileSize(CacheFile) <> -1
;Manage tile file lifetime
If MaxLifeTime <> -1
LifeTime = Date() - GetFileDate(CacheFile, #PB_Date_Modified) ;There's a bug with #PB_Date_Created
If LifeTime > MaxLifeTime
MyDebug("Deleting too old (" + StrU(LifeTime) + " secs) " + CacheFile, 3)
DeleteFile(CacheFile)
ProcedureReturn -1
EndIf
EndIf
;Everything is OK, load the file
Protected nImage.i, LifeTime.i, MaxLifeTime.i
; Everything is OK, loads the file
nImage = LoadImage(#PB_Any, CacheFile)
If IsImage(nImage)
If nImage
MyDebug(" Success loading " + CacheFile + " as nImage " + Str(nImage), 3)
ProcedureReturn nImage
Else
MyDebug(" Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !", 3)
If DeleteFile(CacheFile)
MyDebug(" Deleting faulty image file " + CacheFile, 3)
DeleteFile(CacheFile)
EndIf
Else
MyDebug("Failed loading " + CacheFile + " -> Size <= 0", 3)
MyDebug(" Can't delete faulty image file " + CacheFile, 3)
EndIf
ProcedureReturn -1
EndIf
ProcedureReturn #False
EndProcedure
Procedure.i GetTileFromWeb(TileURL.s, CacheFile.s)
Protected *Buffer
Protected nImage.i = -1
Protected FileSize.i, timg
FileSize = ReceiveHTTPFile(TileURL, CacheFile)
If FileSize > 0
MyDebug("Loaded from web " + TileURL + " as CacheFile " + CacheFile, 3)
nImage = GetTileFromHDD(CacheFile)
Else
MyDebug("Problem loading from web " + TileURL + " as CacheFile " + CacheFile, 3)
EndIf
; **** IMPORTANT NOTICE (please not remove)
; I'm (djes) now using Curl (actually, just normal pb) only, as this original catchimage/saveimage method is a double operation (uncompress/recompress PNG)
; **** OLD IMPORTANT NOTICE (please not remove)
; This original catchimage/saveimage method is a double operation (uncompress/recompress PNG)
; and is modifying the original PNG image which could lead to PNG error (Idle has spent hours debunking the 1 bit PNG bug)
; More than that, the original Purebasic Receive library is still not Proxy enabled.
; Protected *Buffer
; Protected nImage.i = -1
; Protected timg
; *Buffer = ReceiveHTTPMemory(TileURL) ; TODO to thread by using #PB_HTTP_Asynchronous
; If *Buffer
; nImage = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer))
@@ -1084,106 +1135,175 @@ Module PBMap
; MyDebug(" Problem loading from web " + TileURL, 3)
; EndIf
; ****
ProcedureReturn nImage
EndProcedure
;-*** These are threaded
Threaded Progress = 0, Quit = #False
Procedure GetImageThread(*Tile.Tile)
Protected nImage.i = -1
LockMutex(PBMap\MemoryCacheAccessMutex)
MyDebug("Thread nb " + Str(*Tile\GetImageThread) + " " + *Tile\key + " starting for image " + *Tile\CacheFile, 5)
; If MemoryCache is currently being cleaned, abort
; If PBMap\MemoryCacheAccessNB = -1
; MyDebug(" Thread nb " + Str(*Tile\GetImageThread) + " " + *Tile\key + " for image " + *Tile\CacheFile + " canceled because of cleaning.", 5)
; *Tile\Size = 0 ; \Size = 0 signals that the download has failed
; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ; To free memory outside the thread
; UnlockMutex(PBMap\MemoryCacheAccessMutex)
; ProcedureReturn
; EndIf
; We're accessing MemoryCache
UnlockMutex(PBMap\MemoryCacheAccessMutex)
*Tile\Size = 0
*Tile\Download = ReceiveHTTPFile(*Tile\URL, *Tile\CacheFile, #PB_HTTP_Asynchronous)
If *Tile\Download
Repeat
nImage = GetTileFromWeb(*Tile\URL, *Tile\CacheFile)
If nImage <> -1
MyDebug("Image key : " + *Tile\key + " web image loaded", 3)
*Tile\RetryNb = 0
Else
MyDebug("Image key : " + *Tile\key + " web image not correctly loaded, will retry in 2 secs", 3)
Delay(2000)
*Tile\RetryNb - 1
Progress = HTTPProgress(*Tile\Download)
Select Progress
Case #PB_Http_Success
*Tile\Size = FinishHTTP(*Tile\Download) ; \Size signals that the download is OK
MyDebug(" Thread nb " + Str(*Tile\GetImageThread) + " " + *Tile\key + " for image " + *Tile\CacheFile + " finished. Size : " + Str(*Tile\Size), 5)
Quit = #True
Case #PB_Http_Failed
FinishHTTP(*Tile\Download)
*Tile\Size = 0 ; \Size = 0 signals that the download has failed
MyDebug(" Thread nb " + Str(*Tile\GetImageThread) + " " + *Tile\key + " for image " + *Tile\CacheFile + " failed.", 5)
Quit = #True
Case #PB_Http_Aborted
FinishHTTP(*Tile\Download)
*Tile\Size = 0 ; \Size = 0 signals that the download has failed
MyDebug(" Thread nb " + Str(*Tile\GetImageThread) + " " + *Tile\key + " for image " + *Tile\CacheFile + " aborted.", 5)
Quit = #True
Default
MyDebug(" Thread nb " + Str(*Tile\GetImageThread) + " " + *Tile\key + " for image " + *Tile\CacheFile + " downloading " + Str(Progress) + " bytes", 5)
If ElapsedMilliseconds() - *Tile\Time > 10000
MyDebug(" Thread nb " + Str(*Tile\GetImageThread) + " " + *Tile\key + " for image " + *Tile\CacheFile + " canceled after 10 seconds.", 5)
AbortHTTP(*Tile\Download)
EndIf
Until *Tile\RetryNb <= 0
*Tile\nImage = nImage
*Tile\RetryNb = -2 ;End of the thread
EndSelect
Delay(200) ; Frees CPU
Until Quit
EndIf
; End of the memory cache access
LockMutex(PBMap\MemoryCacheAccessMutex)
PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ; To free memory outside the thread
UnlockMutex(PBMap\MemoryCacheAccessMutex)
EndProcedure
;-***
Procedure.i GetTile(key.s, URL.s, CacheFile.s)
; Try to find the tile in memory cache. If not found, add it, try to load it from the
; HDD, or launch a loading thread, and try again on the next drawing loop.
Protected img.i = -1
; MemoryCache access management
LockMutex(PBMap\MemoryCacheAccessMutex)
; Try to find the tile in memory cache
Protected *timg.ImgMemCach = FindMapElement(PBMap\MemCache\Images(), key)
If *timg
MyDebug("Key : " + key + " found in memory cache", 3)
img = *timg\nImage
If img <> -1
MyDebug("Image : " + img + " found in memory cache", 3)
MyDebug("Key : " + key + " found in memory cache", 4)
; Is the associated image already been loaded in memory ?
If *timg\nImage
; Yes, returns the image's nb
MyDebug(" as image " + *timg\nImage, 4)
; *** Cache management
; Move the newly used element to the last position of the time stack
SelectElement(PBMap\MemCache\ImagesTimeStack(), *timg\TimeStackPosition)
; Retrieves the image in the time stack, push it to the end (to say it's the lastly used)
ChangeCurrentElement(PBMap\MemCache\ImagesTimeStack(), *timg\TimeStackPtr)
MoveElement(PBMap\MemCache\ImagesTimeStack(), #PB_List_Last)
; *timg\TimeStackPtr = LastElement(PBMap\MemCache\ImagesTimeStack())
; ***
UnlockMutex(PBMap\MemoryCacheAccessMutex)
ProcedureReturn *timg
Else
; No, try to load it from HD (see below)
MyDebug(" but not the image.", 4)
EndIf
Else
;PushMapPosition(PBMap\MemCache\Images())
;*** Cache management
; if cache size exceeds limit, try to delete the oldest tile used (first in the list)
Protected CacheSize = MapSize(PBMap\MemCache\Images()) * Pow(PBMap\TileSize, 2) * 4 ; Size of a tile = TileSize * TileSize * 4 bytes (RGBA)
Protected CacheLimit = PBMap\Options\MaxMemCache * 1024
MyDebug("Cache size : " + Str(CacheSize/1024) + " / CacheLimit : " + Str(CacheLimit/1024), 4)
ResetList(PBMap\MemCache\ImagesTimeStack())
While NextElement(PBMap\MemCache\ImagesTimeStack()) And CacheSize > CacheLimit
Protected CacheMapKey.s = PBMap\MemCache\ImagesTimeStack()\MapKey
Protected Image = PBMap\MemCache\Images(CacheMapKey)\nImage
If IsImage(Image) ; Check if the image is valid (is a loading thread running ?)
FreeImage(Image)
MyDebug("Delete " + CacheMapKey + " As image nb " + Str(Image), 4)
DeleteMapElement(PBMap\MemCache\Images(), CacheMapKey)
DeleteElement(PBMap\MemCache\ImagesTimeStack())
CacheSize = MapSize(PBMap\MemCache\Images()) * Pow(PBMap\TileSize, 2) * 4 ; Size of a tile = TileSize * TileSize * 4 bytes (RGBA)
; The tile has not been found in the cache, so creates a new cache element
*timg = AddMapElement(PBMap\MemCache\Images(), key)
If *timg = 0
MyDebug(" Can't add a new cache element.", 4)
UnlockMutex(PBMap\MemoryCacheAccessMutex)
ProcedureReturn #False
EndIf
Wend
; add a new time stack element at the End
LastElement(PBMap\MemCache\ImagesTimeStack())
;PopMapPosition(PBMap\MemCache\Images())
AddMapElement(PBMap\MemCache\Images(), key)
AddElement(PBMap\MemCache\ImagesTimeStack())
;MoveElement(PBMap\MemCache\ImagesTimeStack(), #PB_List_Last)
; Stores the time stack ptr
*timg\TimeStackPtr = AddElement(PBMap\MemCache\ImagesTimeStack())
If *timg\TimeStackPtr = 0
MyDebug(" Can't add a new time stack element.", 4)
DeleteMapElement(PBMap\MemCache\Images())
UnlockMutex(PBMap\MemoryCacheAccessMutex)
ProcedureReturn #False
EndIf
; Associates the time stack element to the cache element
PBMap\MemCache\ImagesTimeStack()\MapKey = MapKey(PBMap\MemCache\Images())
;***
MyDebug("Key : " + key + " added in memory cache", 3)
*timg = PBMap\MemCache\Images()
*timg\nImage = -1
MyDebug("Key : " + key + " added in memory cache", 4)
EndIf
If *timg\Tile = 0 ; Check if a loading thread is not running
MyDebug("Trying to load from HDD " + CacheFile, 3)
img = GetTileFromHDD(CacheFile.s)
If img <> -1
MyDebug("Key : " + key + " found on HDD", 3)
*timg\nImage = img
*timg\Alpha = 256
; If there's no active download thread for this tile
If *timg\Tile <= 0
; Manage tile file lifetime, delete if too old
If PBMap\Options\TileLifetime <> -1
If FileSize(CacheFile) > 0 ; Does the file exists ?
If Date() - GetFileDate(CacheFile, #PB_Date_Modified) > PBMap\Options\TileLifetime ; If Lifetime > MaxLifeTime ; There's a bug with #PB_Date_Created
If DeleteFile(CacheFile)
MyDebug(" Deleting too old image file " + CacheFile, 3)
Else
MyDebug(" Can't delete too old image file " + CacheFile, 3)
UnlockMutex(PBMap\MemoryCacheAccessMutex)
ProcedureReturn #False
EndIf
EndIf
EndIf
EndIf
; Try To load it from HD
*timg\nImage = 0
*timg\Size = FileSize(CacheFile)
If *timg\Size > 0
*timg\nImage = GetTileFromHDD(CacheFile.s)
Else
MyDebug(" Failed loading from HDD " + CacheFile + " -> Filesize = " + FileSize(CacheFile), 3)
EndIf
If *timg\nImage
; Image found and loaded from HDD
*timg\Alpha = 0
UnlockMutex(PBMap\MemoryCacheAccessMutex)
ProcedureReturn *timg
EndIf
MyDebug("Key : " + key + " not found on HDD", 3)
;Launch a new thread
Else
; If GetTileFromHDD failed, will load it (again?) from the web
If PBMap\ThreadsNB < PBMap\Options\MaxThreads
If PBMap\DownloadSlots < PBMap\Options\MaxDownloadSlots
; Launch a new web loading thread
PBMap\DownloadSlots + 1
Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile))
If *NewTile
With *NewTile
*timg\Tile = *NewTile
*timg\Alpha = 0
;*timg\nImage = -1
; New tile parameters
\key = key
\URL = URL
\CacheFile = CacheFile
\RetryNb = 5
\nImage = -1
MyDebug(" Creating get image thread nb " + Str(\GetImageThread) + " to get " + CacheFile, 3)
\nImage = 0
\Time = ElapsedMilliseconds()
\GetImageThread = CreateThread(@GetImageThread(), *NewTile)
If \GetImageThread
*timg\Tile = *NewTile ; There's now a loading thread
*timg\Alpha = 0
MyDebug(" Creating get image thread nb " + Str(\GetImageThread) + " to get " + CacheFile + " (key = " + key, 3)
PBMap\ThreadsNB + 1
Else
MyDebug(" Can't create get image thread to get " + CacheFile, 3)
FreeMemory(*NewTile)
EndIf
EndWith
Else
MyDebug(" Error, can't create a new tile loading thread", 3)
MyDebug(" Error, can't allocate memory for a new tile loading thread", 3)
EndIf
Else
MyDebug(" Thread needed " + key + " for image " + CacheFile + " canceled because no free download slot.", 5)
EndIf
Else
MyDebug(" Error, maximum threads nb reached", 3)
EndIf
EndIf
ProcedureReturn *timg
EndIf
UnlockMutex(PBMap\MemoryCacheAccessMutex)
ProcedureReturn #False
EndProcedure
Procedure DrawTiles(*Drawing.DrawingParameters, LayerName.s)
@@ -1263,7 +1383,7 @@ Module PBMap
EndSelect
EndWith
*timg = GetTile(key, URL, CacheFile)
If *timg\nImage <> -1
If *timg And *timg\nImage
MovePathCursor(px, py)
If *timg\Alpha <= 224
DrawVectorImage(ImageID(*timg\nImage), *timg\Alpha * PBMap\Layers()\Alpha)
@@ -1737,23 +1857,23 @@ Module PBMap
VectorFont(FontID(PBMap\Font), 16)
VectorSourceColor(RGBA(0, 0, 0, 80))
MovePathCursor(50, 50)
DrawVectorText(Str(MapSize(PBMap\MemCache\Images())))
DrawVectorText("Images in cache : " + Str(MapSize(PBMap\MemCache\Images())))
MovePathCursor(50, 70)
Protected ThreadCounter = 0
ForEach PBMap\MemCache\Images()
If PBMap\MemCache\Images()\Tile <> 0
If PBMap\MemCache\Images()\Tile > 0
If IsThread(PBMap\MemCache\Images()\Tile\GetImageThread)
ThreadCounter + 1
EndIf
EndIf
Next
DrawVectorText(Str(ThreadCounter))
DrawVectorText("Threads nb : " + Str(ThreadCounter))
MovePathCursor(50, 90)
DrawVectorText(Str(PBMap\Zoom))
DrawVectorText("Zoom : " + Str(PBMap\Zoom))
MovePathCursor(50, 110)
DrawVectorText(StrD(*Drawing\Bounds\NorthWest\Latitude) + "," + StrD(*Drawing\Bounds\NorthWest\Longitude))
DrawVectorText("Lat-Lon 1 : " + StrD(*Drawing\Bounds\NorthWest\Latitude) + "," + StrD(*Drawing\Bounds\NorthWest\Longitude))
MovePathCursor(50, 130)
DrawVectorText(StrD(*Drawing\Bounds\SouthEast\Latitude) + "," + StrD(*Drawing\Bounds\SouthEast\Longitude))
DrawVectorText("Lat-Lon 2 : " + StrD(*Drawing\Bounds\SouthEast\Latitude) + "," + StrD(*Drawing\Bounds\SouthEast\Longitude))
EndProcedure
Procedure DrawOSMCopyright(*Drawing.DrawingParameters)
@@ -2113,11 +2233,11 @@ Module PBMap
EndIf
EndIf
If DeleteDirectory(PBMap\Options\HDDCachePath, "", #PB_FileSystem_Recursive)
MyDebug("Cache in : " + PBMap\Options\HDDCachePath + " cleared")
MyDebug("Cache in : " + PBMap\Options\HDDCachePath + " cleared", 3)
CreateDirectoryEx(PBMap\Options\HDDCachePath)
ProcedureReturn #True
Else
MyDebug("Can't clear cache in " + PBMap\Options\HDDCachePath)
MyDebug("Can't clear cache in " + PBMap\Options\HDDCachePath, 3)
ProcedureReturn #False
EndIf
EndProcedure
@@ -2221,7 +2341,7 @@ Module PBMap
Case #PB_EventType_LeftButtonDown
; LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom)
PBMap\Dragging = #True
;Mem cursor Coord
; Memorize cursor Coord
PBMap\MoveStartingPoint\x = CanvasMouseX
PBMap\MoveStartingPoint\y = CanvasMouseY
; Clip MouseX to the map range (in X, the map is infinite)
@@ -2339,20 +2459,27 @@ Module PBMap
PBMap\Dragging = #False
PBMap\Redraw = #True
Case #PB_MAP_REDRAW
Debug "Redraw"
PBMap\Redraw = #True
Case #PB_MAP_RETRY
Debug "Reload"
PBMap\Redraw = #True
;- Tile web loading thread cleanup
; After a Web tile loading thread, clean the tile structure memory, see GetImageThread()
Case #PB_MAP_TILE_CLEANUP
*Tile = EventData()
key = *Tile\key
;After a Web tile loading thread, clean the tile structure memory and set the image nb in the cache
;avoid to have threads accessing vars (and avoid mutex), see GetImageThread()
Protected timg = PBMap\MemCache\Images(key)\Tile\nImage ;Get this new tile image nb
PBMap\MemCache\Images(key)\nImage = timg ;store it in the cache using the key
FreeMemory(PBMap\MemCache\Images(key)\Tile) ;free the data needed for the thread
PBMap\MemCache\Images(key)\Tile = 0 ;clear the data ptr
*Tile\Download = 0
If FindMapElement(PBMap\MemCache\Images(), key) <> 0
; If the map element has not been deleted during the thread lifetime (should not occur)
PBMap\MemCache\Images(key)\Tile = *Tile\Size
If *Tile\Size
PBMap\MemCache\Images(key)\Tile = -1 ; Web loading thread has finished successfully
Else
PBMap\MemCache\Images(key)\Tile = 0
EndIf
EndIf
FreeMemory(*Tile) ; Frees the data needed for the thread (*tile=PBMap\MemCache\Images(key)\Tile)
PBMap\ThreadsNB - 1
PBMap\DownloadSlots - 1
PBMap\Redraw = #True
EndSelect
EndProcedure
@@ -2360,6 +2487,7 @@ Module PBMap
; Redraws at regular intervals
Procedure TimerEvents()
If EventTimer() = PBMap\Timer And (PBMap\Redraw Or PBMap\Dirty)
MemoryCacheManagement()
Drawing()
EndIf
EndProcedure
@@ -2391,9 +2519,8 @@ Module PBMap
Protected TimeCounter = ElapsedMilliseconds()
Repeat
ForEach PBMap\MemCache\Images()
If PBMap\MemCache\Images()\Tile <> 0
If PBMap\MemCache\Images()\Tile > 0
If IsThread(PBMap\MemCache\Images()\Tile\GetImageThread)
PBMap\MemCache\Images()\Tile\RetryNb = 0
If ElapsedMilliseconds() - TimeCounter > 2000
; Should not occur
KillThread(PBMap\MemCache\Images()\Tile\GetImageThread)
@@ -2411,17 +2538,24 @@ Module PBMap
EndProcedure
Procedure InitPBMap(Window)
With PBMap
Protected Result.i
PBMap\ZoomMin = 1
PBMap\ZoomMax = 18
PBMap\Dragging = #False
PBMap\TileSize = 256
PBMap\Dirty = #False
PBMap\EditMarker = #False
PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold)
PBMap\Window = Window
PBMap\Timer = 1
PBMap\Mode = #MODE_DEFAULT
\ZoomMin = 1
\ZoomMax = 18
\Dragging = #False
\TileSize = 256
\Dirty = #False
\EditMarker = #False
\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold)
\Window = Window
\Timer = 1
\Mode = #MODE_DEFAULT
\MemoryCacheAccessMutex = CreateMutex()
If \MemoryCacheAccessMutex = #False
MyDebug("Cannot create a mutex", 0)
End
EndIf
EndWith
LoadOptions()
TechnicalImagesCreation()
SetLocation(0, 0)
@@ -2592,9 +2726,10 @@ CompilerIf #PB_Compiler_IsMainFile
; Our main gadget
PBMap::InitPBMap(#Window_0)
PBMap::SetOption("ShowDegrees", "0") : Degrees = 0
PBMap::SetOption("ShowDebugInfos", "0")
PBMap::SetOption("Verbose", "0")
PBMap::SetOption("ShowDegrees", "1") : Degrees = 0
PBMap::SetOption("ShowDebugInfos", "1")
PBMap::SetDebugLevel(5)
PBMap::SetOption("Verbose", "1")
PBMap::SetOption("ShowScale", "1")
PBMap::SetOption("Warning", "1")
PBMap::SetOption("ShowMarkersLegend", "1")
@@ -2737,8 +2872,10 @@ CompilerIf #PB_Compiler_IsMainFile
CompilerEndIf
; IDE Options = PureBasic 5.60 (Windows - x64)
; CursorPosition = 2691
; FirstLine = 2684
; CursorPosition = 2552
; FirstLine = 2548
; Folding = -------------------
; EnableThread
; EnableXP
; CompileSourceDirectory
; DisablePurifier = 1,1,1,1

View File

@@ -17,3 +17,4 @@ Thyphoon
djes
Idle
Progi1984
yves86