Merge /spare/repo/linux-2.6/
[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 inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info,
107                                      const u16 *s, u32 attr, u32 cnt,
108                                      u32 d_pitch, u32 s_pitch, u32 cellsize,
109                                      struct fb_image *image, u8 *buf, u8 *dst)
110 {
111         u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
112         u32 idx = vc->vc_font.width >> 3;
113         u8 *src;
114
115         while (cnt--) {
116                 src = vc->vc_font.data + (scr_readw(s++)&
117                                           charmask)*cellsize;
118
119                 if (attr) {
120                         update_attr(buf, src, attr, vc);
121                         src = buf;
122                 }
123
124                 if (likely(idx == 1))
125                         __fb_pad_aligned_buffer(dst, d_pitch, src, idx,
126                                                 image->height);
127                 else
128                         fb_pad_aligned_buffer(dst, d_pitch, src, idx,
129                                               image->height);
130
131                 dst += s_pitch;
132         }
133
134         info->fbops->fb_imageblit(info, image);
135 }
136
137 static inline void bit_putcs_unaligned(struct vc_data *vc,
138                                        struct fb_info *info, const u16 *s,
139                                        u32 attr, u32 cnt, u32 d_pitch,
140                                        u32 s_pitch, u32 cellsize,
141                                        struct fb_image *image, u8 *buf,
142                                        u8 *dst)
143 {
144         u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
145         u32 shift_low = 0, mod = vc->vc_font.width % 8;
146         u32 shift_high = 8;
147         u32 idx = vc->vc_font.width >> 3;
148         u8 *src;
149
150         while (cnt--) {
151                 src = vc->vc_font.data + (scr_readw(s++)&
152                                           charmask)*cellsize;
153
154                 if (attr) {
155                         update_attr(buf, src, attr, vc);
156                         src = buf;
157                 }
158
159                 fb_pad_unaligned_buffer(dst, d_pitch, src, idx,
160                                         image->height, shift_high,
161                                         shift_low, mod);
162                 shift_low += mod;
163                 dst += (shift_low >= 8) ? s_pitch : s_pitch - 1;
164                 shift_low &= 7;
165                 shift_high = 8 - shift_low;
166         }
167
168         info->fbops->fb_imageblit(info, image);
169
170 }
171
172 static void bit_putcs(struct vc_data *vc, struct fb_info *info,
173                       const unsigned short *s, int count, int yy, int xx,
174                       int fg, int bg)
175 {
176         struct fb_image image;
177         u32 width = (vc->vc_font.width + 7)/8;
178         u32 cellsize = width * vc->vc_font.height;
179         u32 maxcnt = info->pixmap.size/cellsize;
180         u32 scan_align = info->pixmap.scan_align - 1;
181         u32 buf_align = info->pixmap.buf_align - 1;
182         u32 mod = vc->vc_font.width % 8, cnt, pitch, size;
183         u32 attribute = get_attribute(info, scr_readw(s));
184         u8 *dst, *buf = NULL;
185
186         image.fg_color = fg;
187         image.bg_color = bg;
188         image.dx = xx * vc->vc_font.width;
189         image.dy = yy * vc->vc_font.height;
190         image.height = vc->vc_font.height;
191         image.depth = 1;
192
193         if (attribute) {
194                 buf = kmalloc(cellsize, GFP_KERNEL);
195                 if (!buf)
196                         return;
197         }
198
199         while (count) {
200                 if (count > maxcnt)
201                         cnt = maxcnt;
202                 else
203                         cnt = count;
204
205                 image.width = vc->vc_font.width * cnt;
206                 pitch = ((image.width + 7) >> 3) + scan_align;
207                 pitch &= ~scan_align;
208                 size = pitch * image.height + buf_align;
209                 size &= ~buf_align;
210                 dst = fb_get_buffer_offset(info, &info->pixmap, size);
211                 image.data = dst;
212
213                 if (!mod)
214                         bit_putcs_aligned(vc, info, s, attribute, cnt, pitch,
215                                           width, cellsize, &image, buf, dst);
216                 else
217                         bit_putcs_unaligned(vc, info, s, attribute, cnt,
218                                             pitch, width, cellsize, &image,
219                                             buf, dst);
220
221                 image.dx += cnt * vc->vc_font.width;
222                 count -= cnt;
223                 s += cnt;
224         }
225
226         /* buf is always NULL except when in monochrome mode, so in this case
227            it's a gain to check buf against NULL even though kfree() handles
228            NULL pointers just fine */
229         if (unlikely(buf))
230                 kfree(buf);
231
232 }
233
234 static void bit_clear_margins(struct vc_data *vc, struct fb_info *info,
235                               int bottom_only)
236 {
237         int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
238         unsigned int cw = vc->vc_font.width;
239         unsigned int ch = vc->vc_font.height;
240         unsigned int rw = info->var.xres - (vc->vc_cols*cw);
241         unsigned int bh = info->var.yres - (vc->vc_rows*ch);
242         unsigned int rs = info->var.xres - rw;
243         unsigned int bs = info->var.yres - bh;
244         struct fb_fillrect region;
245
246         region.color = attr_bgcol_ec(bgshift, vc);
247         region.rop = ROP_COPY;
248
249         if (rw && !bottom_only) {
250                 region.dx = info->var.xoffset + rs;
251                 region.dy = 0;
252                 region.width = rw;
253                 region.height = info->var.yres_virtual;
254                 info->fbops->fb_fillrect(info, &region);
255         }
256
257         if (bh) {
258                 region.dx = info->var.xoffset;
259                 region.dy = info->var.yoffset + bs;
260                 region.width = rs;
261                 region.height = bh;
262                 info->fbops->fb_fillrect(info, &region);
263         }
264 }
265
266 static void bit_cursor(struct vc_data *vc, struct fb_info *info,
267                        struct display *p, int mode, int softback_lines, int fg, int bg)
268 {
269         struct fb_cursor cursor;
270         struct fbcon_ops *ops = (struct fbcon_ops *) info->fbcon_par;
271         unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
272         int w = (vc->vc_font.width + 7) >> 3, c;
273         int y = real_y(p, vc->vc_y);
274         int attribute, use_sw = (vc->vc_cursor_type & 0x10);
275         char *src;
276
277         cursor.set = 0;
278
279         if (softback_lines) {
280                 if (y + softback_lines >= vc->vc_rows) {
281                         mode = CM_ERASE;
282                         ops->cursor_flash = 0;
283                         return;
284                 } else
285                         y += softback_lines;
286         }
287
288         c = scr_readw((u16 *) vc->vc_pos);
289         attribute = get_attribute(info, c);
290         src = vc->vc_font.data + ((c & charmask) * (w * vc->vc_font.height));
291
292         if (ops->cursor_state.image.data != src ||
293             ops->cursor_reset) {
294             ops->cursor_state.image.data = src;
295             cursor.set |= FB_CUR_SETIMAGE;
296         }
297
298         if (attribute) {
299                 u8 *dst;
300
301                 dst = kmalloc(w * vc->vc_font.height, GFP_ATOMIC);
302                 if (!dst)
303                         return;
304                 kfree(ops->cursor_data);
305                 ops->cursor_data = dst;
306                 update_attr(dst, src, attribute, vc);
307                 src = dst;
308         }
309
310         if (ops->cursor_state.image.fg_color != fg ||
311             ops->cursor_state.image.bg_color != bg ||
312             ops->cursor_reset) {
313                 ops->cursor_state.image.fg_color = fg;
314                 ops->cursor_state.image.bg_color = bg;
315                 cursor.set |= FB_CUR_SETCMAP;
316         }
317
318         if ((ops->cursor_state.image.dx != (vc->vc_font.width * vc->vc_x)) ||
319             (ops->cursor_state.image.dy != (vc->vc_font.height * y)) ||
320             ops->cursor_reset) {
321                 ops->cursor_state.image.dx = vc->vc_font.width * vc->vc_x;
322                 ops->cursor_state.image.dy = vc->vc_font.height * y;
323                 cursor.set |= FB_CUR_SETPOS;
324         }
325
326         if (ops->cursor_state.image.height != vc->vc_font.height ||
327             ops->cursor_state.image.width != vc->vc_font.width ||
328             ops->cursor_reset) {
329                 ops->cursor_state.image.height = vc->vc_font.height;
330                 ops->cursor_state.image.width = vc->vc_font.width;
331                 cursor.set |= FB_CUR_SETSIZE;
332         }
333
334         if (ops->cursor_state.hot.x || ops->cursor_state.hot.y ||
335             ops->cursor_reset) {
336                 ops->cursor_state.hot.x = cursor.hot.y = 0;
337                 cursor.set |= FB_CUR_SETHOT;
338         }
339
340         if (cursor.set & FB_CUR_SETSIZE ||
341             vc->vc_cursor_type != p->cursor_shape ||
342             ops->cursor_state.mask == NULL ||
343             ops->cursor_reset) {
344                 char *mask = kmalloc(w*vc->vc_font.height, GFP_ATOMIC);
345                 int cur_height, size, i = 0;
346                 u8 msk = 0xff;
347
348                 if (!mask)
349                         return;
350
351                 kfree(ops->cursor_state.mask);
352                 ops->cursor_state.mask = mask;
353
354                 p->cursor_shape = vc->vc_cursor_type;
355                 cursor.set |= FB_CUR_SETSHAPE;
356
357                 switch (p->cursor_shape & CUR_HWMASK) {
358                 case CUR_NONE:
359                         cur_height = 0;
360                         break;
361                 case CUR_UNDERLINE:
362                         cur_height = (vc->vc_font.height < 10) ? 1 : 2;
363                         break;
364                 case CUR_LOWER_THIRD:
365                         cur_height = vc->vc_font.height/3;
366                         break;
367                 case CUR_LOWER_HALF:
368                         cur_height = vc->vc_font.height >> 1;
369                         break;
370                 case CUR_TWO_THIRDS:
371                         cur_height = (vc->vc_font.height << 1)/3;
372                         break;
373                 case CUR_BLOCK:
374                 default:
375                         cur_height = vc->vc_font.height;
376                         break;
377                 }
378                 size = (vc->vc_font.height - cur_height) * w;
379                 while (size--)
380                         mask[i++] = ~msk;
381                 size = cur_height * w;
382                 while (size--)
383                         mask[i++] = msk;
384         }
385
386         switch (mode) {
387         case CM_ERASE:
388                 ops->cursor_state.enable = 0;
389                 break;
390         case CM_DRAW:
391         case CM_MOVE:
392         default:
393                 ops->cursor_state.enable = (use_sw) ? 0 : 1;
394                 break;
395         }
396
397         cursor.image.data = src;
398         cursor.image.fg_color = ops->cursor_state.image.fg_color;
399         cursor.image.bg_color = ops->cursor_state.image.bg_color;
400         cursor.image.dx = ops->cursor_state.image.dx;
401         cursor.image.dy = ops->cursor_state.image.dy;
402         cursor.image.height = ops->cursor_state.image.height;
403         cursor.image.width = ops->cursor_state.image.width;
404         cursor.hot.x = ops->cursor_state.hot.x;
405         cursor.hot.y = ops->cursor_state.hot.y;
406         cursor.mask = ops->cursor_state.mask;
407         cursor.enable = ops->cursor_state.enable;
408         cursor.image.depth = 1;
409         cursor.rop = ROP_XOR;
410
411         info->fbops->fb_cursor(info, &cursor);
412
413         ops->cursor_reset = 0;
414 }
415
416 void fbcon_set_bitops(struct fbcon_ops *ops)
417 {
418         ops->bmove = bit_bmove;
419         ops->clear = bit_clear;
420         ops->putcs = bit_putcs;
421         ops->clear_margins = bit_clear_margins;
422         ops->cursor = bit_cursor;
423 }
424
425 EXPORT_SYMBOL(fbcon_set_bitops);
426
427 MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
428 MODULE_DESCRIPTION("Bit Blitting Operation");
429 MODULE_LICENSE("GPL");
430