X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Ffbdev%2Fgfx.cc;h=99844220e8b6d5cdb77c1d396db7babee217e0e0;hb=2a5665e9005d05c8d30747f1fcb2c41d0a8f27c8;hp=e4527b0c04998b2198c294ecec0d48e3261d6812;hpb=ecb25bb23fcd6f98fb049297483e312c84fd7b5b;p=winnie diff --git a/src/fbdev/gfx.cc b/src/fbdev/gfx.cc index e4527b0..9984422 100644 --- a/src/fbdev/gfx.cc +++ b/src/fbdev/gfx.cc @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -16,23 +17,34 @@ #define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT) -static unsigned char *framebuffer; -static int dev_fd = -1; +struct Graphics { + unsigned char *framebuffer; + int dev_fd; + Rect screen_rect; + Rect clipping_rect; + int color_depth; + Pixmap *pixmap; +}; -static Rect screen_rect; -static int color_depth; // bits per pixel +static Graphics *gfx; bool init_gfx() { - if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) { + if(!(gfx = (Graphics*)malloc(sizeof *gfx))) { + return false; + } + + gfx->dev_fd = -1; + + if((gfx->dev_fd = open("/dev/fb0", O_RDWR)) == -1) { fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno)); return false; } fb_var_screeninfo sinfo; - if(ioctl(dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) { - close(dev_fd); - dev_fd = -1; + if(ioctl(gfx->dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) { + close(gfx->dev_fd); + gfx->dev_fd = -1; fprintf(stderr, "Unable to get screen info : %s\n", strerror(errno)); return false; } @@ -40,35 +52,41 @@ bool init_gfx() printf("width : %d height : %d\n : bpp : %d\n", sinfo.xres, sinfo.yres, sinfo.bits_per_pixel); printf("virtual w: %d virtual h: %d\n", sinfo.xres_virtual, sinfo.yres_virtual); - screen_rect.x = screen_rect.y = 0; - screen_rect.width = sinfo.xres_virtual; - screen_rect.height = sinfo.yres_virtual; - color_depth = sinfo.bits_per_pixel; + gfx->screen_rect.x = gfx->screen_rect.y = 0; + gfx->screen_rect.width = sinfo.xres_virtual; + gfx->screen_rect.height = sinfo.yres_virtual; + gfx->color_depth = sinfo.bits_per_pixel; - int sz = FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth); - framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0); + set_clipping_rect(gfx->screen_rect); - if(framebuffer == (void*)-1) { - close(dev_fd); - dev_fd = -1; + int sz = FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth); + gfx->framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, gfx->dev_fd, 0); + + if(gfx->framebuffer == (void*)-1) { + close(gfx->dev_fd); + gfx->dev_fd = -1; fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno)); return false; } // TODO: uncomment when I find how to use intelfb instead of i915 GRRRR.- - -/* fb_vblank vblank; - if(ioctl(dev_fd, FBIOGET_VBLANK, &vblank) == -1) { - fprintf(stderr, "FBIOGET_VBLANK error: %s\n", strerror(errno)); + if(ioctl(gfx->dev_fd, FBIOGET_VBLANK, &vblank) == -1) { +// fprintf(stderr, "FBIOGET_VBLANK error: %s\n", strerror(errno)); } - else { +/* + else { printf("flags: %x\n", vblank.flags); printf("count: %d\n", vblank.count); printf("beam position: %d, %d\n", vblank.hcount, vblank.vcount); } */ + gfx->pixmap = new Pixmap; + gfx->pixmap->width = gfx->screen_rect.width; + gfx->pixmap->height = gfx->screen_rect.height; + gfx->pixmap->pixels = gfx->framebuffer; + return true; } @@ -76,67 +94,48 @@ void destroy_gfx() { clear_screen(0, 0, 0); - if(dev_fd != -1) { - close(dev_fd); + if(gfx->dev_fd != -1) { + close(gfx->dev_fd); } - dev_fd = -1; + gfx->dev_fd = -1; - munmap(framebuffer, FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth)); - framebuffer = 0; + munmap(gfx->framebuffer, FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth)); + gfx->framebuffer = 0; + + gfx->pixmap->pixels = 0; + + free(gfx); } unsigned char *get_framebuffer() { - return framebuffer; + return gfx->framebuffer; +} + +Pixmap *get_framebuffer_pixmap() +{ + return gfx->pixmap; } Rect get_screen_size() { - return screen_rect; + return gfx->screen_rect; } int get_color_depth() { - return color_depth; + return gfx->color_depth; } -void clear_screen(int r, int g, int b) +void set_clipping_rect(const Rect &rect) { - fill_rect(screen_rect, r, g, b); + gfx->clipping_rect = rect_intersection(rect, get_screen_size()); } -void fill_rect(const Rect &rect, int r, int g, int b) +const Rect &get_clipping_rect() { - Rect drect = rect; - - if(drect.x < screen_rect.x) { - drect.width -= screen_rect.x - drect.x; - drect.x = screen_rect.x; - } - - if(drect.y < screen_rect.y) { - drect.height -= screen_rect.y - drect.y; - drect.y = screen_rect.y; - } - - if(drect.x + drect.width >= screen_rect.x + screen_rect.width) { - drect.width = screen_rect.width - drect.x; - } - - if(drect.y + drect.height >= screen_rect.y + screen_rect.height) { - drect.height = screen_rect.height - drect.y; - } - - unsigned char *fb = framebuffer + (drect.x + screen_rect.width * drect.y) * 4; - for(int i=0; iclipping_rect; } void set_cursor_visibility(bool visible) @@ -144,106 +143,11 @@ void set_cursor_visibility(bool visible) fb_cursor curs; curs.enable = visible ? 1 : 0; - if(ioctl(dev_fd, FBIO_CURSOR, &curs) == -1) { + if(ioctl(gfx->dev_fd, FBIO_CURSOR, &curs) == -1) { fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno)); } } -void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img, - const Rect &dest_rect, int dest_x, int dest_y) -{ - int width = src_rect.width; - int height = src_rect.height; - - int xoffs = dest_x - dest_rect.x; - if(xoffs < 0) { - dest_x = dest_rect.x; - width += xoffs; - } - - int yoffs = dest_y - dest_rect.y; - if(yoffs < 0) { - dest_y = dest_rect.y; - height += yoffs; - } - - int xend = dest_x + width; - if(xend >= dest_rect.width) { - width -= xend - dest_rect.width; - } - - int yend = dest_y + height; - if(yend >= dest_rect.height) { - height -= yend - dest_rect.height; - } - - if(width <= 0 || height <= 0) { - return; - } - - unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4; - unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4; - - for(int i=0; i= dest_rect.width) { - width -= xend - dest_rect.width; - } - - int yend = dest_y + height; - if(yend >= dest_rect.height) { - height -= yend - dest_rect.height; - } - - if(width <= 0 || height <= 0) { - return; - } - - unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4; - unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4; - - for(int i=0; idev_fd, FBIO_WAITFORVSYNC, &arg) == -1) { // printf("ioctl error %s\n", strerror(errno)); } }