• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
35 typedef struct {
36     GGLSurface texture;
37     unsigned cwidth;
38     unsigned cheight;
39     unsigned ascent;
40 } GRFont;
41 
42 static GRFont *gr_font = 0;
43 static GGLContext *gr_context = 0;
44 static GGLSurface gr_font_texture;
45 static GGLSurface gr_framebuffer[2];
46 static GGLSurface gr_mem_surface;
47 static unsigned gr_active_fb = 0;
48 
49 static int gr_fb_fd = -1;
50 static int gr_vt_fd = -1;
51 
52 static struct fb_var_screeninfo vi;
53 
get_framebuffer(GGLSurface * fb)54 static 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;
91 
92     fb++;
93 
94     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 
get_memory_surface(GGLSurface * ms)104 static 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 
set_active_framebuffer(unsigned n)113 static void set_active_framebuffer(unsigned n)
114 {
115     if (n > 1) return;
116     vi.yres_virtual = vi.yres * 2;
117     vi.yoffset = n * vi.yres;
118     if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
119         perror("active fb swap failed");
120     }
121 }
122 
gr_flip(void)123 void gr_flip(void)
124 {
125     GGLContext *gl = gr_context;
126 
127     /* swap front and back buffers */
128     gr_active_fb = (gr_active_fb + 1) & 1;
129 
130     /* 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 */
136     set_active_framebuffer(gr_active_fb);
137 }
138 
gr_color(unsigned char r,unsigned char g,unsigned char b,unsigned char a)139 void 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 
gr_measure(const char * s)150 int gr_measure(const char *s)
151 {
152     return gr_font->cwidth * strlen(s);
153 }
154 
gr_text(int x,int y,const char * s)155 int 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;
162 
163     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);
168 
169     while((off = *s++)) {
170         off -= 32;
171         if (off < 96) {
172             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 
gr_fill(int x,int y,int w,int h)181 void 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 
gr_blit(gr_surface source,int sx,int sy,int w,int h,int dx,int dy)188 void 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 
gr_get_width(gr_surface surface)203 unsigned int gr_get_width(gr_surface surface) {
204     if (surface == NULL) {
205         return 0;
206     }
207     return ((GGLSurface*) surface)->width;
208 }
209 
gr_get_height(gr_surface surface)210 unsigned int gr_get_height(gr_surface surface) {
211     if (surface == NULL) {
212         return 0;
213     }
214     return ((GGLSurface*) surface)->height;
215 }
216 
gr_init_font(void)217 static 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);
224     ftex = &gr_font->texture;
225 
226     bits = malloc(font.width * font.height);
227 
228     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     }
240 
241     gr_font->cwidth = font.cwidth;
242     gr_font->cheight = font.cheight;
243     gr_font->ascent = font.cheight - 2;
244 }
245 
gr_init(void)246 int gr_init(void)
247 {
248     gglInit(&gr_context);
249     GGLContext *gl = gr_context;
250 
251     gr_init_font();
252     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();
260         return -1;
261     }
262 
263     gr_fb_fd = get_framebuffer(gr_framebuffer);
264     if (gr_fb_fd < 0) {
265         gr_exit();
266         return -1;
267     }
268 
269     get_memory_surface(&gr_mem_surface);
270 
271     fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
272             gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
273 
274         /* start with 0 as front (displayed) and 1 as back (drawing) */
275     gr_active_fb = 0;
276     set_active_framebuffer(0);
277     gl->colorBuffer(gl, &gr_mem_surface);
278 
279 
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 
gr_exit(void)287 void gr_exit(void)
288 {
289     close(gr_fb_fd);
290     gr_fb_fd = -1;
291 
292     free(gr_mem_surface.data);
293 
294     ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
295     close(gr_vt_fd);
296     gr_vt_fd = -1;
297 }
298 
gr_fb_width(void)299 int gr_fb_width(void)
300 {
301     return gr_framebuffer[0].width;
302 }
303 
gr_fb_height(void)304 int gr_fb_height(void)
305 {
306     return gr_framebuffer[0].height;
307 }
308 
gr_fb_data(void)309 gr_pixel *gr_fb_data(void)
310 {
311     return (unsigned short *) gr_mem_surface.data;
312 }
313