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/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 22 /* 23 * Accelerated handlers. 24 */ 25 static void update_attr(u8 *dst, u8 *src, int attribute, 26 struct vc_data *vc) 27 { 28 int i, offset = (vc->vc_font.height < 10) ? 1 : 2; 29 int width = DIV_ROUND_UP(vc->vc_font.width, 8); 30 unsigned int cellsize = vc->vc_font.height * width; 31 u8 c; 32 33 offset = cellsize - (offset * width); 34 for (i = 0; i < cellsize; i++) { 35 c = src[i]; 36 if (attribute & FBCON_ATTRIBUTE_UNDERLINE && i >= offset) 37 c = 0xff; 38 if (attribute & FBCON_ATTRIBUTE_BOLD) 39 c |= c >> 1; 40 if (attribute & FBCON_ATTRIBUTE_REVERSE) 41 c = ~c; 42 dst[i] = c; 43 } 44 } 45 46 static void bit_bmove(struct vc_data *vc, struct fb_info *info, int sy, 47 int sx, int dy, int dx, int height, int width) 48 { 49 struct fb_copyarea area; 50 51 area.sx = sx * vc->vc_font.width; 52 area.sy = sy * vc->vc_font.height; 53 area.dx = dx * vc->vc_font.width; 54 area.dy = dy * vc->vc_font.height; 55 area.height = height * vc->vc_font.height; 56 area.width = width * vc->vc_font.width; 57 58 info->fbops->fb_copyarea(info, &area); 59 } 60 61 static void bit_clear(struct vc_data *vc, struct fb_info *info, int sy, 62 int sx, int height, int width, int fg, int bg) 63 { 64 struct fb_fillrect region; 65 66 region.color = bg; 67 region.dx = sx * vc->vc_font.width; 68 region.dy = sy * vc->vc_font.height; 69 region.width = width * vc->vc_font.width; 70 region.height = height * vc->vc_font.height; 71 region.rop = ROP_COPY; 72 73 info->fbops->fb_fillrect(info, ®ion); 74 } 75 76 static inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info, 77 const u16 *s, u32 attr, u32 cnt, 78 u32 d_pitch, u32 s_pitch, u32 cellsize, 79 struct fb_image *image, u8 *buf, u8 *dst) 80 { 81 u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff; 82 u32 idx = vc->vc_font.width >> 3; 83 u8 *src; 84 85 while (cnt--) { 86 src = vc->vc_font.data + (scr_readw(s++)& 87 charmask)*cellsize; 88 89 if (attr) { 90 update_attr(buf, src, attr, vc); 91 src = buf; 92 } 93 94 if (likely(idx == 1)) 95 __fb_pad_aligned_buffer(dst, d_pitch, src, idx, 96 image->height); 97 else 98 fb_pad_aligned_buffer(dst, d_pitch, src, idx, 99 image->height); 100 101 dst += s_pitch; 102 } 103 104 info->fbops->fb_imageblit(info, image); 105 } 106 107 static inline void bit_putcs_unaligned(struct vc_data *vc, 108 struct fb_info *info, const u16 *s, 109 u32 attr, u32 cnt, u32 d_pitch, 110 u32 s_pitch, u32 cellsize, 111 struct fb_image *image, u8 *buf, 112 u8 *dst) 113 { 114 u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff; 115 u32 shift_low = 0, mod = vc->vc_font.width % 8; 116 u32 shift_high = 8; 117 u32 idx = vc->vc_font.width >> 3; 118 u8 *src; 119 120 while (cnt--) { 121 src = vc->vc_font.data + (scr_readw(s++)& 122 charmask)*cellsize; 123 124 if (attr) { 125 update_attr(buf, src, attr, vc); 126 src = buf; 127 } 128 129 fb_pad_unaligned_buffer(dst, d_pitch, src, idx, 130 image->height, shift_high, 131 shift_low, mod); 132 shift_low += mod; 133 dst += (shift_low >= 8) ? s_pitch : s_pitch - 1; 134 shift_low &= 7; 135 shift_high = 8 - shift_low; 136 } 137 138 info->fbops->fb_imageblit(info, image); 139 140 } 141 142 static void bit_putcs(struct vc_data *vc, struct fb_info *info, 143 const unsigned short *s, int count, int yy, int xx, 144 int fg, int bg) 145 { 146 struct fb_image image; 147 u32 width = DIV_ROUND_UP(vc->vc_font.width, 8); 148 u32 cellsize = width * vc->vc_font.height; 149 u32 maxcnt = info->pixmap.size/cellsize; 150 u32 scan_align = info->pixmap.scan_align - 1; 151 u32 buf_align = info->pixmap.buf_align - 1; 152 u32 mod = vc->vc_font.width % 8, cnt, pitch, size; 153 u32 attribute = get_attribute(info, scr_readw(s)); 154 u8 *dst, *buf = NULL; 155 156 image.fg_color = fg; 157 image.bg_color = bg; 158 image.dx = xx * vc->vc_font.width; 159 image.dy = yy * vc->vc_font.height; 160 image.height = vc->vc_font.height; 161 image.depth = 1; 162 163 if (image.dy >= info->var.yres) 164 return; 165 166 image.height = min(image.height, info->var.yres - image.dy); 167 168 if (attribute) { 169 buf = kmalloc(cellsize, GFP_ATOMIC); 170 if (!buf) 171 return; 172 } 173 174 while (count) { 175 if (count > maxcnt) 176 cnt = maxcnt; 177 else 178 cnt = count; 179 180 image.width = vc->vc_font.width * cnt; 181 182 if (image.dx >= info->var.xres) 183 break; 184 185 if (image.dx + image.width > info->var.xres) { 186 image.width = info->var.xres - image.dx; 187 cnt = image.width / vc->vc_font.width; 188 if (cnt == 0) 189 break; 190 image.width = cnt * vc->vc_font.width; 191 } 192 193 pitch = DIV_ROUND_UP(image.width, 8) + scan_align; 194 pitch &= ~scan_align; 195 size = pitch * image.height + buf_align; 196 size &= ~buf_align; 197 dst = fb_get_buffer_offset(info, &info->pixmap, size); 198 image.data = dst; 199 200 if (!mod) 201 bit_putcs_aligned(vc, info, s, attribute, cnt, pitch, 202 width, cellsize, &image, buf, dst); 203 else 204 bit_putcs_unaligned(vc, info, s, attribute, cnt, 205 pitch, width, cellsize, &image, 206 buf, dst); 207 208 image.dx += cnt * vc->vc_font.width; 209 count -= cnt; 210 s += cnt; 211 } 212 213 /* buf is always NULL except when in monochrome mode, so in this case 214 it's a gain to check buf against NULL even though kfree() handles 215 NULL pointers just fine */ 216 if (unlikely(buf)) 217 kfree(buf); 218 219 } 220 221 static void bit_clear_margins(struct vc_data *vc, struct fb_info *info, 222 int color, int bottom_only) 223 { 224 unsigned int cw = vc->vc_font.width; 225 unsigned int ch = vc->vc_font.height; 226 unsigned int rw = info->var.xres - (vc->vc_cols*cw); 227 unsigned int bh = info->var.yres - (vc->vc_rows*ch); 228 unsigned int rs = info->var.xres - rw; 229 unsigned int bs = info->var.yres - bh; 230 struct fb_fillrect region; 231 232 region.color = color; 233 region.rop = ROP_COPY; 234 235 if ((int) rw > 0 && !bottom_only) { 236 region.dx = info->var.xoffset + rs; 237 region.dy = 0; 238 region.width = rw; 239 region.height = info->var.yres_virtual; 240 info->fbops->fb_fillrect(info, ®ion); 241 } 242 243 if ((int) bh > 0) { 244 region.dx = info->var.xoffset; 245 region.dy = info->var.yoffset + bs; 246 region.width = rs; 247 region.height = bh; 248 info->fbops->fb_fillrect(info, ®ion); 249 } 250 } 251 252 static void bit_cursor(struct vc_data *vc, struct fb_info *info, bool enable, 253 int fg, int bg) 254 { 255 struct fb_cursor cursor; 256 struct fbcon_ops *ops = info->fbcon_par; 257 unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff; 258 int w = DIV_ROUND_UP(vc->vc_font.width, 8), c; 259 int y = real_y(ops->p, vc->state.y); 260 int attribute, use_sw = vc->vc_cursor_type & CUR_SW; 261 int err = 1; 262 char *src; 263 264 cursor.set = 0; 265 266 if (!vc->vc_font.data) 267 return; 268 269 c = scr_readw((u16 *) vc->vc_pos); 270 attribute = get_attribute(info, c); 271 src = vc->vc_font.data + ((c & charmask) * (w * vc->vc_font.height)); 272 273 if (ops->cursor_state.image.data != src || 274 ops->cursor_reset) { 275 ops->cursor_state.image.data = src; 276 cursor.set |= FB_CUR_SETIMAGE; 277 } 278 279 if (attribute) { 280 u8 *dst; 281 282 dst = kmalloc_array(w, vc->vc_font.height, GFP_ATOMIC); 283 if (!dst) 284 return; 285 kfree(ops->cursor_data); 286 ops->cursor_data = dst; 287 update_attr(dst, src, attribute, vc); 288 src = dst; 289 } 290 291 if (ops->cursor_state.image.fg_color != fg || 292 ops->cursor_state.image.bg_color != bg || 293 ops->cursor_reset) { 294 ops->cursor_state.image.fg_color = fg; 295 ops->cursor_state.image.bg_color = bg; 296 cursor.set |= FB_CUR_SETCMAP; 297 } 298 299 if ((ops->cursor_state.image.dx != (vc->vc_font.width * vc->state.x)) || 300 (ops->cursor_state.image.dy != (vc->vc_font.height * y)) || 301 ops->cursor_reset) { 302 ops->cursor_state.image.dx = vc->vc_font.width * vc->state.x; 303 ops->cursor_state.image.dy = vc->vc_font.height * y; 304 cursor.set |= FB_CUR_SETPOS; 305 } 306 307 if (ops->cursor_state.image.height != vc->vc_font.height || 308 ops->cursor_state.image.width != vc->vc_font.width || 309 ops->cursor_reset) { 310 ops->cursor_state.image.height = vc->vc_font.height; 311 ops->cursor_state.image.width = vc->vc_font.width; 312 cursor.set |= FB_CUR_SETSIZE; 313 } 314 315 if (ops->cursor_state.hot.x || ops->cursor_state.hot.y || 316 ops->cursor_reset) { 317 ops->cursor_state.hot.x = cursor.hot.y = 0; 318 cursor.set |= FB_CUR_SETHOT; 319 } 320 321 if (cursor.set & FB_CUR_SETSIZE || 322 vc->vc_cursor_type != ops->p->cursor_shape || 323 ops->cursor_state.mask == NULL || 324 ops->cursor_reset) { 325 char *mask = kmalloc_array(w, vc->vc_font.height, GFP_ATOMIC); 326 int cur_height, size, i = 0; 327 u8 msk = 0xff; 328 329 if (!mask) 330 return; 331 332 kfree(ops->cursor_state.mask); 333 ops->cursor_state.mask = mask; 334 335 ops->p->cursor_shape = vc->vc_cursor_type; 336 cursor.set |= FB_CUR_SETSHAPE; 337 338 switch (CUR_SIZE(ops->p->cursor_shape)) { 339 case CUR_NONE: 340 cur_height = 0; 341 break; 342 case CUR_UNDERLINE: 343 cur_height = (vc->vc_font.height < 10) ? 1 : 2; 344 break; 345 case CUR_LOWER_THIRD: 346 cur_height = vc->vc_font.height/3; 347 break; 348 case CUR_LOWER_HALF: 349 cur_height = vc->vc_font.height >> 1; 350 break; 351 case CUR_TWO_THIRDS: 352 cur_height = (vc->vc_font.height << 1)/3; 353 break; 354 case CUR_BLOCK: 355 default: 356 cur_height = vc->vc_font.height; 357 break; 358 } 359 size = (vc->vc_font.height - cur_height) * w; 360 while (size--) 361 mask[i++] = ~msk; 362 size = cur_height * w; 363 while (size--) 364 mask[i++] = msk; 365 } 366 367 ops->cursor_state.enable = enable && !use_sw; 368 369 cursor.image.data = src; 370 cursor.image.fg_color = ops->cursor_state.image.fg_color; 371 cursor.image.bg_color = ops->cursor_state.image.bg_color; 372 cursor.image.dx = ops->cursor_state.image.dx; 373 cursor.image.dy = ops->cursor_state.image.dy; 374 cursor.image.height = ops->cursor_state.image.height; 375 cursor.image.width = ops->cursor_state.image.width; 376 cursor.hot.x = ops->cursor_state.hot.x; 377 cursor.hot.y = ops->cursor_state.hot.y; 378 cursor.mask = ops->cursor_state.mask; 379 cursor.enable = ops->cursor_state.enable; 380 cursor.image.depth = 1; 381 cursor.rop = ROP_XOR; 382 383 if (info->fbops->fb_cursor) 384 err = info->fbops->fb_cursor(info, &cursor); 385 386 if (err) 387 soft_cursor(info, &cursor); 388 389 ops->cursor_reset = 0; 390 } 391 392 static int bit_update_start(struct fb_info *info) 393 { 394 struct fbcon_ops *ops = info->fbcon_par; 395 int err; 396 397 err = fb_pan_display(info, &ops->var); 398 ops->var.xoffset = info->var.xoffset; 399 ops->var.yoffset = info->var.yoffset; 400 ops->var.vmode = info->var.vmode; 401 return err; 402 } 403 404 void fbcon_set_bitops(struct fbcon_ops *ops) 405 { 406 ops->bmove = bit_bmove; 407 ops->clear = bit_clear; 408 ops->putcs = bit_putcs; 409 ops->clear_margins = bit_clear_margins; 410 ops->cursor = bit_cursor; 411 ops->update_start = bit_update_start; 412 ops->rotate_font = NULL; 413 414 if (ops->rotate) 415 fbcon_set_rotate(ops); 416 } 417