blob: 57ca810f0b53073fbf10565e091557361f0d2dc5 [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
Dima Zavin3c7f00e2011-08-30 11:58:24 -0700204void gr_font_size(int *x, int *y)
205{
206 *x = gr_font->cwidth;
207 *y = gr_font->cheight;
208}
209
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800210int gr_text(int x, int y, const char *s)
211{
212 GGLContext *gl = gr_context;
213 GRFont *font = gr_font;
214 unsigned off;
215
216 y -= font->ascent;
217
218 gl->bindTexture(gl, &font->texture);
219 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
220 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
221 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
222 gl->enable(gl, GGL_TEXTURE_2D);
223
224 while((off = *s++)) {
225 off -= 32;
226 if (off < 96) {
227 gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y);
228 gl->recti(gl, x, y, x + font->cwidth, y + font->cheight);
229 }
230 x += font->cwidth;
231 }
232
233 return x;
234}
235
236void gr_fill(int x, int y, int w, int h)
237{
238 GGLContext *gl = gr_context;
239 gl->disable(gl, GGL_TEXTURE_2D);
240 gl->recti(gl, x, y, w, h);
241}
242
243void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
244 if (gr_context == NULL) {
245 return;
246 }
247 GGLContext *gl = gr_context;
248
249 gl->bindTexture(gl, (GGLSurface*) source);
250 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
251 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
252 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
253 gl->enable(gl, GGL_TEXTURE_2D);
254 gl->texCoord2i(gl, sx - dx, sy - dy);
255 gl->recti(gl, dx, dy, dx + w, dy + h);
256}
257
258unsigned int gr_get_width(gr_surface surface) {
259 if (surface == NULL) {
260 return 0;
261 }
262 return ((GGLSurface*) surface)->width;
263}
264
265unsigned int gr_get_height(gr_surface surface) {
266 if (surface == NULL) {
267 return 0;
268 }
269 return ((GGLSurface*) surface)->height;
270}
271
272static void gr_init_font(void)
273{
274 GGLSurface *ftex;
275 unsigned char *bits, *rle;
276 unsigned char *in, data;
277
278 gr_font = calloc(sizeof(*gr_font), 1);
279 ftex = &gr_font->texture;
280
281 bits = malloc(font.width * font.height);
282
283 ftex->version = sizeof(*ftex);
284 ftex->width = font.width;
285 ftex->height = font.height;
286 ftex->stride = font.width;
287 ftex->data = (void*) bits;
288 ftex->format = GGL_PIXEL_FORMAT_A_8;
289
290 in = font.rundata;
291 while((data = *in++)) {
292 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
293 bits += (data & 0x7f);
294 }
295
296 gr_font->cwidth = font.cwidth;
297 gr_font->cheight = font.cheight;
298 gr_font->ascent = font.cheight - 2;
299}
300
301int gr_init(void)
302{
303 gglInit(&gr_context);
304 GGLContext *gl = gr_context;
305
306 gr_init_font();
307 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
308 if (gr_vt_fd < 0) {
309 // This is non-fatal; post-Cupcake kernels don't have tty0.
310 perror("can't open /dev/tty0");
311 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
312 // However, if we do open tty0, we expect the ioctl to work.
313 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
314 gr_exit();
315 return -1;
316 }
317
318 gr_fb_fd = get_framebuffer(gr_framebuffer);
319 if (gr_fb_fd < 0) {
320 gr_exit();
321 return -1;
322 }
323
324 get_memory_surface(&gr_mem_surface);
325
326 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
327 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
328
329 /* start with 0 as front (displayed) and 1 as back (drawing) */
330 gr_active_fb = 0;
331 set_active_framebuffer(0);
332 gl->colorBuffer(gl, &gr_mem_surface);
333
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800334 gl->activeTexture(gl, 0);
335 gl->enable(gl, GGL_BLEND);
336 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
337
338 return 0;
339}
340
341void gr_exit(void)
342{
343 close(gr_fb_fd);
344 gr_fb_fd = -1;
345
346 free(gr_mem_surface.data);
347
348 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
349 close(gr_vt_fd);
350 gr_vt_fd = -1;
351}
352
353int gr_fb_width(void)
354{
355 return gr_framebuffer[0].width;
356}
357
358int gr_fb_height(void)
359{
360 return gr_framebuffer[0].height;
361}
362
363gr_pixel *gr_fb_data(void)
364{
365 return (unsigned short *) gr_mem_surface.data;
366}