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

@ -54,59 +54,59 @@ void frontend_default_colour(frontend *fe, float *output)
output[0] = output[1]= output[2] = 0.8f;
}
void nestedvm_status_bar(void *handle, const char *text)
void nestedvm_status_bar(drawing *dr, const char *text)
{
_call_java(4,0,(int)text,0);
}
void nestedvm_start_draw(void *handle)
void nestedvm_start_draw(drawing *dr)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
_call_java(5, 0, fe->w, fe->h);
_call_java(4, 1, fe->ox, fe->oy);
}
void nestedvm_clip(void *handle, int x, int y, int w, int h)
void nestedvm_clip(drawing *dr, int x, int y, int w, int h)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
_call_java(5, w, h, 0);
_call_java(4, 3, x + fe->ox, y + fe->oy);
}
void nestedvm_unclip(void *handle)
void nestedvm_unclip(drawing *dr)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
_call_java(4, 4, fe->ox, fe->oy);
}
void nestedvm_draw_text(void *handle, int x, int y, int fonttype, int fontsize,
void nestedvm_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);
_call_java(5, x + fe->ox, y + fe->oy,
(fonttype == FONT_FIXED ? 0x10 : 0x0) | align);
_call_java(7, fontsize, colour, (int)text);
}
void nestedvm_draw_rect(void *handle, int x, int y, int w, int h, int colour)
void nestedvm_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);
_call_java(5, w, h, colour);
_call_java(4, 5, x + fe->ox, y + fe->oy);
}
void nestedvm_draw_line(void *handle, int x1, int y1, int x2, int y2,
void nestedvm_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);
_call_java(5, x2 + fe->ox, y2 + fe->oy, colour);
_call_java(4, 6, x1 + fe->ox, y1 + fe->oy);
}
void nestedvm_draw_poly(void *handle, int *coords, int npoints,
void nestedvm_draw_poly(drawing *dr, int *coords, int npoints,
int fillcolour, int outlinecolour)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
int i;
_call_java(4, 7, npoints, 0);
for (i = 0; i < npoints; i++) {
@ -115,10 +115,10 @@ void nestedvm_draw_poly(void *handle, int *coords, int npoints,
_call_java(4, 8, outlinecolour, fillcolour);
}
void nestedvm_draw_circle(void *handle, int cx, int cy, int radius,
void nestedvm_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);
_call_java(5, cx+fe->ox, cy+fe->oy, radius);
_call_java(4, 9, outlinecolour, fillcolour);
}
@ -127,7 +127,7 @@ struct blitter {
int handle, w, h, x, y;
};
blitter *nestedvm_blitter_new(void *handle, int w, int h)
blitter *nestedvm_blitter_new(drawing *dr, int w, int h)
{
blitter *bl = snew(blitter);
bl->handle = -1;
@ -136,16 +136,16 @@ blitter *nestedvm_blitter_new(void *handle, int w, int h)
return bl;
}
void nestedvm_blitter_free(void *handle, blitter *bl)
void nestedvm_blitter_free(drawing *dr, blitter *bl)
{
if (bl->handle != -1)
_call_java(4, 11, bl->handle, 0);
sfree(bl);
}
void nestedvm_blitter_save(void *handle, blitter *bl, int x, int y)
void nestedvm_blitter_save(drawing *dr, blitter *bl, int x, int y)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
if (bl->handle == -1)
bl->handle = _call_java(4,10,bl->w, bl->h);
bl->x = x;
@ -153,9 +153,9 @@ void nestedvm_blitter_save(void *handle, blitter *bl, int x, int y)
_call_java(8, bl->handle, x + fe->ox, y + fe->oy);
}
void nestedvm_blitter_load(void *handle, blitter *bl, int x, int y)
void nestedvm_blitter_load(drawing *dr, blitter *bl, int x, int y)
{
frontend *fe = (frontend *)handle;
frontend *fe = GET_HANDLE_AS_TYPE(dr, frontend);
assert(bl->handle != -1);
if (x == BLITTER_FROMSAVED && y == BLITTER_FROMSAVED) {
x = bl->x;
@ -164,12 +164,12 @@ void nestedvm_blitter_load(void *handle, blitter *bl, int x, int y)
_call_java(9, bl->handle, x + fe->ox, y + fe->oy);
}
void nestedvm_end_draw(void *handle)
void nestedvm_end_draw(drawing *dr)
{
_call_java(4,2,0,0);
}
char *nestedvm_text_fallback(void *handle, const char *const *strings,
char *nestedvm_text_fallback(drawing *dr, const char *const *strings,
int nstrings)
{
/*
@ -180,6 +180,7 @@ char *nestedvm_text_fallback(void *handle, const char *const *strings,
}
const struct drawing_api nestedvm_drawing = {
1,
nestedvm_draw_text,
nestedvm_draw_rect,
nestedvm_draw_line,