Code cleanup

Misc internal fixes, especially in GetImageThread()
This commit is contained in:
djes
2019-07-18 16:24:12 +02:00
parent be8f378bbc
commit 629c469a6b

View File

@@ -1141,7 +1141,7 @@ Module PBMap
; If cache size exceeds limit, try to delete the oldest tiles used (first in the time stack) ; If cache size exceeds limit, try to delete the oldest tiles used (first in the time stack)
Procedure MemoryCacheManagement(MapGadget.i) Procedure MemoryCacheManagement(MapGadget.i)
Protected *PBMap.PBMap = PBMaps(Str(MapGadget)) Protected *PBMap.PBMap = PBMaps(Str(MapGadget))
LockMutex(*PBMap\MemoryCacheAccessMutex) ; Prevents thread to start or finish LockMutex(*PBMap\MemoryCacheAccessMutex) ; Prevents threads 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 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 Protected CacheLimit = *PBMap\Options\MaxMemCache * 1024
MyDebug(*PBMap, "Cache size : " + Str(CacheSize/1024) + " / CacheLimit : " + Str(CacheLimit/1024), 5) MyDebug(*PBMap, "Cache size : " + Str(CacheSize/1024) + " / CacheLimit : " + Str(CacheLimit/1024), 5)
@@ -1287,7 +1287,7 @@ Module PBMap
Protected *timg.ImgMemCach = FindMapElement(*PBMap\MemCache\Images(), key) Protected *timg.ImgMemCach = FindMapElement(*PBMap\MemCache\Images(), key)
If *timg If *timg
MyDebug(*PBMap, "Key : " + key + " found in memory cache", 4) MyDebug(*PBMap, "Key : " + key + " found in memory cache", 4)
; Is the associated image already been loaded in memory ? ; Is the associated image already loaded in memory ?
If *timg\nImage If *timg\nImage
; Yes, returns the image's nb ; Yes, returns the image's nb
MyDebug(*PBMap, " as image " + *timg\nImage, 4) MyDebug(*PBMap, " as image " + *timg\nImage, 4)
@@ -1325,13 +1325,14 @@ Module PBMap
*PBMap\MemCache\ImagesTimeStack()\MapKey = MapKey(*PBMap\MemCache\Images()) *PBMap\MemCache\ImagesTimeStack()\MapKey = MapKey(*PBMap\MemCache\Images())
MyDebug(*PBMap, "Key : " + key + " added in memory cache", 4) MyDebug(*PBMap, "Key : " + key + " added in memory cache", 4)
EndIf EndIf
; If there's no active download thread for this tile ; If there's no active downloading thread for this image
If *timg\Tile <= 0 If *timg\Tile <= 0
*timg\nImage = 0 *timg\nImage = 0
*timg\Size = FileSize(CacheFile) *timg\Size = FileSize(CacheFile)
; Does a valid file exists on HD ? Try to load it.
If *timg\Size >= 0
; Manage tile file lifetime, delete if too old, or if size = 0 ; Manage tile file lifetime, delete if too old, or if size = 0
If *PBMap\Options\TileLifetime <> -1 If *PBMap\Options\TileLifetime <> -1
If *timg\Size >= 0 ; Does the file exists ?
If *timg\Size = 0 Or (Date() - GetFileDate(CacheFile, #PB_Date_Modified) > *PBMap\Options\TileLifetime) ; If Lifetime > MaxLifeTime ; There's a bug with #PB_Date_Created If *timg\Size = 0 Or (Date() - GetFileDate(CacheFile, #PB_Date_Modified) > *PBMap\Options\TileLifetime) ; If Lifetime > MaxLifeTime ; There's a bug with #PB_Date_Created
If DeleteFile(CacheFile) If DeleteFile(CacheFile)
MyDebug(*PBMap, " Deleting image file " + CacheFile, 3) MyDebug(*PBMap, " Deleting image file " + CacheFile, 3)
@@ -1343,23 +1344,19 @@ Module PBMap
ProcedureReturn #False ProcedureReturn #False
EndIf EndIf
EndIf EndIf
EndIf ; Try to load tile's image from HD
; Try To load it from HD
If *timg\Size > 0
*timg\nImage = GetTileFromHDD(*PBMap, CacheFile.s) *timg\nImage = GetTileFromHDD(*PBMap, CacheFile.s)
Else
MyDebug(*PBMap, " Failed loading from HDD " + CacheFile + " -> Filesize = " + FileSize(CacheFile), 3)
EndIf
If *timg\nImage If *timg\nImage
; Image found and loaded from HDD ; Success : image found and loaded from HDD
*timg\Alpha = 0 *timg\Alpha = 0
UnlockMutex(*PBMap\MemoryCacheAccessMutex) UnlockMutex(*PBMap\MemoryCacheAccessMutex)
ProcedureReturn *timg ProcedureReturn *timg
Else EndIf
; If GetTileFromHDD failed, will load it (again?) from the web EndIf
; If GetTileFromHDD failed, will try to download the image from the web in a thread
MyDebug(*PBMap, " Failed loading from HDD " + CacheFile + " -> Filesize = " + FileSize(CacheFile), 3)
If *PBMap\ThreadsNB < *PBMap\Options\MaxThreads If *PBMap\ThreadsNB < *PBMap\Options\MaxThreads
If *PBMap\DownloadSlots < *PBMap\Options\MaxDownloadSlots If *PBMap\DownloadSlots < *PBMap\Options\MaxDownloadSlots
; Launch a new web loading thread
Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile))
If *NewTile If *NewTile
*timg\Tile = *NewTile ; There's now a loading thread *timg\Tile = *NewTile ; There's now a loading thread
@@ -1395,7 +1392,6 @@ Module PBMap
MyDebug(*PBMap, " Error, maximum threads nb reached", 3) MyDebug(*PBMap, " Error, maximum threads nb reached", 3)
EndIf EndIf
EndIf EndIf
EndIf
UnlockMutex(*PBMap\MemoryCacheAccessMutex) UnlockMutex(*PBMap\MemoryCacheAccessMutex)
ProcedureReturn #False ProcedureReturn #False
EndProcedure EndProcedure
@@ -2702,18 +2698,19 @@ Module PBMap
*PBMap\Redraw = #True *PBMap\Redraw = #True
Case #PB_MAP_RETRY Case #PB_MAP_RETRY
*PBMap\Redraw = #True *PBMap\Redraw = #True
;- #PB_MAP_TILE_CLEANUP : Tile web loading thread cleanup ;- *** Tile web loading thread cleanup
; After a Web tile loading thread, clean the tile structure memory, see GetImageThread() ; After a Web tile loading thread, cleans the tile structure memory, see GetImageThread()
Case #PB_MAP_TILE_CLEANUP Case #PB_MAP_TILE_CLEANUP
LockMutex(*PBMap\MemoryCacheAccessMutex) ; Prevents threads to start or finish
*Tile = EventData() *Tile = EventData()
key = *Tile\key key = *Tile\key
*Tile\Download = 0 *Tile\Download = 0
If FindMapElement(*PBMap\MemCache\Images(), key) <> 0 If FindMapElement(*PBMap\MemCache\Images(), key) <> 0
; If the map element has not been deleted during the thread lifetime (should not occur) ; If the map element has not been deleted during the thread lifetime (should not occur)
*PBMap\MemCache\Images(key)\Tile = *Tile\Size ;*PBMap\MemCache\Images(key)\Tile = *Tile\Size
If *Tile\Size If *Tile\Size
*PBMap\MemCache\Images(key)\Tile = -1 ; Web loading thread has finished successfully *PBMap\MemCache\Images(key)\Tile = -1 ; Web loading thread has finished successfully
;- Allows to post edit the tile image file with a customised code ; Allows to post edit the tile image file with a customised code
If *PBMap\CallBackModifyTileFile If *PBMap\CallBackModifyTileFile
TileNewFilename = *PBMap\CallBackModifyTileFile(*Tile\CacheFile, *Tile\URL) TileNewFilename = *PBMap\CallBackModifyTileFile(*Tile\CacheFile, *Tile\URL)
If TileNewFilename If TileNewFilename
@@ -2729,10 +2726,11 @@ Module PBMap
*PBMap\ThreadsNB - 1 *PBMap\ThreadsNB - 1
*PBMap\DownloadSlots - 1 *PBMap\DownloadSlots - 1
*PBMap\Redraw = #True *PBMap\Redraw = #True
UnlockMutex(*PBMap\MemoryCacheAccessMutex)
EndSelect EndSelect
EndProcedure EndProcedure
; Redraws at regular intervals ;-*** Main timer : Cache management and drawing
Procedure TimerEvents() Procedure TimerEvents()
Protected *PBMap.PBMap Protected *PBMap.PBMap
ForEach PBMaps() ForEach PBMaps()
@@ -3185,8 +3183,8 @@ CompilerIf #PB_Compiler_IsMainFile
CompilerEndIf CompilerEndIf
; IDE Options = PureBasic 5.70 LTS (Windows - x64) ; IDE Options = PureBasic 5.70 LTS (Windows - x64)
; CursorPosition = 3154 ; CursorPosition = 1369
; FirstLine = 3136 ; FirstLine = 1354
; Folding = --------------------- ; Folding = ---------------------
; EnableThread ; EnableThread
; EnableXP ; EnableXP