Home | History | Annotate | Download | only in framebuffer
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <stdlib.h>
     18 #include <unistd.h>
     19 
     20 #include <fcntl.h>
     21 #include <stdio.h>
     22 
     23 #include <sys/ioctl.h>
     24 #include <sys/mman.h>
     25 #include <sys/types.h>
     26 #include <time.h>
     27 
     28 #include <linux/fb.h>
     29 #include <linux/kd.h>
     30 
     31 struct simple_fb {
     32     void *data;
     33     int width;
     34     int height;
     35     int stride;
     36     int bpp;
     37 };
     38 
     39 static struct simple_fb gr_fbs[2];
     40 static unsigned gr_active_fb = 0;
     41 
     42 static int gr_fb_fd = -1;
     43 static int gr_vt_fd = -1;
     44 
     45 static struct fb_var_screeninfo vi;
     46 struct fb_fix_screeninfo fi;
     47 struct timespec tv, tv2;
     48 
     49 static void dumpinfo(struct fb_fix_screeninfo *fi,
     50                      struct fb_var_screeninfo *vi);
     51 
     52 static int get_framebuffer(struct simple_fb *fb, unsigned bpp)
     53 {
     54     int fd;
     55     void *bits;
     56     int bytes_per_pixel;
     57 
     58     fd = open("/dev/graphics/fb0", O_RDWR);
     59     if (fd < 0) {
     60         printf("cannot open /dev/graphics/fb0, retrying with /dev/fb0\n");
     61         if ((fd = open("/dev/fb0", O_RDWR)) < 0) {
     62             perror("cannot open /dev/fb0");
     63             return -1;
     64         }
     65     }
     66 
     67     if(ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
     68         perror("failed to get fb0 info");
     69         return -1;
     70     }
     71 
     72     if (bpp && vi.bits_per_pixel != bpp) {
     73         printf("bpp != %d, forcing...\n", bpp);
     74         vi.bits_per_pixel = bpp;
     75         if(ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
     76             perror("failed to force bpp");
     77             return -1;
     78         }
     79     }
     80 
     81     if(ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
     82         perror("failed to get fb0 info");
     83         return -1;
     84     }
     85 
     86     dumpinfo(&fi, &vi);
     87 
     88     bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
     89     if(bits == MAP_FAILED) {
     90         perror("failed to mmap framebuffer");
     91         return -1;
     92     }
     93 
     94     bytes_per_pixel = vi.bits_per_pixel >> 3;
     95 
     96     fb->width = vi.xres;
     97     fb->height = vi.yres;
     98     fb->stride = fi.line_length / bytes_per_pixel;
     99     fb->data = bits;
    100     fb->bpp = vi.bits_per_pixel;
    101 
    102     fb++;
    103 
    104     fb->width = vi.xres;
    105     fb->height = vi.yres;
    106     fb->stride = fi.line_length / bytes_per_pixel;
    107     fb->data = (void *)((unsigned long)bits +
    108                         vi.yres * vi.xres * bytes_per_pixel);
    109     fb->bpp = vi.bits_per_pixel;
    110 
    111     return fd;
    112 }
    113 
    114 static void set_active_framebuffer(unsigned n)
    115 {
    116     if(n > 1) return;
    117     vi.yres_virtual = vi.yres * 2;
    118     vi.yoffset = n * vi.yres;
    119     if(ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
    120         fprintf(stderr,"active fb swap failed!\n");
    121     } else
    122         printf("active buffer: %d\n", n);
    123 }
    124 
    125 static void dumpinfo(struct fb_fix_screeninfo *fi, struct fb_var_screeninfo *vi)
    126 {
    127     fprintf(stderr,"vi.xres = %d\n", vi->xres);
    128     fprintf(stderr,"vi.yres = %d\n", vi->yres);
    129     fprintf(stderr,"vi.xresv = %d\n", vi->xres_virtual);
    130     fprintf(stderr,"vi.yresv = %d\n", vi->yres_virtual);
    131     fprintf(stderr,"vi.xoff = %d\n", vi->xoffset);
    132     fprintf(stderr,"vi.yoff = %d\n", vi->yoffset);
    133     fprintf(stderr, "vi.bits_per_pixel = %d\n", vi->bits_per_pixel);
    134 
    135     fprintf(stderr, "fi.line_length = %d\n", fi->line_length);
    136 
    137 }
    138 
    139 int gr_init(int bpp, int id)
    140 {
    141     int fd = -1;
    142 
    143     if (!access("/dev/tty0", F_OK)) {
    144         fd = open("/dev/tty0", O_RDWR | O_SYNC);
    145         if(fd < 0)
    146             return -1;
    147 
    148         if(ioctl(fd, KDSETMODE, (void*) KD_GRAPHICS)) {
    149             close(fd);
    150             return -1;
    151         }
    152     }
    153 
    154     gr_fb_fd = get_framebuffer(gr_fbs, bpp);
    155 
    156     if(gr_fb_fd < 0) {
    157         if (fd >= 0) {
    158             ioctl(fd, KDSETMODE, (void*) KD_TEXT);
    159             close(fd);
    160         }
    161         return -1;
    162     }
    163 
    164     gr_vt_fd = fd;
    165 
    166         /* start with 0 as front (displayed) and 1 as back (drawing) */
    167     gr_active_fb = id;
    168     set_active_framebuffer(id);
    169 
    170     return 0;
    171 }
    172 
    173 void gr_exit(void)
    174 {
    175     close(gr_fb_fd);
    176     gr_fb_fd = -1;
    177 
    178     if (gr_vt_fd >= 0) {
    179         ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
    180         close(gr_vt_fd);
    181         gr_vt_fd = -1;
    182     }
    183 }
    184 
    185 int gr_fb_width(void)
    186 {
    187     return gr_fbs[0].width;
    188 }
    189 
    190 int gr_fb_height(void)
    191 {
    192     return gr_fbs[0].height;
    193 }
    194 
    195 uint16_t red = 0xf800;
    196 uint16_t green = 0x07e0;
    197 uint16_t blue = 0x001f;
    198 uint16_t white = 0xffff;
    199 uint16_t black = 0x0;
    200 
    201 uint32_t red32 = 0x00ff0000;
    202 uint32_t green32 = 0x0000ff00;
    203 uint32_t blue32 = 0x000000ff;
    204 uint32_t white32 = 0x00ffffff;
    205 uint32_t black32 = 0x0;
    206 
    207 void draw_grid(int w, int h, void* _loc) {
    208     int i, j;
    209     int stride = fi.line_length / (vi.bits_per_pixel >> 3);
    210     uint16_t *loc = _loc;
    211     uint32_t *loc32 = _loc;
    212 
    213     for (j = 0; j < h/2; j++) {
    214         for (i = 0; i < w/2; i++)
    215             if (vi.bits_per_pixel == 16)
    216                 loc[i + j*(stride)] = red;
    217             else
    218                 loc32[i + j*(stride)] = red32;
    219         for (; i < w; i++)
    220             if (vi.bits_per_pixel == 16)
    221                 loc[i + j*(stride)] = green;
    222             else
    223                 loc32[i + j*(stride)] = green32;
    224     }
    225 
    226     for (; j < h; j++) {
    227         for (i = 0; i < w/2; i++)
    228             if (vi.bits_per_pixel == 16)
    229                 loc[i + j*(stride)] = blue;
    230             else
    231                 loc32[i + j*(stride)] = blue32;
    232         for (; i < w; i++)
    233             if (vi.bits_per_pixel == 16)
    234                 loc[i + j*(stride)] = white;
    235             else
    236                 loc32[i + j*(stride)] = white32;
    237     }
    238 
    239 }
    240 
    241 void clear_screen(int w, int h, void* _loc)
    242 {
    243     int i,j;
    244     int stride = fi.line_length / (vi.bits_per_pixel >> 3);
    245     uint16_t *loc = _loc;
    246     uint32_t *loc32 = _loc;
    247 
    248     for (j = 0; j < h; j++)
    249         for (i = 0; i < w; i++)
    250             if (vi.bits_per_pixel == 16)
    251                 loc[i + j*(stride)] = black;
    252             else
    253                 loc32[i + j*(stride)] = black32;
    254 }
    255 
    256 int main(int argc, char **argv) {
    257   int w;
    258   int h;
    259   int id = 0;
    260   int bpp = 0;
    261 
    262   if (argc > 1)
    263       bpp = atoi(argv[1]);
    264 
    265   if (argc > 4)
    266       id = !!atoi(argv[4]);
    267 
    268   gr_init(bpp, id);
    269 
    270   if (argc > 3) {
    271       w = atoi(argv[2]);
    272       h = atoi(argv[3]);
    273   } else {
    274       w = vi.xres;
    275       h = vi.yres;
    276   }
    277 
    278   clear_screen(vi.xres, vi.yres, gr_fbs[0].data);
    279   clear_screen(vi.xres, vi.yres, gr_fbs[1].data);
    280 
    281   draw_grid(w, h, gr_fbs[id].data);
    282 
    283   set_active_framebuffer(!id);
    284   set_active_framebuffer(id);
    285 
    286   return 0;
    287 }
    288