blob: dc96c3b21ca7e280364ba14b4606bbbde6489442 [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
Devin Kim862d0262012-07-19 10:47:34 -070047#define NUM_BUFFERS 2
48
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049typedef struct {
50 GGLSurface texture;
51 unsigned cwidth;
52 unsigned cheight;
53 unsigned ascent;
54} GRFont;
55
56static GRFont *gr_font = 0;
57static GGLContext *gr_context = 0;
58static GGLSurface gr_font_texture;
Devin Kim862d0262012-07-19 10:47:34 -070059static GGLSurface gr_framebuffer[NUM_BUFFERS];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060static GGLSurface gr_mem_surface;
61static unsigned gr_active_fb = 0;
62
63static int gr_fb_fd = -1;
64static int gr_vt_fd = -1;
65
66static struct fb_var_screeninfo vi;
Michael Ward9d1bcdf2011-06-22 14:30:34 -070067static struct fb_fix_screeninfo fi;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068
69static int get_framebuffer(GGLSurface *fb)
70{
71 int fd;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080072 void *bits;
73
74 fd = open("/dev/graphics/fb0", O_RDWR);
75 if (fd < 0) {
76 perror("cannot open fb0");
77 return -1;
78 }
79
Michael Ward3dbe66b2011-06-23 19:28:53 -070080 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081 perror("failed to get fb0 info");
82 close(fd);
83 return -1;
84 }
85
Michael Ward3dbe66b2011-06-23 19:28:53 -070086 vi.bits_per_pixel = PIXEL_SIZE * 8;
87 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
88 vi.red.offset = 8;
89 vi.red.length = 8;
90 vi.green.offset = 16;
91 vi.green.length = 8;
92 vi.blue.offset = 24;
93 vi.blue.length = 8;
94 vi.transp.offset = 0;
95 vi.transp.length = 8;
96 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
97 vi.red.offset = 24;
98 vi.red.length = 8;
99 vi.green.offset = 16;
100 vi.green.length = 8;
101 vi.blue.offset = 8;
102 vi.blue.length = 8;
103 vi.transp.offset = 0;
104 vi.transp.length = 8;
105 } else { /* RGB565*/
106 vi.red.offset = 11;
107 vi.red.length = 5;
108 vi.green.offset = 5;
109 vi.green.length = 6;
110 vi.blue.offset = 0;
111 vi.blue.length = 5;
112 vi.transp.offset = 0;
113 vi.transp.length = 0;
114 }
115 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
116 perror("failed to put fb0 info");
117 close(fd);
118 return -1;
119 }
120
121 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800122 perror("failed to get fb0 info");
123 close(fd);
124 return -1;
125 }
126
127 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
128 if (bits == MAP_FAILED) {
129 perror("failed to mmap framebuffer");
130 close(fd);
131 return -1;
132 }
133
134 fb->version = sizeof(*fb);
135 fb->width = vi.xres;
136 fb->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700137 fb->stride = fi.line_length/PIXEL_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800138 fb->data = bits;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800139 fb->format = PIXEL_FORMAT;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700140 memset(fb->data, 0, vi.yres * fi.line_length);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800141
142 fb++;
143
144 fb->version = sizeof(*fb);
145 fb->width = vi.xres;
146 fb->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700147 fb->stride = fi.line_length/PIXEL_SIZE;
148 fb->data = (void*) (((unsigned) bits) + vi.yres * fi.line_length);
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800149 fb->format = PIXEL_FORMAT;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700150 memset(fb->data, 0, vi.yres * fi.line_length);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800151
152 return fd;
153}
154
155static void get_memory_surface(GGLSurface* ms) {
156 ms->version = sizeof(*ms);
157 ms->width = vi.xres;
158 ms->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700159 ms->stride = fi.line_length/PIXEL_SIZE;
160 ms->data = malloc(fi.line_length * vi.yres);
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800161 ms->format = PIXEL_FORMAT;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800162}
163
164static void set_active_framebuffer(unsigned n)
165{
166 if (n > 1) return;
Devin Kim862d0262012-07-19 10:47:34 -0700167 vi.yres_virtual = vi.yres * NUM_BUFFERS;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800168 vi.yoffset = n * vi.yres;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800169 vi.bits_per_pixel = PIXEL_SIZE * 8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800170 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
171 perror("active fb swap failed");
172 }
173}
174
175void gr_flip(void)
176{
177 GGLContext *gl = gr_context;
178
179 /* swap front and back buffers */
180 gr_active_fb = (gr_active_fb + 1) & 1;
181
182 /* copy data from the in-memory surface to the buffer we're about
183 * to make active. */
184 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700185 fi.line_length * vi.yres);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800186
187 /* inform the display driver */
188 set_active_framebuffer(gr_active_fb);
189}
190
191void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
192{
193 GGLContext *gl = gr_context;
194 GGLint color[4];
195 color[0] = ((r << 8) | r) + 1;
196 color[1] = ((g << 8) | g) + 1;
197 color[2] = ((b << 8) | b) + 1;
198 color[3] = ((a << 8) | a) + 1;
199 gl->color4xv(gl, color);
200}
201
202int gr_measure(const char *s)
203{
204 return gr_font->cwidth * strlen(s);
205}
206
Dima Zavin3c7f00e2011-08-30 11:58:24 -0700207void gr_font_size(int *x, int *y)
208{
209 *x = gr_font->cwidth;
210 *y = gr_font->cheight;
211}
212
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800213int gr_text(int x, int y, const char *s)
214{
215 GGLContext *gl = gr_context;
216 GRFont *font = gr_font;
217 unsigned off;
218
219 y -= font->ascent;
220
221 gl->bindTexture(gl, &font->texture);
222 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
223 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
224 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
225 gl->enable(gl, GGL_TEXTURE_2D);
226
227 while((off = *s++)) {
228 off -= 32;
229 if (off < 96) {
230 gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y);
231 gl->recti(gl, x, y, x + font->cwidth, y + font->cheight);
232 }
233 x += font->cwidth;
234 }
235
236 return x;
237}
238
239void gr_fill(int x, int y, int w, int h)
240{
241 GGLContext *gl = gr_context;
242 gl->disable(gl, GGL_TEXTURE_2D);
243 gl->recti(gl, x, y, w, h);
244}
245
246void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
247 if (gr_context == NULL) {
248 return;
249 }
250 GGLContext *gl = gr_context;
251
252 gl->bindTexture(gl, (GGLSurface*) source);
253 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
254 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
255 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
256 gl->enable(gl, GGL_TEXTURE_2D);
257 gl->texCoord2i(gl, sx - dx, sy - dy);
258 gl->recti(gl, dx, dy, dx + w, dy + h);
259}
260
261unsigned int gr_get_width(gr_surface surface) {
262 if (surface == NULL) {
263 return 0;
264 }
265 return ((GGLSurface*) surface)->width;
266}
267
268unsigned int gr_get_height(gr_surface surface) {
269 if (surface == NULL) {
270 return 0;
271 }
272 return ((GGLSurface*) surface)->height;
273}
274
275static void gr_init_font(void)
276{
277 GGLSurface *ftex;
278 unsigned char *bits, *rle;
279 unsigned char *in, data;
280
281 gr_font = calloc(sizeof(*gr_font), 1);
282 ftex = &gr_font->texture;
283
284 bits = malloc(font.width * font.height);
285
286 ftex->version = sizeof(*ftex);
287 ftex->width = font.width;
288 ftex->height = font.height;
289 ftex->stride = font.width;
290 ftex->data = (void*) bits;
291 ftex->format = GGL_PIXEL_FORMAT_A_8;
292
293 in = font.rundata;
294 while((data = *in++)) {
295 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
296 bits += (data & 0x7f);
297 }
298
299 gr_font->cwidth = font.cwidth;
300 gr_font->cheight = font.cheight;
301 gr_font->ascent = font.cheight - 2;
302}
303
304int gr_init(void)
305{
306 gglInit(&gr_context);
307 GGLContext *gl = gr_context;
308
309 gr_init_font();
310 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
311 if (gr_vt_fd < 0) {
312 // This is non-fatal; post-Cupcake kernels don't have tty0.
313 perror("can't open /dev/tty0");
314 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
315 // However, if we do open tty0, we expect the ioctl to work.
316 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
317 gr_exit();
318 return -1;
319 }
320
321 gr_fb_fd = get_framebuffer(gr_framebuffer);
322 if (gr_fb_fd < 0) {
323 gr_exit();
324 return -1;
325 }
326
327 get_memory_surface(&gr_mem_surface);
328
329 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
330 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
331
332 /* start with 0 as front (displayed) and 1 as back (drawing) */
333 gr_active_fb = 0;
334 set_active_framebuffer(0);
335 gl->colorBuffer(gl, &gr_mem_surface);
336
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800337 gl->activeTexture(gl, 0);
338 gl->enable(gl, GGL_BLEND);
339 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
340
Doug Zongkerf6abd402011-09-27 13:09:48 -0700341 gr_fb_blank(true);
342 gr_fb_blank(false);
343
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800344 return 0;
345}
346
347void gr_exit(void)
348{
349 close(gr_fb_fd);
350 gr_fb_fd = -1;
351
352 free(gr_mem_surface.data);
353
354 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
355 close(gr_vt_fd);
356 gr_vt_fd = -1;
357}
358
359int gr_fb_width(void)
360{
361 return gr_framebuffer[0].width;
362}
363
364int gr_fb_height(void)
365{
366 return gr_framebuffer[0].height;
367}
368
369gr_pixel *gr_fb_data(void)
370{
371 return (unsigned short *) gr_mem_surface.data;
372}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700373
374void gr_fb_blank(bool blank)
375{
376 int ret;
377
378 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
379 if (ret < 0)
380 perror("ioctl(): blank");
381}