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