uwm_lib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 "ucmd_lib.h"
  5. #include "font_8x16.h"
  6. #include "ufont.h"
  7. #define FONT_H 16
  8. static const unsigned char pal_r[16] = {
  9. 0, 0, 0, 0, 170, 170, 170, 170, 85, 85, 85, 85, 255, 255, 255, 255
  10. };
  11. static const unsigned char pal_g[16] = {
  12. 0, 0, 0, 170, 0, 0, 85, 170, 85, 85, 255, 255, 85, 85, 255, 255
  13. };
  14. static const unsigned char pal_b[16] = {
  15. 0, 0, 170, 0, 170, 170, 0, 170, 85, 255, 85, 255, 85, 255, 85, 255
  16. };
  17. unsigned int usurf_pixels;
  18. void usurf_init(struct usurf *s, void *px, unsigned int w, unsigned int h,
  19. unsigned int pitch, unsigned int bpp)
  20. {
  21. s->px = (unsigned char *)px;
  22. s->w = w;
  23. s->h = h;
  24. s->pitch = pitch;
  25. s->bpp = bpp;
  26. s->font = 0;
  27. s->font_px = 16;
  28. }
  29. /* ---- Adwaita icon masks ------------------------------------------------
  30. * /ramfs/icons/adwaita16.icn (NBICO1, include/icon_abi.h): one 8-bit
  31. * coverage mask per icon, tinted and blended over the surface. Loaded once
  32. * per process into BSS, like ngui's copy, so a page heap reset or a pack
  33. * swap cannot leave a dangling pointer. */
  34. #define UWM_ICON_PX 16
  35. #define UWM_ICON_BYTES (UWM_ICON_PX * UWM_ICON_PX)
  36. static unsigned char icon_data[sizeof(struct icon_file_hdr) +
  37. UWM_ICON_COUNT * UWM_ICON_BYTES + 512];
  38. static int icon_state; /* 0 untried, 1 loaded, -1 unavailable */
  39. static unsigned int icon_count;
  40. static void icon_load(void)
  41. {
  42. const struct icon_file_hdr *h = (const struct icon_file_hdr *)icon_data;
  43. unsigned int max = sizeof(icon_data) - 512, have;
  44. int n, i;
  45. icon_state = -1;
  46. /* The FAT path may write a final partial sector past `max`; the
  47. * buffer keeps 512 bytes of slack for it. */
  48. n = u_syscall3(SYS_READ_FILE, (int)ICON_BUNDLE_PATH, (int)icon_data,
  49. (int)max);
  50. if (n < (int)sizeof(*h))
  51. return;
  52. for (i = 0; i < 8; i++)
  53. if (h->magic[i] != ICON_FILE_MAGIC[i])
  54. return;
  55. if (h->size != UWM_ICON_PX)
  56. return;
  57. have = ((unsigned int)n - sizeof(*h)) / UWM_ICON_BYTES;
  58. if (have > h->count)
  59. have = h->count;
  60. if (have > UWM_ICON_COUNT)
  61. have = UWM_ICON_COUNT;
  62. icon_count = have;
  63. icon_state = 1;
  64. }
  65. static const unsigned char *icon_mask(unsigned int icon)
  66. {
  67. if (icon_state == 0)
  68. icon_load();
  69. if (icon_state != 1 || icon >= icon_count)
  70. return 0;
  71. return icon_data + sizeof(struct icon_file_hdr) +
  72. icon * UWM_ICON_BYTES;
  73. }
  74. /* Tint a coverage mask with `rgb` and blend it over the canvas. */
  75. int usurf_icon(struct usurf *s, int x, int y, enum uwm_icon icon,
  76. unsigned int rgb)
  77. {
  78. const unsigned char *m = icon_mask((unsigned int)icon);
  79. unsigned int sr = (rgb >> 16) & 0xFF;
  80. unsigned int sg = (rgb >> 8) & 0xFF;
  81. unsigned int sb = rgb & 0xFF;
  82. int i, j;
  83. if (!s || !s->px || !m)
  84. return 0;
  85. for (j = 0; j < UWM_ICON_PX; j++) {
  86. int py = y + j;
  87. unsigned char *row;
  88. if (py < 0 || py >= (int)s->h)
  89. continue;
  90. row = s->px + (unsigned long)py * s->pitch;
  91. for (i = 0; i < UWM_ICON_PX; i++) {
  92. int px = x + i, a = m[j * UWM_ICON_PX + i];
  93. unsigned int dr, dg, db;
  94. if (!a || px < 0 || px >= (int)s->w)
  95. continue;
  96. usurf_pixels++;
  97. if (s->bpp == 16) {
  98. unsigned short *d = (unsigned short *)row + px;
  99. unsigned int v = *d;
  100. dr = ((v >> 11) & 0x1F) << 3;
  101. dg = ((v >> 5) & 0x3F) << 2;
  102. db = (v & 0x1F) << 3;
  103. dr = (dr * (255 - a) + sr * a + 127) / 255;
  104. dg = (dg * (255 - a) + sg * a + 127) / 255;
  105. db = (db * (255 - a) + sb * a + 127) / 255;
  106. *d = (unsigned short)(((dr >> 3) << 11) |
  107. ((dg >> 2) << 5) |
  108. (db >> 3));
  109. } else {
  110. unsigned char *p = row + px * (s->bpp / 8);
  111. /* BGR(A) byte order, like put_px(). */
  112. dr = p[2];
  113. dg = p[1];
  114. db = p[0];
  115. p[0] = (unsigned char)((db * (255 - a) +
  116. sb * a + 127) / 255);
  117. p[1] = (unsigned char)((dg * (255 - a) +
  118. sg * a + 127) / 255);
  119. p[2] = (unsigned char)((dr * (255 - a) +
  120. sr * a + 127) / 255);
  121. if (s->bpp == 32)
  122. p[3] = 0xFF;
  123. }
  124. }
  125. }
  126. return 1;
  127. }
  128. void usurf_font_attach(struct usurf *s, const char *path, int px)
  129. {
  130. if (!s)
  131. return;
  132. s->font_px = px;
  133. s->font = ufont_face_open_path(path);
  134. }
  135. unsigned int usurf_text_width(struct usurf *s, const char *str)
  136. {
  137. unsigned int n = 0;
  138. if (!str)
  139. return 0;
  140. if (s && s->font)
  141. return (unsigned int)ufont_text_width(s->font, s->font_px,
  142. str, -1, 0);
  143. while (str[n])
  144. n++;
  145. return n * 8;
  146. }
  147. static void put_px(struct usurf *s, unsigned int x, unsigned int y,
  148. unsigned int r, unsigned int g, unsigned int b)
  149. {
  150. unsigned char *p;
  151. if (x >= s->w || y >= s->h)
  152. return;
  153. usurf_pixels++;
  154. p = s->px + (unsigned long)y * s->pitch;
  155. if (s->bpp == 16) {
  156. unsigned int v = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  157. unsigned short *d = (unsigned short *)p;
  158. d[x] = (unsigned short)v;
  159. } else {
  160. p += x * (s->bpp / 8);
  161. p[0] = (unsigned char)b;
  162. p[1] = (unsigned char)g;
  163. p[2] = (unsigned char)r;
  164. if (s->bpp == 32)
  165. p[3] = 0xFF;
  166. }
  167. }
  168. void usurf_fill(struct usurf *s, int x, int y, unsigned int w,
  169. unsigned int h, unsigned int rgb)
  170. {
  171. unsigned int r = (rgb >> 16) & 0xFF;
  172. unsigned int g = (rgb >> 8) & 0xFF;
  173. unsigned int b = rgb & 0xFF;
  174. unsigned int i, j;
  175. if (x < 0) {
  176. w -= (unsigned int)(-x);
  177. x = 0;
  178. }
  179. if (y < 0) {
  180. h -= (unsigned int)(-y);
  181. y = 0;
  182. }
  183. if ((unsigned int)x >= s->w || (unsigned int)y >= s->h)
  184. return;
  185. if ((unsigned int)x + w > s->w)
  186. w = s->w - (unsigned int)x;
  187. if ((unsigned int)y + h > s->h)
  188. h = s->h - (unsigned int)y;
  189. usurf_pixels += w * h;
  190. if (s->bpp == 32) {
  191. unsigned int v = 0xFF000000u | (r << 16) | (g << 8) | b;
  192. for (j = 0; j < h; j++) {
  193. unsigned int *p = (unsigned int *)(s->px +
  194. (unsigned long)((unsigned int)y + j) * s->pitch) +
  195. (unsigned int)x;
  196. unsigned int n = w;
  197. __asm__ volatile ("cld; rep stosl"
  198. : "+D"(p), "+c"(n)
  199. : "a"(v)
  200. : "memory");
  201. }
  202. } else if (s->bpp == 16) {
  203. unsigned short v = (unsigned short)(((r >> 3) << 11) |
  204. ((g >> 2) << 5) | (b >> 3));
  205. for (j = 0; j < h; j++) {
  206. unsigned short *p = (unsigned short *)(s->px +
  207. (unsigned long)((unsigned int)y + j) * s->pitch) +
  208. (unsigned int)x;
  209. unsigned int n = w;
  210. __asm__ volatile ("cld; rep stosw"
  211. : "+D"(p), "+c"(n)
  212. : "a"(v)
  213. : "memory");
  214. }
  215. } else {
  216. for (j = 0; j < h; j++) {
  217. unsigned char *p = s->px +
  218. (unsigned long)((unsigned int)y + j) * s->pitch +
  219. (unsigned int)x * 3;
  220. for (i = 0; i < w; i++) {
  221. p[0] = (unsigned char)b;
  222. p[1] = (unsigned char)g;
  223. p[2] = (unsigned char)r;
  224. p += 3;
  225. }
  226. }
  227. }
  228. }
  229. void usurf_copy(struct usurf *s, int dx, int dy, int sx, int sy,
  230. unsigned int w, unsigned int h)
  231. {
  232. unsigned int pb = s->bpp / 8;
  233. int backwards;
  234. if (w == 0 || h == 0 || dx < 0 || dy < 0 || sx < 0 || sy < 0)
  235. return;
  236. if ((unsigned int)dx + w > s->w || (unsigned int)sx + w > s->w ||
  237. (unsigned int)dy + h > s->h || (unsigned int)sy + h > s->h)
  238. return;
  239. usurf_pixels += w * h;
  240. backwards = (dy > sy) || (dy == sy && dx > sx);
  241. {
  242. unsigned int i;
  243. for (i = 0; i < h; i++) {
  244. unsigned int syy = backwards ? (unsigned int)(sy + (int)h - 1 - (int)i)
  245. : (unsigned int)sy + i;
  246. unsigned int dyy = backwards ? (unsigned int)(dy + (int)h - 1 - (int)i)
  247. : (unsigned int)dy + i;
  248. unsigned char *src = s->px + (unsigned long)syy * s->pitch +
  249. (unsigned int)sx * pb;
  250. unsigned char *dst = s->px + (unsigned long)dyy * s->pitch +
  251. (unsigned int)dx * pb;
  252. unsigned int n = w;
  253. if (pb == 4) {
  254. unsigned int *d = (unsigned int *)dst;
  255. const unsigned int *s32 = (const unsigned int *)src;
  256. if (backwards) {
  257. while (n--)
  258. d[n] = s32[n];
  259. } else {
  260. __asm__ volatile ("cld; rep movsl"
  261. : "+D"(d), "+c"(n), "+S"(s32)
  262. :
  263. : "memory");
  264. }
  265. } else if (pb == 2) {
  266. unsigned short *d = (unsigned short *)dst;
  267. const unsigned short *s16 = (const unsigned short *)src;
  268. if (backwards) {
  269. while (n--)
  270. d[n] = s16[n];
  271. } else {
  272. __asm__ volatile ("cld; rep movsw"
  273. : "+D"(d), "+c"(n), "+S"(s16)
  274. :
  275. : "memory");
  276. }
  277. } else {
  278. unsigned int k = w * 3;
  279. if (backwards) {
  280. while (k--)
  281. dst[k] = src[k];
  282. } else {
  283. __asm__ volatile ("cld; rep movsb"
  284. : "+D"(dst), "+c"(k), "+S"(src)
  285. :
  286. : "memory");
  287. }
  288. }
  289. }
  290. }
  291. }
  292. void usurf_glyph(struct usurf *s, int x, int y, unsigned char c,
  293. unsigned int fg, unsigned int bg, int transparent,
  294. unsigned int height,
  295. int cx, int cy, unsigned int cw, unsigned int ch)
  296. {
  297. const unsigned char *glyph = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  298. unsigned int fr = (fg >> 16) & 0xFF, fgn = (fg >> 8) & 0xFF, fb = fg & 0xFF;
  299. unsigned int br = (bg >> 16) & 0xFF, bgn = (bg >> 8) & 0xFF, bb = bg & 0xFF;
  300. unsigned int py, px;
  301. for (py = 0; py < height; py++) {
  302. unsigned char bits = (py < FONT_H) ? glyph[py] : 0;
  303. int yy = y + (int)py;
  304. if (yy < cy || yy >= cy + (int)ch)
  305. continue;
  306. for (px = 0; px < 8; px++) {
  307. int xx = x + (int)px;
  308. int on = (bits & (0x80u >> px)) != 0;
  309. if (xx < cx || xx >= cx + (int)cw)
  310. continue;
  311. if (on)
  312. put_px(s, (unsigned int)xx, (unsigned int)yy,
  313. fr, fgn, fb);
  314. else if (!transparent)
  315. put_px(s, (unsigned int)xx, (unsigned int)yy,
  316. br, bgn, bb);
  317. }
  318. }
  319. }
  320. void usurf_text(struct usurf *s, int x, int y, const char *str,
  321. unsigned int fg,
  322. int cx, int cy, unsigned int cw, unsigned int ch)
  323. {
  324. unsigned int i = 0;
  325. struct ufont_metrics m;
  326. int ascent, pen = 0;
  327. if (!s || !str)
  328. return;
  329. if (!s->font) {
  330. while (str[i]) {
  331. usurf_glyph(s, x + (int)i * 8, y,
  332. (unsigned char)str[i], fg, 0, 1,
  333. FONT_H, cx, cy, cw, ch);
  334. i++;
  335. }
  336. return;
  337. }
  338. ufont_metrics(s->font, s->font_px, &m);
  339. ascent = m.ascent;
  340. while (str[i]) {
  341. const struct ufont_glyph *g =
  342. ufont_glyph(s->font, s->font_px,
  343. (unsigned char)str[i]);
  344. if (g) {
  345. usurf_pixels += (unsigned int)ufont_blit_rgb(
  346. s->px, (int)s->w, (int)s->h, (int)s->pitch,
  347. (int)s->bpp, x + (pen >> 6), y + ascent,
  348. g, fg, cx, cy, cw, ch);
  349. pen += g->advance;
  350. }
  351. i++;
  352. }
  353. }
  354. void usurf_cells(struct usurf *s, const unsigned short *cells,
  355. unsigned int cols, unsigned int rows,
  356. int x0, int y0,
  357. int cx, int cy, unsigned int cw, unsigned int ch)
  358. {
  359. int col0 = (cx - x0) / 8;
  360. int col1 = (cx + (int)cw - x0 + 7) / 8;
  361. int row0 = (cy - y0) / 16;
  362. int row1 = (cy + (int)ch - y0 + 15) / 16;
  363. int row, col;
  364. if (col0 < 0) col0 = 0;
  365. if (row0 < 0) row0 = 0;
  366. if (col1 > (int)cols) col1 = (int)cols;
  367. if (row1 > (int)rows) row1 = (int)rows;
  368. if (col1 <= col0 || row1 <= row0)
  369. return;
  370. for (row = row0; row < row1; row++) {
  371. for (col = col0; col < col1; col++) {
  372. unsigned short cell = cells[(unsigned int)row * cols +
  373. (unsigned int)col];
  374. unsigned char c = (unsigned char)(cell & 0xFF);
  375. unsigned char attr = (unsigned char)(cell >> 8);
  376. unsigned int fg = ((unsigned int)pal_r[attr & 15] << 16) |
  377. ((unsigned int)pal_g[attr & 15] << 8) |
  378. pal_b[attr & 15];
  379. unsigned int bg = ((unsigned int)pal_r[(attr >> 4) & 15] << 16) |
  380. ((unsigned int)pal_g[(attr >> 4) & 15] << 8) |
  381. pal_b[(attr >> 4) & 15];
  382. usurf_glyph(s, x0 + col * 8, y0 + row * 16, c,
  383. fg, bg, 0, 16, cx, cy, cw, ch);
  384. }
  385. }
  386. }
  387. void usurf_canvas(struct usurf *s, const struct uwm_canvas *c,
  388. int x0, int y0,
  389. int cx, int cy, unsigned int cw, unsigned int ch)
  390. {
  391. int y;
  392. int sx0 = 0, sy0 = 0;
  393. int sx1 = (int)c->w, sy1 = (int)c->h;
  394. if (cx > x0)
  395. sx0 = cx - x0;
  396. if (cy > y0)
  397. sy0 = cy - y0;
  398. if (cx + (int)cw < x0 + sx1)
  399. sx1 = cx + (int)cw - x0;
  400. if (cy + (int)ch < y0 + sy1)
  401. sy1 = cy + (int)ch - y0;
  402. if (sx1 <= sx0 || sy1 <= sy0)
  403. return;
  404. usurf_pixels += (unsigned int)(sx1 - sx0) * (unsigned int)(sy1 - sy0);
  405. for (y = sy0; y < sy1; y++) {
  406. const unsigned short *src = c->px + (unsigned int)y * c->w +
  407. (unsigned int)sx0;
  408. unsigned int yy = (unsigned int)(y0 + y);
  409. unsigned int xx = (unsigned int)(x0 + sx0);
  410. unsigned char *row = s->px + (unsigned long)yy * s->pitch;
  411. unsigned int n = (unsigned int)(sx1 - sx0);
  412. unsigned int i;
  413. if (s->bpp == 32) {
  414. unsigned int *d = (unsigned int *)row + xx;
  415. for (i = 0; i < n; i++) {
  416. unsigned int p = src[i];
  417. unsigned int r5 = (p >> 11) & 0x1F;
  418. unsigned int g6 = (p >> 5) & 0x3F;
  419. unsigned int b5 = p & 0x1F;
  420. d[i] = 0xFF000000u |
  421. (((r5 << 3) | (r5 >> 2)) << 16) |
  422. (((g6 << 2) | (g6 >> 4)) << 8) |
  423. ((b5 << 3) | (b5 >> 2));
  424. }
  425. } else if (s->bpp == 16) {
  426. unsigned short *d = (unsigned short *)row + xx;
  427. for (i = 0; i < n; i++)
  428. d[i] = src[i];
  429. } else {
  430. unsigned char *d = row + xx * 3;
  431. for (i = 0; i < n; i++) {
  432. unsigned int p = src[i];
  433. unsigned int r5 = (p >> 11) & 0x1F;
  434. unsigned int g6 = (p >> 5) & 0x3F;
  435. unsigned int b5 = p & 0x1F;
  436. d[0] = (unsigned char)((b5 << 3) | (b5 >> 2));
  437. d[1] = (unsigned char)((g6 << 2) | (g6 >> 4));
  438. d[2] = (unsigned char)((r5 << 3) | (r5 >> 2));
  439. d += 3;
  440. }
  441. }
  442. }
  443. }