gfx.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* RGB565 window canvas: page-allocated, page-aligned (so it can be mapped
  2. * into a ring-3 process 1:1), plus bpp-aware blits into the shadow buffer. */
  3. #include "gfx.h"
  4. #include "mm.h"
  5. #include "vga.h"
  6. int gfx_alloc(struct gfx_canvas *c, unsigned int w, unsigned int h)
  7. {
  8. unsigned int bytes, pages, i;
  9. c->px = 0;
  10. c->w = c->h = 0;
  11. c->seq = c->drawn = 0;
  12. if (w == 0 || h == 0 || w > 4096 || h > 4096)
  13. return -1;
  14. bytes = w * h * 2;
  15. pages = (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  16. {
  17. unsigned int phys = mm_alloc_pages(pages);
  18. if (!phys)
  19. return -1;
  20. c->px = (unsigned short *)phys;
  21. }
  22. c->w = w;
  23. c->h = h;
  24. for (i = 0; i < w * h; i++) /* fresh memory is not zeroed */
  25. c->px[i] = 0;
  26. c->seq = 1; /* first frame: draw it once */
  27. c->drawn = 0;
  28. return 0;
  29. }
  30. void gfx_free(struct gfx_canvas *c)
  31. {
  32. unsigned int bytes, pages, i;
  33. if (!c->px)
  34. return;
  35. bytes = c->w * c->h * 2;
  36. pages = (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  37. for (i = 0; i < pages; i++)
  38. mm_free_page((unsigned int)c->px + i * PAGE_SIZE);
  39. c->px = 0;
  40. c->w = c->h = 0;
  41. c->seq = c->drawn = 0;
  42. }
  43. /* One row of RGB565 -> the shadow buffer's pixel format. Byte order mirrors
  44. * fbtext.c's draw_glyph_at(); the 8-bit values replicate the high bits of the
  45. * 5/6-bit fields so black/white and the EGA palette stay exact. */
  46. static void blit_row(unsigned char *dst, const unsigned short *src,
  47. unsigned int n, unsigned int bpp)
  48. {
  49. unsigned int x;
  50. if (bpp == 16) {
  51. unsigned short *d = (unsigned short *)dst;
  52. for (x = 0; x < n; x++)
  53. d[x] = src[x];
  54. } else if (bpp == 24) {
  55. for (x = 0; x < n; x++) {
  56. unsigned int p = src[x];
  57. unsigned int r5 = (p >> 11) & 0x1F;
  58. unsigned int g6 = (p >> 5) & 0x3F;
  59. unsigned int b5 = p & 0x1F;
  60. dst[0] = (unsigned char)((b5 << 3) | (b5 >> 2));
  61. dst[1] = (unsigned char)((g6 << 2) | (g6 >> 4));
  62. dst[2] = (unsigned char)((r5 << 3) | (r5 >> 2));
  63. dst += 3;
  64. }
  65. } else {
  66. unsigned int *d = (unsigned int *)dst;
  67. for (x = 0; x < n; x++) {
  68. unsigned int p = src[x];
  69. unsigned int r5 = (p >> 11) & 0x1F;
  70. unsigned int g6 = (p >> 5) & 0x3F;
  71. unsigned int b5 = p & 0x1F;
  72. d[x] = ((((r5 << 3) | (r5 >> 2))) << 16) |
  73. ((((g6 << 2) | (g6 >> 4))) << 8) |
  74. ((b5 << 3) | (b5 >> 2));
  75. }
  76. }
  77. }
  78. void gfx_blit(const struct gfx_canvas *c, unsigned int x0, unsigned int y0)
  79. {
  80. unsigned char *fb = (unsigned char *)vga_fb_ptr();
  81. unsigned int pitch = vga_fb_pitch();
  82. unsigned int bpp = vga_fb_bpp();
  83. unsigned int pb = (bpp == 16) ? 2 : (bpp == 24) ? 3 : 4;
  84. unsigned int y;
  85. if (!c->px || !fb)
  86. return;
  87. for (y = 0; y < c->h; y++)
  88. blit_row(fb + (unsigned long)(y0 + y) * pitch + (unsigned long)x0 * pb,
  89. c->px + (unsigned long)y * c->w, c->w, bpp);
  90. vga_fb_mark_dirty_rect_xy(y0, c->h, x0, x0 + c->w);
  91. }
  92. void gfx_blit_clip(const struct gfx_canvas *c, unsigned int x0, unsigned int y0,
  93. unsigned int cx, unsigned int cy, unsigned int cw, unsigned int ch)
  94. {
  95. unsigned char *fb = (unsigned char *)vga_fb_ptr();
  96. unsigned int pitch = vga_fb_pitch();
  97. unsigned int bpp = vga_fb_bpp();
  98. unsigned int pb = (bpp == 16) ? 2 : (bpp == 24) ? 3 : 4;
  99. int dx0 = (int)x0, dy0 = (int)y0;
  100. int sx0 = 0, sy0 = 0, sx1 = (int)c->w, sy1 = (int)c->h;
  101. int y;
  102. if (!c->px || !fb)
  103. return;
  104. /* Canvas coords of the clip rect (signed: the rect may miss entirely). */
  105. if ((int)cx > dx0)
  106. sx0 = (int)cx - dx0;
  107. if ((int)cy > dy0)
  108. sy0 = (int)cy - dy0;
  109. if ((int)(cx + cw) < dx0 + (int)c->w)
  110. sx1 = (int)(cx + cw) - dx0;
  111. if ((int)(cy + ch) < dy0 + (int)c->h)
  112. sy1 = (int)(cy + ch) - dy0;
  113. if (sx1 <= sx0 || sy1 <= sy0)
  114. return;
  115. for (y = sy0; y < sy1; y++)
  116. blit_row(fb + (unsigned long)(y0 + (unsigned int)y) * pitch +
  117. (unsigned long)(x0 + (unsigned int)sx0) * pb,
  118. c->px + (unsigned long)y * c->w + sx0,
  119. (unsigned int)(sx1 - sx0), bpp);
  120. vga_fb_mark_dirty_rect_xy(y0 + (unsigned int)sy0,
  121. (unsigned int)(sy1 - sy0),
  122. x0 + (unsigned int)sx0, x0 + (unsigned int)sx1);
  123. }