blob: 4f3026a25944ed1fafa0798f5d7f3d1ed766d50b [file] [log] [blame]
The Android Open Source Project23580ca2008-10-21 07:00:00 -07001/*
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
35typedef struct {
36 GGLSurface texture;
37 unsigned cwidth;
38 unsigned cheight;
39 unsigned ascent;
40} GRFont;
41
42static GRFont *gr_font = 0;
43static GGLContext *gr_context = 0;
44static GGLSurface gr_font_texture;
45static GGLSurface gr_framebuffer[2];
46static unsigned gr_active_fb = 0;
47
48static int gr_fb_fd = -1;
49static int gr_vt_fd = -1;
50
51static struct fb_var_screeninfo vi;
52
53static int get_framebuffer(GGLSurface *fb)
54{
55 int fd;
56 struct fb_fix_screeninfo fi;
57 void *bits;
58
59 fd = open("/dev/graphics/fb0", O_RDWR);
60 if(fd < 0) {
61 perror("cannot open fb0");
62 return -1;
63 }
64
65 if(ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
66 perror("failed to get fb0 info");
67 return -1;
68 }
69
70 if(ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
71 perror("failed to get fb0 info");
72 return -1;
73 }
74
75 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
76 if(bits == MAP_FAILED) {
77 perror("failed to mmap framebuffer");
78 return -1;
79 }
80
81 fb->version = sizeof(*fb);
82 fb->width = vi.xres;
83 fb->height = vi.yres;
84 fb->stride = vi.xres;
85 fb->data = bits;
86 fb->format = GGL_PIXEL_FORMAT_RGB_565;
87
88 fb++;
89
90 fb->version = sizeof(*fb);
91 fb->width = vi.xres;
92 fb->height = vi.yres;
93 fb->stride = vi.xres;
94 fb->data = (void*) (((unsigned) bits) + vi.yres * vi.xres * 2);
95 fb->format = GGL_PIXEL_FORMAT_RGB_565;
96
97 return fd;
98}
99
100static void set_active_framebuffer(unsigned n)
101{
102 if(n > 1) return;
103 vi.yres_virtual = vi.yres * 2;
104 vi.yoffset = n * vi.yres;
105 if(ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
106 fprintf(stderr,"active fb swap failed!\n");
107 }
108}
109
110static void dumpinfo(struct fb_var_screeninfo *vi)
111{
112 fprintf(stderr,"vi.xres = %d\n", vi->xres);
113 fprintf(stderr,"vi.yres = %d\n", vi->yres);
114 fprintf(stderr,"vi.xresv = %d\n", vi->xres_virtual);
115 fprintf(stderr,"vi.yresv = %d\n", vi->yres_virtual);
116 fprintf(stderr,"vi.xoff = %d\n", vi->xoffset);
117 fprintf(stderr,"vi.yoff = %d\n", vi->yoffset);
118}
119
120void gr_flip(void)
121{
122 GGLContext *gl = gr_context;
123
124 /* currently active buffer becomes the backbuffer */
125 gl->colorBuffer(gl, gr_framebuffer + gr_active_fb);
126
127 /* swap front and back buffers */
128 gr_active_fb = (gr_active_fb + 1) & 1;
129
130 /* inform the display driver */
131 set_active_framebuffer(gr_active_fb);
132}
133
134void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
135{
136 GGLContext *gl = gr_context;
137 GGLint color[4];
138 color[0] = ((r << 8) | r) + 1;
139 color[1] = ((g << 8) | g) + 1;
140 color[2] = ((b << 8) | b) + 1;
141 color[3] = ((a << 8) | a) + 1;
142 gl->color4xv(gl, color);
143}
144
145int gr_measure(const char *s)
146{
147 return gr_font->cwidth * strlen(s);
148}
149
150int gr_text(int x, int y, const char *s)
151{
152 GGLContext *gl = gr_context;
153 GRFont *font = gr_font;
154 unsigned off;
155
156 y -= font->ascent;
157
158 gl->bindTexture(gl, &font->texture);
159 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
160 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
161 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
162 gl->enable(gl, GGL_TEXTURE_2D);
163
164 while((off = *s++)) {
165 off -= 32;
166 if(off < 96) {
167 gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y);
168 gl->recti(gl, x, y, x + font->cwidth, y + font->cheight);
169 }
170 x += font->cwidth;
171 }
172
173 return x;
174}
175
176void gr_fill(int x, int y, int w, int h)
177{
178 GGLContext *gl = gr_context;
179 gl->disable(gl, GGL_TEXTURE_2D);
180 gl->recti(gl, x, y, w, h);
181}
182
183void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
184 if (gr_context == NULL) {
185 return;
186 }
187 GGLContext *gl = gr_context;
188
189 gl->bindTexture(gl, (GGLSurface*) source);
190 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
191 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
192 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
193 gl->enable(gl, GGL_TEXTURE_2D);
194 gl->texCoord2i(gl, sx - dx, sy - dy);
195 gl->recti(gl, dx, dy, dx + w, dy + h);
196}
197
198unsigned int gr_get_width(gr_surface surface) {
199 if (surface == NULL) {
200 return 0;
201 }
202 return ((GGLSurface*) surface)->width;
203}
204
205unsigned int gr_get_height(gr_surface surface) {
206 if (surface == NULL) {
207 return 0;
208 }
209 return ((GGLSurface*) surface)->height;
210}
211
212static void gr_init_font(void)
213{
214 GGLSurface *ftex;
215 unsigned char *bits, *rle;
216 unsigned char *in, data;
217
218 gr_font = calloc(sizeof(*gr_font), 1);
219 ftex = &gr_font->texture;
220
221 bits = malloc(font.width * font.height);
222
223 ftex->version = sizeof(*ftex);
224 ftex->width = font.width;
225 ftex->height = font.height;
226 ftex->stride = font.width;
227 ftex->data = (void*) bits;
228 ftex->format = GGL_PIXEL_FORMAT_A_8;
229
230 in = font.rundata;
231 while((data = *in++)) {
232 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
233 bits += (data & 0x7f);
234 }
235
236 gr_font->cwidth = font.cwidth;
237 gr_font->cheight = font.cheight;
238 gr_font->ascent = font.cheight - 2;
239}
240
241int gr_init(void)
242{
243 GGLContext *gl;
244 int fd;
245
246 gglInit(&gr_context);
247 gl = gr_context;
248
249 gr_init_font();
250
251 fd = open("/dev/tty0", O_RDWR | O_SYNC);
252 if(fd < 0) return -1;
253
254 if(ioctl(fd, KDSETMODE, (void*) KD_GRAPHICS)) {
255 close(fd);
256 return -1;
257 }
258
259 gr_fb_fd = get_framebuffer(gr_framebuffer);
260
261 if(gr_fb_fd < 0) {
262 ioctl(fd, KDSETMODE, (void*) KD_TEXT);
263 close(fd);
264 return -1;
265 }
266
267 gr_vt_fd = fd;
268
269 /* start with 0 as front (displayed) and 1 as back (drawing) */
270 gr_active_fb = 0;
271 set_active_framebuffer(0);
272 gl->colorBuffer(gl, gr_framebuffer + 1);
273
274 gl->activeTexture(gl, 0);
275 gl->enable(gl, GGL_BLEND);
276 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
277
278 return 0;
279}
280
281void gr_exit(void)
282{
283 close(gr_fb_fd);
284 gr_fb_fd = -1;
285
286 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
287 close(gr_vt_fd);
288 gr_vt_fd = -1;
289}
290
291int gr_fb_width(void)
292{
293 return gr_framebuffer[0].width;
294}
295
296int gr_fb_height(void)
297{
298 return gr_framebuffer[0].height;
299}
300
301gr_pixel *gr_fb_data(void)
302{
303 return (unsigned short *) gr_framebuffer[1 - gr_active_fb].data;
304}