blit.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 "cpu.h"
  9. #include "gfx.h"
  10. #include "vga.h"
  11. static unsigned int pixel_bytes(unsigned int bpp)
  12. {
  13. if (bpp == 16)
  14. return 2;
  15. if (bpp == 24)
  16. return 3;
  17. return 4;
  18. }
  19. /* Fill `dwords` with `value`; SSE2 for anything worth the call. */
  20. static void fill_dwords(unsigned int *p, unsigned int value, unsigned int dwords)
  21. {
  22. unsigned int i;
  23. if (dwords == 0)
  24. return;
  25. if (dwords >= 8 && cpu_has_sse2()) {
  26. vga_fb_fill_row_sse2(p, value, dwords);
  27. return;
  28. }
  29. for (i = 0; i < dwords; i++)
  30. p[i] = value;
  31. }
  32. /* Solid fill of the target. */
  33. static void software_fill(unsigned int x, unsigned int y, unsigned int w,
  34. unsigned int h, unsigned int rgb)
  35. {
  36. struct surface *t = vga_target();
  37. unsigned int r = (rgb >> 16) & 0xFF;
  38. unsigned int g = (rgb >> 8) & 0xFF;
  39. unsigned int b = rgb & 0xFF;
  40. unsigned int row, x1, y1;
  41. if (!t || !t->px || w == 0 || h == 0)
  42. return;
  43. if (x >= t->w || y >= t->h)
  44. return;
  45. x1 = x + w;
  46. y1 = y + h;
  47. if (x1 > t->w)
  48. x1 = t->w;
  49. if (y1 > t->h)
  50. y1 = t->h;
  51. if (t->bpp == 16) {
  52. unsigned short val = (unsigned short)(((r >> 3) << 11) |
  53. ((g >> 2) << 5) | (b >> 3));
  54. unsigned int val32 = (unsigned int)val | ((unsigned int)val << 16);
  55. for (row = y; row < y1; row++) {
  56. unsigned int *p = (unsigned int *)(t->px +
  57. (unsigned long)row * t->pitch + x * 2);
  58. unsigned int px = x1 - x;
  59. fill_dwords(p, val32, px / 2);
  60. if (px & 1)
  61. ((unsigned short *)p)[px - 1] = val;
  62. }
  63. } else if (t->bpp == 24) {
  64. for (row = y; row < y1; row++) {
  65. unsigned char *p = t->px + (unsigned long)row * t->pitch + x * 3;
  66. unsigned int xx;
  67. for (xx = x; xx < x1; xx++) {
  68. p[0] = (unsigned char)b;
  69. p[1] = (unsigned char)g;
  70. p[2] = (unsigned char)r;
  71. p += 3;
  72. }
  73. }
  74. } else {
  75. unsigned int val = ((unsigned int)t->alpha << 24) |
  76. (r << 16) | (g << 8) | b;
  77. unsigned int dwords = x1 - x;
  78. /* Long rows are bigger than the cache: normal stores would
  79. * read every line in (RFO) before overwriting it. Stream
  80. * them instead and order once at the end. */
  81. if (dwords >= 32 && cpu_has_sse2()) {
  82. for (row = y; row < y1; row++)
  83. vga_fb_fill_row_nt((unsigned int *)(t->px +
  84. (unsigned long)row * t->pitch + x * 4),
  85. val, dwords);
  86. __asm__ volatile ("sfence" ::: "memory");
  87. } else {
  88. for (row = y; row < y1; row++) {
  89. unsigned int *p = (unsigned int *)(t->px +
  90. (unsigned long)row * t->pitch + x * 4);
  91. fill_dwords(p, val, dwords);
  92. }
  93. }
  94. }
  95. vga_target_damage(x, y, x1 - x, y1 - y);
  96. }
  97. /* Overlap-safe row copy (dword when aligned, bytes otherwise). */
  98. static void row_move(unsigned char *dst, const unsigned char *src, unsigned int n)
  99. {
  100. if (dst == src || n == 0)
  101. return;
  102. if ((((unsigned long)dst | (unsigned long)src) & 3) == 0) {
  103. unsigned int words = n >> 2;
  104. unsigned int i;
  105. unsigned int *dw = (unsigned int *)dst;
  106. const unsigned int *sw = (const unsigned int *)src;
  107. if (dst < src) {
  108. for (i = 0; i < words; i++)
  109. dw[i] = sw[i];
  110. for (i = words << 2; i < n; i++)
  111. dst[i] = src[i];
  112. } else {
  113. /* Overlapping move down: back to front. */
  114. for (i = n; i > (words << 2); i--)
  115. dst[i - 1] = src[i - 1];
  116. for (i = words; i-- > 0; )
  117. dw[i] = sw[i];
  118. }
  119. return;
  120. }
  121. if (dst < src) {
  122. unsigned int i;
  123. for (i = 0; i < n; i++)
  124. dst[i] = src[i];
  125. } else {
  126. while (n--)
  127. dst[n] = src[n];
  128. }
  129. }
  130. /* Rectangle copy inside the target (a window move). */
  131. static void software_copy(unsigned int dx, unsigned int dy, unsigned int sx,
  132. unsigned int sy, unsigned int w, unsigned int h)
  133. {
  134. struct surface *t = vga_target();
  135. unsigned int pb, row_bytes, r;
  136. if (!t || !t->px || w == 0 || h == 0)
  137. return;
  138. pb = pixel_bytes(t->bpp);
  139. if (dx >= t->w || dy >= t->h || sx >= t->w || sy >= t->h)
  140. return;
  141. if (w > t->w - dx) w = t->w - dx;
  142. if (w > t->w - sx) w = t->w - sx;
  143. if (h > t->h - dy) h = t->h - dy;
  144. if (h > t->h - sy) h = t->h - sy;
  145. if (w == 0 || h == 0)
  146. return;
  147. row_bytes = w * pb;
  148. if (dy > sy || (dy == sy && dx > sx)) {
  149. for (r = h; r-- > 0; )
  150. row_move(t->px + (unsigned long)(dy + r) * t->pitch + dx * pb,
  151. t->px + (unsigned long)(sy + r) * t->pitch + sx * pb,
  152. row_bytes);
  153. } else {
  154. for (r = 0; r < h; r++)
  155. row_move(t->px + (unsigned long)(dy + r) * t->pitch + dx * pb,
  156. t->px + (unsigned long)(sy + r) * t->pitch + sx * pb,
  157. row_bytes);
  158. }
  159. vga_target_damage(dx, dy, w, h);
  160. }
  161. /* Blend an ARGB8888 source onto the target (uniform alpha, 32 bpp only).
  162. * The source alpha byte is the per-pixel coverage; `alpha` scales it. */
  163. static void software_blend(unsigned int x, unsigned int y, unsigned int w,
  164. unsigned int h, const struct surface *src,
  165. unsigned int sx, unsigned int sy, unsigned int alpha)
  166. {
  167. struct surface *t = vga_target();
  168. unsigned int row, col;
  169. if (!t || !t->px || !src || !src->px)
  170. return;
  171. if (t->bpp != 32 || src->bpp != 32 || w == 0 || h == 0)
  172. return;
  173. if (x >= t->w || y >= t->h || sx >= src->w || sy >= src->h)
  174. return;
  175. if (w > t->w - x) w = t->w - x;
  176. if (w > src->w - sx) w = src->w - sx;
  177. if (h > t->h - y) h = t->h - y;
  178. if (h > src->h - sy) h = src->h - sy;
  179. if (w == 0 || h == 0)
  180. return;
  181. for (row = 0; row < h; row++) {
  182. unsigned int *d = (unsigned int *)(t->px +
  183. (unsigned long)(y + row) * t->pitch + x * 4);
  184. const unsigned int *s = (const unsigned int *)(src->px +
  185. (unsigned long)(sy + row) * src->pitch + sx * 4);
  186. for (col = 0; col < w; col++) {
  187. unsigned int sv = s[col];
  188. unsigned int a = (sv >> 24) & 0xFF;
  189. unsigned int dv = d[col];
  190. unsigned int ia;
  191. if (alpha != 255)
  192. a = (a * alpha) / 255;
  193. if (a == 0)
  194. continue;
  195. if (a == 255) {
  196. d[col] = sv & 0x00FFFFFFu;
  197. continue;
  198. }
  199. ia = 255 - a;
  200. d[col] = ((((((sv >> 16) & 0xFF) * a + ((dv >> 16) & 0xFF) * ia) / 255) & 0xFF) << 16) |
  201. ((((((sv >> 8) & 0xFF) * a + ((dv >> 8) & 0xFF) * ia) / 255) & 0xFF) << 8) |
  202. ((((sv & 0xFF) * a + (dv & 0xFF) * ia) / 255) & 0xFF);
  203. }
  204. }
  205. vga_target_damage(x, y, w, h);
  206. }
  207. static void software_canvas(const struct gfx_canvas *c, unsigned int x,
  208. unsigned int y, unsigned int cx, unsigned int cy,
  209. unsigned int cw, unsigned int ch)
  210. {
  211. gfx_blit_clip(c, x, y, cx, cy, cw, ch);
  212. }
  213. static void software_present(unsigned int max_bytes)
  214. {
  215. vga_present_shadow(max_bytes);
  216. }
  217. static void software_flip(void)
  218. {
  219. /* Single-buffered: present() already pushed the damage to VRAM. */
  220. }
  221. const struct blit_ops blit_software = {
  222. "software",
  223. software_fill,
  224. software_copy,
  225. software_blend,
  226. software_canvas,
  227. software_present,
  228. software_flip,
  229. };
  230. const struct blit_ops *blit = &blit_software;