@geastack/host 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +25 -0
- package/audio_runtime.cpp +807 -0
- package/bluetooth.cpp +125 -0
- package/geolocation.cpp +43 -0
- package/host/apps.cpp +20 -0
- package/host/audio.cpp +240 -0
- package/host/ble.cpp +239 -0
- package/host/camera.cpp +193 -0
- package/host/display.cpp +230 -0
- package/host/fetch.cpp +1060 -0
- package/host/geolocation.cpp +31 -0
- package/host/gpio.cpp +55 -0
- package/host/http.cpp +349 -0
- package/host/image.cpp +335 -0
- package/host/imu.cpp +51 -0
- package/host/input.cpp +12 -0
- package/host/media.cpp +579 -0
- package/host/memory.cpp +91 -0
- package/host/rtc.cpp +212 -0
- package/host/rtc_esp.cpp +291 -0
- package/host/tile_loader.cpp +478 -0
- package/host/timers.cpp +327 -0
- package/host/touch.cpp +25 -0
- package/host/websocket.cpp +279 -0
- package/host/wifi.cpp +99 -0
- package/include/gea/embedded-host.h +38 -0
- package/include/host/apps.h +15 -0
- package/include/host/audio.h +158 -0
- package/include/host/backends.h +455 -0
- package/include/host/battery.h +25 -0
- package/include/host/camera.h +236 -0
- package/include/host/clock.h +45 -0
- package/include/host/device_control.h +23 -0
- package/include/host/display.h +266 -0
- package/include/host/display_orientation.h +291 -0
- package/include/host/fetch.h +310 -0
- package/include/host/gpio.h +36 -0
- package/include/host/http.h +161 -0
- package/include/host/image.h +144 -0
- package/include/host/input.h +14 -0
- package/include/host/media.h +165 -0
- package/include/host/memory.h +44 -0
- package/include/host/navigator.h +283 -0
- package/include/host/notify.h +41 -0
- package/include/host/profiler.h +49 -0
- package/include/host/rtc.h +174 -0
- package/include/host/storage.h +141 -0
- package/include/host/tile_loader.h +59 -0
- package/include/host/timers.h +41 -0
- package/include/host/touch.h +20 -0
- package/include/host/websocket.h +179 -0
- package/include/host/window.h +31 -0
- package/input.cpp +68 -0
- package/package.json +27 -0
- package/services/app_state.cpp +19 -0
- package/services/battery_service.cpp +19 -0
- package/services/bluetooth_service.cpp +37 -0
- package/services/diagnostics.cpp +233 -0
- package/services/diagnostics_connection.cpp +231 -0
- package/services/diagnostics_internal.h +15 -0
- package/services/diagnostics_server.cpp +135 -0
- package/services/mirror.cpp +364 -0
- package/services/network_services.cpp +62 -0
- package/services/runtime_log.cpp +23 -0
- package/services/settings_surface.h +221 -0
- package/wifi.cpp +185 -0
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#include "host/tile_loader.h"
|
|
3
|
+
|
|
4
|
+
#include "host/fetch.h"
|
|
5
|
+
#include "host/display_orientation.h"
|
|
6
|
+
#include "host/image.h"
|
|
7
|
+
#include "image.h"
|
|
8
|
+
#include "platform/file_cache.h"
|
|
9
|
+
|
|
10
|
+
#include <chrono>
|
|
11
|
+
#include <cstdio>
|
|
12
|
+
#include <cstdint>
|
|
13
|
+
#include <deque>
|
|
14
|
+
#include <dirent.h>
|
|
15
|
+
#include <mutex>
|
|
16
|
+
#include <string>
|
|
17
|
+
#include <unistd.h>
|
|
18
|
+
#include <unordered_set>
|
|
19
|
+
#include <vector>
|
|
20
|
+
|
|
21
|
+
#if !defined(ESP_PLATFORM) && !defined(__EMSCRIPTEN__)
|
|
22
|
+
#include <thread>
|
|
23
|
+
#endif
|
|
24
|
+
|
|
25
|
+
#ifdef ESP_PLATFORM
|
|
26
|
+
#include "esp_cache.h"
|
|
27
|
+
#include "freertos/FreeRTOS.h"
|
|
28
|
+
#include "freertos/idf_additions.h" // xTaskCreatePinnedToCoreWithCaps (PSRAM worker stacks)
|
|
29
|
+
#include "freertos/task.h"
|
|
30
|
+
#endif
|
|
31
|
+
|
|
32
|
+
namespace gea::host {
|
|
33
|
+
|
|
34
|
+
namespace {
|
|
35
|
+
|
|
36
|
+
constexpr int kTileStatusDecodePressure = -2;
|
|
37
|
+
|
|
38
|
+
struct TileJob {
|
|
39
|
+
std::string key;
|
|
40
|
+
double num = 0;
|
|
41
|
+
std::string path;
|
|
42
|
+
std::string url;
|
|
43
|
+
bool opaque = false;
|
|
44
|
+
bool rotate90 = false;
|
|
45
|
+
std::int64_t requestedUs = 0;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
struct TileResult {
|
|
49
|
+
std::string key;
|
|
50
|
+
double num = 0;
|
|
51
|
+
int imageId = -1;
|
|
52
|
+
int status = 0;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
std::mutex g_tiles_mutex;
|
|
56
|
+
std::deque<TileJob> g_jobs;
|
|
57
|
+
std::unordered_set<std::string> g_pending; // keys queued or in flight
|
|
58
|
+
std::deque<TileResult> g_results;
|
|
59
|
+
TileResult g_current; // result selected by the last poll()
|
|
60
|
+
// While false (mid-gesture), workers serve the persistent cache only; misses
|
|
61
|
+
// complete immediately with status 0 so the app can re-request after settle.
|
|
62
|
+
volatile bool g_network_allowed = true;
|
|
63
|
+
|
|
64
|
+
std::int64_t nowUs() {
|
|
65
|
+
return std::chrono::duration_cast<std::chrono::microseconds>(
|
|
66
|
+
std::chrono::steady_clock::now().time_since_epoch())
|
|
67
|
+
.count();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
std::vector<std::uint8_t> readWholeFile(const std::string &path) {
|
|
71
|
+
std::vector<std::uint8_t> out;
|
|
72
|
+
std::FILE *f = std::fopen(path.c_str(), "rb");
|
|
73
|
+
if (!f) return out;
|
|
74
|
+
std::fseek(f, 0, SEEK_END);
|
|
75
|
+
const long size = std::ftell(f);
|
|
76
|
+
std::fseek(f, 0, SEEK_SET);
|
|
77
|
+
if (size > 0) {
|
|
78
|
+
out.resize(static_cast<std::size_t>(size));
|
|
79
|
+
const std::size_t got = std::fread(out.data(), 1, out.size(), f);
|
|
80
|
+
out.resize(got);
|
|
81
|
+
}
|
|
82
|
+
std::fclose(f);
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
int decodeBytes(const std::vector<std::uint8_t> &bytes, bool opaque, bool rotate90) {
|
|
87
|
+
auto &store = gea::framework::graphics::ImageStore::instance();
|
|
88
|
+
const int id = opaque ? store.decodeOpaque(bytes.data(), static_cast<int>(bytes.size()), -1)
|
|
89
|
+
: store.decode(bytes.data(), static_cast<int>(bytes.size()), -1);
|
|
90
|
+
#ifdef ESP_PLATFORM
|
|
91
|
+
// Bitmap pre-rotation is only valid when the logical drawing axes are
|
|
92
|
+
// actually swapped 90deg relative to the native panel scanout — i.e. when the
|
|
93
|
+
// effective orientation is landscape (width()==nativeHeight()). A non-square
|
|
94
|
+
// panel that still scans out in its native portrait orientation (e.g. the
|
|
95
|
+
// CO5300 410x502 board, which has no rotation hook so setOrientation pins
|
|
96
|
+
// PortraitPrimary) is NOT swapped: rotating each tile there destroys the
|
|
97
|
+
// shared tile edges and the map looks scrambled. The old "panel is non-square"
|
|
98
|
+
// check mis-fired for exactly that case.
|
|
99
|
+
const bool nativeAxesSwap = gea::framework::display::detail::orientationIsLandscape(
|
|
100
|
+
gea::framework::display::detail::DisplayOrientationState::orientation());
|
|
101
|
+
if (id >= 0 && rotate90 && nativeAxesSwap) store.rotate90(id);
|
|
102
|
+
#endif
|
|
103
|
+
#ifdef ESP_PLATFORM
|
|
104
|
+
// Write the freshly decoded pixels back to PSRAM from THIS core's cache: the
|
|
105
|
+
// renderer (CPU on core 0, or the PPA's DMA) reads them moments later, and
|
|
106
|
+
// dirty lines stuck in core 1's L1 would otherwise serve stale/garbage data.
|
|
107
|
+
if (id >= 0) {
|
|
108
|
+
const auto *pixels = store.currentPixels(id);
|
|
109
|
+
const auto *alpha = store.currentAlpha(id);
|
|
110
|
+
const std::size_t pixelBytes =
|
|
111
|
+
static_cast<std::size_t>(store.width(id)) * store.height(id) * sizeof(*pixels);
|
|
112
|
+
constexpr int kFlags = ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_TYPE_DATA | ESP_CACHE_MSYNC_FLAG_UNALIGNED;
|
|
113
|
+
if (pixels && pixelBytes > 0) esp_cache_msync(const_cast<void *>(static_cast<const void *>(pixels)), pixelBytes, kFlags);
|
|
114
|
+
if (alpha) esp_cache_msync(const_cast<void *>(static_cast<const void *>(alpha)), pixelBytes / sizeof(*pixels), kFlags);
|
|
115
|
+
}
|
|
116
|
+
#endif
|
|
117
|
+
return id;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
void deliver(const TileJob &job, int imageId, int status) {
|
|
121
|
+
std::lock_guard<std::mutex> lock(g_tiles_mutex);
|
|
122
|
+
g_results.push_back(TileResult{job.key, job.num, imageId, status});
|
|
123
|
+
g_pending.erase(job.key);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// SD-first, network-fallback load. Persist the raw bytes only after a
|
|
127
|
+
// successful decode, so a truncated download never poisons the cache.
|
|
128
|
+
// Every tile logs its phase breakdown: queue wait, SD read, network fetch,
|
|
129
|
+
// decode, persist — slow loading must never be guesswork again.
|
|
130
|
+
void processJob(const TileJob &job) {
|
|
131
|
+
const std::int64_t startUs = nowUs();
|
|
132
|
+
const std::int64_t queueUs = job.requestedUs > 0 ? startUs - job.requestedUs : 0;
|
|
133
|
+
if (gea::platform::storage::ensureMounted()) {
|
|
134
|
+
const std::int64_t sd0 = nowUs();
|
|
135
|
+
const std::vector<std::uint8_t> cached = readWholeFile(job.path);
|
|
136
|
+
const std::int64_t sdUs = nowUs() - sd0;
|
|
137
|
+
if (!cached.empty()) {
|
|
138
|
+
const std::int64_t dec0 = nowUs();
|
|
139
|
+
const int id = decodeBytes(cached, job.opaque, job.rotate90);
|
|
140
|
+
const std::int64_t decUs = nowUs() - dec0;
|
|
141
|
+
if (id >= 0) {
|
|
142
|
+
deliver(job, id, 200);
|
|
143
|
+
std::printf("[tiles] %s src=sd queue=%lldms sd=%lldms decode=%lldms total=%lldms\n", job.key.c_str(),
|
|
144
|
+
queueUs / 1000, sdUs / 1000, decUs / 1000, (nowUs() - job.requestedUs) / 1000);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
// Usually PSRAM/image-store pressure, not corruption: cached files are
|
|
148
|
+
// written only after a successful decode. Keep the file and retry after
|
|
149
|
+
// the app has evicted resident tiles.
|
|
150
|
+
deliver(job, -1, kTileStatusDecodePressure);
|
|
151
|
+
std::printf("[tiles] %s src=sd queue=%lldms sd=%lldms decode=fail total=%lldms\n", job.key.c_str(),
|
|
152
|
+
queueUs / 1000, sdUs / 1000, (nowUs() - job.requestedUs) / 1000);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (!g_network_allowed) {
|
|
158
|
+
// Mid-gesture: don't touch the network. Status 0 is retryable — the app
|
|
159
|
+
// re-requests once the gesture settles.
|
|
160
|
+
deliver(job, -1, 0);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const std::int64_t net0 = nowUs();
|
|
164
|
+
const FetchResponse res = fetch(job.url);
|
|
165
|
+
const std::int64_t netUs = nowUs() - net0;
|
|
166
|
+
if (!res.ok) {
|
|
167
|
+
deliver(job, -1, static_cast<int>(res.status));
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const std::int64_t dec0 = nowUs();
|
|
171
|
+
const int id = decodeBytes(res.body, job.opaque, job.rotate90);
|
|
172
|
+
const std::int64_t decUs = nowUs() - dec0;
|
|
173
|
+
if (id < 0) {
|
|
174
|
+
deliver(job, -1, kTileStatusDecodePressure);
|
|
175
|
+
std::printf("[tiles] %s src=net queue=%lldms net=%lldms decode=fail total=%lldms\n",
|
|
176
|
+
job.key.c_str(), queueUs / 1000, netUs / 1000, (nowUs() - job.requestedUs) / 1000);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
// Deliver BEFORE persisting: the SD write costs 30-80ms and the tile is
|
|
180
|
+
// already decoded — make it visible now, write the cache file after.
|
|
181
|
+
deliver(job, id, 200);
|
|
182
|
+
const std::int64_t per0 = nowUs();
|
|
183
|
+
image.writeFile(job.path, res.body);
|
|
184
|
+
std::printf("[tiles] %s src=net queue=%lldms net=%lldms decode=%lldms persist=%lldms total=%lldms\n",
|
|
185
|
+
job.key.c_str(), queueUs / 1000, netUs / 1000, decUs / 1000, (nowUs() - per0) / 1000,
|
|
186
|
+
(nowUs() - job.requestedUs) / 1000);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Pending prune request (set by the app at startup, executed by worker 0
|
|
190
|
+
// before the census so the census reports the post-prune state).
|
|
191
|
+
std::string g_prune_keep_csv;
|
|
192
|
+
volatile bool g_prune_pending = false;
|
|
193
|
+
|
|
194
|
+
// Delete every tiles/<z> level directory whose z is NOT in the keep CSV.
|
|
195
|
+
// Depth-2 tree remove (z/x/y.png) over FAT — worker core only.
|
|
196
|
+
void pruneTileLevels(const std::string &root, const std::string &keepCsv) {
|
|
197
|
+
std::unordered_set<std::string> keep;
|
|
198
|
+
std::string token;
|
|
199
|
+
for (char c : keepCsv + ",") {
|
|
200
|
+
if (c == ',') {
|
|
201
|
+
if (!token.empty()) keep.insert(token);
|
|
202
|
+
token.clear();
|
|
203
|
+
} else {
|
|
204
|
+
token += c;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
DIR *zDir = opendir(root.c_str());
|
|
208
|
+
if (!zDir) return;
|
|
209
|
+
std::vector<std::string> doomed;
|
|
210
|
+
while (dirent *zEntry = readdir(zDir)) {
|
|
211
|
+
if (zEntry->d_name[0] == '.') continue;
|
|
212
|
+
if (!keep.count(zEntry->d_name)) doomed.push_back(zEntry->d_name);
|
|
213
|
+
}
|
|
214
|
+
closedir(zDir);
|
|
215
|
+
for (const std::string &z : doomed) {
|
|
216
|
+
const std::string zPath = root + "/" + z;
|
|
217
|
+
int removed = 0;
|
|
218
|
+
DIR *xDir = opendir(zPath.c_str());
|
|
219
|
+
if (!xDir) continue;
|
|
220
|
+
std::vector<std::string> xDirs;
|
|
221
|
+
while (dirent *xEntry = readdir(xDir)) {
|
|
222
|
+
if (xEntry->d_name[0] != '.') xDirs.push_back(xEntry->d_name);
|
|
223
|
+
}
|
|
224
|
+
closedir(xDir);
|
|
225
|
+
for (const std::string &x : xDirs) {
|
|
226
|
+
const std::string xPath = zPath + "/" + x;
|
|
227
|
+
DIR *yDir = opendir(xPath.c_str());
|
|
228
|
+
if (!yDir) continue;
|
|
229
|
+
std::vector<std::string> files;
|
|
230
|
+
while (dirent *yEntry = readdir(yDir)) {
|
|
231
|
+
if (yEntry->d_name[0] != '.') files.push_back(yEntry->d_name);
|
|
232
|
+
}
|
|
233
|
+
closedir(yDir);
|
|
234
|
+
for (const std::string &f : files) {
|
|
235
|
+
if (std::remove((xPath + "/" + f).c_str()) == 0) removed++;
|
|
236
|
+
}
|
|
237
|
+
::rmdir(xPath.c_str());
|
|
238
|
+
}
|
|
239
|
+
::rmdir(zPath.c_str());
|
|
240
|
+
std::printf("[tiles] pruned z%s (%d files)\n", z.c_str(), removed);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// One-time census of the persistent tile cache: logs how many tiles each zoom
|
|
245
|
+
// level holds (tiles/<z>/<x>/<y>.png). Runs once on the worker core before the
|
|
246
|
+
// first job — readdir over FAT is slow, so it must never touch the frame task.
|
|
247
|
+
void logTileCensus(const std::string &root) {
|
|
248
|
+
DIR *zDir = opendir(root.c_str());
|
|
249
|
+
if (!zDir) return;
|
|
250
|
+
int totalFiles = 0;
|
|
251
|
+
int levels = 0;
|
|
252
|
+
while (dirent *zEntry = readdir(zDir)) {
|
|
253
|
+
if (zEntry->d_name[0] == '.') continue;
|
|
254
|
+
const std::string zPath = root + "/" + zEntry->d_name;
|
|
255
|
+
DIR *xDir = opendir(zPath.c_str());
|
|
256
|
+
if (!xDir) continue;
|
|
257
|
+
int count = 0;
|
|
258
|
+
while (dirent *xEntry = readdir(xDir)) {
|
|
259
|
+
if (xEntry->d_name[0] == '.') continue;
|
|
260
|
+
const std::string xPath = zPath + "/" + xEntry->d_name;
|
|
261
|
+
DIR *yDir = opendir(xPath.c_str());
|
|
262
|
+
if (!yDir) continue;
|
|
263
|
+
while (dirent *yEntry = readdir(yDir)) {
|
|
264
|
+
if (yEntry->d_name[0] != '.') count++;
|
|
265
|
+
}
|
|
266
|
+
closedir(yDir);
|
|
267
|
+
}
|
|
268
|
+
closedir(xDir);
|
|
269
|
+
std::printf("[tiles] census z%s: %d tiles\n", zEntry->d_name, count);
|
|
270
|
+
totalFiles += count;
|
|
271
|
+
levels++;
|
|
272
|
+
}
|
|
273
|
+
closedir(zDir);
|
|
274
|
+
std::printf("[tiles] census total: %d tiles across %d levels\n", totalFiles, levels);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
#ifdef ESP_PLATFORM
|
|
278
|
+
|
|
279
|
+
void tileWorker(void *arg) {
|
|
280
|
+
// Worker 0 handles one-time maintenance before serving jobs: wait briefly
|
|
281
|
+
// for the app's prune request (sent during startup), apply it, then log the
|
|
282
|
+
// census of what's left. Worker 1 serves jobs in the meantime.
|
|
283
|
+
if (arg == nullptr && gea::platform::storage::ensureMounted()) {
|
|
284
|
+
for (int i = 0; i < 30 && !g_prune_pending; i++) vTaskDelay(pdMS_TO_TICKS(100));
|
|
285
|
+
if (g_prune_pending) {
|
|
286
|
+
pruneTileLevels("/sdcard/tiles", g_prune_keep_csv);
|
|
287
|
+
g_prune_pending = false;
|
|
288
|
+
}
|
|
289
|
+
logTileCensus("/sdcard/tiles");
|
|
290
|
+
}
|
|
291
|
+
while (true) {
|
|
292
|
+
TileJob job;
|
|
293
|
+
bool haveJob = false;
|
|
294
|
+
{
|
|
295
|
+
std::lock_guard<std::mutex> lock(g_tiles_mutex);
|
|
296
|
+
if (!g_jobs.empty()) {
|
|
297
|
+
job = g_jobs.front();
|
|
298
|
+
g_jobs.pop_front();
|
|
299
|
+
haveJob = true;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (!haveJob) {
|
|
303
|
+
vTaskDelay(pdMS_TO_TICKS(15));
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
processJob(job);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
void ensureWorker() {
|
|
311
|
+
static bool started = false;
|
|
312
|
+
if (started) return;
|
|
313
|
+
started = true;
|
|
314
|
+
// Second core: the frame task is pinned to core 0
|
|
315
|
+
// (GEA_EMBEDDED_APP_FRAME_TASK_CORE), so SD reads, TLS fetches, and PNG
|
|
316
|
+
// decodes all run on core 1 and never steal frame time. The stack must fit a
|
|
317
|
+
// TLS handshake (~16 KiB peak with the cert bundle). TWO workers: tile
|
|
318
|
+
// fetches are network-latency-bound, so two in flight nearly halve the
|
|
319
|
+
// fill time of an uncached screen (each worker keeps its own thread-local
|
|
320
|
+
// keep-alive connection — two is also the polite per-client cap for OSM).
|
|
321
|
+
// PSRAM-backed stacks: each 20 KiB TLS-handshake stack would otherwise eat
|
|
322
|
+
// internal DRAM (40 KiB for the pair), collapsing the largest contiguous
|
|
323
|
+
// internal block below what the lwIP socket + TLS handshake need (especially
|
|
324
|
+
// once the fatfs/SD stack is also linked). The tile worker only does network,
|
|
325
|
+
// SD-over-SPI, and PSRAM decode — never an internal-flash op — so it is never
|
|
326
|
+
// the task running during a cache-disable window, and a PSRAM stack is safe
|
|
327
|
+
// (IDF freezes it during any other task's flash op). Same pattern as the BLE
|
|
328
|
+
// bring-up's PSRAM-stacked caller.
|
|
329
|
+
static TaskHandle_t s_worker0 = nullptr;
|
|
330
|
+
static TaskHandle_t s_worker1 = nullptr;
|
|
331
|
+
xTaskCreatePinnedToCoreWithCaps(tileWorker, "tile-loader0", 20480, nullptr, 5, &s_worker0, 1,
|
|
332
|
+
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
|
333
|
+
xTaskCreatePinnedToCoreWithCaps(tileWorker, "tile-loader1", 20480, reinterpret_cast<void *>(1), 5,
|
|
334
|
+
&s_worker1, 1, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
#elif defined(__EMSCRIPTEN__)
|
|
338
|
+
|
|
339
|
+
// Web sim / tests: no worker — process synchronously at request() time. The
|
|
340
|
+
// web fetch bridge is itself async-bridged, and tests use canned responses,
|
|
341
|
+
// so blocking here is acceptable.
|
|
342
|
+
void ensureWorker() {}
|
|
343
|
+
|
|
344
|
+
#else
|
|
345
|
+
|
|
346
|
+
// Desktop targets (macOS, raspberry-pi-os): real blocking network backends
|
|
347
|
+
// (libcurl), so the ESP worker model applies — a synchronous fetch at
|
|
348
|
+
// request() time would stall the frame thread ~250ms per tile (seconds per
|
|
349
|
+
// settled pan, a beachball on macOS). Same two-worker sizing as ESP: tile
|
|
350
|
+
// fetches are network-latency-bound, two in flight nearly halve the fill
|
|
351
|
+
// time of an uncached screen, and two is the polite per-client cap for OSM.
|
|
352
|
+
// ImageStore decode is worker-safe by design (slot reserved under its mutex,
|
|
353
|
+
// filled unlocked, id invisible until poll() delivers it).
|
|
354
|
+
void tileWorker()
|
|
355
|
+
{
|
|
356
|
+
while (true) {
|
|
357
|
+
TileJob job;
|
|
358
|
+
bool haveJob = false;
|
|
359
|
+
{
|
|
360
|
+
std::lock_guard<std::mutex> lock(g_tiles_mutex);
|
|
361
|
+
if (!g_jobs.empty()) {
|
|
362
|
+
job = g_jobs.front();
|
|
363
|
+
g_jobs.pop_front();
|
|
364
|
+
haveJob = true;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (!haveJob) {
|
|
368
|
+
std::this_thread::sleep_for(std::chrono::milliseconds(15));
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
processJob(job);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
void ensureWorker()
|
|
376
|
+
{
|
|
377
|
+
static bool started = false;
|
|
378
|
+
if (started) return;
|
|
379
|
+
started = true;
|
|
380
|
+
std::thread(tileWorker).detach();
|
|
381
|
+
std::thread(tileWorker).detach();
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
#endif
|
|
385
|
+
|
|
386
|
+
} // namespace
|
|
387
|
+
|
|
388
|
+
void TileLoaderService::request(const std::string &key,
|
|
389
|
+
double num,
|
|
390
|
+
const std::string &path,
|
|
391
|
+
const std::string &url,
|
|
392
|
+
bool opaque,
|
|
393
|
+
bool rotate90) const {
|
|
394
|
+
{
|
|
395
|
+
std::lock_guard<std::mutex> lock(g_tiles_mutex);
|
|
396
|
+
if (g_pending.count(key)) return;
|
|
397
|
+
// Also dedup against DELIVERED-BUT-UNDRAINED results: a request pass that
|
|
398
|
+
// lands between a worker's deliver() and the app's next-frame drain used
|
|
399
|
+
// to re-request the tile — and immediately SD-hit the file the network
|
|
400
|
+
// load had just persisted, loading nearly every downloaded tile TWICE.
|
|
401
|
+
for (const TileResult &r : g_results) {
|
|
402
|
+
if (r.key == key) return;
|
|
403
|
+
}
|
|
404
|
+
g_pending.insert(key);
|
|
405
|
+
g_jobs.push_back(TileJob{key, num, path, url, opaque, rotate90, nowUs()});
|
|
406
|
+
}
|
|
407
|
+
ensureWorker();
|
|
408
|
+
#if !defined(ESP_PLATFORM) && defined(__EMSCRIPTEN__)
|
|
409
|
+
// Synchronous fallback: drain the job we just queued.
|
|
410
|
+
TileJob job;
|
|
411
|
+
bool haveJob = false;
|
|
412
|
+
{
|
|
413
|
+
std::lock_guard<std::mutex> lock(g_tiles_mutex);
|
|
414
|
+
if (!g_jobs.empty()) {
|
|
415
|
+
job = g_jobs.front();
|
|
416
|
+
g_jobs.pop_front();
|
|
417
|
+
haveJob = true;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (haveJob) processJob(job);
|
|
421
|
+
#endif
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
void TileLoaderService::setNetworkAllowed(bool allowed) const {
|
|
425
|
+
g_network_allowed = allowed;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
void TileLoaderService::pruneLevels(const std::string &keepCsv) const {
|
|
429
|
+
g_prune_keep_csv = keepCsv;
|
|
430
|
+
g_prune_pending = true;
|
|
431
|
+
ensureWorker();
|
|
432
|
+
#ifndef ESP_PLATFORM
|
|
433
|
+
// Synchronous on host builds (tests/sim) — prune immediately.
|
|
434
|
+
pruneTileLevels("/sdcard/tiles", keepCsv);
|
|
435
|
+
g_prune_pending = false;
|
|
436
|
+
#endif
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
void TileLoaderService::purgeQueued() const {
|
|
440
|
+
// Drop every job not yet picked up by a worker, releasing their keys for
|
|
441
|
+
// re-request. The app calls this before re-queuing the CURRENT visible set,
|
|
442
|
+
// so tiles from zoom levels you already pinched past never hold the queue
|
|
443
|
+
// hostage (each stale fetch is ~0.5-1s — a few pinches used to pile up
|
|
444
|
+
// minutes of backlog behind which the visible tiles starved).
|
|
445
|
+
std::lock_guard<std::mutex> lock(g_tiles_mutex);
|
|
446
|
+
for (const TileJob &job : g_jobs) g_pending.erase(job.key);
|
|
447
|
+
g_jobs.clear();
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
bool TileLoaderService::poll() const {
|
|
451
|
+
std::lock_guard<std::mutex> lock(g_tiles_mutex);
|
|
452
|
+
if (g_results.empty()) return false;
|
|
453
|
+
g_current = g_results.front();
|
|
454
|
+
g_results.pop_front();
|
|
455
|
+
return true;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
std::string TileLoaderService::key() const {
|
|
459
|
+
std::lock_guard<std::mutex> lock(g_tiles_mutex);
|
|
460
|
+
return g_current.key;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
double TileLoaderService::keyNum() const {
|
|
464
|
+
std::lock_guard<std::mutex> lock(g_tiles_mutex);
|
|
465
|
+
return g_current.num;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
double TileLoaderService::imageId() const {
|
|
469
|
+
std::lock_guard<std::mutex> lock(g_tiles_mutex);
|
|
470
|
+
return static_cast<double>(g_current.imageId);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
double TileLoaderService::status() const {
|
|
474
|
+
std::lock_guard<std::mutex> lock(g_tiles_mutex);
|
|
475
|
+
return static_cast<double>(g_current.status);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
} // namespace gea::host
|