added memory allocator
[winnie] / src / fbdev / gfx.cc
index 00e1881..7ebc656 100644 (file)
@@ -2,27 +2,42 @@
 #include <errno.h>
 #include <limits.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
+#include <sys/time.h>
 #include <unistd.h>
 
 #include <linux/fb.h>
 
 #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 Rect screen_rect;
-static int color_depth; // bits per pixel
+struct Graphics {
+       Rect screen_rect;
+       Rect clipping_rect;
+       int color_depth;
+       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;
@@ -39,12 +54,14 @@ 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);
+       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) {
@@ -54,6 +71,33 @@ 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));
+       }
+/*     
+       else {
+               printf("flags: %x\n", vblank.flags);
+               printf("count: %d\n", vblank.count);
+               printf("beam position: %d, %d\n", vblank.hcount, vblank.vcount);
+       }
+*/
+
+       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;
 }
 
@@ -67,51 +111,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;
+
+       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 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 < 0) {
-               drect.x = 0;
-       }
-
-       if(drect.y < 0) {
-               drect.y = 0;
-       }
-
-       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;
-       }
+       return gfx->clipping_rect;
 }
 
 void set_cursor_visibility(bool visible)
@@ -124,103 +160,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()
 {
-       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;
-       }
+       memcpy(framebuffer, gfx->pixmap->pixels, gfx->pixmap->width * gfx->pixmap->height * (gfx->color_depth / 8));
 }
 
-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)
+void wait_vsync()
 {
-       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;
+       unsigned long arg = 0;
+       if(ioctl(dev_fd, FBIO_WAITFORVSYNC, &arg) == -1) {
+//             printf("ioctl error %s\n", strerror(errno));
        }
-
-       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()
-{
 }
 
 #endif // WINNIE_FBDEV