bitblit.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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, &info->fix) == 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 inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info,
  90. const u16 *s, u32 attr, u32 cnt,
  91. u32 d_pitch, u32 s_pitch, u32 cellsize,
  92. struct fb_image *image, u8 *buf, u8 *dst)
  93. {
  94. u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
  95. u32 idx = vc->vc_font.width >> 3;
  96. u8 *src;
  97. while (cnt--) {
  98. src = vc->vc_font.data + (scr_readw(s++)&
  99. charmask)*cellsize;
  100. if (attr) {
  101. update_attr(buf, src, attr, vc);
  102. src = buf;
  103. }
  104. if (likely(idx == 1))
  105. __fb_pad_aligned_buffer(dst, d_pitch, src, idx,
  106. image->height);
  107. else
  108. fb_pad_aligned_buffer(dst, d_pitch, src, idx,
  109. image->height);
  110. dst += s_pitch;
  111. }
  112. info->fbops->fb_imageblit(info, image);
  113. }
  114. static inline void bit_putcs_unaligned(struct vc_data *vc,
  115. struct fb_info *info, const u16 *s,
  116. u32 attr, u32 cnt, u32 d_pitch,
  117. u32 s_pitch, u32 cellsize,
  118. struct fb_image *image, u8 *buf,
  119. u8 *dst)
  120. {
  121. u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
  122. u32 shift_low = 0, mod = vc->vc_font.width % 8;
  123. u32 shift_high = 8;
  124. u32 idx = vc->vc_font.width >> 3;
  125. u8 *src;
  126. while (cnt--) {
  127. src = vc->vc_font.data + (scr_readw(s++)&
  128. charmask)*cellsize;
  129. if (attr) {
  130. update_attr(buf, src, attr, vc);
  131. src = buf;
  132. }
  133. fb_pad_unaligned_buffer(dst, d_pitch, src, idx,
  134. image->height, shift_high,
  135. shift_low, mod);
  136. shift_low += mod;
  137. dst += (shift_low >= 8) ? s_pitch : s_pitch - 1;
  138. shift_low &= 7;
  139. shift_high = 8 - shift_low;
  140. }
  141. info->fbops->fb_imageblit(info, image);
  142. }
  143. static void bit_putcs(struct vc_data *vc, struct fb_info *info,
  144. const unsigned short *s, int count, int yy, int xx,
  145. int fg, int bg)
  146. {
  147. struct fb_image image;
  148. u32 width = (vc->vc_font.width + 7)/8;
  149. u32 cellsize = width * vc->vc_font.height;
  150. u32 maxcnt = info->pixmap.size/cellsize;
  151. u32 scan_align = info->pixmap.scan_align - 1;
  152. u32 buf_align = info->pixmap.buf_align - 1;
  153. u32 mod = vc->vc_font.width % 8, cnt, pitch, size;
  154. u32 attribute = get_attribute(info, scr_readw(s));
  155. u8 *dst, *buf = NULL;
  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. if (attribute) {
  163. buf = kmalloc(cellsize, GFP_KERNEL);
  164. if (!buf)
  165. return;
  166. }
  167. while (count) {
  168. if (count > maxcnt)
  169. cnt = maxcnt;
  170. else
  171. cnt = count;
  172. image.width = vc->vc_font.width * cnt;
  173. pitch = ((image.width + 7) >> 3) + scan_align;
  174. pitch &= ~scan_align;
  175. size = pitch * image.height + buf_align;
  176. size &= ~buf_align;
  177. dst = fb_get_buffer_offset(info, &info->pixmap, size);
  178. image.data = dst;
  179. if (!mod)
  180. bit_putcs_aligned(vc, info, s, attribute, cnt, pitch,
  181. width, cellsize, &image, buf, dst);
  182. else
  183. bit_putcs_unaligned(vc, info, s, attribute, cnt,
  184. pitch, width, cellsize, &image,
  185. buf, dst);
  186. image.dx += cnt * vc->vc_font.width;
  187. count -= cnt;
  188. s += cnt;
  189. }
  190. /* buf is always NULL except when in monochrome mode, so in this case
  191. it's a gain to check buf against NULL even though kfree() handles
  192. NULL pointers just fine */
  193. if (unlikely(buf))
  194. kfree(buf);
  195. }
  196. static void bit_clear_margins(struct vc_data *vc, struct fb_info *info,
  197. int bottom_only)
  198. {
  199. int bgshift = (vc->vc_hi_font_mask) ? 13 : 12;
  200. unsigned int cw = vc->vc_font.width;
  201. unsigned int ch = vc->vc_font.height;
  202. unsigned int rw = info->var.xres - (vc->vc_cols*cw);
  203. unsigned int bh = info->var.yres - (vc->vc_rows*ch);
  204. unsigned int rs = info->var.xres - rw;
  205. unsigned int bs = info->var.yres - bh;
  206. struct fb_fillrect region;
  207. region.color = attr_bgcol_ec(bgshift, vc);
  208. region.rop = ROP_COPY;
  209. if (rw && !bottom_only) {
  210. region.dx = info->var.xoffset + rs;
  211. region.dy = 0;
  212. region.width = rw;
  213. region.height = info->var.yres_virtual;
  214. info->fbops->fb_fillrect(info, &region);
  215. }
  216. if (bh) {
  217. region.dx = info->var.xoffset;
  218. region.dy = info->var.yoffset + bs;
  219. region.width = rs;
  220. region.height = bh;
  221. info->fbops->fb_fillrect(info, &region);
  222. }
  223. }
  224. static void bit_cursor(struct vc_data *vc, struct fb_info *info,
  225. struct display *p, int mode, int softback_lines, int fg, int bg)
  226. {
  227. struct fb_cursor cursor;
  228. struct fbcon_ops *ops = (struct fbcon_ops *) info->fbcon_par;
  229. unsigned short charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;
  230. int w = (vc->vc_font.width + 7) >> 3, c;
  231. int y = real_y(p, vc->vc_y);
  232. int attribute, use_sw = (vc->vc_cursor_type & 0x10);
  233. char *src;
  234. cursor.set = 0;
  235. if (softback_lines) {
  236. if (y + softback_lines >= vc->vc_rows) {
  237. mode = CM_ERASE;
  238. ops->cursor_flash = 0;
  239. return;
  240. } else
  241. y += softback_lines;
  242. }
  243. c = scr_readw((u16 *) vc->vc_pos);
  244. attribute = get_attribute(info, c);
  245. src = vc->vc_font.data + ((c & charmask) * (w * vc->vc_font.height));
  246. if (ops->cursor_state.image.data != src ||
  247. ops->cursor_reset) {
  248. ops->cursor_state.image.data = src;
  249. cursor.set |= FB_CUR_SETIMAGE;
  250. }
  251. if (attribute) {
  252. u8 *dst;
  253. dst = kmalloc(w * vc->vc_font.height, GFP_ATOMIC);
  254. if (!dst)
  255. return;
  256. kfree(ops->cursor_data);
  257. ops->cursor_data = dst;
  258. update_attr(dst, src, attribute, vc);
  259. src = dst;
  260. }
  261. if (ops->cursor_state.image.fg_color != fg ||
  262. ops->cursor_state.image.bg_color != bg ||
  263. ops->cursor_reset) {
  264. ops->cursor_state.image.fg_color = fg;
  265. ops->cursor_state.image.bg_color = bg;
  266. cursor.set |= FB_CUR_SETCMAP;
  267. }
  268. if ((ops->cursor_state.image.dx != (vc->vc_font.width * vc->vc_x)) ||
  269. (ops->cursor_state.image.dy != (vc->vc_font.height * y)) ||
  270. ops->cursor_reset) {
  271. ops->cursor_state.image.dx = vc->vc_font.width * vc->vc_x;
  272. ops->cursor_state.image.dy = vc->vc_font.height * y;
  273. cursor.set |= FB_CUR_SETPOS;
  274. }
  275. if (ops->cursor_state.image.height != vc->vc_font.height ||
  276. ops->cursor_state.image.width != vc->vc_font.width ||
  277. ops->cursor_reset) {
  278. ops->cursor_state.image.height = vc->vc_font.height;
  279. ops->cursor_state.image.width = vc->vc_font.width;
  280. cursor.set |= FB_CUR_SETSIZE;
  281. }
  282. if (ops->cursor_state.hot.x || ops->cursor_state.hot.y ||
  283. ops->cursor_reset) {
  284. ops->cursor_state.hot.x = cursor.hot.y = 0;
  285. cursor.set |= FB_CUR_SETHOT;
  286. }
  287. if (cursor.set & FB_CUR_SETSIZE ||
  288. vc->vc_cursor_type != p->cursor_shape ||
  289. ops->cursor_state.mask == NULL ||
  290. ops->cursor_reset) {
  291. char *mask = kmalloc(w*vc->vc_font.height, GFP_ATOMIC);
  292. int cur_height, size, i = 0;
  293. u8 msk = 0xff;
  294. if (!mask)
  295. return;
  296. kfree(ops->cursor_state.mask);
  297. ops->cursor_state.mask = mask;
  298. p->cursor_shape = vc->vc_cursor_type;
  299. cursor.set |= FB_CUR_SETSHAPE;
  300. switch (p->cursor_shape & CUR_HWMASK) {
  301. case CUR_NONE:
  302. cur_height = 0;
  303. break;
  304. case CUR_UNDERLINE:
  305. cur_height = (vc->vc_font.height < 10) ? 1 : 2;
  306. break;
  307. case CUR_LOWER_THIRD:
  308. cur_height = vc->vc_font.height/3;
  309. break;
  310. case CUR_LOWER_HALF:
  311. cur_height = vc->vc_font.height >> 1;
  312. break;
  313. case CUR_TWO_THIRDS:
  314. cur_height = (vc->vc_font.height << 1)/3;
  315. break;
  316. case CUR_BLOCK:
  317. default:
  318. cur_height = vc->vc_font.height;
  319. break;
  320. }
  321. size = (vc->vc_font.height - cur_height) * w;
  322. while (size--)
  323. mask[i++] = ~msk;
  324. size = cur_height * w;
  325. while (size--)
  326. mask[i++] = msk;
  327. }
  328. switch (mode) {
  329. case CM_ERASE:
  330. ops->cursor_state.enable = 0;
  331. break;
  332. case CM_DRAW:
  333. case CM_MOVE:
  334. default:
  335. ops->cursor_state.enable = (use_sw) ? 0 : 1;
  336. break;
  337. }
  338. cursor.image.data = src;
  339. cursor.image.fg_color = ops->cursor_state.image.fg_color;
  340. cursor.image.bg_color = ops->cursor_state.image.bg_color;
  341. cursor.image.dx = ops->cursor_state.image.dx;
  342. cursor.image.dy = ops->cursor_state.image.dy;
  343. cursor.image.height = ops->cursor_state.image.height;
  344. cursor.image.width = ops->cursor_state.image.width;
  345. cursor.hot.x = ops->cursor_state.hot.x;
  346. cursor.hot.y = ops->cursor_state.hot.y;
  347. cursor.mask = ops->cursor_state.mask;
  348. cursor.enable = ops->cursor_state.enable;
  349. cursor.image.depth = 1;
  350. cursor.rop = ROP_XOR;
  351. info->fbops->fb_cursor(info, &cursor);
  352. ops->cursor_reset = 0;
  353. }
  354. void fbcon_set_bitops(struct fbcon_ops *ops)
  355. {
  356. ops->bmove = bit_bmove;
  357. ops->clear = bit_clear;
  358. ops->putcs = bit_putcs;
  359. ops->clear_margins = bit_clear_margins;
  360. ops->cursor = bit_cursor;
  361. }
  362. EXPORT_SYMBOL(fbcon_set_bitops);
  363. MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
  364. MODULE_DESCRIPTION("Bit Blitting Operation");
  365. MODULE_LICENSE("GPL");