blob: d3473db3d5e37ef6372e7d222d06f07cea79ef17 [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
CEnnis91de338e52012-01-14 23:15:17 -050033#if defined(BOARD_XHDPI_RECOVERY)
34 #ifdef BOARD_USE_CUSTOM_FONT
35 #include "roboto_15x24.h"
36 #else
37 #include "font_10x18.h" // only use the big font if we want custom
38 #endif
39#elif defined(BOARD_HDPI_RECOVERY)
40 #ifdef BOARD_USE_CUSTOM_FONT
41 #include "roboto_10x18.h"
42 #else
43 #include "font_10x18.h"
44 #endif
45#elif defined(BOARD_LDPI_RECOVERY)
46 #include "font_7x16.h"
47#else
48 #ifdef BOARD_USE_CUSTOM_FONT
49 #include "roboto_10x18.h"
50 #else
51 #include "font_10x18.h"
52 #endif
53#endif
54
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055#include "minui.h"
56
Michael Ward9d1bcdf2011-06-22 14:30:34 -070057#if defined(RECOVERY_BGRA)
58#define PIXEL_FORMAT GGL_PIXEL_FORMAT_BGRA_8888
59#define PIXEL_SIZE 4
60#elif defined(RECOVERY_RGBX)
Doug Zongkerbe3e6f12011-01-13 16:43:44 -080061#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBX_8888
62#define PIXEL_SIZE 4
63#else
64#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGB_565
65#define PIXEL_SIZE 2
66#endif
67
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068typedef struct {
69 GGLSurface texture;
70 unsigned cwidth;
71 unsigned cheight;
72 unsigned ascent;
73} GRFont;
74
75static GRFont *gr_font = 0;
76static GGLContext *gr_context = 0;
77static GGLSurface gr_font_texture;
78static GGLSurface gr_framebuffer[2];
79static GGLSurface gr_mem_surface;
80static unsigned gr_active_fb = 0;
81
82static int gr_fb_fd = -1;
83static int gr_vt_fd = -1;
84
85static struct fb_var_screeninfo vi;
Michael Ward9d1bcdf2011-06-22 14:30:34 -070086static struct fb_fix_screeninfo fi;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080087
88static int get_framebuffer(GGLSurface *fb)
89{
90 int fd;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080091 void *bits;
92
93 fd = open("/dev/graphics/fb0", O_RDWR);
94 if (fd < 0) {
95 perror("cannot open fb0");
96 return -1;
97 }
98
Michael Ward3dbe66b2011-06-23 19:28:53 -070099 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800100 perror("failed to get fb0 info");
101 close(fd);
102 return -1;
103 }
104
Michael Ward3dbe66b2011-06-23 19:28:53 -0700105 vi.bits_per_pixel = PIXEL_SIZE * 8;
106 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
107 vi.red.offset = 8;
108 vi.red.length = 8;
109 vi.green.offset = 16;
110 vi.green.length = 8;
111 vi.blue.offset = 24;
112 vi.blue.length = 8;
113 vi.transp.offset = 0;
114 vi.transp.length = 8;
115 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
116 vi.red.offset = 24;
117 vi.red.length = 8;
118 vi.green.offset = 16;
119 vi.green.length = 8;
120 vi.blue.offset = 8;
121 vi.blue.length = 8;
122 vi.transp.offset = 0;
123 vi.transp.length = 8;
124 } else { /* RGB565*/
125 vi.red.offset = 11;
126 vi.red.length = 5;
127 vi.green.offset = 5;
128 vi.green.length = 6;
129 vi.blue.offset = 0;
130 vi.blue.length = 5;
131 vi.transp.offset = 0;
132 vi.transp.length = 0;
133 }
134 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
135 perror("failed to put fb0 info");
136 close(fd);
137 return -1;
138 }
139
140 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800141 perror("failed to get fb0 info");
142 close(fd);
143 return -1;
144 }
145
146 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
147 if (bits == MAP_FAILED) {
148 perror("failed to mmap framebuffer");
149 close(fd);
150 return -1;
151 }
152
153 fb->version = sizeof(*fb);
154 fb->width = vi.xres;
155 fb->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700156 fb->stride = fi.line_length/PIXEL_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800157 fb->data = bits;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800158 fb->format = PIXEL_FORMAT;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700159 memset(fb->data, 0, vi.yres * fi.line_length);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800160
161 fb++;
162
163 fb->version = sizeof(*fb);
164 fb->width = vi.xres;
165 fb->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700166 fb->stride = fi.line_length/PIXEL_SIZE;
167 fb->data = (void*) (((unsigned) bits) + vi.yres * fi.line_length);
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800168 fb->format = PIXEL_FORMAT;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700169 memset(fb->data, 0, vi.yres * fi.line_length);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800170
171 return fd;
172}
173
174static void get_memory_surface(GGLSurface* ms) {
175 ms->version = sizeof(*ms);
176 ms->width = vi.xres;
177 ms->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700178 ms->stride = fi.line_length/PIXEL_SIZE;
179 ms->data = malloc(fi.line_length * vi.yres);
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800180 ms->format = PIXEL_FORMAT;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800181}
182
183static void set_active_framebuffer(unsigned n)
184{
185 if (n > 1) return;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800186 vi.yres_virtual = vi.yres * PIXEL_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800187 vi.yoffset = n * vi.yres;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800188 vi.bits_per_pixel = PIXEL_SIZE * 8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800189 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
190 perror("active fb swap failed");
191 }
192}
193
194void gr_flip(void)
195{
196 GGLContext *gl = gr_context;
197
198 /* swap front and back buffers */
199 gr_active_fb = (gr_active_fb + 1) & 1;
200
201 /* copy data from the in-memory surface to the buffer we're about
202 * to make active. */
203 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700204 fi.line_length * vi.yres);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800205
206 /* inform the display driver */
207 set_active_framebuffer(gr_active_fb);
208}
209
210void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
211{
212 GGLContext *gl = gr_context;
213 GGLint color[4];
214 color[0] = ((r << 8) | r) + 1;
215 color[1] = ((g << 8) | g) + 1;
216 color[2] = ((b << 8) | b) + 1;
217 color[3] = ((a << 8) | a) + 1;
218 gl->color4xv(gl, color);
219}
220
221int gr_measure(const char *s)
222{
223 return gr_font->cwidth * strlen(s);
224}
225
Dima Zavin3c7f00e2011-08-30 11:58:24 -0700226void gr_font_size(int *x, int *y)
227{
228 *x = gr_font->cwidth;
229 *y = gr_font->cheight;
230}
231
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800232int gr_text(int x, int y, const char *s)
233{
234 GGLContext *gl = gr_context;
235 GRFont *font = gr_font;
236 unsigned off;
237
238 y -= font->ascent;
239
240 gl->bindTexture(gl, &font->texture);
241 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
242 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
243 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
244 gl->enable(gl, GGL_TEXTURE_2D);
245
246 while((off = *s++)) {
247 off -= 32;
248 if (off < 96) {
249 gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y);
250 gl->recti(gl, x, y, x + font->cwidth, y + font->cheight);
251 }
252 x += font->cwidth;
253 }
254
255 return x;
256}
257
258void gr_fill(int x, int y, int w, int h)
259{
260 GGLContext *gl = gr_context;
261 gl->disable(gl, GGL_TEXTURE_2D);
262 gl->recti(gl, x, y, w, h);
263}
264
265void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
266 if (gr_context == NULL) {
267 return;
268 }
269 GGLContext *gl = gr_context;
270
271 gl->bindTexture(gl, (GGLSurface*) source);
272 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
273 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
274 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
275 gl->enable(gl, GGL_TEXTURE_2D);
276 gl->texCoord2i(gl, sx - dx, sy - dy);
277 gl->recti(gl, dx, dy, dx + w, dy + h);
278}
279
280unsigned int gr_get_width(gr_surface surface) {
281 if (surface == NULL) {
282 return 0;
283 }
284 return ((GGLSurface*) surface)->width;
285}
286
287unsigned int gr_get_height(gr_surface surface) {
288 if (surface == NULL) {
289 return 0;
290 }
291 return ((GGLSurface*) surface)->height;
292}
293
294static void gr_init_font(void)
295{
296 GGLSurface *ftex;
297 unsigned char *bits, *rle;
298 unsigned char *in, data;
299
300 gr_font = calloc(sizeof(*gr_font), 1);
301 ftex = &gr_font->texture;
302
303 bits = malloc(font.width * font.height);
304
305 ftex->version = sizeof(*ftex);
306 ftex->width = font.width;
307 ftex->height = font.height;
308 ftex->stride = font.width;
309 ftex->data = (void*) bits;
310 ftex->format = GGL_PIXEL_FORMAT_A_8;
311
312 in = font.rundata;
313 while((data = *in++)) {
314 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
315 bits += (data & 0x7f);
316 }
317
318 gr_font->cwidth = font.cwidth;
319 gr_font->cheight = font.cheight;
320 gr_font->ascent = font.cheight - 2;
321}
322
323int gr_init(void)
324{
325 gglInit(&gr_context);
326 GGLContext *gl = gr_context;
327
328 gr_init_font();
329 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
330 if (gr_vt_fd < 0) {
331 // This is non-fatal; post-Cupcake kernels don't have tty0.
332 perror("can't open /dev/tty0");
333 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
334 // However, if we do open tty0, we expect the ioctl to work.
335 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
336 gr_exit();
337 return -1;
338 }
339
340 gr_fb_fd = get_framebuffer(gr_framebuffer);
341 if (gr_fb_fd < 0) {
342 gr_exit();
343 return -1;
344 }
345
346 get_memory_surface(&gr_mem_surface);
347
348 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
349 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
350
351 /* start with 0 as front (displayed) and 1 as back (drawing) */
352 gr_active_fb = 0;
353 set_active_framebuffer(0);
354 gl->colorBuffer(gl, &gr_mem_surface);
355
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800356 gl->activeTexture(gl, 0);
357 gl->enable(gl, GGL_BLEND);
358 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
359
Doug Zongkerf6abd402011-09-27 13:09:48 -0700360 gr_fb_blank(true);
361 gr_fb_blank(false);
362
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800363 return 0;
364}
365
366void gr_exit(void)
367{
368 close(gr_fb_fd);
369 gr_fb_fd = -1;
370
371 free(gr_mem_surface.data);
372
373 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
374 close(gr_vt_fd);
375 gr_vt_fd = -1;
376}
377
378int gr_fb_width(void)
379{
380 return gr_framebuffer[0].width;
381}
382
383int gr_fb_height(void)
384{
385 return gr_framebuffer[0].height;
386}
387
388gr_pixel *gr_fb_data(void)
389{
390 return (unsigned short *) gr_mem_surface.data;
391}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700392
393void gr_fb_blank(bool blank)
394{
395 int ret;
396
397 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
398 if (ret < 0)
399 perror("ioctl(): blank");
400}