uwm_lib.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_8x8.h"
  5. #define FONT_H 8
  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. void usurf_init(struct usurf *s, void *px, unsigned int w, unsigned int h,
  16. unsigned int pitch, unsigned int bpp)
  17. {
  18. s->px = (unsigned char *)px;
  19. s->w = w;
  20. s->h = h;
  21. s->pitch = pitch;
  22. s->bpp = bpp;
  23. }
  24. static void put_px(struct usurf *s, unsigned int x, unsigned int y,
  25. unsigned int r, unsigned int g, unsigned int b)
  26. {
  27. unsigned char *p;
  28. if (x >= s->w || y >= s->h)
  29. return;
  30. p = s->px + (unsigned long)y * s->pitch;
  31. if (s->bpp == 16) {
  32. unsigned int v = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  33. unsigned short *d = (unsigned short *)p;
  34. d[x] = (unsigned short)v;
  35. } else {
  36. p += x * (s->bpp / 8);
  37. p[0] = (unsigned char)b;
  38. p[1] = (unsigned char)g;
  39. p[2] = (unsigned char)r;
  40. if (s->bpp == 32)
  41. p[3] = 0xFF;
  42. }
  43. }
  44. void usurf_fill(struct usurf *s, int x, int y, unsigned int w,
  45. unsigned int h, unsigned int rgb)
  46. {
  47. unsigned int r = (rgb >> 16) & 0xFF;
  48. unsigned int g = (rgb >> 8) & 0xFF;
  49. unsigned int b = rgb & 0xFF;
  50. unsigned int i, j;
  51. if (x < 0) {
  52. w -= (unsigned int)(-x);
  53. x = 0;
  54. }
  55. if (y < 0) {
  56. h -= (unsigned int)(-y);
  57. y = 0;
  58. }
  59. if ((unsigned int)x >= s->w || (unsigned int)y >= s->h)
  60. return;
  61. if ((unsigned int)x + w > s->w)
  62. w = s->w - (unsigned int)x;
  63. if ((unsigned int)y + h > s->h)
  64. h = s->h - (unsigned int)y;
  65. if (s->bpp == 16) {
  66. unsigned short v = (unsigned short)(((r >> 3) << 11) |
  67. ((g >> 2) << 5) | (b >> 3));
  68. for (j = 0; j < h; j++) {
  69. unsigned short *p = (unsigned short *)(s->px +
  70. (unsigned long)((unsigned int)y + j) * s->pitch) +
  71. (unsigned int)x;
  72. for (i = 0; i < w; i++)
  73. p[i] = v;
  74. }
  75. } else {
  76. unsigned int pb = s->bpp / 8;
  77. for (j = 0; j < h; j++) {
  78. unsigned char *p = s->px +
  79. (unsigned long)((unsigned int)y + j) * s->pitch +
  80. (unsigned int)x * pb;
  81. for (i = 0; i < w; i++) {
  82. p[0] = (unsigned char)b;
  83. p[1] = (unsigned char)g;
  84. p[2] = (unsigned char)r;
  85. if (pb == 4)
  86. p[3] = 0xFF;
  87. p += pb;
  88. }
  89. }
  90. }
  91. }
  92. void usurf_copy(struct usurf *s, int dx, int dy, int sx, int sy,
  93. unsigned int w, unsigned int h)
  94. {
  95. unsigned int pb = s->bpp / 8;
  96. int backwards;
  97. if (w == 0 || h == 0 || dx < 0 || dy < 0 || sx < 0 || sy < 0)
  98. return;
  99. if ((unsigned int)dx + w > s->w || (unsigned int)sx + w > s->w ||
  100. (unsigned int)dy + h > s->h || (unsigned int)sy + h > s->h)
  101. return;
  102. backwards = (dy > sy) || (dy == sy && dx > sx);
  103. {
  104. unsigned int i;
  105. for (i = 0; i < h; i++) {
  106. unsigned int syy = backwards ? (unsigned int)(sy + (int)h - 1 - (int)i)
  107. : (unsigned int)sy + i;
  108. unsigned int dyy = backwards ? (unsigned int)(dy + (int)h - 1 - (int)i)
  109. : (unsigned int)dy + i;
  110. unsigned char *src = s->px + (unsigned long)syy * s->pitch +
  111. (unsigned int)sx * pb;
  112. unsigned char *dst = s->px + (unsigned long)dyy * s->pitch +
  113. (unsigned int)dx * pb;
  114. unsigned int n = w * pb;
  115. if (backwards) {
  116. while (n--)
  117. dst[n] = src[n];
  118. } else {
  119. unsigned int k;
  120. for (k = 0; k < n; k++)
  121. dst[k] = src[k];
  122. }
  123. }
  124. }
  125. }
  126. void usurf_glyph(struct usurf *s, int x, int y, unsigned char c,
  127. unsigned int fg, unsigned int bg, int transparent,
  128. unsigned int height,
  129. int cx, int cy, unsigned int cw, unsigned int ch)
  130. {
  131. const unsigned char *glyph = &font_8x8[(unsigned int)c * FONT_8X8_ROWS];
  132. unsigned int fr = (fg >> 16) & 0xFF, fgn = (fg >> 8) & 0xFF, fb = fg & 0xFF;
  133. unsigned int br = (bg >> 16) & 0xFF, bgn = (bg >> 8) & 0xFF, bb = bg & 0xFF;
  134. unsigned int py, px;
  135. for (py = 0; py < height; py++) {
  136. unsigned char bits = (py < FONT_H) ? glyph[py] : 0;
  137. int yy = y + (int)py;
  138. if (yy < cy || yy >= cy + (int)ch)
  139. continue;
  140. for (px = 0; px < 8; px++) {
  141. int xx = x + (int)px;
  142. int on = (bits & (0x80u >> px)) != 0;
  143. if (xx < cx || xx >= cx + (int)cw)
  144. continue;
  145. if (on)
  146. put_px(s, (unsigned int)xx, (unsigned int)yy,
  147. fr, fgn, fb);
  148. else if (!transparent)
  149. put_px(s, (unsigned int)xx, (unsigned int)yy,
  150. br, bgn, bb);
  151. }
  152. }
  153. }
  154. void usurf_text(struct usurf *s, int x, int y, const char *str,
  155. unsigned int fg,
  156. int cx, int cy, unsigned int cw, unsigned int ch)
  157. {
  158. unsigned int i = 0;
  159. while (str[i]) {
  160. usurf_glyph(s, x + (int)i * 8, y, (unsigned char)str[i],
  161. fg, 0, 1, FONT_H, cx, cy, cw, ch);
  162. i++;
  163. }
  164. }
  165. void usurf_cells(struct usurf *s, const unsigned short *cells,
  166. unsigned int cols, unsigned int rows,
  167. int x0, int y0,
  168. int cx, int cy, unsigned int cw, unsigned int ch)
  169. {
  170. int col0 = (cx - x0) / 8;
  171. int col1 = (cx + (int)cw - x0 + 7) / 8;
  172. int row0 = (cy - y0) / 10;
  173. int row1 = (cy + (int)ch - y0 + 9) / 10;
  174. int row, col;
  175. if (col0 < 0) col0 = 0;
  176. if (row0 < 0) row0 = 0;
  177. if (col1 > (int)cols) col1 = (int)cols;
  178. if (row1 > (int)rows) row1 = (int)rows;
  179. if (col1 <= col0 || row1 <= row0)
  180. return;
  181. for (row = row0; row < row1; row++) {
  182. for (col = col0; col < col1; col++) {
  183. unsigned short cell = cells[(unsigned int)row * cols +
  184. (unsigned int)col];
  185. unsigned char c = (unsigned char)(cell & 0xFF);
  186. unsigned char attr = (unsigned char)(cell >> 8);
  187. unsigned int fg = ((unsigned int)pal_r[attr & 15] << 16) |
  188. ((unsigned int)pal_g[attr & 15] << 8) |
  189. pal_b[attr & 15];
  190. unsigned int bg = ((unsigned int)pal_r[(attr >> 4) & 15] << 16) |
  191. ((unsigned int)pal_g[(attr >> 4) & 15] << 8) |
  192. pal_b[(attr >> 4) & 15];
  193. usurf_glyph(s, x0 + col * 8, y0 + row * 10, c,
  194. fg, bg, 0, 10, cx, cy, cw, ch);
  195. }
  196. }
  197. }
  198. void usurf_canvas(struct usurf *s, const struct uwm_canvas *c,
  199. int x0, int y0,
  200. int cx, int cy, unsigned int cw, unsigned int ch)
  201. {
  202. int x, y;
  203. int sx0 = 0, sy0 = 0;
  204. int sx1 = (int)c->w, sy1 = (int)c->h;
  205. if (cx > x0)
  206. sx0 = cx - x0;
  207. if (cy > y0)
  208. sy0 = cy - y0;
  209. if (cx + (int)cw < x0 + sx1)
  210. sx1 = cx + (int)cw - x0;
  211. if (cy + (int)ch < y0 + sy1)
  212. sy1 = cy + (int)ch - y0;
  213. if (sx1 <= sx0 || sy1 <= sy0)
  214. return;
  215. for (y = sy0; y < sy1; y++) {
  216. for (x = sx0; x < sx1; x++) {
  217. unsigned int p = c->px[(unsigned int)y * c->w +
  218. (unsigned int)x];
  219. unsigned int r5 = (p >> 11) & 0x1F;
  220. unsigned int g6 = (p >> 5) & 0x3F;
  221. unsigned int b5 = p & 0x1F;
  222. unsigned int r = (r5 << 3) | (r5 >> 2);
  223. unsigned int g = (g6 << 2) | (g6 >> 4);
  224. unsigned int b = (b5 << 3) | (b5 >> 2);
  225. put_px(s, (unsigned int)(x0 + x),
  226. (unsigned int)(y0 + y), r, g, b);
  227. }
  228. }
  229. }