texture_common.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package cache
  2. import (
  3. "sync"
  4. "fyne.io/fyne/v2"
  5. )
  6. var textures = sync.Map{} // map[fyne.CanvasObject]*textureInfo
  7. // DeleteTexture deletes the texture from the cache map.
  8. func DeleteTexture(obj fyne.CanvasObject) {
  9. textures.Delete(obj)
  10. }
  11. // GetTexture gets cached texture.
  12. func GetTexture(obj fyne.CanvasObject) (TextureType, bool) {
  13. t, ok := textures.Load(obj)
  14. if t == nil || !ok {
  15. return NoTexture, false
  16. }
  17. texInfo := t.(*textureInfo)
  18. texInfo.setAlive()
  19. return texInfo.texture, true
  20. }
  21. // RangeExpiredTexturesFor range over the expired textures for the specified canvas.
  22. //
  23. // Note: If this is used to free textures, then it should be called inside a current
  24. // gl context to ensure textures are deleted from gl.
  25. func RangeExpiredTexturesFor(canvas fyne.Canvas, f func(fyne.CanvasObject)) {
  26. now := timeNow()
  27. textures.Range(func(key, value interface{}) bool {
  28. obj, tinfo := key.(fyne.CanvasObject), value.(*textureInfo)
  29. if tinfo.isExpired(now) && tinfo.canvas == canvas {
  30. f(obj)
  31. }
  32. return true
  33. })
  34. }
  35. // RangeTexturesFor range over the textures for the specified canvas.
  36. //
  37. // Note: If this is used to free textures, then it should be called inside a current
  38. // gl context to ensure textures are deleted from gl.
  39. func RangeTexturesFor(canvas fyne.Canvas, f func(fyne.CanvasObject)) {
  40. textures.Range(func(key, value interface{}) bool {
  41. obj, tinfo := key.(fyne.CanvasObject), value.(*textureInfo)
  42. if tinfo.canvas == canvas {
  43. f(obj)
  44. }
  45. return true
  46. })
  47. }
  48. // SetTexture sets cached texture.
  49. func SetTexture(obj fyne.CanvasObject, texture TextureType, canvas fyne.Canvas) {
  50. texInfo := &textureInfo{texture: texture}
  51. texInfo.canvas = canvas
  52. texInfo.setAlive()
  53. textures.Store(obj, texInfo)
  54. }
  55. // textureCacheBase defines base texture cache object.
  56. type textureCacheBase struct {
  57. expiringCacheNoLock
  58. canvas fyne.Canvas
  59. }