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 31struct simple_fb { 32 void *data; 33 int width; 34 int height; 35 int stride; 36 int bpp; 37}; 38 39static struct simple_fb gr_fbs[2]; 40static unsigned gr_active_fb = 0; 41 42static int gr_fb_fd = -1; 43static int gr_vt_fd = -1; 44 45static struct fb_var_screeninfo vi; 46struct fb_fix_screeninfo fi; 47struct timespec tv, tv2; 48 49static void dumpinfo(struct fb_fix_screeninfo *fi, 50 struct fb_var_screeninfo *vi); 51 52static 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 114static 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 125static 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 139int 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 173void 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 185int gr_fb_width(void) 186{ 187 return gr_fbs[0].width; 188} 189 190int gr_fb_height(void) 191{ 192 return gr_fbs[0].height; 193} 194 195uint16_t red = 0xf800; 196uint16_t green = 0x07e0; 197uint16_t blue = 0x001f; 198uint16_t white = 0xffff; 199uint16_t black = 0x0; 200 201uint32_t red32 = 0x00ff0000; 202uint32_t green32 = 0x0000ff00; 203uint32_t blue32 = 0x000000ff; 204uint32_t white32 = 0x00ffffff; 205uint32_t black32 = 0x0; 206 207void draw_grid(int w, int h, void* _loc) { 208 int i, j; 209 int v; 210 int stride = fi.line_length / (vi.bits_per_pixel >> 3); 211 uint16_t *loc = _loc; 212 uint32_t *loc32 = _loc; 213 214 for (j = 0; j < h/2; j++) { 215 for (i = 0; i < w/2; i++) 216 if (vi.bits_per_pixel == 16) 217 loc[i + j*(stride)] = red; 218 else 219 loc32[i + j*(stride)] = red32; 220 for (; i < w; i++) 221 if (vi.bits_per_pixel == 16) 222 loc[i + j*(stride)] = green; 223 else 224 loc32[i + j*(stride)] = green32; 225 } 226 227 for (; j < h; j++) { 228 for (i = 0; i < w/2; i++) 229 if (vi.bits_per_pixel == 16) 230 loc[i + j*(stride)] = blue; 231 else 232 loc32[i + j*(stride)] = blue32; 233 for (; i < w; i++) 234 if (vi.bits_per_pixel == 16) 235 loc[i + j*(stride)] = white; 236 else 237 loc32[i + j*(stride)] = white32; 238 } 239 240} 241 242void clear_screen(int w, int h, void* _loc) 243{ 244 int i,j; 245 int stride = fi.line_length / (vi.bits_per_pixel >> 3); 246 uint16_t *loc = _loc; 247 uint32_t *loc32 = _loc; 248 249 for (j = 0; j < h; j++) 250 for (i = 0; i < w; i++) 251 if (vi.bits_per_pixel == 16) 252 loc[i + j*(stride)] = black; 253 else 254 loc32[i + j*(stride)] = black32; 255} 256 257int main(int argc, char **argv) { 258 int w; 259 int h; 260 int id = 0; 261 int bpp = 0; 262 263 if (argc > 1) 264 bpp = atoi(argv[1]); 265 266 if (argc > 4) 267 id = !!atoi(argv[4]); 268 269 gr_init(bpp, id); 270 271 if (argc > 3) { 272 w = atoi(argv[2]); 273 h = atoi(argv[3]); 274 } else { 275 w = vi.xres; 276 h = vi.yres; 277 } 278 279 clear_screen(vi.xres, vi.yres, gr_fbs[0].data); 280 clear_screen(vi.xres, vi.yres, gr_fbs[1].data); 281 282 draw_grid(w, h, gr_fbs[id].data); 283 284 set_active_framebuffer(!id); 285 set_active_framebuffer(id); 286 287 return 0; 288} 289