uwm_lib.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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, clipped to
  75. * the rectangle (cx,cy,cw,ch). */
  76. int usurf_icon_clip(struct usurf *s, int x, int y, enum uwm_icon icon,
  77. unsigned int rgb, int cx, int cy,
  78. unsigned int cw, unsigned int ch)
  79. {
  80. const unsigned char *m = icon_mask((unsigned int)icon);
  81. unsigned int sr = (rgb >> 16) & 0xFF;
  82. unsigned int sg = (rgb >> 8) & 0xFF;
  83. unsigned int sb = rgb & 0xFF;
  84. int x1 = cx + (int)cw, y1 = cy + (int)ch;
  85. int i, j;
  86. if (!s || !s->px || !m)
  87. return 0;
  88. for (j = 0; j < UWM_ICON_PX; j++) {
  89. int py = y + j;
  90. unsigned char *row;
  91. if (py < cy || py >= y1 || py < 0 || py >= (int)s->h)
  92. continue;
  93. row = s->px + (unsigned long)py * s->pitch;
  94. for (i = 0; i < UWM_ICON_PX; i++) {
  95. int px = x + i, a = m[j * UWM_ICON_PX + i];
  96. unsigned int dr, dg, db;
  97. if (!a || px < cx || px >= x1 ||
  98. px < 0 || px >= (int)s->w)
  99. continue;
  100. usurf_pixels++;
  101. if (s->bpp == 16) {
  102. unsigned short *d = (unsigned short *)row + px;
  103. unsigned int v = *d;
  104. dr = ((v >> 11) & 0x1F) << 3;
  105. dg = ((v >> 5) & 0x3F) << 2;
  106. db = (v & 0x1F) << 3;
  107. dr = (dr * (255 - a) + sr * a + 127) / 255;
  108. dg = (dg * (255 - a) + sg * a + 127) / 255;
  109. db = (db * (255 - a) + sb * a + 127) / 255;
  110. *d = (unsigned short)(((dr >> 3) << 11) |
  111. ((dg >> 2) << 5) |
  112. (db >> 3));
  113. } else {
  114. unsigned char *p = row + px * (s->bpp / 8);
  115. /* BGR(A) byte order, like put_px(). */
  116. dr = p[2];
  117. dg = p[1];
  118. db = p[0];
  119. p[0] = (unsigned char)((db * (255 - a) +
  120. sb * a + 127) / 255);
  121. p[1] = (unsigned char)((dg * (255 - a) +
  122. sg * a + 127) / 255);
  123. p[2] = (unsigned char)((dr * (255 - a) +
  124. sr * a + 127) / 255);
  125. if (s->bpp == 32)
  126. p[3] = 0xFF;
  127. }
  128. }
  129. }
  130. return 1;
  131. }
  132. /* The same without clipping, i.e. clipped to the whole canvas. */
  133. int usurf_icon(struct usurf *s, int x, int y, enum uwm_icon icon,
  134. unsigned int rgb)
  135. {
  136. if (!s)
  137. return 0;
  138. return usurf_icon_clip(s, x, y, icon, rgb, 0, 0, s->w, s->h);
  139. }
  140. /* ---- Window drop shadow ------------------------------------------------ */
  141. #define SHADOW_FALLOFF_N 11
  142. static const unsigned char shadow_falloff[SHADOW_FALLOFF_N] = {
  143. 0, 120, 110, 100, 90, 80, 69, 58, 46, 33, 20
  144. };
  145. /* Darken one band outside (wx,wy)-(wx1,wy1). The alpha comes from the
  146. * rounded distance to the window edge (max + 0.4 * min), so the four bands
  147. * together form a soft ring whose corners fade in a curve instead of a
  148. * square corner, without touching the window interior. */
  149. static void shadow_band(struct usurf *s, int x0, int y0, int x1, int y1,
  150. int wx, int wy, int wx1, int wy1, int spread,
  151. int bytes)
  152. {
  153. int x, y;
  154. for (y = y0; y < y1; y++) {
  155. unsigned char *row = s->px + (unsigned long)y * s->pitch;
  156. int dy = 0;
  157. if (y < wy)
  158. dy = wy - y;
  159. else if (y >= wy1)
  160. dy = y - wy1 + 1;
  161. for (x = x0; x < x1; x++) {
  162. int dx = 0, d, lo, hi;
  163. unsigned int a, ia;
  164. unsigned char *p;
  165. if (x < wx)
  166. dx = wx - x;
  167. else if (x >= wx1)
  168. dx = x - wx1 + 1;
  169. lo = dx < dy ? dx : dy;
  170. hi = dx < dy ? dy : dx;
  171. d = hi + ((lo * 13) >> 5);
  172. if (d <= 0 || d > spread)
  173. continue;
  174. if (d >= SHADOW_FALLOFF_N)
  175. d = SHADOW_FALLOFF_N - 1;
  176. a = shadow_falloff[d];
  177. if (!a)
  178. continue;
  179. ia = 256 - a;
  180. usurf_pixels++;
  181. if (s->bpp == 16) {
  182. unsigned short *q = (unsigned short *)row + x;
  183. unsigned int v = *q;
  184. unsigned int r = ((((v >> 11) & 0x1F) * ia)
  185. + 128) >> 8;
  186. unsigned int g = ((((v >> 5) & 0x3F) * ia)
  187. + 128) >> 8;
  188. unsigned int b = (((v & 0x1F) * ia)
  189. + 128) >> 8;
  190. *q = (unsigned short)((r << 11) | (g << 5) | b);
  191. } else {
  192. p = row + x * bytes;
  193. p[0] = (unsigned char)((p[0] * ia + 128) >> 8);
  194. p[1] = (unsigned char)((p[1] * ia + 128) >> 8);
  195. p[2] = (unsigned char)((p[2] * ia + 128) >> 8);
  196. if (s->bpp == 32)
  197. p[3] = 0xFF;
  198. }
  199. }
  200. }
  201. }
  202. /* A soft drop shadow around a window rectangle: `spread` pixels outward,
  203. * strongest next to the window. The window interior is never touched and
  204. * the whole ring is clipped to (cx,cy,cw,ch); callers pass the current
  205. * damage rectangle so a partial repaint only darkens pixels it owns. */
  206. void usurf_window_shadow(struct usurf *s, int wx, int wy, int ww, int wh,
  207. int spread, int cx, int cy,
  208. unsigned int cw, unsigned int ch)
  209. {
  210. int wx1 = wx + ww, wy1 = wy + wh;
  211. int x0, y0, x1, y1, ix0, iy0, ix1, iy1;
  212. int bytes;
  213. if (!s || !s->px || ww <= 0 || wh <= 0 || spread <= 0)
  214. return;
  215. if (s->bpp != 16 && s->bpp != 24 && s->bpp != 32)
  216. return;
  217. bytes = (int)(s->bpp / 8);
  218. x0 = wx - spread;
  219. y0 = wy - spread;
  220. x1 = wx1 + spread;
  221. y1 = wy1 + spread;
  222. ix0 = cx;
  223. iy0 = cy;
  224. ix1 = cx + (int)cw;
  225. iy1 = cy + (int)ch;
  226. if (ix0 < 0) ix0 = 0;
  227. if (iy0 < 0) iy0 = 0;
  228. if (ix1 > (int)s->w) ix1 = (int)s->w;
  229. if (iy1 > (int)s->h) iy1 = (int)s->h;
  230. if (x0 < ix0) x0 = ix0;
  231. if (y0 < iy0) y0 = iy0;
  232. if (x1 > ix1) x1 = ix1;
  233. if (y1 > iy1) y1 = iy1;
  234. if (x1 <= x0 || y1 <= y0)
  235. return;
  236. /* Corners belong to the top and bottom bands. */
  237. shadow_band(s, x0, y0, x1, wy < y1 ? wy : y1,
  238. wx, wy, wx1, wy1, spread, bytes);
  239. shadow_band(s, x0, wy1 > y0 ? wy1 : y0, x1, y1,
  240. wx, wy, wx1, wy1, spread, bytes);
  241. {
  242. int by0 = wy > y0 ? wy : y0;
  243. int by1 = wy1 < y1 ? wy1 : y1;
  244. shadow_band(s, x0, by0, wx < x1 ? wx : x1, by1,
  245. wx, wy, wx1, wy1, spread, bytes);
  246. shadow_band(s, wx1 > x0 ? wx1 : x0, by0, x1, by1,
  247. wx, wy, wx1, wy1, spread, bytes);
  248. }
  249. }
  250. /* ---- Full-colour app icons -------------------------------------------- */
  251. #define UWM_APP_BYTES (APP_ICON_PX * APP_ICON_PX * 3u)
  252. static unsigned char app_icon_data[sizeof(struct app_icon_file_hdr) +
  253. APP_ICON_COUNT * UWM_APP_BYTES + 512];
  254. static int app_icon_state; /* 0 untried, 1 loaded, -1 unavailable */
  255. static unsigned int app_icon_count;
  256. static void app_icon_load(void)
  257. {
  258. const struct app_icon_file_hdr *h =
  259. (const struct app_icon_file_hdr *)app_icon_data;
  260. unsigned int max = sizeof(app_icon_data) - 512, have;
  261. int n, i;
  262. app_icon_state = -1;
  263. /* The FAT path may write a final partial sector past `max`; the
  264. * buffer keeps 512 bytes of slack for it. */
  265. n = u_syscall3(SYS_READ_FILE, (int)APP_ICON_BUNDLE_PATH,
  266. (int)app_icon_data, (int)max);
  267. if (n < (int)sizeof(*h))
  268. return;
  269. for (i = 0; i < 8; i++)
  270. if (h->magic[i] != APP_ICON_FILE_MAGIC[i])
  271. return;
  272. if (h->size != APP_ICON_PX)
  273. return;
  274. have = ((unsigned int)n - sizeof(*h)) / UWM_APP_BYTES;
  275. if (have > h->count)
  276. have = h->count;
  277. if (have > APP_ICON_COUNT)
  278. have = APP_ICON_COUNT;
  279. app_icon_count = have;
  280. app_icon_state = 1;
  281. }
  282. static const unsigned char *app_icon_src(unsigned int icon)
  283. {
  284. if (app_icon_state == 0)
  285. app_icon_load();
  286. if (app_icon_state != 1 || icon >= app_icon_count)
  287. return 0;
  288. return app_icon_data + sizeof(struct app_icon_file_hdr) +
  289. icon * UWM_APP_BYTES;
  290. }
  291. /* Premultiplied source-over: dst = src + dst * (255 - a) / 255. */
  292. static void blend_premul_px(struct usurf *s, int x, int y,
  293. unsigned int r, unsigned int g, unsigned int b,
  294. unsigned int a)
  295. {
  296. unsigned char *row;
  297. unsigned int dr, dg, db;
  298. if (!a || x < 0 || y < 0 || (unsigned int)x >= s->w ||
  299. (unsigned int)y >= s->h)
  300. return;
  301. usurf_pixels++;
  302. row = s->px + (unsigned long)(unsigned int)y * s->pitch;
  303. if (s->bpp == 16) {
  304. unsigned short *d = (unsigned short *)row + (unsigned int)x;
  305. unsigned int v = *d;
  306. dr = ((v >> 11) & 0x1F) << 3;
  307. dg = ((v >> 5) & 0x3F) << 2;
  308. db = (v & 0x1F) << 3;
  309. if (a == 255) {
  310. dr = r; dg = g; db = b;
  311. } else {
  312. dr = r + (dr * (255 - a) + 127) / 255;
  313. dg = g + (dg * (255 - a) + 127) / 255;
  314. db = b + (db * (255 - a) + 127) / 255;
  315. }
  316. if (dr > 255) dr = 255;
  317. if (dg > 255) dg = 255;
  318. if (db > 255) db = 255;
  319. *d = (unsigned short)(((dr >> 3) << 11) |
  320. ((dg >> 2) << 5) | (db >> 3));
  321. } else {
  322. unsigned char *p = row + (unsigned int)x * (s->bpp / 8);
  323. /* BGR(A) byte order, like put_px(). */
  324. if (a == 255) {
  325. dr = r; dg = g; db = b;
  326. } else {
  327. dr = r + (p[2] * (255 - a) + 127) / 255;
  328. dg = g + (p[1] * (255 - a) + 127) / 255;
  329. db = b + (p[0] * (255 - a) + 127) / 255;
  330. }
  331. if (dr > 255) dr = 255;
  332. if (dg > 255) dg = 255;
  333. if (db > 255) db = 255;
  334. p[0] = (unsigned char)db;
  335. p[1] = (unsigned char)dg;
  336. p[2] = (unsigned char)dr;
  337. if (s->bpp == 32)
  338. p[3] = 0xFF;
  339. }
  340. }
  341. static void app_px(const unsigned char *src, int idx,
  342. unsigned int *r, unsigned int *g, unsigned int *b,
  343. unsigned int *a)
  344. {
  345. const unsigned char *p = src + (unsigned long)idx * 3;
  346. unsigned int v = (unsigned int)p[0] | ((unsigned int)p[1] << 8);
  347. unsigned int r5 = (v >> 11) & 0x1F;
  348. unsigned int g6 = (v >> 5) & 0x3F;
  349. unsigned int b5 = v & 0x1F;
  350. *r = (r5 << 3) | (r5 >> 2);
  351. *g = (g6 << 2) | (g6 >> 4);
  352. *b = (b5 << 3) | (b5 >> 2);
  353. *a = p[2];
  354. }
  355. int usurf_app_icon(struct usurf *s, int x, int y, int size,
  356. enum app_icon icon)
  357. {
  358. const unsigned char *src;
  359. int dx, dy;
  360. if (!s || !s->px || size <= 0 || icon < 0 ||
  361. (unsigned int)icon >= APP_ICON_COUNT)
  362. return 0;
  363. src = app_icon_src((unsigned int)icon);
  364. if (!src)
  365. return 0;
  366. for (dy = 0; dy < size; dy++) {
  367. int py = y + dy;
  368. if (py < 0 || py >= (int)s->h)
  369. continue;
  370. for (dx = 0; dx < size; dx++) {
  371. int px = x + dx;
  372. unsigned int r, g, b, a;
  373. if (px < 0 || px >= (int)s->w)
  374. continue;
  375. if (size == APP_ICON_PX) {
  376. app_px(src, dy * APP_ICON_PX + dx,
  377. &r, &g, &b, &a);
  378. } else if (size * 2 <= APP_ICON_PX) {
  379. /* Downscale: average a 2x2 or 4x4 grid over the
  380. * destination pixel's source box. This keeps the
  381. * 48 px dock and 18 px menu icons clean. */
  382. int taps = APP_ICON_PX >= size * 4 ? 4 : 2;
  383. int sx0 = dx * APP_ICON_PX / size;
  384. int sy0 = dy * APP_ICON_PX / size;
  385. int sx1 = ((dx + 1) * APP_ICON_PX + size - 1) /
  386. size;
  387. int sy1 = ((dy + 1) * APP_ICON_PX + size - 1) /
  388. size;
  389. unsigned int sr = 0, sg = 0, sb = 0, sa = 0;
  390. int tx, ty, n = 0, den = taps * 2;
  391. for (ty = 0; ty < taps; ty++) {
  392. int sy = (sy0 * den +
  393. (sy1 - sy0) *
  394. (2 * ty + 1)) / den;
  395. if (sy >= APP_ICON_PX)
  396. sy = APP_ICON_PX - 1;
  397. for (tx = 0; tx < taps; tx++) {
  398. int sx = (sx0 * den +
  399. (sx1 - sx0) *
  400. (2 * tx + 1)) / den;
  401. if (sx >= APP_ICON_PX)
  402. sx = APP_ICON_PX - 1;
  403. app_px(src,
  404. sy * APP_ICON_PX + sx,
  405. &r, &g, &b, &a);
  406. sr += r; sg += g;
  407. sb += b; sa += a;
  408. n++;
  409. }
  410. }
  411. r = (sr + (unsigned int)n / 2) /
  412. (unsigned int)n;
  413. g = (sg + (unsigned int)n / 2) /
  414. (unsigned int)n;
  415. b = (sb + (unsigned int)n / 2) /
  416. (unsigned int)n;
  417. a = (sa + (unsigned int)n / 2) /
  418. (unsigned int)n;
  419. } else {
  420. /* Upscale (or a small downscale): bilinear
  421. * sampling around the destination centre. */
  422. int sx25 = ((2 * dx + 1) * APP_ICON_PX * 256) /
  423. (2 * size) - 128;
  424. int sy25 = ((2 * dy + 1) * APP_ICON_PX * 256) /
  425. (2 * size) - 128;
  426. int x0, y0, fx, fy;
  427. unsigned int r00, g00, b00, a00;
  428. unsigned int r10, g10, b10, a10;
  429. unsigned int r01, g01, b01, a01;
  430. unsigned int r11, g11, b11, a11;
  431. if (sx25 < 0) sx25 = 0;
  432. if (sy25 < 0) sy25 = 0;
  433. x0 = sx25 >> 8;
  434. y0 = sy25 >> 8;
  435. fx = sx25 & 255;
  436. fy = sy25 & 255;
  437. if (x0 >= APP_ICON_PX - 1) {
  438. x0 = APP_ICON_PX - 2;
  439. fx = 255;
  440. }
  441. if (y0 >= APP_ICON_PX - 1) {
  442. y0 = APP_ICON_PX - 2;
  443. fy = 255;
  444. }
  445. app_px(src, y0 * APP_ICON_PX + x0,
  446. &r00, &g00, &b00, &a00);
  447. app_px(src, y0 * APP_ICON_PX + x0 + 1,
  448. &r10, &g10, &b10, &a10);
  449. app_px(src, (y0 + 1) * APP_ICON_PX + x0,
  450. &r01, &g01, &b01, &a01);
  451. app_px(src, (y0 + 1) * APP_ICON_PX + x0 + 1,
  452. &r11, &g11, &b11, &a11);
  453. r = (r00 * (256 - fx) * (256 - fy) +
  454. r10 * fx * (256 - fy) +
  455. r01 * (256 - fx) * fy +
  456. r11 * fx * fy) >> 16;
  457. g = (g00 * (256 - fx) * (256 - fy) +
  458. g10 * fx * (256 - fy) +
  459. g01 * (256 - fx) * fy +
  460. g11 * fx * fy) >> 16;
  461. b = (b00 * (256 - fx) * (256 - fy) +
  462. b10 * fx * (256 - fy) +
  463. b01 * (256 - fx) * fy +
  464. b11 * fx * fy) >> 16;
  465. a = (a00 * (256 - fx) * (256 - fy) +
  466. a10 * fx * (256 - fy) +
  467. a01 * (256 - fx) * fy +
  468. a11 * fx * fy) >> 16;
  469. }
  470. blend_premul_px(s, px, py, r, g, b, a);
  471. }
  472. }
  473. return 1;
  474. }
  475. void usurf_fill_round_alpha(struct usurf *s, int x, int y, int w, int h,
  476. int radius, unsigned int rgb,
  477. unsigned int alpha)
  478. {
  479. unsigned int sr = (rgb >> 16) & 0xFF;
  480. unsigned int sg = (rgb >> 8) & 0xFF;
  481. unsigned int sb = rgb & 0xFF;
  482. unsigned int pr, pg, pb;
  483. int x0 = x, y0 = y, x1 = x + w, y1 = y + h;
  484. int i, j;
  485. if (!s || !s->px || alpha == 0 || w <= 0 || h <= 0)
  486. return;
  487. if (x0 < 0) x0 = 0;
  488. if (y0 < 0) y0 = 0;
  489. if (x1 > (int)s->w) x1 = (int)s->w;
  490. if (y1 > (int)s->h) y1 = (int)s->h;
  491. if (x1 <= x0 || y1 <= y0)
  492. return;
  493. if (radius < 0)
  494. radius = 0;
  495. if (radius > w / 2)
  496. radius = w / 2;
  497. if (radius > h / 2)
  498. radius = h / 2;
  499. pr = (sr * alpha + 127) / 255;
  500. pg = (sg * alpha + 127) / 255;
  501. pb = (sb * alpha + 127) / 255;
  502. for (j = y0; j < y1; j++) {
  503. int cy = y + radius;
  504. if (j >= y + h - radius)
  505. cy = y + h - radius - 1;
  506. for (i = x0; i < x1; i++) {
  507. int cx = x + radius;
  508. if (i >= x + w - radius)
  509. cx = x + w - radius - 1;
  510. if (radius && (i < x + radius ||
  511. i >= x + w - radius) &&
  512. (j < y + radius || j >= y + h - radius)) {
  513. int ddx = i - cx, ddy = j - cy;
  514. if (ddx * ddx + ddy * ddy > radius * radius)
  515. continue;
  516. }
  517. blend_premul_px(s, i, j, pr, pg, pb, alpha);
  518. }
  519. }
  520. }
  521. void usurf_font_attach(struct usurf *s, const char *path, int px)
  522. {
  523. if (!s)
  524. return;
  525. s->font_px = px;
  526. s->font = ufont_face_open_path(path);
  527. }
  528. unsigned int usurf_text_width(struct usurf *s, const char *str)
  529. {
  530. unsigned int n = 0;
  531. if (!str)
  532. return 0;
  533. if (s && s->font)
  534. return (unsigned int)ufont_text_width(s->font, s->font_px,
  535. str, -1, 0);
  536. while (str[n])
  537. n++;
  538. return n * 8;
  539. }
  540. static void put_px(struct usurf *s, unsigned int x, unsigned int y,
  541. unsigned int r, unsigned int g, unsigned int b)
  542. {
  543. unsigned char *p;
  544. if (x >= s->w || y >= s->h)
  545. return;
  546. usurf_pixels++;
  547. p = s->px + (unsigned long)y * s->pitch;
  548. if (s->bpp == 16) {
  549. unsigned int v = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  550. unsigned short *d = (unsigned short *)p;
  551. d[x] = (unsigned short)v;
  552. } else {
  553. p += x * (s->bpp / 8);
  554. p[0] = (unsigned char)b;
  555. p[1] = (unsigned char)g;
  556. p[2] = (unsigned char)r;
  557. if (s->bpp == 32)
  558. p[3] = 0xFF;
  559. }
  560. }
  561. void usurf_fill(struct usurf *s, int x, int y, unsigned int w,
  562. unsigned int h, unsigned int rgb)
  563. {
  564. unsigned int r = (rgb >> 16) & 0xFF;
  565. unsigned int g = (rgb >> 8) & 0xFF;
  566. unsigned int b = rgb & 0xFF;
  567. unsigned int i, j;
  568. if (x < 0) {
  569. w -= (unsigned int)(-x);
  570. x = 0;
  571. }
  572. if (y < 0) {
  573. h -= (unsigned int)(-y);
  574. y = 0;
  575. }
  576. if ((unsigned int)x >= s->w || (unsigned int)y >= s->h)
  577. return;
  578. if ((unsigned int)x + w > s->w)
  579. w = s->w - (unsigned int)x;
  580. if ((unsigned int)y + h > s->h)
  581. h = s->h - (unsigned int)y;
  582. usurf_pixels += w * h;
  583. if (s->bpp == 32) {
  584. unsigned int v = 0xFF000000u | (r << 16) | (g << 8) | b;
  585. for (j = 0; j < h; j++) {
  586. unsigned int *p = (unsigned int *)(s->px +
  587. (unsigned long)((unsigned int)y + j) * s->pitch) +
  588. (unsigned int)x;
  589. unsigned int n = w;
  590. __asm__ volatile ("cld; rep stosl"
  591. : "+D"(p), "+c"(n)
  592. : "a"(v)
  593. : "memory");
  594. }
  595. } else if (s->bpp == 16) {
  596. unsigned short v = (unsigned short)(((r >> 3) << 11) |
  597. ((g >> 2) << 5) | (b >> 3));
  598. for (j = 0; j < h; j++) {
  599. unsigned short *p = (unsigned short *)(s->px +
  600. (unsigned long)((unsigned int)y + j) * s->pitch) +
  601. (unsigned int)x;
  602. unsigned int n = w;
  603. __asm__ volatile ("cld; rep stosw"
  604. : "+D"(p), "+c"(n)
  605. : "a"(v)
  606. : "memory");
  607. }
  608. } else {
  609. for (j = 0; j < h; j++) {
  610. unsigned char *p = s->px +
  611. (unsigned long)((unsigned int)y + j) * s->pitch +
  612. (unsigned int)x * 3;
  613. for (i = 0; i < w; i++) {
  614. p[0] = (unsigned char)b;
  615. p[1] = (unsigned char)g;
  616. p[2] = (unsigned char)r;
  617. p += 3;
  618. }
  619. }
  620. }
  621. }
  622. void usurf_copy(struct usurf *s, int dx, int dy, int sx, int sy,
  623. unsigned int w, unsigned int h)
  624. {
  625. unsigned int pb = s->bpp / 8;
  626. int backwards;
  627. if (w == 0 || h == 0 || dx < 0 || dy < 0 || sx < 0 || sy < 0)
  628. return;
  629. if ((unsigned int)dx + w > s->w || (unsigned int)sx + w > s->w ||
  630. (unsigned int)dy + h > s->h || (unsigned int)sy + h > s->h)
  631. return;
  632. usurf_pixels += w * h;
  633. backwards = (dy > sy) || (dy == sy && dx > sx);
  634. {
  635. unsigned int i;
  636. for (i = 0; i < h; i++) {
  637. unsigned int syy = backwards ? (unsigned int)(sy + (int)h - 1 - (int)i)
  638. : (unsigned int)sy + i;
  639. unsigned int dyy = backwards ? (unsigned int)(dy + (int)h - 1 - (int)i)
  640. : (unsigned int)dy + i;
  641. unsigned char *src = s->px + (unsigned long)syy * s->pitch +
  642. (unsigned int)sx * pb;
  643. unsigned char *dst = s->px + (unsigned long)dyy * s->pitch +
  644. (unsigned int)dx * pb;
  645. unsigned int n = w;
  646. if (pb == 4) {
  647. unsigned int *d = (unsigned int *)dst;
  648. const unsigned int *s32 = (const unsigned int *)src;
  649. if (backwards) {
  650. while (n--)
  651. d[n] = s32[n];
  652. } else {
  653. __asm__ volatile ("cld; rep movsl"
  654. : "+D"(d), "+c"(n), "+S"(s32)
  655. :
  656. : "memory");
  657. }
  658. } else if (pb == 2) {
  659. unsigned short *d = (unsigned short *)dst;
  660. const unsigned short *s16 = (const unsigned short *)src;
  661. if (backwards) {
  662. while (n--)
  663. d[n] = s16[n];
  664. } else {
  665. __asm__ volatile ("cld; rep movsw"
  666. : "+D"(d), "+c"(n), "+S"(s16)
  667. :
  668. : "memory");
  669. }
  670. } else {
  671. unsigned int k = w * 3;
  672. if (backwards) {
  673. while (k--)
  674. dst[k] = src[k];
  675. } else {
  676. __asm__ volatile ("cld; rep movsb"
  677. : "+D"(dst), "+c"(k), "+S"(src)
  678. :
  679. : "memory");
  680. }
  681. }
  682. }
  683. }
  684. }
  685. void usurf_glyph(struct usurf *s, int x, int y, unsigned char c,
  686. unsigned int fg, unsigned int bg, int transparent,
  687. unsigned int height,
  688. int cx, int cy, unsigned int cw, unsigned int ch)
  689. {
  690. const unsigned char *glyph = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  691. unsigned int fr = (fg >> 16) & 0xFF, fgn = (fg >> 8) & 0xFF, fb = fg & 0xFF;
  692. unsigned int br = (bg >> 16) & 0xFF, bgn = (bg >> 8) & 0xFF, bb = bg & 0xFF;
  693. unsigned int py, px;
  694. for (py = 0; py < height; py++) {
  695. unsigned char bits = (py < FONT_H) ? glyph[py] : 0;
  696. int yy = y + (int)py;
  697. if (yy < cy || yy >= cy + (int)ch)
  698. continue;
  699. for (px = 0; px < 8; px++) {
  700. int xx = x + (int)px;
  701. int on = (bits & (0x80u >> px)) != 0;
  702. if (xx < cx || xx >= cx + (int)cw)
  703. continue;
  704. if (on)
  705. put_px(s, (unsigned int)xx, (unsigned int)yy,
  706. fr, fgn, fb);
  707. else if (!transparent)
  708. put_px(s, (unsigned int)xx, (unsigned int)yy,
  709. br, bgn, bb);
  710. }
  711. }
  712. }
  713. void usurf_text(struct usurf *s, int x, int y, const char *str,
  714. unsigned int fg,
  715. int cx, int cy, unsigned int cw, unsigned int ch)
  716. {
  717. unsigned int i = 0;
  718. struct ufont_metrics m;
  719. int ascent, pen = 0;
  720. if (!s || !str)
  721. return;
  722. if (!s->font) {
  723. while (str[i]) {
  724. usurf_glyph(s, x + (int)i * 8, y,
  725. (unsigned char)str[i], fg, 0, 1,
  726. FONT_H, cx, cy, cw, ch);
  727. i++;
  728. }
  729. return;
  730. }
  731. ufont_metrics(s->font, s->font_px, &m);
  732. ascent = m.ascent;
  733. while (str[i]) {
  734. const struct ufont_glyph *g =
  735. ufont_glyph(s->font, s->font_px,
  736. (unsigned char)str[i]);
  737. if (g) {
  738. usurf_pixels += (unsigned int)ufont_blit_rgb(
  739. s->px, (int)s->w, (int)s->h, (int)s->pitch,
  740. (int)s->bpp, x + (pen >> 6), y + ascent,
  741. g, fg, cx, cy, cw, ch);
  742. pen += g->advance;
  743. }
  744. i++;
  745. }
  746. }
  747. void usurf_cells(struct usurf *s, const unsigned short *cells,
  748. unsigned int cols, unsigned int rows,
  749. int x0, int y0,
  750. int cx, int cy, unsigned int cw, unsigned int ch)
  751. {
  752. int col0 = (cx - x0) / 8;
  753. int col1 = (cx + (int)cw - x0 + 7) / 8;
  754. int row0 = (cy - y0) / 16;
  755. int row1 = (cy + (int)ch - y0 + 15) / 16;
  756. int row, col;
  757. if (col0 < 0) col0 = 0;
  758. if (row0 < 0) row0 = 0;
  759. if (col1 > (int)cols) col1 = (int)cols;
  760. if (row1 > (int)rows) row1 = (int)rows;
  761. if (col1 <= col0 || row1 <= row0)
  762. return;
  763. for (row = row0; row < row1; row++) {
  764. for (col = col0; col < col1; col++) {
  765. unsigned short cell = cells[(unsigned int)row * cols +
  766. (unsigned int)col];
  767. unsigned char c = (unsigned char)(cell & 0xFF);
  768. unsigned char attr = (unsigned char)(cell >> 8);
  769. unsigned int fg = ((unsigned int)pal_r[attr & 15] << 16) |
  770. ((unsigned int)pal_g[attr & 15] << 8) |
  771. pal_b[attr & 15];
  772. unsigned int bg = ((unsigned int)pal_r[(attr >> 4) & 15] << 16) |
  773. ((unsigned int)pal_g[(attr >> 4) & 15] << 8) |
  774. pal_b[(attr >> 4) & 15];
  775. usurf_glyph(s, x0 + col * 8, y0 + row * 16, c,
  776. fg, bg, 0, 16, cx, cy, cw, ch);
  777. }
  778. }
  779. }
  780. void usurf_canvas(struct usurf *s, const struct uwm_canvas *c,
  781. int x0, int y0,
  782. int cx, int cy, unsigned int cw, unsigned int ch)
  783. {
  784. int y;
  785. int sx0 = 0, sy0 = 0;
  786. int sx1 = (int)c->w, sy1 = (int)c->h;
  787. if (cx > x0)
  788. sx0 = cx - x0;
  789. if (cy > y0)
  790. sy0 = cy - y0;
  791. if (cx + (int)cw < x0 + sx1)
  792. sx1 = cx + (int)cw - x0;
  793. if (cy + (int)ch < y0 + sy1)
  794. sy1 = cy + (int)ch - y0;
  795. if (sx1 <= sx0 || sy1 <= sy0)
  796. return;
  797. usurf_pixels += (unsigned int)(sx1 - sx0) * (unsigned int)(sy1 - sy0);
  798. for (y = sy0; y < sy1; y++) {
  799. const unsigned short *src = c->px + (unsigned int)y * c->w +
  800. (unsigned int)sx0;
  801. unsigned int yy = (unsigned int)(y0 + y);
  802. unsigned int xx = (unsigned int)(x0 + sx0);
  803. unsigned char *row = s->px + (unsigned long)yy * s->pitch;
  804. unsigned int n = (unsigned int)(sx1 - sx0);
  805. unsigned int i;
  806. if (s->bpp == 32) {
  807. unsigned int *d = (unsigned int *)row + xx;
  808. for (i = 0; i < n; i++) {
  809. unsigned int p = src[i];
  810. unsigned int r5 = (p >> 11) & 0x1F;
  811. unsigned int g6 = (p >> 5) & 0x3F;
  812. unsigned int b5 = p & 0x1F;
  813. d[i] = 0xFF000000u |
  814. (((r5 << 3) | (r5 >> 2)) << 16) |
  815. (((g6 << 2) | (g6 >> 4)) << 8) |
  816. ((b5 << 3) | (b5 >> 2));
  817. }
  818. } else if (s->bpp == 16) {
  819. unsigned short *d = (unsigned short *)row + xx;
  820. for (i = 0; i < n; i++)
  821. d[i] = src[i];
  822. } else {
  823. unsigned char *d = row + xx * 3;
  824. for (i = 0; i < n; i++) {
  825. unsigned int p = src[i];
  826. unsigned int r5 = (p >> 11) & 0x1F;
  827. unsigned int g6 = (p >> 5) & 0x3F;
  828. unsigned int b5 = p & 0x1F;
  829. d[0] = (unsigned char)((b5 << 3) | (b5 >> 2));
  830. d[1] = (unsigned char)((g6 << 2) | (g6 >> 4));
  831. d[2] = (unsigned char)((r5 << 3) | (r5 >> 2));
  832. d += 3;
  833. }
  834. }
  835. }
  836. }