added clipping rectangles
[winnie] / src / fbdev / gfx.cc
index db579ff..422312c 100644 (file)
@@ -20,8 +20,11 @@ static unsigned char *framebuffer;
 static int dev_fd = -1;
 
 static Rect screen_rect;
+
 static int color_depth; // bits per pixel
 
+static Pixmap *pixmap;
+
 bool init_gfx()
 {
        if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
@@ -45,6 +48,8 @@ bool init_gfx()
        screen_rect.height = sinfo.yres_virtual;
        color_depth = sinfo.bits_per_pixel;
 
+       set_clipping_rect(screen_rect);
+
        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);
 
@@ -55,15 +60,23 @@ bool init_gfx()
                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));
+//             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);
        }
+*/
+
+       pixmap = new Pixmap;
+       pixmap->width = screen_rect.width;
+       pixmap->height = screen_rect.height;
+       pixmap->pixels = framebuffer;
 
        return true;
 }
@@ -80,6 +93,8 @@ void destroy_gfx()
 
        munmap(framebuffer, FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth));
        framebuffer = 0;
+
+       pixmap->pixels = 0;
 }
 
 unsigned char *get_framebuffer()
@@ -87,6 +102,11 @@ unsigned char *get_framebuffer()
        return framebuffer;
 }
 
+Pixmap *get_framebuffer_pixmap()
+{
+       return pixmap;
+}
+
 Rect get_screen_size()
 {
        return screen_rect;
@@ -97,43 +117,6 @@ int get_color_depth()
        return color_depth;
 }
 
-void clear_screen(int r, int g, int b)
-{
-       fill_rect(screen_rect, r, g, b);
-}
-
-void fill_rect(const Rect &rect, int r, int g, int b)
-{
-       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; i<drect.height; i++) {
-               for(int j=0; j<drect.width; j++) {
-                       fb[j * 4] = b;
-                       fb[j * 4 + 1] = g;
-                       fb[j * 4 + 2] = r;
-               }
-               fb += screen_rect.width * 4;
-       }
-}
 
 void set_cursor_visibility(bool visible)
 {
@@ -145,101 +128,6 @@ 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)
-{
-       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<height; i++) {
-               memcpy(dptr, sptr, width * 4);
-               sptr += src_rect.width * 4;
-               dptr += dest_rect.width * 4;
-       }
-}
-
-void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
-               const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b)
-{
-       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<height; i++) {
-               for(int j=0; j<width; j++) {
-                       int r = sptr[j * 4];
-                       int g = sptr[j * 4 + 1];
-                       int b = sptr[j * 4 + 2];
-
-                       if(r != key_r || g != key_g || b != key_b) {
-                               dptr[j * 4] = b;
-                               dptr[j * 4 + 1] = g;
-                               dptr[j * 4 + 2] = r;
-                       }
-               }
-
-               sptr += src_rect.width * 4;
-               dptr += dest_rect.width * 4;
-       }
-}
-
 void gfx_update()
 {
 }
@@ -247,16 +135,9 @@ void gfx_update()
 void wait_vsync()
 {
        unsigned long arg = 0;
-
-       timeval tvstart, tvend;
-       gettimeofday(&tvstart, 0);
-       
        if(ioctl(dev_fd, FBIO_WAITFORVSYNC, &arg) == -1) {
-               printf("ioctl error %s\n", strerror(errno));
+//             printf("ioctl error %s\n", strerror(errno));
        }
-
-       gettimeofday(&tvend, 0);
-       printf("%ld %ld\n", tvend.tv_sec - tvstart.tv_sec, tvend.tv_usec - tvstart.tv_usec);
 }
 
 #endif // WINNIE_FBDEV