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