[PATCH] fbcon/fbdev: Move softcursor out of fbdev to fbcon
[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 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         int err = 1;
276         char *src;
277
278         cursor.set = 0;
279
280         if (softback_lines) {
281                 if (y + softback_lines >= vc->vc_rows) {
282                         mode = CM_ERASE;
283                         ops->cursor_flash = 0;
284                         return;
285                 } else
286                         y += softback_lines;
287         }
288
289         c = scr_readw((u16 *) vc->vc_pos);
290         attribute = get_attribute(info, c);
291         src = vc->vc_font.data + ((c & charmask) * (w * vc->vc_font.height));
292
293         if (ops->cursor_state.image.data != src ||
294             ops->cursor_reset) {
295             ops->cursor_state.image.data = src;
296             cursor.set |= FB_CUR_SETIMAGE;
297         }
298
299         if (attribute) {
300                 u8 *dst;
301
302                 dst = kmalloc(w * vc->vc_font.height, GFP_ATOMIC);
303                 if (!dst)
304                         return;
305                 kfree(ops->cursor_data);
306                 ops->cursor_data = dst;
307                 update_attr(dst, src, attribute, vc);
308                 src = dst;
309         }
310
311         if (ops->cursor_state.image.fg_color != fg ||
312             ops->cursor_state.image.bg_color != bg ||
313             ops->cursor_reset) {
314                 ops->cursor_state.image.fg_color = fg;
315                 ops->cursor_state.image.bg_color = bg;
316                 cursor.set |= FB_CUR_SETCMAP;
317         }
318
319         if ((ops->cursor_state.image.dx != (vc->vc_font.width * vc->vc_x)) ||
320             (ops->cursor_state.image.dy != (vc->vc_font.height * y)) ||
321             ops->cursor_reset) {
322                 ops->cursor_state.image.dx = vc->vc_font.width * vc->vc_x;
323                 ops->cursor_state.image.dy = vc->vc_font.height * y;
324                 cursor.set |= FB_CUR_SETPOS;
325         }
326
327         if (ops->cursor_state.image.height != vc->vc_font.height ||
328             ops->cursor_state.image.width != vc->vc_font.width ||
329             ops->cursor_reset) {
330                 ops->cursor_state.image.height = vc->vc_font.height;
331                 ops->cursor_state.image.width = vc->vc_font.width;
332                 cursor.set |= FB_CUR_SETSIZE;
333         }
334
335         if (ops->cursor_state.hot.x || ops->cursor_state.hot.y ||
336             ops->cursor_reset) {
337                 ops->cursor_state.hot.x = cursor.hot.y = 0;
338                 cursor.set |= FB_CUR_SETHOT;
339         }
340
341         if (cursor.set & FB_CUR_SETSIZE ||
342             vc->vc_cursor_type != p->cursor_shape ||
343             ops->cursor_state.mask == NULL ||
344             ops->cursor_reset) {
345                 char *mask = kmalloc(w*vc->vc_font.height, GFP_ATOMIC);
346                 int cur_height, size, i = 0;
347                 u8 msk = 0xff;
348
349                 if (!mask)
350                         return;
351
352                 kfree(ops->cursor_state.mask);
353                 ops->cursor_state.mask = mask;
354
355                 p->cursor_shape = vc->vc_cursor_type;
356                 cursor.set |= FB_CUR_SETSHAPE;
357
358                 switch (p->cursor_shape & CUR_HWMASK) {
359                 case CUR_NONE:
360                         cur_height = 0;
361                         break;
362                 case CUR_UNDERLINE:
363                         cur_height = (vc->vc_font.height < 10) ? 1 : 2;
364                         break;
365                 case CUR_LOWER_THIRD:
366                         cur_height = vc->vc_font.height/3;
367                         break;
368                 case CUR_LOWER_HALF:
369                         cur_height = vc->vc_font.height >> 1;
370                         break;
371                 case CUR_TWO_THIRDS:
372                         cur_height = (vc->vc_font.height << 1)/3;
373                         break;
374                 case CUR_BLOCK:
375                 default:
376                         cur_height = vc->vc_font.height;
377                         break;
378                 }
379                 size = (vc->vc_font.height - cur_height) * w;
380                 while (size--)
381                         mask[i++] = ~msk;
382                 size = cur_height * w;
383                 while (size--)
384                         mask[i++] = msk;
385         }
386
387         switch (mode) {
388         case CM_ERASE:
389                 ops->cursor_state.enable = 0;
390                 break;
391         case CM_DRAW:
392         case CM_MOVE:
393         default:
394                 ops->cursor_state.enable = (use_sw) ? 0 : 1;
395                 break;
396         }
397
398         cursor.image.data = src;
399         cursor.image.fg_color = ops->cursor_state.image.fg_color;
400         cursor.image.bg_color = ops->cursor_state.image.bg_color;
401         cursor.image.dx = ops->cursor_state.image.dx;
402         cursor.image.dy = ops->cursor_state.image.dy;
403         cursor.image.height = ops->cursor_state.image.height;
404         cursor.image.width = ops->cursor_state.image.width;
405         cursor.hot.x = ops->cursor_state.hot.x;
406         cursor.hot.y = ops->cursor_state.hot.y;
407         cursor.mask = ops->cursor_state.mask;
408         cursor.enable = ops->cursor_state.enable;
409         cursor.image.depth = 1;
410         cursor.rop = ROP_XOR;
411
412         if (info->fbops->fb_cursor)
413                 err = info->fbops->fb_cursor(info, &cursor);
414
415         if (err)
416                 soft_cursor(info, &cursor);
417
418         ops->cursor_reset = 0;
419 }
420
421 void fbcon_set_bitops(struct fbcon_ops *ops)
422 {
423         ops->bmove = bit_bmove;
424         ops->clear = bit_clear;
425         ops->putcs = bit_putcs;
426         ops->clear_margins = bit_clear_margins;
427         ops->cursor = bit_cursor;
428 }
429
430 EXPORT_SYMBOL(fbcon_set_bitops);
431
432 MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
433 MODULE_DESCRIPTION("Bit Blitting Operation");
434 MODULE_LICENSE("GPL");
435