M-Film: film simulation for Canon EOS cameras with Magic Lantern. 72 film stocks (Portra, Velvia, Tri-X, CineStill…), a film-camera interface and an in-camera darkroom that develops your RAWs. 5D Mark III, 6D, 600D, 650D, 700D, EOS M and more.
C analog-photographycamera-firmwarecanoncanon-5d-mark-ivcanon-5dmark2canon-6dcanon-eoscr2darkroomeos-mfilm-emulationfilm-lookfilm-photographyfilm-simulationfujifilm-recipeskodak-portralutmagic-lanternphotographyraw
Plate and HUD put together off-screen; Canon's screen held, and what it draws over ours put back at once (#64, #17, #44, #43, #19, #49)
Claude49e86382ee44
diff --git a/TESTING.md b/TESTING.mdindex be67d6a..1b63529 100644--- a/TESTING.md+++ b/TESTING.md@@ -105,6 +105,23 @@ ML/- A card bigger than 32 GB is formatted as exFAT. If a big card keeps saying this, a card of 32 GB orless is a good second try.+### If Canon's screen flickers over M-Film's++Some DSLRs keep drawing their own screen over M-Film's (the top plate, the HUD or a film OS page).+M-Film puts its own back as soon as it sees that. With **System → Debug log** on, `ML/FILMROLL.LOG`+says what it saw. A line with "seen N times so far" is written the 1st, 51st, 101st... time: N counts+the times, not the lines.+- `hud: the plate: Canon drew over rows 22-60 (39 of them), put back; seen 51 times so far, logged every 50th`+ (`film os:` on a film OS page).+- `Canon took its screen back, held again`: Canon let go of the hold M-Film keeps on its screen.+- `screen: Canon put its other buffer on the screen`: Canon showed its own copy of the screen in+ front of M-Film's, rather than drawing over it.+- `drawn over for 2 s on end ..., not checking for 5 s`: Canon draws there all the time, and M-Film+ stops putting its screen back for a while.++Attach the log and say when the flicker happens: when the camera is idle, when the meter moves,+after each shot, or when you switch screens.+### If you turned on Magic Lantern's consoleThe MENU button opens M-Film, so Magic Lantern's menu (where the console is switched off) can't bediff --git a/ml-dev/filmos/fos.h b/ml-dev/filmos/fos.hindex 1df742b..46df99e 100644--- a/ml-dev/filmos/fos.h+++ b/ml-dev/filmos/fos.h@@ -215,12 +215,19 @@ void fos_hud_clear(uint8_t *vram, int pitch, const struct fos_hud_palette *p);/* the top plate: a DSLR's back screen while you shoot through the viewfinder */void fos_hud_plate(uint8_t *vram, int pitch, const struct fos_hud *h, const struct fos_hud_palette *p);void fos_hud_plate_clear(uint8_t *vram, int pitch, const struct fos_hud_palette *p);+/* the same a band at a time, to put together off-screen: screen rows y0 .. y0 + n+ * into buf, whose first row is y0. The HUD leaves what it doesn't draw on as it+ * was; fos_hud_spans says where in row y it draws (x0, x1 pairs; returns 2 or 4) */+void fos_hud_draw_rows(uint8_t *buf, int pitch, int y0, int n, const struct fos_hud *h, const struct fos_hud_palette *p);+void fos_hud_plate_rows(uint8_t *buf, int pitch, int y0, int n, const struct fos_hud *h, const struct fos_hud_palette *p);+int fos_hud_spans(int y, int x[4]);/* ---------------------------------------------------------------- the UI */void fos_ui_init();void fos_ui_run(int screen);-void fos_ui_overdrawn(); /* the port saw something else draw on the screen: paint it all again */+/* the port saw something else draw on rows y0 .. y1 of the screen: they are put back at once */+void fos_ui_overdrawn(int y0, int y1);/* canister art, fos_film.c; quality 2 = everything, 1 = no texture* filtering, 0 = no reflections either (the UI lowers it on a slow camera) */diff --git a/ml-dev/filmos/fos_gfx.c b/ml-dev/filmos/fos_gfx.cindex 4c4bbbc..2df9826 100644--- a/ml-dev/filmos/fos_gfx.c+++ b/ml-dev/filmos/fos_gfx.c@@ -938,7 +938,7 @@ int fos_text_map(uint8_t *px, int pitch, int w, int h, const struct fos_font *f,if (prev) pen += kern_of(f, prev, ch) + track;int gx = ((pen + 8) >> 4) + g->ox, gy = y + g->oy;const uint8_t *p = f->bits + g->off;- int col = 0, row = 0;+ int col = 0, row = gy < h && gy + g->h > 0 ? 0 : g->h; /* none of it in px: skip it (the HUD draws in bands) */while (row < g->h){int b = *p++;diff --git a/ml-dev/filmos/fos_hud.c b/ml-dev/filmos/fos_hud.cindex 3d897a8..6635507 100644--- a/ml-dev/filmos/fos_hud.c+++ b/ml-dev/filmos/fos_hud.c@@ -3,8 +3,11 @@** Unlike the film OS screens this draws with Canon's palette (LiveView* shows through index 0, and Canon keeps drawing its own bits), so it- * takes the indices it may use from the caller and draws straight into- * the overlay buffer.+ * takes the indices it may use from the caller. The camera puts it+ * together off-screen a band of rows at a time (fos_hud_draw_rows,+ * fos_hud_plate_rows) and copies to the screen only the rows that changed:+ * drawn straight onto the screen, each redraw showed for a moment what it+ * starts with (#44, #64).*/#include "fos.h"@@ -18,20 +21,34 @@#define THIRD_PX 14 /* scale spacing per 1/3 stop */#define SCALE_TOP 26 /* y of f/22; f/1 sits 27 thirds below */+/* the focusing screen's box in the middle of the view */+#define BOX_N 129+#define BOX_X (VIEW_W / 2 - 64)+#define BOX_Y (VIEW_H / 2 - 64)+static uint8_t *vr;static int vp;static const struct fos_hud_palette *pal;+/* the band being drawn: screen rows org_y .. org_y + band_h, vr is org_y */+static int org_y = 0, band_h = H;static void fill(int x, int y, int w, int h, uint8_t c){+ y -= org_y;if (x < 0) { w += x; x = 0; }if (y < 0) { h += y; y = 0; }if (x + w > W) w = W - x;- if (y + h > H) h = H - y;+ if (y + h > band_h) h = band_h - y;for (int j = 0; j < h; j++)memset(vr + (y + j) * vp + x, c, w > 0 ? w : 0);}+static void band(uint8_t *buf, int pitch, int y0, int n, const struct fos_hud_palette *p)+{+ vr = buf; vp = pitch; pal = p;+ org_y = y0; band_h = n;+}+static int fstop_y(int index){return SCALE_TOP + (fos_nfstops - 1 - index) * THIRD_PX;@@ -44,7 +61,7 @@ static int text_gray(const struct fos_font *f, int x, int y, const char *s, intmap[0] = 0;for (int i = 1; i < 16; i++)map[i] = i < 3 ? 0 : pal->gray[pct * i / 15];- return fos_text_map(vr, vp, W, H, f, x, y, s, map, track);+ return fos_text_map(vr, vp, W, band_h, f, x, y - org_y, s, map, track);}static int text_led(const struct fos_font *f, int x, int y, const char *s, int on, int track)@@ -52,7 +69,7 @@ static int text_led(const struct fos_font *f, int x, int y, const char *s, int ouint8_t map[16];for (int i = 0; i < 16; i++)map[i] = i < 5 ? 0 : !on ? pal->gray[i < 10 ? 6 : 9] : i < 10 ? pal->dark_red : pal->red;- return fos_text_map(vr, vp, W, H, f, x, y, s, map, track);+ return fos_text_map(vr, vp, W, band_h, f, x, y - org_y, s, map, track);}/* ---------------------------------------------------------------- seven segments */@@ -89,6 +106,7 @@ static void seg_size(int w, int h, int t, int gap)* you can just see behind a real viewfinder mask */static void seg_digit(int x, int y, int bits, uint8_t on, uint8_t off){+ if (y - org_y >= band_h || y + DIG_H - org_y <= 0) return; /* not in this band */int half = DIG_H / 2;for (int r = 0; r < DIG_H; r++){@@ -155,7 +173,27 @@ static void needle(int y, uint8_t c)void fos_hud_draw(uint8_t *vram, int pitch, const struct fos_hud *h, const struct fos_hud_palette *p){- vr = vram; vp = pitch; pal = p;+ fos_hud_draw_rows(vram, pitch, 0, H, h, p);+}++#ifndef CONFIG_EOSM+/* the parts of screen row y the HUD draws on, as x0, x1 pairs: the meter+ * strip, the focusing screen's box and the LED bar (what fos_hud_clear+ * wipes). Returns how many numbers it put in x: 2 or 4. (The EOS M draws+ * its HUD straight onto the screen: filmroll.c) */+int fos_hud_spans(int y, int x[4])+{+ int n = 0;+ if (y >= VIEW_H) { x[0] = 0; x[1] = W; return 2; }+ if (y >= BOX_Y && y < BOX_Y + BOX_N) { x[n++] = BOX_X; x[n++] = BOX_X + BOX_N; }+ x[n++] = W - STRIP_W; x[n++] = W;+ return n;+}+#endif++void fos_hud_draw_rows(uint8_t *buf, int pitch, int y0, int rows, const struct fos_hud *h, const struct fos_hud_palette *p)+{+ band(buf, pitch, y0, rows, p);uint8_t bg = p->black;char label[8];@@ -253,15 +291,16 @@ void fos_hud_draw(uint8_t *vram, int pitch, const struct fos_hud *h, const struc}fill(cx - 28, cy, 57, 1, p->white);}+ band(vr, vp, 0, H, p);}/* make our strips transparent again so LiveView shows through */void fos_hud_clear(uint8_t *vram, int pitch, const struct fos_hud_palette *p){- vr = vram; vp = pitch; pal = p;+ band(vram, pitch, 0, H, p);fill(W - STRIP_W, 0, STRIP_W, VIEW_H, p->clear);fill(0, VIEW_H, W, BAR_H, p->clear);- fill(VIEW_W / 2 - 64, VIEW_H / 2 - 64, 129, 129, p->clear);+ fill(BOX_X, BOX_Y, BOX_N, BOX_N, p->clear);}/* ---------------------------------------------------------------- the top plate */@@ -277,7 +316,12 @@ void fos_hud_clear(uint8_t *vram, int pitch, const struct fos_hud_palette *p)* film and its speed. */void fos_hud_plate(uint8_t *vram, int pitch, const struct fos_hud *h, const struct fos_hud_palette *p){- vr = vram; vp = pitch; pal = p;+ fos_hud_plate_rows(vram, pitch, 0, H, h, p);+}++void fos_hud_plate_rows(uint8_t *buf, int pitch, int y0, int rows, const struct fos_hud *h, const struct fos_hud_palette *p)+{+ band(buf, pitch, y0, rows, p);char b[24];fill(0, 0, W, H, p->black);@@ -360,12 +404,13 @@ void fos_hud_plate(uint8_t *vram, int pitch, const struct fos_hud *h, const struconst char *hint = "TRASH Home PLAY Prints";int hw = fos_text_w(&fos_font_xs, hint, 24);text_gray(&fos_font_xs, W - 28 - hw, H - 22, hint, 36, 24);+ band(vr, vp, 0, H, p);}/* the plate off: Canon's screen shows again */void fos_hud_plate_clear(uint8_t *vram, int pitch, const struct fos_hud_palette *p){- vr = vram; vp = pitch; pal = p;+ band(vram, pitch, 0, H, p);fill(0, 0, W, H, p->clear);}#endifdiff --git a/ml-dev/filmos/fos_ui.c b/ml-dev/filmos/fos_ui.cindex 03cf98f..1301a02 100644--- a/ml-dev/filmos/fos_ui.c+++ b/ml-dev/filmos/fos_ui.c@@ -23,6 +23,7 @@ static uint8_t *buf_cv, *buf_bg, *buf_snap;static int screen, screen_from;static int dmg_y0 = FOS_H, dmg_y1 = 0;+static int cv_on_screen; /* the screen shows the canvas as it is (no screen change on its way) *//* screen change in progress */static int tr_how, tr_t, tr_len; /* tr_t counts up to tr_len (ms) */@@ -95,8 +96,18 @@ void ui_damage_all()ui_damage(0, FOS_H);}-void fos_ui_overdrawn()+/* Canon drew over the screen: the canvas still holds what was presented+ * last, so those rows go back from it now, before Canon's show for another+ * frame (#49, #64). In a screen change the next frame paints them anyway. */+void fos_ui_overdrawn(int y0, int y1){+ y0 = fos_clamp(y0, 0, FOS_H);+ y1 = fos_clamp(y1, y0, FOS_H);+ if (cv_on_screen && cv && y1 > y0)+ {+ fos_port_present(cv->px + y0 * FOS_W, FOS_W, y0, y1);+ return;+ }ui_clobbered = 1;ui_damage_all();}@@ -779,6 +790,7 @@ void fos_ui_run(int open)/* what is under the shutter while it closes: LiveView */memset(buf_cv, IX_CLEAR, FOS_W * FOS_H);ui_clobbered = 1;+ cv_on_screen = 0;fos_port_palette();boot_then_leave = open == FOS_OPEN_BOOT_LV;@@ -892,6 +904,7 @@ void fos_ui_run(int open)ui_iris(open_k, ang);ui_clobbered = 1;fos_port_present(cv->px, FOS_W, 0, FOS_H);+ cv_on_screen = 1;if (!opened) { opened = 1; stat.open_ms = fos_port_ms() - t_open; }dmg_y0 = FOS_H; dmg_y1 = 0;fos_port_sleep(frame_rest(now));@@ -905,6 +918,7 @@ void fos_ui_run(int open)t_present = fos_port_ms();present_transition((float) tr_t / tr_len);ui_clobbered = 1; /* the screen only reached the panel composed */+ cv_on_screen = 0;dmg_y0 = FOS_H; dmg_y1 = 0;}else@@ -925,6 +939,7 @@ void fos_ui_run(int open)if (dmg_y1 > dmg_y0){fos_port_present(cv->px + dmg_y0 * FOS_W, FOS_W, dmg_y0, dmg_y1);+ if (dmg_y0 == 0 && dmg_y1 == FOS_H) cv_on_screen = 1;if (!opened) { opened = 1; stat.open_ms = fos_port_ms() - t_open; }}dmg_y0 = FOS_H; dmg_y1 = 0;diff --git a/ml-dev/filmos/sim/sim.c b/ml-dev/filmos/sim/sim.cindex 55a0473..e087b1b 100644--- a/ml-dev/filmos/sim/sim.c+++ b/ml-dev/filmos/sim/sim.c@@ -48,9 +48,15 @@ int fos_port_ms() { return now_ms; }/* the palette on the screen now, 0xRRGGBB; each dumped frame gets a copy */static uint32_t cur_pal[256];+/* FOS_OVERDRAW (below): the rows "Canon" scribbled on, not painted again yet */+static uint8_t overdrawn_row[FOS_H];+static int overdraw_wait, overdraw_n, overdraw_missed, overdraw_shown;+static void dump(){char path[512];+ /* a frame that shows a scribble still there (FOS_OVERDRAW) */+ for (int y = 0; y < FOS_H; y++) if (overdrawn_row[y]) { overdraw_shown++; break; }if (!strcmp(outdir, "-")) { frame_no++; last_dump = now_ms; return; } /* measuring only */snprintf(path, sizeof(path), "%s/f_%05d_%06d.idx", outdir, frame_no, now_ms);FILE *f = fopen(path, "wb");@@ -64,12 +70,10 @@ static void dump()}/* FOS_OVERDRAW=1: "Canon" scribbles on the screen every 700 ms, as the 60D- * and 550D do, and the port tells the film OS (fos_ui_overdrawn), as the- * camera's check does. Every scribbled row must be painted again within- * three frames, or the screen on show doesn't repaint all of itself. */-static uint8_t overdrawn_row[FOS_H];-static int overdraw_wait, overdraw_n, overdraw_missed;-+ * and 550D do, and the port tells the film OS which rows (fos_ui_overdrawn),+ * as the camera's check does. The film OS puts them back at once, or in a+ * screen change with its next frames: every scribbled row must be painted+ * again within three frames, or the screen on show doesn't repaint all of itself. */static void overdraw_step(){static int on = -1, last = 0;@@ -96,7 +100,7 @@ static void overdraw_step()}overdraw_n++;overdraw_wait = 3;- fos_ui_overdrawn();+ fos_ui_overdrawn(y0, y0 + 30);}void fos_port_sleep(int ms)@@ -279,6 +283,6 @@ int main(int argc, char **argv)fos_ui_run(open);if (frame_dirty) dump();fprintf(stderr, "%d frames, %d ms\n", frame_no, now_ms);- if (overdraw_n) fprintf(stderr, "overdraw: %d scribbles, %d not painted again\n", overdraw_n, overdraw_missed);+ if (overdraw_n) fprintf(stderr, "overdraw: %d scribbles, %d not painted again, %d frames showed one\n", overdraw_n, overdraw_missed, overdraw_shown);return 0;}diff --git a/ml-dev/fork/src/filmroll.c b/ml-dev/fork/src/filmroll.cindex 60362f5..9a339fb 100644--- a/ml-dev/fork/src/filmroll.c+++ b/ml-dev/fork/src/filmroll.c@@ -483,6 +483,9 @@ static void poll_frames()static volatile int ui_running = 0; /* the film OS owns the screen and the buttons */static volatile int ui_abort = 0;+#if !FOS_LV_ONLY+static volatile int canon_handover = 0; /* the film OS closed with Canon's screen still held, for the HUD or the plate (hud_step) */+#endifstatic int ui_after = 0; /* what to do once it has let go */static int ui_last_mode_poke = 0;static int ui_last_palette = 0;@@ -554,14 +557,21 @@ static void palette_canon()/* Take the display from Canon's GUI the same way the ML menu does:* switch Canon to the ML menu GUI mode (LiveView stops drawing its* overlays and stops handling keys), stop Canon from flipping its own- * bitmap buffer, then turn the bitmap layer back on for us. */+ * bitmap buffer, then turn the bitmap layer back on for us. Its buffer is+ * held from before the switch, and again while it lands: outside LiveView+ * that mode is Canon's own menu, which showed for a moment as the film OS+ * opened (#19), and opening it can let go of the hold. */static void film_ui_open(){NotifyBoxHide();if (lv) bmp_off();- SetGUIRequestMode(GUIMODE_ML_MENU);- msleep(200);canon_gui_disable_front_buffer();+ SetGUIRequestMode(GUIMODE_ML_MENU);+ for (int i = 0; i < 10; i++)+ {+ msleep(20);+ canon_gui_disable_front_buffer(); /* nothing to do while it is still held */+ }CancelDateTimer();clrscr();bmp_on();@@ -574,6 +584,19 @@ static void film_ui_close()if (lv) bmp_off();SetGUIRequestMode(0);msleep(100);+#if !FOS_LV_ONLY+ /* back to the HUD or the plate: Canon's screen stays held, so it doesn't+ * show between ours (#19, #64). hud_step gives it back if ours doesn't+ * come; Canon's menu (AFTER_CANON_MENU) gets it now. */+ if (hud_enabled && roll_loaded && DISPLAY_IS_ON && !is_movie_mode() && !(ui_after & AFTER_CANON_MENU))+ {+ canon_gui_disable_front_buffer();+ clrscr();+ canon_handover = 1;+ if (lv) bmp_on();+ return;+ }+#endifcanon_gui_enable_front_buffer(0);clrscr();redraw();@@ -646,7 +669,7 @@ int fos_port_ms()return get_ms_clock();}-static void ui_overdrawn_check();+static void ui_overdrawn_check(int at_once);void fos_port_sleep(int ms){@@ -673,7 +696,16 @@ void fos_port_sleep(int ms)palette_ours();ui_last_palette = now;}- ui_overdrawn_check();+ /* Canon can let go of the hold on its screen by itself (a dialog of its+ * own changing under us): take it again, and look for what it drew now */+ int let_go = !canon_gui_front_buffer_disabled();+ if (let_go)+ {+ static int times = 0;+ canon_gui_disable_front_buffer();+ if (times++ % 50 == 0) dbg("film os: Canon took its screen back, held again (seen %d time%s so far)", times, times == 1 ? "" : "s");+ }+ ui_overdrawn_check(let_go);}int fos_port_key()@@ -710,15 +742,18 @@ int fos_port_abort()/* Canon drawing over our screen. On some DSLRs Canon keeps drawing its own* screen straight onto the one we show, with its buffer held or not: over- * the top plate on the 50D and 500D (#43 #44), over the film OS on the 60D- * (its exposure scale, #25 #49) and the 550D (its battery icon, #50). Each- * row we put on the screen leaves a sum of what it holds (the film OS's rows- * as they're presented, the plate's after it is drawn: the two never show at- * once, so they share it). Every 50 ms half the rows are read back; a row- * that no longer matches means Canon drew there, and the screen is painted- * again at once. If it never matches for 2 s (Canon drawing all the time,- * or a camera that doesn't read back what we wrote), the check rests for- * 5 s, then 10, up to a minute, so it can't keep painting without end. */+ * the top plate on the 50D, 500D, 550D, 600D and 6D (#43 #44 #64 #17), over+ * the film OS on the 60D (its exposure scale, #25 #49), the 550D (its+ * battery icon, #50) and the 600D (#64). Each row we put on the screen+ * leaves a sum of what it holds (the film OS's rows as they're presented,+ * the plate's as they're copied: the two never show at once, so they share+ * it). Every 50 ms half the rows are read back; once a row no longer+ * matches, all of them are, and the rows Canon drew on are put back at+ * once, from what we drew (the film OS's canvas, the plate's rows). If it+ * never matches for 2 s (Canon drawing all the time, or a camera that+ * doesn't read back what we wrote), the check rests for 5 s, then 10, up to+ * a minute, so it can't keep painting without end. The log counts how many+ * times it found Canon's drawing and says on which rows, every 50th time. */static uint16_t shown_sum[FOS_H]; /* 16 bits a row: 1 KB less at boot, and a 1 in 65,536 miss */static uint16_t row_sum(const uint8_t *row)@@ -732,8 +767,25 @@ static uint16_t row_sum(const uint8_t *row)struct overdraw_check { const char *what; int half, in_a_row, rest, resting_until, times; };-/* 1 if Canon has drawn over vram since its rows' sums were taken */-static int overdrawn(struct overdraw_check *o, const uint8_t *vram)+/* 1 if the buffer on the screen isn't the one we last drew in: Canon put+ * its other one in front (its screen, not ours, until we draw it there).+ * Logged, so a tester's log tells that apart from Canon drawing over ours. */+static int vram_moved(const uint8_t *vram)+{+ static const uint8_t *last = 0;+ static int times = 0;+ if (vram == last) return 0;+ if (last && times++ % 50 == 0)+ dbg("screen: Canon put its other buffer on the screen, %x -> %x (seen %d time%s so far)",+ (uint32_t) (uintptr_t) last, (uint32_t) (uintptr_t) vram, times, times == 1 ? "" : "s");+ last = vram;+ return 1;+}++/* how many rows of vram Canon has drawn over since their sums were taken:+ * the first and the last in *y0, *y1, each marked in stale (a bit a row)+ * if it is given */+static int overdrawn(struct overdraw_check *o, const uint8_t *vram, uint8_t *stale, int *y0, int *y1){int now = get_ms_clock();if (o->resting_until && now - o->resting_until < 0) return 0;@@ -743,7 +795,16 @@ static int overdrawn(struct overdraw_check *o, const uint8_t *vram)for (; y < FOS_H; y += 2)if (row_sum(vram + y * BMPPITCH) != shown_sum[y])break;- if (y >= FOS_H)+ int n = 0;+ if (y < FOS_H)+ for (y = 0; y < FOS_H; y++)+ if (row_sum(vram + y * BMPPITCH) != shown_sum[y])+ {+ if (!n++) *y0 = y;+ *y1 = y;+ if (stale) stale[y >> 3] |= 1 << (y & 7);+ }+ if (!n){o->in_a_row = 0;o->rest = 0;@@ -754,24 +815,29 @@ static int overdrawn(struct overdraw_check *o, const uint8_t *vram)o->in_a_row = 0;o->rest = o->rest ? MIN(o->rest * 2, 60000) : 5000;o->resting_until = (now + o->rest) ? now + o->rest : 1;- dbg("%s: drawn over for 2 s on end (row %d), not checking for %d s", o->what, y, o->rest / 1000);+ dbg("%s: drawn over for 2 s on end (rows %d-%d), not checking for %d s", o->what, *y0, *y1, o->rest / 1000);return 0;}- if (o->times++ % 50 == 0) dbg("%s: Canon drew over it (%d times, row %d)", o->what, o->times, y);- return 1;+ if (o->times++ % 50 == 0)+ dbg("%s: Canon drew over rows %d-%d (%d of them), put back; seen %d time%s so far, logged every 50th",+ o->what, *y0, *y1, n, o->times, o->times == 1 ? "" : "s");+ return n;}-static void ui_overdrawn_check()+static void ui_overdrawn_check(int at_once){static struct overdraw_check check = { .what = "film os" };static int last = 0;int now = get_ms_clock();- if (now - last < 50) return;+ if (now - last < 50 && !at_once) return;last = now;uint8_t *vram = bmp_vram_real();if (!vram || !DISPLAY_IS_ON) return;- if (overdrawn(&check, vram))- fos_ui_overdrawn();+ int y0, y1;+ if (vram_moved(vram))+ fos_ui_overdrawn(0, FOS_H);+ else if (overdrawn(&check, vram, 0, &y0, &y1))+ fos_ui_overdrawn(y0, y1 + 1);}void fos_port_present(const uint8_t *rows, int pitch, int y0, int y1)@@ -1196,7 +1262,144 @@ static void shutter_led(char *buf, int size)buf[n] = 0;}-static void hud_draw(int lit, int blink, int plate)+static int hud_stale = 0; /* some rows are stale, nothing else changed: copy just those */++#if !FOS_LV_ONLY+/* On a DSLR the HUD and the plate are put together off-screen, a band of+ * rows at a time, and only the rows that differ from what is on the screen+ * go to it. Drawn straight onto the screen, each redraw showed for a moment+ * what it starts with: the plate's black, then its digits, the HUD's strip+ * without its scale. The plate was redrawn so every 400 ms for its blink,+ * and on each of Canon's overdraws: the meter and the settings flickering+ * to black and back (#44, #64). Rows Canon drew over, or that were wiped,+ * are stale: they go to the screen whatever their sum. The EOS M draws its+ * HUD straight onto the screen as before: its boot memory is short+ * (MEMORY.md), and no flicker has been reported there. */+#define BAND 48 /* rows at a time: 34 KB, from malloc the first time */+static uint8_t *band;+static uint32_t *put_sum; /* each row as we last put it on the screen, all its pixels (in band's block) */+static uint8_t *stale; /* a bit a row (in band's block) */++static void hud_all_stale()+{+ if (stale) memset(stale, 0xFF, FOS_H / 8);+}++/* the pixels of a row in [x0, x1) pairs, to tell when it changed */+static uint32_t span_sum(const uint8_t *row, const int *x, int n)+{+ uint32_t a = 1, b = 0;+ for (int i = 0; i < n; i += 2)+ {+ int j = x[i], end = x[i + 1];+ for (; j < end && (j & 3); j++) { a += row[j]; b += a; }+ for (; j + 4 <= end; j += 4) { a += *(const uint32_t *) (row + j); b += a; }+ for (; j < end; j++) { a += row[j]; b += a; }+ }+ return a ^ (b << 16) ^ (b >> 16);+}+#else+static void hud_all_stale() {}+#endif++/* The HUD (plate 0) or the plate onto the screen: the rows that changed and+ * the stale ones, or every row (all). only_stale: nothing on it changed, so+ * only the bands with stale rows need putting together. Returns how many+ * rows it copied, -1 if it drew straight onto the screen (no memory). */+static int hud_put(const struct fos_hud *h, int plate, int all, int only_stale)+{+ uint8_t *vram = bmp_vram();+ if (!vram) return 0;+#if FOS_LV_ONLY+ (void) plate; (void) all; (void) only_stale;+ fos_hud_draw(vram, BMPPITCH, h, &hud_pal);+ return -1;+#else+ static int no_band = 0;+ if (!band && !no_band)+ {+ band = malloc(BAND * FOS_W + FOS_H * sizeof(uint32_t) + FOS_H / 8);+ if (band)+ {+ put_sum = (uint32_t *) (band + BAND * FOS_W);+ stale = (uint8_t *) (put_sum + FOS_H);+ hud_all_stale();+ }+ else+ {+ no_band = 1;+ dbg("hud: no memory to put it together off-screen, drawing it straight onto the screen");+ }+ }+ if (!band)+ {+ if (plate) fos_hud_plate(vram, BMPPITCH, h, &hud_pal);+ else fos_hud_draw(vram, BMPPITCH, h, &hud_pal);+ return -1;+ }++ if (vram_moved(vram)) hud_all_stale();+ int copied = 0;+ for (int y0 = 0; y0 < FOS_H; y0 += BAND)+ {+ int n = MIN(BAND, FOS_H - y0);+ if (only_stale && !all)+ {+ int any = 0;+ for (int y = y0; y < y0 + n; y += 8) any |= stale[y >> 3];+ if (!any) continue;+ }+ memset(band, hud_pal.clear, n * FOS_W);+ if (plate) fos_hud_plate_rows(band, FOS_W, y0, n, h, &hud_pal);+ else fos_hud_draw_rows(band, FOS_W, y0, n, h, &hud_pal);+ for (int y = y0; y < y0 + n; y++)+ {+ int x[4] = { 0, FOS_W }, nx = plate ? 2 : fos_hud_spans(y, x);+ const uint8_t *src = band + (y - y0) * FOS_W;+ uint32_t sum = span_sum(src, x, nx);+ uint8_t bit = 1 << (y & 7);+ if (!all && !(stale[y >> 3] & bit) && sum == put_sum[y]) continue;+ uint8_t *dst = vram + y * BMPPITCH;+ for (int i = 0; i < nx; i += 2)+ memcpy(dst + x[i], src + x[i], x[i + 1] - x[i]);+ put_sum[y] = sum;+ stale[y >> 3] &= ~bit;+ if (plate) shown_sum[y] = row_sum(dst); /* for the check, as it reads back */+ copied++;+ }+ }+ return copied;+#endif+}++#if !FOS_LV_ONLY+/* The top plate's watchdog (Canon drawing over our screen, above): checked+ * every 50 ms while the plate is up, from hud_step and the main loop. */+static volatile int plate_on_screen = 0;++static void plate_sums()+{+ uint8_t *vram = bmp_vram();+ if (!vram) return;+ for (int y = 0; y < FOS_H; y++)+ shown_sum[y] = row_sum(vram + y * BMPPITCH);+}++/* 1 if Canon drew over the plate: its rows are stale, for hud_step to put back */+static int plate_overdrawn()+{+ static struct overdraw_check check = { .what = "hud: the plate" };+ if (!plate_on_screen || hud_dirty) return 0; /* not up, or about to be drawn anyway */+ uint8_t *vram = bmp_vram();+ int y0, y1;+ if (!vram || !overdrawn(&check, vram, stale, &y0, &y1)) return 0;+ if (stale) hud_stale = 1;+ else hud_dirty = 1; /* drawn straight onto the screen: all of it again */+ return 1;+}+#endif++static void hud_draw(int lit, int blink, int plate, int all, int only_stale){struct fos_hud h;memset(&h, 0, sizeof(h));@@ -1218,12 +1421,12 @@ static void hud_draw(int lit, int blink, int plate)h.focus_screen = focus_screen;h.film = fos_films[COERCE(roll_film, 0, fos_nfilms - 1)].name;h.iso = fos_films[COERCE(roll_film, 0, fos_nfilms - 1)].box_iso;- uint8_t *vram = bmp_vram();+ int put = hud_put(&h, plate, all, only_stale);#if !FOS_LV_ONLY- if (vram && plate) fos_hud_plate(vram, BMPPITCH, &h, &hud_pal);- else+ if (put < 0 && plate) plate_sums();+#else+ (void) put;#endif- if (vram) fos_hud_draw(vram, BMPPITCH, &h, &hud_pal);}static void hud_clear()@@ -1231,37 +1434,14 @@ static void hud_clear()uint8_t *vram = bmp_vram();if (vram)fos_hud_clear(vram, BMPPITCH, &hud_pal);+ hud_all_stale();}-#if !FOS_LV_ONLY-/* The top plate's watchdog (Canon drawing over our screen, above): checked- * every 50 ms while the plate is up, from hud_step and the main loop. */-static volatile int plate_on_screen = 0;--static void plate_sums()-{- uint8_t *vram = bmp_vram();- if (!vram) return;- for (int y = 0; y < FOS_H; y++)- shown_sum[y] = row_sum(vram + y * BMPPITCH);-}--static int plate_overdrawn()-{- static struct overdraw_check check = { .what = "hud: the plate" };- if (!plate_on_screen || hud_dirty) return 0; /* not up, or about to be drawn anyway */- uint8_t *vram = bmp_vram();- if (!vram || !overdrawn(&check, vram)) return 0;- hud_dirty = 1;- return 1;-}-#endif-/* one HUD refresh; called from the main loop */static void hud_step(){static int last_sig = -1;- static int last_draw = 0;+ static int last_all = 0;static int drawn = 0;static int was_visible = -1;static int plate_drawn = 0;@@ -1273,6 +1453,7 @@ static void hud_step()dbg("hud: visible %d plate %d (enabled %d roll %d lv %d movie %d ui %d)",visible, plate, hud_enabled, roll_loaded, lv, is_movie_mode(), ui_running);was_visible = visible * 2 + plate;+ hud_all_stale(); /* someone else's screen was there */}hud_showing = visible;@@ -1283,24 +1464,43 @@ static void hud_step()* HUD or the plate is up, stop Canon flipping its buffer, as the ML menu* does, and wipe what it left. Canon gets the screen back as soon as ours* goes (a Canon menu, playback, the display off). The film OS's own pages- * take the buffer themselves (film_ui_open). */+ * take the buffer themselves (film_ui_open), and leave it held for ours+ * when they close (film_ui_close). *//* Canon's screen comes back only once ours has been gone a moment: the* display isn't idle for a blink while a frame is taken, and giving the* screen back then (and taking it again) flashed Canon's screen, and its* picture of the frame, over ours (#17). The film OS's pages take the* screen at once, and so does anything that isn't a blink. */static int canon_held = 0, gone_since = 0;+ if (canon_handover && !ui_running) /* once the film OS has gone, or the hold would be dropped below */+ {+ canon_handover = 0;+ canon_held = 1;+ dbg("hud: Canon's screen still held from the film OS");+ }if (visible){gone_since = 0;if (!canon_held){canon_gui_disable_front_buffer();- clrscr();+ if (!plate) clrscr(); /* the plate covers all of it */canon_held = 1;+ hud_all_stale();hud_dirty = 1;dbg("hud: Canon's screen held");}+ else if (!canon_gui_front_buffer_disabled())+ {+ /* Canon let go of the hold by itself, and would draw over ours+ * from now on (#64): take it again, and put all of ours back */+ static int times = 0;+ canon_gui_disable_front_buffer();+ if (!plate) clrscr();+ hud_all_stale();+ hud_stale = 1;+ if (times++ % 50 == 0) dbg("hud: Canon took its screen back, held again (seen %d time%s so far)", times, times == 1 ? "" : "s");+ }}else if (canon_held){@@ -1334,6 +1534,7 @@ static void hud_step()#if !FOS_LV_ONLYplate_on_screen = 0;#endif+ hud_all_stale();last_sig = -1;}@@ -1361,17 +1562,21 @@ static void hud_step()int sig = lit | blink << 1 | need_wind << 2 | (frames_shot & 0xFF) << 3| lens_info.raw_shutter << 11 | (ev8 & 0xFF) << 19 | (current_fstop() & 0x1F) << 27;- /* redraw on change, and every second to win over Canon's overlays */- if (sig != last_sig || hud_dirty || now - last_draw > 1000)+ /* put together on change (the rows that changed go to the screen), the+ * stale rows put back, and every second all of it, to win over Canon's+ * overlays where the check doesn't see them */+ int changed = sig != last_sig || hud_dirty;+ int all = now - last_all > 1000;+ if (changed || all || hud_stale){hud_dirty = 0;+ hud_stale = 0;last_sig = sig;- last_draw = now;- hud_draw(lit, blink, plate);+ if (all || FOS_LV_ONLY) last_all = now; /* the EOS M draws all of it each time */+ hud_draw(lit, blink, plate, all, !changed);plate_drawn = plate;#if !FOS_LV_ONLYplate_on_screen = plate;- if (plate) plate_sums();#endif}}