diff --git a/src/main/java/com/mapconductor/openmobilemaps/marker/OpenMobileMapsMarkerController.kt b/src/main/java/com/mapconductor/openmobilemaps/marker/OpenMobileMapsMarkerController.kt index f5d8684..6f17c67 100644 --- a/src/main/java/com/mapconductor/openmobilemaps/marker/OpenMobileMapsMarkerController.kt +++ b/src/main/java/com/mapconductor/openmobilemaps/marker/OpenMobileMapsMarkerController.kt @@ -21,8 +21,11 @@ import com.mapconductor.core.tileserver.TileServerRegistry import com.mapconductor.openmobilemaps.OpenMobileMapsActualMarker import com.mapconductor.openmobilemaps.OpenMobileMapsMarkerOverlayRenderer import java.util.UUID +import kotlin.time.Duration.Companion.milliseconds import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.sync.withPermit @@ -123,11 +126,12 @@ class OpenMobileMapsMarkerController( if (currentFinger == prevFinger) return semaphore.withPermit { + // 件数だけが要る。allEntities() は全件の写しを作るので、ラベルの付け替えのように + // 数十本が続けて変わると、そのたびに 14 万件を写すことになる。 val tilingEnabled = - markerTiling.enabled && markerManager.allEntities().size >= markerManager.minMarkerCount + markerTiling.enabled && markerManager.getMemoryStats().entityCount >= markerManager.minMarkerCount val wantsTiled = tilingEnabled && !state.draggable && state.getAnimation() == null val wasTiled = tiledMarkerIds.contains(state.id) - val markerIcon = state.icon?.toBitmapIcon() ?: defaultMarkerIcon if (wantsTiled) { if (!wasTiled) { @@ -145,14 +149,19 @@ class OpenMobileMapsMarkerController( tiling = true, ), ) - renderer.onPostProcess() - syncTiledOverlay() + // タイル担当のままなら、ネイティブのマーカーには何も起きていない。onPostProcess は + // 全件を写してネイティブ分を描き直すので(14 万件で 1 回 50ms)、ネイティブから + // タイルへ移ったときだけ呼ぶ。 + if (!wasTiled) renderer.onPostProcess() + scheduleTileRefresh() return@withPermit } if (wasTiled) { tiledMarkerIds.remove(state.id) } + // ネイティブマーカーとして描くときだけ焼き込む。タイル担当なら要らない。 + val markerIcon = state.icon?.toBitmapIcon() ?: defaultMarkerIcon val params = object : MarkerOverlayRendererInterface.ChangeParamsInterface { @@ -183,7 +192,7 @@ class OpenMobileMapsMarkerController( renderer.onPostProcess() if (tiledMarkerIds.isNotEmpty()) { - syncTiledOverlay() + scheduleTileRefresh() } else { removeTileOverlay() } @@ -191,6 +200,25 @@ class OpenMobileMapsMarkerController( viewportSwitch.requestReapply() } + private var tileRefreshJob: Job? = null + + /** + * 1 本ずつの変更からの描き直しを 1 回にまとめる。 + * + * ラベルの付け替えのように数十本が続けて変わると、1 本ごとにラスターソースを差し替え、 + * そのたびに地図が見えているタイルを全部取り直していた(MapLibre・Pixel 5a で 45 回、 + * 描き直したタイル 494 枚、最後のタイルまで 6 秒)。続けて来た変更は + * [TILE_REFRESH_COALESCE] の間に受けて、まとめて 1 回だけ差し替える。 + */ + private fun scheduleTileRefresh() { + if (tileRefreshJob?.isActive == true) return + tileRefreshJob = + coroutine.launch { + delay(TILE_REFRESH_COALESCE) + semaphore.withPermit { syncTiledOverlay() } + } + } + override suspend fun clear() { viewportSwitch.destroy() semaphore.withPermit { @@ -226,6 +254,8 @@ class OpenMobileMapsMarkerController( override fun destroy() { viewportSwitch.destroy() + // 待っている描き直しが後から走ると、外したタイル経路をまた登録してしまう。 + tileRefreshJob?.cancel() // タイルサーバはプロセス共有のシングルトン。**stop してはいけない** // (他の地図やオーバーレイ拡張のタイルまで止まる)。自分の経路だけ外す。 markerTileGroupId?.let { tileServer.unregister(it) } @@ -319,4 +349,9 @@ class OpenMobileMapsMarkerController( rasterLayerCallback?.onRasterLayerUpdate(null) markerTileRasterLayerState = null } + + private companion object { + /** 続けて来る変更を 1 回の描き直しにまとめる窓。1 フレーム分。 */ + val TILE_REFRESH_COALESCE = 16.milliseconds + } }