blob: 42c85e77781412a52e88df9118a2ba6cfe558c9d [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
Doug Zongkerbe3e6f12011-01-13 16:43:44 -080035#ifdef RECOVERY_24_BIT
36#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBX_8888
37#define PIXEL_SIZE 4
38#else
39#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGB_565
40#define PIXEL_SIZE 2
41#endif
42
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043typedef struct {
44 GGLSurface texture;
45 unsigned cwidth;
46 unsigned cheight;
47 unsigned ascent;
48} GRFont;
49
50static GRFont *gr_font = 0;
51static GGLContext *gr_context = 0;
52static GGLSurface gr_font_texture;
53static GGLSurface gr_framebuffer[2];
54static GGLSurface gr_mem_surface;
55static unsigned gr_active_fb = 0;
56
57static int gr_fb_fd = -1;
58static int gr_vt_fd = -1;
59
60static struct fb_var_screeninfo vi;
61
62static int get_framebuffer(GGLSurface *fb)
63{
64 int fd;
65 struct fb_fix_screeninfo fi;
66 void *bits;
67
68 fd = open("/dev/graphics/fb0", O_RDWR);
69 if (fd < 0) {
70 perror("cannot open fb0");
71 return -1;
72 }
73
74 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
75 perror("failed to get fb0 info");
76 close(fd);
77 return -1;
78 }
79
80 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
81 perror("failed to get fb0 info");
82 close(fd);
83 return -1;
84 }
85
86 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
87 if (bits == MAP_FAILED) {
88 perror("failed to mmap framebuffer");
89 close(fd);
90 return -1;
91 }
92
93 fb->version = sizeof(*fb);
94 fb->width = vi.xres;
95 fb->height = vi.yres;
96 fb->stride = vi.xres;
97 fb->data = bits;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -080098 fb->format = PIXEL_FORMAT;
99 memset(fb->data, 0, vi.yres * vi.xres * PIXEL_SIZE);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800100
101 fb++;
102
103 fb->version = sizeof(*fb);
104 fb->width = vi.xres;
105 fb->height = vi.yres;
106 fb->stride = vi.xres;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800107 fb->data = (void*) (((unsigned) bits) + vi.yres * vi.xres * PIXEL_SIZE);
108 fb->format = PIXEL_FORMAT;
109 memset(fb->data, 0, vi.yres * vi.xres * PIXEL_SIZE);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800110
111 return fd;
112}
113
114static void get_memory_surface(GGLSurface* ms) {
115 ms->version = sizeof(*ms);
116 ms->width = vi.xres;
117 ms->height = vi.yres;
118 ms->stride = vi.xres;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800119 ms->data = malloc(vi.xres * vi.yres * PIXEL_SIZE);
120 ms->format = PIXEL_FORMAT;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800121}
122
123static void set_active_framebuffer(unsigned n)
124{
125 if (n > 1) return;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800126 vi.yres_virtual = vi.yres * PIXEL_SIZE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800127 vi.yoffset = n * vi.yres;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800128 vi.bits_per_pixel = PIXEL_SIZE * 8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800129 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
130 perror("active fb swap failed");
131 }
132}
133
134void gr_flip(void)
135{
136 GGLContext *gl = gr_context;
137
138 /* swap front and back buffers */
139 gr_active_fb = (gr_active_fb + 1) & 1;
140
141 /* copy data from the in-memory surface to the buffer we're about
142 * to make active. */
143 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800144 vi.xres * vi.yres * PIXEL_SIZE);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800145
146 /* inform the display driver */
147 set_active_framebuffer(gr_active_fb);
148}
149
150void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
151{
152 GGLContext *gl = gr_context;
153 GGLint color[4];
154 color[0] = ((r << 8) | r) + 1;
155 color[1] = ((g << 8) | g) + 1;
156 color[2] = ((b << 8) | b) + 1;
157 color[3] = ((a << 8) | a) + 1;
158 gl->color4xv(gl, color);
159}
160
161int gr_measure(const char *s)
162{
163 return gr_font->cwidth * strlen(s);
164}
165
166int gr_text(int x, int y, const char *s)
167{
168 GGLContext *gl = gr_context;
169 GRFont *font = gr_font;
170 unsigned off;
171
172 y -= font->ascent;
173
174 gl->bindTexture(gl, &font->texture);
175 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
176 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
177 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
178 gl->enable(gl, GGL_TEXTURE_2D);
179
180 while((off = *s++)) {
181 off -= 32;
182 if (off < 96) {
183 gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y);
184 gl->recti(gl, x, y, x + font->cwidth, y + font->cheight);
185 }
186 x += font->cwidth;
187 }
188
189 return x;
190}
191
192void gr_fill(int x, int y, int w, int h)
193{
194 GGLContext *gl = gr_context;
195 gl->disable(gl, GGL_TEXTURE_2D);
196 gl->recti(gl, x, y, w, h);
197}
198
199void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
200 if (gr_context == NULL) {
201 return;
202 }
203 GGLContext *gl = gr_context;
204
205 gl->bindTexture(gl, (GGLSurface*) source);
206 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
207 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
208 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
209 gl->enable(gl, GGL_TEXTURE_2D);
210 gl->texCoord2i(gl, sx - dx, sy - dy);
211 gl->recti(gl, dx, dy, dx + w, dy + h);
212}
213
214unsigned int gr_get_width(gr_surface surface) {
215 if (surface == NULL) {
216 return 0;
217 }
218 return ((GGLSurface*) surface)->width;
219}
220
221unsigned int gr_get_height(gr_surface surface) {
222 if (surface == NULL) {
223 return 0;
224 }
225 return ((GGLSurface*) surface)->height;
226}
227
228static void gr_init_font(void)
229{
230 GGLSurface *ftex;
231 unsigned char *bits, *rle;
232 unsigned char *in, data;
233
234 gr_font = calloc(sizeof(*gr_font), 1);
235 ftex = &gr_font->texture;
236
237 bits = malloc(font.width * font.height);
238
239 ftex->version = sizeof(*ftex);
240 ftex->width = font.width;
241 ftex->height = font.height;
242 ftex->stride = font.width;
243 ftex->data = (void*) bits;
244 ftex->format = GGL_PIXEL_FORMAT_A_8;
245
246 in = font.rundata;
247 while((data = *in++)) {
248 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
249 bits += (data & 0x7f);
250 }
251
252 gr_font->cwidth = font.cwidth;
253 gr_font->cheight = font.cheight;
254 gr_font->ascent = font.cheight - 2;
255}
256
257int gr_init(void)
258{
259 gglInit(&gr_context);
260 GGLContext *gl = gr_context;
261
262 gr_init_font();
263 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
264 if (gr_vt_fd < 0) {
265 // This is non-fatal; post-Cupcake kernels don't have tty0.
266 perror("can't open /dev/tty0");
267 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
268 // However, if we do open tty0, we expect the ioctl to work.
269 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
270 gr_exit();
271 return -1;
272 }
273
274 gr_fb_fd = get_framebuffer(gr_framebuffer);
275 if (gr_fb_fd < 0) {
276 gr_exit();
277 return -1;
278 }
279
280 get_memory_surface(&gr_mem_surface);
281
282 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
283 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
284
285 /* start with 0 as front (displayed) and 1 as back (drawing) */
286 gr_active_fb = 0;
287 set_active_framebuffer(0);
288 gl->colorBuffer(gl, &gr_mem_surface);
289
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800290 gl->activeTexture(gl, 0);
291 gl->enable(gl, GGL_BLEND);
292 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
293
294 return 0;
295}
296
297void gr_exit(void)
298{
299 close(gr_fb_fd);
300 gr_fb_fd = -1;
301
302 free(gr_mem_surface.data);
303
304 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
305 close(gr_vt_fd);
306 gr_vt_fd = -1;
307}
308
309int gr_fb_width(void)
310{
311 return gr_framebuffer[0].width;
312}
313
314int gr_fb_height(void)
315{
316 return gr_framebuffer[0].height;
317}
318
319gr_pixel *gr_fb_data(void)
320{
321 return (unsigned short *) gr_mem_surface.data;
322}