uwm_lib.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* Userland drawing primitives for the ring-3 WM. Mirrors the kernel's
  2. * fbtext/gfx byte order so the mapped shadow matches the kernel console. */
  3. #include "uwm_lib.h"
  4. #include "font_8x16.h"
  5. #define FONT_H 16
  6. static const unsigned char pal_r[16] = {
  7. 0, 0, 0, 0, 170, 170, 170, 170, 85, 85, 85, 85, 255, 255, 255, 255
  8. };
  9. static const unsigned char pal_g[16] = {
  10. 0, 0, 0, 170, 0, 0, 85, 170, 85, 85, 255, 255, 85, 85, 255, 255
  11. };
  12. static const unsigned char pal_b[16] = {
  13. 0, 0, 170, 0, 170, 170, 0, 170, 85, 255, 85, 255, 85, 255, 85, 255
  14. };
  15. unsigned int usurf_pixels;
  16. void usurf_init(struct usurf *s, void *px, unsigned int w, unsigned int h,
  17. unsigned int pitch, unsigned int bpp)
  18. {
  19. s->px = (unsigned char *)px;
  20. s->w = w;
  21. s->h = h;
  22. s->pitch = pitch;
  23. s->bpp = bpp;
  24. }
  25. static void put_px(struct usurf *s, unsigned int x, unsigned int y,
  26. unsigned int r, unsigned int g, unsigned int b)
  27. {
  28. unsigned char *p;
  29. if (x >= s->w || y >= s->h)
  30. return;
  31. usurf_pixels++;
  32. p = s->px + (unsigned long)y * s->pitch;
  33. if (s->bpp == 16) {
  34. unsigned int v = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  35. unsigned short *d = (unsigned short *)p;
  36. d[x] = (unsigned short)v;
  37. } else {
  38. p += x * (s->bpp / 8);
  39. p[0] = (unsigned char)b;
  40. p[1] = (unsigned char)g;
  41. p[2] = (unsigned char)r;
  42. if (s->bpp == 32)
  43. p[3] = 0xFF;
  44. }
  45. }
  46. void usurf_fill(struct usurf *s, int x, int y, unsigned int w,
  47. unsigned int h, unsigned int rgb)
  48. {
  49. unsigned int r = (rgb >> 16) & 0xFF;
  50. unsigned int g = (rgb >> 8) & 0xFF;
  51. unsigned int b = rgb & 0xFF;
  52. unsigned int i, j;
  53. if (x < 0) {
  54. w -= (unsigned int)(-x);
  55. x = 0;
  56. }
  57. if (y < 0) {
  58. h -= (unsigned int)(-y);
  59. y = 0;
  60. }
  61. if ((unsigned int)x >= s->w || (unsigned int)y >= s->h)
  62. return;
  63. if ((unsigned int)x + w > s->w)
  64. w = s->w - (unsigned int)x;
  65. if ((unsigned int)y + h > s->h)
  66. h = s->h - (unsigned int)y;
  67. usurf_pixels += w * h;
  68. if (s->bpp == 32) {
  69. unsigned int v = 0xFF000000u | (r << 16) | (g << 8) | b;
  70. for (j = 0; j < h; j++) {
  71. unsigned int *p = (unsigned int *)(s->px +
  72. (unsigned long)((unsigned int)y + j) * s->pitch) +
  73. (unsigned int)x;
  74. unsigned int n = w;
  75. __asm__ volatile ("cld; rep stosl"
  76. : "+D"(p), "+c"(n)
  77. : "a"(v)
  78. : "memory");
  79. }
  80. } else if (s->bpp == 16) {
  81. unsigned short v = (unsigned short)(((r >> 3) << 11) |
  82. ((g >> 2) << 5) | (b >> 3));
  83. for (j = 0; j < h; j++) {
  84. unsigned short *p = (unsigned short *)(s->px +
  85. (unsigned long)((unsigned int)y + j) * s->pitch) +
  86. (unsigned int)x;
  87. unsigned int n = w;
  88. __asm__ volatile ("cld; rep stosw"
  89. : "+D"(p), "+c"(n)
  90. : "a"(v)
  91. : "memory");
  92. }
  93. } else {
  94. for (j = 0; j < h; j++) {
  95. unsigned char *p = s->px +
  96. (unsigned long)((unsigned int)y + j) * s->pitch +
  97. (unsigned int)x * 3;
  98. for (i = 0; i < w; i++) {
  99. p[0] = (unsigned char)b;
  100. p[1] = (unsigned char)g;
  101. p[2] = (unsigned char)r;
  102. p += 3;
  103. }
  104. }
  105. }
  106. }
  107. void usurf_copy(struct usurf *s, int dx, int dy, int sx, int sy,
  108. unsigned int w, unsigned int h)
  109. {
  110. unsigned int pb = s->bpp / 8;
  111. int backwards;
  112. if (w == 0 || h == 0 || dx < 0 || dy < 0 || sx < 0 || sy < 0)
  113. return;
  114. if ((unsigned int)dx + w > s->w || (unsigned int)sx + w > s->w ||
  115. (unsigned int)dy + h > s->h || (unsigned int)sy + h > s->h)
  116. return;
  117. usurf_pixels += w * h;
  118. backwards = (dy > sy) || (dy == sy && dx > sx);
  119. {
  120. unsigned int i;
  121. for (i = 0; i < h; i++) {
  122. unsigned int syy = backwards ? (unsigned int)(sy + (int)h - 1 - (int)i)
  123. : (unsigned int)sy + i;
  124. unsigned int dyy = backwards ? (unsigned int)(dy + (int)h - 1 - (int)i)
  125. : (unsigned int)dy + i;
  126. unsigned char *src = s->px + (unsigned long)syy * s->pitch +
  127. (unsigned int)sx * pb;
  128. unsigned char *dst = s->px + (unsigned long)dyy * s->pitch +
  129. (unsigned int)dx * pb;
  130. unsigned int n = w;
  131. if (pb == 4) {
  132. unsigned int *d = (unsigned int *)dst;
  133. const unsigned int *s32 = (const unsigned int *)src;
  134. if (backwards) {
  135. while (n--)
  136. d[n] = s32[n];
  137. } else {
  138. __asm__ volatile ("cld; rep movsl"
  139. : "+D"(d), "+c"(n), "+S"(s32)
  140. :
  141. : "memory");
  142. }
  143. } else if (pb == 2) {
  144. unsigned short *d = (unsigned short *)dst;
  145. const unsigned short *s16 = (const unsigned short *)src;
  146. if (backwards) {
  147. while (n--)
  148. d[n] = s16[n];
  149. } else {
  150. __asm__ volatile ("cld; rep movsw"
  151. : "+D"(d), "+c"(n), "+S"(s16)
  152. :
  153. : "memory");
  154. }
  155. } else {
  156. unsigned int k = w * 3;
  157. if (backwards) {
  158. while (k--)
  159. dst[k] = src[k];
  160. } else {
  161. __asm__ volatile ("cld; rep movsb"
  162. : "+D"(dst), "+c"(k), "+S"(src)
  163. :
  164. : "memory");
  165. }
  166. }
  167. }
  168. }
  169. }
  170. void usurf_glyph(struct usurf *s, int x, int y, unsigned char c,
  171. unsigned int fg, unsigned int bg, int transparent,
  172. unsigned int height,
  173. int cx, int cy, unsigned int cw, unsigned int ch)
  174. {
  175. const unsigned char *glyph = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  176. unsigned int fr = (fg >> 16) & 0xFF, fgn = (fg >> 8) & 0xFF, fb = fg & 0xFF;
  177. unsigned int br = (bg >> 16) & 0xFF, bgn = (bg >> 8) & 0xFF, bb = bg & 0xFF;
  178. unsigned int py, px;
  179. for (py = 0; py < height; py++) {
  180. unsigned char bits = (py < FONT_H) ? glyph[py] : 0;
  181. int yy = y + (int)py;
  182. if (yy < cy || yy >= cy + (int)ch)
  183. continue;
  184. for (px = 0; px < 8; px++) {
  185. int xx = x + (int)px;
  186. int on = (bits & (0x80u >> px)) != 0;
  187. if (xx < cx || xx >= cx + (int)cw)
  188. continue;
  189. if (on)
  190. put_px(s, (unsigned int)xx, (unsigned int)yy,
  191. fr, fgn, fb);
  192. else if (!transparent)
  193. put_px(s, (unsigned int)xx, (unsigned int)yy,
  194. br, bgn, bb);
  195. }
  196. }
  197. }
  198. void usurf_text(struct usurf *s, int x, int y, const char *str,
  199. unsigned int fg,
  200. int cx, int cy, unsigned int cw, unsigned int ch)
  201. {
  202. unsigned int i = 0;
  203. while (str[i]) {
  204. usurf_glyph(s, x + (int)i * 8, y, (unsigned char)str[i],
  205. fg, 0, 1, FONT_H, cx, cy, cw, ch);
  206. i++;
  207. }
  208. }
  209. void usurf_cells(struct usurf *s, const unsigned short *cells,
  210. unsigned int cols, unsigned int rows,
  211. int x0, int y0,
  212. int cx, int cy, unsigned int cw, unsigned int ch)
  213. {
  214. int col0 = (cx - x0) / 8;
  215. int col1 = (cx + (int)cw - x0 + 7) / 8;
  216. int row0 = (cy - y0) / 16;
  217. int row1 = (cy + (int)ch - y0 + 15) / 16;
  218. int row, col;
  219. if (col0 < 0) col0 = 0;
  220. if (row0 < 0) row0 = 0;
  221. if (col1 > (int)cols) col1 = (int)cols;
  222. if (row1 > (int)rows) row1 = (int)rows;
  223. if (col1 <= col0 || row1 <= row0)
  224. return;
  225. for (row = row0; row < row1; row++) {
  226. for (col = col0; col < col1; col++) {
  227. unsigned short cell = cells[(unsigned int)row * cols +
  228. (unsigned int)col];
  229. unsigned char c = (unsigned char)(cell & 0xFF);
  230. unsigned char attr = (unsigned char)(cell >> 8);
  231. unsigned int fg = ((unsigned int)pal_r[attr & 15] << 16) |
  232. ((unsigned int)pal_g[attr & 15] << 8) |
  233. pal_b[attr & 15];
  234. unsigned int bg = ((unsigned int)pal_r[(attr >> 4) & 15] << 16) |
  235. ((unsigned int)pal_g[(attr >> 4) & 15] << 8) |
  236. pal_b[(attr >> 4) & 15];
  237. usurf_glyph(s, x0 + col * 8, y0 + row * 16, c,
  238. fg, bg, 0, 16, cx, cy, cw, ch);
  239. }
  240. }
  241. }
  242. void usurf_canvas(struct usurf *s, const struct uwm_canvas *c,
  243. int x0, int y0,
  244. int cx, int cy, unsigned int cw, unsigned int ch)
  245. {
  246. int y;
  247. int sx0 = 0, sy0 = 0;
  248. int sx1 = (int)c->w, sy1 = (int)c->h;
  249. if (cx > x0)
  250. sx0 = cx - x0;
  251. if (cy > y0)
  252. sy0 = cy - y0;
  253. if (cx + (int)cw < x0 + sx1)
  254. sx1 = cx + (int)cw - x0;
  255. if (cy + (int)ch < y0 + sy1)
  256. sy1 = cy + (int)ch - y0;
  257. if (sx1 <= sx0 || sy1 <= sy0)
  258. return;
  259. usurf_pixels += (unsigned int)(sx1 - sx0) * (unsigned int)(sy1 - sy0);
  260. for (y = sy0; y < sy1; y++) {
  261. const unsigned short *src = c->px + (unsigned int)y * c->w +
  262. (unsigned int)sx0;
  263. unsigned int yy = (unsigned int)(y0 + y);
  264. unsigned int xx = (unsigned int)(x0 + sx0);
  265. unsigned char *row = s->px + (unsigned long)yy * s->pitch;
  266. unsigned int n = (unsigned int)(sx1 - sx0);
  267. unsigned int i;
  268. if (s->bpp == 32) {
  269. unsigned int *d = (unsigned int *)row + xx;
  270. for (i = 0; i < n; i++) {
  271. unsigned int p = src[i];
  272. unsigned int r5 = (p >> 11) & 0x1F;
  273. unsigned int g6 = (p >> 5) & 0x3F;
  274. unsigned int b5 = p & 0x1F;
  275. d[i] = 0xFF000000u |
  276. (((r5 << 3) | (r5 >> 2)) << 16) |
  277. (((g6 << 2) | (g6 >> 4)) << 8) |
  278. ((b5 << 3) | (b5 >> 2));
  279. }
  280. } else if (s->bpp == 16) {
  281. unsigned short *d = (unsigned short *)row + xx;
  282. for (i = 0; i < n; i++)
  283. d[i] = src[i];
  284. } else {
  285. unsigned char *d = row + xx * 3;
  286. for (i = 0; i < n; i++) {
  287. unsigned int p = src[i];
  288. unsigned int r5 = (p >> 11) & 0x1F;
  289. unsigned int g6 = (p >> 5) & 0x3F;
  290. unsigned int b5 = p & 0x1F;
  291. d[0] = (unsigned char)((b5 << 3) | (b5 >> 2));
  292. d[1] = (unsigned char)((g6 << 2) | (g6 >> 4));
  293. d[2] = (unsigned char)((r5 << 3) | (r5 >> 2));
  294. d += 3;
  295. }
  296. }
  297. }
  298. }