blob: b79631a319dac1325a3351d8b7f745ddc76cb702 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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
27#include <linux/fb.h>
28#include <linux/kd.h>
29
30#include <pixelflinger/pixelflinger.h>
31
32#include "font_10x18.h"
33#include "minui.h"
34
Michael Ward9d1bcdf2011-06-22 14:30:34 -070035#if defined(RECOVERY_BGRA)
36#define PIXEL_FORMAT GGL_PIXEL_FORMAT_BGRA_8888
37#define PIXEL_SIZE 4
38#elif defined(RECOVERY_RGBX)
Doug Zongkerbe3e6f12011-01-13 16:43:44 -080039#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBX_8888
40#define PIXEL_SIZE 4
41#else
42#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGB_565
43#define PIXEL_SIZE 2
44#endif
45
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080046typedef struct {
47 GGLSurface texture;
48 unsigned cwidth;
49 unsigned cheight;
50 unsigned ascent;
51} GRFont;
52
53static GRFont *gr_font = 0;
54static GGLContext *gr_context = 0;
55static GGLSurface gr_font_texture;
56static GGLSurface gr_framebuffer[2];
57static GGLSurface gr_mem_surface;
58static unsigned gr_active_fb = 0;
59
60static int gr_fb_fd = -1;
61static int gr_vt_fd = -1;
62
63static struct fb_var_screeninfo vi;
Michael Ward9d1bcdf2011-06-22 14:30:34 -070064static struct fb_fix_screeninfo fi;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065
66static int get_framebuffer(GGLSurface *fb)
67{
68 int fd;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080069 void *bits;
70
71 fd = open("/dev/graphics/fb0", O_RDWR);
72 if (fd < 0) {
73 perror("cannot open fb0");
74 return -1;
75 }
76
Michael Ward3dbe66b2011-06-23 19:28:53 -070077 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080078 perror("failed to get fb0 info");
79 close(fd);
80 return -1;
81 }
82
Michael Ward3dbe66b2011-06-23 19:28:53 -070083 vi.bits_per_pixel = PIXEL_SIZE * 8;
84 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
85 vi.red.offset = 8;
86 vi.red.length = 8;
87 vi.green.offset = 16;
88 vi.green.length = 8;
89 vi.blue.offset = 24;
90 vi.blue.length = 8;
91 vi.transp.offset = 0;
92 vi.transp.length = 8;
93 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
94 vi.red.offset = 24;
95 vi.red.length = 8;
96 vi.green.offset = 16;
97 vi.green.length = 8;
98 vi.blue.offset = 8;
99 vi.blue.length = 8;
100 vi.transp.offset = 0;
101 vi.transp.length = 8;
102 } else { /* RGB565*/
103 vi.red.offset = 11;
104 vi.red.length = 5;
105 vi.green.offset = 5;
106 vi.green.length = 6;
107 vi.blue.offset = 0;
108 vi.blue.length = 5;
109 vi.transp.offset = 0;
110 vi.transp.length = 0;
111 }
112 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
113 perror("failed to put fb0 info");
114 close(fd);
115 return -1;
116 }
117
118 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800119 perror("failed to get fb0 info");
120 close(fd);
121 return -1;
122 }
123
124 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
125 if (bits == MAP_FAILED) {
126 perror("failed to mmap framebuffer");
127 close(fd);
128 return -1;
129 }
130
131 fb->version = sizeof(*fb);
132 fb->width = vi.xres;
133 fb->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700134 fb->stride = fi.line_length/PIXEL_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800135 fb->data = bits;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800136 fb->format = PIXEL_FORMAT;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700137 memset(fb->data, 0, vi.yres * fi.line_length);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800138
139 fb++;
140
141 fb->version = sizeof(*fb);
142 fb->width = vi.xres;
143 fb->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700144 fb->stride = fi.line_length/PIXEL_SIZE;
145 fb->data = (void*) (((unsigned) bits) + vi.yres * fi.line_length);
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800146 fb->format = PIXEL_FORMAT;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700147 memset(fb->data, 0, vi.yres * fi.line_length);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800148
149 return fd;
150}
151
152static void get_memory_surface(GGLSurface* ms) {
153 ms->version = sizeof(*ms);
154 ms->width = vi.xres;
155 ms->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700156 ms->stride = fi.line_length/PIXEL_SIZE;
157 ms->data = malloc(fi.line_length * vi.yres);
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800158 ms->format = PIXEL_FORMAT;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800159}
160
161static void set_active_framebuffer(unsigned n)
162{
163 if (n > 1) return;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800164 vi.yres_virtual = vi.yres * PIXEL_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800165 vi.yoffset = n * vi.yres;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800166 vi.bits_per_pixel = PIXEL_SIZE * 8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800167 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
168 perror("active fb swap failed");
169 }
170}
171
172void gr_flip(void)
173{
174 GGLContext *gl = gr_context;
175
176 /* swap front and back buffers */
177 gr_active_fb = (gr_active_fb + 1) & 1;
178
179 /* copy data from the in-memory surface to the buffer we're about
180 * to make active. */
181 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700182 fi.line_length * vi.yres);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800183
184 /* inform the display driver */
185 set_active_framebuffer(gr_active_fb);
186}
187
188void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
189{
190 GGLContext *gl = gr_context;
191 GGLint color[4];
192 color[0] = ((r << 8) | r) + 1;
193 color[1] = ((g << 8) | g) + 1;
194 color[2] = ((b << 8) | b) + 1;
195 color[3] = ((a << 8) | a) + 1;
196 gl->color4xv(gl, color);
197}
198
199int gr_measure(const char *s)
200{
201 return gr_font->cwidth * strlen(s);
202}
203
204int gr_text(int x, int y, const char *s)
205{
206 GGLContext *gl = gr_context;
207 GRFont *font = gr_font;
208 unsigned off;
209
210 y -= font->ascent;
211
212 gl->bindTexture(gl, &font->texture);
213 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
214 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
215 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
216 gl->enable(gl, GGL_TEXTURE_2D);
217
218 while((off = *s++)) {
219 off -= 32;
220 if (off < 96) {
221 gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y);
222 gl->recti(gl, x, y, x + font->cwidth, y + font->cheight);
223 }
224 x += font->cwidth;
225 }
226
227 return x;
228}
229
230void gr_fill(int x, int y, int w, int h)
231{
232 GGLContext *gl = gr_context;
233 gl->disable(gl, GGL_TEXTURE_2D);
234 gl->recti(gl, x, y, w, h);
235}
236
237void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
238 if (gr_context == NULL) {
239 return;
240 }
241 GGLContext *gl = gr_context;
242
243 gl->bindTexture(gl, (GGLSurface*) source);
244 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
245 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
246 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
247 gl->enable(gl, GGL_TEXTURE_2D);
248 gl->texCoord2i(gl, sx - dx, sy - dy);
249 gl->recti(gl, dx, dy, dx + w, dy + h);
250}
251
252unsigned int gr_get_width(gr_surface surface) {
253 if (surface == NULL) {
254 return 0;
255 }
256 return ((GGLSurface*) surface)->width;
257}
258
259unsigned int gr_get_height(gr_surface surface) {
260 if (surface == NULL) {
261 return 0;
262 }
263 return ((GGLSurface*) surface)->height;
264}
265
266static void gr_init_font(void)
267{
268 GGLSurface *ftex;
269 unsigned char *bits, *rle;
270 unsigned char *in, data;
271
272 gr_font = calloc(sizeof(*gr_font), 1);
273 ftex = &gr_font->texture;
274
275 bits = malloc(font.width * font.height);
276
277 ftex->version = sizeof(*ftex);
278 ftex->width = font.width;
279 ftex->height = font.height;
280 ftex->stride = font.width;
281 ftex->data = (void*) bits;
282 ftex->format = GGL_PIXEL_FORMAT_A_8;
283
284 in = font.rundata;
285 while((data = *in++)) {
286 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
287 bits += (data & 0x7f);
288 }
289
290 gr_font->cwidth = font.cwidth;
291 gr_font->cheight = font.cheight;
292 gr_font->ascent = font.cheight - 2;
293}
294
295int gr_init(void)
296{
297 gglInit(&gr_context);
298 GGLContext *gl = gr_context;
299
300 gr_init_font();
301 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
302 if (gr_vt_fd < 0) {
303 // This is non-fatal; post-Cupcake kernels don't have tty0.
304 perror("can't open /dev/tty0");
305 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
306 // However, if we do open tty0, we expect the ioctl to work.
307 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
308 gr_exit();
309 return -1;
310 }
311
312 gr_fb_fd = get_framebuffer(gr_framebuffer);
313 if (gr_fb_fd < 0) {
314 gr_exit();
315 return -1;
316 }
317
318 get_memory_surface(&gr_mem_surface);
319
320 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
321 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
322
323 /* start with 0 as front (displayed) and 1 as back (drawing) */
324 gr_active_fb = 0;
325 set_active_framebuffer(0);
326 gl->colorBuffer(gl, &gr_mem_surface);
327
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800328 gl->activeTexture(gl, 0);
329 gl->enable(gl, GGL_BLEND);
330 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
331
332 return 0;
333}
334
335void gr_exit(void)
336{
337 close(gr_fb_fd);
338 gr_fb_fd = -1;
339
340 free(gr_mem_surface.data);
341
342 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
343 close(gr_vt_fd);
344 gr_vt_fd = -1;
345}
346
347int gr_fb_width(void)
348{
349 return gr_framebuffer[0].width;
350}
351
352int gr_fb_height(void)
353{
354 return gr_framebuffer[0].height;
355}
356
357gr_pixel *gr_fb_data(void)
358{
359 return (unsigned short *) gr_mem_surface.data;
360}