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