File: fb_test.c

package info (click to toggle)
android-platform-system-extras 10.0.0%2Br36%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,588 kB
  • sloc: cpp: 45,171; python: 10,047; ansic: 8,169; sh: 4,029; java: 2,239; javascript: 1,752; xml: 991; asm: 169; makefile: 13
file content (287 lines) | stat: -rw-r--r-- 6,776 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <unistd.h>

#include <fcntl.h>
#include <stdio.h>

#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <time.h>

#include <linux/fb.h>
#include <linux/kd.h>

struct simple_fb {
    void *data;
    int width;
    int height;
    int stride;
    int bpp;
};

static struct simple_fb gr_fbs[2];
static unsigned gr_active_fb = 0;

static int gr_fb_fd = -1;
static int gr_vt_fd = -1;

static struct fb_var_screeninfo vi;
struct fb_fix_screeninfo fi;
struct timespec tv, tv2;

static void dumpinfo(struct fb_fix_screeninfo *fi,
                     struct fb_var_screeninfo *vi);

static int get_framebuffer(struct simple_fb *fb, unsigned bpp)
{
    int fd;
    void *bits;
    int bytes_per_pixel;

    fd = open("/dev/graphics/fb0", O_RDWR);
    if (fd < 0) {
        printf("cannot open /dev/graphics/fb0, retrying with /dev/fb0\n");
        if ((fd = open("/dev/fb0", O_RDWR)) < 0) {
            perror("cannot open /dev/fb0");
            return -1;
        }
    }

    if(ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
        perror("failed to get fb0 info");
        return -1;
    }

    if (bpp && vi.bits_per_pixel != bpp) {
        printf("bpp != %d, forcing...\n", bpp);
        vi.bits_per_pixel = bpp;
        if(ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
            perror("failed to force bpp");
            return -1;
        }
    }

    if(ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
        perror("failed to get fb0 info");
        return -1;
    }

    dumpinfo(&fi, &vi);

    bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(bits == MAP_FAILED) {
        perror("failed to mmap framebuffer");
        return -1;
    }

    bytes_per_pixel = vi.bits_per_pixel >> 3;

    fb->width = vi.xres;
    fb->height = vi.yres;
    fb->stride = fi.line_length / bytes_per_pixel;
    fb->data = bits;
    fb->bpp = vi.bits_per_pixel;

    fb++;

    fb->width = vi.xres;
    fb->height = vi.yres;
    fb->stride = fi.line_length / bytes_per_pixel;
    fb->data = (void *)((unsigned long)bits +
                        vi.yres * vi.xres * bytes_per_pixel);
    fb->bpp = vi.bits_per_pixel;

    return fd;
}

static void set_active_framebuffer(unsigned n)
{
    if(n > 1) return;
    vi.yres_virtual = vi.yres * 2;
    vi.yoffset = n * vi.yres;
    if(ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
        fprintf(stderr,"active fb swap failed!\n");
    } else
        printf("active buffer: %d\n", n);
}

static void dumpinfo(struct fb_fix_screeninfo *fi, struct fb_var_screeninfo *vi)
{
    fprintf(stderr,"vi.xres = %d\n", vi->xres);
    fprintf(stderr,"vi.yres = %d\n", vi->yres);
    fprintf(stderr,"vi.xresv = %d\n", vi->xres_virtual);
    fprintf(stderr,"vi.yresv = %d\n", vi->yres_virtual);
    fprintf(stderr,"vi.xoff = %d\n", vi->xoffset);
    fprintf(stderr,"vi.yoff = %d\n", vi->yoffset);
    fprintf(stderr, "vi.bits_per_pixel = %d\n", vi->bits_per_pixel);

    fprintf(stderr, "fi.line_length = %d\n", fi->line_length);

}

int gr_init(int bpp, int id)
{
    int fd = -1;

    if (!access("/dev/tty0", F_OK)) {
        fd = open("/dev/tty0", O_RDWR | O_SYNC);
        if(fd < 0)
            return -1;

        if(ioctl(fd, KDSETMODE, (void*) KD_GRAPHICS)) {
            close(fd);
            return -1;
        }
    }

    gr_fb_fd = get_framebuffer(gr_fbs, bpp);

    if(gr_fb_fd < 0) {
        if (fd >= 0) {
            ioctl(fd, KDSETMODE, (void*) KD_TEXT);
            close(fd);
        }
        return -1;
    }

    gr_vt_fd = fd;

        /* start with 0 as front (displayed) and 1 as back (drawing) */
    gr_active_fb = id;
    set_active_framebuffer(id);

    return 0;
}

void gr_exit(void)
{
    close(gr_fb_fd);
    gr_fb_fd = -1;

    if (gr_vt_fd >= 0) {
        ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
        close(gr_vt_fd);
        gr_vt_fd = -1;
    }
}

int gr_fb_width(void)
{
    return gr_fbs[0].width;
}

int gr_fb_height(void)
{
    return gr_fbs[0].height;
}

uint16_t red = 0xf800;
uint16_t green = 0x07e0;
uint16_t blue = 0x001f;
uint16_t white = 0xffff;
uint16_t black = 0x0;

uint32_t red32 = 0x00ff0000;
uint32_t green32 = 0x0000ff00;
uint32_t blue32 = 0x000000ff;
uint32_t white32 = 0x00ffffff;
uint32_t black32 = 0x0;

void draw_grid(int w, int h, void* _loc) {
    int i, j;
    int stride = fi.line_length / (vi.bits_per_pixel >> 3);
    uint16_t *loc = _loc;
    uint32_t *loc32 = _loc;

    for (j = 0; j < h/2; j++) {
        for (i = 0; i < w/2; i++)
            if (vi.bits_per_pixel == 16)
                loc[i + j*(stride)] = red;
            else
                loc32[i + j*(stride)] = red32;
        for (; i < w; i++)
            if (vi.bits_per_pixel == 16)
                loc[i + j*(stride)] = green;
            else
                loc32[i + j*(stride)] = green32;
    }

    for (; j < h; j++) {
        for (i = 0; i < w/2; i++)
            if (vi.bits_per_pixel == 16)
                loc[i + j*(stride)] = blue;
            else
                loc32[i + j*(stride)] = blue32;
        for (; i < w; i++)
            if (vi.bits_per_pixel == 16)
                loc[i + j*(stride)] = white;
            else
                loc32[i + j*(stride)] = white32;
    }

}

void clear_screen(int w, int h, void* _loc)
{
    int i,j;
    int stride = fi.line_length / (vi.bits_per_pixel >> 3);
    uint16_t *loc = _loc;
    uint32_t *loc32 = _loc;

    for (j = 0; j < h; j++)
        for (i = 0; i < w; i++)
            if (vi.bits_per_pixel == 16)
                loc[i + j*(stride)] = black;
            else
                loc32[i + j*(stride)] = black32;
}

int main(int argc, char **argv) {
  int w;
  int h;
  int id = 0;
  int bpp = 0;

  if (argc > 1)
      bpp = atoi(argv[1]);

  if (argc > 4)
      id = !!atoi(argv[4]);

  gr_init(bpp, id);

  if (argc > 3) {
      w = atoi(argv[2]);
      h = atoi(argv[3]);
  } else {
      w = vi.xres;
      h = vi.yres;
  }

  clear_screen(vi.xres, vi.yres, gr_fbs[0].data);
  clear_screen(vi.xres, vi.yres, gr_fbs[1].data);

  draw_grid(w, h, gr_fbs[id].data);

  set_active_framebuffer(!id);
  set_active_framebuffer(id);

  return 0;
}