Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/OpenStreetMap-esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,36 +215,36 @@ void OpenStreetMap::makeJobList(const tileList &requiredTiles, std::vector<TileJ
{
if (y < 0 || y >= (1 << zoom))
{
tilePointers.push_back(nullptr); // we need to keep 1:1 grid alignment with requiredTiles for composeMap
tilePointers.emplace_back(nullptr); // keep alignment
continue;
}

const CachedTile *cachedTile = isTileCached(x, y, zoom);
if (cachedTile)
{
tilePointers.push_back(cachedTile->buffer);
tilePointers.emplace_back(const_cast<CachedTile *>(cachedTile));
continue;
}

// Check if this tile is already in the job list
const auto job = std::find_if(jobs.begin(), jobs.end(), [&](const TileJob &job)
{ return job.x == x && job.y == static_cast<uint32_t>(y) && job.z == zoom; });
if (job != jobs.end())
{
tilePointers.push_back(job->tile->buffer); // reuse buffer from already queued job
tilePointers.emplace_back(const_cast<CachedTile *>(cachedTile));
continue;
}

CachedTile *tileToReplace = findUnusedTile(requiredTiles, zoom);
if (!tileToReplace)
{
log_e("Cache error, no unused tile found, could not store tile %lu, %i, %u", x, y, zoom);
tilePointers.push_back(nullptr); // again, keep 1:1 aligned
tilePointers.emplace_back(nullptr); // keep alignment
continue;
}

tilePointers.push_back(tileToReplace->buffer); // store buffer for rendering
jobs.push_back({x, static_cast<uint32_t>(y), zoom, tileToReplace}); // queue job
// store buffer and current validity for rendering
tilePointers.emplace_back(tileToReplace);
jobs.push_back({x, static_cast<uint32_t>(y), zoom, tileToReplace});
}
}

Expand Down Expand Up @@ -284,13 +284,18 @@ bool OpenStreetMap::composeMap(LGFX_Sprite &mapSprite, TileBufferList &tilePoint
{
const int drawX = startOffsetX + (tileIndex % numberOfColums) * currentProvider->tileSize;
const int drawY = startOffsetY + (tileIndex / numberOfColums) * currentProvider->tileSize;
const uint16_t *tile = tilePointers[tileIndex];
if (!tile)
const TileBuffer &tb = tilePointers[tileIndex];
CachedTile *ct = tb.cached;

// draw background if no tile or tile not valid yet
if (!ct || !ct->valid || !ct->buffer)
{
mapSprite.fillRect(drawX, drawY, currentProvider->tileSize, currentProvider->tileSize, OSM_BGCOLOR);
continue;
}
mapSprite.pushImage(drawX, drawY, currentProvider->tileSize, currentProvider->tileSize, tile);

// draw from live cached buffer
mapSprite.pushImage(drawX, drawY, currentProvider->tileSize, currentProvider->tileSize, ct->buffer);
}

mapSprite.setTextColor(TFT_WHITE, OSM_BGCOLOR);
Expand Down Expand Up @@ -530,10 +535,6 @@ void OpenStreetMap::invalidateTile(CachedTile *tile)
{
if (!tile)
return;

const size_t tileByteCount = currentProvider->tileSize * currentProvider->tileSize * 2;
memset(tile->buffer, 0, tileByteCount);

tile->valid = false;
tile->busy = false;
}
10 changes: 9 additions & 1 deletion src/OpenStreetMap-esp32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ constexpr int OSM_SINGLECORE_NUMBER = 1;
static_assert(OSM_SINGLECORE_NUMBER < 2, "OSM_SINGLECORE_NUMBER must be 0 or 1 (ESP32 has only 2 cores)");

using tileList = std::vector<std::pair<uint32_t, int32_t>>;
using TileBufferList = std::vector<uint16_t *>;

struct TileBuffer
{
CachedTile *cached; // nullptr = tile not present / out-of-range
TileBuffer() : cached(nullptr) {}
TileBuffer(CachedTile *c) : cached(c) {}
};

using TileBufferList = std::vector<TileBuffer>;

namespace
{
Expand Down