From 68b0c39a58b9d38e4edeaaa8e650c1937b1de024 Mon Sep 17 00:00:00 2001 From: Jon Ringle Date: Mon, 25 Sep 2023 13:56:14 -0400 Subject: [PATCH] Use width/height provided by scenic opts and center on screen if smaller --- c_src/device/cairo.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/c_src/device/cairo.c b/c_src/device/cairo.c index 18735b6..64fa3d5 100644 --- a/c_src/device/cairo.c +++ b/c_src/device/cairo.c @@ -111,7 +111,7 @@ int device_init(const device_opts_t* p_opts, p_ctx->ratio = 1.0f; p_ctx->dist_tolerance = 0.1f * p_ctx->ratio; - size_t pix_count = g_cairo_fb.var.xres * g_cairo_fb.var.yres; + size_t pix_count = p_opts->width * p_opts->height; switch (g_cairo_fb.var.bits_per_pixel) { case 8: @@ -137,8 +137,8 @@ int device_init(const device_opts_t* p_opts, set332map(g_cairo_fb.fd); } - p_info->width = g_cairo_fb.var.xres; - p_info->height = g_cairo_fb.var.yres; + p_info->width = p_opts->width; + p_info->height = p_opts->height; p_ctx->font_size = 10.0; // Cairo default p_ctx->text_align = TEXT_ALIGN_LEFT; @@ -238,6 +238,8 @@ void render_cairo_surface_to_fb(scenic_cairo_ctx_t* p_ctx) { cairo_surface_flush(p_ctx->surface); uint8_t* cairo_buff = cairo_image_surface_get_data(p_ctx->surface); + uint32_t width = cairo_image_surface_get_width(p_ctx->surface); + uint32_t height = cairo_image_surface_get_height(p_ctx->surface); bool is_bgr555 = ((g_cairo_fb.var.red.offset == 0 && g_cairo_fb.var.green.offset == 5 && @@ -245,7 +247,7 @@ void render_cairo_surface_to_fb(scenic_cairo_ctx_t* p_ctx) ? true : false; - size_t pix_count = g_cairo_fb.var.xres * g_cairo_fb.var.yres; + size_t pix_count = width * height; int cpp = 0; switch (g_cairo_fb.var.bits_per_pixel) @@ -308,10 +310,24 @@ void render_cairo_surface_to_fb(scenic_cairo_ctx_t* p_ctx) break; } - uint32_t yc = g_cairo_fb.var.yres_virtual; - uint32_t xc = g_cairo_fb.var.xres_virtual; - size_t x_stride = (g_cairo_fb.fix.line_length * 8) / g_cairo_fb.var.bits_per_pixel; - size_t fb_size = x_stride * yc * cpp; + uint32_t x_stride = (g_cairo_fb.fix.line_length * 8) / g_cairo_fb.var.bits_per_pixel; + + uint32_t pic_xs = g_device_info.width; + uint32_t pic_ys = g_device_info.height; + uint32_t scr_xs = x_stride; + uint32_t scr_ys = g_cairo_fb.var.yres; + + uint32_t xc = (pic_xs > scr_xs) ? scr_xs : pic_xs; + uint32_t yc = (pic_ys > scr_ys) ? scr_ys : pic_ys; + + uint32_t x_offs = (pic_xs < g_cairo_fb.var.xres) + ? (g_cairo_fb.var.xres - pic_xs) / 2 + : 0; + uint32_t y_offs = (pic_ys < g_cairo_fb.var.yres) + ? (g_cairo_fb.var.yres - pic_ys) / 2 + : 0; + + size_t fb_size = scr_xs * scr_ys * cpp; uint8_t* fb = mmap(NULL, fb_size, PROT_WRITE | PROT_READ, MAP_SHARED, g_cairo_fb.fd, 0); if (fb == MAP_FAILED) { @@ -319,10 +335,10 @@ void render_cairo_surface_to_fb(scenic_cairo_ctx_t* p_ctx) return; } - uint8_t* p_fb = fb; + uint8_t* p_fb = fb + (y_offs * scr_xs + x_offs) * cpp; uint8_t* p_image = p_ctx->fbbuff.c; - for (uint32_t i = 0; i < yc; i++, p_fb += x_stride * cpp, p_image += xc * cpp) + for (uint32_t i = 0; i < yc; i++, p_fb += scr_xs * cpp, p_image += pic_xs * cpp) memcpy(p_fb, p_image, xc * cpp); munmap(fb, fb_size);