6550875ef9c53f7fed6e44629a28b1130e454eea
[safe/jmp/linux-2.6] / drivers / video / console / bitblit.c
1 /*
2  *  linux/drivers/video/console/bitblit.c -- BitBlitting Operation
3  *
4  *  Originally from the 'accel_*' routines in drivers/video/console/fbcon.c
5  *
6  *      Copyright (C) 2004 Antonino Daplas <adaplas @pol.net>
7  *
8  *  This file is subject to the terms and conditions of the GNU General Public
9  *  License.  See the file COPYING in the main directory of this archive for
10  *  more details.
11  */
12
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/string.h>
16 #include <linux/fb.h>
17 #include <linux/vt_kern.h>
18 #include <linux/console.h>
19 #include <asm/types.h>
20 #include "fbcon.h"
21
22 /*
23  * Accelerated handlers.
24  */
25 #define FBCON_ATTRIBUTE_UNDERLINE 1
26 #define FBCON_ATTRIBUTE_REVERSE   2
27 #define FBCON_ATTRIBUTE_BOLD      4
28
29 static inline int real_y(struct display *p, int ypos)
30 {
31         int rows = p->vrows;
32
33         ypos += p->yscroll;
34         return ypos < rows ? ypos : ypos - rows;
35 }
36
37
38 static inline int get_attribute(struct fb_info *info, u16 c)
39 {
40         int attribute = 0;
41
42         if (fb_get_color_depth(&info->var, &info->fix) == 1) {
43                 if (attr_underline(c))
44                         attribute |= FBCON_ATTRIBUTE_UNDERLINE;
45                 if (attr_reverse(c))
46                         attribute |= FBCON_ATTRIBUTE_REVERSE;
47                 if (attr_bold(c))
48                         attribute |= FBCON_ATTRIBUTE_BOLD;
49         }
50
51         return attribute;
52 }
53
54 static inline void update_attr(u8 *dst, u8 *src, int attribute,
55                                struct vc_data *vc)
56 {
57         int i, offset = (vc->vc_font.height < 10) ? 1 : 2;
58         int width = (vc->vc_font.width + 7) >> 3;
59         unsigned int cellsize = vc->vc_font.height * width;
60         u8 c;
61
62         offset = cellsize - (offset * width);
63         for (i = 0; i < cellsize; i++) {
64                 c = src[i];
65                 if (attribute & FBCON_ATTRIBUTE_UNDERLINE && i >= offset)
66                         c = 0xff;
67                 if (attribute & FBCON_ATTRIBUTE_BOLD)
68                         c |= c >> 1;
69                 if (attribute & FBCON_ATTRIBUTE_REVERSE)
70                         c = ~c;
71                 dst[i] = c;
72         }
73 }
74
75 static void bit_bmove(struct vc_data *vc, struct fb_info *info, int sy,
76                       int sx, int dy, int dx, int height, int width)
77 {
78         struct fb_copyarea area;
79
80         area.sx = sx * vc->vc_font.width;
81         area.sy = sy * vc->vc_font.height;
82         area.dx = dx * vc->vc_font.width;
83         area.dy = dy * vc->vc_font.height;
84         area.height = height * vc->vc_font.height;
85         area.width = width * vc->vc_font.width;
86
87         info->fbops->fb_copyarea(info, &area);
88 }
89
90 static void bit_clear(struct vc_data *vc, struct fb_info *info, int sy,
91                       int sx, int height, int width)
92 {
93         int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
94         struct fb_fillrect region;
95
96         region.color = attr_bgcol_ec(bgshift, vc);
97         region.dx = sx * vc->vc_font.width;
98         region.dy = sy * vc->vc_font.height;
99         region.width = width * vc->vc_font.width;
100         region.height = height * vc->vc_font.height;
101         region.rop = ROP_COPY;
102
103         info->fbops->fb_fillrect(info, &region);
104 }
105
106 static void bit_putcs(struct vc_data *vc, struct fb_info *info,
107                       const unsigned short *s, int count, int yy, int xx,
108                       int fg, int bg)
109 {
110         unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
111         unsigned int width = (vc->vc_font.width + 7) >> 3;
112         unsigned int cellsize = vc->vc_font.height * width;
113         unsigned int maxcnt = info->pixmap.size/cellsize;
114         unsigned int scan_align = info->pixmap.scan_align - 1;
115         unsigned int buf_align = info->pixmap.buf_align - 1;
116         unsigned int shift_low = 0, mod = vc->vc_font.width % 8;
117         unsigned int shift_high = 8, pitch, cnt, size, i, k;
118         unsigned int idx = vc->vc_font.width >> 3;
119         unsigned int attribute = get_attribute(info, scr_readw(s));
120         struct fb_image image;
121         u8 *src, *dst, *buf = NULL;
122
123         if (attribute) {
124                 buf = kmalloc(cellsize, GFP_KERNEL);
125                 if (!buf)
126                         return;
127         }
128
129         image.fg_color = fg;
130         image.bg_color = bg;
131
132         image.dx = xx * vc->vc_font.width;
133         image.dy = yy * vc->vc_font.height;
134         image.height = vc->vc_font.height;
135         image.depth = 1;
136
137         while (count) {
138                 if (count > maxcnt)
139                         cnt = k = maxcnt;
140                 else
141                         cnt = k = count;
142
143                 image.width = vc->vc_font.width * cnt;
144                 pitch = ((image.width + 7) >> 3) + scan_align;
145                 pitch &= ~scan_align;
146                 size = pitch * image.height + buf_align;
147                 size &= ~buf_align;
148                 dst = fb_get_buffer_offset(info, &info->pixmap, size);
149                 image.data = dst;
150                 if (mod) {
151                         while (k--) {
152                                 src = vc->vc_font.data + (scr_readw(s++)&
153                                                           charmask)*cellsize;
154
155                                 if (attribute) {
156                                         update_attr(buf, src, attribute, vc);
157                                         src = buf;
158                                 }
159
160                                 fb_pad_unaligned_buffer(dst, pitch, src, idx,
161                                                 image.height, shift_high,
162                                                 shift_low, mod);
163                                 shift_low += mod;
164                                 dst += (shift_low >= 8) ? width : width - 1;
165                                 shift_low &= 7;
166                                 shift_high = 8 - shift_low;
167                         }
168                 } else {
169                         while (k--) {
170                                 src = vc->vc_font.data + (scr_readw(s++)&
171                                                           charmask)*cellsize;
172
173                                 if (attribute) {
174                                         update_attr(buf, src, attribute, vc);
175                                         src = buf;
176                                 }
177
178                                 if (idx == 1)
179                                         for(i=0; i < image.height; i++)
180                                                 dst[pitch*i] = src[i];
181                                 else
182                                         fb_pad_aligned_buffer(dst, pitch, src, idx, image.height);
183                                 dst += width;
184                         }
185                 }
186                 info->fbops->fb_imageblit(info, &image);
187                 image.dx += cnt * vc->vc_font.width;
188                 count -= cnt;
189         }
190
191         /* buf is always NULL except when in monochrome mode, so in this case
192            it's a gain to check buf against NULL even though kfree() handles
193            NULL pointers just fine */
194         if (unlikely(buf))
195                 kfree(buf);
196 }
197
198 static void bit_clear_margins(struct vc_data *vc, struct fb_info *info,
199                               int bottom_only)
200 {
201         int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
202         unsigned int cw = vc->vc_font.width;
203         unsigned int ch = vc->vc_font.height;
204         unsigned int rw = info->var.xres - (vc->vc_cols*cw);
205         unsigned int bh = info->var.yres - (vc->vc_rows*ch);
206         unsigned int rs = info->var.xres - rw;
207         unsigned int bs = info->var.yres - bh;
208         struct fb_fillrect region;
209
210         region.color = attr_bgcol_ec(bgshift, vc);
211         region.rop = ROP_COPY;
212
213         if (rw && !bottom_only) {
214                 region.dx = info->var.xoffset + rs;
215                 region.dy = 0;
216                 region.width = rw;
217                 region.height = info->var.yres_virtual;
218                 info->fbops->fb_fillrect(info, &region);
219         }
220
221         if (bh) {
222                 region.dx = info->var.xoffset;
223                 region.dy = info->var.yoffset + bs;
224                 region.width = rs;
225                 region.height = bh;
226                 info->fbops->fb_fillrect(info, &region);
227         }
228 }
229
230 static void bit_cursor(struct vc_data *vc, struct fb_info *info,
231                        struct display *p, int mode, int softback_lines, int fg, int bg)
232 {
233         struct fb_cursor cursor;
234         struct fbcon_ops *ops = (struct fbcon_ops *) info->fbcon_par;
235         unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
236         int w = (vc->vc_font.width + 7) >> 3, c;
237         int y = real_y(p, vc->vc_y);
238         int attribute, use_sw = (vc->vc_cursor_type & 0x10);
239         char *src;
240
241         cursor.set = 0;
242
243         if (softback_lines) {
244                 if (y + softback_lines >= vc->vc_rows) {
245                         mode = CM_ERASE;
246                         ops->cursor_flash = 0;
247                         return;
248                 } else
249                         y += softback_lines;
250         }
251
252         c = scr_readw((u16 *) vc->vc_pos);
253         attribute = get_attribute(info, c);
254         src = vc->vc_font.data + ((c & charmask) * (w * vc->vc_font.height));
255
256         if (ops->cursor_state.image.data != src ||
257             ops->cursor_reset) {
258             ops->cursor_state.image.data = src;
259             cursor.set |= FB_CUR_SETIMAGE;
260         }
261
262         if (attribute) {
263                 u8 *dst;
264
265                 dst = kmalloc(w * vc->vc_font.height, GFP_ATOMIC);
266                 if (!dst)
267                         return;
268                 kfree(ops->cursor_data);
269                 ops->cursor_data = dst;
270                 update_attr(dst, src, attribute, vc);
271                 src = dst;
272         }
273
274         if (ops->cursor_state.image.fg_color != fg ||
275             ops->cursor_state.image.bg_color != bg ||
276             ops->cursor_reset) {
277                 ops->cursor_state.image.fg_color = fg;
278                 ops->cursor_state.image.bg_color = bg;
279                 cursor.set |= FB_CUR_SETCMAP;
280         }
281
282         if ((ops->cursor_state.image.dx != (vc->vc_font.width * vc->vc_x)) ||
283             (ops->cursor_state.image.dy != (vc->vc_font.height * y)) ||
284             ops->cursor_reset) {
285                 ops->cursor_state.image.dx = vc->vc_font.width * vc->vc_x;
286                 ops->cursor_state.image.dy = vc->vc_font.height * y;
287                 cursor.set |= FB_CUR_SETPOS;
288         }
289
290         if (ops->cursor_state.image.height != vc->vc_font.height ||
291             ops->cursor_state.image.width != vc->vc_font.width ||
292             ops->cursor_reset) {
293                 ops->cursor_state.image.height = vc->vc_font.height;
294                 ops->cursor_state.image.width = vc->vc_font.width;
295                 cursor.set |= FB_CUR_SETSIZE;
296         }
297
298         if (ops->cursor_state.hot.x || ops->cursor_state.hot.y ||
299             ops->cursor_reset) {
300                 ops->cursor_state.hot.x = cursor.hot.y = 0;
301                 cursor.set |= FB_CUR_SETHOT;
302         }
303
304         if (cursor.set & FB_CUR_SETSIZE ||
305             vc->vc_cursor_type != p->cursor_shape ||
306             ops->cursor_state.mask == NULL ||
307             ops->cursor_reset) {
308                 char *mask = kmalloc(w*vc->vc_font.height, GFP_ATOMIC);
309                 int cur_height, size, i = 0;
310                 u8 msk = 0xff;
311
312                 if (!mask)
313                         return;
314
315                 kfree(ops->cursor_state.mask);
316                 ops->cursor_state.mask = mask;
317
318                 p->cursor_shape = vc->vc_cursor_type;
319                 cursor.set |= FB_CUR_SETSHAPE;
320
321                 switch (p->cursor_shape & CUR_HWMASK) {
322                 case CUR_NONE:
323                         cur_height = 0;
324                         break;
325                 case CUR_UNDERLINE:
326                         cur_height = (vc->vc_font.height < 10) ? 1 : 2;
327                         break;
328                 case CUR_LOWER_THIRD:
329                         cur_height = vc->vc_font.height/3;
330                         break;
331                 case CUR_LOWER_HALF:
332                         cur_height = vc->vc_font.height >> 1;
333                         break;
334                 case CUR_TWO_THIRDS:
335                         cur_height = (vc->vc_font.height << 1)/3;
336                         break;
337                 case CUR_BLOCK:
338                 default:
339                         cur_height = vc->vc_font.height;
340                         break;
341                 }
342                 size = (vc->vc_font.height - cur_height) * w;
343                 while (size--)
344                         mask[i++] = ~msk;
345                 size = cur_height * w;
346                 while (size--)
347                         mask[i++] = msk;
348         }
349
350         switch (mode) {
351         case CM_ERASE:
352                 ops->cursor_state.enable = 0;
353                 break;
354         case CM_DRAW:
355         case CM_MOVE:
356         default:
357                 ops->cursor_state.enable = (use_sw) ? 0 : 1;
358                 break;
359         }
360
361         cursor.image.data = src;
362         cursor.image.fg_color = ops->cursor_state.image.fg_color;
363         cursor.image.bg_color = ops->cursor_state.image.bg_color;
364         cursor.image.dx = ops->cursor_state.image.dx;
365         cursor.image.dy = ops->cursor_state.image.dy;
366         cursor.image.height = ops->cursor_state.image.height;
367         cursor.image.width = ops->cursor_state.image.width;
368         cursor.hot.x = ops->cursor_state.hot.x;
369         cursor.hot.y = ops->cursor_state.hot.y;
370         cursor.mask = ops->cursor_state.mask;
371         cursor.enable = ops->cursor_state.enable;
372         cursor.image.depth = 1;
373         cursor.rop = ROP_XOR;
374
375         info->fbops->fb_cursor(info, &cursor);
376
377         ops->cursor_reset = 0;
378 }
379
380 void fbcon_set_bitops(struct fbcon_ops *ops)
381 {
382         ops->bmove = bit_bmove;
383         ops->clear = bit_clear;
384         ops->putcs = bit_putcs;
385         ops->clear_margins = bit_clear_margins;
386         ops->cursor = bit_cursor;
387 }
388
389 EXPORT_SYMBOL(fbcon_set_bitops);
390
391 MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
392 MODULE_DESCRIPTION("Bit Blitting Operation");
393 MODULE_LICENSE("GPL");
394