@@ -159,12 +159,15 @@ CachedTile *OpenStreetMap::findUnusedTile(const tileList &requiredTiles, uint8_t
159
159
return nullptr ; // no unused tile found
160
160
}
161
161
162
- bool OpenStreetMap::isTileCachedOrBusy (uint32_t x, uint32_t y, uint8_t z)
162
+ bool OpenStreetMap::isTileCached (uint32_t x, uint32_t y, uint8_t z, TileBufferList &tilePointers )
163
163
{
164
164
for (const auto &tile : tilesCache)
165
165
{
166
- if (tile.x == x && tile.y == y && tile.z == z && (tile.valid || tile.busy ))
166
+ if (tile.x == x && tile.y == y && tile.z == z && tile.valid )
167
+ {
168
+ tilePointers.push_back (tile.buffer );
167
169
return true ;
170
+ }
168
171
}
169
172
return false ;
170
173
}
@@ -197,30 +200,40 @@ bool OpenStreetMap::resizeTilesCache(uint16_t numberOfTiles)
197
200
return true ;
198
201
}
199
202
200
- void OpenStreetMap::updateCache (const tileList &requiredTiles, uint8_t zoom)
203
+ void OpenStreetMap::updateCache (const tileList &requiredTiles, uint8_t zoom, TileBufferList &tilePointers )
201
204
{
202
205
[[maybe_unused]] const unsigned long startMS = millis ();
203
206
std::vector<TileJob> jobs;
204
- makeJobList (requiredTiles, jobs, zoom);
207
+ makeJobList (requiredTiles, jobs, zoom, tilePointers );
205
208
if (!jobs.empty ())
206
209
{
207
210
runJobs (jobs);
208
211
log_d (" Updated %i tiles in %lu ms - %i ms/tile" , jobs.size (), millis () - startMS, (millis () - startMS) / jobs.size ());
209
212
}
210
213
}
211
214
212
- void OpenStreetMap::makeJobList (const tileList &requiredTiles, std::vector<TileJob> &jobs, uint8_t zoom)
215
+ void OpenStreetMap::makeJobList (const tileList &requiredTiles, std::vector<TileJob> &jobs, uint8_t zoom, TileBufferList &tilePointers )
213
216
{
214
217
for (const auto &[x, y] : requiredTiles)
215
218
{
216
- if (isTileCachedOrBusy (x, y, zoom) || y < 0 || y >= (1 << zoom))
219
+ if (y < 0 || y >= (1 << zoom))
220
+ {
221
+ tilePointers.push_back (nullptr ); // we need to keep 1:1 grid alignment with requiredTiles for composeMap
222
+ continue ;
223
+ }
224
+
225
+ if (isTileCached (x, y, zoom, tilePointers)) // isTileCached will push_back a valid ptr if tile is cached
217
226
continue ;
218
227
219
228
CachedTile *tileToReplace = findUnusedTile (requiredTiles, zoom);
220
229
if (!tileToReplace)
221
- continue ; // Should never happen if cache sizing is correct
230
+ {
231
+ tilePointers.push_back (nullptr ); // again, keep 1:1 aligned
232
+ continue ;
233
+ }
222
234
223
- jobs.push_back ({x, static_cast <uint32_t >(y), zoom, tileToReplace});
235
+ tilePointers.push_back (tileToReplace->buffer ); // push_back the still-to-download tile ptr
236
+ jobs.push_back ({x, static_cast <uint32_t >(y), zoom, tileToReplace}); // push_back tile ptr to the job list
224
237
}
225
238
}
226
239
@@ -240,7 +253,7 @@ void OpenStreetMap::runJobs(const std::vector<TileJob> &jobs)
240
253
vTaskDelay (pdMS_TO_TICKS (1 ));
241
254
}
242
255
243
- bool OpenStreetMap::composeMap (LGFX_Sprite &mapSprite, const tileList &requiredTiles, uint8_t zoom )
256
+ bool OpenStreetMap::composeMap (LGFX_Sprite &mapSprite, TileBufferList &tilePointers )
244
257
{
245
258
if (mapSprite.width () != mapWidth || mapSprite.height () != mapHeight)
246
259
{
@@ -255,30 +268,17 @@ bool OpenStreetMap::composeMap(LGFX_Sprite &mapSprite, const tileList &requiredT
255
268
}
256
269
}
257
270
258
- int tileIndex = 0 ;
259
- for (const auto &[tileX, tileY] : requiredTiles)
271
+ for (size_t tileIndex = 0 ; tileIndex < tilePointers.size (); ++tileIndex)
260
272
{
261
- if (tileY < 0 || tileY >= (1 << zoom))
273
+ const int drawX = startOffsetX + (tileIndex % numberOfColums) * currentProvider->tileSize ;
274
+ const int drawY = startOffsetY + (tileIndex / numberOfColums) * currentProvider->tileSize ;
275
+ const uint16_t *tile = tilePointers[tileIndex];
276
+ if (!tile)
262
277
{
263
- tileIndex++ ;
278
+ mapSprite. fillRect (drawX, drawY, currentProvider-> tileSize , currentProvider-> tileSize , OSM_BGCOLOR) ;
264
279
continue ;
265
280
}
266
-
267
- int drawX = startOffsetX + (tileIndex % numberOfColums) * currentProvider->tileSize ;
268
- int drawY = startOffsetY + (tileIndex / numberOfColums) * currentProvider->tileSize ;
269
-
270
- auto it = std::find_if (tilesCache.begin (), tilesCache.end (),
271
- [&](const CachedTile &tile)
272
- {
273
- return tile.x == tileX && tile.y == tileY && tile.z == zoom && tile.valid ;
274
- });
275
-
276
- if (it != tilesCache.end ())
277
- mapSprite.pushImage (drawX, drawY, currentProvider->tileSize , currentProvider->tileSize , it->buffer );
278
- else
279
- log_w (" Tile (z=%d, x=%d, y=%d) not found in cache" , zoom, tileX, tileY);
280
-
281
- tileIndex++;
281
+ mapSprite.pushImage (drawX, drawY, currentProvider->tileSize , currentProvider->tileSize , tile);
282
282
}
283
283
284
284
mapSprite.setTextColor (TFT_BLACK);
@@ -324,14 +324,13 @@ bool OpenStreetMap::fetchMap(LGFX_Sprite &mapSprite, double longitude, double la
324
324
return false ;
325
325
}
326
326
327
- updateCache (requiredTiles, zoom) ;
328
-
329
- if (!composeMap (mapSprite, requiredTiles, zoom ))
327
+ TileBufferList tilePointers ;
328
+ updateCache (requiredTiles, zoom, tilePointers);
329
+ if (!composeMap (mapSprite, tilePointers ))
330
330
{
331
331
log_e (" Failed to compose map" );
332
332
return false ;
333
333
}
334
-
335
334
return true ;
336
335
}
337
336
0 commit comments