vga.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /* VGA: VESA LFB (from boot) with a shadow buffer and a damage-rect flush. */
  2. #include "vga.h"
  3. #include "blit.h"
  4. #include "pci.h"
  5. #include "gpu.h"
  6. #include "ati.h"
  7. #include "paging.h"
  8. #include "process.h"
  9. #include "serial.h"
  10. /* Cached boot framebuffer info (set once when vga_fb_* first used). */
  11. static unsigned int vga_fb_w, vga_fb_h, vga_fb_pitch_val, vga_fb_bpp_val;
  12. static unsigned int vga_fb_vram_kb_val;
  13. static volatile unsigned char *vga_fb_lfb;
  14. static int vga_fb_cached;
  15. /* Shadow buffer state. */
  16. static unsigned char *vga_shadow_buf;
  17. static unsigned int vga_shadow_pitch;
  18. static unsigned int vga_shadow_size;
  19. /* Damage rect list. Each entry is a half-open [x0,x1) x [y0,y1) region of
  20. * the shadow buffer that has not been copied to VRAM yet. Entries merge when
  21. * they overlap, touch or would barely waste space, so related marks (the
  22. * per-pixel writes of one glyph or cursor draw, the rectangles of one window
  23. * repaint) collapse into a few rects, while unrelated damage (a window and the
  24. * cursor on the other side of the screen) stays separate and the flush copies
  25. * each row only over the columns that changed. A rect that is only partially
  26. * flushed keeps its remaining rows and moves behind the newer damage. */
  27. #define DAMAGE_SLOTS 16
  28. struct damage_rect {
  29. unsigned int x0, y0, x1, y1;
  30. };
  31. static struct damage_rect damage_rects[DAMAGE_SLOTS];
  32. static unsigned int damage_count;
  33. static void damage_add(unsigned int x0, unsigned int y0,
  34. unsigned int x1, unsigned int y1)
  35. {
  36. unsigned int i;
  37. if (x1 > vga_fb_w)
  38. x1 = vga_fb_w;
  39. if (y1 > vga_fb_h)
  40. y1 = vga_fb_h;
  41. if (x0 >= x1 || y0 >= y1)
  42. return;
  43. /* Already covered? Scanning backwards catches the repeated per-pixel
  44. marks of a glyph or cursor draw in one step. */
  45. for (i = damage_count; i-- > 0; ) {
  46. struct damage_rect *d = &damage_rects[i];
  47. if (x0 >= d->x0 && y0 >= d->y0 && x1 <= d->x1 && y1 <= d->y1)
  48. return;
  49. }
  50. /* Merge with every rect this one touches; a merge can bring another
  51. rect into reach, so restart the scan after each one. */
  52. for (i = 0; i < damage_count; ) {
  53. struct damage_rect *d = &damage_rects[i];
  54. unsigned int nx0 = (x0 < d->x0) ? x0 : d->x0;
  55. unsigned int ny0 = (y0 < d->y0) ? y0 : d->y0;
  56. unsigned int nx1 = (x1 > d->x1) ? x1 : d->x1;
  57. unsigned int ny1 = (y1 > d->y1) ? y1 : d->y1;
  58. unsigned int merged_area = (nx1 - nx0) * (ny1 - ny0);
  59. unsigned int area = (d->x1 - d->x0) * (d->y1 - d->y0) +
  60. (x1 - x0) * (y1 - y0);
  61. if (nx0 <= d->x1 && d->x0 <= nx1 &&
  62. ny0 <= d->y1 && d->y0 <= ny1 &&
  63. merged_area <= 2 * area) {
  64. x0 = nx0;
  65. y0 = ny0;
  66. x1 = nx1;
  67. y1 = ny1;
  68. damage_rects[i] = damage_rects[--damage_count];
  69. i = 0;
  70. continue;
  71. }
  72. i++;
  73. }
  74. if (damage_count < DAMAGE_SLOTS) {
  75. damage_rects[damage_count].x0 = x0;
  76. damage_rects[damage_count].y0 = y0;
  77. damage_rects[damage_count].x1 = x1;
  78. damage_rects[damage_count].y1 = y1;
  79. damage_count++;
  80. return;
  81. }
  82. /* List full: extend whichever rect grows the least (correct, just a
  83. few redundant pixels copied). */
  84. {
  85. unsigned int best = 0, best_grow = 0xFFFFFFFFu;
  86. for (i = 0; i < DAMAGE_SLOTS; i++) {
  87. struct damage_rect *d = &damage_rects[i];
  88. unsigned int nx0 = (x0 < d->x0) ? x0 : d->x0;
  89. unsigned int ny0 = (y0 < d->y0) ? y0 : d->y0;
  90. unsigned int nx1 = (x1 > d->x1) ? x1 : d->x1;
  91. unsigned int ny1 = (y1 > d->y1) ? y1 : d->y1;
  92. unsigned int grow = (nx1 - nx0) * (ny1 - ny0) -
  93. (d->x1 - d->x0) * (d->y1 - d->y0);
  94. if (grow < best_grow) {
  95. best_grow = grow;
  96. best = i;
  97. }
  98. }
  99. {
  100. struct damage_rect *d = &damage_rects[best];
  101. if (x0 < d->x0) d->x0 = x0;
  102. if (y0 < d->y0) d->y0 = y0;
  103. if (x1 > d->x1) d->x1 = x1;
  104. if (y1 > d->y1) d->y1 = y1;
  105. }
  106. }
  107. }
  108. static int damage_any(void)
  109. {
  110. return damage_count != 0;
  111. }
  112. static void damage_clear(void)
  113. {
  114. damage_count = 0;
  115. }
  116. static void mark_dirty_rect_xy(unsigned int y, unsigned int h,
  117. unsigned int x0, unsigned int x1)
  118. {
  119. damage_add(x0, y, x1, y + h);
  120. }
  121. /* Full-width dirty rows (used where the x-extent isn't tracked). */
  122. static void mark_dirty_rect(unsigned int y, unsigned int h)
  123. {
  124. damage_add(0, y, vga_fb_w, y + h);
  125. }
  126. /*
  127. * Force CRTC Start Address to 0 and Line Compare to max (0x3FF).
  128. * Fixes real hardware where VBE mode set leaves stale CRTC state,
  129. * causing the display to appear rotated (bottom portion at top).
  130. */
  131. static void vga_reset_crtc(void)
  132. {
  133. unsigned char v;
  134. /* Start Address High/Low = 0 */
  135. port_outb(0x3D4, 0x0C);
  136. port_outb(0x3D5, 0x00);
  137. port_outb(0x3D4, 0x0D);
  138. port_outb(0x3D5, 0x00);
  139. /* Unlock CRTC registers 0-7: clear bit 7 of register 0x11 */
  140. port_outb(0x3D4, 0x11);
  141. v = port_inb(0x3D5);
  142. port_outb(0x3D5, v & 0x7F);
  143. /* Line Compare bits 7:0 = 0xFF (register 0x18) */
  144. port_outb(0x3D4, 0x18);
  145. port_outb(0x3D5, 0xFF);
  146. /* Line Compare bit 8 = 1 (register 0x07, bit 4) */
  147. port_outb(0x3D4, 0x07);
  148. v = port_inb(0x3D5);
  149. port_outb(0x3D5, v | 0x10);
  150. /* Line Compare bit 9 = 1 (register 0x09, bit 6) */
  151. port_outb(0x3D4, 0x09);
  152. v = port_inb(0x3D5);
  153. port_outb(0x3D5, v | 0x40);
  154. }
  155. static volatile struct vga_boot_fb_info *vga_get_boot_fb(void)
  156. {
  157. volatile struct vga_boot_fb_info *p = VGA_BOOT_FB;
  158. __asm__ volatile ("" : "+r"(p));
  159. return p;
  160. }
  161. static void vga_cache_boot_fb(void)
  162. {
  163. volatile struct vga_boot_fb_info *info;
  164. if (vga_fb_cached)
  165. return;
  166. vga_fb_cached = 1;
  167. info = vga_get_boot_fb();
  168. if (!info->valid) {
  169. vga_fb_w = vga_fb_h = vga_fb_pitch_val = vga_fb_bpp_val = 0;
  170. vga_fb_lfb = 0;
  171. gpu_init(0);
  172. return;
  173. }
  174. vga_fb_w = info->width;
  175. vga_fb_h = info->height;
  176. vga_fb_pitch_val = info->pitch;
  177. vga_fb_bpp_val = info->bpp;
  178. /* Some BIOSes (e.g. 1024x768) report pitch in pixels or too small; enforce minimum. */
  179. {
  180. unsigned int min_pitch = vga_fb_w;
  181. if (vga_fb_bpp_val == 16)
  182. min_pitch *= 2;
  183. else if (vga_fb_bpp_val == 24)
  184. min_pitch *= 3;
  185. else if (vga_fb_bpp_val == 32)
  186. min_pitch *= 4;
  187. if (vga_fb_pitch_val < min_pitch)
  188. vga_fb_pitch_val = min_pitch;
  189. }
  190. vga_fb_lfb = (volatile unsigned char *)(unsigned long)info->lfb_base;
  191. vga_fb_vram_kb_val = (unsigned int)info->vram_64k * 64;
  192. /* Clamp height to what fits in VRAM (prevents wrap on small VRAM cards) */
  193. if (vga_fb_vram_kb_val > 0 && vga_fb_pitch_val > 0) {
  194. unsigned int vram_bytes = vga_fb_vram_kb_val * 1024;
  195. unsigned int max_h = vram_bytes / vga_fb_pitch_val;
  196. if (max_h < vga_fb_h)
  197. vga_fb_h = max_h;
  198. }
  199. gpu_init(info->lfb_base);
  200. if (gpu_vendor() == GPU_VENDOR_ATI && !ati_is_radeon())
  201. vga_reset_crtc();
  202. }
  203. /* ---- LFB API ---- */
  204. unsigned int vga_fb_width(void)
  205. {
  206. vga_cache_boot_fb();
  207. return vga_fb_w;
  208. }
  209. unsigned int vga_fb_height(void)
  210. {
  211. vga_cache_boot_fb();
  212. return vga_fb_h;
  213. }
  214. unsigned int vga_fb_pitch(void)
  215. {
  216. vga_cache_boot_fb();
  217. return vga_fb_pitch_val;
  218. }
  219. unsigned int vga_fb_bpp(void)
  220. {
  221. vga_cache_boot_fb();
  222. return vga_fb_bpp_val;
  223. }
  224. unsigned int vga_fb_vram_kb(void)
  225. {
  226. vga_cache_boot_fb();
  227. return vga_fb_vram_kb_val;
  228. }
  229. unsigned int vga_fb_pci_id(void)
  230. {
  231. vga_cache_boot_fb();
  232. return gpu_pci_id();
  233. }
  234. void *vga_fb_ptr(void)
  235. {
  236. vga_cache_boot_fb();
  237. if (vga_shadow_buf)
  238. return (void *)vga_shadow_buf;
  239. return (void *)vga_fb_lfb;
  240. }
  241. unsigned long vga_fb_lfb_addr(void)
  242. {
  243. vga_cache_boot_fb();
  244. return (unsigned long)vga_fb_lfb;
  245. }
  246. int vga_has_vesa(void)
  247. {
  248. vga_cache_boot_fb();
  249. return (vga_fb_w != 0 && vga_fb_lfb != 0);
  250. }
  251. /* ---- Surfaces and the render target ---- */
  252. /* The screen surface: the shadow buffer (or the LFB when there is none).
  253. * Kept in sync by vga_shadow_init() invalidating it. */
  254. static struct surface screen_surf;
  255. struct surface *vga_screen_surface(void)
  256. {
  257. vga_cache_boot_fb();
  258. if (!screen_surf.px) {
  259. screen_surf.w = vga_fb_w;
  260. screen_surf.h = vga_fb_h;
  261. screen_surf.pitch = vga_shadow_buf ? vga_shadow_pitch
  262. : vga_fb_pitch_val;
  263. screen_surf.bpp = vga_fb_bpp_val;
  264. screen_surf.alpha = 255;
  265. screen_surf.px = vga_shadow_buf ? vga_shadow_buf
  266. : (unsigned char *)vga_fb_lfb;
  267. }
  268. return &screen_surf;
  269. }
  270. struct surface *vga_target(void)
  271. {
  272. struct surface *t = process_render_target();
  273. return t ? t : vga_screen_surface();
  274. }
  275. void vga_set_target(struct surface *s)
  276. {
  277. process_set_render_target(s);
  278. }
  279. int vga_target_is_screen(void)
  280. {
  281. return vga_target() == vga_screen_surface();
  282. }
  283. void vga_target_damage(unsigned int x, unsigned int y, unsigned int w,
  284. unsigned int h)
  285. {
  286. if (w == 0 || h == 0 || !vga_target_is_screen())
  287. return;
  288. mark_dirty_rect_xy(y, h, x, x + w);
  289. }
  290. /* 16bpp: RGB 5:6:5. 32bpp: BGRX byte order (standard VBE / QEMU). */
  291. static void target_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
  292. {
  293. struct surface *t = vga_target();
  294. unsigned int pitch, bpp;
  295. unsigned char *p;
  296. if (!t->px || x >= t->w || y >= t->h)
  297. return;
  298. pitch = t->pitch;
  299. bpp = t->bpp;
  300. p = t->px + (unsigned long)y * pitch;
  301. if (bpp == 16) {
  302. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  303. unsigned int off = x * 2;
  304. p[off] = (unsigned char)(rgb16 & 0xFF);
  305. p[off + 1] = (unsigned char)(rgb16 >> 8);
  306. } else if (bpp == 24) {
  307. unsigned int off = x * 3;
  308. p[off] = (unsigned char)b;
  309. p[off + 1] = (unsigned char)g;
  310. p[off + 2] = (unsigned char)r;
  311. } else if (bpp == 32) {
  312. unsigned int off = x * 4;
  313. p[off] = (unsigned char)b;
  314. p[off + 1] = (unsigned char)g;
  315. p[off + 2] = (unsigned char)r;
  316. p[off + 3] = t->alpha;
  317. }
  318. vga_target_damage(x, y, 1, 1);
  319. }
  320. void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
  321. {
  322. vga_cache_boot_fb();
  323. target_put_pixel(x, y, r, g, b);
  324. }
  325. void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  326. unsigned int r, unsigned int g, unsigned int b)
  327. {
  328. vga_cache_boot_fb();
  329. if (!blit || !blit->fill)
  330. return;
  331. blit->fill(x, y, w, h, ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF));
  332. }
  333. static unsigned int vga_pixel_bytes(void)
  334. {
  335. if (vga_fb_bpp_val == 16)
  336. return 2;
  337. if (vga_fb_bpp_val == 24)
  338. return 3;
  339. return 4;
  340. }
  341. /* The software cursor saves and restores pixels of the *screen*: these two
  342. * keep using the shadow buffer directly, never the render target. */
  343. void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  344. unsigned char *buf)
  345. {
  346. unsigned int pitch, pb, row, col;
  347. unsigned char *fb;
  348. const unsigned char *src;
  349. vga_cache_boot_fb();
  350. if (!vga_shadow_buf || vga_fb_w == 0 || w == 0 || h == 0)
  351. return;
  352. pitch = vga_shadow_pitch;
  353. pb = vga_pixel_bytes();
  354. fb = vga_shadow_buf;
  355. if (pb == 4) {
  356. for (row = 0; row < h; row++) {
  357. const unsigned int *src = (const unsigned int *)
  358. (fb + (unsigned long)(y + row) * pitch + x * 4);
  359. unsigned int *dst = (unsigned int *)
  360. (buf + (unsigned long)row * w * 4);
  361. for (col = 0; col < w; col++)
  362. dst[col] = src[col];
  363. }
  364. return;
  365. }
  366. for (row = 0; row < h; row++) {
  367. src = fb + (unsigned long)(y + row) * pitch + x * pb;
  368. for (col = 0; col < w * pb; col++)
  369. buf[row * w * pb + col] = src[col];
  370. }
  371. }
  372. void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  373. const unsigned char *buf)
  374. {
  375. unsigned int pitch, pb, row, col;
  376. unsigned char *dst;
  377. vga_cache_boot_fb();
  378. if (!vga_shadow_buf || vga_fb_w == 0 || w == 0 || h == 0)
  379. return;
  380. pitch = vga_shadow_pitch;
  381. pb = vga_pixel_bytes();
  382. if (pb == 4) {
  383. for (row = 0; row < h; row++) {
  384. unsigned int *dst = (unsigned int *)
  385. (vga_shadow_buf + (unsigned long)(y + row) * pitch + x * 4);
  386. const unsigned int *src = (const unsigned int *)
  387. (buf + (unsigned long)row * w * 4);
  388. for (col = 0; col < w; col++)
  389. dst[col] = src[col];
  390. }
  391. goto mark;
  392. }
  393. for (row = 0; row < h; row++) {
  394. dst = vga_shadow_buf + (unsigned long)(y + row) * pitch + x * pb;
  395. for (col = 0; col < w * pb; col++)
  396. dst[col] = buf[row * w * pb + col];
  397. }
  398. mark:
  399. {
  400. unsigned int xe = x + w;
  401. if (xe > vga_fb_w)
  402. xe = vga_fb_w;
  403. mark_dirty_rect_xy(y, h, x, xe);
  404. }
  405. }
  406. void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
  407. {
  408. struct surface *t;
  409. vga_cache_boot_fb();
  410. t = vga_target();
  411. if (!t->px)
  412. return;
  413. vga_fill_rect(0, 0, t->w, t->h, r, g, b);
  414. }
  415. /* ---- Shadow buffer init and flush ---- */
  416. void vga_shadow_init(void)
  417. {
  418. vga_cache_boot_fb();
  419. if (!vga_fb_lfb || vga_fb_w == 0 || vga_fb_h == 0)
  420. return;
  421. /* The GPU probe (inside vga_cache_boot_fb) maps its BARs -- including the
  422. * NVIDIA VRAM aperture, which covers the VBE LFB -- uncacheable, so the
  423. * framebuffer mapping is re-applied once everything is up. */
  424. paging_lfb_refresh();
  425. vga_shadow_buf = (unsigned char *)VGA_SHADOW_BASE;
  426. vga_shadow_pitch = vga_fb_pitch_val;
  427. vga_shadow_size = vga_fb_h * vga_shadow_pitch;
  428. screen_surf.px = 0; /* re-derive from the shadow buffer */
  429. damage_clear();
  430. serial_puts(SERIAL_COM1, "blit backend: ");
  431. serial_puts(SERIAL_COM1, blit ? blit->name : "none");
  432. serial_puts(SERIAL_COM1, "\r\n");
  433. }
  434. /* Copy damage rects from the shadow to VRAM, at most about `max_bytes` this
  435. * call (0 = no limit), and drop the rects that are done. A rect that fits
  436. * the budget is copied whole even when that overshoots a little, so a
  437. * window-sized repaint is never split across iterations; only a rect larger
  438. * than the budget (a full-screen repaint) is chunked, and its remaining rows
  439. * move to the back of the list so newer, smaller damage is flushed first. */
  440. static unsigned int shadow_copy_damage(unsigned int max_bytes)
  441. {
  442. unsigned int i = 0, bytes = 0;
  443. unsigned int pb = vga_pixel_bytes();
  444. unsigned int align = (pb == 2) ? 2 : (pb == 3) ? 4 : 1;
  445. unsigned char *src = vga_shadow_buf;
  446. unsigned char *dst = (unsigned char *)vga_fb_lfb;
  447. while (i < damage_count) {
  448. struct damage_rect *d = &damage_rects[i];
  449. unsigned int rx0 = d->x0 & ~(align - 1);
  450. unsigned int rx1 = (d->x1 + align - 1) & ~(align - 1);
  451. unsigned int row_bytes, rect_bytes, dwords, tail, y;
  452. if (rx1 > vga_fb_w)
  453. rx1 = vga_fb_w;
  454. if (rx0 >= rx1) {
  455. damage_rects[i] = damage_rects[--damage_count];
  456. continue;
  457. }
  458. row_bytes = (rx1 - rx0) * pb;
  459. rect_bytes = row_bytes * (d->y1 - d->y0);
  460. dwords = row_bytes / 4;
  461. tail = row_bytes % 4;
  462. /* The budget is spent and this rect is bigger than a whole
  463. call: leave it (rows already copied keep their progress). */
  464. if (max_bytes && bytes > 0 && rect_bytes > max_bytes)
  465. break;
  466. for (y = d->y0; y < d->y1; y++) {
  467. unsigned char *s = src + (unsigned long)y * vga_shadow_pitch + rx0 * pb;
  468. unsigned char *t = dst + (unsigned long)y * vga_fb_pitch_val + rx0 * pb;
  469. vga_fb_copy_row(t, s, dwords);
  470. if (tail) {
  471. unsigned int k;
  472. for (k = dwords * 4; k < row_bytes; k++)
  473. t[k] = s[k];
  474. }
  475. bytes += row_bytes;
  476. if (max_bytes && rect_bytes > max_bytes && bytes >= max_bytes) {
  477. d->y0 = y + 1;
  478. break;
  479. }
  480. }
  481. if (y < d->y1) {
  482. /* Chunked: keep the remaining rows, behind the rest. */
  483. struct damage_rect rest = *d;
  484. damage_rects[i] = damage_rects[--damage_count];
  485. damage_rects[damage_count++] = rest;
  486. return bytes;
  487. }
  488. damage_rects[i] = damage_rects[--damage_count];
  489. if (max_bytes && bytes >= max_bytes)
  490. break;
  491. }
  492. return bytes;
  493. }
  494. /* Set while a process is inside shadow_copy_damage(). The flush walks and
  495. * trims a shared damage list, so it must not be entered twice: a process can
  496. * be preempted mid-flush (a full-screen copy takes several milliseconds) and
  497. * the scheduler may well run another process that flushes too. */
  498. static int present_busy;
  499. /* Bytes copied by the presenter so far (see vga.h). */
  500. unsigned int vga_present_copied;
  501. static inline unsigned int irq_save_disable(void)
  502. {
  503. unsigned int flags;
  504. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  505. return flags;
  506. }
  507. static inline void irq_restore(unsigned int flags)
  508. {
  509. __asm__ volatile ("pushl %0; popfl" :: "r"(flags));
  510. }
  511. /* Copy up to `max_bytes` of the pending damage to VRAM. Called by the main
  512. * loop on every iteration: small updates (a cursor move is ~1 KB) reach the
  513. * screen within one loop instead of waiting for a fixed 30 Hz tick, while a
  514. * full-screen repaint is chunked so it cannot stall input. */
  515. void vga_present_shadow(unsigned int max_bytes)
  516. {
  517. unsigned int flags;
  518. vga_cache_boot_fb();
  519. if (!vga_shadow_buf || !vga_fb_lfb)
  520. return;
  521. if (!damage_any())
  522. return;
  523. flags = irq_save_disable();
  524. if (present_busy) {
  525. /* Another process is mid-flush; its remaining damage stays
  526. * queued and is copied by whoever flushes next. */
  527. irq_restore(flags);
  528. return;
  529. }
  530. present_busy = 1;
  531. irq_restore(flags);
  532. vga_present_copied += shadow_copy_damage(max_bytes);
  533. present_busy = 0;
  534. }
  535. /* Make drawn content visible: the backend's present() (the software one copies
  536. * the pending damage rects to VRAM). */
  537. void vga_fb_flush_budget(unsigned int max_bytes)
  538. {
  539. vga_cache_boot_fb();
  540. if (!blit || !blit->present)
  541. return;
  542. blit->present(max_bytes);
  543. }
  544. void vga_fb_flush(void)
  545. {
  546. vga_fb_flush_budget(0);
  547. }
  548. int vga_shadow_is_active(void)
  549. {
  550. return vga_shadow_buf != 0;
  551. }
  552. void vga_fb_mark_dirty_rect(unsigned int y, unsigned int h)
  553. {
  554. vga_cache_boot_fb();
  555. mark_dirty_rect(y, h);
  556. }
  557. /* Mark a rectangle dirty, limited to the horizontal span [x0, x1). */
  558. void vga_fb_mark_dirty_rect_xy(unsigned int y, unsigned int h,
  559. unsigned int x0, unsigned int x1)
  560. {
  561. vga_cache_boot_fb();
  562. mark_dirty_rect_xy(y, h, x0, x1);
  563. }
  564. void vga_fb_clear_dirty(void)
  565. {
  566. damage_clear();
  567. }