Skip to content

Commit

Permalink
Use width/height provided by scenic opts and center on screen if smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
ringlej committed Oct 2, 2023
1 parent 288dc5d commit 68b0c39
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions c_src/device/cairo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
Expand Down Expand Up @@ -238,14 +238,16 @@ 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 &&
g_cairo_fb.var.blue.offset == 10))
? 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)
Expand Down Expand Up @@ -308,21 +310,35 @@ 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) {
log_error("cairo: failed to mmap fb");
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);
Expand Down

0 comments on commit 68b0c39

Please sign in to comment.