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