bitblit.c 11 KB

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