Refine drawing API semantics to pass drawing * instead of void *

This changes the drawing API so that implementations receive a
`drawing *` pointer with each call, instead of a `void *` pointer as
they did previously. The `void *` context pointer has been moved to be
a member of the `drawing` structure (which has been made public), from
which it can be retrieved via the new `GET_HANDLE_AS_TYPE()` macro. To
signal this breaking change to downstream front end authors, I've
added a version number to the `drawing_api` struct, which will
hopefully force them to notice.

The motivation for this change is the upcoming introduction of a
draw_polygon_fallback() function, which will use a series of calls to
draw_line() to perform software polygon rasterization on platforms
without a native polygon fill primitive. This function is fairly
large, so I desired that it not be included in the binary
distribution, except on platforms which require it (e.g. my Rockbox
port). One way to achieve this is via link-time optimization (LTO,
a.k.a. "interprocedural optimization"/IPO), so that the code is
unconditionally compiled (preventing bit-rot) but only included in the
linked executable if it is actually referenced from elsewhere.
Practically, this precludes the otherwise straightforward route of
including a run-time check of the `draw_polygon` pointer in the
drawing.c middleware. Instead, Simon recommended that a front end be
able to set its `draw_polygon` field to point to
draw_polygon_fallback(). However, the old drawing API's semantics of
passing a `void *` pointer prevented this from working in practice,
since draw_polygon_fallback(), implemented in middleware, would not be
able to perform any drawing operations without a `drawing *` pointer;
with the new API, this restriction is removed, clearing the way for
that function's introduction.

This is a breaking change for front ends, which must update their
implementations of the drawing API to conform. The migration process
is fairly straightforward: every drawing API function which previously
took a `void *` context pointer should be updated to take a `drawing *`
pointer in its place. Then, where each such function would have
previously casted the `void *` pointer to a meaningful type, they now
instead retrieve the context pointer from the `handle` field of the
`drawing` structure. To make this transition easier, the
`GET_HANDLE_AS_TYPE()` macro is introduced to wrap the context pointer
retrieval (see below for usage).

As an example, an old drawing API function implementation would have
looked like this:

void frontend_draw_func(void *handle, ...)
{
    frontend *fe = (frontend *)handle;
    /* do stuff with fe */
}

After this change, that function would be rewritten as:

void frontend_draw_func(drawing *dr, ...)
{
    frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
    /* do stuff with fe */
}

I have already made these changes to all the in-tree front ends, but
out-of-tree front ends will need to follow the procedure outlined
above.

Simon pointed out that changing the drawing API function pointer
signatures to take `drawing *` instead of `void *` results only in a
compiler warning, not an outright error. Thus, I've introduced a
version field to the beginning of the `drawing_api` struct, which will
cause a compilation error and hopefully force front ends to notice
this. This field should be set to 1 for now. Going forward, it will
provide a clear means of communicating future breaking API changes.
This commit is contained in:
Franklin Wei
2024-08-11 19:28:35 -04:00
committed by Simon Tatham
parent a993fd45eb
commit f37913002a
11 changed files with 459 additions and 342 deletions

View File

@ -253,14 +253,14 @@ void get_random_seed(void **randseed, int *randseedsize)
*randseedsize = sizeof(SYSTEMTIME);
}
static void win_status_bar(void *handle, const char *text)
static void win_status_bar(drawing *dr, const char *text)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
SetWindowText(fe->statusbar, text);
}
static blitter *win_blitter_new(void *handle, int w, int h)
static blitter *win_blitter_new(drawing *dr, int w, int h)
{
blitter *bl = snew(blitter);
@ -272,7 +272,7 @@ static blitter *win_blitter_new(void *handle, int w, int h)
return bl;
}
static void win_blitter_free(void *handle, blitter *bl)
static void win_blitter_free(drawing *dr, blitter *bl)
{
if (bl->bitmap) DeleteObject(bl->bitmap);
sfree(bl);
@ -287,9 +287,9 @@ static void blitter_mkbitmap(frontend *fe, blitter *bl)
/* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
static void win_blitter_save(void *handle, blitter *bl, int x, int y)
static void win_blitter_save(drawing *dr, blitter *bl, int x, int y)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
HDC hdc_win, hdc_blit;
HBITMAP prev_blit;
@ -316,9 +316,9 @@ static void win_blitter_save(void *handle, blitter *bl, int x, int y)
ReleaseDC(fe->hwnd, hdc_win);
}
static void win_blitter_load(void *handle, blitter *bl, int x, int y)
static void win_blitter_load(drawing *dr, blitter *bl, int x, int y)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
HDC hdc_win, hdc_blit;
HBITMAP prev_blit;
@ -463,9 +463,9 @@ static void win_reset_pen(frontend *fe)
DeleteObject(pen);
}
static void win_clip(void *handle, int x, int y, int w, int h)
static void win_clip(drawing *dr, int x, int y, int w, int h)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
POINT p, q;
if (fe->drawstatus == NOTHING)
@ -476,9 +476,9 @@ static void win_clip(void *handle, int x, int y, int w, int h)
IntersectClipRect(fe->hdc, p.x, p.y, q.x, q.y);
}
static void win_unclip(void *handle)
static void win_unclip(drawing *dr)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
if (fe->drawstatus == NOTHING)
return;
@ -486,11 +486,11 @@ static void win_unclip(void *handle)
SelectClipRgn(fe->hdc, NULL);
}
static void win_draw_text(void *handle, int x, int y, int fonttype,
static void win_draw_text(drawing *dr, int x, int y, int fonttype,
int fontsize, int align, int colour,
const char *text)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
POINT xy;
int i;
LOGFONT lf;
@ -566,9 +566,9 @@ static void win_draw_text(void *handle, int x, int y, int fonttype,
}
}
static void win_draw_rect(void *handle, int x, int y, int w, int h, int colour)
static void win_draw_rect(drawing *dr, int x, int y, int w, int h, int colour)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
POINT p, q;
if (fe->drawstatus == NOTHING)
@ -593,9 +593,9 @@ static void win_draw_rect(void *handle, int x, int y, int w, int h, int colour)
}
}
static void win_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
static void win_draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
POINT pp[2];
if (fe->drawstatus == NOTHING)
@ -610,10 +610,10 @@ static void win_draw_line(void *handle, int x1, int y1, int x2, int y2, int colo
win_reset_pen(fe);
}
static void win_draw_circle(void *handle, int cx, int cy, int radius,
static void win_draw_circle(drawing *dr, int cx, int cy, int radius,
int fillcolour, int outlinecolour)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
POINT p, q;
assert(outlinecolour >= 0);
@ -634,10 +634,10 @@ static void win_draw_circle(void *handle, int cx, int cy, int radius,
win_reset_pen(fe);
}
static void win_draw_polygon(void *handle, const int *coords, int npoints,
static void win_draw_polygon(drawing *dr, const int *coords, int npoints,
int fillcolour, int outlinecolour)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
POINT *pts;
int i;
@ -668,9 +668,9 @@ static void win_draw_polygon(void *handle, const int *coords, int npoints,
sfree(pts);
}
static void win_start_draw(void *handle)
static void win_start_draw(drawing *dr)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
HDC hdc_win;
assert(fe->drawstatus == NOTHING);
@ -684,9 +684,9 @@ static void win_start_draw(void *handle)
fe->drawstatus = DRAWING;
}
static void win_draw_update(void *handle, int x, int y, int w, int h)
static void win_draw_update(drawing *dr, int x, int y, int w, int h)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
RECT r;
if (fe->drawstatus != DRAWING)
@ -701,9 +701,9 @@ static void win_draw_update(void *handle, int x, int y, int w, int h)
InvalidateRect(fe->hwnd, &r, false);
}
static void win_end_draw(void *handle)
static void win_end_draw(drawing *dr)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
assert(fe->drawstatus == DRAWING);
SelectObject(fe->hdc, fe->prevbm);
DeleteDC(fe->hdc);
@ -714,9 +714,9 @@ static void win_end_draw(void *handle)
fe->drawstatus = NOTHING;
}
static void win_line_width(void *handle, float width)
static void win_line_width(drawing *dr, float width)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
assert(fe->drawstatus != DRAWING);
if (fe->drawstatus == NOTHING)
@ -725,9 +725,9 @@ static void win_line_width(void *handle, float width)
fe->linewidth = (int)(width * fe->printpixelscale);
}
static void win_line_dotted(void *handle, bool dotted)
static void win_line_dotted(drawing *dr, bool dotted)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
assert(fe->drawstatus != DRAWING);
if (fe->drawstatus == NOTHING)
@ -736,9 +736,9 @@ static void win_line_dotted(void *handle, bool dotted)
fe->linedotted = dotted;
}
static void win_begin_doc(void *handle, int pages)
static void win_begin_doc(drawing *dr, int pages)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
assert(fe->drawstatus != DRAWING);
if (fe->drawstatus == NOTHING)
@ -761,9 +761,9 @@ static void win_begin_doc(void *handle, int pages)
fe->fontstart = fe->nfonts;
}
static void win_begin_page(void *handle, int number)
static void win_begin_page(drawing *dr, int number)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
assert(fe->drawstatus != DRAWING);
if (fe->drawstatus == NOTHING)
@ -778,10 +778,10 @@ static void win_begin_page(void *handle, int number)
}
}
static void win_begin_puzzle(void *handle, float xm, float xc,
static void win_begin_puzzle(drawing *dr, float xm, float xc,
float ym, float yc, int pw, int ph, float wmm)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
int ppw, pph, pox, poy;
float mmpw, mmph, mmox, mmoy;
float scale;
@ -829,14 +829,14 @@ static void win_begin_puzzle(void *handle, float xm, float xc,
fe->linedotted = false;
}
static void win_end_puzzle(void *handle)
static void win_end_puzzle(drawing *dr)
{
/* Nothing needs to be done here. */
}
static void win_end_page(void *handle, int number)
static void win_end_page(drawing *dr, int number)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
assert(fe->drawstatus != DRAWING);
@ -852,9 +852,9 @@ static void win_end_page(void *handle, int number)
}
}
static void win_end_doc(void *handle)
static void win_end_doc(drawing *dr)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
assert(fe->drawstatus != DRAWING);
@ -885,7 +885,7 @@ static void win_end_doc(void *handle)
}
}
char *win_text_fallback(void *handle, const char *const *strings, int nstrings)
char *win_text_fallback(drawing *dr, const char *const *strings, int nstrings)
{
/*
* We assume Windows can cope with any UTF-8 likely to be
@ -895,6 +895,7 @@ char *win_text_fallback(void *handle, const char *const *strings, int nstrings)
}
const struct drawing_api win_drawing = {
1,
win_draw_text,
win_draw_rect,
win_draw_line,