Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / video / console / bitblit.c
blob33712c033e314fab33fabc7c67980036f14973e3
1 /*
2 * linux/drivers/video/console/bitblit.c -- BitBlitting Operation
4 * Originally from the 'accel_*' routines in drivers/video/console/fbcon.c
6 * Copyright (C) 2004 Antonino Daplas <adaplas @pol.net>
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.
13 #include <linux/module.h>
14 #include <linux/slab.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 #include "fbcondecor.h"
24 * Accelerated handlers.
26 static void update_attr(u8 *dst, u8 *src, int attribute,
27 struct vc_data *vc)
29 int i, offset = (vc->vc_font.height < 10) ? 1 : 2;
30 int width = DIV_ROUND_UP(vc->vc_font.width, 8);
31 unsigned int cellsize = vc->vc_font.height * width;
32 u8 c;
34 offset = cellsize - (offset * width);
35 for (i = 0; i < cellsize; i++) {
36 c = src[i];
37 if (attribute & FBCON_ATTRIBUTE_UNDERLINE && i >= offset)
38 c = 0xff;
39 if (attribute & FBCON_ATTRIBUTE_BOLD)
40 c |= c >> 1;
41 if (attribute & FBCON_ATTRIBUTE_REVERSE)
42 c = ~c;
43 dst[i] = c;
47 static void bit_bmove(struct vc_data *vc, struct fb_info *info, int sy,
48 int sx, int dy, int dx, int height, int width)
50 struct fb_copyarea area;
52 area.sx = sx * vc->vc_font.width;
53 area.sy = sy * vc->vc_font.height;
54 area.dx = dx * vc->vc_font.width;
55 area.dy = dy * vc->vc_font.height;
56 area.height = height * vc->vc_font.height;
57 area.width = width * vc->vc_font.width;
59 if (fbcon_decor_active(info, vc)) {
60 area.sx += vc->vc_decor.tx;
61 area.sy += vc->vc_decor.ty;
62 area.dx += vc->vc_decor.tx;
63 area.dy += vc->vc_decor.ty;
66 info->fbops->fb_copyarea(info, &area);
69 static void bit_clear(struct vc_data *vc, struct fb_info *info, int sy,
70 int sx, int height, int width)
72 int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
73 struct fb_fillrect region;
75 region.color = attr_bgcol_ec(bgshift, vc, info);
76 region.dx = sx * vc->vc_font.width;
77 region.dy = sy * vc->vc_font.height;
78 region.width = width * vc->vc_font.width;
79 region.height = height * vc->vc_font.height;
80 region.rop = ROP_COPY;
82 info->fbops->fb_fillrect(info, &region);
85 static inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info,
86 const u16 *s, u32 attr, u32 cnt,
87 u32 d_pitch, u32 s_pitch, u32 cellsize,
88 struct fb_image *image, u8 *buf, u8 *dst)
90 u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
91 u32 idx = vc->vc_font.width >> 3;
92 u8 *src;
94 while (cnt--) {
95 src = vc->vc_font.data + (scr_readw(s++)&
96 charmask)*cellsize;
98 if (attr) {
99 update_attr(buf, src, attr, vc);
100 src = buf;
103 if (likely(idx == 1))
104 __fb_pad_aligned_buffer(dst, d_pitch, src, idx,
105 image->height);
106 else
107 fb_pad_aligned_buffer(dst, d_pitch, src, idx,
108 image->height);
110 dst += s_pitch;
113 info->fbops->fb_imageblit(info, image);
116 static inline void bit_putcs_unaligned(struct vc_data *vc,
117 struct fb_info *info, const u16 *s,
118 u32 attr, u32 cnt, u32 d_pitch,
119 u32 s_pitch, u32 cellsize,
120 struct fb_image *image, u8 *buf,
121 u8 *dst)
123 u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
124 u32 shift_low = 0, mod = vc->vc_font.width % 8;
125 u32 shift_high = 8;
126 u32 idx = vc->vc_font.width >> 3;
127 u8 *src;
129 while (cnt--) {
130 src = vc->vc_font.data + (scr_readw(s++)&
131 charmask)*cellsize;
133 if (attr) {
134 update_attr(buf, src, attr, vc);
135 src = buf;
138 fb_pad_unaligned_buffer(dst, d_pitch, src, idx,
139 image->height, shift_high,
140 shift_low, mod);
141 shift_low += mod;
142 dst += (shift_low >= 8) ? s_pitch : s_pitch - 1;
143 shift_low &= 7;
144 shift_high = 8 - shift_low;
147 info->fbops->fb_imageblit(info, image);
151 static void bit_putcs(struct vc_data *vc, struct fb_info *info,
152 const unsigned short *s, int count, int yy, int xx,
153 int fg, int bg)
155 struct fb_image image;
156 u32 width = DIV_ROUND_UP(vc->vc_font.width, 8);
157 u32 cellsize = width * vc->vc_font.height;
158 u32 maxcnt = info->pixmap.size/cellsize;
159 u32 scan_align = info->pixmap.scan_align - 1;
160 u32 buf_align = info->pixmap.buf_align - 1;
161 u32 mod = vc->vc_font.width % 8, cnt, pitch, size;
162 u32 attribute = get_attribute(info, scr_readw(s));
163 u8 *dst, *buf = NULL;
165 image.fg_color = fg;
166 image.bg_color = bg;
167 image.dx = xx * vc->vc_font.width;
168 image.dy = yy * vc->vc_font.height;
169 image.height = vc->vc_font.height;
170 image.depth = 1;
172 if (attribute) {
173 buf = kmalloc(cellsize, GFP_KERNEL);
174 if (!buf)
175 return;
178 while (count) {
179 if (count > maxcnt)
180 cnt = maxcnt;
181 else
182 cnt = count;
184 image.width = vc->vc_font.width * cnt;
185 pitch = DIV_ROUND_UP(image.width, 8) + scan_align;
186 pitch &= ~scan_align;
187 size = pitch * image.height + buf_align;
188 size &= ~buf_align;
189 dst = fb_get_buffer_offset(info, &info->pixmap, size);
190 image.data = dst;
192 if (!mod)
193 bit_putcs_aligned(vc, info, s, attribute, cnt, pitch,
194 width, cellsize, &image, buf, dst);
195 else
196 bit_putcs_unaligned(vc, info, s, attribute, cnt,
197 pitch, width, cellsize, &image,
198 buf, dst);
200 image.dx += cnt * vc->vc_font.width;
201 count -= cnt;
202 s += cnt;
205 /* buf is always NULL except when in monochrome mode, so in this case
206 it's a gain to check buf against NULL even though kfree() handles
207 NULL pointers just fine */
208 if (unlikely(buf))
209 kfree(buf);
213 static void bit_clear_margins(struct vc_data *vc, struct fb_info *info,
214 int bottom_only)
216 int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
217 unsigned int cw = vc->vc_font.width;
218 unsigned int ch = vc->vc_font.height;
219 unsigned int rw = info->var.xres - (vc->vc_cols*cw);
220 unsigned int bh = info->var.yres - (vc->vc_rows*ch);
221 unsigned int rs = info->var.xres - rw;
222 unsigned int bs = info->var.yres - bh;
223 struct fb_fillrect region;
225 region.color = attr_bgcol_ec(bgshift, vc, info);
226 region.rop = ROP_COPY;
228 if (rw && !bottom_only) {
229 region.dx = info->var.xoffset + rs;
230 region.dy = 0;
231 region.width = rw;
232 region.height = info->var.yres_virtual;
233 info->fbops->fb_fillrect(info, &region);
236 if (bh) {
237 region.dx = info->var.xoffset;
238 region.dy = info->var.yoffset + bs;
239 region.width = rs;
240 region.height = bh;
241 info->fbops->fb_fillrect(info, &region);
245 static void bit_cursor(struct vc_data *vc, struct fb_info *info, int mode,
246 int softback_lines, int fg, int bg)
248 struct fb_cursor cursor;
249 struct fbcon_ops *ops = info->fbcon_par;
250 unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
251 int w = DIV_ROUND_UP(vc->vc_font.width, 8), c;
252 int y = real_y(ops->p, vc->vc_y);
253 int attribute, use_sw = (vc->vc_cursor_type & 0x10);
254 int err = 1;
255 char *src;
257 cursor.set = 0;
259 if (softback_lines) {
260 if (y + softback_lines >= vc->vc_rows) {
261 mode = CM_ERASE;
262 ops->cursor_flash = 0;
263 return;
264 } else
265 y += softback_lines;
268 c = scr_readw((u16 *) vc->vc_pos);
269 attribute = get_attribute(info, c);
270 src = vc->vc_font.data + ((c & charmask) * (w * vc->vc_font.height));
272 if (ops->cursor_state.image.data != src ||
273 ops->cursor_reset) {
274 ops->cursor_state.image.data = src;
275 cursor.set |= FB_CUR_SETIMAGE;
278 if (attribute) {
279 u8 *dst;
281 dst = kmalloc(w * vc->vc_font.height, GFP_ATOMIC);
282 if (!dst)
283 return;
284 kfree(ops->cursor_data);
285 ops->cursor_data = dst;
286 update_attr(dst, src, attribute, vc);
287 src = dst;
290 if (ops->cursor_state.image.fg_color != fg ||
291 ops->cursor_state.image.bg_color != bg ||
292 ops->cursor_reset) {
293 ops->cursor_state.image.fg_color = fg;
294 ops->cursor_state.image.bg_color = bg;
295 cursor.set |= FB_CUR_SETCMAP;
298 if ((ops->cursor_state.image.dx != (vc->vc_font.width * vc->vc_x)) ||
299 (ops->cursor_state.image.dy != (vc->vc_font.height * y)) ||
300 ops->cursor_reset) {
301 ops->cursor_state.image.dx = vc->vc_font.width * vc->vc_x;
302 ops->cursor_state.image.dy = vc->vc_font.height * y;
303 cursor.set |= FB_CUR_SETPOS;
306 if (ops->cursor_state.image.height != vc->vc_font.height ||
307 ops->cursor_state.image.width != vc->vc_font.width ||
308 ops->cursor_reset) {
309 ops->cursor_state.image.height = vc->vc_font.height;
310 ops->cursor_state.image.width = vc->vc_font.width;
311 cursor.set |= FB_CUR_SETSIZE;
314 if (ops->cursor_state.hot.x || ops->cursor_state.hot.y ||
315 ops->cursor_reset) {
316 ops->cursor_state.hot.x = cursor.hot.y = 0;
317 cursor.set |= FB_CUR_SETHOT;
320 if (cursor.set & FB_CUR_SETSIZE ||
321 vc->vc_cursor_type != ops->p->cursor_shape ||
322 ops->cursor_state.mask == NULL ||
323 ops->cursor_reset) {
324 char *mask = kmalloc(w*vc->vc_font.height, GFP_ATOMIC);
325 int cur_height, size, i = 0;
326 u8 msk = 0xff;
328 if (!mask)
329 return;
331 kfree(ops->cursor_state.mask);
332 ops->cursor_state.mask = mask;
334 ops->p->cursor_shape = vc->vc_cursor_type;
335 cursor.set |= FB_CUR_SETSHAPE;
337 switch (ops->p->cursor_shape & CUR_HWMASK) {
338 case CUR_NONE:
339 cur_height = 0;
340 break;
341 case CUR_UNDERLINE:
342 cur_height = (vc->vc_font.height < 10) ? 1 : 2;
343 break;
344 case CUR_LOWER_THIRD:
345 cur_height = vc->vc_font.height/3;
346 break;
347 case CUR_LOWER_HALF:
348 cur_height = vc->vc_font.height >> 1;
349 break;
350 case CUR_TWO_THIRDS:
351 cur_height = (vc->vc_font.height << 1)/3;
352 break;
353 case CUR_BLOCK:
354 default:
355 cur_height = vc->vc_font.height;
356 break;
358 size = (vc->vc_font.height - cur_height) * w;
359 while (size--)
360 mask[i++] = ~msk;
361 size = cur_height * w;
362 while (size--)
363 mask[i++] = msk;
366 switch (mode) {
367 case CM_ERASE:
368 ops->cursor_state.enable = 0;
369 break;
370 case CM_DRAW:
371 case CM_MOVE:
372 default:
373 ops->cursor_state.enable = (use_sw) ? 0 : 1;
374 break;
377 cursor.image.data = src;
378 cursor.image.fg_color = ops->cursor_state.image.fg_color;
379 cursor.image.bg_color = ops->cursor_state.image.bg_color;
380 cursor.image.dx = ops->cursor_state.image.dx;
381 cursor.image.dy = ops->cursor_state.image.dy;
382 cursor.image.height = ops->cursor_state.image.height;
383 cursor.image.width = ops->cursor_state.image.width;
384 cursor.hot.x = ops->cursor_state.hot.x;
385 cursor.hot.y = ops->cursor_state.hot.y;
386 cursor.mask = ops->cursor_state.mask;
387 cursor.enable = ops->cursor_state.enable;
388 cursor.image.depth = 1;
389 cursor.rop = ROP_XOR;
391 if (fbcon_decor_active(info, vc)) {
392 fbcon_decor_cursor(info, &cursor);
393 } else {
394 if (info->fbops->fb_cursor)
395 err = info->fbops->fb_cursor(info, &cursor);
397 if (err)
398 soft_cursor(info, &cursor);
401 ops->cursor_reset = 0;
404 static int bit_update_start(struct fb_info *info)
406 struct fbcon_ops *ops = info->fbcon_par;
407 int err;
409 err = fb_pan_display(info, &ops->var);
410 ops->var.xoffset = info->var.xoffset;
411 ops->var.yoffset = info->var.yoffset;
412 ops->var.vmode = info->var.vmode;
413 return err;
416 void fbcon_set_bitops(struct fbcon_ops *ops)
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 ops->update_start = bit_update_start;
424 ops->rotate_font = NULL;
426 if (ops->rotate)
427 fbcon_set_rotate(ops);
430 EXPORT_SYMBOL(fbcon_set_bitops);
432 MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
433 MODULE_DESCRIPTION("Bit Blitting Operation");
434 MODULE_LICENSE("GPL");