video fbdev: expand to support fb resize and stuff
authornotaz <notasas@gmail.com>
Mon, 6 Sep 2010 11:17:13 +0000 (11:17 +0000)
committernotaz <notasas@gmail.com>
Mon, 6 Sep 2010 11:17:13 +0000 (11:17 +0000)
git-svn-id: file:///home/notaz/opt/svn/PicoDrive@884 be3aeb3a-fb24-0410-a615-afba39da0efa

platform/linux/fbdev.c
platform/linux/fbdev.h

index afcd529..532a282 100644 (file)
@@ -21,8 +21,9 @@ struct vout_fbdev {
        struct  fb_var_screeninfo fbvar_old;
        struct  fb_var_screeninfo fbvar_new;
        int     buffer_write;
-       void    *buffers[FBDEV_MAX_BUFFERS];
+       int     fb_size;
        int     buffer_count;
+       int     top_border, bottom_border;
 };
 
 void *vout_fbdev_flip(struct vout_fbdev *fbdev)
@@ -37,11 +38,13 @@ void *vout_fbdev_flip(struct vout_fbdev *fbdev)
        if (fbdev->buffer_write >= fbdev->buffer_count)
                fbdev->buffer_write = 0;
 
-       fbdev->fbvar_new.yoffset = fbdev->fbvar_old.yres * draw_buf;
+       fbdev->fbvar_new.yoffset = 
+               (fbdev->top_border + fbdev->fbvar_new.yres + fbdev->bottom_border) * draw_buf +
+               fbdev->top_border;
 
        ioctl(fbdev->fd, FBIOPAN_DISPLAY, &fbdev->fbvar_new);
 
-       return fbdev->buffers[fbdev->buffer_write];
+       return (char *)fbdev->mem + fbdev->fb_size * fbdev->buffer_write;
 }
 
 void vout_fbdev_wait_vsync(struct vout_fbdev *fbdev)
@@ -50,15 +53,108 @@ void vout_fbdev_wait_vsync(struct vout_fbdev *fbdev)
        ioctl(fbdev->fd, FBIO_WAITFORVSYNC, &arg);
 }
 
+int vout_fbdev_resize(struct vout_fbdev *fbdev, int w, int h,
+                     int left_border, int right_border, int top_border, int bottom_border, int no_dblbuf)
+{
+       int w_total = left_border + w + right_border;
+       int h_total = top_border + h + bottom_border;
+       size_t mem_size;
+       int ret;
+
+       // unblank to be sure the mode is really accepted
+       ioctl(fbdev->fd, FBIOBLANK, FB_BLANK_UNBLANK);
+
+       if (fbdev->fbvar_new.bits_per_pixel != 16 ||
+                       w != fbdev->fbvar_new.xres ||
+                       h != fbdev->fbvar_new.yres ||
+                       w_total != fbdev->fbvar_new.xres_virtual ||
+                       h_total > fbdev->fbvar_new.yres_virtual ||
+                       left_border != fbdev->fbvar_new.xoffset) {
+               fbdev->fbvar_new.xres = w;
+               fbdev->fbvar_new.yres = h;
+               fbdev->fbvar_new.xres_virtual = w_total;
+               fbdev->fbvar_new.yres_virtual = h_total;
+               fbdev->fbvar_new.xoffset = left_border;
+               fbdev->fbvar_new.bits_per_pixel = 16;
+               printf(" switching to %dx%d@16\n", w, h);
+               ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
+               if (ret == -1) {
+                       perror("FBIOPUT_VSCREENINFO ioctl");
+                       return -1;
+               }
+       }
+
+       fbdev->buffer_count = FBDEV_MAX_BUFFERS; // be optimistic
+       if (no_dblbuf)
+               fbdev->buffer_count = 1;
+
+       if (fbdev->fbvar_new.yres_virtual < h_total * fbdev->buffer_count) {
+               fbdev->fbvar_new.yres_virtual = h_total * fbdev->buffer_count;
+               ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
+               if (ret == -1) {
+                       fbdev->buffer_count = 1;
+                       fprintf(stderr, "Warning: failed to increase virtual resolution, "
+                                       "doublebuffering disabled\n");
+               }
+       }
+
+       fbdev->fb_size = w_total * h_total * 2;
+       fbdev->top_border = top_border;
+       fbdev->bottom_border = bottom_border;
+
+       mem_size = fbdev->fb_size * fbdev->buffer_count;
+       if (fbdev->mem_size >= mem_size)
+               return 0;
+
+       if (fbdev->mem != NULL)
+               munmap(fbdev->mem, fbdev->mem_size);
+
+       fbdev->mem = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
+       if (fbdev->mem == MAP_FAILED && fbdev->buffer_count > 1) {
+               fprintf(stderr, "Warning: can't map %zd bytes, doublebuffering disabled\n", fbdev->mem_size);
+               fbdev->buffer_count = 1;
+               mem_size = fbdev->fb_size;
+               fbdev->mem = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
+       }
+       if (fbdev->mem == MAP_FAILED) {
+               fbdev->mem = NULL;
+               fbdev->mem_size = 0;
+               perror("mmap framebuffer");
+               return -1;
+       }
+
+       fbdev->mem_size = mem_size;
+       return 0;
+}
+
 void vout_fbdev_clear(struct vout_fbdev *fbdev)
 {
        memset(fbdev->mem, 0, fbdev->mem_size);
 }
 
+void vout_fbdev_clear_lines(struct vout_fbdev *fbdev, int y, int count)
+{
+       int stride = fbdev->fbvar_new.xres * fbdev->fbvar_new.bits_per_pixel / 8;
+       int i;
+
+       if (y + count > fbdev->fbvar_new.yres)
+               count = fbdev->fbvar_new.yres - y;
+
+       if (y >= 0 && count > 0)
+               for (i = 0; i < fbdev->buffer_count; i++)
+                       memset((char *)fbdev->mem + fbdev->fb_size * i + y * stride, 0, stride * count);
+}
+
+int vout_fbdev_get_fd(struct vout_fbdev *fbdev)
+{
+       return fbdev->fd;
+}
+
 struct vout_fbdev *vout_fbdev_init(const char *fbdev_name, int *w, int *h, int no_dblbuf)
 {
        struct vout_fbdev *fbdev;
-       int i, ret;
+       int req_w, req_h;
+       int ret;
 
        fbdev = calloc(1, sizeof(*fbdev));
        if (fbdev == NULL)
@@ -78,49 +174,24 @@ struct vout_fbdev *vout_fbdev_init(const char *fbdev_name, int *w, int *h, int n
        }
 
        fbdev->fbvar_new = fbdev->fbvar_old;
-       printf("%s: %ix%i@%d\n", fbdev_name, fbdev->fbvar_old.xres, fbdev->fbvar_old.yres,
-               fbdev->fbvar_old.bits_per_pixel);
-       *w = fbdev->fbvar_old.xres;
-       *h = fbdev->fbvar_old.yres;
-       fbdev->buffer_count = FBDEV_MAX_BUFFERS; // be optimistic
-       if (no_dblbuf)
-               fbdev->buffer_count = 1;
 
-       if (fbdev->fbvar_new.bits_per_pixel != 16) {
-               printf(" switching to 16bpp\n");
-               fbdev->fbvar_new.bits_per_pixel = 16;
-               ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
-               if (ret == -1) {
-                       perror("FBIOPUT_VSCREENINFO ioctl");
-                       goto fail;
-               }
-       }
-
-       if (fbdev->fbvar_new.yres_virtual < fbdev->fbvar_old.yres * fbdev->buffer_count) {
-               fbdev->fbvar_new.yres_virtual = fbdev->fbvar_old.yres * fbdev->buffer_count;
-               ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
-               if (ret == -1) {
-                       fbdev->buffer_count = 1;
-                       fprintf(stderr, "Warning: failed to increase virtual resolution, "
-                                       "doublebuffering disabled\n");
-               }
-       }
+       req_w = fbdev->fbvar_new.xres;
+       if (*w != 0)
+               req_w = *w;
+       req_h = fbdev->fbvar_new.yres;
+       if (*h != 0)
+               req_h = *h;
 
-       fbdev->mem_size = *w * *h * 2 * fbdev->buffer_count;
-       fbdev->mem = mmap(0, fbdev->mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
-       if (fbdev->mem == MAP_FAILED && fbdev->buffer_count > 1) {
-               fprintf(stderr, "Warning: can't map %zd bytes, doublebuffering disabled\n", fbdev->mem_size);
-               fbdev->mem_size = *w * *h * 2;
-               fbdev->buffer_count = 1;
-               fbdev->mem = mmap(0, fbdev->mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
-       }
-       if (fbdev->mem == MAP_FAILED) {
-               perror("mmap framebuffer");
+       ret = vout_fbdev_resize(fbdev, req_w, req_h, 0, 0, 0, 0, no_dblbuf);
+       if (ret != 0)
                goto fail;
-       }
+
+       printf("%s: %ix%i@%d\n", fbdev_name, fbdev->fbvar_new.xres, fbdev->fbvar_new.yres,
+               fbdev->fbvar_new.bits_per_pixel);
+       *w = fbdev->fbvar_new.xres;
+       *h = fbdev->fbvar_new.yres;
+
        memset(fbdev->mem, 0, fbdev->mem_size);
-       for (i = 0; i < fbdev->buffer_count; i++)
-               fbdev->buffers[i] = (char *)fbdev->mem + i * *w * *h * 2;
 
        // some checks
        ret = 0;
@@ -130,7 +201,7 @@ struct vout_fbdev *vout_fbdev_init(const char *fbdev_name, int *w, int *h, int n
 
        if (fbdev->buffer_count > 1) {
                fbdev->buffer_write = 0;
-               fbdev->fbvar_new.yoffset = fbdev->fbvar_old.yres * (fbdev->buffer_count - 1);
+               fbdev->fbvar_new.yoffset = fbdev->fbvar_new.yres * (fbdev->buffer_count - 1);
                ret = ioctl(fbdev->fd, FBIOPAN_DISPLAY, &fbdev->fbvar_new);
                if (ret != 0) {
                        fbdev->buffer_count = 1;
@@ -160,21 +231,3 @@ void vout_fbdev_finish(struct vout_fbdev *fbdev)
        free(fbdev);
 }
 
-#if 0
-void *g_screen_ptr;
-int main()
-{
-       int w, h;
-       vout_fbdev_init(&w, &h);
-       //while (1)
-       {
-               memset(g_screen_ptr, 0xff, fbdev_mem_size / 2);
-               plat_video_wait_vsync();
-               plat_video_flip();
-               memset(g_screen_ptr, 0x00, fbdev_mem_size / 2);
-               usleep(8000);
-//             plat_video_wait_vsync();
-               plat_video_flip();
-       }
-}
-#endif
index 9e5d433..fa163aa 100644 (file)
@@ -3,5 +3,10 @@ struct vout_fbdev;
 struct vout_fbdev *vout_fbdev_init(const char *fbdev_name, int *w, int *h, int no_dblbuf);
 void *vout_fbdev_flip(struct vout_fbdev *fbdev);
 void  vout_fbdev_wait_vsync(struct vout_fbdev *fbdev);
+int   vout_fbdev_resize(struct vout_fbdev *fbdev, int w, int h,
+                       int left_border, int right_border, int top_border, int bottom_border,
+                       int no_dblbuf);
 void  vout_fbdev_clear(struct vout_fbdev *fbdev);
+void  vout_fbdev_clear_lines(struct vout_fbdev *fbdev, int y, int count);
+int   vout_fbdev_get_fd(struct vout_fbdev *fbdev);
 void  vout_fbdev_finish(struct vout_fbdev *fbdev);