blob: 915873b54e58cbf0993528b600fe4f35294fb946 [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 Duttad0fff562010-12-18 23:18:50 -080093#ifdef BOARD_HAS_JANKY_BACKBUFFER
94 fb->stride = fi.line_length/2;
95#else
96 fb->stride = vi.xres;
97#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080098 fb->data = bits;
99 fb->format = GGL_PIXEL_FORMAT_RGB_565;
Doug Zongker51266d12010-11-01 10:19:12 -0700100 memset(fb->data, 0, vi.yres * vi.xres * 2);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800101
102 fb++;
103
104 fb->version = sizeof(*fb);
105 fb->width = vi.xres;
106 fb->height = vi.yres;
Koushik Dutta49951142010-12-18 21:58:43 -0800107#ifdef BOARD_HAS_JANKY_BACKBUFFER
Koushik Duttad0fff562010-12-18 23:18:50 -0800108 fb->stride = fi.line_length/2;
Koushik Dutta49951142010-12-18 21:58:43 -0800109 fb->data = (void*) (((unsigned) bits) + vi.yres * fi.line_length);
110#else
Koushik Duttad0fff562010-12-18 23:18:50 -0800111 fb->stride = vi.xres;
112 fb->data = (void*) (((unsigned) bits) + vi.yres * vi.xres * 2);
Koushik Dutta49951142010-12-18 21:58:43 -0800113#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800114 fb->format = GGL_PIXEL_FORMAT_RGB_565;
Doug Zongker51266d12010-11-01 10:19:12 -0700115 memset(fb->data, 0, vi.yres * vi.xres * 2);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800116
117 return fd;
118}
119
120static void get_memory_surface(GGLSurface* ms) {
121 ms->version = sizeof(*ms);
122 ms->width = vi.xres;
123 ms->height = vi.yres;
124 ms->stride = vi.xres;
125 ms->data = malloc(vi.xres * vi.yres * 2);
126 ms->format = GGL_PIXEL_FORMAT_RGB_565;
127}
128
129static void set_active_framebuffer(unsigned n)
130{
131 if (n > 1) return;
132 vi.yres_virtual = vi.yres * 2;
133 vi.yoffset = n * vi.yres;
Rebecca Schultz Zavin573fd7b2009-06-05 16:56:07 -0700134 vi.bits_per_pixel = 16;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800135 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
136 perror("active fb swap failed");
137 }
138}
139
140void gr_flip(void)
141{
142 GGLContext *gl = gr_context;
143
144 /* swap front and back buffers */
145 gr_active_fb = (gr_active_fb + 1) & 1;
146
147 /* copy data from the in-memory surface to the buffer we're about
148 * to make active. */
149 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
150 vi.xres * vi.yres * 2);
151
152 /* inform the display driver */
153 set_active_framebuffer(gr_active_fb);
154}
155
156void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
157{
158 GGLContext *gl = gr_context;
159 GGLint color[4];
160 color[0] = ((r << 8) | r) + 1;
161 color[1] = ((g << 8) | g) + 1;
162 color[2] = ((b << 8) | b) + 1;
163 color[3] = ((a << 8) | a) + 1;
164 gl->color4xv(gl, color);
165}
166
167int gr_measure(const char *s)
168{
169 return gr_font->cwidth * strlen(s);
170}
171
172int gr_text(int x, int y, const char *s)
173{
174 GGLContext *gl = gr_context;
175 GRFont *font = gr_font;
176 unsigned off;
177
178 y -= font->ascent;
179
180 gl->bindTexture(gl, &font->texture);
181 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
182 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
183 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
184 gl->enable(gl, GGL_TEXTURE_2D);
185
186 while((off = *s++)) {
187 off -= 32;
188 if (off < 96) {
189 gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y);
190 gl->recti(gl, x, y, x + font->cwidth, y + font->cheight);
191 }
192 x += font->cwidth;
193 }
194
195 return x;
196}
197
198void gr_fill(int x, int y, int w, int h)
199{
200 GGLContext *gl = gr_context;
201 gl->disable(gl, GGL_TEXTURE_2D);
202 gl->recti(gl, x, y, w, h);
203}
204
205void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
206 if (gr_context == NULL) {
207 return;
208 }
209 GGLContext *gl = gr_context;
210
211 gl->bindTexture(gl, (GGLSurface*) source);
212 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
213 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
214 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
215 gl->enable(gl, GGL_TEXTURE_2D);
216 gl->texCoord2i(gl, sx - dx, sy - dy);
217 gl->recti(gl, dx, dy, dx + w, dy + h);
218}
219
220unsigned int gr_get_width(gr_surface surface) {
221 if (surface == NULL) {
222 return 0;
223 }
224 return ((GGLSurface*) surface)->width;
225}
226
227unsigned int gr_get_height(gr_surface surface) {
228 if (surface == NULL) {
229 return 0;
230 }
231 return ((GGLSurface*) surface)->height;
232}
233
234static void gr_init_font(void)
235{
236 GGLSurface *ftex;
237 unsigned char *bits, *rle;
238 unsigned char *in, data;
239
240 gr_font = calloc(sizeof(*gr_font), 1);
241 ftex = &gr_font->texture;
242
243 bits = malloc(font.width * font.height);
244
245 ftex->version = sizeof(*ftex);
246 ftex->width = font.width;
247 ftex->height = font.height;
248 ftex->stride = font.width;
249 ftex->data = (void*) bits;
250 ftex->format = GGL_PIXEL_FORMAT_A_8;
251
252 in = font.rundata;
253 while((data = *in++)) {
254 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
255 bits += (data & 0x7f);
256 }
257
258 gr_font->cwidth = font.cwidth;
259 gr_font->cheight = font.cheight;
260 gr_font->ascent = font.cheight - 2;
261}
262
263int gr_init(void)
264{
265 gglInit(&gr_context);
266 GGLContext *gl = gr_context;
267
268 gr_init_font();
269 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
270 if (gr_vt_fd < 0) {
271 // This is non-fatal; post-Cupcake kernels don't have tty0.
272 perror("can't open /dev/tty0");
273 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
274 // However, if we do open tty0, we expect the ioctl to work.
275 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
276 gr_exit();
277 return -1;
278 }
279
280 gr_fb_fd = get_framebuffer(gr_framebuffer);
281 if (gr_fb_fd < 0) {
282 gr_exit();
283 return -1;
284 }
285
286 get_memory_surface(&gr_mem_surface);
287
288 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
289 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
290
291 /* start with 0 as front (displayed) and 1 as back (drawing) */
292 gr_active_fb = 0;
293 set_active_framebuffer(0);
294 gl->colorBuffer(gl, &gr_mem_surface);
295
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800296 gl->activeTexture(gl, 0);
297 gl->enable(gl, GGL_BLEND);
298 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
299
300 return 0;
301}
302
303void gr_exit(void)
304{
305 close(gr_fb_fd);
306 gr_fb_fd = -1;
307
308 free(gr_mem_surface.data);
309
310 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
311 close(gr_vt_fd);
312 gr_vt_fd = -1;
313}
314
315int gr_fb_width(void)
316{
317 return gr_framebuffer[0].width;
318}
319
320int gr_fb_height(void)
321{
322 return gr_framebuffer[0].height;
323}
324
325gr_pixel *gr_fb_data(void)
326{
327 return (unsigned short *) gr_mem_surface.data;
328}