bitblit.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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_pad_unaligned_buffer(dst, pitch, src, idx,
  137. image.height, shift_high,
  138. 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_pad_aligned_buffer(dst, pitch, src, idx, image.height);
  153. dst += width;
  154. }
  155. }
  156. info->fbops->fb_imageblit(info, &image);
  157. image.dx += cnt * vc->vc_font.width;
  158. count -= cnt;
  159. }
  160. /* buf is always NULL except when in monochrome mode, so in this case
  161. it's a gain to check buf against NULL even though kfree() handles
  162. NULL pointers just fine */
  163. if (unlikely(buf))
  164. kfree(buf);
  165. }
  166. static void bit_clear_margins(struct vc_data *vc, struct fb_info *info,
  167. int bottom_only)
  168. {
  169. int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
  170. unsigned int cw = vc->vc_font.width;
  171. unsigned int ch = vc->vc_font.height;
  172. unsigned int rw = info->var.xres - (vc->vc_cols*cw);
  173. unsigned int bh = info->var.yres - (vc->vc_rows*ch);
  174. unsigned int rs = info->var.xres - rw;
  175. unsigned int bs = info->var.yres - bh;
  176. struct fb_fillrect region;
  177. region.color = attr_bgcol_ec(bgshift, vc);
  178. region.rop = ROP_COPY;
  179. if (rw && !bottom_only) {
  180. region.dx = info->var.xoffset + rs;
  181. region.dy = 0;
  182. region.width = rw;
  183. region.height = info->var.yres_virtual;
  184. info->fbops->fb_fillrect(info, &region);
  185. }
  186. if (bh) {
  187. region.dx = info->var.xoffset;
  188. region.dy = info->var.yoffset + bs;
  189. region.width = rs;
  190. region.height = bh;
  191. info->fbops->fb_fillrect(info, &region);
  192. }
  193. }
  194. static void bit_cursor(struct vc_data *vc, struct fb_info *info,
  195. struct display *p, int mode, int softback_lines, int fg, int bg)
  196. {
  197. struct fb_cursor cursor;
  198. struct fbcon_ops *ops = (struct fbcon_ops *) info->fbcon_par;
  199. unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
  200. int w = (vc->vc_font.width + 7) >> 3, c;
  201. int y = real_y(p, vc->vc_y);
  202. int attribute, use_sw = (vc->vc_cursor_type & 0x10);
  203. char *src;
  204. cursor.set = 0;
  205. if (softback_lines) {
  206. if (y + softback_lines >= vc->vc_rows) {
  207. mode = CM_ERASE;
  208. ops->cursor_flash = 0;
  209. return;
  210. } else
  211. y += softback_lines;
  212. }
  213. c = scr_readw((u16 *) vc->vc_pos);
  214. attribute = get_attribute(info, c);
  215. src = vc->vc_font.data + ((c & charmask) * (w * vc->vc_font.height));
  216. if (ops->cursor_state.image.data != src ||
  217. ops->cursor_reset) {
  218. ops->cursor_state.image.data = src;
  219. cursor.set |= FB_CUR_SETIMAGE;
  220. }
  221. if (attribute) {
  222. u8 *dst;
  223. dst = kmalloc(w * vc->vc_font.height, GFP_ATOMIC);
  224. if (!dst)
  225. return;
  226. kfree(ops->cursor_data);
  227. ops->cursor_data = dst;
  228. update_attr(dst, src, attribute, vc);
  229. src = dst;
  230. }
  231. if (ops->cursor_state.image.fg_color != fg ||
  232. ops->cursor_state.image.bg_color != bg ||
  233. ops->cursor_reset) {
  234. ops->cursor_state.image.fg_color = fg;
  235. ops->cursor_state.image.bg_color = bg;
  236. cursor.set |= FB_CUR_SETCMAP;
  237. }
  238. if ((ops->cursor_state.image.dx != (vc->vc_font.width * vc->vc_x)) ||
  239. (ops->cursor_state.image.dy != (vc->vc_font.height * y)) ||
  240. ops->cursor_reset) {
  241. ops->cursor_state.image.dx = vc->vc_font.width * vc->vc_x;
  242. ops->cursor_state.image.dy = vc->vc_font.height * y;
  243. cursor.set |= FB_CUR_SETPOS;
  244. }
  245. if (ops->cursor_state.image.height != vc->vc_font.height ||
  246. ops->cursor_state.image.width != vc->vc_font.width ||
  247. ops->cursor_reset) {
  248. ops->cursor_state.image.height = vc->vc_font.height;
  249. ops->cursor_state.image.width = vc->vc_font.width;
  250. cursor.set |= FB_CUR_SETSIZE;
  251. }
  252. if (ops->cursor_state.hot.x || ops->cursor_state.hot.y ||
  253. ops->cursor_reset) {
  254. ops->cursor_state.hot.x = cursor.hot.y = 0;
  255. cursor.set |= FB_CUR_SETHOT;
  256. }
  257. if (cursor.set & FB_CUR_SETSIZE ||
  258. vc->vc_cursor_type != p->cursor_shape ||
  259. ops->cursor_state.mask == NULL ||
  260. ops->cursor_reset) {
  261. char *mask = kmalloc(w*vc->vc_font.height, GFP_ATOMIC);
  262. int cur_height, size, i = 0;
  263. u8 msk = 0xff;
  264. if (!mask)
  265. return;
  266. kfree(ops->cursor_state.mask);
  267. ops->cursor_state.mask = mask;
  268. p->cursor_shape = vc->vc_cursor_type;
  269. cursor.set |= FB_CUR_SETSHAPE;
  270. switch (p->cursor_shape & CUR_HWMASK) {
  271. case CUR_NONE:
  272. cur_height = 0;
  273. break;
  274. case CUR_UNDERLINE:
  275. cur_height = (vc->vc_font.height < 10) ? 1 : 2;
  276. break;
  277. case CUR_LOWER_THIRD:
  278. cur_height = vc->vc_font.height/3;
  279. break;
  280. case CUR_LOWER_HALF:
  281. cur_height = vc->vc_font.height >> 1;
  282. break;
  283. case CUR_TWO_THIRDS:
  284. cur_height = (vc->vc_font.height << 1)/3;
  285. break;
  286. case CUR_BLOCK:
  287. default:
  288. cur_height = vc->vc_font.height;
  289. break;
  290. }
  291. size = (vc->vc_font.height - cur_height) * w;
  292. while (size--)
  293. mask[i++] = ~msk;
  294. size = cur_height * w;
  295. while (size--)
  296. mask[i++] = msk;
  297. }
  298. switch (mode) {
  299. case CM_ERASE:
  300. ops->cursor_state.enable = 0;
  301. break;
  302. case CM_DRAW:
  303. case CM_MOVE:
  304. default:
  305. ops->cursor_state.enable = (use_sw) ? 0 : 1;
  306. break;
  307. }
  308. cursor.image.data = src;
  309. cursor.image.fg_color = ops->cursor_state.image.fg_color;
  310. cursor.image.bg_color = ops->cursor_state.image.bg_color;
  311. cursor.image.dx = ops->cursor_state.image.dx;
  312. cursor.image.dy = ops->cursor_state.image.dy;
  313. cursor.image.height = ops->cursor_state.image.height;
  314. cursor.image.width = ops->cursor_state.image.width;
  315. cursor.hot.x = ops->cursor_state.hot.x;
  316. cursor.hot.y = ops->cursor_state.hot.y;
  317. cursor.mask = ops->cursor_state.mask;
  318. cursor.enable = ops->cursor_state.enable;
  319. cursor.image.depth = 1;
  320. cursor.rop = ROP_XOR;
  321. info->fbops->fb_cursor(info, &cursor);
  322. ops->cursor_reset = 0;
  323. }
  324. void fbcon_set_bitops(struct fbcon_ops *ops)
  325. {
  326. ops->bmove = bit_bmove;
  327. ops->clear = bit_clear;
  328. ops->putcs = bit_putcs;
  329. ops->clear_margins = bit_clear_margins;
  330. ops->cursor = bit_cursor;
  331. }
  332. EXPORT_SYMBOL(fbcon_set_bitops);
  333. MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
  334. MODULE_DESCRIPTION("Bit Blitting Operation");
  335. MODULE_LICENSE("GPL");