finish refactor, zero allocs to make valgrind happy

This commit is contained in:
Caleb Connolly 2023-02-20 01:25:37 +00:00
parent 7b714e433e
commit 4a98f0a0cf
No known key found for this signature in database
GPG key ID: 0583312B195F64B6
5 changed files with 187 additions and 109 deletions

View file

@ -182,7 +182,7 @@ NSVGpath* nsvgDuplicatePath(NSVGpath* p);
// Deletes an image. // Deletes an image.
void nsvgDelete(NSVGimage* image); void nsvgDelete(NSVGimage* image);
NSVGshape** nsvgGetTextShapes(NSVGimage* image, const char* text, int textLen); NSVGshape** nsvgGetTextShapes(const NSVGimage* image, const char* text, int textLen);
#ifndef NANOSVG_CPLUSPLUS #ifndef NANOSVG_CPLUSPLUS
#ifdef __cplusplus #ifdef __cplusplus
@ -2963,7 +2963,7 @@ static void nsvg__scaleToViewbox(NSVGparser* p, const char* units)
} }
} }
NSVGshape** nsvgGetTextShapes(NSVGimage* image, const char* text, int textLen) NSVGshape** nsvgGetTextShapes(const NSVGimage* image, const char* text, int textLen)
{ {
NSVGshape *shape = NULL; NSVGshape *shape = NULL;
NSVGshape **ret = calloc(textLen, sizeof(shape)); // array of paths, text to render NSVGshape **ret = calloc(textLen, sizeof(shape)); // array of paths, text to render

View file

@ -67,7 +67,7 @@ void nsvgDeleteRasterizer(NSVGrasterizer*);
void nsvgRasterizeText(NSVGrasterizer* r, void nsvgRasterizeText(NSVGrasterizer* r,
NSVGimage* font, float tx, float ty, float scale, const NSVGimage* font, float tx, float ty, float scale,
unsigned char* dst, int w, int h, int stride, unsigned char* dst, int w, int h, int stride,
const char* text); const char* text);
@ -1369,7 +1369,7 @@ static void dumpEdges(NSVGrasterizer* r, const char* name)
*/ */
void nsvgRasterizeText(NSVGrasterizer* r, void nsvgRasterizeText(NSVGrasterizer* r,
NSVGimage* font, float tx, float ty, float scale, const NSVGimage* font, float tx, float ty, float scale,
unsigned char* dst, int w, int h, int stride, unsigned char* dst, int w, int h, int stride,
const char* text) const char* text)
{ {

View file

@ -1,6 +1,8 @@
#ifndef __pbsplash_h__ #ifndef __pbsplash_h__
#define __pbsplash_h__ #define __pbsplash_h__
#define MM_TO_PX(dpi, mm) (dpi / 25.4) * (mm)
struct col { struct col {
union { union {
unsigned int rgba; unsigned int rgba;

View file

@ -7,18 +7,16 @@
struct col color = { .r = 255, .g = 255, .b = 255, .a = 255 }; struct col color = { .r = 255, .g = 255, .b = 255, .a = 255 };
#define PI 3.1415926535897932384626433832795 #define PI 3.1415926535897932384626433832795
#define n_circles 3 #define n_circles 3
#define speed 5 #define speed 5
void circles_wave(int frame, int w, int y_off, long dpi) static void circles_wave(int frame, int w, int y_off, long dpi)
{ {
unsigned int t_col = tfb_make_color(color.r, color.g, color.b); unsigned int t_col = tfb_make_color(color.r, color.g, color.b);
int f = frame * speed; int f = frame * speed;
int rad = (int)(dpi * 3 / 96.0); int rad = MM_TO_PX(dpi, 1);
int dist = rad * 3; int dist = rad * 3.5;
int amplitude = rad * 1; int amplitude = rad * 1;
int left = ((float)w / 2) - (dist * (n_circles - 1) / 2.0); int left = ((float)w / 2) - (dist * (n_circles - 1) / 2.0);
@ -30,9 +28,6 @@ void circles_wave(int frame, int w, int y_off, long dpi)
rad * 2 + 6, amplitude * 2 + rad * 2 + 6, rad * 2 + 6, amplitude * 2 + rad * 2 + 6,
tfb_black); tfb_black);
tfb_fill_circle(x, y, rad, t_col); tfb_fill_circle(x, y, rad, t_col);
// tfb_draw_circle(x, y, rad, t_col);
// tfb_draw_circle(x, y, rad+1, t_col);
// tfb_draw_circle(x, y, rad+2, t_col);
} }
} }

View file

@ -24,13 +24,12 @@
#define LOGO_SIZE_MAX_MM 40 #define LOGO_SIZE_MAX_MM 40
#define FONT_SIZE_PT 9 #define FONT_SIZE_PT 9
#define FONT_SIZE_B_PT 6 #define FONT_SIZE_B_PT 6
#define B_MESSAGE_OFFSET_MM 3
#define PT_TO_MM 0.38f #define PT_TO_MM 0.38f
#define TTY_PATH_LEN 11 #define TTY_PATH_LEN 11
#define DEBUGRENDER 0 #define DEBUGRENDER 0
#define MM_TO_PX(dpi, mm) (dpi / 25.4) * (mm)
volatile sig_atomic_t terminate = 0; volatile sig_atomic_t terminate = 0;
bool debug = false; bool debug = false;
@ -38,13 +37,15 @@ struct col background_color = { .r = 0, .g = 0, .b = 0, .a = 255 };
static int screenWidth, screenHeight; static int screenWidth, screenHeight;
#define zalloc(size) calloc(1, size)
#define LOG(fmt, ...) \ #define LOG(fmt, ...) \
do { \ do { \
if (debug) \ if (debug) \
printf(fmt, ##__VA_ARGS__); \ printf(fmt, ##__VA_ARGS__); \
} while (0) } while (0)
int usage() static int usage()
{ {
// clang-format off // clang-format off
fprintf(stderr, "pbsplash: postmarketOS bootsplash generator\n"); fprintf(stderr, "pbsplash: postmarketOS bootsplash generator\n");
@ -68,7 +69,7 @@ int usage()
return 1; return 1;
} }
void term(int signum) static void term(int signum)
{ {
terminate = 1; terminate = 1;
} }
@ -125,9 +126,9 @@ static void blit_buf(unsigned char *buf, int x, int y, int w, int h, bool vflip,
static void draw_svg(NSVGimage *image, int x, int y, int w, int h) static void draw_svg(NSVGimage *image, int x, int y, int w, int h)
{ {
float sz = (int)((float)w / (float)image->width * 100.f) / 100.f; float sz = (int)((float)w / (float)image->width * 100.f) / 100.f;
LOG("draw_svg: %dx%d, %dx%d, %f\n", x, y, w, h, sz); LOG("draw_svg: (%d, %d), %dx%d, %f\n", x, y, w, h, sz);
NSVGrasterizer *rast = nsvgCreateRasterizer(); NSVGrasterizer *rast = nsvgCreateRasterizer();
unsigned char *img = malloc(w * h * 4); unsigned char *img = zalloc(w * h * 4);
nsvgRasterize(rast, image, 0, 0, sz, img, w, h, w * 4); nsvgRasterize(rast, image, 0, 0, sz, img, w, h, w * 4);
blit_buf(img, x, y, w, h, false, false); blit_buf(img, x, y, w, h, false, false);
@ -136,13 +137,13 @@ static void draw_svg(NSVGimage *image, int x, int y, int w, int h)
nsvgDeleteRasterizer(rast); nsvgDeleteRasterizer(rast);
} }
static void draw_text(NSVGimage *font, const char *text, int x, int y, int width, static void draw_text(const NSVGimage *font, const char *text, int x, int y, int width,
int height, float scale, unsigned int tfb_col) int height, float scale, unsigned int tfb_col)
{ {
LOG("text '%s': fontsz=%f, x=%d, y=%d, dimensions: %d x %d\n", text, LOG("text '%s': fontsz=%f, x=%d, y=%d, dimensions: %d x %d\n", text,
scale, x, y, width, height); scale, x, y, width, height);
NSVGshape **shapes = nsvgGetTextShapes(font, text, strlen(text)); NSVGshape **shapes = nsvgGetTextShapes(font, text, strlen(text));
unsigned char *img = malloc(width * height * 4); unsigned char *img = zalloc(width * height * 4);
NSVGrasterizer *rast = nsvgCreateRasterizer(); NSVGrasterizer *rast = nsvgCreateRasterizer();
nsvgRasterizeText(rast, font, 0, 0, scale, img, width, height, nsvgRasterizeText(rast, font, 0, 0, scale, img, width, height,
@ -159,7 +160,7 @@ static void draw_text(NSVGimage *font, const char *text, int x, int y, int width
* Get the dimensions of a string in pixels. * Get the dimensions of a string in pixels.
* based on the font size and the font SVG file. * based on the font size and the font SVG file.
*/ */
static const char *getTextDimensions(NSVGimage *font, const char *text, float scale, static const char *getTextDimensions(const NSVGimage *font, const char *text, float scale,
int *width, int *height) int *width, int *height)
{ {
int i; int i;
@ -169,7 +170,7 @@ static const char *getTextDimensions(NSVGimage *font, const char *text, float sc
if (text == NULL) if (text == NULL)
return text; return text;
char *out_text = malloc(strlen(text)); char *out_text = zalloc(strlen(text) + 1);
*width = font->defaultHorizAdv * scale; *width = font->defaultHorizAdv * scale;
// The height is simply the height of the font * the scale factor // The height is simply the height of the font * the scale factor
@ -211,8 +212,6 @@ struct dpi_info {
int pixels_per_milli; int pixels_per_milli;
float logo_size_px; float logo_size_px;
int logo_size_max_mm; int logo_size_max_mm;
int font_size;
int font_size_b; // Bottom text font size
}; };
static void calculate_dpi_info(struct dpi_info *dpi_info) static void calculate_dpi_info(struct dpi_info *dpi_info)
@ -268,54 +267,123 @@ static void calculate_dpi_info(struct dpi_info *dpi_info)
screenHeight, w_mm, h_mm, dpi_info->dpi, dpi_info->logo_size_px); screenHeight, w_mm, h_mm, dpi_info->dpi, dpi_info->logo_size_px);
} }
int show_messages(const struct dpi_info *dpi_info, const char *font_path, const char *message, const char *message_bottom) struct msg_info {
{ const char *src_message;
int textWidth, textHeight, bottomTextHeight = MM_TO_PX(dpi_info->dpi, 6); const char *message;
int bottomTextOffset = MM_TO_PX(dpi_info->dpi, 3); int x;
NSVGimage *font = NULL; int y;
int width;
int height;
float fontsz; float fontsz;
int tx, ty; };
font = nsvgParseFromFile(font_path, "px", 512); static void load_message(struct msg_info *msg_info, const struct dpi_info *dpi_info, float font_size_pt, const NSVGimage *font)
if (!font || !font->shapes) { {
msg_info->fontsz = (font_size_pt * PT_TO_MM) /
(font->fontAscent - font->fontDescent) *
dpi_info->pixels_per_milli;
msg_info->message = getTextDimensions(font, msg_info->src_message, msg_info->fontsz, &msg_info->width, &msg_info->height);
msg_info->x = (screenWidth - msg_info->width) / 2;
// Y coordinate is set later
}
static void free_message(struct msg_info *msg_info)
{
if (!msg_info)
return;
if (msg_info->message && msg_info->message != msg_info->src_message)
free((void *)msg_info->message);
}
struct messages {
const char *font_path;
NSVGimage *font;
int font_size_pt;
int font_size_b_pt;
struct msg_info *msg;
struct msg_info *bottom_msg;
};
static inline void show_message(const struct msg_info *msg_info, const NSVGimage *font)
{
draw_text(font, msg_info->message, msg_info->x, msg_info->y, msg_info->width,
msg_info->height, msg_info->fontsz, tfb_gray);
}
static void show_messages(struct messages *msgs, const struct dpi_info *dpi_info)
{
static bool font_failed = false;
if (font_failed)
return;
if (!msgs->font)
msgs->font = nsvgParseFromFile(msgs->font_path, "px", 512);
if (!msgs->font || !msgs->font->shapes) {
font_failed = true;
fprintf(stderr, "failed to load SVG font, can't render messages\n"); fprintf(stderr, "failed to load SVG font, can't render messages\n");
fprintf(stderr, " font_path: %s\n", msgs->font_path);
fprintf(stderr, "msg: %s\n\nbottom_message: %s\n", msgs->msg->src_message, msgs->bottom_msg->src_message);
return;
}
if (msgs->bottom_msg) {
if (!msgs->bottom_msg->message) {
load_message(msgs->bottom_msg, dpi_info, msgs->font_size_b_pt, msgs->font);
msgs->bottom_msg->y = screenHeight - msgs->bottom_msg->height - MM_TO_PX(dpi_info->dpi, B_MESSAGE_OFFSET_MM);
}
show_message(msgs->bottom_msg, msgs->font);
}
if (msgs->msg) {
if (!msgs->msg->message) {
load_message(msgs->msg, dpi_info, msgs->font_size_pt, msgs->font);
if (msgs->bottom_msg)
msgs->msg->y = msgs->bottom_msg->y - msgs->msg->height - (MM_TO_PX(dpi_info->dpi, msgs->font_size_b_pt * PT_TO_MM) * 0.6);
else
msgs->msg->y = screenHeight - msgs->msg->height - (MM_TO_PX(dpi_info->dpi, msgs->font_size_pt * PT_TO_MM) * 2);
}
show_message(msgs->msg, msgs->font);
}
}
struct image_info {
const char *path;
NSVGimage *image;
float width;
float height;
float x;
float y;
};
static int load_image(const struct dpi_info *dpi_info, struct image_info *image_info)
{
int logo_size_px = dpi_info->logo_size_px;
image_info->image = nsvgParseFromFile(image_info->path, "", logo_size_px);
if (!image_info->image) {
fprintf(stderr, "failed to load SVG image\n");
fprintf(stderr, " image path: %s\n", image_info->path);
return 1; return 1;
} }
if (message_bottom) { // For taller images make sure they don't get too wide
fontsz = ((float)dpi_info->font_size_b * PT_TO_MM) / if (image_info->image->width < image_info->image->height * 1.1)
(font->fontAscent - font->fontDescent) * logo_size_px = MM_TO_PX(dpi_info->dpi, 25);
dpi_info->pixels_per_milli;
const char *text = getTextDimensions(font, message_bottom, float sz =
fontsz, &textWidth, (float)logo_size_px /
&bottomTextHeight); (image_info->image->width > image_info->image->height ? image_info->image->height : image_info->image->width);
tx = screenWidth / 2.f - textWidth / 2.f; image_info->width = image_info->image->width * sz + 0.5;
ty = screenHeight - bottomTextHeight - bottomTextOffset; image_info->height = image_info->image->height * sz + 0.5;
draw_text(font, text, tx, ty, textWidth, if (image_info->width > (dpi_info->logo_size_max_mm * dpi_info->pixels_per_milli)) {
bottomTextHeight, fontsz, tfb_gray); float scalefactor =
if (text != message_bottom) ((float)(dpi_info->logo_size_max_mm * dpi_info->pixels_per_milli) / image_info->width);
free((void *)text); printf("Got scale factor: %f\n", scalefactor);
} image_info->width = dpi_info->logo_size_max_mm * dpi_info->pixels_per_milli;
image_info->height *= scalefactor;
if (message) {
fontsz = ((float)dpi_info->font_size * PT_TO_MM) /
(font->fontAscent - font->fontDescent) *
dpi_info->pixels_per_milli;
LOG("Fontsz: %f\n", fontsz);
const char *text = getTextDimensions(font, message, fontsz,
&textWidth, &textHeight);
tx = screenWidth / 2.f - textWidth / 2.f;
ty = (screenHeight - bottomTextHeight - bottomTextOffset) -
MM_TO_PX(dpi_info->dpi, dpi_info->font_size_b * PT_TO_MM) -
textHeight;
draw_text(font, message, tx, ty, textWidth, textHeight,
fontsz, tfb_gray);
if (text != message)
free((void *)text);
} }
image_info->x = (float)screenWidth / 2 - image_info->width * 0.5f;
image_info->y = (float)screenHeight / 2 - image_info->height * 0.5f;
return 0; return 0;
} }
@ -325,19 +393,28 @@ int main(int argc, char **argv)
int rc = 0; int rc = 0;
char *message = NULL; char *message = NULL;
char *message_bottom = NULL; char *message_bottom = NULL;
char *splash_image = NULL;
char *font_path = DEFAULT_FONT_PATH;
char active_tty[TTY_PATH_LEN + 1]; char active_tty[TTY_PATH_LEN + 1];
NSVGimage *image = NULL;
NSVGimage *font = NULL;
struct sigaction action; struct sigaction action;
struct messages msgs = {
.font_path = DEFAULT_FONT_PATH,
.font_size_pt = FONT_SIZE_PT,
.font_size_b_pt = FONT_SIZE_B_PT,
.msg = NULL,
.bottom_msg = NULL,
};
struct dpi_info dpi_info = { struct dpi_info dpi_info = {
.dpi = 0, .dpi = 0,
.pixels_per_milli = 0, .pixels_per_milli = 0,
.logo_size_px = 0, .logo_size_px = 0,
.logo_size_max_mm = LOGO_SIZE_MAX_MM, .logo_size_max_mm = LOGO_SIZE_MAX_MM,
.font_size = FONT_SIZE_PT, };
.font_size_b = FONT_SIZE_B_PT, struct image_info image_info = {
.path = NULL,
.image = NULL,
.width = 0,
.height = 0,
.x = 0,
.y = 0,
}; };
int optflag; int optflag;
bool animation = true; bool animation = true;
@ -359,19 +436,21 @@ int main(int argc, char **argv)
debug = true; debug = true;
break; break;
case 'f': case 'f':
font_path = optarg; msgs.font_path = optarg;
break; break;
case 's': case 's':
splash_image = optarg; image_info.path = optarg;
break; break;
case 'm': case 'm':
message = optarg; message = malloc(strlen(optarg) + 1);
strcpy(message, optarg);
break; break;
case 'b': case 'b':
message_bottom = optarg; message_bottom = malloc(strlen(optarg) + 1);
strcpy(message_bottom, optarg);
break; break;
case 'o': case 'o':
dpi_info.font_size_b = strtof(optarg, &end); msgs.font_size_b_pt = strtof(optarg, &end);
if (end == optarg) { if (end == optarg) {
fprintf(stderr, "Invalid font size: %s\n", fprintf(stderr, "Invalid font size: %s\n",
optarg); optarg);
@ -379,7 +458,7 @@ int main(int argc, char **argv)
} }
break; break;
case 'p': case 'p':
dpi_info.font_size = strtof(optarg, &end); msgs.font_size_pt = strtof(optarg, &end);
if (end == optarg) { if (end == optarg) {
fprintf(stderr, "Invalid font size: %s\n", fprintf(stderr, "Invalid font size: %s\n",
optarg); optarg);
@ -435,43 +514,36 @@ int main(int argc, char **argv)
calculate_dpi_info(&dpi_info); calculate_dpi_info(&dpi_info);
image = nsvgParseFromFile(splash_image, "", dpi_info.logo_size_px); rc = load_image(&dpi_info, &image_info);
if (!image) { if (rc)
fprintf(stderr, "failed to load SVG image\n");
rc = 1;
goto out; goto out;
}
if (image->width < image->height * 1.5) float animation_y = image_info.y + image_info.height + MM_TO_PX(dpi_info.dpi, 5);
dpi_info.logo_size_px = MM_TO_PX(dpi_info.dpi, 25);
float sz =
(float)dpi_info.logo_size_px /
(image->width > image->height ? image->height : image->width);
float image_w = image->width * sz + 0.5;
float image_h = image->height * sz + 0.5;
if (image_w > (dpi_info.logo_size_max_mm * dpi_info.pixels_per_milli)) {
float scalefactor =
((float)(dpi_info.logo_size_max_mm * dpi_info.pixels_per_milli) / image_w);
printf("Got scale factor: %f\n", scalefactor);
image_w = dpi_info.logo_size_max_mm * dpi_info.pixels_per_milli;
image_h *= scalefactor;
}
float x = (float)screenWidth / 2;
float y = (float)screenHeight / 2;
// Center the image
x -= image_w * 0.5f;
y -= image_h * 0.5f;
float animation_y = y + image_h + MM_TO_PX(dpi_info.dpi, 5);
tfb_clear_screen(tfb_make_color(background_color.r, background_color.g, tfb_clear_screen(tfb_make_color(background_color.r, background_color.g,
background_color.b)); background_color.b));
draw_svg(image, x, y, image_w, image_h); draw_svg(image_info.image, image_info.x, image_info.y, image_info.width, image_info.height);
if (message || message_bottom) if (!message && !message_bottom)
show_messages(&dpi_info, font_path, message, message_bottom); goto no_messages;
struct msg_info bottom_msg, msg;
memset(&bottom_msg, 0, sizeof(bottom_msg));
memset(&msg, 0, sizeof(msg));
bottom_msg.src_message = message_bottom;
msg.src_message = message;
if (message_bottom)
msgs.bottom_msg = &bottom_msg;
if (message)
msgs.msg = &msg;
show_messages(&msgs, &dpi_info);
no_messages:
tfb_flush_window(); tfb_flush_window();
tfb_flush_fb(); tfb_flush_fb();
@ -492,13 +564,22 @@ int main(int argc, char **argv)
out: out:
// Before we exit print the logo so it will persist // Before we exit print the logo so it will persist
if (image) { if (image_info.image) {
ioctl(tty, KDSETMODE, KD_TEXT); ioctl(tty, KDSETMODE, KD_TEXT);
draw_svg(image, x, y, image_w, image_h); draw_svg(image_info.image, image_info.x, image_info.y, image_info.width, image_info.height);
} }
nsvgDelete(font); // Draw the messages again so they will persist
nsvgDelete(image); show_messages(&msgs, &dpi_info);
nsvgDelete(image_info.image);
nsvgDelete(msgs.font);
free_message(msgs.msg);
free_message(msgs.bottom_msg);
if (message)
free(message);
if (message_bottom)
free(message_bottom);
// The TTY might end up in a weird state if this // The TTY might end up in a weird state if this
// is not called! // is not called!
tfb_release_fb(); tfb_release_fb();