vga.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* VGA: VESA LFB (from boot). Shadow buffer; the main loop copies pending
  2. * damage rectangles to VRAM on every iteration (byte budget, no fixed frame
  3. * rate). */
  4. #ifndef VGA_H
  5. #define VGA_H
  6. /* Boot video info at 0x600 (written by boot.S in safe conventional memory).
  7. Layout: valid(1) width(2) height(2) pitch(2) bpp(1) lfb_base(4) vram_64k(2) = 14 bytes. */
  8. struct vga_boot_fb_info {
  9. unsigned char valid;
  10. unsigned short width;
  11. unsigned short height;
  12. unsigned short pitch;
  13. unsigned char bpp;
  14. unsigned int lfb_base;
  15. unsigned short vram_64k; /* total VRAM in 64KB blocks (from VBE) */
  16. } __attribute__((packed));
  17. #define VGA_BOOT_FB ((volatile struct vga_boot_fb_info *)0x600)
  18. /* Shadow buffer lives at 1MB — safely above kernel, stacks, DMA, and program load area. */
  19. #define VGA_SHADOW_BASE 0x100000
  20. /* LFB API. RGB 0..255. All drawing goes into the shadow buffer; the main loop
  21. * flushes dirty areas to VRAM on every iteration (byte-budgeted). These
  22. * describe the *screen*, not the current render target (see below). */
  23. unsigned int vga_fb_width(void);
  24. unsigned int vga_fb_height(void);
  25. unsigned int vga_fb_pitch(void);
  26. unsigned int vga_fb_bpp(void);
  27. unsigned int vga_fb_vram_kb(void);
  28. unsigned int vga_fb_pci_id(void);
  29. void *vga_fb_ptr(void);
  30. /* Physical address of the linear framebuffer (for off-screen VRAM access). */
  31. unsigned long vga_fb_lfb_addr(void);
  32. int vga_has_vesa(void);
  33. /* A drawing surface: the screen's shadow buffer, a window layer or a canvas.
  34. * Drawing helpers paint into the *current render target*, which is the screen
  35. * unless process_set_render_target() pointed this process at a surface. */
  36. struct surface {
  37. unsigned char *px;
  38. unsigned int w, h;
  39. unsigned int pitch;
  40. unsigned int bpp; /* 16, 24 or 32 */
  41. /* Alpha written into the high byte of 32 bpp pixels. The screen uses
  42. * 255 (the byte is padding there); a window layer sets what its
  43. * background fill should composite with, while glyph foregrounds are
  44. * always written opaque. */
  45. unsigned char alpha;
  46. };
  47. /* The screen surface (shadow buffer, or the LFB when there is no shadow). */
  48. struct surface *vga_screen_surface(void);
  49. /* Current render target for this process: the screen by default. */
  50. struct surface *vga_target(void);
  51. void vga_set_target(struct surface *s);
  52. int vga_target_is_screen(void);
  53. /* Mark a rectangle of the screen dirty. A no-op while the target is not the
  54. * screen: drawing into a layer must not touch the screen's damage list, the
  55. * compositor marks the rects it copies out. */
  56. void vga_target_damage(unsigned int x, unsigned int y, unsigned int w,
  57. unsigned int h);
  58. /* The software presenter used by the blit backend: copies up to max_bytes of
  59. * the pending damage rects from the shadow buffer to VRAM. */
  60. void vga_present_shadow(unsigned int max_bytes);
  61. /* 1 when VRAM writes go through a write-combining path (the stolen-VRAM
  62. * alias on ION-class hardware, where the PCIe aperture is UC no matter what
  63. * the MTRR says). */
  64. int vga_fb_lfb_wc(void);
  65. /* Bytes the presenter has copied in total (lets a benchmark measure the real
  66. * work even when the flush is skipped because another process is in one). */
  67. extern unsigned int vga_present_copied;
  68. void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b);
  69. void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  70. unsigned int r, unsigned int g, unsigned int b);
  71. void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b);
  72. /* Tightly packed row-major: each row is w * pixel_bytes contiguous (pixel_bytes from bpp). */
  73. void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  74. unsigned char *buf);
  75. void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  76. const unsigned char *buf);
  77. /* Initialize shadow buffer (called once from kernel_main after vga_cache_boot_fb). */
  78. void vga_shadow_init(void);
  79. /* Copy shadow buffer to VRAM (everything dirty, no budget). Called after
  80. * blocking redraws and at boot. */
  81. void vga_fb_flush(void);
  82. /* Copy up to max_bytes of the pending damage rects to VRAM (0 = no limit).
  83. * Called by the main loop every iteration: small updates reach the screen
  84. * within one loop, large ones are chunked so they cannot stall input. */
  85. void vga_fb_flush_budget(unsigned int max_bytes);
  86. /* Low-level row helpers from vga_copy.S (the blit backend uses the fill). */
  87. void vga_fb_copy_row(void *dst, const void *src, unsigned int dwords);
  88. void vga_fb_copy_row_nt(void *dst, const void *src, unsigned int dwords);
  89. void vga_fb_fill_row_sse2(void *dst, unsigned int value, unsigned int dwords);
  90. void vga_fb_fill_row_nt(void *dst, unsigned int value, unsigned int dwords);
  91. /* Per-iteration flush budget: large enough that a window-sized repaint
  92. * (~0.7 MB) is flushed as one frame, small enough that a full-screen repaint
  93. * is chunked instead of stalling input behind it. */
  94. #define VGA_FLUSH_BUDGET_BYTES (1024u * 1024u)
  95. /* Returns 1 if shadow buffer is active, 0 if rendering directly to VRAM. */
  96. int vga_shadow_is_active(void);
  97. /* Mark rows dirty for direct buffer manipulators (e.g. fbtext_scroll). */
  98. void vga_fb_mark_dirty_rect(unsigned int y, unsigned int h);
  99. /* Mark a rectangle dirty, limited to the horizontal span [x0, x1). */
  100. void vga_fb_mark_dirty_rect_xy(unsigned int y, unsigned int h,
  101. unsigned int x0, unsigned int x1);
  102. /* Drop all pending damage (so only explicitly re-marked areas get flushed). */
  103. void vga_fb_clear_dirty(void);
  104. #endif