gfx.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 target'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. Canvas
  46. * pixels are opaque, so the 32 bpp high byte is 0xFF. */
  47. static void blit_row(unsigned char *dst, const unsigned short *src,
  48. unsigned int n, unsigned int bpp)
  49. {
  50. unsigned int x;
  51. if (bpp == 16) {
  52. unsigned short *d = (unsigned short *)dst;
  53. for (x = 0; x < n; x++)
  54. d[x] = src[x];
  55. } else if (bpp == 24) {
  56. for (x = 0; x < n; x++) {
  57. unsigned int p = src[x];
  58. unsigned int r5 = (p >> 11) & 0x1F;
  59. unsigned int g6 = (p >> 5) & 0x3F;
  60. unsigned int b5 = p & 0x1F;
  61. dst[0] = (unsigned char)((b5 << 3) | (b5 >> 2));
  62. dst[1] = (unsigned char)((g6 << 2) | (g6 >> 4));
  63. dst[2] = (unsigned char)((r5 << 3) | (r5 >> 2));
  64. dst += 3;
  65. }
  66. } else {
  67. unsigned int *d = (unsigned int *)dst;
  68. for (x = 0; x < n; x++) {
  69. unsigned int p = src[x];
  70. unsigned int r5 = (p >> 11) & 0x1F;
  71. unsigned int g6 = (p >> 5) & 0x3F;
  72. unsigned int b5 = p & 0x1F;
  73. d[x] = 0xFF000000u |
  74. ((((r5 << 3) | (r5 >> 2))) << 16) |
  75. ((((g6 << 2) | (g6 >> 4))) << 8) |
  76. ((b5 << 3) | (b5 >> 2));
  77. }
  78. }
  79. }
  80. void gfx_blit(const struct gfx_canvas *c, unsigned int x0, unsigned int y0)
  81. {
  82. struct surface *t = vga_target();
  83. unsigned int pb = (t->bpp == 16) ? 2 : (t->bpp == 24) ? 3 : 4;
  84. unsigned int y;
  85. if (!c->px || !t->px)
  86. return;
  87. if (y0 + c->h > t->h || x0 + c->w > t->w)
  88. return;
  89. for (y = 0; y < c->h; y++)
  90. blit_row(t->px + (unsigned long)(y0 + y) * t->pitch + (unsigned long)x0 * pb,
  91. c->px + (unsigned long)y * c->w, c->w, t->bpp);
  92. vga_target_damage(x0, y0, c->w, c->h);
  93. }
  94. void gfx_blit_clip(const struct gfx_canvas *c, unsigned int x0, unsigned int y0,
  95. unsigned int cx, unsigned int cy, unsigned int cw, unsigned int ch)
  96. {
  97. struct surface *t = vga_target();
  98. unsigned int pb = (t->bpp == 16) ? 2 : (t->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 || !t->px)
  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(t->px + (unsigned long)(y0 + (unsigned int)y) * t->pitch +
  117. (unsigned long)(x0 + (unsigned int)sx0) * pb,
  118. c->px + (unsigned long)y * c->w + sx0,
  119. (unsigned int)(sx1 - sx0), t->bpp);
  120. vga_target_damage(x0 + (unsigned int)sx0,
  121. y0 + (unsigned int)sy0,
  122. (unsigned int)(sx1 - sx0),
  123. (unsigned int)(sy1 - sy0));
  124. }