From 62525f673867fa0c277da76423f4660f9e025853 Mon Sep 17 00:00:00 2001 From: Henk Wiedig Date: Tue, 19 Nov 2024 21:36:30 +0100 Subject: [PATCH] read surface size from shm --- osd.c | 5 +-- osd/util/Render_Rockchip.c | 73 ++++++++++++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/osd.c b/osd.c index 9659eda..5745cf5 100644 --- a/osd.c +++ b/osd.c @@ -317,8 +317,8 @@ static const display_info_t fhd_display_info = { char font_2_name[256]; -uint16_t OVERLAY_WIDTH =1280; -uint16_t OVERLAY_HEIGHT =720; +uint16_t OVERLAY_WIDTH =1800; +uint16_t OVERLAY_HEIGHT =1000; static displayport_vtable_t *display_driver; @@ -1973,6 +1973,7 @@ static void InitMSPHook(){ if (DrawOSD) Init_x86(&OVERLAY_WIDTH, &OVERLAY_HEIGHT); height=OVERLAY_HEIGHT; + printf("OSD is %ix%i pixels\n",OVERLAY_WIDTH,OVERLAY_HEIGHT); #endif //Get video resolution diff --git a/osd/util/Render_Rockchip.c b/osd/util/Render_Rockchip.c index 7052e0a..76c8590 100644 --- a/osd/util/Render_Rockchip.c +++ b/osd/util/Render_Rockchip.c @@ -21,32 +21,77 @@ cairo_surface_t* surface=NULL; cairo_surface_t* image_surface = NULL; cairo_t* cr=NULL; Pixmap backbuffer_pixmap; -cairo_surface_t* backbuffer_surface; +// Define the shared memory region structure +typedef struct { + uint16_t width; // Image width + uint16_t height; // Image height + unsigned char data[]; // Flexible array for image data +} SharedMemoryRegion; extern int AHI_TiltY; int Init_x86(uint16_t *width, uint16_t *height) { // Create shared memory region + const char *shm_name = "msposd"; // Name of the shared memory region + // Open the shared memory segment - int shm_fd = shm_open("msposd", O_RDWR, 0666); + int shm_fd = shm_open(shm_name, O_RDWR, 0666); if (shm_fd == -1) { - perror("Failed to open shared memory"); + perror("Failed to open shared memory"); + return -1; + } + + // Map just the header to read width and height + size_t header_size = sizeof(SharedMemoryRegion); + SharedMemoryRegion *shm_region = (SharedMemoryRegion *) mmap(0, header_size, PROT_READ, MAP_SHARED, shm_fd, 0); + if (shm_region == MAP_FAILED) { + perror("Failed to map shared memory header"); + close(shm_fd); + return -1; + } + + // Validate width and height (optional) + if (shm_region->width <= 0 || shm_region->width <= 0) { + fprintf(stderr, "Invalid width or height in shared memory\n"); + munmap(shm_region, header_size); + close(shm_fd); + return -1; + } + + int shm_width = shm_region->width; + int shm_height = shm_region->height; + *width = shm_region->width; + *height = shm_region->height; + + printf("Surface is %ix%i pixels\n",shm_width,shm_height); + + // Unmap the header (optional, as we'll remap everything) + munmap(shm_region, header_size); + + // Calculate the total size of shared memory + size_t shm_size = header_size + (shm_width * shm_height * 4); // Header + Image data + + // Remap the entire shared memory region (header + image data) + shm_region = (SharedMemoryRegion *) mmap(0, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); + if (shm_region == MAP_FAILED) { + perror("Failed to remap shared memory"); + close(shm_fd); + return -1; } - // Map the shared memory to the process's address space - size_t shm_size = (*width) * (*height) * 4; // 4 bytes per pixel (RGBA) - unsigned char *shm_data = (unsigned char*) mmap(0, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); - if (shm_data == MAP_FAILED) { - perror("Failed to map shared memory"); - close(shm_fd); + // Create a Cairo surface for the image data + surface = cairo_image_surface_create_for_data( + shm_region->data, CAIRO_FORMAT_ARGB32, shm_width, shm_height, shm_width * 4); + if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) { + fprintf(stderr, "Failed to create Cairo surface\n"); + munmap(shm_region, shm_size); + close(shm_fd); + return -1; } - // Create a Cairo image surface on the shared memory buffer - cairo_surface_t *surface = cairo_image_surface_create_for_data( - shm_data, CAIRO_FORMAT_ARGB32, (*width) , (*height), (*width) * 4 - ); - cr = cairo_create(surface); + // Create a Cairo context + cr = cairo_create(surface); }