blob: 81f13ad2ce86aa62997a361ddae830a315def02c [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;
Octavian Purdila0e348802011-07-01 17:57:45 +030062static unsigned double_buffering = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080063
64static int gr_fb_fd = -1;
65static int gr_vt_fd = -1;
66
67static struct fb_var_screeninfo vi;
Michael Ward9d1bcdf2011-06-22 14:30:34 -070068static struct fb_fix_screeninfo fi;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080069
70static int get_framebuffer(GGLSurface *fb)
71{
72 int fd;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073 void *bits;
74
75 fd = open("/dev/graphics/fb0", O_RDWR);
76 if (fd < 0) {
77 perror("cannot open fb0");
78 return -1;
79 }
80
Michael Ward3dbe66b2011-06-23 19:28:53 -070081 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080082 perror("failed to get fb0 info");
83 close(fd);
84 return -1;
85 }
86
Michael Ward3dbe66b2011-06-23 19:28:53 -070087 vi.bits_per_pixel = PIXEL_SIZE * 8;
88 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
89 vi.red.offset = 8;
90 vi.red.length = 8;
91 vi.green.offset = 16;
92 vi.green.length = 8;
93 vi.blue.offset = 24;
94 vi.blue.length = 8;
95 vi.transp.offset = 0;
96 vi.transp.length = 8;
97 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
98 vi.red.offset = 24;
99 vi.red.length = 8;
100 vi.green.offset = 16;
101 vi.green.length = 8;
102 vi.blue.offset = 8;
103 vi.blue.length = 8;
104 vi.transp.offset = 0;
105 vi.transp.length = 8;
106 } else { /* RGB565*/
107 vi.red.offset = 11;
108 vi.red.length = 5;
109 vi.green.offset = 5;
110 vi.green.length = 6;
111 vi.blue.offset = 0;
112 vi.blue.length = 5;
113 vi.transp.offset = 0;
114 vi.transp.length = 0;
115 }
116 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
117 perror("failed to put fb0 info");
118 close(fd);
119 return -1;
120 }
121
122 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800123 perror("failed to get fb0 info");
124 close(fd);
125 return -1;
126 }
127
128 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
129 if (bits == MAP_FAILED) {
130 perror("failed to mmap framebuffer");
131 close(fd);
132 return -1;
133 }
134
135 fb->version = sizeof(*fb);
136 fb->width = vi.xres;
137 fb->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700138 fb->stride = fi.line_length/PIXEL_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800139 fb->data = bits;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800140 fb->format = PIXEL_FORMAT;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700141 memset(fb->data, 0, vi.yres * fi.line_length);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800142
143 fb++;
144
Octavian Purdila0e348802011-07-01 17:57:45 +0300145 /* check if we can use double buffering */
146 if (vi.yres * fi.line_length * 2 > fi.smem_len)
147 return fd;
148
149 double_buffering = 1;
150
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800151 fb->version = sizeof(*fb);
152 fb->width = vi.xres;
153 fb->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700154 fb->stride = fi.line_length/PIXEL_SIZE;
155 fb->data = (void*) (((unsigned) bits) + vi.yres * fi.line_length);
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800156 fb->format = PIXEL_FORMAT;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700157 memset(fb->data, 0, vi.yres * fi.line_length);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800158
159 return fd;
160}
161
162static void get_memory_surface(GGLSurface* ms) {
163 ms->version = sizeof(*ms);
164 ms->width = vi.xres;
165 ms->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700166 ms->stride = fi.line_length/PIXEL_SIZE;
167 ms->data = malloc(fi.line_length * vi.yres);
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800168 ms->format = PIXEL_FORMAT;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800169}
170
171static void set_active_framebuffer(unsigned n)
172{
Octavian Purdila0e348802011-07-01 17:57:45 +0300173 if (n > 1 || !double_buffering) return;
Devin Kim862d0262012-07-19 10:47:34 -0700174 vi.yres_virtual = vi.yres * NUM_BUFFERS;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800175 vi.yoffset = n * vi.yres;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800176 vi.bits_per_pixel = PIXEL_SIZE * 8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800177 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
178 perror("active fb swap failed");
179 }
180}
181
182void gr_flip(void)
183{
184 GGLContext *gl = gr_context;
185
186 /* swap front and back buffers */
Octavian Purdila0e348802011-07-01 17:57:45 +0300187 if (double_buffering)
188 gr_active_fb = (gr_active_fb + 1) & 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800189
190 /* copy data from the in-memory surface to the buffer we're about
191 * to make active. */
192 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700193 fi.line_length * vi.yres);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800194
195 /* inform the display driver */
196 set_active_framebuffer(gr_active_fb);
197}
198
199void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
200{
201 GGLContext *gl = gr_context;
202 GGLint color[4];
203 color[0] = ((r << 8) | r) + 1;
204 color[1] = ((g << 8) | g) + 1;
205 color[2] = ((b << 8) | b) + 1;
206 color[3] = ((a << 8) | a) + 1;
207 gl->color4xv(gl, color);
208}
209
210int gr_measure(const char *s)
211{
212 return gr_font->cwidth * strlen(s);
213}
214
Dima Zavin3c7f00e2011-08-30 11:58:24 -0700215void gr_font_size(int *x, int *y)
216{
217 *x = gr_font->cwidth;
218 *y = gr_font->cheight;
219}
220
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800221int gr_text(int x, int y, const char *s)
222{
223 GGLContext *gl = gr_context;
224 GRFont *font = gr_font;
225 unsigned off;
226
227 y -= font->ascent;
228
229 gl->bindTexture(gl, &font->texture);
230 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
231 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
232 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
233 gl->enable(gl, GGL_TEXTURE_2D);
234
235 while((off = *s++)) {
236 off -= 32;
237 if (off < 96) {
238 gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y);
239 gl->recti(gl, x, y, x + font->cwidth, y + font->cheight);
240 }
241 x += font->cwidth;
242 }
243
244 return x;
245}
246
247void gr_fill(int x, int y, int w, int h)
248{
249 GGLContext *gl = gr_context;
250 gl->disable(gl, GGL_TEXTURE_2D);
251 gl->recti(gl, x, y, w, h);
252}
253
254void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
255 if (gr_context == NULL) {
256 return;
257 }
258 GGLContext *gl = gr_context;
259
260 gl->bindTexture(gl, (GGLSurface*) source);
261 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
262 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
263 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
264 gl->enable(gl, GGL_TEXTURE_2D);
265 gl->texCoord2i(gl, sx - dx, sy - dy);
266 gl->recti(gl, dx, dy, dx + w, dy + h);
267}
268
269unsigned int gr_get_width(gr_surface surface) {
270 if (surface == NULL) {
271 return 0;
272 }
273 return ((GGLSurface*) surface)->width;
274}
275
276unsigned int gr_get_height(gr_surface surface) {
277 if (surface == NULL) {
278 return 0;
279 }
280 return ((GGLSurface*) surface)->height;
281}
282
283static void gr_init_font(void)
284{
285 GGLSurface *ftex;
286 unsigned char *bits, *rle;
287 unsigned char *in, data;
288
289 gr_font = calloc(sizeof(*gr_font), 1);
290 ftex = &gr_font->texture;
291
292 bits = malloc(font.width * font.height);
293
294 ftex->version = sizeof(*ftex);
295 ftex->width = font.width;
296 ftex->height = font.height;
297 ftex->stride = font.width;
298 ftex->data = (void*) bits;
299 ftex->format = GGL_PIXEL_FORMAT_A_8;
300
301 in = font.rundata;
302 while((data = *in++)) {
303 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
304 bits += (data & 0x7f);
305 }
306
307 gr_font->cwidth = font.cwidth;
308 gr_font->cheight = font.cheight;
309 gr_font->ascent = font.cheight - 2;
310}
311
312int gr_init(void)
313{
314 gglInit(&gr_context);
315 GGLContext *gl = gr_context;
316
317 gr_init_font();
318 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
319 if (gr_vt_fd < 0) {
320 // This is non-fatal; post-Cupcake kernels don't have tty0.
321 perror("can't open /dev/tty0");
322 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
323 // However, if we do open tty0, we expect the ioctl to work.
324 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
325 gr_exit();
326 return -1;
327 }
328
329 gr_fb_fd = get_framebuffer(gr_framebuffer);
330 if (gr_fb_fd < 0) {
331 gr_exit();
332 return -1;
333 }
334
335 get_memory_surface(&gr_mem_surface);
336
337 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
338 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
339
340 /* start with 0 as front (displayed) and 1 as back (drawing) */
341 gr_active_fb = 0;
342 set_active_framebuffer(0);
343 gl->colorBuffer(gl, &gr_mem_surface);
344
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800345 gl->activeTexture(gl, 0);
346 gl->enable(gl, GGL_BLEND);
347 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
348
Doug Zongkerf6abd402011-09-27 13:09:48 -0700349 gr_fb_blank(true);
350 gr_fb_blank(false);
351
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800352 return 0;
353}
354
355void gr_exit(void)
356{
357 close(gr_fb_fd);
358 gr_fb_fd = -1;
359
360 free(gr_mem_surface.data);
361
362 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
363 close(gr_vt_fd);
364 gr_vt_fd = -1;
365}
366
367int gr_fb_width(void)
368{
369 return gr_framebuffer[0].width;
370}
371
372int gr_fb_height(void)
373{
374 return gr_framebuffer[0].height;
375}
376
377gr_pixel *gr_fb_data(void)
378{
379 return (unsigned short *) gr_mem_surface.data;
380}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700381
382void gr_fb_blank(bool blank)
383{
384 int ret;
385
386 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
387 if (ret < 0)
388 perror("ioctl(): blank");
389}