fb_test.c revision acbb5bc99bccf7e625cc73fb1aaa90355bedb733
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#include <pixelflinger/pixelflinger.h> 32 33#include "minui.h" 34 35typedef struct { 36 GGLSurface texture; 37 unsigned cwidth; 38 unsigned cheight; 39 unsigned ascent; 40} GRFont; 41 42static GGLContext *gr_context = 0; 43static GGLSurface gr_framebuffer[2]; 44static unsigned gr_active_fb = 0; 45 46static int gr_fb_fd = -1; 47static int gr_vt_fd = -1; 48 49static struct fb_var_screeninfo vi; 50struct fb_fix_screeninfo fi; 51struct timespec tv, tv2; 52 53static void dumpinfo(struct fb_fix_screeninfo *fi, 54 struct fb_var_screeninfo *vi); 55 56static int get_framebuffer(GGLSurface *fb) 57{ 58 int fd; 59 void *bits; 60 61 fd = open("/dev/graphics/fb0", O_RDWR); 62 if (fd < 0) { 63 printf("cannot open /dev/graphics/fb0, retrying with /dev/fb0\n"); 64 if ((fd = open("/dev/fb0", O_RDWR)) < 0) { 65 perror("cannot open /dev/fb0"); 66 return -1; 67 } 68 } 69 70 if(ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { 71 perror("failed to get fb0 info"); 72 return -1; 73 } 74 75 if(ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) { 76 perror("failed to get fb0 info"); 77 return -1; 78 } 79 80 dumpinfo(&fi, &vi); 81 82 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 83 if(bits == MAP_FAILED) { 84 perror("failed to mmap framebuffer"); 85 return -1; 86 } 87 88 fb->version = sizeof(*fb); 89 fb->width = vi.xres; 90 fb->height = vi.yres; 91 fb->stride = fi.line_length / (vi.bits_per_pixel >> 3); 92 fb->data = bits; 93 fb->format = GGL_PIXEL_FORMAT_RGB_565; 94 95 fb++; 96 97 fb->version = sizeof(*fb); 98 fb->width = vi.xres; 99 fb->height = vi.yres; 100 fb->stride = fi.line_length / (vi.bits_per_pixel >> 3); 101 fb->data = (void*) (((unsigned) bits) + vi.yres * vi.xres * 2); 102 fb->format = GGL_PIXEL_FORMAT_RGB_565; 103 104 return fd; 105} 106 107static void set_active_framebuffer(unsigned n) 108{ 109 if(n > 1) return; 110 vi.yres_virtual = vi.yres * 2; 111 vi.yoffset = n * vi.yres; 112 if(ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) { 113 fprintf(stderr,"active fb swap failed!\n"); 114 } 115} 116 117static void dumpinfo(struct fb_fix_screeninfo *fi, struct fb_var_screeninfo *vi) 118{ 119 fprintf(stderr,"vi.xres = %d\n", vi->xres); 120 fprintf(stderr,"vi.yres = %d\n", vi->yres); 121 fprintf(stderr,"vi.xresv = %d\n", vi->xres_virtual); 122 fprintf(stderr,"vi.yresv = %d\n", vi->yres_virtual); 123 fprintf(stderr,"vi.xoff = %d\n", vi->xoffset); 124 fprintf(stderr,"vi.yoff = %d\n", vi->yoffset); 125 fprintf(stderr, "vi.bits_per_pixel = %d\n", vi->bits_per_pixel); 126 127 fprintf(stderr, "fi.line_length = %d\n", fi->line_length); 128 129} 130 131int gr_init(void) 132{ 133 int fd = -1; 134 135 if (!access("/dev/tty0", F_OK)) { 136 fd = open("/dev/tty0", O_RDWR | O_SYNC); 137 if(fd < 0) 138 return -1; 139 140 if(ioctl(fd, KDSETMODE, (void*) KD_GRAPHICS)) { 141 close(fd); 142 return -1; 143 } 144 } 145 146 gr_fb_fd = get_framebuffer(gr_framebuffer); 147 148 if(gr_fb_fd < 0) { 149 if (fd >= 0) { 150 ioctl(fd, KDSETMODE, (void*) KD_TEXT); 151 close(fd); 152 } 153 return -1; 154 } 155 156 gr_vt_fd = fd; 157 158 /* start with 0 as front (displayed) and 1 as back (drawing) */ 159 gr_active_fb = 0; 160 set_active_framebuffer(0); 161 162 return 0; 163} 164 165void gr_exit(void) 166{ 167 close(gr_fb_fd); 168 gr_fb_fd = -1; 169 170 if (gr_vt_fd >= 0) { 171 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT); 172 close(gr_vt_fd); 173 gr_vt_fd = -1; 174 } 175} 176 177int gr_fb_width(void) 178{ 179 return gr_framebuffer[0].width; 180} 181 182int gr_fb_height(void) 183{ 184 return gr_framebuffer[0].height; 185} 186 187uint16_t red = 0xf800; 188uint16_t green = 0x07e0; 189uint16_t blue = 0x001f; 190 191void draw_grid(int w, int h, uint16_t* loc) { 192 int i, j; 193 int v; 194 int stride = fi.line_length / (vi.bits_per_pixel >> 3); 195 196 for (j = 0; j < h/2; j++) { 197 for (i = 0; i < w/2; i++) 198 loc[i + j*(stride)] = red; 199 for (; i < w; i++) 200 loc[i + j*(stride)] = green; 201 } 202 for (; j < h; j++) { 203 for (i = 0; i < w/2; i++) 204 loc[i + j*(stride)] = blue; 205 for (; i < w; i++) 206 loc[i + j*(stride)] = 0xffff; 207 } 208 209} 210 211void clear_screen(int w, int h, uint16_t* loc) 212{ 213 int i,j; 214 int stride = fi.line_length / (vi.bits_per_pixel >> 3); 215 216 for (j = 0; j < h; j++) 217 for (i = 0; i < w; i++) 218 loc[i + j*(stride)] = 0x0000; 219} 220 221 222int main(int argc, char **argv) { 223 int w; 224 int h; 225 gr_init(); 226 w = vi.xres; 227 h = vi.yres; 228 clear_screen(w, h, (uint16_t *)gr_framebuffer[0].data); 229 230 if (argc > 2) { 231 w = atoi(argv[1]); 232 h = atoi(argv[2]); 233 } 234 235 draw_grid(w, h, (uint16_t *)gr_framebuffer[0].data); 236 printf("%lld\n", (tv2.tv_sec*1000000000LL + tv2.tv_nsec) - (tv.tv_sec*1000000000LL + tv.tv_nsec)); 237 set_active_framebuffer(1); 238 set_active_framebuffer(0); 239 240 return 0; 241} 242