diff --git a/frontends/monkey/browser.h b/frontends/monkey/browser.h --- a/frontends/monkey/browser.h +++ b/frontends/monkey/browser.h @@ -37,1 +37,6 @@ struct gui_window { +#if defined(__montauk__) + int native_id; + uint32_t *pixels; + int clip_x0, clip_y0, clip_x1, clip_y1; +#endif char *host; /* Ignore this, it's in case RING*() gets debugging for fetchers */ diff --git a/frontends/monkey/browser.c b/frontends/monkey/browser.c --- a/frontends/monkey/browser.c +++ b/frontends/monkey/browser.c @@ -21,4 +21,7 @@ #include #include #include +#if defined(__montauk__) +#include +#endif @@ -88,3 +91,14 @@ gui_window_create(struct browser_window *bw, ret->width = 800; ret->height = 600; +#if defined(__montauk__) + mtk_win_result native; + if (mtk_win_create("NetSurf", ret->width, ret->height, &native) != 0) { + free(ret); + return NULL; + } + ret->native_id = native.id; + ret->pixels = (uint32_t *)(uintptr_t)native.pixels; + ret->clip_x1 = ret->width; + ret->clip_y1 = ret->height; +#endif @@ -107,4 +121,7 @@ gui_window_destroy(struct gui_window *g) { moutf(MOUT_WINDOW, "DESTROY WIN %u", g->win_num); +#if defined(__montauk__) + mtk_win_destroy(g->native_id); +#endif RING_REMOVE(gw_ring, g); free(g); @@ -569,6 +586,7 @@ monkey_window_handle_redraw(int argc, char **argv) struct redraw_context ctx = { .interactive = true, .background_images = true, - .plot = monkey_plotters + .plot = monkey_plotters, + .priv = NULL }; @@ -597,7 +615,17 @@ monkey_window_handle_redraw(int argc, char **argv) } NSLOG(netsurf, INFO, "Issue redraw"); +#if defined(__montauk__) + ctx.priv = gw; + for (int y = 0; y < gw->height; y++) { + for (int x = 0; x < gw->width; x++) + gw->pixels[y * gw->width + x] = 0xffffffffu; + } +#endif moutf(MOUT_WINDOW, "REDRAW WIN %d START", atoi(argv[2])); browser_window_redraw(gw->bw, gw->scrollx, gw->scrolly, &clip, &ctx); +#if defined(__montauk__) + mtk_win_present(gw->native_id); +#endif moutf(MOUT_WINDOW, "REDRAW WIN %d STOP", atoi(argv[2])); } diff --git a/frontends/monkey/plot.c b/frontends/monkey/plot.c --- a/frontends/monkey/plot.c +++ b/frontends/monkey/plot.c @@ -23,4 +23,83 @@ #include "netsurf/plotters.h" #include "monkey/output.h" +#include "monkey/browser.h" + +#if defined(__montauk__) +extern const unsigned char mtk_font_data[] __asm__("_ZN3gui9font_dataE"); + +struct bitmap { + void *ptr; + size_t rowstride; + int width; + int height; + bool opaque; +}; + +static uint32_t native_colour(colour c) +{ + if (c == NS_TRANSPARENT) return 0; + return 0xff000000u | ((c & 0x0000ffu) << 16) | + (c & 0x00ff00u) | ((c & 0xff0000u) >> 16); +} + +static void native_pixel(struct gui_window *gw, int x, int y, uint32_t colour) +{ + if (gw == NULL || gw->pixels == NULL || x < gw->clip_x0 || y < gw->clip_y0 || + x >= gw->clip_x1 || y >= gw->clip_y1 || x < 0 || y < 0 || + x >= gw->width || y >= gw->height || colour == 0) return; + gw->pixels[y * gw->width + x] = colour; +} + +static void native_fill(struct gui_window *gw, int x0, int y0, int x1, int y1, + uint32_t colour) +{ + if (gw == NULL || colour == 0) return; + if (x0 < gw->clip_x0) x0 = gw->clip_x0; + if (y0 < gw->clip_y0) y0 = gw->clip_y0; + if (x1 > gw->clip_x1) x1 = gw->clip_x1; + if (y1 > gw->clip_y1) y1 = gw->clip_y1; + x0 = x0 < 0 ? 0 : x0; y0 = y0 < 0 ? 0 : y0; + if (x1 > gw->width) x1 = gw->width; + if (y1 > gw->height) y1 = gw->height; + for (int y = y0; y < y1; y++) + for (int x = x0; x < x1; x++) gw->pixels[y * gw->width + x] = colour; +} + +static void native_line(struct gui_window *gw, int x0, int y0, int x1, int y1, + uint32_t colour) +{ + int dx = x1 > x0 ? x1 - x0 : x0 - x1, sx = x0 < x1 ? 1 : -1; + int dy = -(y1 > y0 ? y1 - y0 : y0 - y1), sy = y0 < y1 ? 1 : -1; + int err = dx + dy; + for (;;) { + native_pixel(gw, x0, y0, colour); + if (x0 == x1 && y0 == y1) break; + int twice = 2 * err; + if (twice >= dy) { err += dy; x0 += sx; } + if (twice <= dx) { err += dx; y0 += sy; } + } +} + +static void native_text(struct gui_window *gw, int x, int baseline, + const char *text, size_t length, const struct plot_font_style *style) +{ + int px = plot_style_fixed_to_int(style->size); + int sx = px > 8 ? px / 8 : 1; + int sy = px > 16 ? px / 16 : 1; + int top = baseline - 13 * sy; + uint32_t colour = native_colour(style->foreground); + for (size_t i = 0; i < length; i++) { + unsigned char ch = (unsigned char)text[i]; + if (ch >= 128) continue; + const unsigned char *glyph = mtk_font_data + ch * 16; + for (int row = 0; row < 16; row++) + for (int col = 0; col < 8; col++) + if (glyph[row] & (0x80u >> col)) + native_fill(gw, x + col * sx, top + row * sy, + x + (col + 1) * sx, top + (row + 1) * sy, colour); + x += px > 0 ? px : 8; + } +} +#endif @@ -37,3 +116,12 @@ monkey_plot_clip(const struct redraw_context *ctx, const struct rect *clip) { +#if defined(__montauk__) + struct gui_window *gw = ctx->priv; + if (gw != NULL) { + gw->clip_x0 = clip->x0; + gw->clip_y0 = clip->y0; + gw->clip_x1 = clip->x1; + gw->clip_y1 = clip->y1; + } +#endif moutf(MOUT_PLOT, "CLIP X0 %d Y0 %d X1 %d Y1 %d", clip->x0, clip->y0, clip->x1, clip->y1); @@ -108,3 +196,6 @@ monkey_plot_line(const struct redraw_context *ctx, { +#if defined(__montauk__) + native_line(ctx->priv, line->x0, line->y0, line->x1, line->y1, native_colour(style->stroke_colour)); +#endif moutf(MOUT_PLOT, "LINE X0 %d Y0 %d X1 %d Y1 %d", line->x0, line->y0, line->x1, line->y1); @@ -132,3 +223,16 @@ monkey_plot_rectangle(const struct redraw_context *ctx, { +#if defined(__montauk__) + struct gui_window *gw = ctx->priv; + if (style->fill_type != PLOT_OP_TYPE_NONE) + native_fill(gw, rect->x0, rect->y0, rect->x1, rect->y1, + native_colour(style->fill_colour)); + if (style->stroke_type != PLOT_OP_TYPE_NONE) { + uint32_t c = native_colour(style->stroke_colour); + native_line(gw, rect->x0, rect->y0, rect->x1 - 1, rect->y0, c); + native_line(gw, rect->x1 - 1, rect->y0, rect->x1 - 1, rect->y1 - 1, c); + native_line(gw, rect->x1 - 1, rect->y1 - 1, rect->x0, rect->y1 - 1, c); + native_line(gw, rect->x0, rect->y1 - 1, rect->x0, rect->y0, c); + } +#endif moutf(MOUT_PLOT, "RECT X0 %d Y0 %d X1 %d Y1 %d", rect->x0, rect->y0, rect->x1, rect->y1); @@ -222,3 +325,33 @@ monkey_plot_bitmap(const struct redraw_context *ctx, { +#if defined(__montauk__) + struct gui_window *gw = ctx->priv; + if (bitmap != NULL && bitmap->ptr != NULL && width > 0 && height > 0) { + uint32_t *src = bitmap->ptr; + for (int dy = 0; dy < height; dy++) { + int sy = (int)((long long)dy * bitmap->height / height); + for (int dx = 0; dx < width; dx++) { + int sx = (int)((long long)dx * bitmap->width / width); + uint32_t p = src[sy * (bitmap->rowstride / 4) + sx]; + unsigned a = p >> 24; + if (a == 0) continue; + /* NetSurf bitmap storage is RGBA in memory, which is this + * little-endian word layout; convert to ARGB framebuffer. */ + uint32_t out = 0xff000000u | ((p & 0xffu) << 16) | + (p & 0xff00u) | ((p >> 16) & 0xffu); + if (a != 255) { + int tx = x + dx, ty = y + dy; + if (tx >= 0 && ty >= 0 && tx < gw->width && ty < gw->height) { + uint32_t d = gw->pixels[ty * gw->width + tx]; + unsigned r = (((out >> 16) & 255) * a + ((d >> 16) & 255) * (255-a)) / 255; + unsigned g = (((out >> 8) & 255) * a + ((d >> 8) & 255) * (255-a)) / 255; + unsigned b = ((out & 255) * a + (d & 255) * (255-a)) / 255; + out = 0xff000000u | (r << 16) | (g << 8) | b; + } + } + native_pixel(gw, x + dx, y + dy, out); + } + } + } +#endif moutf(MOUT_PLOT, "BITMAP X %d Y %d WIDTH %d HEIGHT %d", x, y, width, height); @@ -247,3 +380,6 @@ monkey_plot_text(const struct redraw_context *ctx, { +#if defined(__montauk__) + native_text(ctx->priv, x, y, text, length, fstyle); +#endif moutf(MOUT_PLOT, "TEXT X %d Y %d STR %.*s", x, y, (int)length, text); return NSERROR_OK;