blob: 296e2e078a38165750ebc8e4981758ee52e28946 [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
Dima Zavin4daf48a2011-08-30 11:59:20 -070017#include <stdbool.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080018#include <stdlib.h>
19#include <unistd.h>
20
21#include <fcntl.h>
22#include <stdio.h>
23
24#include <sys/ioctl.h>
25#include <sys/mman.h>
26#include <sys/types.h>
27
28#include <linux/fb.h>
29#include <linux/kd.h>
30
31#include <pixelflinger/pixelflinger.h>
32
33#include "font_10x18.h"
34#include "minui.h"
35
Michael Ward9d1bcdf2011-06-22 14:30:34 -070036#if defined(RECOVERY_BGRA)
37#define PIXEL_FORMAT GGL_PIXEL_FORMAT_BGRA_8888
38#define PIXEL_SIZE 4
39#elif defined(RECOVERY_RGBX)
Doug Zongkerbe3e6f12011-01-13 16:43:44 -080040#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBX_8888
41#define PIXEL_SIZE 4
42#else
43#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGB_565
44#define PIXEL_SIZE 2
45#endif
46
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047typedef struct {
48 GGLSurface texture;
49 unsigned cwidth;
50 unsigned cheight;
51 unsigned ascent;
52} GRFont;
53
54static GRFont *gr_font = 0;
55static GGLContext *gr_context = 0;
56static GGLSurface gr_font_texture;
57static GGLSurface gr_framebuffer[2];
58static GGLSurface gr_mem_surface;
59static unsigned gr_active_fb = 0;
Octavian Purdila0e348802011-07-01 17:57:45 +030060static unsigned double_buffering = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061
62static int gr_fb_fd = -1;
63static int gr_vt_fd = -1;
64
65static struct fb_var_screeninfo vi;
Michael Ward9d1bcdf2011-06-22 14:30:34 -070066static struct fb_fix_screeninfo fi;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080067
68static int get_framebuffer(GGLSurface *fb)
69{
70 int fd;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080071 void *bits;
72
73 fd = open("/dev/graphics/fb0", O_RDWR);
74 if (fd < 0) {
75 perror("cannot open fb0");
76 return -1;
77 }
78
Michael Ward3dbe66b2011-06-23 19:28:53 -070079 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080080 perror("failed to get fb0 info");
81 close(fd);
82 return -1;
83 }
84
Michael Ward3dbe66b2011-06-23 19:28:53 -070085 vi.bits_per_pixel = PIXEL_SIZE * 8;
86 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
87 vi.red.offset = 8;
88 vi.red.length = 8;
89 vi.green.offset = 16;
90 vi.green.length = 8;
91 vi.blue.offset = 24;
92 vi.blue.length = 8;
93 vi.transp.offset = 0;
94 vi.transp.length = 8;
95 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
96 vi.red.offset = 24;
97 vi.red.length = 8;
98 vi.green.offset = 16;
99 vi.green.length = 8;
100 vi.blue.offset = 8;
101 vi.blue.length = 8;
102 vi.transp.offset = 0;
103 vi.transp.length = 8;
104 } else { /* RGB565*/
105 vi.red.offset = 11;
106 vi.red.length = 5;
107 vi.green.offset = 5;
108 vi.green.length = 6;
109 vi.blue.offset = 0;
110 vi.blue.length = 5;
111 vi.transp.offset = 0;
112 vi.transp.length = 0;
113 }
114 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
115 perror("failed to put fb0 info");
116 close(fd);
117 return -1;
118 }
119
120 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800121 perror("failed to get fb0 info");
122 close(fd);
123 return -1;
124 }
125
126 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
127 if (bits == MAP_FAILED) {
128 perror("failed to mmap framebuffer");
129 close(fd);
130 return -1;
131 }
132
133 fb->version = sizeof(*fb);
134 fb->width = vi.xres;
135 fb->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700136 fb->stride = fi.line_length/PIXEL_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800137 fb->data = bits;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800138 fb->format = PIXEL_FORMAT;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700139 memset(fb->data, 0, vi.yres * fi.line_length);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140
141 fb++;
142
Octavian Purdila0e348802011-07-01 17:57:45 +0300143 /* check if we can use double buffering */
144 if (vi.yres * fi.line_length * 2 > fi.smem_len)
145 return fd;
146
147 double_buffering = 1;
148
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800149 fb->version = sizeof(*fb);
150 fb->width = vi.xres;
151 fb->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700152 fb->stride = fi.line_length/PIXEL_SIZE;
153 fb->data = (void*) (((unsigned) bits) + vi.yres * fi.line_length);
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800154 fb->format = PIXEL_FORMAT;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700155 memset(fb->data, 0, vi.yres * fi.line_length);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800156
157 return fd;
158}
159
160static void get_memory_surface(GGLSurface* ms) {
161 ms->version = sizeof(*ms);
162 ms->width = vi.xres;
163 ms->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700164 ms->stride = fi.line_length/PIXEL_SIZE;
165 ms->data = malloc(fi.line_length * vi.yres);
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800166 ms->format = PIXEL_FORMAT;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800167}
168
169static void set_active_framebuffer(unsigned n)
170{
Octavian Purdila0e348802011-07-01 17:57:45 +0300171 if (n > 1 || !double_buffering) return;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800172 vi.yres_virtual = vi.yres * PIXEL_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800173 vi.yoffset = n * vi.yres;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800174 vi.bits_per_pixel = PIXEL_SIZE * 8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800175 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
176 perror("active fb swap failed");
177 }
178}
179
180void gr_flip(void)
181{
182 GGLContext *gl = gr_context;
183
184 /* swap front and back buffers */
Octavian Purdila0e348802011-07-01 17:57:45 +0300185 if (double_buffering)
186 gr_active_fb = (gr_active_fb + 1) & 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800187
188 /* copy data from the in-memory surface to the buffer we're about
189 * to make active. */
190 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700191 fi.line_length * vi.yres);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800192
193 /* inform the display driver */
194 set_active_framebuffer(gr_active_fb);
195}
196
197void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
198{
199 GGLContext *gl = gr_context;
200 GGLint color[4];
201 color[0] = ((r << 8) | r) + 1;
202 color[1] = ((g << 8) | g) + 1;
203 color[2] = ((b << 8) | b) + 1;
204 color[3] = ((a << 8) | a) + 1;
205 gl->color4xv(gl, color);
206}
207
208int gr_measure(const char *s)
209{
210 return gr_font->cwidth * strlen(s);
211}
212
Dima Zavin3c7f00e2011-08-30 11:58:24 -0700213void gr_font_size(int *x, int *y)
214{
215 *x = gr_font->cwidth;
216 *y = gr_font->cheight;
217}
218
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800219int gr_text(int x, int y, const char *s)
220{
221 GGLContext *gl = gr_context;
222 GRFont *font = gr_font;
223 unsigned off;
224
225 y -= font->ascent;
226
227 gl->bindTexture(gl, &font->texture);
228 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
229 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
230 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
231 gl->enable(gl, GGL_TEXTURE_2D);
232
233 while((off = *s++)) {
234 off -= 32;
235 if (off < 96) {
236 gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y);
237 gl->recti(gl, x, y, x + font->cwidth, y + font->cheight);
238 }
239 x += font->cwidth;
240 }
241
242 return x;
243}
244
245void gr_fill(int x, int y, int w, int h)
246{
247 GGLContext *gl = gr_context;
248 gl->disable(gl, GGL_TEXTURE_2D);
249 gl->recti(gl, x, y, w, h);
250}
251
252void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
253 if (gr_context == NULL) {
254 return;
255 }
256 GGLContext *gl = gr_context;
257
258 gl->bindTexture(gl, (GGLSurface*) source);
259 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
260 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
261 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
262 gl->enable(gl, GGL_TEXTURE_2D);
263 gl->texCoord2i(gl, sx - dx, sy - dy);
264 gl->recti(gl, dx, dy, dx + w, dy + h);
265}
266
267unsigned int gr_get_width(gr_surface surface) {
268 if (surface == NULL) {
269 return 0;
270 }
271 return ((GGLSurface*) surface)->width;
272}
273
274unsigned int gr_get_height(gr_surface surface) {
275 if (surface == NULL) {
276 return 0;
277 }
278 return ((GGLSurface*) surface)->height;
279}
280
281static void gr_init_font(void)
282{
283 GGLSurface *ftex;
284 unsigned char *bits, *rle;
285 unsigned char *in, data;
286
287 gr_font = calloc(sizeof(*gr_font), 1);
288 ftex = &gr_font->texture;
289
290 bits = malloc(font.width * font.height);
291
292 ftex->version = sizeof(*ftex);
293 ftex->width = font.width;
294 ftex->height = font.height;
295 ftex->stride = font.width;
296 ftex->data = (void*) bits;
297 ftex->format = GGL_PIXEL_FORMAT_A_8;
298
299 in = font.rundata;
300 while((data = *in++)) {
301 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
302 bits += (data & 0x7f);
303 }
304
305 gr_font->cwidth = font.cwidth;
306 gr_font->cheight = font.cheight;
307 gr_font->ascent = font.cheight - 2;
308}
309
310int gr_init(void)
311{
312 gglInit(&gr_context);
313 GGLContext *gl = gr_context;
314
315 gr_init_font();
316 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
317 if (gr_vt_fd < 0) {
318 // This is non-fatal; post-Cupcake kernels don't have tty0.
319 perror("can't open /dev/tty0");
320 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
321 // However, if we do open tty0, we expect the ioctl to work.
322 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
323 gr_exit();
324 return -1;
325 }
326
327 gr_fb_fd = get_framebuffer(gr_framebuffer);
328 if (gr_fb_fd < 0) {
329 gr_exit();
330 return -1;
331 }
332
333 get_memory_surface(&gr_mem_surface);
334
335 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
336 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
337
338 /* start with 0 as front (displayed) and 1 as back (drawing) */
339 gr_active_fb = 0;
340 set_active_framebuffer(0);
341 gl->colorBuffer(gl, &gr_mem_surface);
342
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800343 gl->activeTexture(gl, 0);
344 gl->enable(gl, GGL_BLEND);
345 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
346
Doug Zongkerf6abd402011-09-27 13:09:48 -0700347 gr_fb_blank(true);
348 gr_fb_blank(false);
349
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800350 return 0;
351}
352
353void gr_exit(void)
354{
355 close(gr_fb_fd);
356 gr_fb_fd = -1;
357
358 free(gr_mem_surface.data);
359
360 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
361 close(gr_vt_fd);
362 gr_vt_fd = -1;
363}
364
365int gr_fb_width(void)
366{
367 return gr_framebuffer[0].width;
368}
369
370int gr_fb_height(void)
371{
372 return gr_framebuffer[0].height;
373}
374
375gr_pixel *gr_fb_data(void)
376{
377 return (unsigned short *) gr_mem_surface.data;
378}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700379
380void gr_fb_blank(bool blank)
381{
382 int ret;
383
384 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
385 if (ret < 0)
386 perror("ioctl(): blank");
387}