forked from Tasssadar/multirom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframebuffer_generic.c
161 lines (136 loc) · 4.34 KB
/
framebuffer_generic.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
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
/*
* This file is part of MultiROM.
*
* MultiROM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MultiROM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MultiROM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <string.h>
#include <linux/fb.h>
#include "framebuffer.h"
#include "log.h"
#include "util.h"
// only double-buffering is implemented, this define is just
// for the code to know how many buffers we use
#define NUM_BUFFERS 2
struct fb_generic_data {
px_type *mapped[NUM_BUFFERS];
int active_buff;
};
static int impl_open(struct framebuffer *fb)
{
fb->vi.bits_per_pixel = PIXEL_SIZE * 8;
INFO("Pixel format: %dx%d @ %dbpp\n", fb->vi.xres, fb->vi.yres, fb->vi.bits_per_pixel);
#ifdef RECOVERY_BGRA
INFO("Pixel format: BGRA_8888\n");
fb->vi.red.offset = 8;
fb->vi.red.length = 8;
fb->vi.green.offset = 16;
fb->vi.green.length = 8;
fb->vi.blue.offset = 24;
fb->vi.blue.length = 8;
fb->vi.transp.offset = 0;
fb->vi.transp.length = 8;
#elif defined(RECOVERY_RGBX)
INFO("Pixel format: RGBX_8888\n");
fb->vi.red.offset = 24;
fb->vi.red.length = 8;
fb->vi.green.offset = 16;
fb->vi.green.length = 8;
fb->vi.blue.offset = 8;
fb->vi.blue.length = 8;
fb->vi.transp.offset = 0;
fb->vi.transp.length = 8;
#elif defined(RECOVERY_RGB_565)
INFO("Pixel format: RGB_565\n");
fb->vi.blue.offset = 0;
fb->vi.green.offset = 5;
fb->vi.red.offset = 11;
fb->vi.blue.length = 5;
fb->vi.green.length = 6;
fb->vi.red.length = 5;
fb->vi.blue.msb_right = 0;
fb->vi.green.msb_right = 0;
fb->vi.red.msb_right = 0;
fb->vi.transp.offset = 0;
fb->vi.transp.length = 0;
#else
#error "Unknown pixel format"
#endif
fb->vi.vmode = FB_VMODE_NONINTERLACED;
fb->vi.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
// mmap and memset to 0 before setting the vi to prevent screen flickering during init
px_type *mapped = mmap(0, fb->fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fb->fd, 0);
if (mapped == MAP_FAILED)
return -1;
memset(mapped, 0, fb->fi.smem_len);
munmap(mapped, fb->fi.smem_len);
if (ioctl(fb->fd, FBIOPUT_VSCREENINFO, &fb->vi) < 0)
{
ERROR("failed to set fb0 vi info");
return -1;
}
if (ioctl(fb->fd, FBIOGET_FSCREENINFO, &fb->fi) < 0)
return -1;
mapped = mmap(0, fb->fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fb->fd, 0);
if (mapped == MAP_FAILED)
return -1;
struct fb_generic_data *data = mzalloc(sizeof(struct fb_generic_data));
data->mapped[0] = mapped;
data->mapped[1] = (px_type*) (((uint8_t*)mapped) + (fb->vi.yres * fb->fi.line_length));
fb->impl_data = data;
return 0;
}
static void impl_close(struct framebuffer *fb)
{
struct fb_generic_data *data = fb->impl_data;
if(data)
{
munmap(data->mapped[0], fb->fi.smem_len);
free(data);
fb->impl_data = NULL;
}
}
static int impl_update(struct framebuffer *fb)
{
struct fb_generic_data *data = fb->impl_data;
fb->vi.yres_virtual = fb->vi.yres * NUM_BUFFERS;
fb->vi.yoffset = data->active_buff * fb->vi.yres;
if (ioctl(fb->fd, FBIOPUT_VSCREENINFO, &fb->vi) < 0)
{
ERROR("active fb swap failed");
return -1;
}
return 0;
}
static void *impl_get_frame_dest(struct framebuffer *fb)
{
struct fb_generic_data *data = fb->impl_data;
data->active_buff = !data->active_buff;
return data->mapped[data->active_buff];
}
const struct fb_impl fb_impl_generic = {
.name = "Generic",
.impl_id = FB_IMPL_GENERIC,
.open = impl_open,
.close = impl_close,
.update = impl_update,
.get_frame_dest = impl_get_frame_dest,
};