blob: 06c5fdfcd0dc9d18fa13b1980c426dd0b04e0a06 [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];
The Android Open Source Project8b7334b2009-02-10 15:43:56 -080046static GGLSurface gr_mem_surface;
The Android Open Source Project23580ca2008-10-21 07:00:00 -070047static unsigned gr_active_fb = 0;
48
49static int gr_fb_fd = -1;
50static int gr_vt_fd = -1;
51
52static struct fb_var_screeninfo vi;
53
54static int get_framebuffer(GGLSurface *fb)
55{
56 int fd;
57 struct fb_fix_screeninfo fi;
58 void *bits;
The Android Open Source Project8b7334b2009-02-10 15:43:56 -080059
The Android Open Source Project23580ca2008-10-21 07:00:00 -070060 fd = open("/dev/graphics/fb0", O_RDWR);
The Android Open Source Projectff3d9382008-12-17 18:03:49 -080061 if (fd < 0) {
The Android Open Source Project23580ca2008-10-21 07:00:00 -070062 perror("cannot open fb0");
63 return -1;
64 }
65
The Android Open Source Projectff3d9382008-12-17 18:03:49 -080066 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
The Android Open Source Project23580ca2008-10-21 07:00:00 -070067 perror("failed to get fb0 info");
The Android Open Source Projectff3d9382008-12-17 18:03:49 -080068 close(fd);
The Android Open Source Project23580ca2008-10-21 07:00:00 -070069 return -1;
70 }
71
The Android Open Source Projectff3d9382008-12-17 18:03:49 -080072 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
The Android Open Source Project23580ca2008-10-21 07:00:00 -070073 perror("failed to get fb0 info");
The Android Open Source Projectff3d9382008-12-17 18:03:49 -080074 close(fd);
The Android Open Source Project23580ca2008-10-21 07:00:00 -070075 return -1;
76 }
77
78 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
The Android Open Source Projectff3d9382008-12-17 18:03:49 -080079 if (bits == MAP_FAILED) {
The Android Open Source Project23580ca2008-10-21 07:00:00 -070080 perror("failed to mmap framebuffer");
The Android Open Source Projectff3d9382008-12-17 18:03:49 -080081 close(fd);
The Android Open Source Project23580ca2008-10-21 07:00:00 -070082 return -1;
83 }
84
85 fb->version = sizeof(*fb);
86 fb->width = vi.xres;
87 fb->height = vi.yres;
88 fb->stride = vi.xres;
89 fb->data = bits;
90 fb->format = GGL_PIXEL_FORMAT_RGB_565;
The Android Open Source Project8b7334b2009-02-10 15:43:56 -080091
The Android Open Source Project23580ca2008-10-21 07:00:00 -070092 fb++;
The Android Open Source Project8b7334b2009-02-10 15:43:56 -080093
The Android Open Source Project23580ca2008-10-21 07:00:00 -070094 fb->version = sizeof(*fb);
95 fb->width = vi.xres;
96 fb->height = vi.yres;
97 fb->stride = vi.xres;
98 fb->data = (void*) (((unsigned) bits) + vi.yres * vi.xres * 2);
99 fb->format = GGL_PIXEL_FORMAT_RGB_565;
100
101 return fd;
102}
103
The Android Open Source Project8b7334b2009-02-10 15:43:56 -0800104static void get_memory_surface(GGLSurface* ms) {
105 ms->version = sizeof(*ms);
106 ms->width = vi.xres;
107 ms->height = vi.yres;
108 ms->stride = vi.xres;
109 ms->data = malloc(vi.xres * vi.yres * 2);
110 ms->format = GGL_PIXEL_FORMAT_RGB_565;
111}
112
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700113static void set_active_framebuffer(unsigned n)
114{
The Android Open Source Projectff3d9382008-12-17 18:03:49 -0800115 if (n > 1) return;
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700116 vi.yres_virtual = vi.yres * 2;
117 vi.yoffset = n * vi.yres;
The Android Open Source Projectff3d9382008-12-17 18:03:49 -0800118 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
119 perror("active fb swap failed");
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700120 }
121}
122
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700123void gr_flip(void)
124{
125 GGLContext *gl = gr_context;
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700126
The Android Open Source Project8b7334b2009-02-10 15:43:56 -0800127 /* swap front and back buffers */
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700128 gr_active_fb = (gr_active_fb + 1) & 1;
129
The Android Open Source Project8b7334b2009-02-10 15:43:56 -0800130 /* copy data from the in-memory surface to the buffer we're about
131 * to make active. */
132 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
133 vi.xres * vi.yres * 2);
134
135 /* inform the display driver */
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700136 set_active_framebuffer(gr_active_fb);
137}
138
139void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
140{
141 GGLContext *gl = gr_context;
142 GGLint color[4];
143 color[0] = ((r << 8) | r) + 1;
144 color[1] = ((g << 8) | g) + 1;
145 color[2] = ((b << 8) | b) + 1;
146 color[3] = ((a << 8) | a) + 1;
147 gl->color4xv(gl, color);
148}
149
150int gr_measure(const char *s)
151{
152 return gr_font->cwidth * strlen(s);
153}
154
155int gr_text(int x, int y, const char *s)
156{
157 GGLContext *gl = gr_context;
158 GRFont *font = gr_font;
159 unsigned off;
160
161 y -= font->ascent;
The Android Open Source Project8b7334b2009-02-10 15:43:56 -0800162
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700163 gl->bindTexture(gl, &font->texture);
164 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
165 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
166 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
167 gl->enable(gl, GGL_TEXTURE_2D);
The Android Open Source Project8b7334b2009-02-10 15:43:56 -0800168
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700169 while((off = *s++)) {
170 off -= 32;
The Android Open Source Projectff3d9382008-12-17 18:03:49 -0800171 if (off < 96) {
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700172 gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y);
173 gl->recti(gl, x, y, x + font->cwidth, y + font->cheight);
174 }
175 x += font->cwidth;
176 }
177
178 return x;
179}
180
181void gr_fill(int x, int y, int w, int h)
182{
183 GGLContext *gl = gr_context;
184 gl->disable(gl, GGL_TEXTURE_2D);
185 gl->recti(gl, x, y, w, h);
186}
187
188void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
189 if (gr_context == NULL) {
190 return;
191 }
192 GGLContext *gl = gr_context;
193
194 gl->bindTexture(gl, (GGLSurface*) source);
195 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
196 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
197 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
198 gl->enable(gl, GGL_TEXTURE_2D);
199 gl->texCoord2i(gl, sx - dx, sy - dy);
200 gl->recti(gl, dx, dy, dx + w, dy + h);
201}
202
203unsigned int gr_get_width(gr_surface surface) {
204 if (surface == NULL) {
205 return 0;
206 }
207 return ((GGLSurface*) surface)->width;
208}
209
210unsigned int gr_get_height(gr_surface surface) {
211 if (surface == NULL) {
212 return 0;
213 }
214 return ((GGLSurface*) surface)->height;
215}
216
217static void gr_init_font(void)
218{
219 GGLSurface *ftex;
220 unsigned char *bits, *rle;
221 unsigned char *in, data;
222
223 gr_font = calloc(sizeof(*gr_font), 1);
The Android Open Source Project8b7334b2009-02-10 15:43:56 -0800224 ftex = &gr_font->texture;
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700225
226 bits = malloc(font.width * font.height);
The Android Open Source Project8b7334b2009-02-10 15:43:56 -0800227
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700228 ftex->version = sizeof(*ftex);
229 ftex->width = font.width;
230 ftex->height = font.height;
231 ftex->stride = font.width;
232 ftex->data = (void*) bits;
233 ftex->format = GGL_PIXEL_FORMAT_A_8;
234
235 in = font.rundata;
236 while((data = *in++)) {
237 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
238 bits += (data & 0x7f);
239 }
The Android Open Source Project8b7334b2009-02-10 15:43:56 -0800240
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700241 gr_font->cwidth = font.cwidth;
242 gr_font->cheight = font.cheight;
243 gr_font->ascent = font.cheight - 2;
244}
245
246int gr_init(void)
247{
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700248 gglInit(&gr_context);
The Android Open Source Projectff3d9382008-12-17 18:03:49 -0800249 GGLContext *gl = gr_context;
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700250
251 gr_init_font();
The Android Open Source Projectff3d9382008-12-17 18:03:49 -0800252 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
253 if (gr_vt_fd < 0) {
254 // This is non-fatal; post-Cupcake kernels don't have tty0.
255 perror("can't open /dev/tty0");
256 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
257 // However, if we do open tty0, we expect the ioctl to work.
258 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
259 gr_exit();
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700260 return -1;
261 }
262
263 gr_fb_fd = get_framebuffer(gr_framebuffer);
The Android Open Source Projectff3d9382008-12-17 18:03:49 -0800264 if (gr_fb_fd < 0) {
265 gr_exit();
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700266 return -1;
267 }
268
The Android Open Source Project8b7334b2009-02-10 15:43:56 -0800269 get_memory_surface(&gr_mem_surface);
270
The Android Open Source Projectff3d9382008-12-17 18:03:49 -0800271 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
272 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700273
274 /* start with 0 as front (displayed) and 1 as back (drawing) */
275 gr_active_fb = 0;
276 set_active_framebuffer(0);
The Android Open Source Project8b7334b2009-02-10 15:43:56 -0800277 gl->colorBuffer(gl, &gr_mem_surface);
278
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700279
280 gl->activeTexture(gl, 0);
281 gl->enable(gl, GGL_BLEND);
282 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
283
284 return 0;
285}
286
287void gr_exit(void)
288{
289 close(gr_fb_fd);
290 gr_fb_fd = -1;
The Android Open Source Project8b7334b2009-02-10 15:43:56 -0800291
292 free(gr_mem_surface.data);
293
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700294 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
295 close(gr_vt_fd);
296 gr_vt_fd = -1;
297}
298
299int gr_fb_width(void)
300{
301 return gr_framebuffer[0].width;
302}
303
304int gr_fb_height(void)
305{
306 return gr_framebuffer[0].height;
307}
308
309gr_pixel *gr_fb_data(void)
310{
The Android Open Source Project8b7334b2009-02-10 15:43:56 -0800311 return (unsigned short *) gr_mem_surface.data;
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700312}