From dfe686910851c48d006114efa2f0aeb2ef2ed1a1 Mon Sep 17 00:00:00 2001 From: Vincent Wei Date: Sat, 29 Feb 2020 22:23:47 +0800 Subject: [PATCH] improve dummy engine to support PROCS runmode (sharedfb schema) --- src/newgal/dummy/nullvideo.c | 129 +++++++++++++++++++++++++++++++---- src/newgal/dummy/nullvideo.h | 9 ++- 2 files changed, 120 insertions(+), 18 deletions(-) diff --git a/src/newgal/dummy/nullvideo.c b/src/newgal/dummy/nullvideo.c index 5beb3ea7..5bdcd078 100644 --- a/src/newgal/dummy/nullvideo.c +++ b/src/newgal/dummy/nullvideo.c @@ -60,6 +60,7 @@ #include #include "common.h" +#include "minigui.h" #include "newgal.h" #include "sysvideo.h" #include "pixels_c.h" @@ -68,6 +69,21 @@ #include "nullvideo.h" +#if defined(_MGRM_PROCESSES) && defined (_MGSCHEMA_SHAREDFB) +/* Since 5.0.0, + we use shm_open for the shared frame buffer among processes + under MiniGUI-Processes runtime mode and shared frame buffer schema. */ +#include +#include /* For mode constants */ +#include +#include /* For O_* constants */ +#include + +#include "sharedres.h" + +#define DUMMYVID_SHAREDMEM_NAME "/minigui-procs-dummy" +#endif + #define DUMMYVID_DRIVER_NAME "dummy" /* Initialization/Query functions */ @@ -100,19 +116,19 @@ static GAL_VideoDevice *DUMMY_CreateDevice(int devindex) /* Initialize all variables that we clean on shutdown */ device = (GAL_VideoDevice *)malloc(sizeof(GAL_VideoDevice)); - if ( device ) { + if (device) { memset(device, 0, (sizeof *device)); device->hidden = (struct GAL_PrivateVideoData *) malloc((sizeof *device->hidden)); } - if ( (device == NULL) || (device->hidden == NULL) ) { + if ((device == NULL) || (device->hidden == NULL)) { GAL_OutOfMemory(); - if ( device ) { + if (device) { free(device); } return(0); } - memset(device->hidden, 0, (sizeof *device->hidden)); + memset (device->hidden, 0, (sizeof *device->hidden)); /* Set the function pointers */ device->VideoInit = DUMMY_VideoInit; @@ -140,7 +156,22 @@ VideoBootStrap DUMMY_bootstrap = { static int DUMMY_VideoInit(_THIS, GAL_PixelFormat *vformat) { - fprintf (stderr, "NEWGAL>DUMMY: Calling init method!\n"); +#if defined(_MGRM_PROCESSES) && defined (_MGSCHEMA_SHAREDFB) + struct GAL_PrivateVideoData* data = this->hidden; + + if (mgIsServer) { + data->fd = shm_open (DUMMYVID_SHAREDMEM_NAME, + O_CREAT | O_RDWR | O_CLOEXEC, 0666); + if (data->fd < 0) + return -1; + } + else { + data->fd = shm_open (DUMMYVID_SHAREDMEM_NAME, + O_RDWR | O_CLOEXEC, 0666); + if (data->fd < 0) + return -1; + } +#endif /* Determine the screen depth (use default 8-bit depth) */ /* we change this during the GAL_SetVideoMode implementation... */ @@ -165,16 +196,69 @@ static GAL_Surface *DUMMY_SetVideoMode(_THIS, GAL_Surface *current, { int pitch; +#if defined(_MGRM_PROCESSES) && defined (_MGSCHEMA_SHAREDFB) + if (mgIsServer) { + + pitch = width * ((bpp + 7) / 8); + pitch = (pitch + 3) & ~3; + this->hidden->length = pitch * height; + + if (ftruncate (this->hidden->fd, this->hidden->length)) { + close (this->hidden->fd); + this->hidden->fd = -1; + _ERR_PRINTF ("NEWGAL>DUMMY: " + "failed to calling ftruncate to set length of shared FB\n"); + return NULL; + } + } + else { + /* get the video mode info from shared resource */ + bpp = SHAREDRES_VIDEO_DEPTH; + width = SHAREDRES_VIDEO_HRES; + height = SHAREDRES_VIDEO_VRES; + + pitch = width * ((bpp + 7) / 8); + pitch = (pitch + 3) & ~3; + this->hidden->length = pitch * height; + } + + this->hidden->buffer = + mmap (NULL, this->hidden->length, PROT_READ | PROT_WRITE, MAP_SHARED, + this->hidden->fd, 0); + if (this->hidden->buffer == MAP_FAILED) { + _ERR_PRINTF ("NEWGAL>DUMMY: " + "failed to map the shared FB object\n"); + close (this->hidden->fd); + this->hidden->fd = -1; + return NULL; + } + + /* mmap succeed, close fd */ + close (this->hidden->fd); + this->hidden->fd = -1; + + /* Allocate the new pixel format for the screen */ + if (!GAL_ReallocFormat (current, bpp, 0, 0, 0, 0)) { + munmap (this->hidden->buffer, this->hidden->length); + close (this->hidden->fd); + + this->hidden->buffer = NULL; + this->hidden->fd = -1; + + _ERR_PRINTF ("NEWGAL>DUMMY: " + "Couldn't allocate new pixel format for requested mode\n"); + return (NULL); + } + +#else /* defined(_MGRM_PROCESSES) && defined (_MGSCHEMA_SHAREDFB) */ + if (this->hidden->buffer) { free (this->hidden->buffer); } - pitch = width * ((bpp + 7) / 8); - pitch = (pitch + 3) & ~3; - this->hidden->buffer = malloc (pitch * height); if (!this->hidden->buffer) { - fprintf (stderr, "NEWGAL>DUMMY: " + _ERR_PRINTF ("NEWGAL>DUMMY: " "Couldn't allocate buffer for requested mode\n"); return NULL; } @@ -185,20 +269,21 @@ static GAL_Surface *DUMMY_SetVideoMode(_THIS, GAL_Surface *current, if (!GAL_ReallocFormat (current, bpp, 0, 0, 0, 0)) { free(this->hidden->buffer); this->hidden->buffer = NULL; - fprintf (stderr, "NEWGAL>DUMMY: " + _ERR_PRINTF ("NEWGAL>DUMMY: " "Couldn't allocate new pixel format for requested mode\n"); return(NULL); } +#endif /*! (defined(_MGRM_PROCESSES) && defined (_MGSCHEMA_SHAREDFB)) */ /* Set up the new mode framebuffer */ - current->flags = flags & GAL_FULLSCREEN; + current->flags = GAL_FULLSCREEN | GAL_HWSURFACE; this->hidden->w = current->w = width; this->hidden->h = current->h = height; current->pitch = pitch; current->pixels = this->hidden->buffer; /* We're done */ - return(current); + return (current); } /* We don't actually allow hardware surfaces other than the main one */ @@ -222,11 +307,25 @@ static int DUMMY_SetColors(_THIS, int firstcolor, int ncolors, GAL_Color *colors */ static void DUMMY_VideoQuit(_THIS) { - if (this->screen->pixels != NULL) - { - free(this->screen->pixels); +#if defined(_MGRM_PROCESSES) && defined (_MGSCHEMA_SHAREDFB) + if (this->hidden->buffer) { + munmap (this->hidden->buffer, this->hidden->length); + if (this->hidden->fd >= 0) + close (this->hidden->fd); + + this->hidden->buffer = NULL; + this->hidden->fd = -1; + } + + if (mgIsServer) { + shm_unlink (DUMMYVID_SHAREDMEM_NAME); + } +#else + if (this->screen->pixels != NULL) { + free (this->screen->pixels); this->screen->pixels = NULL; } +#endif /* not (defined(_MGRM_PROCESSES) && defined (_MGSCHEMA_SHAREDFB)) */ } #endif /* _MGGAL_DUMMY */ diff --git a/src/newgal/dummy/nullvideo.h b/src/newgal/dummy/nullvideo.h index 828d736e..e9633481 100644 --- a/src/newgal/dummy/nullvideo.h +++ b/src/newgal/dummy/nullvideo.h @@ -53,10 +53,13 @@ #define _THIS GAL_VideoDevice *this /* Private display data */ - struct GAL_PrivateVideoData { - int w, h; - void *buffer; + int w, h; + void * buffer; +#if defined(_MGRM_PROCESSES) && defined (_MGSCHEMA_SHAREDFB) + int fd; // the file descriptor of the shared frame buffer object + size_t length; +#endif }; #endif /* _GAL_nullvideo_h */