X-Git-Url: http://git.mutantstargoat.com?p=winnie;a=blobdiff_plain;f=src%2Ffbdev%2Fgfx.cc;h=1421348fccf50677f68f55425eb3b2362d6c334d;hp=742b6c687b48611461bfa537934e635dee65cb57;hb=e2626c41c841dbbfb64ddf6341b4e23089036299;hpb=695cf39b73ecc76cbbc39e54f844ae55f4a9f938 diff --git a/src/fbdev/gfx.cc b/src/fbdev/gfx.cc index 742b6c6..1421348 100644 --- a/src/fbdev/gfx.cc +++ b/src/fbdev/gfx.cc @@ -1,7 +1,29 @@ +/* +winnie - an experimental window system + +Copyright (C) 2013 Eleni Maria Stea + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Author: Eleni Maria Stea +*/ + #ifdef WINNIE_FBDEV #include #include #include +#include #include #include @@ -13,19 +35,31 @@ #include #include "gfx.h" +#include "shalloc.h" #define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT) static unsigned char *framebuffer; -static int dev_fd = -1; +static int dev_fd; +static int rgb_order[3]; -static Rect screen_rect; -static int color_depth; // bits per pixel +struct Graphics { + Rect screen_rect; + Rect clipping_rect; + int color_depth; + Pixmap *pixmap; +}; -static Pixmap *pixmap; +static Graphics *gfx; bool init_gfx() { + if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) { + return false; + } + + dev_fd = -1; + if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) { fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno)); return false; @@ -42,12 +76,18 @@ 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; + + rgb_order[0] = sinfo.red.offset / 8; + rgb_order[1] = sinfo.green.offset / 8; + rgb_order[2] = sinfo.blue.offset / 8; - int sz = FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth); + set_clipping_rect(gfx->screen_rect); + + int sz = FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth); framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0); if(framebuffer == (void*)-1) { @@ -70,10 +110,19 @@ bool init_gfx() } */ - pixmap = new Pixmap; - pixmap->width = screen_rect.width; - pixmap->height = screen_rect.height; - pixmap->pixels = framebuffer; + if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) { + fprintf(stderr, "Failed to allocate pixmap.\n"); + return false; + } + + gfx->pixmap->width = gfx->screen_rect.width; + gfx->pixmap->height = gfx->screen_rect.height; + + int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8; + if(!(gfx->pixmap->pixels = (unsigned char*)sh_malloc(fbsize))) { + fprintf(stderr, "failed to allocate the pixmap framebuffer.\n"); + return false; + } return true; } @@ -81,6 +130,7 @@ bool init_gfx() void destroy_gfx() { clear_screen(0, 0, 0); + gfx_update(gfx->screen_rect); if(dev_fd != -1) { close(dev_fd); @@ -88,68 +138,43 @@ void destroy_gfx() dev_fd = -1; - munmap(framebuffer, FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth)); + munmap(framebuffer, FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth)); framebuffer = 0; - pixmap->pixels = 0; + sh_free(gfx->pixmap->pixels); + gfx->pixmap->pixels = 0; + sh_free(gfx->pixmap); + sh_free(gfx); } unsigned char *get_framebuffer() { - return framebuffer; + return gfx->pixmap->pixels; } Pixmap *get_framebuffer_pixmap() { - return 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) @@ -162,103 +187,17 @@ void set_cursor_visibility(bool visible) } } -void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img, - const Rect &dest_rect, int dest_x, int dest_y) +void gfx_update(const Rect &upd_rect) { - 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; - } + Rect rect = rect_intersection(upd_rect, gfx->screen_rect); + unsigned char *sptr = gfx->pixmap->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4; + unsigned char *dptr = framebuffer + (rect.y * gfx->screen_rect.width + rect.x) * 4; - int yoffs = dest_y - dest_rect.y; - if(yoffs < 0) { - dest_y = dest_rect.y; - height += yoffs; + for(int i=0; iscreen_rect.width * 4; + dptr += gfx->screen_rect.width * 4; } - - 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; i