@geastack/targets 0.1.108 → 0.1.109
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/build-config.json +47 -1
- package/package.json +5 -5
- package/targets/esp32/chip_bindings/audio/ns4168.cpp +326 -0
- package/targets/esp32/chip_bindings/displays/rgb_panel.cpp +2485 -105
- package/targets/esp32/chip_bindings/expanders/elecrow_board_control.cpp +169 -0
- package/targets/esp32/chip_bindings/expanders/io_expander.h +4 -0
- package/targets/esp32/chip_bindings/rtc/bm8563.cpp +129 -0
- package/targets/esp32/services/device_control.cpp +70 -6
- package/targets/esp32/services/heap.cpp +25 -1
- package/targets/esp32-s3-elecrow-crowpanel-advance-7.0.json +182 -0
- package/targets/esp32-s3-touch-amoled-1.8/main/CMakeLists.txt +3 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/CMakeLists.txt +31 -2
- package/targets/esp32-s3-touch-amoled-2.06/main/app_main.cpp +11 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/sdcard_mount.cpp +56 -0
- package/targets.json +10 -0
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
// CH422G); a panel reset line, when the board has one, is likewise an expander
|
|
9
9
|
// pin. The raster/present pipeline below is shared with the Elecrow rotary's
|
|
10
10
|
// ST7701 target, which is where it was measured and tuned.
|
|
11
|
+
#include "esp_cpu.h"
|
|
11
12
|
#include "display.h"
|
|
12
13
|
|
|
13
14
|
#include "board.h"
|
|
@@ -28,10 +29,49 @@
|
|
|
28
29
|
#define GEA_EMBEDDED_DISPLAY_RGB_STRIP_RASTER 1
|
|
29
30
|
#endif
|
|
30
31
|
|
|
32
|
+
// Bounce buffers vs direct PSRAM scanout: with bounce, a core-0 ISR memcpys
|
|
33
|
+
// every scanline batch from PSRAM into internal SRAM -- a standing CPU tax on
|
|
34
|
+
// the ISR's core. With 0 the GDMA reads the PSRAM framebuffer directly (no CPU
|
|
35
|
+
// in the scanout path); the risk is scanout underrun (visible drift) when CPU
|
|
36
|
+
// PSRAM traffic starves the DMA.
|
|
37
|
+
#ifndef GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
|
|
38
|
+
#define GEA_EMBEDDED_DISPLAY_RGB_BOUNCE 1
|
|
39
|
+
#endif
|
|
40
|
+
|
|
41
|
+
// Tear-free present: two driver-owned scanout framebuffers plus the persistent
|
|
42
|
+
// draw buffer. A frame is published into whichever scanout buffer the panel is
|
|
43
|
+
// NOT reading, then handed to the driver, which switches to it at the next frame
|
|
44
|
+
// boundary. With a single scanout buffer the publish copy wrote rows the panel
|
|
45
|
+
// was scanning at that moment, so every animated frame tore (and a ~17ms copy at
|
|
46
|
+
// 44Hz refresh spans most of a scan). 0 restores the single-buffer path.
|
|
47
|
+
// Core for the panel's interrupts (the bounce-buffer copy ISR); -1 = the core
|
|
48
|
+
// that initialises the display.
|
|
49
|
+
// Core 1: the renderer runs on core 0, and the bounce copy costs it ~10% there.
|
|
50
|
+
// With three scanout buffers, svg-charts on the CrowPanel 7 measured 26.6 fps
|
|
51
|
+
// with the ISR on core 1 against 25.0 on core 0. (With two buffers, core 1 was
|
|
52
|
+
// worse: a swap from the other core waits two frame ends, so frames snapped to
|
|
53
|
+
// panel-rate / 3.)
|
|
54
|
+
#ifndef GEA_EMBEDDED_DISPLAY_RGB_ISR_CORE
|
|
55
|
+
#define GEA_EMBEDDED_DISPLAY_RGB_ISR_CORE 1
|
|
56
|
+
#endif
|
|
57
|
+
|
|
58
|
+
#ifndef GEA_EMBEDDED_DISPLAY_RGB_SWAP_FBS
|
|
59
|
+
#define GEA_EMBEDDED_DISPLAY_RGB_SWAP_FBS 1
|
|
60
|
+
#endif
|
|
61
|
+
// Scanout buffers in swap mode (2 or 3). With three, the renderer draws into a
|
|
62
|
+
// buffer the panel released one frame earlier, so a publish never waits for the
|
|
63
|
+
// second vsync after the previous one: the frame rate follows the frame work
|
|
64
|
+
// instead of snapping to panel-rate / 2.
|
|
65
|
+
// The board picks the count (chips.display.scanFramebuffers); a build can still
|
|
66
|
+
// force it with this macro.
|
|
67
|
+
|
|
31
68
|
#include "driver/gpio.h"
|
|
32
69
|
#include "driver/ledc.h"
|
|
33
70
|
#include "esp_cache.h"
|
|
71
|
+
#include "esp_private/esp_cache_private.h"
|
|
72
|
+
#include "hal/cache_hal.h"
|
|
34
73
|
#include "esp_heap_caps.h"
|
|
74
|
+
#include "esp_ipc.h"
|
|
35
75
|
#include "esp_lcd_panel_ops.h"
|
|
36
76
|
#include "esp_lcd_panel_rgb.h"
|
|
37
77
|
#include "esp_log.h"
|
|
@@ -39,8 +79,14 @@
|
|
|
39
79
|
#include "esp_timer.h"
|
|
40
80
|
#include "freertos/FreeRTOS.h"
|
|
41
81
|
#include "freertos/task.h"
|
|
82
|
+
#include "freertos/semphr.h"
|
|
42
83
|
#include "freertos/idf_additions.h"
|
|
43
84
|
#include "soc/clk_tree_defs.h"
|
|
85
|
+
#if CONFIG_IDF_TARGET_ESP32S3
|
|
86
|
+
#include "esp32s3/rom/cache.h"
|
|
87
|
+
#include "soc/gdma_struct.h"
|
|
88
|
+
#include "esp_async_memcpy.h"
|
|
89
|
+
#endif
|
|
44
90
|
|
|
45
91
|
#include <algorithm>
|
|
46
92
|
#include <atomic>
|
|
@@ -66,20 +112,46 @@ constexpr int kPixelClockHz = GEA_EMBEDDED_DISPLAY_RGB_PCLK_HZ;
|
|
|
66
112
|
static_assert(kPixelClockHz > 0, "RGB pixel clock must be positive");
|
|
67
113
|
#else
|
|
68
114
|
constexpr int kPixelClockHz = gea::platform::board::display.pclkHz;
|
|
115
|
+
#endif
|
|
116
|
+
// Bounce buffers are refilled from PSRAM while the other core renders into
|
|
117
|
+
// PSRAM too; a refill that overruns its window lets the LCD DMA send a half
|
|
118
|
+
// old buffer (a tear band). Larger buffers average the render's PSRAM bursts
|
|
119
|
+
// over a longer window: 20-row refills peaked at 1.29 ms of a 1.09 ms window
|
|
120
|
+
// on a busy screen. 40 rows (128 KB) does not fit in internal RAM by default;
|
|
121
|
+
// a board whose screens need it sets chips.display.bounceRows (see
|
|
122
|
+
// bounceBufferRows), taking that RAM from the strip staging allocated after.
|
|
123
|
+
#ifndef GEA_EMBEDDED_DISPLAY_RGB_BOUNCE_MAX_ROWS
|
|
124
|
+
#define GEA_EMBEDDED_DISPLAY_RGB_BOUNCE_MAX_ROWS 30
|
|
69
125
|
#endif
|
|
70
126
|
// Bounce rows: the largest candidate that divides the panel height, so the
|
|
71
127
|
// framebuffer splits into whole bounce buffers whatever the geometry is.
|
|
72
128
|
constexpr int pickBounceRows(int height)
|
|
73
129
|
{
|
|
74
|
-
constexpr int candidates[] = {20, 16, 12, 10, 8, 6, 5, 4, 3, 2, 1};
|
|
130
|
+
constexpr int candidates[] = {GEA_EMBEDDED_DISPLAY_RGB_BOUNCE_MAX_ROWS, 48, 40, 30, 24, 20, 16, 12, 10, 8, 6, 5, 4, 3, 2, 1};
|
|
75
131
|
for (int rows : candidates) {
|
|
76
|
-
if (height % rows == 0) return rows;
|
|
132
|
+
if (rows <= GEA_EMBEDDED_DISPLAY_RGB_BOUNCE_MAX_ROWS && rows > 0 && height % rows == 0) return rows;
|
|
77
133
|
}
|
|
78
134
|
return 1;
|
|
79
135
|
}
|
|
80
136
|
constexpr int kRgbBounceRows = pickBounceRows(kHeight);
|
|
81
137
|
constexpr std::size_t kRgbBounceBufferPixels = static_cast<std::size_t>(kWidth) * kRgbBounceRows;
|
|
82
138
|
static_assert(kPixelCount % kRgbBounceBufferPixels == 0, "RGB bounce buffer must divide the framebuffer evenly");
|
|
139
|
+
// The board's bounce depth when it names one that divides the panel height
|
|
140
|
+
// (the driver needs whole buffers per frame), else the default above. Not
|
|
141
|
+
// a free win: on the CrowPanel 7.0 a deeper bounce pair left no internal
|
|
142
|
+
// RAM for the strip staging, and rasterizing straight into PSRAM made the
|
|
143
|
+
// picture flicker far worse than the overruns it was meant to absorb.
|
|
144
|
+
int bounceBufferRows()
|
|
145
|
+
{
|
|
146
|
+
#ifdef GEA_EMBEDDED_RGB_BOUNCE_ROWS
|
|
147
|
+
// An app may size them itself (gea.defines), e.g. to free internal RAM
|
|
148
|
+
// for the copy-back pipeline GEA_EMBEDDED_RGB_COPYBACK_DMA uses.
|
|
149
|
+
const int rows = GEA_EMBEDDED_RGB_BOUNCE_ROWS;
|
|
150
|
+
#else
|
|
151
|
+
const int rows = gea::platform::board::display.bounceRows;
|
|
152
|
+
#endif
|
|
153
|
+
return rows > 0 && kHeight % rows == 0 ? rows : kRgbBounceRows;
|
|
154
|
+
}
|
|
83
155
|
constexpr ledc_mode_t kBacklightLedcMode = LEDC_LOW_SPEED_MODE;
|
|
84
156
|
constexpr ledc_channel_t kBacklightLedcChannel = LEDC_CHANNEL_0;
|
|
85
157
|
constexpr ledc_timer_t kBacklightLedcTimer = LEDC_TIMER_0;
|
|
@@ -96,6 +168,24 @@ namespace present = gea::framework::display_present;
|
|
|
96
168
|
constexpr int kStripRows = 24;
|
|
97
169
|
constexpr int kStripBufferRows = kStripRows * 2;
|
|
98
170
|
constexpr std::size_t kStripBufferPixels = static_cast<std::size_t>(kWidth) * kStripBufferRows;
|
|
171
|
+
// Rows per strip half actually allocated: kStripRows when internal RAM allows,
|
|
172
|
+
// fewer on an app whose static data already took part of it (0 = no strips).
|
|
173
|
+
// A shorter strip costs a few more raster passes; no strip at all puts every
|
|
174
|
+
// raster write on the PSRAM bus the scanout DMA shares, and the panel slips.
|
|
175
|
+
constexpr int kStripRowChoices[] = {kStripRows, 16, 12, 8};
|
|
176
|
+
// Opt-in GDMA copy-back pipeline (see kCopyBackDma).
|
|
177
|
+
#ifndef GEA_EMBEDDED_RGB_COPYBACK_DMA
|
|
178
|
+
#define GEA_EMBEDDED_RGB_COPYBACK_DMA 0
|
|
179
|
+
#endif
|
|
180
|
+
// One half of three 10-row pipeline slots (see allocStripBuffer), and the
|
|
181
|
+
// internal RAM it must leave free beyond the caller's headroom (GDMA link
|
|
182
|
+
// lists, task stacks).
|
|
183
|
+
constexpr int kTripleSlotRows = 30;
|
|
184
|
+
constexpr std::size_t kTripleSlotReserve = 12 * 1024;
|
|
185
|
+
int g_stripRows = 0;
|
|
186
|
+
// 2: a strip per core (the fused present double-buffers across both). 1: internal
|
|
187
|
+
// RAM held only one, enough for the main core to stage its replay.
|
|
188
|
+
int g_stripHalves = 0;
|
|
99
189
|
|
|
100
190
|
gea::framework::graphics::Canvas g_canvas;
|
|
101
191
|
// Band canvas for the renderer's coarse 2-core replay split: bound to the SAME
|
|
@@ -112,7 +202,26 @@ bool onWorkerCore()
|
|
|
112
202
|
return g_renderCoreId >= 0 && static_cast<int>(xPortGetCoreID()) != g_renderCoreId;
|
|
113
203
|
}
|
|
114
204
|
|
|
205
|
+
using gea::platform::display::DisplayFlushRect;
|
|
206
|
+
bool g_swapFbs = false;
|
|
207
|
+
bool g_backReady = true;
|
|
208
|
+
void ensureBackReady(const DisplayFlushRect *skip = nullptr, int skipCount = 0);
|
|
209
|
+
|
|
210
|
+
// Every Display draw entry point and Display::canvas() come through here, so this
|
|
211
|
+
// is where the back buffer is made safe to draw into after a swap (a flag test
|
|
212
|
+
// on the fast path). The worker core's band replay is covered by the main core
|
|
213
|
+
// readying the buffer before it submits the band.
|
|
115
214
|
gea::framework::graphics::Canvas &replayCanvas()
|
|
215
|
+
{
|
|
216
|
+
if (onWorkerCore()) return g_workerCanvas;
|
|
217
|
+
if (!g_backReady) ensureBackReady();
|
|
218
|
+
return g_canvas;
|
|
219
|
+
}
|
|
220
|
+
// The same canvas without readying the back buffer, for state that touches no
|
|
221
|
+
// pixels (clip, alpha, rebinding). Readying early would patch the whole stale
|
|
222
|
+
// area before the fused flush can say which rects it repaints, and would block
|
|
223
|
+
// on the panel releasing the buffer right after every swap.
|
|
224
|
+
gea::framework::graphics::Canvas &stateCanvas()
|
|
116
225
|
{
|
|
117
226
|
return onWorkerCore() ? g_workerCanvas : g_canvas;
|
|
118
227
|
}
|
|
@@ -120,6 +229,55 @@ esp_lcd_panel_handle_t g_panel = nullptr;
|
|
|
120
229
|
std::uint16_t *g_framebuffer = nullptr;
|
|
121
230
|
std::uint16_t *g_drawFramebuffer = nullptr;
|
|
122
231
|
std::uint16_t *g_stripBuffer = nullptr;
|
|
232
|
+
|
|
233
|
+
std::size_t stripBufferPixels() { return static_cast<std::size_t>(kWidth) * g_stripRows * g_stripHalves; }
|
|
234
|
+
|
|
235
|
+
// Allocates the two strip halves at the tallest height internal RAM holds while
|
|
236
|
+
// leaving `headroom` bytes free (radios reserve some after boot).
|
|
237
|
+
void allocStripBuffer(std::size_t headroom)
|
|
238
|
+
{
|
|
239
|
+
g_stripRows = 0;
|
|
240
|
+
g_stripHalves = 0;
|
|
241
|
+
// Two halves when they fit at full height; otherwise the tallest strip,
|
|
242
|
+
// one half before two: the main core is the one that stages, and a staged
|
|
243
|
+
// repaint re-walks the display list once per strip (a board whose bounce
|
|
244
|
+
// buffers take most of the internal RAM -- the CrowPanel 7.0 -- has room
|
|
245
|
+
// for a single 16-row strip or two 8-row ones).
|
|
246
|
+
#if GEA_EMBEDDED_RGB_COPYBACK_DMA
|
|
247
|
+
// A single taller half cut into three slots beats a second half of the
|
|
248
|
+
// default height when the copy-back runs on GDMA (copyDmaReady): every
|
|
249
|
+
// strip re-walks the display list and re-sets-up each primitive it
|
|
250
|
+
// crosses, so on a full-screen 3D underlay the strip count is the cost.
|
|
251
|
+
// Tried only where two default halves do not fit, so boards that have
|
|
252
|
+
// them keep them.
|
|
253
|
+
const auto tryAlloc = [&](int rows, int halves) {
|
|
254
|
+
const std::size_t bytes = static_cast<std::size_t>(kWidth) * rows * halves * sizeof(std::uint16_t);
|
|
255
|
+
if (heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) < bytes ||
|
|
256
|
+
heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) < bytes + headroom + kTripleSlotReserve)
|
|
257
|
+
return false;
|
|
258
|
+
g_stripBuffer = static_cast<std::uint16_t *>(heap_caps_aligned_alloc(16, bytes, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT));
|
|
259
|
+
if (!g_stripBuffer) return false;
|
|
260
|
+
g_stripRows = rows;
|
|
261
|
+
g_stripHalves = halves;
|
|
262
|
+
return true;
|
|
263
|
+
};
|
|
264
|
+
if (tryAlloc(kStripRows, 2) || tryAlloc(kTripleSlotRows, 1)) return;
|
|
265
|
+
#endif
|
|
266
|
+
for (int rows : kStripRowChoices) {
|
|
267
|
+
for (int halves = 2; halves >= 1; --halves) {
|
|
268
|
+
const std::size_t bytes = static_cast<std::size_t>(kWidth) * rows * halves * sizeof(std::uint16_t);
|
|
269
|
+
if (heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) < bytes ||
|
|
270
|
+
heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) < bytes + headroom)
|
|
271
|
+
continue;
|
|
272
|
+
g_stripBuffer = static_cast<std::uint16_t *>(heap_caps_aligned_alloc(16, bytes, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT));
|
|
273
|
+
if (g_stripBuffer) {
|
|
274
|
+
g_stripRows = rows;
|
|
275
|
+
g_stripHalves = halves;
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
123
281
|
std::atomic<std::size_t> g_internalReservation{0};
|
|
124
282
|
std::size_t g_appliedInternalReservation = 0;
|
|
125
283
|
std::size_t g_internalCacheHeadroom = 0;
|
|
@@ -144,17 +302,29 @@ std::size_t g_internalCacheHeadroom = 0;
|
|
|
144
302
|
ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
|
|
145
303
|
}
|
|
146
304
|
|
|
147
|
-
|
|
305
|
+
// Write a scanout region back from the data cache so the LCD's GDMA sees it.
|
|
306
|
+
// Only direct-PSRAM scanout needs this: in bounce-buffer mode the driver's ISR
|
|
307
|
+
// memcpys the framebuffer through the same (shared, single) S3 data cache, so
|
|
308
|
+
// dirty lines are already coherent for it -- IDF's own draw_bitmap skips the
|
|
309
|
+
// sync in that mode for the same reason. Skipping it saves a cache walk over
|
|
310
|
+
// every published row.
|
|
311
|
+
void cacheWritebackRect(std::uint16_t *buffer, int x0, int y0, int x1, int y1)
|
|
148
312
|
{
|
|
149
|
-
|
|
313
|
+
#if GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
|
|
314
|
+
(void)buffer; (void)x0; (void)y0; (void)x1; (void)y1;
|
|
315
|
+
#else
|
|
316
|
+
if (!buffer || !clipRect(&x0, &y0, &x1, &y1)) return;
|
|
150
317
|
const std::size_t begin = static_cast<std::size_t>(y0) * kWidth + static_cast<std::size_t>(x0);
|
|
151
318
|
const std::size_t end = static_cast<std::size_t>(y1) * kWidth + static_cast<std::size_t>(x1) + 1;
|
|
152
319
|
if (end <= begin || end > kPixelCount) return;
|
|
153
|
-
esp_cache_msync(&
|
|
320
|
+
esp_cache_msync(&buffer[begin],
|
|
154
321
|
(end - begin) * sizeof(std::uint16_t),
|
|
155
322
|
ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
|
|
323
|
+
#endif
|
|
156
324
|
}
|
|
157
325
|
|
|
326
|
+
void cacheWritebackRect(int x0, int y0, int x1, int y1) { cacheWritebackRect(g_framebuffer, x0, y0, x1, y1); }
|
|
327
|
+
|
|
158
328
|
void addFlushStats(int64_t totalUs, int pixelCount)
|
|
159
329
|
{
|
|
160
330
|
g_stats.totalUs += totalUs;
|
|
@@ -168,6 +338,960 @@ void addFlushStageStats(int64_t copyUs, int64_t cacheUs)
|
|
|
168
338
|
g_stats.completeWaitUs += cacheUs;
|
|
169
339
|
}
|
|
170
340
|
|
|
341
|
+
// ── Swapped scanout buffers (direct mode) ────────────────────────────────────
|
|
342
|
+
// Two driver-owned scanout buffers and no separate draw buffer: the renderer
|
|
343
|
+
// draws straight into the back buffer (g_drawFramebuffer points at it), and a
|
|
344
|
+
// publish hands that buffer to the panel, which switches at the next frame wrap.
|
|
345
|
+
// g_scanFb[g_frontIndex] is the buffer most recently handed over (g_framebuffer;
|
|
346
|
+
// screenshots read it).
|
|
347
|
+
//
|
|
348
|
+
// After a swap the new back buffer lags by whatever the frame just published
|
|
349
|
+
// changed (its stale list). Before anything draws into it, ensureBackReady()
|
|
350
|
+
// waits for the panel to release it and patches those regions in from the front
|
|
351
|
+
// -- the previous frame's dirty rects only, never the whole frame. The fused
|
|
352
|
+
// raster path (flushRectsRasterized) knows which rects it is about to repaint
|
|
353
|
+
// completely, so it patches only the stale area OUTSIDE them: when an animation
|
|
354
|
+
// redraws the same region every frame, almost nothing is copied and each dirty
|
|
355
|
+
// pixel reaches PSRAM exactly once, from an internal-RAM strip.
|
|
356
|
+
struct StaleRect {
|
|
357
|
+
int x0, y0, x1, y1;
|
|
358
|
+
};
|
|
359
|
+
// Many small animations (64 bouncing balls: an old and a new box each) must stay
|
|
360
|
+
// separate rects: with 8, the list folded into one screen-sized box and every
|
|
361
|
+
// frame copied ~300k PSRAM pixels front to back before drawing.
|
|
362
|
+
constexpr int kMaxStaleRects = 160;
|
|
363
|
+
struct StaleList {
|
|
364
|
+
StaleRect rects[kMaxStaleRects];
|
|
365
|
+
int count = 0;
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
constexpr int kMaxScanFbs = 3;
|
|
369
|
+
std::uint16_t *g_scanFb[kMaxScanFbs] = {nullptr, nullptr, nullptr};
|
|
370
|
+
int g_scanFbCount = 1;
|
|
371
|
+
int g_frontIndex = 0;
|
|
372
|
+
// The buffer the renderer draws into: the one after the front in rotation.
|
|
373
|
+
int g_drawIndex = 1;
|
|
374
|
+
StaleList g_stale[kMaxScanFbs];
|
|
375
|
+
// New pixels reached the back buffer since the last swap.
|
|
376
|
+
bool g_publishPending = false;
|
|
377
|
+
// g_backReady (declared above replayCanvas): the back buffer is released by the
|
|
378
|
+
// panel and patched up to date, so drawing into it is safe. Cleared by every swap.
|
|
379
|
+
// Serializes readying, in case a draw on another task races the frame task.
|
|
380
|
+
SemaphoreHandle_t g_readyMutex = nullptr;
|
|
381
|
+
// A sideways scroll copy is running on the row worker (gea_display_scroll_rect_begin).
|
|
382
|
+
bool g_scrollCopyInFlight = false;
|
|
383
|
+
// The viewport a finished scroll copy wrote back row by row this frame (empty:
|
|
384
|
+
// x0 > x1). publishSwap need not write back the frame's draws inside it.
|
|
385
|
+
DisplayFlushRect g_cleanRect = {0, 0, -1, -1};
|
|
386
|
+
// An unstaged replay drew while the scroll copy ran (gea_display_note_direct_draw).
|
|
387
|
+
bool g_directDrawDuringCopy = false;
|
|
388
|
+
// Every pixel this frame drew went out through scanoutRowCopy,
|
|
389
|
+
// already written back from the cache (a fused frame with no direct draws).
|
|
390
|
+
bool g_frameWrittenBack = false;
|
|
391
|
+
// Per buffer, per screen row: the signature (gea_display_scroll_rect_rows) of
|
|
392
|
+
// what the last frame drawn into that buffer left in its scroll viewport, valid
|
|
393
|
+
// only while every frame since was a sideways scroll copy of the same viewport.
|
|
394
|
+
// A row whose new signature matches the back buffer's already holds the
|
|
395
|
+
// shifted picture, so the copy skips it.
|
|
396
|
+
std::uint32_t *g_rowSig[kMaxScanFbs] = {nullptr, nullptr, nullptr};
|
|
397
|
+
DisplayFlushRect g_rowSigRect[kMaxScanFbs] = {};
|
|
398
|
+
bool g_rowSigValid[kMaxScanFbs] = {false, false, false};
|
|
399
|
+
// The coming copy's signatures (screen rows from g_nextSigY0), and whether the
|
|
400
|
+
// frame being drawn is a sideways scroll copy.
|
|
401
|
+
const std::uint32_t *g_nextSig = nullptr;
|
|
402
|
+
int g_nextSigY0 = 0, g_nextSigRows = 0;
|
|
403
|
+
bool g_frameIsScrollCopy = false;
|
|
404
|
+
std::uint8_t g_skipRow[kHeight];
|
|
405
|
+
// Counted by the driver's frame-complete callback, which runs right after the
|
|
406
|
+
// bounce ISR has latched the driver's current buffer for the next frame.
|
|
407
|
+
volatile std::uint32_t g_frameSeq = 0;
|
|
408
|
+
// DIAG: panel VSYNCs. In steady state there is exactly one frame-buffer wrap per
|
|
409
|
+
// VSYNC; a difference means the bounce ISR fell behind and the driver had to
|
|
410
|
+
// restart the scan (the visible "shift").
|
|
411
|
+
volatile std::uint32_t g_vsyncSeq = 0;
|
|
412
|
+
std::uint32_t g_diagVsync0 = 0, g_diagFrame0 = 0;
|
|
413
|
+
// Buffer i is released by the panel once g_frameSeq reaches g_freeSeq[i].
|
|
414
|
+
std::uint32_t g_freeSeq[kMaxScanFbs] = {0, 0, 0};
|
|
415
|
+
int g_panelIsrCore = -1;
|
|
416
|
+
// Set while the bounce ISR refills a buffer (see scanoutRowCopy).
|
|
417
|
+
DRAM_ATTR volatile bool g_bounceFilling = false;
|
|
418
|
+
// Refill collisions, read from the hardware: the GDMA channel feeding the
|
|
419
|
+
// panel reports the descriptor it is on, and so the buffer it is sending. If
|
|
420
|
+
// that is the buffer the ISR is refilling, the panel shows some of its old
|
|
421
|
+
// rows (thin glitch lines): at the start of a fill it means the fills are a
|
|
422
|
+
// buffer out of step with the DMA, at the end that the fill ran late.
|
|
423
|
+
// GEADEV PANEL reads the counts.
|
|
424
|
+
struct RefillStats {
|
|
425
|
+
// [0]: the channel's current descriptor; [1]: the one before it (the
|
|
426
|
+
// registers' meaning is loosely documented, so both are read).
|
|
427
|
+
std::uint32_t fills, atStart[2], atEnd[2], unknown, corrected, fallback;
|
|
428
|
+
std::uint16_t atStartPos[32], atEndPos[32]; // per bounce position, [0] reading
|
|
429
|
+
};
|
|
430
|
+
DRAM_ATTR RefillStats g_refill = {};
|
|
431
|
+
DRAM_ATTR int g_panelDmaChannel = -1;
|
|
432
|
+
|
|
433
|
+
// A private semaphore, not a task notification: the frame task's notification
|
|
434
|
+
// slot belongs to whoever else wakes it, and there is only one slot.
|
|
435
|
+
SemaphoreHandle_t g_frameSem = nullptr;
|
|
436
|
+
volatile bool g_swapWaiting = false;
|
|
437
|
+
portMUX_TYPE g_swapMux = portMUX_INITIALIZER_UNLOCKED;
|
|
438
|
+
// DIAG: publish cost decomposition, printed every kSwapDiagFrames swaps.
|
|
439
|
+
constexpr int kSwapDiagFrames = 100;
|
|
440
|
+
struct SwapDiag {
|
|
441
|
+
std::int64_t copyUs = 0, waitUs = 0, totalUs = 0, rasterUs = 0;
|
|
442
|
+
std::int64_t px = 0, patchPx = 0, preparePx = 0;
|
|
443
|
+
int prepares = 0;
|
|
444
|
+
int frames = 0, waits = 0, timeouts = 0, unready = 0, late = 0, regions = 0, rings = 0, fallbacks = 0;
|
|
445
|
+
int paths[4] = {0, 0, 0, 0}; // flush, flushRects, rasterized, strip
|
|
446
|
+
std::int64_t windowStartUs = 0;
|
|
447
|
+
} g_swapDiag;
|
|
448
|
+
std::int64_t g_lastBlockUs = 0;
|
|
449
|
+
|
|
450
|
+
// ── Ring scroll (bounce mode) ────────────────────────────────────────────────
|
|
451
|
+
// The S3's LCD peripheral has no scroll register, but in bounce mode the CPU
|
|
452
|
+
// fills every scanline batch, and this file does that fill itself (the driver
|
|
453
|
+
// owns no framebuffer: `no_fb` + on_bounce_empty). So a vertical scroll of a
|
|
454
|
+
// full-width region is the Canvas's ring mapping (Canvas::rowToPhysical) plus
|
|
455
|
+
// a per-buffer offset the fill applies: no pixel moves, only the rows the
|
|
456
|
+
// scroll exposes are drawn.
|
|
457
|
+
//
|
|
458
|
+
// Each scanout buffer carries its own mapping, because the buffers hold
|
|
459
|
+
// different frames. The draw buffer's mapping is the canvas's; the others lag
|
|
460
|
+
// by the rotations scrolled since they were last current (g_pendingRot), and
|
|
461
|
+
// their stale rects are shifted with every scroll so they stay in the current
|
|
462
|
+
// frame's coordinates. A buffer that is fully stale adopts the front's mapping
|
|
463
|
+
// when it is readied, and is then patched in full.
|
|
464
|
+
struct ScanMap {
|
|
465
|
+
int y = 0, h = 0, off = 0; // region rows [y, y+h); off in [0, h)
|
|
466
|
+
};
|
|
467
|
+
ScanMap g_map[kMaxScanFbs];
|
|
468
|
+
int g_pendingRot[kMaxScanFbs] = {0, 0, 0};
|
|
469
|
+
bool g_mapReset[kMaxScanFbs] = {false, false, false};
|
|
470
|
+
int g_regionY = 0, g_regionH = 0;
|
|
471
|
+
bool g_ringScroll = false;
|
|
472
|
+
// The buffer the panel is scanning and the one it switches to at the next frame
|
|
473
|
+
// wrap, with the scanned buffer's mapping latched at that wrap.
|
|
474
|
+
volatile int g_scanIndex = 0;
|
|
475
|
+
volatile int g_pendingScan = 0;
|
|
476
|
+
ScanMap g_scanMap;
|
|
477
|
+
|
|
478
|
+
__attribute__((always_inline)) inline int mapRow(const ScanMap &m, int y)
|
|
479
|
+
{
|
|
480
|
+
if (m.h <= 0 || y < m.y || y >= m.y + m.h) return y;
|
|
481
|
+
int rel = y - m.y + m.off;
|
|
482
|
+
if (rel >= m.h) rel -= m.h;
|
|
483
|
+
return m.y + rel;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
int normOffset(int value, int h) { return h > 0 ? ((value % h) + h) % h : 0; }
|
|
487
|
+
|
|
488
|
+
std::uint16_t *drawRow(int y) { return g_drawFramebuffer + static_cast<std::size_t>(mapRow(g_map[g_drawIndex], y)) * kWidth; }
|
|
489
|
+
|
|
490
|
+
void applyCanvasMap()
|
|
491
|
+
{
|
|
492
|
+
const ScanMap &m = g_map[g_drawIndex];
|
|
493
|
+
if (g_canvas.pixels() == g_drawFramebuffer) g_canvas.setScrollRegion(m.y, m.h, m.off);
|
|
494
|
+
if (g_workerCanvas.pixels() == g_drawFramebuffer) g_workerCanvas.setScrollRegion(m.y, m.h, m.off);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// When the panel last started displaying a frame, and when the bounce ISR last
|
|
498
|
+
// latched a buffer for the next one: together they say how far down the frame in
|
|
499
|
+
// flight the panel has read (see backRowReleased).
|
|
500
|
+
volatile std::int64_t g_vsyncUs = 0;
|
|
501
|
+
volatile std::int64_t g_frameCompleteUs = 0;
|
|
502
|
+
|
|
503
|
+
// VSYNC interrupt lateness: the interval between VSYNC interrupts against
|
|
504
|
+
// the frame period (a late one is a late DMA restart).
|
|
505
|
+
DRAM_ATTR std::int32_t g_vsyncLateMaxUs = 0;
|
|
506
|
+
DRAM_ATTR std::uint32_t g_vsyncLate200 = 0;
|
|
507
|
+
// Where the DMA sits at each VSYNC: bin = buffer * 16 + descriptor index.
|
|
508
|
+
DRAM_ATTR std::uint32_t g_vsyncAt[32] = {};
|
|
509
|
+
DRAM_ATTR std::uint32_t g_vsyncRestarts = 0;
|
|
510
|
+
DRAM_ATTR std::uint32_t g_vsyncSkipped = 0;
|
|
511
|
+
DRAM_ATTR std::uint32_t g_vsyncRealigned = 0;
|
|
512
|
+
DRAM_ATTR std::uint32_t g_frameEofFills = 0; // refills from DMA EOFs since the last VSYNC
|
|
513
|
+
DRAM_ATTR std::uint32_t g_shortFrames = 0; // VSYNCs that found fewer: the driver restarts there
|
|
514
|
+
DRAM_ATTR std::uint32_t g_shortFramesLate = 0; // ... on a late interrupt
|
|
515
|
+
// The last few VSYNCs that found the DMA out of place, with their context.
|
|
516
|
+
struct SlipEvent {
|
|
517
|
+
std::uint32_t ms;
|
|
518
|
+
std::uint16_t bin;
|
|
519
|
+
std::uint16_t lateUs;
|
|
520
|
+
std::uint16_t prevLateUs;
|
|
521
|
+
std::uint8_t eofFills;
|
|
522
|
+
std::uint8_t prevEofFills;
|
|
523
|
+
};
|
|
524
|
+
DRAM_ATTR SlipEvent g_slips[6] = {};
|
|
525
|
+
DRAM_ATTR std::uint32_t g_slipCount = 0;
|
|
526
|
+
DRAM_ATTR std::uint16_t g_prevLateUs = 0;
|
|
527
|
+
DRAM_ATTR std::uint8_t g_prevEofFills = 0;
|
|
528
|
+
DRAM_ATTR std::uint32_t g_vsyncPhaseQ4 = 0;
|
|
529
|
+
// A VSYNC interrupt this late (past the hardware event, in 1/16 us) may
|
|
530
|
+
// already be inside the next frame's first lines: no restart then.
|
|
531
|
+
constexpr std::uint32_t kVsyncOnTimeQ4 = 40 * 16;
|
|
532
|
+
int panelDmaBin();
|
|
533
|
+
IRAM_ATTR bool onPanelVsync(esp_lcd_panel_handle_t panel, const esp_lcd_rgb_panel_event_data_t *, void *)
|
|
534
|
+
{
|
|
535
|
+
const std::int64_t now = esp_timer_get_time();
|
|
536
|
+
{
|
|
537
|
+
// Realign the DMA with the panel at every VSYNC that comes in time: the
|
|
538
|
+
// DMA is then still parked on buffer 0 just past its first descriptor
|
|
539
|
+
// (the FIFO's preserved pixels), waiting out the blanking. A later
|
|
540
|
+
// interrupt finds it already further in, sending the next frame, and a
|
|
541
|
+
// restart then would shift that frame sideways, so it is skipped; a
|
|
542
|
+
// slip waits for the next VSYNC in time. (The driver's own
|
|
543
|
+
// CONFIG_LCD_RGB_RESTART_IN_VSYNC restarts at every VSYNC, late or not:
|
|
544
|
+
// the screen shook while scrolling. With no restart at all a slip stayed
|
|
545
|
+
// on screen, the picture split and shifted.)
|
|
546
|
+
// The descriptor alone leaves ~140 us of the next frame unseen (the
|
|
547
|
+
// DMA moves through descriptor 1 slowly at first), so the interrupt's
|
|
548
|
+
// own lateness gates it too. VSYNC is a fixed hardware period: track its
|
|
549
|
+
// phase from the interrupts that come in time and measure this one
|
|
550
|
+
// against it.
|
|
551
|
+
// Phase in 1/16 us, 32-bit: the anchor moves every frame in time, so
|
|
552
|
+
// the distance from it stays small.
|
|
553
|
+
constexpr auto &d = gea::platform::board::display;
|
|
554
|
+
constexpr std::uint32_t kPeriodQ4 = static_cast<std::uint32_t>(
|
|
555
|
+
(static_cast<std::uint64_t>(kWidth + d.hsyncPulseWidth + d.hsyncBackPorch + d.hsyncFrontPorch) *
|
|
556
|
+
(kHeight + d.vsyncPulseWidth + d.vsyncBackPorch + d.vsyncFrontPorch) * 16000000ULL) / kPixelClockHz);
|
|
557
|
+
const std::uint32_t nowQ4 = static_cast<std::uint32_t>(now) << 4;
|
|
558
|
+
std::uint32_t lateQ4 = 0;
|
|
559
|
+
const std::uint32_t sinceQ4 = nowQ4 - g_vsyncPhaseQ4;
|
|
560
|
+
if (g_vsyncPhaseQ4 == 0 || sinceQ4 > 0x40000000u) {
|
|
561
|
+
g_vsyncPhaseQ4 = nowQ4;
|
|
562
|
+
} else {
|
|
563
|
+
const std::uint32_t n = (sinceQ4 + kPeriodQ4 / 2) / kPeriodQ4;
|
|
564
|
+
const std::uint32_t expected = g_vsyncPhaseQ4 + n * kPeriodQ4;
|
|
565
|
+
const std::int32_t late = static_cast<std::int32_t>(nowQ4 - expected);
|
|
566
|
+
if (late < 0) {
|
|
567
|
+
g_vsyncPhaseQ4 = nowQ4; // the estimate ran late: this one is the earliest seen
|
|
568
|
+
} else {
|
|
569
|
+
lateQ4 = static_cast<std::uint32_t>(late);
|
|
570
|
+
if (lateQ4 <= kVsyncOnTimeQ4) g_vsyncPhaseQ4 = expected + lateQ4 / 4; // follow drift slowly
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
const int bin = panelDmaBin();
|
|
574
|
+
if (bin >= 0 && bin < 32) g_vsyncAt[bin]++;
|
|
575
|
+
// An interrupt in time that finds the DMA anywhere but buffer 0 /
|
|
576
|
+
// descriptor 1 means the DMA itself is out of step with the panel
|
|
577
|
+
// (behind after an underrun, or ahead): that is the slip a restart
|
|
578
|
+
// exists for, so it restarts too.
|
|
579
|
+
{
|
|
580
|
+
const std::uint32_t fills = g_frameEofFills;
|
|
581
|
+
g_frameEofFills = 0;
|
|
582
|
+
const std::uint16_t lateUs = static_cast<std::uint16_t>(std::min<std::uint32_t>(lateQ4 >> 4, 65535));
|
|
583
|
+
if (fills < static_cast<std::uint32_t>(kHeight / kRgbBounceRows)) {
|
|
584
|
+
g_shortFrames++;
|
|
585
|
+
if (lateQ4 > kVsyncOnTimeQ4) g_shortFramesLate++;
|
|
586
|
+
}
|
|
587
|
+
if (bin != 1 && g_vsyncSeq > 200) {
|
|
588
|
+
SlipEvent &e = g_slips[g_slipCount % 6];
|
|
589
|
+
e = {static_cast<std::uint32_t>(now / 1000), static_cast<std::uint16_t>(bin), lateUs, g_prevLateUs,
|
|
590
|
+
static_cast<std::uint8_t>(std::min<std::uint32_t>(fills, 255)), g_prevEofFills};
|
|
591
|
+
g_slipCount++;
|
|
592
|
+
}
|
|
593
|
+
g_prevLateUs = lateUs;
|
|
594
|
+
g_prevEofFills = static_cast<std::uint8_t>(std::min<std::uint32_t>(fills, 255));
|
|
595
|
+
}
|
|
596
|
+
if (lateQ4 <= kVsyncOnTimeQ4) {
|
|
597
|
+
if (bin != 1) g_vsyncRealigned++;
|
|
598
|
+
if (esp_lcd_rgb_panel_restart(panel) == ESP_OK) {
|
|
599
|
+
g_vsyncRestarts++;
|
|
600
|
+
}
|
|
601
|
+
} else {
|
|
602
|
+
g_vsyncSkipped++;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
if (g_vsyncUs != 0) {
|
|
606
|
+
const auto &d = gea::platform::board::display;
|
|
607
|
+
const std::int64_t frameUs = static_cast<std::int64_t>(kWidth + d.hsyncPulseWidth + d.hsyncBackPorch + d.hsyncFrontPorch) *
|
|
608
|
+
(kHeight + d.vsyncPulseWidth + d.vsyncBackPorch + d.vsyncFrontPorch) * 1000000 / kPixelClockHz;
|
|
609
|
+
std::int32_t dev = static_cast<std::int32_t>(now - g_vsyncUs - frameUs);
|
|
610
|
+
if (dev < 0) dev = -dev;
|
|
611
|
+
if (dev > g_vsyncLateMaxUs) g_vsyncLateMaxUs = dev;
|
|
612
|
+
if (dev > 200) g_vsyncLate200++;
|
|
613
|
+
}
|
|
614
|
+
g_vsyncSeq = g_vsyncSeq + 1;
|
|
615
|
+
g_vsyncUs = now;
|
|
616
|
+
return false;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// The panel stops reading the old front once the refill starts on the next
|
|
620
|
+
// frame's first band: switch the scanned buffer there.
|
|
621
|
+
IRAM_ATTR void latchScanBuffer()
|
|
622
|
+
{
|
|
623
|
+
portENTER_CRITICAL_SAFE(&g_swapMux);
|
|
624
|
+
g_scanIndex = g_pendingScan;
|
|
625
|
+
g_scanMap = g_map[g_scanIndex];
|
|
626
|
+
g_frameSeq = g_frameSeq + 1;
|
|
627
|
+
portEXIT_CRITICAL_SAFE(&g_swapMux);
|
|
628
|
+
g_frameCompleteUs = esp_timer_get_time();
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
IRAM_ATTR bool wakeSwapWaiter()
|
|
632
|
+
{
|
|
633
|
+
if (!g_swapWaiting) return false;
|
|
634
|
+
if (!xPortInIsrContext()) {
|
|
635
|
+
xSemaphoreGive(g_frameSem);
|
|
636
|
+
return false;
|
|
637
|
+
}
|
|
638
|
+
BaseType_t woken = pdFALSE;
|
|
639
|
+
xSemaphoreGiveFromISR(g_frameSem, &woken);
|
|
640
|
+
return woken == pdTRUE;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
// Set by each refill that chose its own band (see onBounceEmpty); the driver
|
|
644
|
+
// calls this right after the refill, and its frame count is then not used.
|
|
645
|
+
DRAM_ATTR bool g_bandManaged = false;
|
|
646
|
+
|
|
647
|
+
IRAM_ATTR bool onPanelFrameComplete(esp_lcd_panel_handle_t, const esp_lcd_rgb_panel_event_data_t *, void *)
|
|
648
|
+
{
|
|
649
|
+
if (g_bandManaged) return false;
|
|
650
|
+
latchScanBuffer();
|
|
651
|
+
return wakeSwapWaiter();
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// Fill one bounce buffer from the scanned buffer through its ring mapping:
|
|
655
|
+
// contiguous physical runs, then a cache preload of the next batch's first run
|
|
656
|
+
// (the driver's own memcpy path does the same preload).
|
|
657
|
+
// Which of the GDMA channel's descriptors point into [buf, buf+len): bit 0
|
|
658
|
+
// the current one, bit 1 the previous; 4 when the channel is unknown.
|
|
659
|
+
IRAM_ATTR int panelDmaSending(const void *buf, int len)
|
|
660
|
+
{
|
|
661
|
+
#if CONFIG_IDF_TARGET_ESP32S3
|
|
662
|
+
int ch = g_panelDmaChannel;
|
|
663
|
+
if (ch < 0) {
|
|
664
|
+
for (int i = 0; i < 5; ++i)
|
|
665
|
+
if (GDMA.channel[i].out.peri_sel.sel == 5 && GDMA.channel[i].out.dscr != 0) ch = i; // 5: LCD_CAM
|
|
666
|
+
if (ch < 0) return 4;
|
|
667
|
+
g_panelDmaChannel = ch;
|
|
668
|
+
}
|
|
669
|
+
const auto lo = reinterpret_cast<std::uintptr_t>(buf);
|
|
670
|
+
const auto hi = lo + static_cast<std::uintptr_t>(len);
|
|
671
|
+
auto inside = [&](std::uint32_t desc) {
|
|
672
|
+
if (desc < SOC_DRAM_LOW || desc >= SOC_DRAM_HIGH) return false;
|
|
673
|
+
const auto b = static_cast<std::uintptr_t>(reinterpret_cast<const volatile std::uint32_t *>(desc)[1]);
|
|
674
|
+
return b >= lo && b < hi;
|
|
675
|
+
};
|
|
676
|
+
return (inside(GDMA.channel[ch].out.dscr) ? 1 : 0) | (inside(GDMA.channel[ch].out.dscr_bf0) ? 2 : 0);
|
|
677
|
+
#else
|
|
678
|
+
return 4;
|
|
679
|
+
#endif
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
// The two bounce buffers; [0] is the one the driver restarts the DMA on at
|
|
683
|
+
// every VSYNC, and the first it fills (at position 0) when transmission starts.
|
|
684
|
+
DRAM_ATTR std::uint8_t *g_bounceBuf[2] = {nullptr, nullptr};
|
|
685
|
+
// The newest frame (by VSYNC count) the scanned buffer has been switched for.
|
|
686
|
+
DRAM_ATTR std::uint32_t g_latchedFor = 0;
|
|
687
|
+
|
|
688
|
+
// The bounce buffer the GDMA channel is sending now, or -1 if unknown.
|
|
689
|
+
IRAM_ATTR int panelDmaBuffer(int len)
|
|
690
|
+
{
|
|
691
|
+
for (int i = 0; i < 2; ++i)
|
|
692
|
+
if (g_bounceBuf[i] && (panelDmaSending(g_bounceBuf[i], len) & 6) == 2) return i;
|
|
693
|
+
return -1;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
IRAM_ATTR int panelDmaBin()
|
|
697
|
+
{
|
|
698
|
+
#if CONFIG_IDF_TARGET_ESP32S3
|
|
699
|
+
if (g_panelDmaChannel < 0 || !g_bounceBuf[1]) return -1;
|
|
700
|
+
const std::uint32_t desc = GDMA.channel[g_panelDmaChannel].out.dscr_bf0;
|
|
701
|
+
if (desc < SOC_DRAM_LOW || desc >= SOC_DRAM_HIGH) return -1;
|
|
702
|
+
const auto b = static_cast<std::uintptr_t>(reinterpret_cast<const volatile std::uint32_t *>(desc)[1]);
|
|
703
|
+
for (int i = 0; i < 2; ++i) {
|
|
704
|
+
const auto lo = reinterpret_cast<std::uintptr_t>(g_bounceBuf[i]);
|
|
705
|
+
if (b >= lo && b < lo + 24u * kWidth * 2u) return i * 16 + static_cast<int>((b - lo) / 4095u);
|
|
706
|
+
}
|
|
707
|
+
#endif
|
|
708
|
+
return -1;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// Refill one bounce buffer from the scanned buffer through its ring mapping:
|
|
712
|
+
// contiguous physical runs, then a cache preload of the next band's first run.
|
|
713
|
+
//
|
|
714
|
+
// The buffer and band are chosen here, not taken from the driver. The driver
|
|
715
|
+
// picks the buffer from a running count of DMA EOF interrupts, and with
|
|
716
|
+
// CONFIG_LCD_RGB_RESTART_IN_VSYNC (the DMA restarts on buffer 0 at every
|
|
717
|
+
// VSYNC) that count is never reset: one EOF lost to interrupt latency leaves
|
|
718
|
+
// every later refill writing the buffer the DMA is sending, one band late,
|
|
719
|
+
// until another loss flips it back. The panel then shows the head of each
|
|
720
|
+
// 24-row band from the old contents -- thin lines, on a still screen too. On
|
|
721
|
+
// the bench it slipped at most boots and under load, and stayed slipped for
|
|
722
|
+
// seconds at a time. So: the channel's current descriptor says which buffer
|
|
723
|
+
// the DMA is sending (refill the other), and the time since VSYNC, snapped to
|
|
724
|
+
// that buffer's parity (band k is in buffer k % 2 after the restart), says
|
|
725
|
+
// which band it is on (refill the next).
|
|
726
|
+
IRAM_ATTR bool refillBand(std::uint8_t *given, int posPx, int lenBytes)
|
|
727
|
+
{
|
|
728
|
+
g_bounceFilling = true;
|
|
729
|
+
constexpr std::size_t kRowBytes = static_cast<std::size_t>(kWidth) * sizeof(std::uint16_t);
|
|
730
|
+
const int rows = static_cast<int>(static_cast<std::size_t>(lenBytes) / kRowBytes);
|
|
731
|
+
if (esp_timer_get_time() - g_vsyncUs >= 40) g_frameEofFills = g_frameEofFills + 1;
|
|
732
|
+
const int bands = kHeight / rows;
|
|
733
|
+
|
|
734
|
+
std::uint8_t *dst = given;
|
|
735
|
+
int band = posPx / (kWidth * rows);
|
|
736
|
+
bool managed = false;
|
|
737
|
+
std::uint32_t frame = 0;
|
|
738
|
+
const std::uint32_t vseq = g_vsyncSeq;
|
|
739
|
+
if (vseq != 0 && g_bounceBuf[1]) {
|
|
740
|
+
const auto &d = gea::platform::board::display;
|
|
741
|
+
const std::int64_t lineClocks = kWidth + d.hsyncPulseWidth + d.hsyncBackPorch + d.hsyncFrontPorch;
|
|
742
|
+
const int frameLines = kHeight + d.vsyncPulseWidth + d.vsyncBackPorch + d.vsyncFrontPorch;
|
|
743
|
+
const std::int64_t line = (esp_timer_get_time() - g_vsyncUs) * kPixelClockHz / (lineClocks * 1000000);
|
|
744
|
+
if (line <= frameLines + rows) {
|
|
745
|
+
if (posPx == 0 && line < 2) {
|
|
746
|
+
// The driver refilling buffer 0 inside the VSYNC interrupt, just
|
|
747
|
+
// before it restarts the DMA there: buffer 0 already holds band 0
|
|
748
|
+
// (refilled while the DMA sent band 19), and a copy here would hold
|
|
749
|
+
// the restart back.
|
|
750
|
+
g_bandManaged = true;
|
|
751
|
+
g_bounceFilling = false;
|
|
752
|
+
return false;
|
|
753
|
+
} else if (const int p = panelDmaBuffer(lenBytes); p >= 0) {
|
|
754
|
+
// The DMA moves to band k about 4 lines after line 24k (measured).
|
|
755
|
+
constexpr int kLeadLines = 4;
|
|
756
|
+
const int num = static_cast<int>(line) - kLeadLines - rows * p + rows / 2;
|
|
757
|
+
const int span = 2 * rows;
|
|
758
|
+
const int k = p + 2 * (num >= 0 ? num / span : -((span - 1 - num) / span));
|
|
759
|
+
int t = k + 1;
|
|
760
|
+
frame = vseq;
|
|
761
|
+
if (t >= bands) {
|
|
762
|
+
t -= bands;
|
|
763
|
+
frame = vseq + 1;
|
|
764
|
+
}
|
|
765
|
+
managed = t >= 0;
|
|
766
|
+
band = t;
|
|
767
|
+
dst = g_bounceBuf[1 - p];
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
g_bandManaged = managed;
|
|
772
|
+
bool yield = false;
|
|
773
|
+
if (managed && static_cast<std::int32_t>(frame - g_latchedFor) > 0) {
|
|
774
|
+
g_latchedFor = frame;
|
|
775
|
+
latchScanBuffer();
|
|
776
|
+
yield = wakeSwapWaiter();
|
|
777
|
+
}
|
|
778
|
+
const int sendingAtStart = panelDmaSending(dst, lenBytes);
|
|
779
|
+
|
|
780
|
+
const int row0 = band * rows;
|
|
781
|
+
const std::uint16_t *fb = g_scanFb[g_scanIndex];
|
|
782
|
+
const ScanMap m = g_scanMap;
|
|
783
|
+
for (int r = 0; r < rows;) {
|
|
784
|
+
const int phys = mapRow(m, row0 + r);
|
|
785
|
+
int run = 1;
|
|
786
|
+
while (r + run < rows && mapRow(m, row0 + r + run) == phys + run) ++run;
|
|
787
|
+
std::memcpy(dst + static_cast<std::size_t>(r) * kRowBytes, fb + static_cast<std::size_t>(phys) * kWidth, static_cast<std::size_t>(run) * kRowBytes);
|
|
788
|
+
r += run;
|
|
789
|
+
}
|
|
790
|
+
#if CONFIG_IDF_TARGET_ESP32S3
|
|
791
|
+
const int next = row0 + rows;
|
|
792
|
+
if (next < kHeight) {
|
|
793
|
+
const int phys = mapRow(m, next);
|
|
794
|
+
int run = 1;
|
|
795
|
+
while (run < rows && next + run < kHeight && mapRow(m, next + run) == phys + run) ++run;
|
|
796
|
+
const std::uint32_t bytes = static_cast<std::uint32_t>(static_cast<std::size_t>(run) * kRowBytes);
|
|
797
|
+
Cache_Start_DCache_Preload(reinterpret_cast<std::uint32_t>(fb + static_cast<std::size_t>(phys) * kWidth), bytes, 0);
|
|
798
|
+
}
|
|
799
|
+
#endif
|
|
800
|
+
{
|
|
801
|
+
const int sendingAtEnd = panelDmaSending(dst, lenBytes);
|
|
802
|
+
g_refill.fills++;
|
|
803
|
+
if (!managed) g_refill.fallback++;
|
|
804
|
+
else if (dst != given || band != posPx / (kWidth * rows)) g_refill.corrected++;
|
|
805
|
+
if (sendingAtStart & 4) g_refill.unknown++;
|
|
806
|
+
for (int k = 0; k < 2; ++k) {
|
|
807
|
+
if (!(sendingAtStart & 4) && (sendingAtStart & (2 >> k))) g_refill.atStart[k]++;
|
|
808
|
+
if (!(sendingAtEnd & 4) && (sendingAtEnd & (2 >> k))) g_refill.atEnd[k]++;
|
|
809
|
+
}
|
|
810
|
+
if (band >= 0 && band < 32) {
|
|
811
|
+
if (sendingAtStart & 2) g_refill.atStartPos[band]++;
|
|
812
|
+
if (sendingAtEnd & 2) g_refill.atEndPos[band]++;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
g_bounceFilling = false;
|
|
816
|
+
return yield;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
IRAM_ATTR bool onBounceEmpty(esp_lcd_panel_handle_t, void *bounce, int posPx, int lenBytes, void *)
|
|
820
|
+
{
|
|
821
|
+
auto *given = static_cast<std::uint8_t *>(bounce);
|
|
822
|
+
if (!g_bounceBuf[0]) {
|
|
823
|
+
if (posPx == 0) g_bounceBuf[0] = given;
|
|
824
|
+
} else if (!g_bounceBuf[1] && given != g_bounceBuf[0]) {
|
|
825
|
+
g_bounceBuf[1] = given;
|
|
826
|
+
}
|
|
827
|
+
return refillBand(given, posPx, lenBytes);
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
int rectArea(int x0, int y0, int x1, int y1) { return (x1 - x0 + 1) * (y1 - y0 + 1); }
|
|
831
|
+
|
|
832
|
+
void staleAdd(StaleList &list, int x0, int y0, int x1, int y1)
|
|
833
|
+
{
|
|
834
|
+
for (int i = 0; i < list.count; ++i) {
|
|
835
|
+
StaleRect &r = list.rects[i];
|
|
836
|
+
if (x0 >= r.x0 && y0 >= r.y0 && x1 <= r.x1 && y1 <= r.y1) return; // already covered
|
|
837
|
+
// Merge when the bounding box costs no more than copying both: an
|
|
838
|
+
// animation's dirty box drifts a little frame to frame, and copying the two
|
|
839
|
+
// overlapping boxes separately would move the overlap twice.
|
|
840
|
+
const int ux0 = std::min(x0, r.x0), uy0 = std::min(y0, r.y0);
|
|
841
|
+
const int ux1 = std::max(x1, r.x1), uy1 = std::max(y1, r.y1);
|
|
842
|
+
if (rectArea(ux0, uy0, ux1, uy1) <= rectArea(x0, y0, x1, y1) + rectArea(r.x0, r.y0, r.x1, r.y1)) {
|
|
843
|
+
list.rects[i] = list.rects[--list.count];
|
|
844
|
+
staleAdd(list, ux0, uy0, ux1, uy1); // the grown box may now swallow others
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
if (list.count < kMaxStaleRects) {
|
|
849
|
+
list.rects[list.count++] = {x0, y0, x1, y1};
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
// Full: fold everything into one bounding box (correct, just copies more).
|
|
853
|
+
StaleRect box = {x0, y0, x1, y1};
|
|
854
|
+
for (int i = 0; i < list.count; ++i) {
|
|
855
|
+
const StaleRect &r = list.rects[i];
|
|
856
|
+
box = {std::min(box.x0, r.x0), std::min(box.y0, r.y0), std::max(box.x1, r.x1), std::max(box.y1, r.y1)};
|
|
857
|
+
}
|
|
858
|
+
list.rects[0] = box;
|
|
859
|
+
list.count = 1;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// A region the back buffer now holds new pixels for: the front lags there.
|
|
863
|
+
void markStale(int x0, int y0, int x1, int y1)
|
|
864
|
+
{
|
|
865
|
+
for (int i = 0; i < g_scanFbCount; ++i)
|
|
866
|
+
if (i != g_drawIndex) staleAdd(g_stale[i], x0, y0, x1, y1);
|
|
867
|
+
g_publishPending = true;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
// Block (not spin) until the panel has stopped reading the back buffer. Normally
|
|
871
|
+
// the swap latched long ago and this returns at once; it only waits when frames
|
|
872
|
+
// are published faster than the panel refreshes.
|
|
873
|
+
void waitBackBufferFree()
|
|
874
|
+
{
|
|
875
|
+
const std::uint32_t freeSeq = g_freeSeq[g_drawIndex];
|
|
876
|
+
if (static_cast<std::int32_t>(g_frameSeq - freeSeq) >= 0) return;
|
|
877
|
+
const std::int64_t w0 = esp_timer_get_time();
|
|
878
|
+
xSemaphoreTake(g_frameSem, 0); // drop a stale give from an earlier wait
|
|
879
|
+
g_swapWaiting = true;
|
|
880
|
+
while (static_cast<std::int32_t>(g_frameSeq - freeSeq) < 0) {
|
|
881
|
+
xSemaphoreTake(g_frameSem, pdMS_TO_TICKS(5));
|
|
882
|
+
if (esp_timer_get_time() - w0 > 100000) {
|
|
883
|
+
// The panel stopped scanning (or never started): don't hang the frame.
|
|
884
|
+
g_swapDiag.timeouts++;
|
|
885
|
+
break;
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
g_swapWaiting = false;
|
|
889
|
+
const std::int64_t now = esp_timer_get_time();
|
|
890
|
+
g_swapDiag.waitUs += now - w0;
|
|
891
|
+
g_swapDiag.waits++;
|
|
892
|
+
g_lastBlockUs = now;
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
// Hand the back buffer to the panel. The driver switches to it when the bounce
|
|
896
|
+
// ISR wraps to the next frame; g_freeSeq says when the OLD front is released.
|
|
897
|
+
// If this task runs on the ISR's core, masking interrupts around the switch
|
|
898
|
+
// guarantees the next frame-complete already scans the new buffer; from the other
|
|
899
|
+
// core the ISR may be mid-wrap with the old index, so one more frame is required.
|
|
900
|
+
void commitSwap(int back)
|
|
901
|
+
{
|
|
902
|
+
std::uint32_t seq;
|
|
903
|
+
#if GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
|
|
904
|
+
// The bounce fill reads g_scanFb[g_scanIndex], which the frame-complete ISR
|
|
905
|
+
// switches to g_pendingScan at the wrap; the shared spinlock makes "the next
|
|
906
|
+
// wrap takes it" exact from either core, so the old front is released at the
|
|
907
|
+
// very next frame end.
|
|
908
|
+
taskENTER_CRITICAL(&g_swapMux);
|
|
909
|
+
g_pendingScan = back;
|
|
910
|
+
seq = g_frameSeq + 1;
|
|
911
|
+
taskEXIT_CRITICAL(&g_swapMux);
|
|
912
|
+
#else
|
|
913
|
+
// Direct scanout: draw_bitmap writes the whole frame back from the cache
|
|
914
|
+
// (too long for a critical section), and the GDMA may prefetch one more pass
|
|
915
|
+
// of the old buffer, so always allow two frame ends.
|
|
916
|
+
esp_lcd_panel_draw_bitmap(g_panel, 0, 0, kWidth, kHeight, g_scanFb[back]);
|
|
917
|
+
seq = g_frameSeq + 2;
|
|
918
|
+
#endif
|
|
919
|
+
g_freeSeq[g_frontIndex] = seq;
|
|
920
|
+
std::uint16_t *oldBack = g_scanFb[back];
|
|
921
|
+
g_frontIndex = back;
|
|
922
|
+
g_drawIndex = (back + 1) % g_scanFbCount;
|
|
923
|
+
g_framebuffer = g_scanFb[back];
|
|
924
|
+
g_drawFramebuffer = g_scanFb[g_drawIndex];
|
|
925
|
+
g_backReady = false;
|
|
926
|
+
// Follow the swap with the draw canvases -- unless the render layer has one
|
|
927
|
+
// bound to a scratch buffer right now (it rebinds via rebindCanvasToFramebuffer).
|
|
928
|
+
if (g_canvas.pixels() == oldBack) g_canvas.bindPixels(g_drawFramebuffer, kWidth, kHeight);
|
|
929
|
+
if (g_workerCanvas.pixels() == oldBack) g_workerCanvas.bindPixels(g_drawFramebuffer, kWidth, kHeight);
|
|
930
|
+
applyCanvasMap();
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
// One line per kSwapDiagFrames published frames, the same in both modes:
|
|
934
|
+
// publish = whole Display flush call, raster = fused raster inside it, copy =
|
|
935
|
+
// pixel copies into scanout memory, wait = blocked on the panel releasing the back buffer.
|
|
936
|
+
void noteFrameDiag(int path, std::int64_t totalUs, int px)
|
|
937
|
+
{
|
|
938
|
+
if (px <= 0) return;
|
|
939
|
+
g_swapDiag.paths[path]++;
|
|
940
|
+
g_swapDiag.totalUs += totalUs;
|
|
941
|
+
g_swapDiag.px += px;
|
|
942
|
+
if (g_swapDiag.windowStartUs == 0) g_swapDiag.windowStartUs = esp_timer_get_time();
|
|
943
|
+
if (++g_swapDiag.frames >= kSwapDiagFrames) {
|
|
944
|
+
const std::int64_t now = esp_timer_get_time();
|
|
945
|
+
const int n = g_swapDiag.frames;
|
|
946
|
+
const std::uint32_t vs = g_vsyncSeq, fr = g_frameSeq;
|
|
947
|
+
const double secs = static_cast<double>(now - g_swapDiag.windowStartUs) / 1e6;
|
|
948
|
+
std::printf("[rgb-swap] panel vsync/s=%.2f wraps/s=%.2f slip=%d | swap=%d frames=%d paths=%d/%d/%d/%d publish=%dus raster=%dus copy=%dus wait=%dus(%d waits, %d timeouts) px=%d patch_px=%d unready=%d late=%d regions=%d rings=%d fallbacks=%d prepares=%d prepare_px=%d frames/s=%.1f\n",
|
|
949
|
+
(vs - g_diagVsync0) / secs, (fr - g_diagFrame0) / secs,
|
|
950
|
+
static_cast<int>((vs - g_diagVsync0) - (fr - g_diagFrame0)),
|
|
951
|
+
g_swapFbs ? 1 : 0,
|
|
952
|
+
n,
|
|
953
|
+
g_swapDiag.paths[0], g_swapDiag.paths[1], g_swapDiag.paths[2], g_swapDiag.paths[3],
|
|
954
|
+
static_cast<int>(g_swapDiag.totalUs / n),
|
|
955
|
+
static_cast<int>(g_swapDiag.rasterUs / n),
|
|
956
|
+
static_cast<int>(g_swapDiag.copyUs / n),
|
|
957
|
+
static_cast<int>(g_swapDiag.waitUs / n),
|
|
958
|
+
g_swapDiag.waits,
|
|
959
|
+
g_swapDiag.timeouts,
|
|
960
|
+
static_cast<int>(g_swapDiag.px / n),
|
|
961
|
+
static_cast<int>(g_swapDiag.patchPx / n),
|
|
962
|
+
g_swapDiag.unready,
|
|
963
|
+
g_swapDiag.late,
|
|
964
|
+
g_swapDiag.regions, g_swapDiag.rings, g_swapDiag.fallbacks,
|
|
965
|
+
g_swapDiag.prepares, static_cast<int>(g_swapDiag.preparePx / n),
|
|
966
|
+
n * 1000000.0 / static_cast<double>(now - g_swapDiag.windowStartUs));
|
|
967
|
+
g_swapDiag = {};
|
|
968
|
+
g_swapDiag.windowStartUs = now;
|
|
969
|
+
g_diagVsync0 = vs;
|
|
970
|
+
g_diagFrame0 = fr;
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
// One row's copy into a scanout buffer, kept out of the bounce refills'
|
|
975
|
+
// way. A refill reads its rows from PSRAM through the data cache it shares
|
|
976
|
+
// with every core, and has one bounce period to finish or the LCD DMA sends a
|
|
977
|
+
// half-refilled band (a tear). Two things land in that window: the other
|
|
978
|
+
// core's PSRAM traffic, and -- worse -- dirty framebuffer lines a draw left in
|
|
979
|
+
// the cache, which the refill's own reads evict and so write back inside its
|
|
980
|
+
// window. So a copy on the other core waits out a refill in progress, and every
|
|
981
|
+
// copy writes its own lines back at once, while the refill is not running
|
|
982
|
+
// (on the ISR's core it cannot be); publishSwap does the same for the frame's
|
|
983
|
+
// direct draws. Scrolling Profile, 30-row refills overran 1.3% of their
|
|
984
|
+
// 1.64 ms windows (to 1.84 ms) before this; after it, none in ~12k refills per
|
|
985
|
+
// tab on Profile, Shop and Analytics, for about one frame per second.
|
|
986
|
+
// Spins (no PSRAM access: the flag is in internal RAM) while a refill runs on
|
|
987
|
+
// the ISR's core; bounded, so a stopped panel cannot hang the caller.
|
|
988
|
+
inline void waitOutRefill()
|
|
989
|
+
{
|
|
990
|
+
const std::uint32_t t0 = esp_cpu_get_cycle_count();
|
|
991
|
+
while (g_bounceFilling && esp_cpu_get_cycle_count() - t0 < 240u * 3000u) {
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
// One framebuffer row written back from the data cache. esp_cache_msync does the
|
|
995
|
+
// same under the same lock, but also takes a mutex and validates its arguments
|
|
996
|
+
// on every call: ~5 us a row, which a frame of 64 bouncing balls (~2,900 short
|
|
997
|
+
// rows) paid ~14 ms for. The rows here are always cacheable PSRAM.
|
|
998
|
+
inline void writebackRow(const void *dst, std::size_t bytes)
|
|
999
|
+
{
|
|
1000
|
+
esp_cache_sync_ops_enter_critical_section();
|
|
1001
|
+
cache_hal_writeback_addr(reinterpret_cast<std::uint32_t>(dst), bytes);
|
|
1002
|
+
esp_cache_sync_ops_exit_critical_section();
|
|
1003
|
+
}
|
|
1004
|
+
// Row-repeat records (see g_rowSum): a write they did not see forgets them.
|
|
1005
|
+
void forgetRowSums();
|
|
1006
|
+
inline void forgetRowSumAt(const void *dst);
|
|
1007
|
+
void scanoutRowCopy(void *dst, const void *src, std::size_t bytes)
|
|
1008
|
+
{
|
|
1009
|
+
forgetRowSumAt(dst);
|
|
1010
|
+
#if GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
|
|
1011
|
+
if (static_cast<int>(xPortGetCoreID()) != g_panelIsrCore) waitOutRefill();
|
|
1012
|
+
std::memcpy(dst, src, bytes);
|
|
1013
|
+
writebackRow(dst, bytes);
|
|
1014
|
+
#else
|
|
1015
|
+
std::memcpy(dst, src, bytes);
|
|
1016
|
+
#endif
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
// Copy rect r of the front buffer into the back buffer.
|
|
1020
|
+
void patchRect(const StaleRect &r)
|
|
1021
|
+
{
|
|
1022
|
+
const std::uint16_t *src = g_scanFb[g_frontIndex];
|
|
1023
|
+
std::uint16_t *dst = g_scanFb[g_drawIndex];
|
|
1024
|
+
const std::size_t bytes = static_cast<std::size_t>(r.x1 - r.x0 + 1) * sizeof(std::uint16_t);
|
|
1025
|
+
const ScanMap &ms = g_map[g_frontIndex];
|
|
1026
|
+
const ScanMap &md = g_map[g_drawIndex];
|
|
1027
|
+
for (int y = r.y0; y <= r.y1; ++y) {
|
|
1028
|
+
scanoutRowCopy(&dst[static_cast<std::size_t>(mapRow(md, y)) * kWidth + r.x0],
|
|
1029
|
+
&src[static_cast<std::size_t>(mapRow(ms, y)) * kWidth + r.x0], bytes);
|
|
1030
|
+
}
|
|
1031
|
+
cacheWritebackRect(dst, r.x0, r.y0, r.x1, r.y1);
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
// a minus b, appended to out (up to 4 pieces). Returns false if out is full.
|
|
1035
|
+
bool subtractRect(const StaleRect &a, const StaleRect &b, StaleRect *out, int &n, int cap)
|
|
1036
|
+
{
|
|
1037
|
+
if (b.x1 < a.x0 || b.x0 > a.x1 || b.y1 < a.y0 || b.y0 > a.y1) {
|
|
1038
|
+
if (n >= cap) return false;
|
|
1039
|
+
out[n++] = a;
|
|
1040
|
+
return true;
|
|
1041
|
+
}
|
|
1042
|
+
StaleRect pieces[4];
|
|
1043
|
+
int k = 0;
|
|
1044
|
+
if (b.y0 > a.y0) pieces[k++] = {a.x0, a.y0, a.x1, b.y0 - 1};
|
|
1045
|
+
if (b.y1 < a.y1) pieces[k++] = {a.x0, b.y1 + 1, a.x1, a.y1};
|
|
1046
|
+
const int my0 = std::max(a.y0, b.y0), my1 = std::min(a.y1, b.y1);
|
|
1047
|
+
if (b.x0 > a.x0) pieces[k++] = {a.x0, my0, b.x0 - 1, my1};
|
|
1048
|
+
if (b.x1 < a.x1) pieces[k++] = {b.x1 + 1, my0, a.x1, my1};
|
|
1049
|
+
if (n + k > cap) return false;
|
|
1050
|
+
for (int i = 0; i < k; ++i) out[n++] = pieces[i];
|
|
1051
|
+
return true;
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
// Bring the back buffer up to date from the front, except inside `skip` (rects
|
|
1055
|
+
// the caller is about to repaint completely). Skipping is only an optimisation:
|
|
1056
|
+
// the patch always runs before the repaint, so copying extra is still correct.
|
|
1057
|
+
int patchBack(const DisplayFlushRect *skip, int skipCount)
|
|
1058
|
+
{
|
|
1059
|
+
StaleList &list = g_stale[g_drawIndex];
|
|
1060
|
+
// Each stale rect is cut by the skip rects on its own, so a long stale list
|
|
1061
|
+
// needs no more scratch than one rect's fragments.
|
|
1062
|
+
constexpr int kCap = 64;
|
|
1063
|
+
StaleRect work[2][kCap];
|
|
1064
|
+
int px = 0;
|
|
1065
|
+
for (int r = 0; r < list.count; ++r) {
|
|
1066
|
+
const StaleRect &a = list.rects[r];
|
|
1067
|
+
int n = 1, cur = 0;
|
|
1068
|
+
work[0][0] = a;
|
|
1069
|
+
for (int s = 0; s < skipCount && n > 0; ++s) {
|
|
1070
|
+
const StaleRect b = {skip[s].x0, skip[s].y0, skip[s].x1, skip[s].y1};
|
|
1071
|
+
if (b.x1 < a.x0 || b.x0 > a.x1 || b.y1 < a.y0 || b.y0 > a.y1) continue;
|
|
1072
|
+
int m = 0;
|
|
1073
|
+
bool ok = true;
|
|
1074
|
+
for (int i = 0; i < n && ok; ++i) ok = subtractRect(work[cur][i], b, work[cur ^ 1], m, kCap);
|
|
1075
|
+
if (!ok) break; // too fragmented: copy what we have, still correct
|
|
1076
|
+
cur ^= 1;
|
|
1077
|
+
n = m;
|
|
1078
|
+
}
|
|
1079
|
+
for (int i = 0; i < n; ++i) {
|
|
1080
|
+
patchRect(work[cur][i]);
|
|
1081
|
+
px += rectArea(work[cur][i].x0, work[cur][i].y0, work[cur][i].x1, work[cur][i].y1);
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
list.count = 0;
|
|
1085
|
+
return px;
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
// Rows [first, first+count) of buffer `fb`, rotated left by `by` rows, through
|
|
1089
|
+
// one row of internal RAM (juggling: every row moves once).
|
|
1090
|
+
void rotateRows(std::uint16_t *fb, int first, int count, int by)
|
|
1091
|
+
{
|
|
1092
|
+
by = normOffset(by, count);
|
|
1093
|
+
if (count <= 1 || by == 0) return;
|
|
1094
|
+
forgetRowSums();
|
|
1095
|
+
static std::uint16_t temp[kWidth];
|
|
1096
|
+
constexpr std::size_t kRowBytes = static_cast<std::size_t>(kWidth) * sizeof(std::uint16_t);
|
|
1097
|
+
auto row = [&](int i) { return fb + static_cast<std::size_t>(first + i) * kWidth; };
|
|
1098
|
+
int a = count, b = by;
|
|
1099
|
+
while (b) { const int t = a % b; a = b; b = t; }
|
|
1100
|
+
for (int start = 0; start < a; ++start) {
|
|
1101
|
+
std::memcpy(temp, row(start), kRowBytes);
|
|
1102
|
+
int i = start;
|
|
1103
|
+
for (;;) {
|
|
1104
|
+
int j = i + by;
|
|
1105
|
+
if (j >= count) j -= count;
|
|
1106
|
+
if (j == start) break;
|
|
1107
|
+
std::memcpy(row(i), row(j), kRowBytes);
|
|
1108
|
+
i = j;
|
|
1109
|
+
}
|
|
1110
|
+
std::memcpy(row(i), temp, kRowBytes);
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
// Physically re-lay buffer i (not being scanned) from its mapping to `target`
|
|
1115
|
+
// without changing what any logical row holds.
|
|
1116
|
+
void remapBuffer(int i, const ScanMap &target)
|
|
1117
|
+
{
|
|
1118
|
+
ScanMap &m = g_map[i];
|
|
1119
|
+
if (m.y == target.y && m.h == target.h && m.off == target.off) return;
|
|
1120
|
+
if (m.h > 0) rotateRows(g_scanFb[i], m.y, m.h, m.off); // to identity
|
|
1121
|
+
if (target.h > 0) rotateRows(g_scanFb[i], target.y, target.h, -target.off); // to target
|
|
1122
|
+
m = target;
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
// Bring the draw buffer's mapping up to the current frame before it is patched.
|
|
1126
|
+
void bringMapCurrent(int i)
|
|
1127
|
+
{
|
|
1128
|
+
ScanMap &m = g_map[i];
|
|
1129
|
+
if (g_mapReset[i]) {
|
|
1130
|
+
// Fully stale: any layout works, and matching the front's makes the full
|
|
1131
|
+
// patch a straight copy.
|
|
1132
|
+
const ScanMap &front = g_map[g_frontIndex];
|
|
1133
|
+
m = (front.y == g_regionY && front.h == g_regionH) ? front : ScanMap{g_regionY, g_regionH, 0};
|
|
1134
|
+
g_mapReset[i] = false;
|
|
1135
|
+
} else if (g_pendingRot[i] != 0) {
|
|
1136
|
+
m.off = normOffset(m.off - g_pendingRot[i], m.h);
|
|
1137
|
+
}
|
|
1138
|
+
g_pendingRot[i] = 0;
|
|
1139
|
+
applyCanvasMap();
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
// A new ring region (or none). The draw buffer, ready and current, is re-laid
|
|
1143
|
+
// in place; every other buffer is marked fully stale and adopts a mapping when
|
|
1144
|
+
// it is next readied.
|
|
1145
|
+
void changeRegion(int y, int h)
|
|
1146
|
+
{
|
|
1147
|
+
if (g_regionY == y && g_regionH == h) return;
|
|
1148
|
+
g_swapDiag.regions++;
|
|
1149
|
+
g_regionY = h > 0 ? y : 0;
|
|
1150
|
+
g_regionH = h > 0 ? h : 0;
|
|
1151
|
+
const std::int64_t c0 = esp_timer_get_time();
|
|
1152
|
+
remapBuffer(g_drawIndex, ScanMap{g_regionY, g_regionH, 0});
|
|
1153
|
+
g_swapDiag.copyUs += esp_timer_get_time() - c0;
|
|
1154
|
+
for (int i = 0; i < g_scanFbCount; ++i) {
|
|
1155
|
+
if (i == g_drawIndex) continue;
|
|
1156
|
+
g_mapReset[i] = true;
|
|
1157
|
+
g_pendingRot[i] = 0;
|
|
1158
|
+
g_stale[i].count = 0;
|
|
1159
|
+
staleAdd(g_stale[i], 0, 0, kWidth - 1, kHeight - 1);
|
|
1160
|
+
}
|
|
1161
|
+
applyCanvasMap();
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
// Shift a lagging buffer's stale rects by a scroll of the region: the part
|
|
1165
|
+
// inside the region moves with the content; rows pushed out of the region wrap
|
|
1166
|
+
// to the exposed side, which the scroll frame repaints and marks stale itself.
|
|
1167
|
+
void shiftStale(StaleList &list, int ry, int rh, int dy)
|
|
1168
|
+
{
|
|
1169
|
+
static StaleRect old[kMaxStaleRects]; // render task only, under g_readyMutex
|
|
1170
|
+
const int n = list.count;
|
|
1171
|
+
for (int i = 0; i < n; ++i) old[i] = list.rects[i];
|
|
1172
|
+
list.count = 0;
|
|
1173
|
+
const int ry1 = ry + rh - 1;
|
|
1174
|
+
for (int i = 0; i < n; ++i) {
|
|
1175
|
+
const StaleRect r = old[i];
|
|
1176
|
+
if (r.y0 < ry) staleAdd(list, r.x0, r.y0, r.x1, std::min(r.y1, ry - 1));
|
|
1177
|
+
if (r.y1 > ry1) staleAdd(list, r.x0, std::max(r.y0, ry1 + 1), r.x1, r.y1);
|
|
1178
|
+
const int y0 = std::max(r.y0, ry) + dy, y1 = std::min(r.y1, ry1) + dy;
|
|
1179
|
+
if (std::max(r.y0, ry) <= std::min(r.y1, ry1)) {
|
|
1180
|
+
const int cy0 = std::max(y0, ry), cy1 = std::min(y1, ry1);
|
|
1181
|
+
if (cy0 <= cy1) staleAdd(list, r.x0, cy0, r.x1, cy1);
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
// Make the back buffer safe to draw into: released by the panel and current.
|
|
1187
|
+
__attribute__((noinline)) void ensureBackReady(const DisplayFlushRect *skip, int skipCount)
|
|
1188
|
+
{
|
|
1189
|
+
if (!g_swapFbs || g_backReady) return;
|
|
1190
|
+
xSemaphoreTake(g_readyMutex, portMAX_DELAY);
|
|
1191
|
+
if (!g_backReady) {
|
|
1192
|
+
// Pixels already drawn since the swap went in ahead of the gate: into a
|
|
1193
|
+
// buffer the panel may still have been scanning, and the patch below may
|
|
1194
|
+
// now overwrite them.
|
|
1195
|
+
int dx0, dy0, dx1, dy1;
|
|
1196
|
+
if (g_canvas.dirty(&dx0, &dy0, &dx1, &dy1) || g_workerCanvas.dirty(&dx0, &dy0, &dx1, &dy1)) g_swapDiag.late++;
|
|
1197
|
+
waitBackBufferFree();
|
|
1198
|
+
bringMapCurrent(g_drawIndex);
|
|
1199
|
+
const std::int64_t c0 = esp_timer_get_time();
|
|
1200
|
+
g_swapDiag.patchPx += patchBack(skip, skipCount);
|
|
1201
|
+
g_swapDiag.copyUs += esp_timer_get_time() - c0;
|
|
1202
|
+
g_backReady = true;
|
|
1203
|
+
}
|
|
1204
|
+
xSemaphoreGive(g_readyMutex);
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
bool g_vsyncWanted = true;
|
|
1208
|
+
int publishSwap();
|
|
1209
|
+
// Switch between swapped buffers and direct draw (see Display::setVSync) when
|
|
1210
|
+
// nothing is in flight; a ring-scroll region or a scroll copy defers it to the
|
|
1211
|
+
// next call.
|
|
1212
|
+
void applyVSync()
|
|
1213
|
+
{
|
|
1214
|
+
if (!g_framebuffer || g_scanFbCount < 2 || g_vsyncWanted == g_swapFbs) return;
|
|
1215
|
+
if (g_regionH > 0 || g_scrollCopyInFlight) return;
|
|
1216
|
+
xSemaphoreTake(g_readyMutex, portMAX_DELAY);
|
|
1217
|
+
if (!g_vsyncWanted) {
|
|
1218
|
+
// The last frame drawn becomes the front, then is drawn into in place
|
|
1219
|
+
// once the panel scans it.
|
|
1220
|
+
publishSwap();
|
|
1221
|
+
waitBackBufferFree();
|
|
1222
|
+
g_drawIndex = g_frontIndex;
|
|
1223
|
+
g_swapFbs = false;
|
|
1224
|
+
g_ringScroll = false;
|
|
1225
|
+
} else {
|
|
1226
|
+
g_drawIndex = (g_frontIndex + 1) % g_scanFbCount;
|
|
1227
|
+
g_stale[g_drawIndex].count = 0;
|
|
1228
|
+
staleAdd(g_stale[g_drawIndex], 0, 0, kWidth - 1, kHeight - 1);
|
|
1229
|
+
g_swapFbs = true;
|
|
1230
|
+
g_ringScroll = GEA_EMBEDDED_DISPLAY_RGB_BOUNCE;
|
|
1231
|
+
}
|
|
1232
|
+
g_drawFramebuffer = g_scanFb[g_drawIndex];
|
|
1233
|
+
g_backReady = !g_swapFbs;
|
|
1234
|
+
g_canvas.bindPixels(g_drawFramebuffer, kWidth, kHeight);
|
|
1235
|
+
g_workerCanvas.bindPixels(g_drawFramebuffer, kWidth, kHeight);
|
|
1236
|
+
applyCanvasMap();
|
|
1237
|
+
xSemaphoreGive(g_readyMutex);
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
// Swap in everything drawn since the last swap. No pixels move here.
|
|
1241
|
+
int publishSwap()
|
|
1242
|
+
{
|
|
1243
|
+
if (!g_publishPending) return 0;
|
|
1244
|
+
g_publishPending = false;
|
|
1245
|
+
// Something drew into the back buffer without going through replayCanvas /
|
|
1246
|
+
// ensureBackReady: it may have written rows the panel was still scanning.
|
|
1247
|
+
if (!g_backReady) g_swapDiag.unready++;
|
|
1248
|
+
#if GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
|
|
1249
|
+
// The frame's direct draws left dirty lines in the cache; written back now,
|
|
1250
|
+
// between refills, they are not evicted inside one (see scanoutRowCopy).
|
|
1251
|
+
// Every other buffer's stale list holds what this frame drew; one of them
|
|
1252
|
+
// is enough.
|
|
1253
|
+
if (g_scanFbCount > 1 && !g_frameWrittenBack) {
|
|
1254
|
+
const StaleList &drawn = g_stale[(g_drawIndex + 1) % g_scanFbCount];
|
|
1255
|
+
const ScanMap &m = g_map[g_drawIndex];
|
|
1256
|
+
const bool otherCore = static_cast<int>(xPortGetCoreID()) != g_panelIsrCore;
|
|
1257
|
+
for (int k = 0; k < drawn.count; ++k) {
|
|
1258
|
+
const StaleRect &r = drawn.rects[k];
|
|
1259
|
+
// Inside a scroll copy's viewport every row already left the cache:
|
|
1260
|
+
// the copy wrote each back, and so did every staged repaint in it.
|
|
1261
|
+
// The per-row walk (and refill wait) cost ~1 ms of a chart scroll frame.
|
|
1262
|
+
if (r.x0 >= g_cleanRect.x0 && r.y0 >= g_cleanRect.y0 && r.x1 <= g_cleanRect.x1 && r.y1 <= g_cleanRect.y1) continue;
|
|
1263
|
+
const std::size_t bytes = static_cast<std::size_t>(r.x1 - r.x0 + 1) * sizeof(std::uint16_t);
|
|
1264
|
+
for (int y = r.y0; y <= r.y1; ++y) {
|
|
1265
|
+
if (otherCore) waitOutRefill();
|
|
1266
|
+
writebackRow(g_drawFramebuffer + static_cast<std::size_t>(mapRow(m, y)) * kWidth + r.x0, bytes);
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
#endif
|
|
1271
|
+
g_frameWrittenBack = false;
|
|
1272
|
+
g_cleanRect = {0, 0, -1, -1};
|
|
1273
|
+
// Any other kind of frame may have changed what any buffer's viewport holds.
|
|
1274
|
+
if (!g_frameIsScrollCopy)
|
|
1275
|
+
for (bool &v : g_rowSigValid) v = false;
|
|
1276
|
+
g_frameIsScrollCopy = false;
|
|
1277
|
+
g_nextSig = nullptr; // signatures name this frame's list only
|
|
1278
|
+
commitSwap(g_drawIndex);
|
|
1279
|
+
return 1;
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
// The frame task never blocks on an RGB panel (no transfer to wait for), so on a
|
|
1283
|
+
// long animation IDLE never runs on its core and the task watchdog fires every
|
|
1284
|
+
// 5s -- and its ISR prints a backtrace over the 115200-baud console with that
|
|
1285
|
+
// core's interrupts held off long enough to starve the bounce-buffer ISR, a
|
|
1286
|
+
// visible glitch. Give IDLE one tick if nothing has blocked for a while.
|
|
1287
|
+
void yieldToIdleIfStarved()
|
|
1288
|
+
{
|
|
1289
|
+
const std::int64_t now = esp_timer_get_time();
|
|
1290
|
+
if (now - g_lastBlockUs < 500000) return;
|
|
1291
|
+
g_lastBlockUs = now;
|
|
1292
|
+
vTaskDelay(1);
|
|
1293
|
+
}
|
|
1294
|
+
|
|
171
1295
|
#if GEA_BOARD_HAS_EXPANDER
|
|
172
1296
|
constexpr int kExpanderBacklightPin = gea::platform::board::expander.backlight;
|
|
173
1297
|
constexpr int kExpanderDisplayResetPin = gea::platform::board::expander.displayReset;
|
|
@@ -195,12 +1319,14 @@ esp_err_t updateBacklightDuty()
|
|
|
195
1319
|
return ESP_OK;
|
|
196
1320
|
}
|
|
197
1321
|
|
|
198
|
-
// An expander line is on/off
|
|
1322
|
+
// An expander line is on/off (any non-zero brightness lights the panel) unless
|
|
1323
|
+
// the expander carries a level on it, as a board-control MCU does.
|
|
199
1324
|
esp_err_t applyBacklight()
|
|
200
1325
|
{
|
|
201
1326
|
#if GEA_BOARD_HAS_EXPANDER
|
|
202
1327
|
if constexpr (kExpanderBacklightPin >= 0) {
|
|
203
1328
|
auto &expander = gea::platform::esp32::chip_bindings::expanders::ioExpander();
|
|
1329
|
+
if (expander.setLevel(kExpanderBacklightPin, g_brightness)) return ESP_OK;
|
|
204
1330
|
return expander.writePin(kExpanderBacklightPin, g_brightness > 0) ? ESP_OK : ESP_FAIL;
|
|
205
1331
|
}
|
|
206
1332
|
#endif
|
|
@@ -307,6 +1433,35 @@ esp_err_t initRgbController()
|
|
|
307
1433
|
return ESP_OK;
|
|
308
1434
|
}
|
|
309
1435
|
|
|
1436
|
+
struct PanelCreate {
|
|
1437
|
+
esp_lcd_rgb_panel_config_t *config;
|
|
1438
|
+
esp_err_t err;
|
|
1439
|
+
int core; // where the driver installed its interrupts (the calling core)
|
|
1440
|
+
SemaphoreHandle_t done;
|
|
1441
|
+
};
|
|
1442
|
+
|
|
1443
|
+
void createPanel(PanelCreate &c)
|
|
1444
|
+
{
|
|
1445
|
+
c.core = static_cast<int>(xPortGetCoreID());
|
|
1446
|
+
c.err = esp_lcd_new_rgb_panel(c.config, &g_panel);
|
|
1447
|
+
// Bounce buffers live in internal DMA RAM, which other services share: when
|
|
1448
|
+
// the preferred size does not fit, step down to the next whole-row divisor
|
|
1449
|
+
// rather than leaving the panel dark.
|
|
1450
|
+
while (c.err == ESP_ERR_NO_MEM && c.config->bounce_buffer_size_px > static_cast<std::size_t>(kWidth)) {
|
|
1451
|
+
int rows = static_cast<int>(c.config->bounce_buffer_size_px / kWidth) - 1;
|
|
1452
|
+
while (rows > 1 && kHeight % rows != 0) --rows;
|
|
1453
|
+
ESP_LOGW(kTag, "no room for %d-row bounce buffers; trying %d", int(c.config->bounce_buffer_size_px / kWidth), rows);
|
|
1454
|
+
c.config->bounce_buffer_size_px = static_cast<std::size_t>(kWidth) * rows;
|
|
1455
|
+
c.err = esp_lcd_new_rgb_panel(c.config, &g_panel);
|
|
1456
|
+
}
|
|
1457
|
+
while (c.err != ESP_OK && c.config->num_fbs > 1) {
|
|
1458
|
+
ESP_LOGW(kTag, "no room for %d scanout buffers (%s); trying %d", int(c.config->num_fbs),
|
|
1459
|
+
esp_err_to_name(c.err), int(c.config->num_fbs) - 1);
|
|
1460
|
+
c.config->num_fbs -= 1;
|
|
1461
|
+
c.err = esp_lcd_new_rgb_panel(c.config, &g_panel);
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
|
|
310
1465
|
esp_err_t initRgbPanel()
|
|
311
1466
|
{
|
|
312
1467
|
const auto &display = gea::platform::board::display;
|
|
@@ -325,17 +1480,30 @@ esp_err_t initRgbPanel()
|
|
|
325
1480
|
config.data_width = 16;
|
|
326
1481
|
config.in_color_format = LCD_COLOR_FMT_RGB565;
|
|
327
1482
|
config.out_color_format = LCD_COLOR_FMT_RGB565;
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
// PSRAM framebuffer directly (no CPU in the scanout path); risk is scanout
|
|
333
|
-
// underrun (visible drift) when CPU PSRAM traffic starves the DMA.
|
|
334
|
-
#ifndef GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
|
|
335
|
-
#define GEA_EMBEDDED_DISPLAY_RGB_BOUNCE 1
|
|
1483
|
+
#ifdef GEA_EMBEDDED_DISPLAY_RGB_SCAN_FBS
|
|
1484
|
+
config.num_fbs = GEA_EMBEDDED_DISPLAY_RGB_SWAP_FBS ? GEA_EMBEDDED_DISPLAY_RGB_SCAN_FBS : 1;
|
|
1485
|
+
#else
|
|
1486
|
+
config.num_fbs = GEA_EMBEDDED_DISPLAY_RGB_SWAP_FBS ? gea::platform::board::display.scanFramebuffers : 1;
|
|
336
1487
|
#endif
|
|
337
1488
|
#if GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
|
|
338
|
-
config.bounce_buffer_size_px =
|
|
1489
|
+
config.bounce_buffer_size_px = static_cast<std::size_t>(kWidth) * bounceBufferRows();
|
|
1490
|
+
// This file fills the bounce buffers itself (onBounceEmpty) so it can apply
|
|
1491
|
+
// the ring-scroll mapping, which the driver only allows when it owns no
|
|
1492
|
+
// framebuffer: the scanout buffers are allocated below instead.
|
|
1493
|
+
const int wantFbs = static_cast<int>(config.num_fbs > 0 ? config.num_fbs : 1);
|
|
1494
|
+
config.num_fbs = 0;
|
|
1495
|
+
config.flags.no_fb = 1;
|
|
1496
|
+
int ownFbs = 0;
|
|
1497
|
+
for (; ownFbs < wantFbs; ++ownFbs) {
|
|
1498
|
+
g_scanFb[ownFbs] = static_cast<std::uint16_t *>(heap_caps_aligned_alloc(64, kFramebufferBytes, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
1499
|
+
if (!g_scanFb[ownFbs]) break;
|
|
1500
|
+
std::memset(g_scanFb[ownFbs], 0, kFramebufferBytes);
|
|
1501
|
+
}
|
|
1502
|
+
if (ownFbs == 0) {
|
|
1503
|
+
ESP_LOGE(kTag, "no PSRAM for a scanout buffer");
|
|
1504
|
+
return ESP_ERR_NO_MEM;
|
|
1505
|
+
}
|
|
1506
|
+
if (ownFbs < wantFbs) ESP_LOGW(kTag, "no room for %d scanout buffers; using %d", wantFbs, ownFbs);
|
|
339
1507
|
#else
|
|
340
1508
|
config.bounce_buffer_size_px = 0;
|
|
341
1509
|
#endif
|
|
@@ -348,24 +1516,87 @@ esp_err_t initRgbPanel()
|
|
|
348
1516
|
for (int i = 0; i < 16; ++i) config.data_gpio_nums[i] = display.data[i];
|
|
349
1517
|
config.flags.fb_in_psram = 1;
|
|
350
1518
|
|
|
351
|
-
|
|
1519
|
+
// The driver allocates its GDMA EOF and LCD interrupts on the calling core;
|
|
1520
|
+
// commitSwap() needs to know which one that is.
|
|
1521
|
+
PanelCreate create = {&config, ESP_FAIL, -1, nullptr};
|
|
1522
|
+
#if GEA_EMBEDDED_DISPLAY_RGB_ISR_CORE >= 0
|
|
1523
|
+
// Install the panel's interrupts (the bounce-buffer copy ISR) on a chosen
|
|
1524
|
+
// core, off the runtime's, by creating the panel from a task pinned there.
|
|
1525
|
+
create.done = xSemaphoreCreateBinary();
|
|
1526
|
+
if (create.done &&
|
|
1527
|
+
xTaskCreatePinnedToCore([](void *arg) {
|
|
1528
|
+
auto *c = static_cast<PanelCreate *>(arg);
|
|
1529
|
+
createPanel(*c);
|
|
1530
|
+
xSemaphoreGive(c->done);
|
|
1531
|
+
vTaskDelete(nullptr);
|
|
1532
|
+
}, "rgb_init", 4096, &create, configMAX_PRIORITIES - 2, nullptr, GEA_EMBEDDED_DISPLAY_RGB_ISR_CORE) == pdPASS) {
|
|
1533
|
+
xSemaphoreTake(create.done, portMAX_DELAY);
|
|
1534
|
+
} else {
|
|
1535
|
+
createPanel(create);
|
|
1536
|
+
}
|
|
1537
|
+
if (create.done) vSemaphoreDelete(create.done);
|
|
1538
|
+
#else
|
|
1539
|
+
createPanel(create);
|
|
1540
|
+
#endif
|
|
1541
|
+
g_panelIsrCore = create.core;
|
|
1542
|
+
esp_err_t err = create.err;
|
|
1543
|
+
ESP_LOGI(kTag, "panel interrupts on core %d", g_panelIsrCore);
|
|
352
1544
|
if (err != ESP_OK) {
|
|
353
1545
|
ESP_LOGE(kTag, "failed to create RGB panel: %s", esp_err_to_name(err));
|
|
354
1546
|
return err;
|
|
355
1547
|
}
|
|
1548
|
+
#if GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
|
|
1549
|
+
g_scanFbCount = ownFbs;
|
|
1550
|
+
{
|
|
1551
|
+
ESP_LOGI(kTag, "bounce buffers: %d rows", static_cast<int>(config.bounce_buffer_size_px / kWidth));
|
|
1552
|
+
}
|
|
1553
|
+
#else
|
|
1554
|
+
g_scanFbCount = static_cast<int>(config.num_fbs);
|
|
1555
|
+
#endif
|
|
1556
|
+
g_swapFbs = g_scanFbCount > 1;
|
|
1557
|
+
if (g_swapFbs || GEA_EMBEDDED_DISPLAY_RGB_BOUNCE) {
|
|
1558
|
+
if (g_swapFbs) {
|
|
1559
|
+
g_frameSem = xSemaphoreCreateBinary();
|
|
1560
|
+
g_readyMutex = xSemaphoreCreateMutex();
|
|
1561
|
+
if (!g_frameSem || !g_readyMutex) return ESP_ERR_NO_MEM;
|
|
1562
|
+
}
|
|
1563
|
+
esp_lcd_rgb_panel_event_callbacks_t callbacks = {};
|
|
1564
|
+
callbacks.on_frame_buf_complete = onPanelFrameComplete;
|
|
1565
|
+
callbacks.on_vsync = onPanelVsync;
|
|
1566
|
+
#if GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
|
|
1567
|
+
callbacks.on_bounce_empty = onBounceEmpty;
|
|
1568
|
+
#endif
|
|
1569
|
+
err = esp_lcd_rgb_panel_register_event_callbacks(g_panel, &callbacks, nullptr);
|
|
1570
|
+
if (err != ESP_OK) {
|
|
1571
|
+
ESP_LOGE(kTag, "failed to register RGB frame callback: %s", esp_err_to_name(err));
|
|
1572
|
+
return err;
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
356
1575
|
err = esp_lcd_panel_reset(g_panel);
|
|
357
1576
|
if (err != ESP_OK) return err;
|
|
358
1577
|
err = esp_lcd_panel_init(g_panel);
|
|
359
1578
|
if (err != ESP_OK) return err;
|
|
360
1579
|
|
|
361
|
-
|
|
362
|
-
|
|
1580
|
+
#if !GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
|
|
1581
|
+
void *fb0 = nullptr;
|
|
1582
|
+
void *fb1 = nullptr;
|
|
1583
|
+
void *fb2 = nullptr;
|
|
1584
|
+
err = g_scanFbCount >= 3 ? esp_lcd_rgb_panel_get_frame_buffer(g_panel, 3, &fb0, &fb1, &fb2)
|
|
1585
|
+
: g_scanFbCount == 2 ? esp_lcd_rgb_panel_get_frame_buffer(g_panel, 2, &fb0, &fb1)
|
|
1586
|
+
: esp_lcd_rgb_panel_get_frame_buffer(g_panel, 1, &fb0);
|
|
363
1587
|
if (err != ESP_OK) {
|
|
364
1588
|
ESP_LOGE(kTag, "failed to get RGB framebuffer: %s", esp_err_to_name(err));
|
|
365
1589
|
return err;
|
|
366
1590
|
}
|
|
367
|
-
|
|
368
|
-
|
|
1591
|
+
g_scanFb[0] = static_cast<std::uint16_t *>(fb0);
|
|
1592
|
+
g_scanFb[1] = static_cast<std::uint16_t *>(fb1);
|
|
1593
|
+
g_scanFb[2] = static_cast<std::uint16_t *>(fb2);
|
|
1594
|
+
#endif
|
|
1595
|
+
g_ringScroll = GEA_EMBEDDED_DISPLAY_RGB_BOUNCE && g_swapFbs;
|
|
1596
|
+
g_frontIndex = 0;
|
|
1597
|
+
g_drawIndex = g_scanFbCount > 1 ? 1 : 0;
|
|
1598
|
+
g_framebuffer = g_scanFb[0];
|
|
1599
|
+
if (!g_framebuffer || (g_swapFbs && !g_scanFb[1])) return ESP_ERR_NO_MEM;
|
|
369
1600
|
|
|
370
1601
|
// Double buffer: the panel framebuffer (g_framebuffer) is continuously scanned out,
|
|
371
1602
|
// so drawing directly into it exposes every intermediate step (e.g. a tick's
|
|
@@ -373,16 +1604,23 @@ esp_err_t initRgbPanel()
|
|
|
373
1604
|
// PSRAM back buffer instead; publishDrawRect() copies the fully-composited dirty
|
|
374
1605
|
// region into the scanout framebuffer in one shot, so the panel only ever sees
|
|
375
1606
|
// finished pixels. Falls back to single-buffered direct draw if PSRAM is exhausted.
|
|
376
|
-
|
|
377
|
-
|
|
1607
|
+
// With swapped scanout buffers the renderer draws straight into the back one.
|
|
1608
|
+
g_drawFramebuffer = g_swapFbs ? g_scanFb[1]
|
|
1609
|
+
: static_cast<std::uint16_t *>(heap_caps_aligned_alloc(
|
|
1610
|
+
64, kFramebufferBytes, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
378
1611
|
if (!g_drawFramebuffer) {
|
|
379
1612
|
ESP_LOGW(kTag, "no PSRAM for back buffer; falling back to single-buffered draw");
|
|
380
1613
|
g_drawFramebuffer = g_framebuffer;
|
|
1614
|
+
g_swapFbs = false; // drawing straight into the front buffer: nothing to swap
|
|
381
1615
|
}
|
|
382
1616
|
|
|
383
1617
|
std::memset(g_framebuffer, 0, kFramebufferBytes);
|
|
384
1618
|
if (g_drawFramebuffer != g_framebuffer) std::memset(g_drawFramebuffer, 0, kFramebufferBytes);
|
|
385
|
-
|
|
1619
|
+
cacheWritebackAll();
|
|
1620
|
+
for (int i = 1; i < g_scanFbCount; ++i) {
|
|
1621
|
+
std::memset(g_scanFb[i], 0, kFramebufferBytes);
|
|
1622
|
+
cacheWritebackRect(g_scanFb[i], 0, 0, kWidth - 1, kHeight - 1);
|
|
1623
|
+
}
|
|
386
1624
|
g_canvas.bindPixels(g_drawFramebuffer, kWidth, kHeight);
|
|
387
1625
|
g_workerCanvas.bindPixels(g_drawFramebuffer, kWidth, kHeight);
|
|
388
1626
|
|
|
@@ -390,17 +1628,21 @@ esp_err_t initRgbPanel()
|
|
|
390
1628
|
// Internal SRAM (not PSRAM): the whole point is to raster off the scanout bus.
|
|
391
1629
|
// 16-byte aligned: rows are 960B (16-multiple), so an aligned base keeps every
|
|
392
1630
|
// staged-replay row 16-aligned — maximal PIE-SIMD span coverage per scanline.
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
1631
|
+
allocStripBuffer(0);
|
|
1632
|
+
ESP_LOGI(kTag, "strip-raster %s strip_buf=%p rows=%d halves=%d (internal free=%u largest=%u)",
|
|
1633
|
+
g_stripBuffer ? "ON" : "OFF (no internal SRAM)", g_stripBuffer, g_stripRows, g_stripHalves,
|
|
1634
|
+
static_cast<unsigned>(heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)),
|
|
1635
|
+
static_cast<unsigned>(heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)));
|
|
397
1636
|
#endif
|
|
398
|
-
ESP_LOGI(kTag, "RGB panel ready %dx%d pclk=%d bounce_rows=%d fb=%p draw_fb=%p single_fb=%d row_bytes=%u fb_alignment64=%u vsync_restart=%d",
|
|
1637
|
+
ESP_LOGI(kTag, "RGB panel ready %dx%d pclk=%d bounce_rows=%d fb=%p fb2=%p swap=%d isr_core=%d draw_fb=%p single_fb=%d row_bytes=%u fb_alignment64=%u vsync_restart=%d",
|
|
399
1638
|
kWidth,
|
|
400
1639
|
kHeight,
|
|
401
1640
|
kPixelClockHz,
|
|
402
1641
|
int(config.bounce_buffer_size_px / kWidth),
|
|
403
1642
|
g_framebuffer,
|
|
1643
|
+
g_scanFb[1],
|
|
1644
|
+
g_swapFbs ? 1 : 0,
|
|
1645
|
+
g_panelIsrCore,
|
|
404
1646
|
g_drawFramebuffer,
|
|
405
1647
|
g_framebuffer == g_drawFramebuffer ? 1 : 0,
|
|
406
1648
|
static_cast<unsigned>(kWidth * sizeof(std::uint16_t)),
|
|
@@ -410,6 +1652,7 @@ esp_err_t initRgbPanel()
|
|
|
410
1652
|
#else
|
|
411
1653
|
0);
|
|
412
1654
|
#endif
|
|
1655
|
+
applyVSync(); // the app may have asked before the panel was up
|
|
413
1656
|
return ESP_OK;
|
|
414
1657
|
}
|
|
415
1658
|
|
|
@@ -527,11 +1770,21 @@ void replayPresentCommand(const gea::platform::display::DisplayPresentCommand &c
|
|
|
527
1770
|
break;
|
|
528
1771
|
case Type::FillText:
|
|
529
1772
|
g_canvas.setGlobalAlpha(command.fillText.alpha);
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
1773
|
+
// A baked family draws in that font, as display_present.h's replay
|
|
1774
|
+
// does; only a family-less command takes the built-in bitmap font.
|
|
1775
|
+
if (command.fillText.fontFamilyId >= 0)
|
|
1776
|
+
g_canvas.drawTextFontFamily(command.fillText.text,
|
|
1777
|
+
command.fillText.x,
|
|
1778
|
+
command.fillText.y,
|
|
1779
|
+
command.fillText.color,
|
|
1780
|
+
command.fillText.fontFamilyId,
|
|
1781
|
+
command.fillText.fontSizePx);
|
|
1782
|
+
else
|
|
1783
|
+
g_canvas.drawText(command.fillText.text,
|
|
1784
|
+
command.fillText.x,
|
|
1785
|
+
command.fillText.y,
|
|
1786
|
+
command.fillText.color,
|
|
1787
|
+
command.fillText.scale);
|
|
535
1788
|
break;
|
|
536
1789
|
}
|
|
537
1790
|
}
|
|
@@ -555,10 +1808,18 @@ bool clipRect(int *x0, int *y0, int *x1, int *y1)
|
|
|
555
1808
|
if (outY1) *outY1 = y1;
|
|
556
1809
|
const int width = x1 - x0 + 1;
|
|
557
1810
|
if (g_framebuffer == g_drawFramebuffer) return width * (y1 - y0 + 1);
|
|
1811
|
+
if (g_swapFbs) {
|
|
1812
|
+
// Deferred: recorded against both scanout buffers, copied by publishSwap().
|
|
1813
|
+
markStale(x0, y0, x1, y1);
|
|
1814
|
+
return width * (y1 - y0 + 1);
|
|
1815
|
+
}
|
|
1816
|
+
const std::int64_t c0 = esp_timer_get_time();
|
|
558
1817
|
for (int y = y0; y <= y1; ++y) {
|
|
559
1818
|
const std::size_t offset = static_cast<std::size_t>(y) * kWidth + static_cast<std::size_t>(x0);
|
|
560
1819
|
std::memcpy(&g_framebuffer[offset], &g_drawFramebuffer[offset], static_cast<std::size_t>(width) * sizeof(std::uint16_t));
|
|
1820
|
+
forgetRowSumAt(&g_framebuffer[offset]);
|
|
561
1821
|
}
|
|
1822
|
+
g_swapDiag.copyUs += esp_timer_get_time() - c0;
|
|
562
1823
|
return width * (y1 - y0 + 1);
|
|
563
1824
|
}
|
|
564
1825
|
|
|
@@ -577,6 +1838,7 @@ struct RenderRowWorker {
|
|
|
577
1838
|
};
|
|
578
1839
|
|
|
579
1840
|
RenderRowWorker g_renderRowWorker;
|
|
1841
|
+
std::atomic<bool> g_renderWorkerBlockIdle{false};
|
|
580
1842
|
// 12KB (was 8KB for the memcpy-only strip copies): the worker now also runs the
|
|
581
1843
|
// renderer's full band replay (gradient projection math + rounded-rect batch +
|
|
582
1844
|
// command handlers), which is much deeper than a strip copy.
|
|
@@ -598,7 +1860,10 @@ void renderRowWorkerTask(void *)
|
|
|
598
1860
|
std::int64_t idleSince = esp_timer_get_time();
|
|
599
1861
|
for (;;) {
|
|
600
1862
|
while (g_renderRowWorker.jobSeq.load(std::memory_order_acquire) == seen) {
|
|
601
|
-
|
|
1863
|
+
// Every submit notifies, so blocking at once loses no job; spinning
|
|
1864
|
+
// first only shaves the wake-up, and starves a lower-priority task
|
|
1865
|
+
// that shares this core (gea_render_worker_block_when_idle).
|
|
1866
|
+
if (g_renderWorkerBlockIdle.load(std::memory_order_relaxed) || esp_timer_get_time() - idleSince > 4000) {
|
|
602
1867
|
ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(100));
|
|
603
1868
|
idleSince = esp_timer_get_time();
|
|
604
1869
|
}
|
|
@@ -616,14 +1881,21 @@ void renderRowWorkerTask(void *)
|
|
|
616
1881
|
#if GEA_EMBEDDED_DISPLAY_RGB_STRIP_RASTER
|
|
617
1882
|
// Runs on the worker core: copy a finished SRAM strip (ctx) to PSRAM scanout rows
|
|
618
1883
|
// [y0,y1] and write it back so the panel's scanout DMA sees it.
|
|
1884
|
+
// Where presentStripFused publishes: the live buffer on a single-buffer panel,
|
|
1885
|
+
// the off-screen back buffer when scanout buffers swap.
|
|
1886
|
+
std::uint16_t *g_stripTarget = nullptr;
|
|
1887
|
+
|
|
619
1888
|
void copyStripToScanoutThunk(void *ctx, int y0, int y1)
|
|
620
1889
|
{
|
|
621
1890
|
const std::uint16_t *src = static_cast<const std::uint16_t *>(ctx);
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
1891
|
+
// Row by row through the scanout guard: one memcpy of the whole strip left
|
|
1892
|
+
// up to ~38 KB of dirty lines in the 64 KB cache for the next refill to
|
|
1893
|
+
// evict inside its window (the overruns on every full-screen redraw, such
|
|
1894
|
+
// as a tab switch).
|
|
1895
|
+
for (int y = y0; y <= y1; ++y)
|
|
1896
|
+
scanoutRowCopy(&g_stripTarget[static_cast<std::size_t>(y) * kWidth],
|
|
1897
|
+
&src[static_cast<std::size_t>(y - y0) * kWidth], kWidth * sizeof(std::uint16_t));
|
|
1898
|
+
cacheWritebackRect(g_stripTarget, 0, y0, kWidth - 1, y1);
|
|
627
1899
|
}
|
|
628
1900
|
|
|
629
1901
|
// Compose the whole frame strip-by-strip in internal SRAM, copying each finished
|
|
@@ -638,16 +1910,35 @@ void copyStripToScanoutThunk(void *ctx, int y0, int y1)
|
|
|
638
1910
|
// copy instead of adding to it.
|
|
639
1911
|
bool presentStripFused(const present::Frame &frame)
|
|
640
1912
|
{
|
|
641
|
-
if (!g_framebuffer || !g_stripBuffer) return false;
|
|
642
|
-
int stripRows = g_flushRows > 0 ? g_flushRows :
|
|
643
|
-
if (stripRows >
|
|
1913
|
+
if (!g_framebuffer || !g_stripBuffer || g_stripHalves < 2) return false;
|
|
1914
|
+
int stripRows = g_flushRows > 0 ? g_flushRows : g_stripRows;
|
|
1915
|
+
if (stripRows > g_stripRows) stripRows = g_stripRows;
|
|
644
1916
|
const std::int64_t start = esp_timer_get_time();
|
|
1917
|
+
int back = 0;
|
|
1918
|
+
if (g_swapFbs) {
|
|
1919
|
+
// The whole frame lands in the back buffer, so its pending regions are moot:
|
|
1920
|
+
// wait for the panel to release it, but patch nothing.
|
|
1921
|
+
back = g_drawIndex;
|
|
1922
|
+
if (!g_backReady) {
|
|
1923
|
+
waitBackBufferFree();
|
|
1924
|
+
g_stale[back].count = 0;
|
|
1925
|
+
g_backReady = true;
|
|
1926
|
+
}
|
|
1927
|
+
// Every row is written below, so the buffer can take the region's identity
|
|
1928
|
+
// layout (offset 0) for free.
|
|
1929
|
+
g_map[back] = ScanMap{g_regionY, g_regionH, 0};
|
|
1930
|
+
g_pendingRot[back] = 0;
|
|
1931
|
+
g_mapReset[back] = false;
|
|
1932
|
+
g_stripTarget = g_scanFb[back];
|
|
1933
|
+
} else {
|
|
1934
|
+
g_stripTarget = g_framebuffer;
|
|
1935
|
+
}
|
|
645
1936
|
bool inFlight = false;
|
|
646
1937
|
int idx = 0;
|
|
647
1938
|
for (int row = 0; row < kHeight; row += stripRows, ++idx) {
|
|
648
1939
|
int rows = stripRows;
|
|
649
1940
|
if (row + rows > kHeight) rows = kHeight - row;
|
|
650
|
-
std::uint16_t *buf = g_stripBuffer + static_cast<std::size_t>(idx & 1) *
|
|
1941
|
+
std::uint16_t *buf = g_stripBuffer + static_cast<std::size_t>(idx & 1) * g_stripRows * kWidth;
|
|
651
1942
|
// Raster strip `idx` into its SRAM half — overlaps the worker copying the
|
|
652
1943
|
// previous strip out of the OTHER half (different buffer + bus, no contention).
|
|
653
1944
|
present::rasterFrameRowsStrided(buf, kWidth, 0, kWidth, row, rows, kHeight, frame);
|
|
@@ -661,8 +1952,24 @@ bool presentStripFused(const present::Frame &frame)
|
|
|
661
1952
|
}
|
|
662
1953
|
}
|
|
663
1954
|
if (inFlight) gea_render_parallel_wait();
|
|
664
|
-
|
|
1955
|
+
if (g_swapFbs) {
|
|
1956
|
+
g_stale[back].count = 0;
|
|
1957
|
+
g_publishPending = false;
|
|
1958
|
+
commitSwap(back);
|
|
1959
|
+
// The new back buffer lags by a whole frame; a later partial frame patches
|
|
1960
|
+
// it in full once (a following full-frame present needs no patch at all).
|
|
1961
|
+
for (int i = 0; i < g_scanFbCount; ++i) {
|
|
1962
|
+
if (i == back) continue;
|
|
1963
|
+
g_stale[i].count = 0;
|
|
1964
|
+
staleAdd(g_stale[i], 0, 0, kWidth - 1, kHeight - 1);
|
|
1965
|
+
g_mapReset[i] = true;
|
|
1966
|
+
g_pendingRot[i] = 0;
|
|
1967
|
+
}
|
|
1968
|
+
} else {
|
|
1969
|
+
g_drawBufferStale = (g_drawFramebuffer != g_framebuffer);
|
|
1970
|
+
}
|
|
665
1971
|
addFlushStats(esp_timer_get_time() - start, kWidth * kHeight);
|
|
1972
|
+
noteFrameDiag(3, esp_timer_get_time() - start, kWidth * kHeight);
|
|
666
1973
|
return true;
|
|
667
1974
|
}
|
|
668
1975
|
#endif
|
|
@@ -703,13 +2010,28 @@ bool submitRenderWorkerJob(void (*fn)(void *, int, int), void *ctx, int y0, int
|
|
|
703
2010
|
workerCore,
|
|
704
2011
|
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
|
705
2012
|
#else
|
|
706
|
-
|
|
2013
|
+
BaseType_t created = xTaskCreatePinnedToCore(renderRowWorkerTask,
|
|
707
2014
|
"gea_rowwrk",
|
|
708
2015
|
kRenderRowWorkerStackBytes,
|
|
709
2016
|
nullptr,
|
|
710
2017
|
priority,
|
|
711
2018
|
&g_renderRowWorker.task,
|
|
712
2019
|
workerCore);
|
|
2020
|
+
#endif
|
|
2021
|
+
#if !GEA_EMBEDDED_RENDER_WORKER_STACK_EXTERNAL
|
|
2022
|
+
if (created != pdPASS || !g_renderRowWorker.task) {
|
|
2023
|
+
// Internal RAM is tight on an RGB board (bounce buffers, strip staging);
|
|
2024
|
+
// the stack holds no DMA buffers, so PSRAM is a correct second choice.
|
|
2025
|
+
g_renderRowWorker.task = nullptr;
|
|
2026
|
+
created = xTaskCreatePinnedToCoreWithCaps(renderRowWorkerTask,
|
|
2027
|
+
"gea_rowwrk",
|
|
2028
|
+
kRenderRowWorkerStackBytes,
|
|
2029
|
+
nullptr,
|
|
2030
|
+
priority,
|
|
2031
|
+
&g_renderRowWorker.task,
|
|
2032
|
+
workerCore,
|
|
2033
|
+
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
|
2034
|
+
}
|
|
713
2035
|
#endif
|
|
714
2036
|
if (created != pdPASS || !g_renderRowWorker.task) {
|
|
715
2037
|
ESP_LOGW(kTag, "row worker create failed: create=%d stack=%u",
|
|
@@ -735,8 +2057,16 @@ bool submitRenderWorkerJob(void (*fn)(void *, int, int), void *ctx, int y0, int
|
|
|
735
2057
|
|
|
736
2058
|
} // namespace
|
|
737
2059
|
|
|
2060
|
+
// A task that shares the worker's core at a lower priority (gea3d's frame
|
|
2061
|
+
// build) asks the worker to sleep between jobs instead of spinning.
|
|
2062
|
+
extern "C" void gea_render_worker_block_when_idle(bool on)
|
|
2063
|
+
{
|
|
2064
|
+
g_renderWorkerBlockIdle.store(on, std::memory_order_relaxed);
|
|
2065
|
+
}
|
|
2066
|
+
|
|
738
2067
|
extern "C" bool gea_render_parallel_rows_submit(void (*fn)(void *, int, int), void *ctx, int y0, int y1)
|
|
739
2068
|
{
|
|
2069
|
+
if (g_scrollCopyInFlight) return false; // the worker is copying a scroll
|
|
740
2070
|
g_lastSubmitWasDrawBand = false;
|
|
741
2071
|
return submitRenderWorkerJob(fn, ctx, y0, y1);
|
|
742
2072
|
}
|
|
@@ -753,7 +2083,9 @@ extern "C" bool gea_render_parallel_submit(void (*fn)(void *, int, int), void *c
|
|
|
753
2083
|
// Record the render core so Display draw calls on the OTHER core (the worker's
|
|
754
2084
|
// band replay) route to the band canvas. Re-set each submit in case the frame
|
|
755
2085
|
// task ever migrates cores.
|
|
2086
|
+
if (g_scrollCopyInFlight) return false; // the worker is copying a scroll
|
|
756
2087
|
g_renderCoreId = static_cast<int>(xPortGetCoreID());
|
|
2088
|
+
ensureBackReady(); // the band draws into the back buffer
|
|
757
2089
|
const bool ok = submitRenderWorkerJob(fn, ctx, y0, y1);
|
|
758
2090
|
g_lastSubmitWasDrawBand = ok;
|
|
759
2091
|
return ok;
|
|
@@ -784,6 +2116,9 @@ struct ReplayStageState {
|
|
|
784
2116
|
bool hadDirty = false; // canvas dirty union accumulated before the
|
|
785
2117
|
int dx0 = 0, dy0 = 0, dx1 = -1, dy1 = -1; // rebind (bindPixels resets it)
|
|
786
2118
|
std::uint8_t alpha = 255;
|
|
2119
|
+
bool unseeded = false; // the window was not seeded: every pixel must be copied back
|
|
2120
|
+
int stride = 0; // strip row stride in pixels: the window width, 16-byte rounded
|
|
2121
|
+
std::uint16_t *strip = nullptr; // the window's buffer (a strip half or a pipeline slot)
|
|
787
2122
|
// DIAG: per-strip cost decomposition (seed copy-in / replay body / copy-back),
|
|
788
2123
|
// printed every kStageDiagStrips strips per core.
|
|
789
2124
|
std::int64_t beginDoneUs = 0;
|
|
@@ -809,15 +2144,228 @@ volatile int g_pieCount8 = 0;
|
|
|
809
2144
|
#define GEA_EMBEDDED_DISPLAY_REPLAY_SRAM_STAGE 0
|
|
810
2145
|
#endif
|
|
811
2146
|
|
|
812
|
-
|
|
2147
|
+
// Direct replay is about to repaint `rects` completely (see tree_render): ready
|
|
2148
|
+
// the back buffer now, patching only the stale area outside them.
|
|
2149
|
+
extern "C" void gea_display_prepare_repaint(const gea::platform::display::DisplayFlushRect *rects, int count)
|
|
2150
|
+
{
|
|
2151
|
+
if (rects && count > 0) {
|
|
2152
|
+
g_swapDiag.prepares++;
|
|
2153
|
+
for (int i = 0; i < count; ++i) g_swapDiag.preparePx += (rects[i].x1 - rects[i].x0 + 1) * (rects[i].y1 - rects[i].y0 + 1);
|
|
2154
|
+
ensureBackReady(rects, count);
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
|
|
2158
|
+
// In-place frames: an app that repaints every pixel each frame can render
|
|
2159
|
+
// straight into the swap buffers instead of into its own frame that present()
|
|
2160
|
+
// then copies over (a whole frame of PSRAM reads and writes, ~40 ms at 800x480).
|
|
2161
|
+
// Only with swapped buffers laid out as plain rows (no ring-scroll region): the
|
|
2162
|
+
// app writes row-major pixels.
|
|
2163
|
+
static bool panelBuffersUsable()
|
|
2164
|
+
{
|
|
2165
|
+
return g_swapFbs && g_scanFbCount >= 2 && g_regionH == 0;
|
|
2166
|
+
}
|
|
2167
|
+
|
|
2168
|
+
extern "C" int gea_display_panel_buffer_count(int *width, int *height)
|
|
2169
|
+
{
|
|
2170
|
+
if (width) *width = kWidth;
|
|
2171
|
+
if (height) *height = kHeight;
|
|
2172
|
+
return panelBuffersUsable() ? g_scanFbCount : 0;
|
|
2173
|
+
}
|
|
2174
|
+
|
|
2175
|
+
extern "C" std::uint16_t *gea_display_panel_buffer(int index)
|
|
2176
|
+
{
|
|
2177
|
+
return index >= 0 && index < g_scanFbCount ? g_scanFb[index] : nullptr;
|
|
2178
|
+
}
|
|
2179
|
+
|
|
2180
|
+
// The buffer the next frame draws into, once the panel has released it. The
|
|
2181
|
+
// caller repaints all of it, so nothing is carried over from the front.
|
|
2182
|
+
extern "C" int gea_display_acquire_panel_buffer()
|
|
2183
|
+
{
|
|
2184
|
+
if (!panelBuffersUsable()) return -1;
|
|
2185
|
+
const DisplayFlushRect whole{0, 0, kWidth - 1, kHeight - 1};
|
|
2186
|
+
ensureBackReady(&whole, 1);
|
|
2187
|
+
g_drawBufferStale = false;
|
|
2188
|
+
return g_drawIndex;
|
|
2189
|
+
}
|
|
2190
|
+
|
|
2191
|
+
// State-only view of the draw canvas (dirty box, scroll region): no pixels move
|
|
2192
|
+
// through it, so it does not ready the back buffer -- the replay readies it once
|
|
2193
|
+
// it can name the rects it repaints whole (gea_display_prepare_repaint).
|
|
2194
|
+
extern "C" gea::framework::graphics::Canvas *gea_display_state_canvas()
|
|
2195
|
+
{
|
|
2196
|
+
return &stateCanvas();
|
|
2197
|
+
}
|
|
2198
|
+
|
|
2199
|
+
extern "C" int gea_replay_stage_rows()
|
|
813
2200
|
{
|
|
814
2201
|
#if GEA_EMBEDDED_DISPLAY_REPLAY_SRAM_STAGE
|
|
815
|
-
return g_stripBuffer ?
|
|
2202
|
+
return g_stripBuffer ? g_stripRows : 0;
|
|
816
2203
|
#else
|
|
817
2204
|
return 0;
|
|
818
2205
|
#endif
|
|
819
2206
|
}
|
|
820
2207
|
|
|
2208
|
+
// Whole-display-list replays clipped to a large region (a scroller's
|
|
2209
|
+
// overlay/extra repaint, the conservative full replay) are the bursts that
|
|
2210
|
+
// starve a bounce refill: tens of thousands of read-modify-written PSRAM
|
|
2211
|
+
// pixels with no guard. They stage through the strip even where the per-region
|
|
2212
|
+
// direct replay above does not (that one is cheap per strip and was measured
|
|
2213
|
+
// tear-free; these are rare, so the per-strip list walk costs little).
|
|
2214
|
+
extern "C" int gea_bulk_replay_stage_rows()
|
|
2215
|
+
{
|
|
2216
|
+
#if GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
|
|
2217
|
+
return g_stripBuffer ? g_stripRows : 0;
|
|
2218
|
+
#else
|
|
2219
|
+
return gea_replay_stage_rows();
|
|
2220
|
+
#endif
|
|
2221
|
+
}
|
|
2222
|
+
|
|
2223
|
+
namespace {
|
|
2224
|
+
// A staged window packs its rows at its own width, so a narrow window (a
|
|
2225
|
+
// scrollbar thumb's 10 px column) holds many more rows than a full-width strip:
|
|
2226
|
+
// one strip, one display-list walk, instead of one per 16 rows. Measured on the
|
|
2227
|
+
// Analytics tab: the 10x337 thumb repaint took ~5 ms as 21 strips (160 us list
|
|
2228
|
+
// walk + 116 us copy-back each).
|
|
2229
|
+
int stageStrideFor(int width)
|
|
2230
|
+
{
|
|
2231
|
+
int stride = (width + 7) & ~7;
|
|
2232
|
+
if (stride > kWidth) stride = kWidth;
|
|
2233
|
+
return stride < 8 ? 8 : stride;
|
|
2234
|
+
}
|
|
2235
|
+
// Pipelined staging (gea_replay_stage_pipeline): the main core replays into one
|
|
2236
|
+
// slot of the strip buffer while the worker core copies the previous slot back
|
|
2237
|
+
// into the framebuffer. The copy-back is PSRAM-write bound (~1.3 ms per 24-row
|
|
2238
|
+
// full-width strip, ~27 ms a full frame), the replay mostly SRAM work, so they
|
|
2239
|
+
// overlap instead of adding up. With one strip half the strip is split into
|
|
2240
|
+
// two half-height slots.
|
|
2241
|
+
bool g_stagePipeline = false;
|
|
2242
|
+
int g_pipeSlot = 0;
|
|
2243
|
+
bool g_pipeInFlight = false;
|
|
2244
|
+
// Slots the single strip half is cut into: 2, or 3 when the copy-back runs on
|
|
2245
|
+
// GDMA. A DMA copy is queued and left running, so the main core waits only
|
|
2246
|
+
// when the slot it is about to replay into is still being copied; a third
|
|
2247
|
+
// slot rides out strips that replay faster than their copy (cheap rows that
|
|
2248
|
+
// changed), which with two left the main core waiting ~12% of the frame.
|
|
2249
|
+
constexpr int kMaxPipeSlots = 3;
|
|
2250
|
+
int g_pipeSlots = 2;
|
|
2251
|
+
std::atomic<int> g_slotPending[kMaxPipeSlots];
|
|
2252
|
+
// Copy-back by GDMA, opt-in per app (gea.defines GEA_EMBEDDED_RGB_COPYBACK_DMA=1).
|
|
2253
|
+
// A transfer writes PSRAM in one burst whenever the engine gets to it, with no
|
|
2254
|
+
// way to hold it out of a bounce-buffer refill the way the CPU copy does
|
|
2255
|
+
// (waitOutRefill): partial UI repaints (charts, a colour picker) tear. A
|
|
2256
|
+
// full-screen 3D game redraws every frame and gains the worker core back.
|
|
2257
|
+
constexpr bool kCopyBackDma = GEA_EMBEDDED_RGB_COPYBACK_DMA != 0;
|
|
2258
|
+
bool copyDmaReady();
|
|
2259
|
+
void waitSlotCopied(int slot)
|
|
2260
|
+
{
|
|
2261
|
+
while (g_slotPending[slot].load(std::memory_order_acquire) != 0) {
|
|
2262
|
+
}
|
|
2263
|
+
}
|
|
2264
|
+
struct StageCopyJob {
|
|
2265
|
+
const std::uint16_t *strip; // the slot, addressed at (dx0, dy0)
|
|
2266
|
+
int stride; // slot row stride in pixels
|
|
2267
|
+
int x0; // first framebuffer column
|
|
2268
|
+
std::size_t rowBytes;
|
|
2269
|
+
int buffer; // scanout buffer index written
|
|
2270
|
+
bool sums; // full-width rows: skip the ones that repeat
|
|
2271
|
+
int slot; // pipeline slot the rows live in
|
|
2272
|
+
};
|
|
2273
|
+
|
|
2274
|
+
// Row repeats. A full repaint staged strip by strip (a 3D frame under the UI)
|
|
2275
|
+
// often leaves many rows exactly as the same buffer already holds them -- the
|
|
2276
|
+
// sky above Neon Drift's horizon, half the screen -- and the copy-back is
|
|
2277
|
+
// PSRAM-write bound (~84 us a full row). Per buffer and physical row: a hash of
|
|
2278
|
+
// what the last staged copy wrote there, trusted only while nothing else has
|
|
2279
|
+
// written the row since. Every other writer forgets: scanoutRowCopy (patches,
|
|
2280
|
+
// scroll and rect copies, strip publishes) per row, an unstaged replay
|
|
2281
|
+
// (gea_display_note_direct_draw), a frame flushed without a staged repaint, and
|
|
2282
|
+
// whole-buffer copies and rotations entirely. Hashing a row is ~7 us of
|
|
2283
|
+
// internal-RAM reads on the worker core.
|
|
2284
|
+
std::uint64_t *g_rowSum[kMaxScanFbs] = {nullptr, nullptr, nullptr};
|
|
2285
|
+
std::uint8_t *g_rowSumOk[kMaxScanFbs] = {nullptr, nullptr, nullptr};
|
|
2286
|
+
// A pipelined staged repaint ran since the last flush.
|
|
2287
|
+
bool g_frameStagedRepaint = false;
|
|
2288
|
+
|
|
2289
|
+
void forgetRowSums()
|
|
2290
|
+
{
|
|
2291
|
+
for (int b = 0; b < kMaxScanFbs; ++b)
|
|
2292
|
+
if (g_rowSumOk[b]) std::memset(g_rowSumOk[b], 0, kHeight);
|
|
2293
|
+
}
|
|
2294
|
+
|
|
2295
|
+
// Forgets the row a write to `dst` lands in, if it is in a scanout buffer.
|
|
2296
|
+
inline void forgetRowSumAt(const void *dst)
|
|
2297
|
+
{
|
|
2298
|
+
const auto *p = static_cast<const std::uint16_t *>(dst);
|
|
2299
|
+
for (int b = 0; b < kMaxScanFbs; ++b) {
|
|
2300
|
+
if (!g_rowSumOk[b] || p < g_scanFb[b] || p >= g_scanFb[b] + kPixelCount) continue;
|
|
2301
|
+
g_rowSumOk[b][(p - g_scanFb[b]) / kWidth] = 0;
|
|
2302
|
+
return;
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
|
|
2306
|
+
bool ensureRowSums(int buffer)
|
|
2307
|
+
{
|
|
2308
|
+
if (buffer < 0 || buffer >= kMaxScanFbs) return false;
|
|
2309
|
+
if (!g_rowSum[buffer]) g_rowSum[buffer] = static_cast<std::uint64_t *>(heap_caps_calloc(kHeight, sizeof(std::uint64_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
2310
|
+
if (!g_rowSumOk[buffer]) g_rowSumOk[buffer] = static_cast<std::uint8_t *>(heap_caps_calloc(kHeight, 1, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
2311
|
+
return g_rowSum[buffer] && g_rowSumOk[buffer];
|
|
2312
|
+
}
|
|
2313
|
+
|
|
2314
|
+
// Four independent multiply chains over interleaved words, so the in-order
|
|
2315
|
+
// core never waits on one multiply's result for the next.
|
|
2316
|
+
inline std::uint64_t rowSum(const std::uint16_t *row)
|
|
2317
|
+
{
|
|
2318
|
+
static_assert(kWidth % 8 == 0, "rows hash as groups of four words");
|
|
2319
|
+
const std::uint32_t *w = reinterpret_cast<const std::uint32_t *>(row);
|
|
2320
|
+
constexpr std::uint32_t kPrime = 0x01000193u;
|
|
2321
|
+
std::uint32_t a = 0x9E3779B9u, b = 0x85EBCA6Bu, c = 0xC2B2AE35u, d = 0x27D4EB2Fu;
|
|
2322
|
+
for (int i = 0; i < kWidth / 2; i += 4) {
|
|
2323
|
+
a = (a + w[i]) * kPrime;
|
|
2324
|
+
b = (b + w[i + 1]) * kPrime;
|
|
2325
|
+
c = (c + w[i + 2]) * kPrime;
|
|
2326
|
+
d = (d + w[i + 3]) * kPrime;
|
|
2327
|
+
}
|
|
2328
|
+
return (static_cast<std::uint64_t>((a * kPrime) ^ b) << 32) | ((c * kPrime) ^ d);
|
|
2329
|
+
}
|
|
2330
|
+
|
|
2331
|
+
// True when physical row `phys` of `buffer` already holds `src` (a full row);
|
|
2332
|
+
// otherwise records `src` as what the caller is about to write there.
|
|
2333
|
+
inline bool rowRepeats(int buffer, int phys, const std::uint16_t *src)
|
|
2334
|
+
{
|
|
2335
|
+
const std::uint64_t sum = rowSum(src);
|
|
2336
|
+
if (g_rowSumOk[buffer][phys] && g_rowSum[buffer][phys] == sum) return true;
|
|
2337
|
+
g_rowSum[buffer][phys] = sum;
|
|
2338
|
+
g_rowSumOk[buffer][phys] = 1;
|
|
2339
|
+
return false;
|
|
2340
|
+
}
|
|
2341
|
+
StageCopyJob g_stageCopyJobs[kMaxPipeSlots];
|
|
2342
|
+
int pipeSlotRows() { return g_stripHalves >= 2 ? g_stripRows : g_stripRows / g_pipeSlots; }
|
|
2343
|
+
std::uint16_t *pipeSlotBase(int slot) { return g_stripBuffer + static_cast<std::size_t>(slot) * pipeSlotRows() * kWidth; }
|
|
2344
|
+
int stageRowsFor(int width)
|
|
2345
|
+
{
|
|
2346
|
+
if (!g_stripBuffer || g_stripRows <= 0) return 0;
|
|
2347
|
+
const int slotRows = g_stagePipeline ? pipeSlotRows() : g_stripRows;
|
|
2348
|
+
const int rows = slotRows * kWidth / stageStrideFor(width);
|
|
2349
|
+
return rows < kHeight ? rows : kHeight;
|
|
2350
|
+
}
|
|
2351
|
+
} // namespace
|
|
2352
|
+
|
|
2353
|
+
// Rows a staged window of `width` pixels may span (0 = no staging).
|
|
2354
|
+
extern "C" int gea_bulk_replay_stage_rows_for_width(int width)
|
|
2355
|
+
{
|
|
2356
|
+
if (gea_bulk_replay_stage_rows() <= 0) return 0;
|
|
2357
|
+
return stageRowsFor(width);
|
|
2358
|
+
}
|
|
2359
|
+
|
|
2360
|
+
extern "C" bool gea_replay_stage_active()
|
|
2361
|
+
{
|
|
2362
|
+
return g_replayStage[xPortGetCoreID() & 1].active;
|
|
2363
|
+
}
|
|
2364
|
+
|
|
2365
|
+
// The next stages need no seed: the replay paints every pixel of them opaquely.
|
|
2366
|
+
bool g_stageSkipSeed = false;
|
|
2367
|
+
extern "C" void gea_replay_stage_skip_seed(bool skip) { g_stageSkipSeed = skip; }
|
|
2368
|
+
|
|
821
2369
|
extern "C" bool gea_replay_stage_begin(int x0, int y0, int x1, int y1)
|
|
822
2370
|
{
|
|
823
2371
|
if (!g_stripBuffer || !g_drawFramebuffer) return false;
|
|
@@ -826,34 +2374,50 @@ extern "C" bool gea_replay_stage_begin(int x0, int y0, int x1, int y1)
|
|
|
826
2374
|
if (x1 >= kWidth) x1 = kWidth - 1;
|
|
827
2375
|
if (y1 >= kHeight) y1 = kHeight - 1;
|
|
828
2376
|
const int rows = y1 - y0 + 1;
|
|
829
|
-
if (x0 > x1 || rows <= 0 || rows >
|
|
2377
|
+
if (x0 > x1 || rows <= 0 || rows > stageRowsFor(x1 - x0 + 1)) return false;
|
|
830
2378
|
const int core = xPortGetCoreID() & 1;
|
|
2379
|
+
const bool pipelined = g_stagePipeline && core == 0;
|
|
2380
|
+
if (!pipelined && core >= g_stripHalves) return false;
|
|
831
2381
|
ReplayStageState &st = g_replayStage[core];
|
|
832
2382
|
if (st.active) return false; // staging never nests; fall back to direct if it ever would
|
|
833
|
-
|
|
2383
|
+
// Only a replay into the draw framebuffer stages: one into a scratch buffer
|
|
2384
|
+
// (a retained snapshot, a fused raster chunk) would have its strip copied
|
|
2385
|
+
// back into the framebuffer instead of the buffer it was drawing.
|
|
2386
|
+
if (stateCanvas().pixels() != g_drawFramebuffer) return false;
|
|
2387
|
+
std::uint16_t *strip = pipelined ? pipeSlotBase(g_pipeSlot) : g_stripBuffer + static_cast<std::size_t>(core) * g_stripRows * kWidth;
|
|
2388
|
+
// Ready the back buffer first: until then it holds a frame-old picture under
|
|
2389
|
+
// a ring mapping that has not taken the scrolls since, so a seed read now
|
|
2390
|
+
// comes from the wrong rows and the copy-back writes them over the patched
|
|
2391
|
+
// frame -- on a scrolling Analytics tab, bands of the page (the top rows
|
|
2392
|
+
// among them) landed elsewhere on screen.
|
|
2393
|
+
auto &canvas = replayCanvas();
|
|
834
2394
|
// Seed the window from the back buffer so alpha blends and partial paints meet
|
|
835
2395
|
// the same dst bytes the direct path would have.
|
|
836
2396
|
const std::int64_t s0 = esp_timer_get_time();
|
|
837
2397
|
const int width = x1 - x0 + 1;
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
2398
|
+
const int stride = stageStrideFor(width);
|
|
2399
|
+
for (int y = y0; y <= y1 && !g_stageSkipSeed; ++y) {
|
|
2400
|
+
std::memcpy(&strip[static_cast<std::size_t>(y - y0) * stride],
|
|
2401
|
+
drawRow(y) + x0,
|
|
841
2402
|
static_cast<std::size_t>(width) * sizeof(std::uint16_t));
|
|
842
2403
|
}
|
|
843
2404
|
st.seedUs += esp_timer_get_time() - s0;
|
|
844
2405
|
st.px += width * rows;
|
|
845
|
-
auto &canvas = replayCanvas();
|
|
846
2406
|
st.hadDirty = canvas.dirty(&st.dx0, &st.dy0, &st.dx1, &st.dy1);
|
|
847
2407
|
st.alpha = canvas.globalAlpha();
|
|
848
|
-
// Window binding: claim rows [0..y1] with the pixel base rewound by y0 rows
|
|
849
|
-
// screen
|
|
850
|
-
//
|
|
851
|
-
//
|
|
2408
|
+
// Window binding: claim rows [0..y1] with the pixel base rewound by y0 rows and
|
|
2409
|
+
// x0 columns, so screen pixel (x, y) lands at strip[(y - y0) * stride + x - x0]
|
|
2410
|
+
// -- the replay keeps drawing in screen coordinates. Everything outside the
|
|
2411
|
+
// window is unbacked (or another row's) memory; the guard clip below keeps
|
|
2412
|
+
// every drawer inside the window (all drawers clamp to the clip, and the
|
|
852
2413
|
// replay never reads outside it).
|
|
853
|
-
canvas.bindPixels(strip - static_cast<std::size_t>(y0) *
|
|
2414
|
+
canvas.bindPixels(strip - static_cast<std::size_t>(y0) * stride - x0, kWidth, y1 + 1, stride);
|
|
854
2415
|
canvas.setGlobalAlpha(st.alpha);
|
|
855
2416
|
canvas.pushClip(x0, y0, width, rows);
|
|
856
2417
|
st.active = true;
|
|
2418
|
+
st.unseeded = g_stageSkipSeed;
|
|
2419
|
+
st.strip = strip;
|
|
2420
|
+
st.stride = stride;
|
|
857
2421
|
st.x0 = x0;
|
|
858
2422
|
st.y0 = y0;
|
|
859
2423
|
st.x1 = x1;
|
|
@@ -862,6 +2426,155 @@ extern "C" bool gea_replay_stage_begin(int x0, int y0, int x1, int y1)
|
|
|
862
2426
|
return true;
|
|
863
2427
|
}
|
|
864
2428
|
|
|
2429
|
+
namespace {
|
|
2430
|
+
#if GEA_EMBEDDED_DISPLAY_RGB_BOUNCE && CONFIG_IDF_TARGET_ESP32S3
|
|
2431
|
+
// The pipelined copy-back on a GDMA channel pair (SRAM slot -> PSRAM rows).
|
|
2432
|
+
// The copy is bound by PSRAM writes either way (~84 us a row); done by the CPU
|
|
2433
|
+
// it held the worker core for that whole time, the core that also builds the
|
|
2434
|
+
// next 3D frame, so both cores sat saturated. The worker now queues one
|
|
2435
|
+
// transfer per run of consecutive rows and sleeps until the last completes, and
|
|
2436
|
+
// the build runs meanwhile. The driver writes the source back (a no-op for
|
|
2437
|
+
// SRAM) and invalidates the destination lines when each transfer lands.
|
|
2438
|
+
async_memcpy_handle_t g_copyDma = nullptr;
|
|
2439
|
+
bool g_copyDmaTried = false;
|
|
2440
|
+
|
|
2441
|
+
bool IRAM_ATTR onCopyDmaDone(async_memcpy_handle_t, async_memcpy_event_t *, void *arg)
|
|
2442
|
+
{
|
|
2443
|
+
static_cast<std::atomic<int> *>(arg)->fetch_sub(1, std::memory_order_release);
|
|
2444
|
+
return false;
|
|
2445
|
+
}
|
|
2446
|
+
|
|
2447
|
+
bool copyDmaReady()
|
|
2448
|
+
{
|
|
2449
|
+
if (g_copyDma) return true;
|
|
2450
|
+
if (g_copyDmaTried) return false;
|
|
2451
|
+
g_copyDmaTried = true;
|
|
2452
|
+
async_memcpy_config_t cfg = ASYNC_MEMCPY_DEFAULT_CONFIG();
|
|
2453
|
+
cfg.backlog = 32;
|
|
2454
|
+
cfg.dma_burst_size = 32;
|
|
2455
|
+
if (esp_async_memcpy_install_gdma_ahb(&cfg, &g_copyDma) != ESP_OK) {
|
|
2456
|
+
g_copyDma = nullptr;
|
|
2457
|
+
ESP_LOGW(kTag, "copy-back GDMA unavailable; the worker copies with the CPU");
|
|
2458
|
+
return false;
|
|
2459
|
+
}
|
|
2460
|
+
if (g_stripHalves == 1 && g_stripRows % 3 == 0 && g_stripRows / 3 >= 4) g_pipeSlots = 3;
|
|
2461
|
+
return true;
|
|
2462
|
+
}
|
|
2463
|
+
|
|
2464
|
+
// Full-width rows [y0, y1] of a slot by GDMA, skipping repeats. False if it
|
|
2465
|
+
// could not start (the caller copies with the CPU instead).
|
|
2466
|
+
bool stageCopyRowsDma(const StageCopyJob &job, int y0, int y1)
|
|
2467
|
+
{
|
|
2468
|
+
// Every transfer allocates its descriptors and a cache-line stash buffer
|
|
2469
|
+
// from internal DMA-capable RAM. While that is short (the first frames
|
|
2470
|
+
// after boot) the allocation fails -- loudly, on a log path no level
|
|
2471
|
+
// silences -- so the CPU copies until there is room again.
|
|
2472
|
+
if (heap_caps_get_free_size(MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL) < 8 * 1024) return false;
|
|
2473
|
+
if (!copyDmaReady()) return false;
|
|
2474
|
+
std::atomic<int> &pending = g_slotPending[job.slot];
|
|
2475
|
+
std::uint16_t *runDst = nullptr;
|
|
2476
|
+
const std::uint16_t *runSrc = nullptr;
|
|
2477
|
+
int runRows = 0;
|
|
2478
|
+
bool ok = true;
|
|
2479
|
+
auto flushRun = [&]() {
|
|
2480
|
+
if (!runRows) return;
|
|
2481
|
+
pending.fetch_add(1, std::memory_order_relaxed);
|
|
2482
|
+
if (ok && esp_async_memcpy(g_copyDma, runDst, const_cast<std::uint16_t *>(runSrc), static_cast<std::size_t>(runRows) * job.rowBytes, onCopyDmaDone, &pending) == ESP_OK) {
|
|
2483
|
+
} else {
|
|
2484
|
+
pending.fetch_sub(1, std::memory_order_relaxed);
|
|
2485
|
+
ok = false; // copy it here; repeats already recorded stay valid
|
|
2486
|
+
for (int r = 0; r < runRows; ++r) {
|
|
2487
|
+
std::memcpy(runDst + static_cast<std::size_t>(r) * kWidth, runSrc + static_cast<std::size_t>(r) * job.stride, job.rowBytes);
|
|
2488
|
+
}
|
|
2489
|
+
esp_cache_msync(runDst, static_cast<std::size_t>(runRows) * job.rowBytes, ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
|
|
2490
|
+
}
|
|
2491
|
+
runDst = nullptr;
|
|
2492
|
+
runRows = 0;
|
|
2493
|
+
};
|
|
2494
|
+
for (int y = y0; y <= y1; ++y) {
|
|
2495
|
+
std::uint16_t *dst = drawRow(y);
|
|
2496
|
+
const std::uint16_t *src = job.strip + static_cast<std::ptrdiff_t>(y) * job.stride;
|
|
2497
|
+
if (rowRepeats(job.buffer, static_cast<int>((dst - g_scanFb[job.buffer]) / kWidth), src)) {
|
|
2498
|
+
flushRun();
|
|
2499
|
+
continue;
|
|
2500
|
+
}
|
|
2501
|
+
if (runRows && dst != runDst + static_cast<std::size_t>(runRows) * kWidth) flushRun();
|
|
2502
|
+
if (!runRows) {
|
|
2503
|
+
runDst = dst;
|
|
2504
|
+
runSrc = src;
|
|
2505
|
+
}
|
|
2506
|
+
++runRows;
|
|
2507
|
+
}
|
|
2508
|
+
flushRun();
|
|
2509
|
+
return true;
|
|
2510
|
+
}
|
|
2511
|
+
#endif
|
|
2512
|
+
|
|
2513
|
+
// Worker-core half of a pipelined strip: copies rows [y0, y1] of the slot back
|
|
2514
|
+
// into the framebuffer, one cache write-back per run of consecutive rows.
|
|
2515
|
+
void stageCopyRows(void *ctx, int y0, int y1)
|
|
2516
|
+
{
|
|
2517
|
+
const StageCopyJob &job = *static_cast<const StageCopyJob *>(ctx);
|
|
2518
|
+
#if GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
|
|
2519
|
+
#if CONFIG_IDF_TARGET_ESP32S3
|
|
2520
|
+
if (kCopyBackDma && job.sums && job.stride == kWidth && xPortGetCoreID() != 0 && stageCopyRowsDma(job, y0, y1)) return;
|
|
2521
|
+
#endif
|
|
2522
|
+
const bool waitRefill = static_cast<int>(xPortGetCoreID()) != g_panelIsrCore;
|
|
2523
|
+
std::uint16_t *runStart = nullptr;
|
|
2524
|
+
std::uint16_t *runLast = nullptr;
|
|
2525
|
+
int runRows = 0;
|
|
2526
|
+
auto flushRun = [&]() {
|
|
2527
|
+
if (!runStart) return;
|
|
2528
|
+
esp_cache_msync(runStart,
|
|
2529
|
+
static_cast<std::size_t>(runLast - runStart) * sizeof(std::uint16_t) + job.rowBytes,
|
|
2530
|
+
ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
|
|
2531
|
+
runStart = runLast = nullptr;
|
|
2532
|
+
runRows = 0;
|
|
2533
|
+
};
|
|
2534
|
+
for (int y = y0; y <= y1; ++y) {
|
|
2535
|
+
std::uint16_t *dst = drawRow(y) + job.x0;
|
|
2536
|
+
const std::uint16_t *src = job.strip + static_cast<std::ptrdiff_t>(y) * job.stride;
|
|
2537
|
+
if (job.sums && rowRepeats(job.buffer, static_cast<int>((dst - job.x0 - g_scanFb[job.buffer]) / kWidth), src)) continue;
|
|
2538
|
+
if (runStart && (dst != runLast + kWidth || runRows >= 64)) flushRun();
|
|
2539
|
+
if (waitRefill) waitOutRefill();
|
|
2540
|
+
std::memcpy(dst, src, job.rowBytes);
|
|
2541
|
+
if (!runStart) runStart = dst;
|
|
2542
|
+
runLast = dst;
|
|
2543
|
+
++runRows;
|
|
2544
|
+
}
|
|
2545
|
+
flushRun();
|
|
2546
|
+
#else
|
|
2547
|
+
for (int y = y0; y <= y1; ++y)
|
|
2548
|
+
scanoutRowCopy(drawRow(y) + job.x0, job.strip + static_cast<std::ptrdiff_t>(y) * job.stride, job.rowBytes);
|
|
2549
|
+
#endif
|
|
2550
|
+
}
|
|
2551
|
+
} // namespace
|
|
2552
|
+
|
|
2553
|
+
// Turns pipelined staging on for a run of full-replay strips on the main core
|
|
2554
|
+
// (see g_stagePipeline), or off, waiting out the last copy-back so the
|
|
2555
|
+
// framebuffer is whole before anything reads or publishes it.
|
|
2556
|
+
extern "C" void gea_replay_stage_pipeline(bool on)
|
|
2557
|
+
{
|
|
2558
|
+
if (on) {
|
|
2559
|
+
#if GEA_EMBEDDED_DISPLAY_RGB_BOUNCE && CONFIG_IDF_TARGET_ESP32S3
|
|
2560
|
+
if (kCopyBackDma && !g_stagePipeline && g_stripBuffer) copyDmaReady();
|
|
2561
|
+
#endif
|
|
2562
|
+
if (g_stagePipeline || !g_stripBuffer || pipeSlotRows() < 4 || (xPortGetCoreID() & 1) != 0) return;
|
|
2563
|
+
if (g_replayStage[0].active) return;
|
|
2564
|
+
g_stagePipeline = true;
|
|
2565
|
+
g_pipeSlot = 0;
|
|
2566
|
+
g_frameStagedRepaint = true;
|
|
2567
|
+
return;
|
|
2568
|
+
}
|
|
2569
|
+
if (!g_stagePipeline) return;
|
|
2570
|
+
if (g_pipeInFlight) {
|
|
2571
|
+
gea_render_parallel_wait();
|
|
2572
|
+
g_pipeInFlight = false;
|
|
2573
|
+
}
|
|
2574
|
+
for (int i = 0; i < g_pipeSlots; ++i) waitSlotCopied(i);
|
|
2575
|
+
g_stagePipeline = false;
|
|
2576
|
+
}
|
|
2577
|
+
|
|
865
2578
|
extern "C" void gea_replay_stage_end()
|
|
866
2579
|
{
|
|
867
2580
|
const int core = xPortGetCoreID() & 1;
|
|
@@ -873,7 +2586,17 @@ extern "C" void gea_replay_stage_end()
|
|
|
873
2586
|
auto &canvas = replayCanvas();
|
|
874
2587
|
int dx0, dy0, dx1, dy1;
|
|
875
2588
|
bool dirty = canvas.dirty(&dx0, &dy0, &dx1, &dy1);
|
|
876
|
-
if (
|
|
2589
|
+
if (st.unseeded) {
|
|
2590
|
+
// No seed: the dirty bbox no longer bounds what differs from the back
|
|
2591
|
+
// buffer. A fill trims pixels that already hold its colour and leaves
|
|
2592
|
+
// them out of the dirty rect -- in an unseeded strip those are leftovers
|
|
2593
|
+
// of an earlier strip, matching the fill but not the back buffer. Copied
|
|
2594
|
+
// back by bbox, a scroll frame left the blit-dragged corner button's edge
|
|
2595
|
+
// standing in the page gutter (a blue bar beside the scrollbar). The
|
|
2596
|
+
// cover painted the whole window, so copy all of it.
|
|
2597
|
+
dx0 = st.x0; dy0 = st.y0; dx1 = st.x1; dy1 = st.y1;
|
|
2598
|
+
dirty = true;
|
|
2599
|
+
} else if (dirty) {
|
|
877
2600
|
// Copy back only the painted bbox (clamped to the window) — unpainted strip
|
|
878
2601
|
// pixels are seed-identical, so skipping them saves PSRAM write traffic.
|
|
879
2602
|
if (dx0 < st.x0) dx0 = st.x0;
|
|
@@ -882,16 +2605,80 @@ extern "C" void gea_replay_stage_end()
|
|
|
882
2605
|
if (dy1 > st.y1) dy1 = st.y1;
|
|
883
2606
|
dirty = dx0 <= dx1 && dy0 <= dy1;
|
|
884
2607
|
}
|
|
885
|
-
|
|
886
|
-
|
|
2608
|
+
bool copied = false;
|
|
2609
|
+
if (dirty && g_stagePipeline && core == 0) {
|
|
2610
|
+
// Hand the copy-back to the worker core; the next strip replays into the
|
|
2611
|
+
// next slot meanwhile. One job in flight on the worker. With the CPU
|
|
2612
|
+
// copy, a busy worker means this core copies the slot itself. With the
|
|
2613
|
+
// GDMA copy (kCopyBackDma) the worker only hashes the rows and queues
|
|
2614
|
+
// their transfers, and this core waits only for the slot it replays into
|
|
2615
|
+
// next: copying here instead cost ~25% of this core, against ~12% for
|
|
2616
|
+
// the wait.
|
|
2617
|
+
StageCopyJob &job = g_stageCopyJobs[g_pipeSlot];
|
|
2618
|
+
job.strip = st.strip + static_cast<std::size_t>(dy0 - st.y0) * st.stride + (dx0 - st.x0) - static_cast<std::ptrdiff_t>(dy0) * st.stride;
|
|
2619
|
+
job.stride = st.stride;
|
|
2620
|
+
job.x0 = dx0;
|
|
2621
|
+
job.rowBytes = static_cast<std::size_t>(dx1 - dx0 + 1) * sizeof(std::uint16_t);
|
|
2622
|
+
job.buffer = g_drawIndex;
|
|
2623
|
+
job.sums = dx0 == 0 && dx1 == kWidth - 1 && st.stride == kWidth && ensureRowSums(g_drawIndex);
|
|
2624
|
+
job.slot = g_pipeSlot;
|
|
2625
|
+
if (!kCopyBackDma && g_pipeInFlight && !g_renderRowWorker.done.load(std::memory_order_acquire)) {
|
|
2626
|
+
// CPU copy-back: rather than wait on a busy worker (its core also
|
|
2627
|
+
// runs the panel ISR and the 3D frame build), copy the slot here
|
|
2628
|
+
// and let the next strip reuse it.
|
|
2629
|
+
stageCopyRows(&job, dy0, dy1);
|
|
2630
|
+
copied = true;
|
|
2631
|
+
} else if (g_pipeInFlight) {
|
|
2632
|
+
gea_render_parallel_wait();
|
|
2633
|
+
g_pipeInFlight = false;
|
|
2634
|
+
}
|
|
2635
|
+
if (!copied && gea_render_parallel_rows_submit(stageCopyRows, &job, dy0, dy1)) {
|
|
2636
|
+
g_pipeInFlight = true;
|
|
2637
|
+
g_pipeSlot = (g_pipeSlot + 1) % g_pipeSlots;
|
|
2638
|
+
waitSlotCopied(g_pipeSlot);
|
|
2639
|
+
copied = true;
|
|
2640
|
+
}
|
|
2641
|
+
}
|
|
2642
|
+
if (dirty && !copied) {
|
|
2643
|
+
const std::uint16_t *strip = st.strip;
|
|
887
2644
|
const int width = dx1 - dx0 + 1;
|
|
2645
|
+
const std::size_t rowBytes = static_cast<std::size_t>(width) * sizeof(std::uint16_t);
|
|
2646
|
+
#if GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
|
|
2647
|
+
// One cache write-back per run of physically consecutive rows, not one per
|
|
2648
|
+
// row: a narrow window (a scrollbar thumb's 10 px x 337 rows) paid ~7 us of
|
|
2649
|
+
// esp_cache_msync per 20-byte row, ~2.4 ms of a 3 ms repaint. The run's
|
|
2650
|
+
// span also covers the other columns' lines between its rows; writing
|
|
2651
|
+
// those back early is harmless.
|
|
2652
|
+
const bool waitRefill = static_cast<int>(xPortGetCoreID()) != g_panelIsrCore;
|
|
2653
|
+
std::uint16_t *runStart = nullptr;
|
|
2654
|
+
std::uint16_t *runLast = nullptr;
|
|
2655
|
+
int runRows = 0;
|
|
2656
|
+
auto flushRun = [&]() {
|
|
2657
|
+
if (!runStart) return;
|
|
2658
|
+
esp_cache_msync(runStart,
|
|
2659
|
+
static_cast<std::size_t>(runLast - runStart) * sizeof(std::uint16_t) + rowBytes,
|
|
2660
|
+
ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
|
|
2661
|
+
runStart = runLast = nullptr;
|
|
2662
|
+
runRows = 0;
|
|
2663
|
+
};
|
|
888
2664
|
for (int y = dy0; y <= dy1; ++y) {
|
|
889
|
-
std::
|
|
890
|
-
|
|
891
|
-
|
|
2665
|
+
std::uint16_t *dst = drawRow(y) + dx0;
|
|
2666
|
+
if (runStart && (dst != runLast + kWidth || runRows >= 64)) flushRun();
|
|
2667
|
+
if (waitRefill) waitOutRefill();
|
|
2668
|
+
std::memcpy(dst, &strip[static_cast<std::size_t>(y - st.y0) * st.stride + (dx0 - st.x0)], rowBytes);
|
|
2669
|
+
forgetRowSumAt(dst);
|
|
2670
|
+
if (!runStart) runStart = dst;
|
|
2671
|
+
runLast = dst;
|
|
2672
|
+
++runRows;
|
|
892
2673
|
}
|
|
2674
|
+
flushRun();
|
|
2675
|
+
#else
|
|
2676
|
+
for (int y = dy0; y <= dy1; ++y)
|
|
2677
|
+
scanoutRowCopy(drawRow(y) + dx0, &strip[static_cast<std::size_t>(y - st.y0) * st.stride + (dx0 - st.x0)], rowBytes);
|
|
2678
|
+
#endif
|
|
893
2679
|
}
|
|
894
2680
|
canvas.bindPixels(g_drawFramebuffer, kWidth, kHeight);
|
|
2681
|
+
applyCanvasMap();
|
|
895
2682
|
canvas.setGlobalAlpha(st.alpha);
|
|
896
2683
|
// bindPixels reset the dirty tracker: restore the frame's accumulated union,
|
|
897
2684
|
// then add this strip's painted bbox so the publish copy transmits it.
|
|
@@ -1069,7 +2856,22 @@ bool Display::start()
|
|
|
1069
2856
|
// Routed per-core: the worker core's band replay (and render.cpp helpers that
|
|
1070
2857
|
// draw raw pixels through canvas()) must hit the band canvas — its own clip +
|
|
1071
2858
|
// dirty accumulator over the same framebuffer. On the main core this is g_canvas.
|
|
1072
|
-
gea::framework::graphics::Canvas *Display::canvas()
|
|
2859
|
+
gea::framework::graphics::Canvas *Display::canvas()
|
|
2860
|
+
{
|
|
2861
|
+
#if GEA_EMBEDDED_DISPLAY_FUSE_REPLAY_FLUSH
|
|
2862
|
+
// Fused builds: the engine asks for canvas() early each frame only to read
|
|
2863
|
+
// its dirty box and scroll region, and draws inside the fused raster
|
|
2864
|
+
// callbacks. Readying here would patch the whole stale area before
|
|
2865
|
+
// flushRectsRasterized can say which rects it repaints (measured on the
|
|
2866
|
+
// CrowPanel 7: 85k px / 12ms per frame, 13 fps vs 15.4). Still wait for the
|
|
2867
|
+
// panel to let go of the buffer, so a raw draw through this pointer can never
|
|
2868
|
+
// hit rows being scanned; one that lands before the patch is counted as late.
|
|
2869
|
+
if (g_swapFbs && !g_backReady && !onWorkerCore()) waitBackBufferFree();
|
|
2870
|
+
return &stateCanvas();
|
|
2871
|
+
#else
|
|
2872
|
+
return &replayCanvas();
|
|
2873
|
+
#endif
|
|
2874
|
+
}
|
|
1073
2875
|
|
|
1074
2876
|
// The render layer binds canvas() onto a scratch buffer (retained snapshots for
|
|
1075
2877
|
// screenshots/OTA, fused rasterized flushes) and calls this to point it back at
|
|
@@ -1077,7 +2879,8 @@ gea::framework::graphics::Canvas *Display::canvas() { return &replayCanvas(); }
|
|
|
1077
2879
|
// here: the union it held describes the scratch buffer, not the framebuffer.
|
|
1078
2880
|
void Display::rebindCanvasToFramebuffer()
|
|
1079
2881
|
{
|
|
1080
|
-
|
|
2882
|
+
stateCanvas().bindPixels(g_drawFramebuffer, kWidth, kHeight);
|
|
2883
|
+
applyCanvasMap();
|
|
1081
2884
|
}
|
|
1082
2885
|
|
|
1083
2886
|
// The RGB panel scans out of g_framebuffer continuously, so it holds exactly
|
|
@@ -1093,8 +2896,14 @@ bool Display::copySnapshotRgb565(std::uint16_t *dst, int pixelCapacity, int *wid
|
|
|
1093
2896
|
if (!dst || pixelCapacity < static_cast<int>(kPixelCount)) return false;
|
|
1094
2897
|
const std::uint16_t *source = g_framebuffer ? g_framebuffer : g_drawFramebuffer;
|
|
1095
2898
|
if (!source) return false;
|
|
1096
|
-
|
|
1097
|
-
|
|
2899
|
+
const ScanMap map = g_framebuffer ? g_map[g_frontIndex] : g_map[g_drawIndex];
|
|
2900
|
+
// Native pixels are panel byte order; a snapshot is plain RGB565. Rows go
|
|
2901
|
+
// through the buffer's ring mapping, as the panel shows them.
|
|
2902
|
+
for (int y = 0; y < kHeight; ++y) {
|
|
2903
|
+
const std::uint16_t *row = source + static_cast<std::size_t>(mapRow(map, y)) * kWidth;
|
|
2904
|
+
std::uint16_t *out = dst + static_cast<std::size_t>(y) * kWidth;
|
|
2905
|
+
for (int x = 0; x < kWidth; ++x) out[x] = gea::framework::graphics::pixel::toRgb565(row[x]);
|
|
2906
|
+
}
|
|
1098
2907
|
return true;
|
|
1099
2908
|
}
|
|
1100
2909
|
|
|
@@ -1111,15 +2920,27 @@ int Display::countNonBlackPixels(bool)
|
|
|
1111
2920
|
|
|
1112
2921
|
void Display::clear()
|
|
1113
2922
|
{
|
|
2923
|
+
ensureBackReady();
|
|
1114
2924
|
g_canvas.clear(0x0000);
|
|
1115
2925
|
flush();
|
|
1116
2926
|
}
|
|
1117
2927
|
|
|
1118
|
-
void Display::clearNoFlush()
|
|
2928
|
+
void Display::clearNoFlush()
|
|
2929
|
+
{
|
|
2930
|
+
ensureBackReady();
|
|
2931
|
+
g_canvas.clear(0x0000);
|
|
2932
|
+
}
|
|
1119
2933
|
void Display::print(const char *) {}
|
|
1120
2934
|
|
|
1121
2935
|
void Display::flush()
|
|
1122
2936
|
{
|
|
2937
|
+
// Whatever drew this frame outside a staged repaint left no record. (A
|
|
2938
|
+
// flush with nothing drawn since the last one is not a frame of its own.)
|
|
2939
|
+
{
|
|
2940
|
+
int fx0, fy0, fx1, fy1;
|
|
2941
|
+
if (!g_frameStagedRepaint && g_canvas.dirty(&fx0, &fy0, &fx1, &fy1)) forgetRowSums();
|
|
2942
|
+
}
|
|
2943
|
+
g_frameStagedRepaint = false;
|
|
1123
2944
|
if (!g_framebuffer || !g_drawFramebuffer) return;
|
|
1124
2945
|
const int64_t start = esp_timer_get_time();
|
|
1125
2946
|
int64_t copyUs = 0;
|
|
@@ -1140,12 +2961,26 @@ void Display::flush()
|
|
|
1140
2961
|
cacheUs += esp_timer_get_time() - cacheStart;
|
|
1141
2962
|
}
|
|
1142
2963
|
g_canvas.resetDirty();
|
|
2964
|
+
if (g_swapFbs) {
|
|
2965
|
+
const int64_t swapStart = esp_timer_get_time();
|
|
2966
|
+
publishSwap();
|
|
2967
|
+
copyUs += esp_timer_get_time() - swapStart;
|
|
2968
|
+
yieldToIdleIfStarved();
|
|
2969
|
+
}
|
|
2970
|
+
noteFrameDiag(0, esp_timer_get_time() - start, pixels);
|
|
1143
2971
|
addFlushStats(esp_timer_get_time() - start, pixels);
|
|
1144
2972
|
addFlushStageStats(copyUs, cacheUs);
|
|
1145
2973
|
}
|
|
1146
2974
|
|
|
1147
2975
|
void Display::flushRects(const DisplayFlushRect *rects, int count, bool)
|
|
1148
2976
|
{
|
|
2977
|
+
// Whatever drew this frame outside a staged repaint left no record. (A
|
|
2978
|
+
// flush with nothing drawn since the last one is not a frame of its own.)
|
|
2979
|
+
{
|
|
2980
|
+
int fx0, fy0, fx1, fy1;
|
|
2981
|
+
if (!g_frameStagedRepaint && g_canvas.dirty(&fx0, &fy0, &fx1, &fy1)) forgetRowSums();
|
|
2982
|
+
}
|
|
2983
|
+
g_frameStagedRepaint = false;
|
|
1149
2984
|
if (!g_framebuffer || !g_drawFramebuffer || !rects || count <= 0) return;
|
|
1150
2985
|
const int64_t start = esp_timer_get_time();
|
|
1151
2986
|
int64_t copyUs = 0;
|
|
@@ -1166,12 +3001,40 @@ void Display::flushRects(const DisplayFlushRect *rects, int count, bool)
|
|
|
1166
3001
|
cacheUs += esp_timer_get_time() - cacheStart;
|
|
1167
3002
|
}
|
|
1168
3003
|
}
|
|
3004
|
+
if (g_swapFbs) {
|
|
3005
|
+
// The back buffer is the live draw target, so anything drawn outside the
|
|
3006
|
+
// rects is about to be shown too: record it, or the front would never
|
|
3007
|
+
// catch up and the pixels would flicker between frames.
|
|
3008
|
+
int dx0, dy0, dx1, dy1;
|
|
3009
|
+
if (g_canvas.dirty(&dx0, &dy0, &dx1, &dy1) && clipRect(&dx0, &dy0, &dx1, &dy1)) {
|
|
3010
|
+
// The dirty tracker is one bounding box, so test it against the rects'
|
|
3011
|
+
// bounding box: the renderer clips its drawing to the rects themselves.
|
|
3012
|
+
int bx0 = rects[0].x0, by0 = rects[0].y0, bx1 = rects[0].x1, by1 = rects[0].y1;
|
|
3013
|
+
for (int i = 1; i < count; ++i) {
|
|
3014
|
+
bx0 = std::min(bx0, rects[i].x0);
|
|
3015
|
+
by0 = std::min(by0, rects[i].y0);
|
|
3016
|
+
bx1 = std::max(bx1, rects[i].x1);
|
|
3017
|
+
by1 = std::max(by1, rects[i].y1);
|
|
3018
|
+
}
|
|
3019
|
+
if (dx0 < bx0 || dy0 < by0 || dx1 > bx1 || dy1 > by1) markStale(dx0, dy0, dx1, dy1);
|
|
3020
|
+
}
|
|
3021
|
+
}
|
|
1169
3022
|
g_canvas.resetDirty();
|
|
3023
|
+
if (g_swapFbs) {
|
|
3024
|
+
const int64_t swapStart = esp_timer_get_time();
|
|
3025
|
+
publishSwap();
|
|
3026
|
+
copyUs += esp_timer_get_time() - swapStart;
|
|
3027
|
+
yieldToIdleIfStarved();
|
|
3028
|
+
}
|
|
3029
|
+
noteFrameDiag(1, esp_timer_get_time() - start, pixels);
|
|
1170
3030
|
addFlushStats(esp_timer_get_time() - start, pixels);
|
|
1171
3031
|
addFlushStageStats(copyUs, cacheUs);
|
|
1172
3032
|
}
|
|
1173
3033
|
|
|
1174
|
-
|
|
3034
|
+
} // namespace gea::platform::display
|
|
3035
|
+
|
|
3036
|
+
namespace {
|
|
3037
|
+
bool streamRectNoSwap(int x, int y, int w, int h, gea::platform::display::DisplayStreamRasterFn raster, void *user)
|
|
1175
3038
|
{
|
|
1176
3039
|
if (!g_framebuffer || !g_drawFramebuffer || !raster || w <= 0 || h <= 0) return false;
|
|
1177
3040
|
int x0 = x;
|
|
@@ -1186,19 +3049,39 @@ bool Display::streamRect(int x, int y, int w, int h, DisplayStreamRasterFn raste
|
|
|
1186
3049
|
|
|
1187
3050
|
const int width = x1 - x0 + 1;
|
|
1188
3051
|
const int height = y1 - y0 + 1;
|
|
1189
|
-
|
|
1190
|
-
auto *scratch = static_cast<std::uint16_t *>(heap_caps_malloc(bytes, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
1191
|
-
if (!scratch) scratch = static_cast<std::uint16_t *>(heap_caps_malloc(bytes, MALLOC_CAP_8BIT));
|
|
1192
|
-
if (!scratch) return false;
|
|
1193
|
-
|
|
3052
|
+
ensureBackReady(); // no-op when flushRectsRasterized already readied it
|
|
1194
3053
|
const int64_t start = esp_timer_get_time();
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
3054
|
+
if (g_stripBuffer) {
|
|
3055
|
+
// Raster the region band by band into internal RAM (fast, off the PSRAM
|
|
3056
|
+
// bus, blends never read PSRAM) and write each finished band out once. The
|
|
3057
|
+
// raster contract is per-chunk -- the QSPI backend calls it per DMA chunk.
|
|
3058
|
+
const int bandRows = std::max(1, static_cast<int>(stripBufferPixels() / static_cast<std::size_t>(width)));
|
|
3059
|
+
for (int row = 0; row < height; row += bandRows) {
|
|
3060
|
+
const int rows = std::min(bandRows, height - row);
|
|
3061
|
+
const std::int64_t r0 = esp_timer_get_time();
|
|
3062
|
+
raster(g_stripBuffer, width, rows, x0, y0 + row, user);
|
|
3063
|
+
const std::int64_t r1 = esp_timer_get_time();
|
|
3064
|
+
for (int i = 0; i < rows; ++i) {
|
|
3065
|
+
scanoutRowCopy(drawRow(y0 + row + i) + x0, &g_stripBuffer[static_cast<std::size_t>(i) * width],
|
|
3066
|
+
static_cast<std::size_t>(width) * sizeof(std::uint16_t));
|
|
3067
|
+
}
|
|
3068
|
+
g_swapDiag.rasterUs += r1 - r0;
|
|
3069
|
+
g_swapDiag.copyUs += esp_timer_get_time() - r1;
|
|
3070
|
+
}
|
|
3071
|
+
} else {
|
|
3072
|
+
const std::size_t bytes = static_cast<std::size_t>(width) * static_cast<std::size_t>(height) * sizeof(std::uint16_t);
|
|
3073
|
+
auto *scratch = static_cast<std::uint16_t *>(heap_caps_malloc(bytes, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
3074
|
+
if (!scratch) scratch = static_cast<std::uint16_t *>(heap_caps_malloc(bytes, MALLOC_CAP_8BIT));
|
|
3075
|
+
if (!scratch) return false;
|
|
3076
|
+
raster(scratch, width, height, x0, y0, user);
|
|
3077
|
+
g_swapDiag.rasterUs += esp_timer_get_time() - start;
|
|
3078
|
+
for (int row = 0; row < height; ++row) {
|
|
3079
|
+
std::copy_n(&scratch[static_cast<std::size_t>(row) * width],
|
|
3080
|
+
width,
|
|
3081
|
+
drawRow(y0 + row) + x0);
|
|
3082
|
+
}
|
|
3083
|
+
heap_caps_free(scratch);
|
|
1200
3084
|
}
|
|
1201
|
-
heap_caps_free(scratch);
|
|
1202
3085
|
int publishedX0 = 0;
|
|
1203
3086
|
int publishedY0 = 0;
|
|
1204
3087
|
int publishedX1 = -1;
|
|
@@ -1209,28 +3092,153 @@ bool Display::streamRect(int x, int y, int w, int h, DisplayStreamRasterFn raste
|
|
|
1209
3092
|
addFlushStats(esp_timer_get_time() - start, pixels);
|
|
1210
3093
|
return true;
|
|
1211
3094
|
}
|
|
3095
|
+
} // namespace
|
|
3096
|
+
|
|
3097
|
+
namespace gea::platform::display {
|
|
1212
3098
|
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
3099
|
+
bool Display::streamRect(int x, int y, int w, int h, DisplayStreamRasterFn raster, void *user)
|
|
3100
|
+
{
|
|
3101
|
+
if (raster && w > 0 && h > 0) {
|
|
3102
|
+
const DisplayFlushRect rect = {x, y, x + w - 1, y + h - 1};
|
|
3103
|
+
ensureBackReady(&rect, 1);
|
|
3104
|
+
}
|
|
3105
|
+
const bool ok = streamRectNoSwap(x, y, w, h, raster, user);
|
|
3106
|
+
if (g_swapFbs) publishSwap();
|
|
3107
|
+
return ok;
|
|
3108
|
+
}
|
|
3109
|
+
|
|
3110
|
+
// Many small rects (64 bouncing balls): copying a rect's short rows out to
|
|
3111
|
+
// PSRAM is bound by bus latency, not bandwidth -- each row is a few cache-line
|
|
3112
|
+
// round trips while scanout and PSRAM-resident code share the bus -- and cost
|
|
3113
|
+
// ~2x the raster. So the worker core copies rect i out of one half of the SRAM
|
|
3114
|
+
// strip while this core rasters rect i+1 into the other half.
|
|
3115
|
+
struct RectCopyJob {
|
|
3116
|
+
const std::uint16_t *src;
|
|
3117
|
+
int x0, y0, width, height;
|
|
3118
|
+
};
|
|
3119
|
+
RectCopyJob g_rectCopyJob{};
|
|
3120
|
+
|
|
3121
|
+
void copyRectJob(void *ctx, int, int)
|
|
3122
|
+
{
|
|
3123
|
+
const RectCopyJob &j = *static_cast<const RectCopyJob *>(ctx);
|
|
3124
|
+
const std::size_t bytes = static_cast<std::size_t>(j.width) * sizeof(std::uint16_t);
|
|
3125
|
+
for (int i = 0; i < j.height; ++i)
|
|
3126
|
+
scanoutRowCopy(drawRow(j.y0 + i) + j.x0, j.src + static_cast<std::size_t>(i) * j.width, bytes);
|
|
3127
|
+
}
|
|
3128
|
+
|
|
3129
|
+
void waitRectCopy()
|
|
3130
|
+
{
|
|
3131
|
+
if (!g_scrollCopyInFlight) return;
|
|
3132
|
+
const std::int64_t w0 = esp_timer_get_time();
|
|
3133
|
+
while (!g_renderRowWorker.done.load(std::memory_order_acquire)) {
|
|
3134
|
+
if (esp_timer_get_time() - w0 > 100000) break; // a stuck worker must not hang the frame
|
|
3135
|
+
}
|
|
3136
|
+
g_scrollCopyInFlight = false;
|
|
3137
|
+
g_swapDiag.copyUs += esp_timer_get_time() - w0;
|
|
3138
|
+
}
|
|
3139
|
+
|
|
3140
|
+
bool smallRectsPipelinable(const DisplayFlushRect *rects, int count)
|
|
3141
|
+
{
|
|
3142
|
+
if (!g_stripBuffer || count < 4 || g_scrollCopyInFlight) return false;
|
|
3143
|
+
const std::size_t slot = stripBufferPixels() / 2;
|
|
3144
|
+
for (int i = 0; i < count; ++i) {
|
|
3145
|
+
const std::size_t w = static_cast<std::size_t>(rects[i].x1 - rects[i].x0 + 1);
|
|
3146
|
+
const std::size_t h = static_cast<std::size_t>(rects[i].y1 - rects[i].y0 + 1);
|
|
3147
|
+
if (w * h > slot) return false;
|
|
3148
|
+
}
|
|
3149
|
+
return true;
|
|
3150
|
+
}
|
|
3151
|
+
|
|
3152
|
+
// The pipelined path for flushRectsRasterized (smallRectsPipelinable said so).
|
|
3153
|
+
void flushSmallRectsPipelined(const DisplayFlushRect *rects, int count, DisplayStreamRasterFn raster, void *user, int *pxOut)
|
|
3154
|
+
{
|
|
3155
|
+
const std::size_t slot = stripBufferPixels() / 2;
|
|
3156
|
+
ensureBackReady(rects, count);
|
|
3157
|
+
int px = 0, half = 0;
|
|
3158
|
+
for (int i = 0; i < count; ++i) {
|
|
3159
|
+
int x0 = rects[i].x0, y0 = rects[i].y0, x1 = rects[i].x1, y1 = rects[i].y1;
|
|
3160
|
+
if (!clipRect(&x0, &y0, &x1, &y1)) continue;
|
|
3161
|
+
const int width = x1 - x0 + 1, height = y1 - y0 + 1;
|
|
3162
|
+
std::uint16_t *buf = g_stripBuffer + half * slot;
|
|
3163
|
+
const std::int64_t r0 = esp_timer_get_time();
|
|
3164
|
+
raster(buf, width, height, x0, y0, user);
|
|
3165
|
+
g_swapDiag.rasterUs += esp_timer_get_time() - r0;
|
|
3166
|
+
waitRectCopy(); // the other half is free again, and the worker idle
|
|
3167
|
+
g_rectCopyJob = {buf, x0, y0, width, height};
|
|
3168
|
+
g_scrollCopyInFlight = true; // band replays stay off the busy worker
|
|
3169
|
+
if (!submitRenderWorkerJob(copyRectJob, &g_rectCopyJob, y0, y1)) {
|
|
3170
|
+
g_scrollCopyInFlight = false;
|
|
3171
|
+
copyRectJob(&g_rectCopyJob, y0, y1);
|
|
3172
|
+
}
|
|
3173
|
+
publishDrawRect(x0, y0, x1, y1);
|
|
3174
|
+
px += width * height;
|
|
3175
|
+
half ^= 1;
|
|
3176
|
+
}
|
|
3177
|
+
waitRectCopy();
|
|
3178
|
+
g_canvas.resetDirty();
|
|
3179
|
+
*pxOut = px;
|
|
3180
|
+
}
|
|
3181
|
+
|
|
3182
|
+
// Fused flush: each rect is rastered from the display list into internal-RAM
|
|
3183
|
+
// bands and written to the back buffer once. Because these rects are repainted
|
|
3184
|
+
// completely, readying the back buffer patches only the stale area outside them.
|
|
1217
3185
|
void Display::flushRectsRasterized(const DisplayFlushRect *rects, int count, DisplayStreamRasterFn raster, void *user, bool)
|
|
1218
3186
|
{
|
|
3187
|
+
forgetRowSums();
|
|
3188
|
+
g_frameStagedRepaint = false;
|
|
1219
3189
|
if (!rects || count <= 0 || !raster) return;
|
|
1220
|
-
|
|
1221
|
-
|
|
3190
|
+
const std::int64_t start = esp_timer_get_time();
|
|
3191
|
+
// Nothing drew around the strips (no canvas or worker damage yet; the strip
|
|
3192
|
+
// writes below reset it): then every row of this frame leaves the cache as
|
|
3193
|
+
// it is written, and the publish need not walk them again.
|
|
3194
|
+
int dx0, dy0, dx1, dy1;
|
|
3195
|
+
const bool writtenBack = !g_canvas.dirty(&dx0, &dy0, &dx1, &dy1) && !g_workerCanvas.dirty(&dx0, &dy0, &dx1, &dy1);
|
|
3196
|
+
int px = 0;
|
|
3197
|
+
if (smallRectsPipelinable(rects, count)) {
|
|
3198
|
+
flushSmallRectsPipelined(rects, count, raster, user, &px);
|
|
3199
|
+
} else {
|
|
3200
|
+
ensureBackReady(rects, count);
|
|
3201
|
+
for (int i = 0; i < count; ++i) {
|
|
3202
|
+
streamRectNoSwap(rects[i].x0, rects[i].y0, rects[i].x1 - rects[i].x0 + 1, rects[i].y1 - rects[i].y0 + 1, raster, user);
|
|
3203
|
+
px += (rects[i].x1 - rects[i].x0 + 1) * (rects[i].y1 - rects[i].y0 + 1);
|
|
3204
|
+
}
|
|
1222
3205
|
}
|
|
3206
|
+
if (g_swapFbs) {
|
|
3207
|
+
g_frameWrittenBack = writtenBack;
|
|
3208
|
+
publishSwap();
|
|
3209
|
+
yieldToIdleIfStarved();
|
|
3210
|
+
}
|
|
3211
|
+
noteFrameDiag(2, esp_timer_get_time() - start, px);
|
|
1223
3212
|
rebindCanvasToFramebuffer();
|
|
1224
3213
|
}
|
|
1225
3214
|
|
|
1226
3215
|
bool Display::present(const DisplayPresentCommand *commands, int commandCount)
|
|
1227
3216
|
{
|
|
1228
3217
|
if (!commands || commandCount <= 0) return false;
|
|
3218
|
+
// A frame the app rendered in place (gea_display_acquire_panel_buffer) opens
|
|
3219
|
+
// with a draw of the back buffer onto itself: already there, so only what is
|
|
3220
|
+
// drawn over it replays.
|
|
3221
|
+
const DisplayPresentCommand &base = commands[0];
|
|
3222
|
+
if (base.type == gea::platform::display::DisplayPresentCommandType::DrawImage && g_drawFramebuffer &&
|
|
3223
|
+
base.drawImage.pixels == g_drawFramebuffer && base.drawImage.x == 0 && base.drawImage.y == 0 &&
|
|
3224
|
+
base.drawImage.srcWidth == kWidth && base.drawImage.srcHeight == kHeight && !base.drawImage.alphaPixels &&
|
|
3225
|
+
base.drawImage.alpha == 255 && panelBuffersUsable()) {
|
|
3226
|
+
const DisplayFlushRect whole{0, 0, kWidth - 1, kHeight - 1};
|
|
3227
|
+
ensureBackReady(&whole, 1);
|
|
3228
|
+
g_drawBufferStale = false;
|
|
3229
|
+
const std::int64_t replayStart = esp_timer_get_time();
|
|
3230
|
+
for (int i = 1; i < commandCount; ++i) replayPresentCommand(commands[i]);
|
|
3231
|
+
g_swapDiag.rasterUs += esp_timer_get_time() - replayStart;
|
|
3232
|
+
g_canvas.setGlobalAlpha(255);
|
|
3233
|
+
g_canvas.markDirty(0, 0, kWidth - 1, kHeight - 1);
|
|
3234
|
+
flush();
|
|
3235
|
+
return true;
|
|
3236
|
+
}
|
|
1229
3237
|
#if GEA_EMBEDDED_DISPLAY_RGB_STRIP_RASTER
|
|
1230
3238
|
// Fast path: full-screen-redraw frames compose in SRAM strips and publish
|
|
1231
3239
|
// straight to scanout. Anything without a full-screen opaque base needs the
|
|
1232
3240
|
// persistent back buffer, so it falls through.
|
|
1233
|
-
if (g_stripBuffer) {
|
|
3241
|
+
if (g_stripBuffer && g_stripHalves == 2) {
|
|
1234
3242
|
present::Frame frame;
|
|
1235
3243
|
if (present::extractFrame(commands, commandCount, frame) &&
|
|
1236
3244
|
present::frameHasOpaqueBase(frame, kWidth, kHeight)) {
|
|
@@ -1241,13 +3249,27 @@ bool Display::present(const DisplayPresentCommand *commands, int commandCount)
|
|
|
1241
3249
|
// Fallback: per-command replay into the PSRAM back buffer, then publish. If
|
|
1242
3250
|
// the last frame went through the strip path, the back buffer is stale —
|
|
1243
3251
|
// re-seed it from the live scanout before drawing the partial update on top.
|
|
3252
|
+
// A frame whose first command paints every pixel (a whole-screen
|
|
3253
|
+
// putImageData or fill) needs none of the old pixels: bringing the back
|
|
3254
|
+
// buffer current from the front would copy the whole frame (~47 ms of PSRAM
|
|
3255
|
+
// traffic at 800x480) only for the replay to paint over it.
|
|
3256
|
+
present::Frame frame;
|
|
3257
|
+
if (present::extractFrame(commands, commandCount, frame) && present::frameHasOpaqueBase(frame, kWidth, kHeight)) {
|
|
3258
|
+
const DisplayFlushRect whole{0, 0, kWidth - 1, kHeight - 1};
|
|
3259
|
+
ensureBackReady(&whole, 1);
|
|
3260
|
+
g_drawBufferStale = false;
|
|
3261
|
+
}
|
|
3262
|
+
ensureBackReady();
|
|
1244
3263
|
if (g_drawBufferStale && g_drawFramebuffer != g_framebuffer) {
|
|
1245
3264
|
std::memcpy(g_drawFramebuffer, g_framebuffer, kFramebufferBytes);
|
|
3265
|
+
forgetRowSums();
|
|
1246
3266
|
g_drawBufferStale = false;
|
|
1247
3267
|
}
|
|
3268
|
+
const std::int64_t replayStart = esp_timer_get_time();
|
|
1248
3269
|
for (int i = 0; i < commandCount; ++i) {
|
|
1249
3270
|
replayPresentCommand(commands[i]);
|
|
1250
3271
|
}
|
|
3272
|
+
g_swapDiag.rasterUs += esp_timer_get_time() - replayStart;
|
|
1251
3273
|
g_canvas.setGlobalAlpha(255);
|
|
1252
3274
|
flush();
|
|
1253
3275
|
return true;
|
|
@@ -1277,22 +3299,46 @@ void Display::applyPendingInternalReserve()
|
|
|
1277
3299
|
g_internalCacheHeadroom = std::max(g_internalCacheHeadroom, requested);
|
|
1278
3300
|
if (g_stripBuffer) {
|
|
1279
3301
|
gea_render_parallel_wait();
|
|
3302
|
+
const unsigned bytes = static_cast<unsigned>(stripBufferPixels() * sizeof(std::uint16_t));
|
|
1280
3303
|
heap_caps_free(g_stripBuffer);
|
|
1281
3304
|
g_stripBuffer = nullptr;
|
|
1282
|
-
|
|
1283
|
-
|
|
3305
|
+
g_stripRows = 0;
|
|
3306
|
+
g_stripHalves = 0;
|
|
3307
|
+
ESP_LOGI(kTag, "Released %u bytes of strip staging for internal-memory reservation", bytes);
|
|
1284
3308
|
}
|
|
1285
3309
|
} else {
|
|
1286
3310
|
#if GEA_EMBEDDED_DISPLAY_RGB_STRIP_RASTER
|
|
1287
3311
|
// Restoring an optimization must not consume the headroom just requested
|
|
1288
3312
|
// by the radios. The PSRAM back-buffer path remains fully functional.
|
|
1289
|
-
|
|
1290
|
-
if (!g_stripBuffer && heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) >= bytes + g_internalCacheHeadroom) {
|
|
1291
|
-
g_stripBuffer = static_cast<std::uint16_t *>(heap_caps_aligned_alloc(16, bytes, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT));
|
|
1292
|
-
}
|
|
3313
|
+
if (!g_stripBuffer) allocStripBuffer(g_internalCacheHeadroom);
|
|
1293
3314
|
#endif
|
|
1294
3315
|
}
|
|
1295
3316
|
}
|
|
3317
|
+
// An app's own internal-RAM allocations (Memory.beginInternal/endInternal, on
|
|
3318
|
+
// the frame task like the reservation above): the strip yields its block, the
|
|
3319
|
+
// app takes what it asked for, and the strip comes back at the tallest height
|
|
3320
|
+
// left -- shorter strips cost a few raster passes, a table read from PSRAM per
|
|
3321
|
+
// pixel costs far more.
|
|
3322
|
+
extern "C" void gea_display_release_internal(std::size_t)
|
|
3323
|
+
{
|
|
3324
|
+
if (!g_stripBuffer) return;
|
|
3325
|
+
gea_render_parallel_wait();
|
|
3326
|
+
heap_caps_free(g_stripBuffer);
|
|
3327
|
+
g_stripBuffer = nullptr;
|
|
3328
|
+
g_stripRows = 0;
|
|
3329
|
+
g_stripHalves = 0;
|
|
3330
|
+
}
|
|
3331
|
+
extern "C" void gea_display_restore_internal()
|
|
3332
|
+
{
|
|
3333
|
+
#if GEA_EMBEDDED_DISPLAY_RGB_STRIP_RASTER
|
|
3334
|
+
// Before init() the panel allocates its own strip.
|
|
3335
|
+
if (!g_initialized || g_stripBuffer || g_appliedInternalReservation) return;
|
|
3336
|
+
allocStripBuffer(g_internalCacheHeadroom);
|
|
3337
|
+
ESP_LOGI(kTag, "strip-raster rows=%d halves=%d after app internal allocation (internal free=%u largest=%u)", g_stripRows, g_stripHalves,
|
|
3338
|
+
static_cast<unsigned>(heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)),
|
|
3339
|
+
static_cast<unsigned>(heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)));
|
|
3340
|
+
#endif
|
|
3341
|
+
}
|
|
1296
3342
|
int Display::flushChunkRows() { return g_flushRows; }
|
|
1297
3343
|
int Display::flushQueueDepth() { return g_flushDepth; }
|
|
1298
3344
|
int Display::flushBufferBytes() { return 0; }
|
|
@@ -1300,11 +3346,11 @@ int Display::flushBufferBytes() { return 0; }
|
|
|
1300
3346
|
// 2nd-core band replay lands on the worker canvas (its own clip + dirty over
|
|
1301
3347
|
// the same framebuffer). On the main core replayCanvas() == g_canvas, so the
|
|
1302
3348
|
// single-core paths are unchanged.
|
|
1303
|
-
void Display::pushClip(int x, int y, int w, int h) {
|
|
1304
|
-
void Display::popClip() {
|
|
1305
|
-
void Display::resetClip() {
|
|
1306
|
-
void Display::setAlpha(std::uint8_t alpha) {
|
|
1307
|
-
std::uint8_t Display::alpha() { return
|
|
3349
|
+
void Display::pushClip(int x, int y, int w, int h) { stateCanvas().pushClip(x, y, w, h); }
|
|
3350
|
+
void Display::popClip() { stateCanvas().popClip(); }
|
|
3351
|
+
void Display::resetClip() { stateCanvas().resetClip(); }
|
|
3352
|
+
void Display::setAlpha(std::uint8_t alpha) { stateCanvas().setGlobalAlpha(alpha); }
|
|
3353
|
+
std::uint8_t Display::alpha() { return stateCanvas().globalAlpha(); }
|
|
1308
3354
|
int Display::brightness() { return g_brightness; }
|
|
1309
3355
|
|
|
1310
3356
|
void Display::setBrightness(int brightnessPercent)
|
|
@@ -1315,16 +3361,331 @@ void Display::setBrightness(int brightnessPercent)
|
|
|
1315
3361
|
applyBacklight();
|
|
1316
3362
|
}
|
|
1317
3363
|
|
|
1318
|
-
//
|
|
1319
|
-
|
|
3364
|
+
// VSync off (Display.setVSync(false)): draw straight into the buffer the panel
|
|
3365
|
+
// scans, as the single-buffer fallback does. No swap, so no waiting for the
|
|
3366
|
+
// panel to release the back buffer and no patching it from the front -- on 64
|
|
3367
|
+
// bouncing balls those cost ~9 ms and a PSRAM copy of every changed pixel a
|
|
3368
|
+
// frame. The app accepts that a moving object may show split across a scan.
|
|
3369
|
+
void Display::setVSync(bool on)
|
|
3370
|
+
{
|
|
3371
|
+
g_vsyncWanted = on;
|
|
3372
|
+
applyVSync();
|
|
3373
|
+
}
|
|
1320
3374
|
void Display::invalidate() {}
|
|
1321
|
-
bool Display::vsyncEnabled() { return
|
|
3375
|
+
bool Display::vsyncEnabled() { return g_swapFbs; }
|
|
1322
3376
|
void Display::vsyncWaitForFrame() {}
|
|
1323
3377
|
|
|
1324
|
-
void Display::clip(int *x0, int *y0, int *x1, int *y1) {
|
|
3378
|
+
void Display::clip(int *x0, int *y0, int *x1, int *y1) { stateCanvas().currentClip(x0, y0, x1, y1); }
|
|
3379
|
+
|
|
1325
3380
|
void Display::fillRect(int x, int y, int w, int h, std::uint16_t color) { replayCanvas().fillRect(x, y, w, h, color); }
|
|
1326
|
-
|
|
1327
|
-
|
|
3381
|
+
namespace {
|
|
3382
|
+
// Whether the panel is done reading `row` of the back buffer. It is released as a
|
|
3383
|
+
// whole one frame end after the swap; before that, the panel is in the buffer's
|
|
3384
|
+
// last frame, and a row it has already DISPLAYED in that frame is never read
|
|
3385
|
+
// again (the bounce ISR reads ahead of the display, never behind it). The
|
|
3386
|
+
// display position is counted from that frame's VSYNC, less the rows before the
|
|
3387
|
+
// active area and a few rows of margin.
|
|
3388
|
+
bool backRowReleased(std::uint32_t freeSeq, int row)
|
|
3389
|
+
{
|
|
3390
|
+
const std::uint32_t seq = g_frameSeq;
|
|
3391
|
+
if (static_cast<std::int32_t>(seq - freeSeq) >= 0) return true;
|
|
3392
|
+
if (seq + 1 != freeSeq) return false;
|
|
3393
|
+
const std::int64_t vsyncUs = g_vsyncUs, latchUs = g_frameCompleteUs;
|
|
3394
|
+
if (g_frameSeq != seq) return backRowReleased(freeSeq, row);
|
|
3395
|
+
if (vsyncUs <= latchUs) return false; // the last frame has not started displaying yet
|
|
3396
|
+
const auto &d = gea::platform::board::display;
|
|
3397
|
+
const std::int64_t lineClocks = kWidth + d.hsyncPulseWidth + d.hsyncBackPorch + d.hsyncFrontPorch;
|
|
3398
|
+
const std::int64_t lines = (esp_timer_get_time() - vsyncUs) * kPixelClockHz / (lineClocks * 1000000);
|
|
3399
|
+
return row < lines - d.vsyncPulseWidth - d.vsyncBackPorch - 4;
|
|
3400
|
+
}
|
|
3401
|
+
} // namespace
|
|
3402
|
+
|
|
3403
|
+
namespace {
|
|
3404
|
+
struct ScrollCopy {
|
|
3405
|
+
const std::uint16_t *src;
|
|
3406
|
+
std::uint16_t *dst;
|
|
3407
|
+
int dstX, srcX;
|
|
3408
|
+
std::size_t bytes;
|
|
3409
|
+
int y0, y1, dy;
|
|
3410
|
+
std::uint32_t freeSeq;
|
|
3411
|
+
// Rows (by screen row) the back buffer already holds shifted, or null.
|
|
3412
|
+
const std::uint8_t *skip = nullptr;
|
|
3413
|
+
};
|
|
3414
|
+
|
|
3415
|
+
void scrollCopyRows(void *ctx, int, int)
|
|
3416
|
+
{
|
|
3417
|
+
auto &c = *static_cast<ScrollCopy *>(ctx);
|
|
3418
|
+
bool released = false;
|
|
3419
|
+
for (int row = c.y0; row <= c.y1; ++row) {
|
|
3420
|
+
const int srcRow = row - c.dy;
|
|
3421
|
+
if (srcRow < c.y0 || srcRow > c.y1) continue;
|
|
3422
|
+
if (c.skip && c.skip[row]) continue;
|
|
3423
|
+
if (!released) {
|
|
3424
|
+
const std::int64_t r0 = esp_timer_get_time();
|
|
3425
|
+
while (!(released = static_cast<std::int32_t>(g_frameSeq - c.freeSeq) >= 0) && !backRowReleased(c.freeSeq, row)) {
|
|
3426
|
+
if (esp_timer_get_time() - r0 > 100000) break; // panel stopped: don't hang
|
|
3427
|
+
esp_rom_delay_us(20);
|
|
3428
|
+
}
|
|
3429
|
+
}
|
|
3430
|
+
// Row by row, each written back at once (see scanoutRowCopy): batching
|
|
3431
|
+
// the write-back left dirty lines for the bounce refills to evict, and
|
|
3432
|
+
// the panel showed thin glitch lines.
|
|
3433
|
+
scanoutRowCopy(&c.dst[static_cast<std::size_t>(row) * kWidth + c.dstX],
|
|
3434
|
+
&c.src[static_cast<std::size_t>(srcRow) * kWidth + c.srcX], c.bytes);
|
|
3435
|
+
}
|
|
3436
|
+
}
|
|
3437
|
+
} // namespace
|
|
3438
|
+
|
|
3439
|
+
void Display::scrollRect(int x, int y, int w, int h, int dx, int dy)
|
|
3440
|
+
{
|
|
3441
|
+
if (g_ringScroll && !onWorkerCore()) {
|
|
3442
|
+
if (dx == 0 && dy != 0 && x <= 0 && x + w >= kWidth && y >= 0 && h > 0 && y + h <= kHeight && dy > -h && dy < h) {
|
|
3443
|
+
// The ring path: rotate the draw buffer's mapping. The rows the shift
|
|
3444
|
+
// exposes keep pixels from the opposite edge, exactly as an in-place
|
|
3445
|
+
// scroll leaves stale rows; the caller repaints them and flushes only
|
|
3446
|
+
// what it repainted (gea_display_scroll_rect_self_flushing).
|
|
3447
|
+
g_swapDiag.rings++;
|
|
3448
|
+
ensureBackReady();
|
|
3449
|
+
changeRegion(y, h);
|
|
3450
|
+
ScanMap &m = g_map[g_drawIndex];
|
|
3451
|
+
m.off = normOffset(m.off - dy, m.h);
|
|
3452
|
+
applyCanvasMap();
|
|
3453
|
+
for (int i = 0; i < g_scanFbCount; ++i) {
|
|
3454
|
+
if (i == g_drawIndex || g_mapReset[i]) continue;
|
|
3455
|
+
g_pendingRot[i] += dy;
|
|
3456
|
+
shiftStale(g_stale[i], y, h, dy);
|
|
3457
|
+
}
|
|
3458
|
+
g_publishPending = true;
|
|
3459
|
+
return;
|
|
3460
|
+
}
|
|
3461
|
+
// Anything else moves pixels on an identity layout.
|
|
3462
|
+
g_swapDiag.fallbacks++;
|
|
3463
|
+
if (g_regionH > 0) {
|
|
3464
|
+
ensureBackReady();
|
|
3465
|
+
changeRegion(0, 0);
|
|
3466
|
+
}
|
|
3467
|
+
}
|
|
3468
|
+
// Swapped scanout buffers, back not yet readied since the swap: its pixels
|
|
3469
|
+
// are a frame old, so readying it would copy the whole viewport from the
|
|
3470
|
+
// front only for the in-place scroll to move it all again. Ready everything
|
|
3471
|
+
// outside the viewport, then copy the viewport from the front, shifted --
|
|
3472
|
+
// one pass over the pixels instead of two. Rows the shift exposes keep stale
|
|
3473
|
+
// pixels, exactly like an in-place scroll; the caller repaints them.
|
|
3474
|
+
if (g_swapFbs && !g_backReady && !onWorkerCore() && (dx == 0 || dy == 0) && (dx != 0 || dy != 0)) {
|
|
3475
|
+
int x0 = x, y0 = y, x1 = x + w - 1, y1 = y + h - 1;
|
|
3476
|
+
int cx0, cy0, cx1, cy1;
|
|
3477
|
+
g_canvas.currentClip(&cx0, &cy0, &cx1, &cy1);
|
|
3478
|
+
x0 = std::max(x0, cx0); y0 = std::max(y0, cy0);
|
|
3479
|
+
x1 = std::min(x1, cx1); y1 = std::min(y1, cy1);
|
|
3480
|
+
if (!clipRect(&x0, &y0, &x1, &y1)) return;
|
|
3481
|
+
const int rw = x1 - x0 + 1, rh = y1 - y0 + 1;
|
|
3482
|
+
if (dx <= -rw || dx >= rw || dy <= -rh || dy >= rh) {
|
|
3483
|
+
replayCanvas(); // nothing survives the shift; just ready the buffer
|
|
3484
|
+
return;
|
|
3485
|
+
}
|
|
3486
|
+
const DisplayFlushRect skip = {x0, y0, x1, y1};
|
|
3487
|
+
const std::int64_t c0 = esp_timer_get_time();
|
|
3488
|
+
const std::uint16_t *src = g_framebuffer;
|
|
3489
|
+
std::uint16_t *dst = g_drawFramebuffer;
|
|
3490
|
+
const int sx0 = dx > 0 ? x0 : x0 - dx; // first source column
|
|
3491
|
+
const int dxs = dx > 0 ? x0 + dx : x0; // first destination column
|
|
3492
|
+
const std::size_t bytes = static_cast<std::size_t>(rw - (dx > 0 ? dx : -dx)) * sizeof(std::uint16_t);
|
|
3493
|
+
// Copy top down behind the panel's scan of the buffer's last frame instead of
|
|
3494
|
+
// first waiting for the whole buffer: the copy outruns nothing (the scan is
|
|
3495
|
+
// faster), so the wait for the release overlaps the copy. The copy runs on
|
|
3496
|
+
// the panel ISR's core when the row worker lives there: a PSRAM-to-PSRAM
|
|
3497
|
+
// copy at full speed from the other core starves the bounce ISR's refill of
|
|
3498
|
+
// the same bus, and the panel shows stale bounce rows (short glitch lines);
|
|
3499
|
+
// on the ISR's core the ISR simply preempts it.
|
|
3500
|
+
ScrollCopy job{src, dst, dxs, sx0, bytes, y0, y1, dy, g_freeSeq[g_drawIndex]};
|
|
3501
|
+
if (g_panelIsrCore >= 0 && g_panelIsrCore != static_cast<int>(xPortGetCoreID()) &&
|
|
3502
|
+
submitRenderWorkerJob(scrollCopyRows, &job, y0, y1)) {
|
|
3503
|
+
g_lastSubmitWasDrawBand = false;
|
|
3504
|
+
gea_render_parallel_wait();
|
|
3505
|
+
} else {
|
|
3506
|
+
scrollCopyRows(&job, y0, y1);
|
|
3507
|
+
}
|
|
3508
|
+
cacheWritebackRect(dst, x0, y0, x1, y1);
|
|
3509
|
+
const std::int64_t c1 = esp_timer_get_time();
|
|
3510
|
+
g_swapDiag.copyUs += c1 - c0;
|
|
3511
|
+
ensureBackReady(&skip, 1); // the rest of the buffer, outside the viewport
|
|
3512
|
+
g_canvas.markDirty(x0, y0, x1, y1);
|
|
3513
|
+
// The whole viewport changed: hand it to the other buffers here. A frame
|
|
3514
|
+
// published with flushRects records the canvas box only when it reaches
|
|
3515
|
+
// past the rects' bounding box, and a strip on one edge with an overlay
|
|
3516
|
+
// on the other spans the viewport: a tab swipe's last step then never
|
|
3517
|
+
// reached the other buffer, and every later swap flickered between them.
|
|
3518
|
+
markStale(x0, y0, x1, y1);
|
|
3519
|
+
return;
|
|
3520
|
+
}
|
|
3521
|
+
replayCanvas().scrollRect(x, y, w, h, dx, dy);
|
|
3522
|
+
}
|
|
3523
|
+
// A sideways scroll's shifted copy, started without waiting for it: the
|
|
3524
|
+
// worker on the panel ISR's core copies the viewport while this core repaints
|
|
3525
|
+
// the strip the scroll exposes, which the copy never writes. On the CrowPanel
|
|
3526
|
+
// 7.0 the copy alone took ~30 ms of a tab swipe's frame and the strip as long
|
|
3527
|
+
// again, one after the other. Both halves of a scroll's work then fit in one.
|
|
3528
|
+
namespace {
|
|
3529
|
+
struct AsyncScroll {
|
|
3530
|
+
ScrollCopy job;
|
|
3531
|
+
DisplayFlushRect skip;
|
|
3532
|
+
std::int64_t startUs;
|
|
3533
|
+
};
|
|
3534
|
+
AsyncScroll g_asyncScroll{};
|
|
3535
|
+
|
|
3536
|
+
} // namespace
|
|
3537
|
+
|
|
3538
|
+
extern "C" bool gea_display_scroll_rect_begin(int x, int y, int w, int h, int dx, int dy)
|
|
3539
|
+
{
|
|
3540
|
+
// Taken here whatever happens: a later begin must never see these.
|
|
3541
|
+
const std::uint32_t *const nextSig = g_nextSig;
|
|
3542
|
+
g_nextSig = nullptr;
|
|
3543
|
+
if (!g_swapFbs || g_backReady || onWorkerCore() || dy != 0 || dx == 0 || g_regionH > 0) return false;
|
|
3544
|
+
if (g_panelIsrCore < 0 || g_panelIsrCore == static_cast<int>(xPortGetCoreID())) return false;
|
|
3545
|
+
int x0 = x, y0 = y, x1 = x + w - 1, y1 = y + h - 1;
|
|
3546
|
+
int cx0, cy0, cx1, cy1;
|
|
3547
|
+
g_canvas.currentClip(&cx0, &cy0, &cx1, &cy1);
|
|
3548
|
+
x0 = std::max(x0, cx0); y0 = std::max(y0, cy0);
|
|
3549
|
+
x1 = std::min(x1, cx1); y1 = std::min(y1, cy1);
|
|
3550
|
+
if (!clipRect(&x0, &y0, &x1, &y1)) return false;
|
|
3551
|
+
const int rw = x1 - x0 + 1;
|
|
3552
|
+
if (dx <= -rw || dx >= rw) return false;
|
|
3553
|
+
xSemaphoreTake(g_readyMutex, portMAX_DELAY);
|
|
3554
|
+
// The strip repaint draws into the back buffer straight away, so the panel
|
|
3555
|
+
// must be done with it first (it nearly always is by now).
|
|
3556
|
+
waitBackBufferFree();
|
|
3557
|
+
bringMapCurrent(g_drawIndex);
|
|
3558
|
+
const int sx0 = dx > 0 ? x0 : x0 - dx;
|
|
3559
|
+
const int dxs = dx > 0 ? x0 + dx : x0;
|
|
3560
|
+
const std::size_t bytes = static_cast<std::size_t>(rw - (dx > 0 ? dx : -dx)) * sizeof(std::uint16_t);
|
|
3561
|
+
g_asyncScroll.job = ScrollCopy{g_framebuffer, g_drawFramebuffer, dxs, sx0, bytes, y0, y1, 0, g_freeSeq[g_drawIndex]};
|
|
3562
|
+
g_asyncScroll.skip = {x0, y0, x1, y1};
|
|
3563
|
+
// Skip the rows whose content the shift leaves alone and the back buffer
|
|
3564
|
+
// already holds; record the new signatures for this buffer's next turn.
|
|
3565
|
+
const int b = g_drawIndex;
|
|
3566
|
+
if (!g_rowSig[b]) g_rowSig[b] = static_cast<std::uint32_t *>(heap_caps_calloc(kHeight, sizeof(std::uint32_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
3567
|
+
if (g_rowSig[b] && nextSig && g_nextSigY0 <= y0 && g_nextSigY0 + g_nextSigRows - 1 >= y1) {
|
|
3568
|
+
const DisplayFlushRect &pr = g_rowSigRect[b];
|
|
3569
|
+
const bool same = g_rowSigValid[b] && pr.x0 == x0 && pr.y0 == y0 && pr.x1 == x1 && pr.y1 == y1;
|
|
3570
|
+
int skipped = 0;
|
|
3571
|
+
for (int row = y0; row <= y1; ++row) {
|
|
3572
|
+
const std::uint32_t sig = nextSig[row - g_nextSigY0];
|
|
3573
|
+
g_skipRow[row] = same && sig != 0 && g_rowSig[b][row] == sig;
|
|
3574
|
+
skipped += g_skipRow[row];
|
|
3575
|
+
g_rowSig[b][row] = sig;
|
|
3576
|
+
}
|
|
3577
|
+
g_rowSigRect[b] = {x0, y0, x1, y1};
|
|
3578
|
+
g_rowSigValid[b] = true;
|
|
3579
|
+
if (skipped) g_asyncScroll.job.skip = g_skipRow;
|
|
3580
|
+
} else {
|
|
3581
|
+
g_rowSigValid[b] = false;
|
|
3582
|
+
}
|
|
3583
|
+
g_frameIsScrollCopy = true;
|
|
3584
|
+
g_asyncScroll.startUs = esp_timer_get_time();
|
|
3585
|
+
if (!submitRenderWorkerJob(scrollCopyRows, &g_asyncScroll.job, y0, y1)) {
|
|
3586
|
+
g_rowSigValid[b] = false;
|
|
3587
|
+
g_frameIsScrollCopy = false;
|
|
3588
|
+
xSemaphoreGive(g_readyMutex);
|
|
3589
|
+
return false;
|
|
3590
|
+
}
|
|
3591
|
+
g_lastSubmitWasDrawBand = false;
|
|
3592
|
+
g_scrollCopyInFlight = true;
|
|
3593
|
+
g_directDrawDuringCopy = false;
|
|
3594
|
+
// Ready for drawing inside the viewport; the end call brings the rest of
|
|
3595
|
+
// the buffer up to date once the copy is done.
|
|
3596
|
+
g_backReady = true;
|
|
3597
|
+
xSemaphoreGive(g_readyMutex);
|
|
3598
|
+
return true;
|
|
3599
|
+
}
|
|
3600
|
+
|
|
3601
|
+
// GEADEV PANEL: the refill counters since the last read, then reset.
|
|
3602
|
+
// corrected: refills whose buffer or band differed from the driver's choice;
|
|
3603
|
+
// fallback: refills that took the driver's (no VSYNC yet, or the DMA position
|
|
3604
|
+
// unreadable); at_start/at_end: refills that began or ended while the DMA
|
|
3605
|
+
// was sending that buffer (current/next descriptor) -- at_start must stay 0.
|
|
3606
|
+
extern "C" void gea_display_panel_stats(char *out, int size)
|
|
3607
|
+
{
|
|
3608
|
+
const RefillStats r = g_refill;
|
|
3609
|
+
std::memset(&g_refill, 0, sizeof(g_refill));
|
|
3610
|
+
int n = std::snprintf(out, static_cast<std::size_t>(size), "vsync_dev_max_us=%d vsync_dev_gt200=%u ", static_cast<int>(g_vsyncLateMaxUs), static_cast<unsigned>(g_vsyncLate200));
|
|
3611
|
+
g_vsyncLateMaxUs = 0;
|
|
3612
|
+
g_vsyncLate200 = 0;
|
|
3613
|
+
n += std::snprintf(out + n, static_cast<std::size_t>(size - n), "restarts=%u skipped=%u realigned=%u short=%u/%u vs_at:", static_cast<unsigned>(g_vsyncRestarts), static_cast<unsigned>(g_vsyncSkipped), static_cast<unsigned>(g_vsyncRealigned), static_cast<unsigned>(g_shortFrames), static_cast<unsigned>(g_shortFramesLate));
|
|
3614
|
+
for (int i = 0; i < 32 && n > 0 && n < size; ++i)
|
|
3615
|
+
if (g_vsyncAt[i]) n += std::snprintf(out + n, static_cast<std::size_t>(size - n), " %d.%d:%u", i / 16, i % 16, static_cast<unsigned>(g_vsyncAt[i]));
|
|
3616
|
+
std::memset(g_vsyncAt, 0, sizeof(g_vsyncAt));
|
|
3617
|
+
g_vsyncRestarts = 0;
|
|
3618
|
+
g_vsyncSkipped = 0;
|
|
3619
|
+
g_vsyncRealigned = 0;
|
|
3620
|
+
g_shortFrames = 0;
|
|
3621
|
+
g_shortFramesLate = 0;
|
|
3622
|
+
// Slips: uptime_ms:bin/late_us/prev_late_us/eof_fills/prev_eof_fills (kept across reads).
|
|
3623
|
+
n += std::snprintf(out + n, static_cast<std::size_t>(size - n), " slips=%u", static_cast<unsigned>(g_slipCount));
|
|
3624
|
+
for (std::uint32_t i = g_slipCount > 6 ? g_slipCount - 6 : 0; i < g_slipCount && n > 0 && n < size; ++i) {
|
|
3625
|
+
const SlipEvent &e = g_slips[i % 6];
|
|
3626
|
+
n += std::snprintf(out + n, static_cast<std::size_t>(size - n), " %u:%u.%u/%u/%u/%u/%u", static_cast<unsigned>(e.ms), e.bin / 16u, e.bin % 16u,
|
|
3627
|
+
static_cast<unsigned>(e.lateUs), static_cast<unsigned>(e.prevLateUs), static_cast<unsigned>(e.eofFills), static_cast<unsigned>(e.prevEofFills));
|
|
3628
|
+
}
|
|
3629
|
+
n += std::snprintf(out + n, static_cast<std::size_t>(size - n), " ");
|
|
3630
|
+
n += std::snprintf(out + n, static_cast<std::size_t>(size - n), "fills=%u corrected=%u fallback=%u at_start=%u/%u at_end=%u/%u unknown=%u",
|
|
3631
|
+
static_cast<unsigned>(r.fills), static_cast<unsigned>(r.corrected), static_cast<unsigned>(r.fallback), static_cast<unsigned>(r.atStart[0]),
|
|
3632
|
+
static_cast<unsigned>(r.atStart[1]), static_cast<unsigned>(r.atEnd[0]),
|
|
3633
|
+
static_cast<unsigned>(r.atEnd[1]), static_cast<unsigned>(r.unknown));
|
|
3634
|
+
for (int i = 0; i < 32 && n > 0 && n < size; ++i)
|
|
3635
|
+
if (r.atStartPos[i]) n += std::snprintf(out + n, static_cast<std::size_t>(size - n), " s%d:%u", i, static_cast<unsigned>(r.atStartPos[i]));
|
|
3636
|
+
for (int i = 0; i < 32 && n > 0 && n < size; ++i)
|
|
3637
|
+
if (r.atEndPos[i]) n += std::snprintf(out + n, static_cast<std::size_t>(size - n), " e%d:%u", i, static_cast<unsigned>(r.atEndPos[i]));
|
|
3638
|
+
}
|
|
3639
|
+
|
|
3640
|
+
// The svg painter asks before splitting a paint across both cores.
|
|
3641
|
+
extern "C" bool gea_display_second_core_busy() { return g_scrollCopyInFlight; }
|
|
3642
|
+
|
|
3643
|
+
extern "C" void gea_display_scroll_rect_end()
|
|
3644
|
+
{
|
|
3645
|
+
if (!g_scrollCopyInFlight) return;
|
|
3646
|
+
// Only the worker copies: from this core, a copy competes with the bounce
|
|
3647
|
+
// refills for PSRAM and the panel shows thin glitch lines.
|
|
3648
|
+
gea_render_parallel_wait();
|
|
3649
|
+
g_scrollCopyInFlight = false;
|
|
3650
|
+
const DisplayFlushRect &r = g_asyncScroll.skip; // each row left the cache as it was copied
|
|
3651
|
+
g_swapDiag.copyUs += esp_timer_get_time() - g_asyncScroll.startUs;
|
|
3652
|
+
xSemaphoreTake(g_readyMutex, portMAX_DELAY);
|
|
3653
|
+
g_swapDiag.patchPx += patchBack(&r, 1); // the rest of the buffer, outside the viewport
|
|
3654
|
+
xSemaphoreGive(g_readyMutex);
|
|
3655
|
+
g_canvas.markDirty(r.x0, r.y0, r.x1, r.y1);
|
|
3656
|
+
markStale(r.x0, r.y0, r.x1, r.y1); // see Display::scrollRect
|
|
3657
|
+
if (!g_directDrawDuringCopy) g_cleanRect = r;
|
|
3658
|
+
// An unstaged draw may have left rows the signatures do not describe.
|
|
3659
|
+
else g_rowSigValid[g_drawIndex] = false;
|
|
3660
|
+
}
|
|
3661
|
+
|
|
3662
|
+
extern "C" void gea_display_scroll_rect_rows(const std::uint32_t *sig, int y0, int rows)
|
|
3663
|
+
{
|
|
3664
|
+
g_nextSig = sig;
|
|
3665
|
+
g_nextSigY0 = y0;
|
|
3666
|
+
g_nextSigRows = rows;
|
|
3667
|
+
}
|
|
3668
|
+
|
|
3669
|
+
// An unstaged replay drew into the framebuffer through the cache: the publish
|
|
3670
|
+
// must write the scroll viewport's rows back after all.
|
|
3671
|
+
extern "C" void gea_display_note_direct_draw()
|
|
3672
|
+
{
|
|
3673
|
+
forgetRowSums();
|
|
3674
|
+
g_directDrawDuringCopy = true;
|
|
3675
|
+
g_cleanRect.x1 = -1;
|
|
3676
|
+
}
|
|
3677
|
+
|
|
3678
|
+
void Display::resetScrollRegion()
|
|
3679
|
+
{
|
|
3680
|
+
if (g_ringScroll) {
|
|
3681
|
+
if (g_regionH > 0) {
|
|
3682
|
+
ensureBackReady();
|
|
3683
|
+
changeRegion(0, 0);
|
|
3684
|
+
}
|
|
3685
|
+
return;
|
|
3686
|
+
}
|
|
3687
|
+
replayCanvas().setScrollRegion(0, 0, 0);
|
|
3688
|
+
}
|
|
1328
3689
|
void Display::strokeRect(int x, int y, int w, int h, std::uint16_t color) { replayCanvas().strokeRect(x, y, w, h, color); }
|
|
1329
3690
|
void Display::fillCircle(int cx, int cy, int r, std::uint16_t color) { replayCanvas().fillCircle(cx, cy, r, color); }
|
|
1330
3691
|
void Display::strokeCircle(int cx, int cy, int r, std::uint16_t color) { replayCanvas().strokeCircle(cx, cy, r, color); }
|
|
@@ -1363,6 +3724,25 @@ DisplayFlushStageDetail Display::flushStageDetail() { return {}; }
|
|
|
1363
3724
|
|
|
1364
3725
|
} // namespace gea::platform::display
|
|
1365
3726
|
|
|
3727
|
+
extern "C" bool gea_display_flush_presents_frame() { return g_swapFbs; }
|
|
3728
|
+
|
|
3729
|
+
// The ring scroll moves no pixels and publishes the rotation itself, so a scroll
|
|
3730
|
+
// frame needs to flush only what it repainted -- flushing the whole viewport
|
|
3731
|
+
// would mark it stale and copy it into the next back buffer.
|
|
3732
|
+
extern "C" bool gea_display_scroll_rect_self_flushing() { return g_ringScroll; }
|
|
3733
|
+
|
|
3734
|
+
// Every framebuffer writer here maps rows through the ring, so content drawn on
|
|
3735
|
+
// a non-scroll frame lands correctly under a rotated region: the engine keeps
|
|
3736
|
+
// the region instead of resetting it (and repainting it) on such frames.
|
|
3737
|
+
extern "C" bool gea_display_scroll_region_persists() { return g_ringScroll; }
|
|
3738
|
+
|
|
3739
|
+
extern "C" const std::uint16_t *gea_display_presented_framebuffer(int *width, int *height)
|
|
3740
|
+
{
|
|
3741
|
+
if (width) *width = gea::platform::display::kWidth;
|
|
3742
|
+
if (height) *height = gea::platform::display::kHeight;
|
|
3743
|
+
return g_framebuffer ? g_framebuffer : g_drawFramebuffer;
|
|
3744
|
+
}
|
|
3745
|
+
|
|
1366
3746
|
// Full-screen PSRAM scratch for the static-backdrop cache. Without this hook the
|
|
1367
3747
|
// weak default in ui/render.cpp returns nullptr and the bake SILENTLY never runs —
|
|
1368
3748
|
// an animated-transform scene (css-3d-cube) then full-rebuilds + re-rasterizes the
|