webglMapRenderer.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import { CourseLayer } from '../layer/courseLayer'
  2. import { TrackLayer } from '../layer/trackLayer'
  3. import { GpsLayer } from '../layer/gpsLayer'
  4. import { TileLayer } from '../layer/tileLayer'
  5. import { TileStore, type TileStoreCallbacks } from '../tile/tileStore'
  6. import { type MapRenderer, type MapRendererStats, type MapScene } from './mapRenderer'
  7. import { WebGLTileRenderer } from './webglTileRenderer'
  8. import { WebGLVectorRenderer } from './webglVectorRenderer'
  9. import { CourseLabelRenderer } from './courseLabelRenderer'
  10. import { type MockSimulatorDebugLogLevel } from '../debug/mockSimulatorDebugLogger'
  11. const RENDER_FRAME_MS = 16
  12. const ANIMATION_FRAME_MS = 33
  13. export class WebGLMapRenderer implements MapRenderer {
  14. tileStore: TileStore
  15. osmTileStore: TileStore
  16. tileLayer: TileLayer
  17. osmTileLayer: TileLayer
  18. courseLayer: CourseLayer
  19. trackLayer: TrackLayer
  20. gpsLayer: GpsLayer
  21. tileRenderer: WebGLTileRenderer
  22. vectorRenderer: WebGLVectorRenderer
  23. labelRenderer: CourseLabelRenderer
  24. scene: MapScene | null
  25. renderTimer: number
  26. animationTimer: number
  27. destroyed: boolean
  28. animationPaused: boolean
  29. pulseFrame: number
  30. lastStats: MapRendererStats
  31. lastGpsLogoDebugInfo: { status: string; url: string; resolvedSrc: string }
  32. onStats?: (stats: MapRendererStats) => void
  33. onTileError?: (message: string) => void
  34. onGpsLogoDebug?: (info: { status: string; url: string; resolvedSrc: string }) => void
  35. onDebugLog?: (
  36. scope: string,
  37. level: MockSimulatorDebugLogLevel,
  38. message: string,
  39. payload?: Record<string, unknown>,
  40. ) => void
  41. constructor(
  42. onStats?: (stats: MapRendererStats) => void,
  43. onTileError?: (message: string) => void,
  44. onGpsLogoDebug?: (info: { status: string; url: string; resolvedSrc: string }) => void,
  45. onDebugLog?: (
  46. scope: string,
  47. level: MockSimulatorDebugLogLevel,
  48. message: string,
  49. payload?: Record<string, unknown>,
  50. ) => void,
  51. ) {
  52. this.onStats = onStats
  53. this.onTileError = onTileError
  54. this.onGpsLogoDebug = onGpsLogoDebug
  55. this.onDebugLog = onDebugLog
  56. this.tileStore = new TileStore({
  57. onTileReady: () => {
  58. this.scheduleRender()
  59. },
  60. onTileError: (message) => {
  61. if (this.onTileError) {
  62. this.onTileError(message)
  63. }
  64. this.scheduleRender()
  65. },
  66. } satisfies TileStoreCallbacks)
  67. this.osmTileStore = new TileStore({
  68. onTileReady: () => {
  69. this.scheduleRender()
  70. },
  71. onTileError: () => {
  72. this.scheduleRender()
  73. },
  74. } satisfies TileStoreCallbacks)
  75. this.tileLayer = new TileLayer()
  76. this.osmTileLayer = new TileLayer()
  77. this.courseLayer = new CourseLayer()
  78. this.trackLayer = new TrackLayer()
  79. this.gpsLayer = new GpsLayer()
  80. this.tileRenderer = new WebGLTileRenderer(this.tileLayer, this.tileStore, this.osmTileLayer, this.osmTileStore)
  81. this.vectorRenderer = new WebGLVectorRenderer(this.courseLayer, this.trackLayer, this.gpsLayer)
  82. this.labelRenderer = new CourseLabelRenderer(this.courseLayer, onDebugLog)
  83. this.scene = null
  84. this.renderTimer = 0
  85. this.animationTimer = 0
  86. this.destroyed = false
  87. this.animationPaused = false
  88. this.pulseFrame = 0
  89. this.lastStats = {
  90. visibleTileCount: 0,
  91. readyTileCount: 0,
  92. memoryTileCount: 0,
  93. diskTileCount: 0,
  94. memoryHitCount: 0,
  95. diskHitCount: 0,
  96. networkFetchCount: 0,
  97. }
  98. this.lastGpsLogoDebugInfo = {
  99. status: 'idle',
  100. url: '',
  101. resolvedSrc: '',
  102. }
  103. }
  104. attachCanvas(canvasNode: any, width: number, height: number, dpr: number, labelCanvasNode?: any): void {
  105. this.tileRenderer.attachCanvas(canvasNode, width, height, dpr)
  106. this.vectorRenderer.attachContext(this.tileRenderer.gl, canvasNode)
  107. if (labelCanvasNode) {
  108. this.labelRenderer.attachCanvas(labelCanvasNode, width, height, dpr)
  109. }
  110. this.startAnimation()
  111. this.scheduleRender()
  112. }
  113. updateScene(scene: MapScene): void {
  114. this.scene = scene
  115. this.scheduleRender()
  116. }
  117. setAnimationPaused(paused: boolean): void {
  118. this.animationPaused = paused
  119. if (!paused) {
  120. this.scheduleRender()
  121. }
  122. }
  123. destroy(): void {
  124. this.destroyed = true
  125. if (this.renderTimer) {
  126. clearTimeout(this.renderTimer)
  127. this.renderTimer = 0
  128. }
  129. if (this.animationTimer) {
  130. clearTimeout(this.animationTimer)
  131. this.animationTimer = 0
  132. }
  133. this.labelRenderer.destroy()
  134. this.vectorRenderer.destroy()
  135. this.tileRenderer.destroy()
  136. this.tileStore.destroy()
  137. this.osmTileStore.destroy()
  138. this.scene = null
  139. }
  140. startAnimation(): void {
  141. if (this.animationTimer) {
  142. return
  143. }
  144. const tick = () => {
  145. if (this.destroyed) {
  146. this.animationTimer = 0
  147. return
  148. }
  149. if (!this.animationPaused) {
  150. this.pulseFrame = (this.pulseFrame + 1) % 360
  151. this.scheduleRender()
  152. }
  153. this.animationTimer = setTimeout(tick, this.getAnimationFrameMs()) as unknown as number
  154. }
  155. tick()
  156. }
  157. getAnimationFrameMs(): number {
  158. return this.scene && this.scene.animationLevel === 'lite' ? 48 : ANIMATION_FRAME_MS
  159. }
  160. scheduleRender(): void {
  161. if (this.renderTimer || !this.scene || this.destroyed) {
  162. return
  163. }
  164. this.renderTimer = setTimeout(() => {
  165. this.renderTimer = 0
  166. this.renderFrame()
  167. }, RENDER_FRAME_MS) as unknown as number
  168. }
  169. renderFrame(): void {
  170. if (!this.scene) {
  171. return
  172. }
  173. this.tileRenderer.render(this.scene)
  174. this.vectorRenderer.render(this.scene, this.pulseFrame)
  175. this.labelRenderer.render(this.scene)
  176. this.emitGpsLogoDebug(this.labelRenderer.getGpsLogoDebugInfo())
  177. this.emitStats(this.tileStore.getStats(this.tileLayer.lastVisibleTileCount, this.tileLayer.lastReadyTileCount))
  178. }
  179. getGpsLogoDebugInfo(): { status: string; url: string; resolvedSrc: string } {
  180. return this.labelRenderer.getGpsLogoDebugInfo()
  181. }
  182. emitStats(stats: MapRendererStats): void {
  183. if (
  184. stats.visibleTileCount === this.lastStats.visibleTileCount
  185. && stats.readyTileCount === this.lastStats.readyTileCount
  186. && stats.memoryTileCount === this.lastStats.memoryTileCount
  187. && stats.diskTileCount === this.lastStats.diskTileCount
  188. && stats.memoryHitCount === this.lastStats.memoryHitCount
  189. && stats.diskHitCount === this.lastStats.diskHitCount
  190. && stats.networkFetchCount === this.lastStats.networkFetchCount
  191. ) {
  192. return
  193. }
  194. this.lastStats = stats
  195. if (this.onStats) {
  196. this.onStats(stats)
  197. }
  198. }
  199. emitGpsLogoDebug(info: { status: string; url: string; resolvedSrc: string }): void {
  200. if (
  201. info.status === this.lastGpsLogoDebugInfo.status
  202. && info.url === this.lastGpsLogoDebugInfo.url
  203. && info.resolvedSrc === this.lastGpsLogoDebugInfo.resolvedSrc
  204. ) {
  205. return
  206. }
  207. this.lastGpsLogoDebugInfo = info
  208. if (this.onGpsLogoDebug) {
  209. this.onGpsLogoDebug(info)
  210. }
  211. }
  212. }