blob: 8192d3ff4acd49cbe838129145b2600c69361ae1 [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
Carlos Silvab5d0bd02011-01-17 00:22:40 +0000147#ifdef BOARD_HAS_FLIPPED_SCREEN
148 /* flip buffer 180 degrees for devices with physicaly inverted screens */
149 unsigned int i;
150 for (i = 1; i < (vi.xres * vi.yres); i++) {
151 unsigned short tmp = gr_mem_surface.data[i];
152 gr_mem_surface.data[i] = gr_mem_surface.data[(vi.xres * vi.yres * 2) - i];
153 gr_mem_surface.data[(vi.xres * vi.yres * 2) - i] = tmp;
154 }
155#endif
156
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800157 /* copy data from the in-memory surface to the buffer we're about
158 * to make active. */
159 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
160 vi.xres * vi.yres * 2);
161
162 /* inform the display driver */
163 set_active_framebuffer(gr_active_fb);
164}
165
166void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
167{
168 GGLContext *gl = gr_context;
169 GGLint color[4];
170 color[0] = ((r << 8) | r) + 1;
171 color[1] = ((g << 8) | g) + 1;
172 color[2] = ((b << 8) | b) + 1;
173 color[3] = ((a << 8) | a) + 1;
174 gl->color4xv(gl, color);
175}
176
177int gr_measure(const char *s)
178{
179 return gr_font->cwidth * strlen(s);
180}
181
182int gr_text(int x, int y, const char *s)
183{
184 GGLContext *gl = gr_context;
185 GRFont *font = gr_font;
186 unsigned off;
187
188 y -= font->ascent;
189
190 gl->bindTexture(gl, &font->texture);
191 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
192 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
193 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
194 gl->enable(gl, GGL_TEXTURE_2D);
195
196 while((off = *s++)) {
197 off -= 32;
198 if (off < 96) {
199 gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y);
200 gl->recti(gl, x, y, x + font->cwidth, y + font->cheight);
201 }
202 x += font->cwidth;
203 }
204
205 return x;
206}
207
208void gr_fill(int x, int y, int w, int h)
209{
210 GGLContext *gl = gr_context;
211 gl->disable(gl, GGL_TEXTURE_2D);
212 gl->recti(gl, x, y, w, h);
213}
214
215void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
216 if (gr_context == NULL) {
217 return;
218 }
219 GGLContext *gl = gr_context;
220
221 gl->bindTexture(gl, (GGLSurface*) source);
222 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
223 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
224 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
225 gl->enable(gl, GGL_TEXTURE_2D);
226 gl->texCoord2i(gl, sx - dx, sy - dy);
227 gl->recti(gl, dx, dy, dx + w, dy + h);
228}
229
230unsigned int gr_get_width(gr_surface surface) {
231 if (surface == NULL) {
232 return 0;
233 }
234 return ((GGLSurface*) surface)->width;
235}
236
237unsigned int gr_get_height(gr_surface surface) {
238 if (surface == NULL) {
239 return 0;
240 }
241 return ((GGLSurface*) surface)->height;
242}
243
244static void gr_init_font(void)
245{
246 GGLSurface *ftex;
247 unsigned char *bits, *rle;
248 unsigned char *in, data;
249
250 gr_font = calloc(sizeof(*gr_font), 1);
251 ftex = &gr_font->texture;
252
253 bits = malloc(font.width * font.height);
254
255 ftex->version = sizeof(*ftex);
256 ftex->width = font.width;
257 ftex->height = font.height;
258 ftex->stride = font.width;
259 ftex->data = (void*) bits;
260 ftex->format = GGL_PIXEL_FORMAT_A_8;
261
262 in = font.rundata;
263 while((data = *in++)) {
264 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
265 bits += (data & 0x7f);
266 }
267
268 gr_font->cwidth = font.cwidth;
269 gr_font->cheight = font.cheight;
270 gr_font->ascent = font.cheight - 2;
271}
272
273int gr_init(void)
274{
275 gglInit(&gr_context);
276 GGLContext *gl = gr_context;
277
278 gr_init_font();
279 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
280 if (gr_vt_fd < 0) {
281 // This is non-fatal; post-Cupcake kernels don't have tty0.
282 perror("can't open /dev/tty0");
283 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
284 // However, if we do open tty0, we expect the ioctl to work.
285 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
286 gr_exit();
287 return -1;
288 }
289
290 gr_fb_fd = get_framebuffer(gr_framebuffer);
291 if (gr_fb_fd < 0) {
292 gr_exit();
293 return -1;
294 }
295
296 get_memory_surface(&gr_mem_surface);
297
298 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
299 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
300
301 /* start with 0 as front (displayed) and 1 as back (drawing) */
302 gr_active_fb = 0;
303 set_active_framebuffer(0);
304 gl->colorBuffer(gl, &gr_mem_surface);
305
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800306 gl->activeTexture(gl, 0);
307 gl->enable(gl, GGL_BLEND);
308 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
309
310 return 0;
311}
312
313void gr_exit(void)
314{
315 close(gr_fb_fd);
316 gr_fb_fd = -1;
317
318 free(gr_mem_surface.data);
319
320 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
321 close(gr_vt_fd);
322 gr_vt_fd = -1;
323}
324
325int gr_fb_width(void)
326{
327 return gr_framebuffer[0].width;
328}
329
330int gr_fb_height(void)
331{
332 return gr_framebuffer[0].height;
333}
334
335gr_pixel *gr_fb_data(void)
336{
337 return (unsigned short *) gr_mem_surface.data;
338}