bitblit.c 11 KB

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