| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928 |
- /* Userland drawing primitives for the ring-3 WM. Mirrors the kernel's
- * fbtext/gfx byte order so the mapped shadow matches the kernel console. */
- #include "uwm_lib.h"
- #include "ucmd_lib.h"
- #include "font_8x16.h"
- #include "ufont.h"
- #define FONT_H 16
- static const unsigned char pal_r[16] = {
- 0, 0, 0, 0, 170, 170, 170, 170, 85, 85, 85, 85, 255, 255, 255, 255
- };
- static const unsigned char pal_g[16] = {
- 0, 0, 0, 170, 0, 0, 85, 170, 85, 85, 255, 255, 85, 85, 255, 255
- };
- static const unsigned char pal_b[16] = {
- 0, 0, 170, 0, 170, 170, 0, 170, 85, 255, 85, 255, 85, 255, 85, 255
- };
- unsigned int usurf_pixels;
- void usurf_init(struct usurf *s, void *px, unsigned int w, unsigned int h,
- unsigned int pitch, unsigned int bpp)
- {
- s->px = (unsigned char *)px;
- s->w = w;
- s->h = h;
- s->pitch = pitch;
- s->bpp = bpp;
- s->font = 0;
- s->font_px = 16;
- }
- /* ---- Adwaita icon masks ------------------------------------------------
- * /ramfs/icons/adwaita16.icn (NBICO1, include/icon_abi.h): one 8-bit
- * coverage mask per icon, tinted and blended over the surface. Loaded once
- * per process into BSS, like ngui's copy, so a page heap reset or a pack
- * swap cannot leave a dangling pointer. */
- #define UWM_ICON_PX 16
- #define UWM_ICON_BYTES (UWM_ICON_PX * UWM_ICON_PX)
- static unsigned char icon_data[sizeof(struct icon_file_hdr) +
- UWM_ICON_COUNT * UWM_ICON_BYTES + 512];
- static int icon_state; /* 0 untried, 1 loaded, -1 unavailable */
- static unsigned int icon_count;
- static void icon_load(void)
- {
- const struct icon_file_hdr *h = (const struct icon_file_hdr *)icon_data;
- unsigned int max = sizeof(icon_data) - 512, have;
- int n, i;
- icon_state = -1;
- /* The FAT path may write a final partial sector past `max`; the
- * buffer keeps 512 bytes of slack for it. */
- n = u_syscall3(SYS_READ_FILE, (int)ICON_BUNDLE_PATH, (int)icon_data,
- (int)max);
- if (n < (int)sizeof(*h))
- return;
- for (i = 0; i < 8; i++)
- if (h->magic[i] != ICON_FILE_MAGIC[i])
- return;
- if (h->size != UWM_ICON_PX)
- return;
- have = ((unsigned int)n - sizeof(*h)) / UWM_ICON_BYTES;
- if (have > h->count)
- have = h->count;
- if (have > UWM_ICON_COUNT)
- have = UWM_ICON_COUNT;
- icon_count = have;
- icon_state = 1;
- }
- static const unsigned char *icon_mask(unsigned int icon)
- {
- if (icon_state == 0)
- icon_load();
- if (icon_state != 1 || icon >= icon_count)
- return 0;
- return icon_data + sizeof(struct icon_file_hdr) +
- icon * UWM_ICON_BYTES;
- }
- /* Tint a coverage mask with `rgb` and blend it over the canvas, clipped to
- * the rectangle (cx,cy,cw,ch). */
- int usurf_icon_clip(struct usurf *s, int x, int y, enum uwm_icon icon,
- unsigned int rgb, int cx, int cy,
- unsigned int cw, unsigned int ch)
- {
- const unsigned char *m = icon_mask((unsigned int)icon);
- unsigned int sr = (rgb >> 16) & 0xFF;
- unsigned int sg = (rgb >> 8) & 0xFF;
- unsigned int sb = rgb & 0xFF;
- int x1 = cx + (int)cw, y1 = cy + (int)ch;
- int i, j;
- if (!s || !s->px || !m)
- return 0;
- for (j = 0; j < UWM_ICON_PX; j++) {
- int py = y + j;
- unsigned char *row;
- if (py < cy || py >= y1 || py < 0 || py >= (int)s->h)
- continue;
- row = s->px + (unsigned long)py * s->pitch;
- for (i = 0; i < UWM_ICON_PX; i++) {
- int px = x + i, a = m[j * UWM_ICON_PX + i];
- unsigned int dr, dg, db;
- if (!a || px < cx || px >= x1 ||
- px < 0 || px >= (int)s->w)
- continue;
- usurf_pixels++;
- if (s->bpp == 16) {
- unsigned short *d = (unsigned short *)row + px;
- unsigned int v = *d;
- dr = ((v >> 11) & 0x1F) << 3;
- dg = ((v >> 5) & 0x3F) << 2;
- db = (v & 0x1F) << 3;
- dr = (dr * (255 - a) + sr * a + 127) / 255;
- dg = (dg * (255 - a) + sg * a + 127) / 255;
- db = (db * (255 - a) + sb * a + 127) / 255;
- *d = (unsigned short)(((dr >> 3) << 11) |
- ((dg >> 2) << 5) |
- (db >> 3));
- } else {
- unsigned char *p = row + px * (s->bpp / 8);
- /* BGR(A) byte order, like put_px(). */
- dr = p[2];
- dg = p[1];
- db = p[0];
- p[0] = (unsigned char)((db * (255 - a) +
- sb * a + 127) / 255);
- p[1] = (unsigned char)((dg * (255 - a) +
- sg * a + 127) / 255);
- p[2] = (unsigned char)((dr * (255 - a) +
- sr * a + 127) / 255);
- if (s->bpp == 32)
- p[3] = 0xFF;
- }
- }
- }
- return 1;
- }
- /* The same without clipping, i.e. clipped to the whole canvas. */
- int usurf_icon(struct usurf *s, int x, int y, enum uwm_icon icon,
- unsigned int rgb)
- {
- if (!s)
- return 0;
- return usurf_icon_clip(s, x, y, icon, rgb, 0, 0, s->w, s->h);
- }
- /* ---- Window drop shadow ------------------------------------------------ */
- #define SHADOW_FALLOFF_N 11
- static const unsigned char shadow_falloff[SHADOW_FALLOFF_N] = {
- 0, 120, 110, 100, 90, 80, 69, 58, 46, 33, 20
- };
- /* Darken one band outside (wx,wy)-(wx1,wy1). The alpha comes from the
- * rounded distance to the window edge (max + 0.4 * min), so the four bands
- * together form a soft ring whose corners fade in a curve instead of a
- * square corner, without touching the window interior. */
- static void shadow_band(struct usurf *s, int x0, int y0, int x1, int y1,
- int wx, int wy, int wx1, int wy1, int spread,
- int bytes)
- {
- int x, y;
- for (y = y0; y < y1; y++) {
- unsigned char *row = s->px + (unsigned long)y * s->pitch;
- int dy = 0;
- if (y < wy)
- dy = wy - y;
- else if (y >= wy1)
- dy = y - wy1 + 1;
- for (x = x0; x < x1; x++) {
- int dx = 0, d, lo, hi;
- unsigned int a, ia;
- unsigned char *p;
- if (x < wx)
- dx = wx - x;
- else if (x >= wx1)
- dx = x - wx1 + 1;
- lo = dx < dy ? dx : dy;
- hi = dx < dy ? dy : dx;
- d = hi + ((lo * 13) >> 5);
- if (d <= 0 || d > spread)
- continue;
- if (d >= SHADOW_FALLOFF_N)
- d = SHADOW_FALLOFF_N - 1;
- a = shadow_falloff[d];
- if (!a)
- continue;
- ia = 256 - a;
- usurf_pixels++;
- if (s->bpp == 16) {
- unsigned short *q = (unsigned short *)row + x;
- unsigned int v = *q;
- unsigned int r = ((((v >> 11) & 0x1F) * ia)
- + 128) >> 8;
- unsigned int g = ((((v >> 5) & 0x3F) * ia)
- + 128) >> 8;
- unsigned int b = (((v & 0x1F) * ia)
- + 128) >> 8;
- *q = (unsigned short)((r << 11) | (g << 5) | b);
- } else {
- p = row + x * bytes;
- p[0] = (unsigned char)((p[0] * ia + 128) >> 8);
- p[1] = (unsigned char)((p[1] * ia + 128) >> 8);
- p[2] = (unsigned char)((p[2] * ia + 128) >> 8);
- if (s->bpp == 32)
- p[3] = 0xFF;
- }
- }
- }
- }
- /* A soft drop shadow around a window rectangle: `spread` pixels outward,
- * strongest next to the window. The window interior is never touched and
- * the whole ring is clipped to (cx,cy,cw,ch); callers pass the current
- * damage rectangle so a partial repaint only darkens pixels it owns. */
- void usurf_window_shadow(struct usurf *s, int wx, int wy, int ww, int wh,
- int spread, int cx, int cy,
- unsigned int cw, unsigned int ch)
- {
- int wx1 = wx + ww, wy1 = wy + wh;
- int x0, y0, x1, y1, ix0, iy0, ix1, iy1;
- int bytes;
- if (!s || !s->px || ww <= 0 || wh <= 0 || spread <= 0)
- return;
- if (s->bpp != 16 && s->bpp != 24 && s->bpp != 32)
- return;
- bytes = (int)(s->bpp / 8);
- x0 = wx - spread;
- y0 = wy - spread;
- x1 = wx1 + spread;
- y1 = wy1 + spread;
- ix0 = cx;
- iy0 = cy;
- ix1 = cx + (int)cw;
- iy1 = cy + (int)ch;
- if (ix0 < 0) ix0 = 0;
- if (iy0 < 0) iy0 = 0;
- if (ix1 > (int)s->w) ix1 = (int)s->w;
- if (iy1 > (int)s->h) iy1 = (int)s->h;
- if (x0 < ix0) x0 = ix0;
- if (y0 < iy0) y0 = iy0;
- if (x1 > ix1) x1 = ix1;
- if (y1 > iy1) y1 = iy1;
- if (x1 <= x0 || y1 <= y0)
- return;
- /* Corners belong to the top and bottom bands. */
- shadow_band(s, x0, y0, x1, wy < y1 ? wy : y1,
- wx, wy, wx1, wy1, spread, bytes);
- shadow_band(s, x0, wy1 > y0 ? wy1 : y0, x1, y1,
- wx, wy, wx1, wy1, spread, bytes);
- {
- int by0 = wy > y0 ? wy : y0;
- int by1 = wy1 < y1 ? wy1 : y1;
- shadow_band(s, x0, by0, wx < x1 ? wx : x1, by1,
- wx, wy, wx1, wy1, spread, bytes);
- shadow_band(s, wx1 > x0 ? wx1 : x0, by0, x1, by1,
- wx, wy, wx1, wy1, spread, bytes);
- }
- }
- /* ---- Full-colour app icons -------------------------------------------- */
- #define UWM_APP_BYTES (APP_ICON_PX * APP_ICON_PX * 3u)
- static unsigned char app_icon_data[sizeof(struct app_icon_file_hdr) +
- APP_ICON_COUNT * UWM_APP_BYTES + 512];
- static int app_icon_state; /* 0 untried, 1 loaded, -1 unavailable */
- static unsigned int app_icon_count;
- static void app_icon_load(void)
- {
- const struct app_icon_file_hdr *h =
- (const struct app_icon_file_hdr *)app_icon_data;
- unsigned int max = sizeof(app_icon_data) - 512, have;
- int n, i;
- app_icon_state = -1;
- /* The FAT path may write a final partial sector past `max`; the
- * buffer keeps 512 bytes of slack for it. */
- n = u_syscall3(SYS_READ_FILE, (int)APP_ICON_BUNDLE_PATH,
- (int)app_icon_data, (int)max);
- if (n < (int)sizeof(*h))
- return;
- for (i = 0; i < 8; i++)
- if (h->magic[i] != APP_ICON_FILE_MAGIC[i])
- return;
- if (h->size != APP_ICON_PX)
- return;
- have = ((unsigned int)n - sizeof(*h)) / UWM_APP_BYTES;
- if (have > h->count)
- have = h->count;
- if (have > APP_ICON_COUNT)
- have = APP_ICON_COUNT;
- app_icon_count = have;
- app_icon_state = 1;
- }
- static const unsigned char *app_icon_src(unsigned int icon)
- {
- if (app_icon_state == 0)
- app_icon_load();
- if (app_icon_state != 1 || icon >= app_icon_count)
- return 0;
- return app_icon_data + sizeof(struct app_icon_file_hdr) +
- icon * UWM_APP_BYTES;
- }
- /* Premultiplied source-over: dst = src + dst * (255 - a) / 255. */
- static void blend_premul_px(struct usurf *s, int x, int y,
- unsigned int r, unsigned int g, unsigned int b,
- unsigned int a)
- {
- unsigned char *row;
- unsigned int dr, dg, db;
- if (!a || x < 0 || y < 0 || (unsigned int)x >= s->w ||
- (unsigned int)y >= s->h)
- return;
- usurf_pixels++;
- row = s->px + (unsigned long)(unsigned int)y * s->pitch;
- if (s->bpp == 16) {
- unsigned short *d = (unsigned short *)row + (unsigned int)x;
- unsigned int v = *d;
- dr = ((v >> 11) & 0x1F) << 3;
- dg = ((v >> 5) & 0x3F) << 2;
- db = (v & 0x1F) << 3;
- if (a == 255) {
- dr = r; dg = g; db = b;
- } else {
- dr = r + (dr * (255 - a) + 127) / 255;
- dg = g + (dg * (255 - a) + 127) / 255;
- db = b + (db * (255 - a) + 127) / 255;
- }
- if (dr > 255) dr = 255;
- if (dg > 255) dg = 255;
- if (db > 255) db = 255;
- *d = (unsigned short)(((dr >> 3) << 11) |
- ((dg >> 2) << 5) | (db >> 3));
- } else {
- unsigned char *p = row + (unsigned int)x * (s->bpp / 8);
- /* BGR(A) byte order, like put_px(). */
- if (a == 255) {
- dr = r; dg = g; db = b;
- } else {
- dr = r + (p[2] * (255 - a) + 127) / 255;
- dg = g + (p[1] * (255 - a) + 127) / 255;
- db = b + (p[0] * (255 - a) + 127) / 255;
- }
- if (dr > 255) dr = 255;
- if (dg > 255) dg = 255;
- if (db > 255) db = 255;
- p[0] = (unsigned char)db;
- p[1] = (unsigned char)dg;
- p[2] = (unsigned char)dr;
- if (s->bpp == 32)
- p[3] = 0xFF;
- }
- }
- static void app_px(const unsigned char *src, int idx,
- unsigned int *r, unsigned int *g, unsigned int *b,
- unsigned int *a)
- {
- const unsigned char *p = src + (unsigned long)idx * 3;
- unsigned int v = (unsigned int)p[0] | ((unsigned int)p[1] << 8);
- unsigned int r5 = (v >> 11) & 0x1F;
- unsigned int g6 = (v >> 5) & 0x3F;
- unsigned int b5 = v & 0x1F;
- *r = (r5 << 3) | (r5 >> 2);
- *g = (g6 << 2) | (g6 >> 4);
- *b = (b5 << 3) | (b5 >> 2);
- *a = p[2];
- }
- int usurf_app_icon(struct usurf *s, int x, int y, int size,
- enum app_icon icon)
- {
- const unsigned char *src;
- int dx, dy;
- if (!s || !s->px || size <= 0 || icon < 0 ||
- (unsigned int)icon >= APP_ICON_COUNT)
- return 0;
- src = app_icon_src((unsigned int)icon);
- if (!src)
- return 0;
- for (dy = 0; dy < size; dy++) {
- int py = y + dy;
- if (py < 0 || py >= (int)s->h)
- continue;
- for (dx = 0; dx < size; dx++) {
- int px = x + dx;
- unsigned int r, g, b, a;
- if (px < 0 || px >= (int)s->w)
- continue;
- if (size == APP_ICON_PX) {
- app_px(src, dy * APP_ICON_PX + dx,
- &r, &g, &b, &a);
- } else if (size * 2 <= APP_ICON_PX) {
- /* Downscale: average a 2x2 or 4x4 grid over the
- * destination pixel's source box. This keeps the
- * 48 px dock and 18 px menu icons clean. */
- int taps = APP_ICON_PX >= size * 4 ? 4 : 2;
- int sx0 = dx * APP_ICON_PX / size;
- int sy0 = dy * APP_ICON_PX / size;
- int sx1 = ((dx + 1) * APP_ICON_PX + size - 1) /
- size;
- int sy1 = ((dy + 1) * APP_ICON_PX + size - 1) /
- size;
- unsigned int sr = 0, sg = 0, sb = 0, sa = 0;
- int tx, ty, n = 0, den = taps * 2;
- for (ty = 0; ty < taps; ty++) {
- int sy = (sy0 * den +
- (sy1 - sy0) *
- (2 * ty + 1)) / den;
- if (sy >= APP_ICON_PX)
- sy = APP_ICON_PX - 1;
- for (tx = 0; tx < taps; tx++) {
- int sx = (sx0 * den +
- (sx1 - sx0) *
- (2 * tx + 1)) / den;
- if (sx >= APP_ICON_PX)
- sx = APP_ICON_PX - 1;
- app_px(src,
- sy * APP_ICON_PX + sx,
- &r, &g, &b, &a);
- sr += r; sg += g;
- sb += b; sa += a;
- n++;
- }
- }
- r = (sr + (unsigned int)n / 2) /
- (unsigned int)n;
- g = (sg + (unsigned int)n / 2) /
- (unsigned int)n;
- b = (sb + (unsigned int)n / 2) /
- (unsigned int)n;
- a = (sa + (unsigned int)n / 2) /
- (unsigned int)n;
- } else {
- /* Upscale (or a small downscale): bilinear
- * sampling around the destination centre. */
- int sx25 = ((2 * dx + 1) * APP_ICON_PX * 256) /
- (2 * size) - 128;
- int sy25 = ((2 * dy + 1) * APP_ICON_PX * 256) /
- (2 * size) - 128;
- int x0, y0, fx, fy;
- unsigned int r00, g00, b00, a00;
- unsigned int r10, g10, b10, a10;
- unsigned int r01, g01, b01, a01;
- unsigned int r11, g11, b11, a11;
- if (sx25 < 0) sx25 = 0;
- if (sy25 < 0) sy25 = 0;
- x0 = sx25 >> 8;
- y0 = sy25 >> 8;
- fx = sx25 & 255;
- fy = sy25 & 255;
- if (x0 >= APP_ICON_PX - 1) {
- x0 = APP_ICON_PX - 2;
- fx = 255;
- }
- if (y0 >= APP_ICON_PX - 1) {
- y0 = APP_ICON_PX - 2;
- fy = 255;
- }
- app_px(src, y0 * APP_ICON_PX + x0,
- &r00, &g00, &b00, &a00);
- app_px(src, y0 * APP_ICON_PX + x0 + 1,
- &r10, &g10, &b10, &a10);
- app_px(src, (y0 + 1) * APP_ICON_PX + x0,
- &r01, &g01, &b01, &a01);
- app_px(src, (y0 + 1) * APP_ICON_PX + x0 + 1,
- &r11, &g11, &b11, &a11);
- r = (r00 * (256 - fx) * (256 - fy) +
- r10 * fx * (256 - fy) +
- r01 * (256 - fx) * fy +
- r11 * fx * fy) >> 16;
- g = (g00 * (256 - fx) * (256 - fy) +
- g10 * fx * (256 - fy) +
- g01 * (256 - fx) * fy +
- g11 * fx * fy) >> 16;
- b = (b00 * (256 - fx) * (256 - fy) +
- b10 * fx * (256 - fy) +
- b01 * (256 - fx) * fy +
- b11 * fx * fy) >> 16;
- a = (a00 * (256 - fx) * (256 - fy) +
- a10 * fx * (256 - fy) +
- a01 * (256 - fx) * fy +
- a11 * fx * fy) >> 16;
- }
- blend_premul_px(s, px, py, r, g, b, a);
- }
- }
- return 1;
- }
- void usurf_fill_round_alpha(struct usurf *s, int x, int y, int w, int h,
- int radius, unsigned int rgb,
- unsigned int alpha)
- {
- unsigned int sr = (rgb >> 16) & 0xFF;
- unsigned int sg = (rgb >> 8) & 0xFF;
- unsigned int sb = rgb & 0xFF;
- unsigned int pr, pg, pb;
- int x0 = x, y0 = y, x1 = x + w, y1 = y + h;
- int i, j;
- if (!s || !s->px || alpha == 0 || w <= 0 || h <= 0)
- return;
- if (x0 < 0) x0 = 0;
- if (y0 < 0) y0 = 0;
- if (x1 > (int)s->w) x1 = (int)s->w;
- if (y1 > (int)s->h) y1 = (int)s->h;
- if (x1 <= x0 || y1 <= y0)
- return;
- if (radius < 0)
- radius = 0;
- if (radius > w / 2)
- radius = w / 2;
- if (radius > h / 2)
- radius = h / 2;
- pr = (sr * alpha + 127) / 255;
- pg = (sg * alpha + 127) / 255;
- pb = (sb * alpha + 127) / 255;
- for (j = y0; j < y1; j++) {
- int cy = y + radius;
- if (j >= y + h - radius)
- cy = y + h - radius - 1;
- for (i = x0; i < x1; i++) {
- int cx = x + radius;
- if (i >= x + w - radius)
- cx = x + w - radius - 1;
- if (radius && (i < x + radius ||
- i >= x + w - radius) &&
- (j < y + radius || j >= y + h - radius)) {
- int ddx = i - cx, ddy = j - cy;
- if (ddx * ddx + ddy * ddy > radius * radius)
- continue;
- }
- blend_premul_px(s, i, j, pr, pg, pb, alpha);
- }
- }
- }
- void usurf_font_attach(struct usurf *s, const char *path, int px)
- {
- if (!s)
- return;
- s->font_px = px;
- s->font = ufont_face_open_path(path);
- }
- unsigned int usurf_text_width(struct usurf *s, const char *str)
- {
- unsigned int n = 0;
- if (!str)
- return 0;
- if (s && s->font)
- return (unsigned int)ufont_text_width(s->font, s->font_px,
- str, -1, 0);
- while (str[n])
- n++;
- return n * 8;
- }
- static void put_px(struct usurf *s, unsigned int x, unsigned int y,
- unsigned int r, unsigned int g, unsigned int b)
- {
- unsigned char *p;
- if (x >= s->w || y >= s->h)
- return;
- usurf_pixels++;
- p = s->px + (unsigned long)y * s->pitch;
- if (s->bpp == 16) {
- unsigned int v = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
- unsigned short *d = (unsigned short *)p;
- d[x] = (unsigned short)v;
- } else {
- p += x * (s->bpp / 8);
- p[0] = (unsigned char)b;
- p[1] = (unsigned char)g;
- p[2] = (unsigned char)r;
- if (s->bpp == 32)
- p[3] = 0xFF;
- }
- }
- void usurf_fill(struct usurf *s, int x, int y, unsigned int w,
- unsigned int h, unsigned int rgb)
- {
- unsigned int r = (rgb >> 16) & 0xFF;
- unsigned int g = (rgb >> 8) & 0xFF;
- unsigned int b = rgb & 0xFF;
- unsigned int i, j;
- if (x < 0) {
- w -= (unsigned int)(-x);
- x = 0;
- }
- if (y < 0) {
- h -= (unsigned int)(-y);
- y = 0;
- }
- if ((unsigned int)x >= s->w || (unsigned int)y >= s->h)
- return;
- if ((unsigned int)x + w > s->w)
- w = s->w - (unsigned int)x;
- if ((unsigned int)y + h > s->h)
- h = s->h - (unsigned int)y;
- usurf_pixels += w * h;
- if (s->bpp == 32) {
- unsigned int v = 0xFF000000u | (r << 16) | (g << 8) | b;
- for (j = 0; j < h; j++) {
- unsigned int *p = (unsigned int *)(s->px +
- (unsigned long)((unsigned int)y + j) * s->pitch) +
- (unsigned int)x;
- unsigned int n = w;
- __asm__ volatile ("cld; rep stosl"
- : "+D"(p), "+c"(n)
- : "a"(v)
- : "memory");
- }
- } else if (s->bpp == 16) {
- unsigned short v = (unsigned short)(((r >> 3) << 11) |
- ((g >> 2) << 5) | (b >> 3));
- for (j = 0; j < h; j++) {
- unsigned short *p = (unsigned short *)(s->px +
- (unsigned long)((unsigned int)y + j) * s->pitch) +
- (unsigned int)x;
- unsigned int n = w;
- __asm__ volatile ("cld; rep stosw"
- : "+D"(p), "+c"(n)
- : "a"(v)
- : "memory");
- }
- } else {
- for (j = 0; j < h; j++) {
- unsigned char *p = s->px +
- (unsigned long)((unsigned int)y + j) * s->pitch +
- (unsigned int)x * 3;
- for (i = 0; i < w; i++) {
- p[0] = (unsigned char)b;
- p[1] = (unsigned char)g;
- p[2] = (unsigned char)r;
- p += 3;
- }
- }
- }
- }
- void usurf_copy(struct usurf *s, int dx, int dy, int sx, int sy,
- unsigned int w, unsigned int h)
- {
- unsigned int pb = s->bpp / 8;
- int backwards;
- if (w == 0 || h == 0 || dx < 0 || dy < 0 || sx < 0 || sy < 0)
- return;
- if ((unsigned int)dx + w > s->w || (unsigned int)sx + w > s->w ||
- (unsigned int)dy + h > s->h || (unsigned int)sy + h > s->h)
- return;
- usurf_pixels += w * h;
- backwards = (dy > sy) || (dy == sy && dx > sx);
- {
- unsigned int i;
- for (i = 0; i < h; i++) {
- unsigned int syy = backwards ? (unsigned int)(sy + (int)h - 1 - (int)i)
- : (unsigned int)sy + i;
- unsigned int dyy = backwards ? (unsigned int)(dy + (int)h - 1 - (int)i)
- : (unsigned int)dy + i;
- unsigned char *src = s->px + (unsigned long)syy * s->pitch +
- (unsigned int)sx * pb;
- unsigned char *dst = s->px + (unsigned long)dyy * s->pitch +
- (unsigned int)dx * pb;
- unsigned int n = w;
- if (pb == 4) {
- unsigned int *d = (unsigned int *)dst;
- const unsigned int *s32 = (const unsigned int *)src;
- if (backwards) {
- while (n--)
- d[n] = s32[n];
- } else {
- __asm__ volatile ("cld; rep movsl"
- : "+D"(d), "+c"(n), "+S"(s32)
- :
- : "memory");
- }
- } else if (pb == 2) {
- unsigned short *d = (unsigned short *)dst;
- const unsigned short *s16 = (const unsigned short *)src;
- if (backwards) {
- while (n--)
- d[n] = s16[n];
- } else {
- __asm__ volatile ("cld; rep movsw"
- : "+D"(d), "+c"(n), "+S"(s16)
- :
- : "memory");
- }
- } else {
- unsigned int k = w * 3;
- if (backwards) {
- while (k--)
- dst[k] = src[k];
- } else {
- __asm__ volatile ("cld; rep movsb"
- : "+D"(dst), "+c"(k), "+S"(src)
- :
- : "memory");
- }
- }
- }
- }
- }
- void usurf_glyph(struct usurf *s, int x, int y, unsigned char c,
- unsigned int fg, unsigned int bg, int transparent,
- unsigned int height,
- int cx, int cy, unsigned int cw, unsigned int ch)
- {
- const unsigned char *glyph = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
- unsigned int fr = (fg >> 16) & 0xFF, fgn = (fg >> 8) & 0xFF, fb = fg & 0xFF;
- unsigned int br = (bg >> 16) & 0xFF, bgn = (bg >> 8) & 0xFF, bb = bg & 0xFF;
- unsigned int py, px;
- for (py = 0; py < height; py++) {
- unsigned char bits = (py < FONT_H) ? glyph[py] : 0;
- int yy = y + (int)py;
- if (yy < cy || yy >= cy + (int)ch)
- continue;
- for (px = 0; px < 8; px++) {
- int xx = x + (int)px;
- int on = (bits & (0x80u >> px)) != 0;
- if (xx < cx || xx >= cx + (int)cw)
- continue;
- if (on)
- put_px(s, (unsigned int)xx, (unsigned int)yy,
- fr, fgn, fb);
- else if (!transparent)
- put_px(s, (unsigned int)xx, (unsigned int)yy,
- br, bgn, bb);
- }
- }
- }
- void usurf_text(struct usurf *s, int x, int y, const char *str,
- unsigned int fg,
- int cx, int cy, unsigned int cw, unsigned int ch)
- {
- unsigned int i = 0;
- struct ufont_metrics m;
- int ascent, pen = 0;
- if (!s || !str)
- return;
- if (!s->font) {
- while (str[i]) {
- usurf_glyph(s, x + (int)i * 8, y,
- (unsigned char)str[i], fg, 0, 1,
- FONT_H, cx, cy, cw, ch);
- i++;
- }
- return;
- }
- ufont_metrics(s->font, s->font_px, &m);
- ascent = m.ascent;
- while (str[i]) {
- const struct ufont_glyph *g =
- ufont_glyph(s->font, s->font_px,
- (unsigned char)str[i]);
- if (g) {
- usurf_pixels += (unsigned int)ufont_blit_rgb(
- s->px, (int)s->w, (int)s->h, (int)s->pitch,
- (int)s->bpp, x + (pen >> 6), y + ascent,
- g, fg, cx, cy, cw, ch);
- pen += g->advance;
- }
- i++;
- }
- }
- void usurf_cells(struct usurf *s, const unsigned short *cells,
- unsigned int cols, unsigned int rows,
- int x0, int y0,
- int cx, int cy, unsigned int cw, unsigned int ch)
- {
- int col0 = (cx - x0) / 8;
- int col1 = (cx + (int)cw - x0 + 7) / 8;
- int row0 = (cy - y0) / 16;
- int row1 = (cy + (int)ch - y0 + 15) / 16;
- int row, col;
- if (col0 < 0) col0 = 0;
- if (row0 < 0) row0 = 0;
- if (col1 > (int)cols) col1 = (int)cols;
- if (row1 > (int)rows) row1 = (int)rows;
- if (col1 <= col0 || row1 <= row0)
- return;
- for (row = row0; row < row1; row++) {
- for (col = col0; col < col1; col++) {
- unsigned short cell = cells[(unsigned int)row * cols +
- (unsigned int)col];
- unsigned char c = (unsigned char)(cell & 0xFF);
- unsigned char attr = (unsigned char)(cell >> 8);
- unsigned int fg = ((unsigned int)pal_r[attr & 15] << 16) |
- ((unsigned int)pal_g[attr & 15] << 8) |
- pal_b[attr & 15];
- unsigned int bg = ((unsigned int)pal_r[(attr >> 4) & 15] << 16) |
- ((unsigned int)pal_g[(attr >> 4) & 15] << 8) |
- pal_b[(attr >> 4) & 15];
- usurf_glyph(s, x0 + col * 8, y0 + row * 16, c,
- fg, bg, 0, 16, cx, cy, cw, ch);
- }
- }
- }
- void usurf_canvas(struct usurf *s, const struct uwm_canvas *c,
- int x0, int y0,
- int cx, int cy, unsigned int cw, unsigned int ch)
- {
- int y;
- int sx0 = 0, sy0 = 0;
- int sx1 = (int)c->w, sy1 = (int)c->h;
- if (cx > x0)
- sx0 = cx - x0;
- if (cy > y0)
- sy0 = cy - y0;
- if (cx + (int)cw < x0 + sx1)
- sx1 = cx + (int)cw - x0;
- if (cy + (int)ch < y0 + sy1)
- sy1 = cy + (int)ch - y0;
- if (sx1 <= sx0 || sy1 <= sy0)
- return;
- usurf_pixels += (unsigned int)(sx1 - sx0) * (unsigned int)(sy1 - sy0);
- for (y = sy0; y < sy1; y++) {
- const unsigned short *src = c->px + (unsigned int)y * c->w +
- (unsigned int)sx0;
- unsigned int yy = (unsigned int)(y0 + y);
- unsigned int xx = (unsigned int)(x0 + sx0);
- unsigned char *row = s->px + (unsigned long)yy * s->pitch;
- unsigned int n = (unsigned int)(sx1 - sx0);
- unsigned int i;
- if (s->bpp == 32) {
- unsigned int *d = (unsigned int *)row + xx;
- for (i = 0; i < n; i++) {
- unsigned int p = src[i];
- unsigned int r5 = (p >> 11) & 0x1F;
- unsigned int g6 = (p >> 5) & 0x3F;
- unsigned int b5 = p & 0x1F;
- d[i] = 0xFF000000u |
- (((r5 << 3) | (r5 >> 2)) << 16) |
- (((g6 << 2) | (g6 >> 4)) << 8) |
- ((b5 << 3) | (b5 >> 2));
- }
- } else if (s->bpp == 16) {
- unsigned short *d = (unsigned short *)row + xx;
- for (i = 0; i < n; i++)
- d[i] = src[i];
- } else {
- unsigned char *d = row + xx * 3;
- for (i = 0; i < n; i++) {
- unsigned int p = src[i];
- unsigned int r5 = (p >> 11) & 0x1F;
- unsigned int g6 = (p >> 5) & 0x3F;
- unsigned int b5 = p & 0x1F;
- d[0] = (unsigned char)((b5 << 3) | (b5 >> 2));
- d[1] = (unsigned char)((g6 << 2) | (g6 >> 4));
- d[2] = (unsigned char)((r5 << 3) | (r5 >> 2));
- d += 3;
- }
- }
- }
- }
|