blit.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Blit backend: software implementation (the fallback for every machine).
  2. *
  3. * The ops draw into the current render target, so a later layer compositor can
  4. * reuse them unchanged; copy() and present() work on the screen. The software
  5. * paths are plain cached-memory loops (the target is the shadow buffer in RAM
  6. * for now; a flip/VRAM backend would replace them with GPU operations). */
  7. #include "blit.h"
  8. #include "gfx.h"
  9. #include "vga.h"
  10. static unsigned int pixel_bytes(unsigned int bpp)
  11. {
  12. if (bpp == 16)
  13. return 2;
  14. if (bpp == 24)
  15. return 3;
  16. return 4;
  17. }
  18. /* Solid fill of the target. */
  19. static void software_fill(unsigned int x, unsigned int y, unsigned int w,
  20. unsigned int h, unsigned int rgb)
  21. {
  22. struct surface *t = vga_target();
  23. unsigned int r = (rgb >> 16) & 0xFF;
  24. unsigned int g = (rgb >> 8) & 0xFF;
  25. unsigned int b = rgb & 0xFF;
  26. unsigned int row, x1, y1;
  27. if (!t || !t->px || w == 0 || h == 0)
  28. return;
  29. if (x >= t->w || y >= t->h)
  30. return;
  31. x1 = x + w;
  32. y1 = y + h;
  33. if (x1 > t->w)
  34. x1 = t->w;
  35. if (y1 > t->h)
  36. y1 = t->h;
  37. if (t->bpp == 16) {
  38. unsigned short val = (unsigned short)(((r >> 3) << 11) |
  39. ((g >> 2) << 5) | (b >> 3));
  40. for (row = y; row < y1; row++) {
  41. unsigned short *p = (unsigned short *)(t->px +
  42. (unsigned long)row * t->pitch + x * 2);
  43. unsigned int xx;
  44. for (xx = x; xx < x1; xx++)
  45. *p++ = val;
  46. }
  47. } else if (t->bpp == 24) {
  48. for (row = y; row < y1; row++) {
  49. unsigned char *p = t->px + (unsigned long)row * t->pitch + x * 3;
  50. unsigned int xx;
  51. for (xx = x; xx < x1; xx++) {
  52. p[0] = (unsigned char)b;
  53. p[1] = (unsigned char)g;
  54. p[2] = (unsigned char)r;
  55. p += 3;
  56. }
  57. }
  58. } else {
  59. unsigned int val = ((unsigned int)t->alpha << 24) |
  60. (r << 16) | (g << 8) | b;
  61. for (row = y; row < y1; row++) {
  62. unsigned int *p = (unsigned int *)(t->px +
  63. (unsigned long)row * t->pitch + x * 4);
  64. unsigned int xx;
  65. for (xx = x; xx < x1; xx++)
  66. *p++ = val;
  67. }
  68. }
  69. vga_target_damage(x, y, x1 - x, y1 - y);
  70. }
  71. /* Overlap-safe row copy (dword when aligned, bytes otherwise). */
  72. static void row_move(unsigned char *dst, const unsigned char *src, unsigned int n)
  73. {
  74. if (dst == src || n == 0)
  75. return;
  76. if ((((unsigned long)dst | (unsigned long)src) & 3) == 0) {
  77. unsigned int words = n >> 2;
  78. unsigned int i;
  79. unsigned int *dw = (unsigned int *)dst;
  80. const unsigned int *sw = (const unsigned int *)src;
  81. if (dst < src) {
  82. for (i = 0; i < words; i++)
  83. dw[i] = sw[i];
  84. for (i = words << 2; i < n; i++)
  85. dst[i] = src[i];
  86. } else {
  87. /* Overlapping move down: back to front. */
  88. for (i = n; i > (words << 2); i--)
  89. dst[i - 1] = src[i - 1];
  90. for (i = words; i-- > 0; )
  91. dw[i] = sw[i];
  92. }
  93. return;
  94. }
  95. if (dst < src) {
  96. unsigned int i;
  97. for (i = 0; i < n; i++)
  98. dst[i] = src[i];
  99. } else {
  100. while (n--)
  101. dst[n] = src[n];
  102. }
  103. }
  104. /* Rectangle copy inside the target (a window move). */
  105. static void software_copy(unsigned int dx, unsigned int dy, unsigned int sx,
  106. unsigned int sy, unsigned int w, unsigned int h)
  107. {
  108. struct surface *t = vga_target();
  109. unsigned int pb, row_bytes, r;
  110. if (!t || !t->px || w == 0 || h == 0)
  111. return;
  112. pb = pixel_bytes(t->bpp);
  113. if (dx >= t->w || dy >= t->h || sx >= t->w || sy >= t->h)
  114. return;
  115. if (w > t->w - dx) w = t->w - dx;
  116. if (w > t->w - sx) w = t->w - sx;
  117. if (h > t->h - dy) h = t->h - dy;
  118. if (h > t->h - sy) h = t->h - sy;
  119. if (w == 0 || h == 0)
  120. return;
  121. row_bytes = w * pb;
  122. if (dy > sy || (dy == sy && dx > sx)) {
  123. for (r = h; r-- > 0; )
  124. row_move(t->px + (unsigned long)(dy + r) * t->pitch + dx * pb,
  125. t->px + (unsigned long)(sy + r) * t->pitch + sx * pb,
  126. row_bytes);
  127. } else {
  128. for (r = 0; r < h; r++)
  129. row_move(t->px + (unsigned long)(dy + r) * t->pitch + dx * pb,
  130. t->px + (unsigned long)(sy + r) * t->pitch + sx * pb,
  131. row_bytes);
  132. }
  133. vga_target_damage(dx, dy, w, h);
  134. }
  135. /* Blend an ARGB8888 source onto the target (uniform alpha, 32 bpp only).
  136. * The source alpha byte is the per-pixel coverage; `alpha` scales it. */
  137. static void software_blend(unsigned int x, unsigned int y, unsigned int w,
  138. unsigned int h, const struct surface *src,
  139. unsigned int sx, unsigned int sy, unsigned int alpha)
  140. {
  141. struct surface *t = vga_target();
  142. unsigned int row, col;
  143. if (!t || !t->px || !src || !src->px)
  144. return;
  145. if (t->bpp != 32 || src->bpp != 32 || w == 0 || h == 0)
  146. return;
  147. if (x >= t->w || y >= t->h || sx >= src->w || sy >= src->h)
  148. return;
  149. if (w > t->w - x) w = t->w - x;
  150. if (w > src->w - sx) w = src->w - sx;
  151. if (h > t->h - y) h = t->h - y;
  152. if (h > src->h - sy) h = src->h - sy;
  153. if (w == 0 || h == 0)
  154. return;
  155. for (row = 0; row < h; row++) {
  156. unsigned int *d = (unsigned int *)(t->px +
  157. (unsigned long)(y + row) * t->pitch + x * 4);
  158. const unsigned int *s = (const unsigned int *)(src->px +
  159. (unsigned long)(sy + row) * src->pitch + sx * 4);
  160. for (col = 0; col < w; col++) {
  161. unsigned int sv = s[col];
  162. unsigned int a = (sv >> 24) & 0xFF;
  163. unsigned int dv = d[col];
  164. unsigned int ia;
  165. if (alpha != 255)
  166. a = (a * alpha) / 255;
  167. if (a == 0)
  168. continue;
  169. if (a == 255) {
  170. d[col] = sv & 0x00FFFFFFu;
  171. continue;
  172. }
  173. ia = 255 - a;
  174. d[col] = ((((((sv >> 16) & 0xFF) * a + ((dv >> 16) & 0xFF) * ia) / 255) & 0xFF) << 16) |
  175. ((((((sv >> 8) & 0xFF) * a + ((dv >> 8) & 0xFF) * ia) / 255) & 0xFF) << 8) |
  176. ((((sv & 0xFF) * a + (dv & 0xFF) * ia) / 255) & 0xFF);
  177. }
  178. }
  179. vga_target_damage(x, y, w, h);
  180. }
  181. static void software_canvas(const struct gfx_canvas *c, unsigned int x,
  182. unsigned int y, unsigned int cx, unsigned int cy,
  183. unsigned int cw, unsigned int ch)
  184. {
  185. gfx_blit_clip(c, x, y, cx, cy, cw, ch);
  186. }
  187. static void software_present(unsigned int max_bytes)
  188. {
  189. vga_present_shadow(max_bytes);
  190. }
  191. static void software_flip(void)
  192. {
  193. /* Single-buffered: present() already pushed the damage to VRAM. */
  194. }
  195. const struct blit_ops blit_software = {
  196. "software",
  197. software_fill,
  198. software_copy,
  199. software_blend,
  200. software_canvas,
  201. software_present,
  202. software_flip,
  203. };
  204. const struct blit_ops *blit = &blit_software;