-
Notifications
You must be signed in to change notification settings - Fork 22
/
draw_framebuffer.c
96 lines (89 loc) · 2.29 KB
/
draw_framebuffer.c
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
/*
* draw_framebuffer.c
*
* Created on: Dec 24, 2015
* Author: Lincoln
*/
#include "draw_framebuffer.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <unistd.h>
static int fd = -1;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
static char* fbp = NULL;
static unsigned int screensize = 0;
void free_framebuffer
(
void
)
{
munmap(fbp, screensize);
close(fd);
}
void init_framebuffer
(
void
)
{
fd = open("/dev/fb0", O_RDWR);
if (fd == -1)
{
perror("Error opening framebuffer device");
exit(EXIT_FAILURE);
}
// Get fixed screen information
if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
{
perror("Error reading fixed information");
exit(EXIT_FAILURE);
}
// Get variable screen information
if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) == -1)
{
perror("Error reading variable information");
exit(EXIT_FAILURE);
}
screensize = (vinfo.xres * vinfo.yres * vinfo.bits_per_pixel) >> 3; /* (>>3): bits to bytes */
printf("xoffset: %d, yoffset: %d\nxres: %d, yres: %d\nbits_per_pixel: %d, line_length: %d\n",
vinfo.xoffset, vinfo.yoffset, vinfo.xres, vinfo.yres, vinfo.bits_per_pixel, finfo.line_length);
/*
vinfo.xres = 800;
vinfo.yres = 600;
if (ioctl(fd, FBIOPUT_VSCREENINFO, &vinfo) == -1)
{
perror("Error puting variable information");
exit(EXIT_FAILURE);
}
screensize = finfo.line_length * vinfo.yres;
printf("xoffset: %d, yoffset: %d\nxres: %d, yres: %d\nbits_per_pixel: %d, line_length: %d\n",
vinfo.xoffset, vinfo.yoffset, vinfo.xres, vinfo.yres, vinfo.bits_per_pixel, finfo.line_length);
*/
fbp = mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (fbp == (char *)-1)
{
perror("Error mapping framebuffer device to memory");
exit(EXIT_FAILURE);
}
}
void draw_framebuffer(unsigned char* src, int width, int height)
{
int x, y;
unsigned int location = 0;
int i = 0;
for(y = 0; y < height; y++)
{
for(x = 0; x < width; x++)
{
location = (x + vinfo.xoffset) * (vinfo.bits_per_pixel >> 3) + (y + vinfo.yoffset) * finfo.line_length;
*(fbp + location) = src[i*3]; //B
*(fbp + location + 1) = src[i*3 + 1]; //G
*(fbp + location + 2) = src[i*3 + 2]; //R
i++;
}
}
}