bitblit.c 10 KB

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