cfbimgblt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Generic BitBLT function for frame buffer with packed pixels of any depth.
  3. *
  4. * Copyright (C) June 1999 James Simmons
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive for
  8. * more details.
  9. *
  10. * NOTES:
  11. *
  12. * This function copys a image from system memory to video memory. The
  13. * image can be a bitmap where each 0 represents the background color and
  14. * each 1 represents the foreground color. Great for font handling. It can
  15. * also be a color image. This is determined by image_depth. The color image
  16. * must be laid out exactly in the same format as the framebuffer. Yes I know
  17. * their are cards with hardware that coverts images of various depths to the
  18. * framebuffer depth. But not every card has this. All images must be rounded
  19. * up to the nearest byte. For example a bitmap 12 bits wide must be two
  20. * bytes width.
  21. *
  22. * Tony:
  23. * Incorporate mask tables similar to fbcon-cfb*.c in 2.4 API. This speeds
  24. * up the code significantly.
  25. *
  26. * Code for depths not multiples of BITS_PER_LONG is still kludgy, which is
  27. * still processed a bit at a time.
  28. *
  29. * Also need to add code to deal with cards endians that are different than
  30. * the native cpu endians. I also need to deal with MSB position in the word.
  31. * Modified by Harm Hanemaaijer (fgenfb@yahoo.com) 2013:
  32. * - Provide optimized versions of fast_imageblit for 16 and 32bpp that are
  33. * significantly faster than the previous implementation.
  34. * - Simplify the fast/slow_imageblit selection code, avoiding integer
  35. * divides.
  36. */
  37. #include <linux/module.h>
  38. #include <linux/string.h>
  39. #include <linux/fb.h>
  40. #include <asm/types.h>
  41. #include "fb_draw.h"
  42. #define DEBUG
  43. #ifdef DEBUG
  44. #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt,__func__,## args)
  45. #else
  46. #define DPRINTK(fmt, args...)
  47. #endif
  48. static const u32 cfb_tab8_be[] = {
  49. 0x00000000,0x000000ff,0x0000ff00,0x0000ffff,
  50. 0x00ff0000,0x00ff00ff,0x00ffff00,0x00ffffff,
  51. 0xff000000,0xff0000ff,0xff00ff00,0xff00ffff,
  52. 0xffff0000,0xffff00ff,0xffffff00,0xffffffff
  53. };
  54. static const u32 cfb_tab8_le[] = {
  55. 0x00000000,0xff000000,0x00ff0000,0xffff0000,
  56. 0x0000ff00,0xff00ff00,0x00ffff00,0xffffff00,
  57. 0x000000ff,0xff0000ff,0x00ff00ff,0xffff00ff,
  58. 0x0000ffff,0xff00ffff,0x00ffffff,0xffffffff
  59. };
  60. static const u32 cfb_tab16_be[] = {
  61. 0x00000000, 0x0000ffff, 0xffff0000, 0xffffffff
  62. };
  63. static const u32 cfb_tab16_le[] = {
  64. 0x00000000, 0xffff0000, 0x0000ffff, 0xffffffff
  65. };
  66. static const u32 cfb_tab32[] = {
  67. 0x00000000, 0xffffffff
  68. };
  69. #define FB_WRITEL fb_writel
  70. #define FB_READL fb_readl
  71. static inline void color_imageblit(const struct fb_image *image,
  72. struct fb_info *p, u8 __iomem *dst1,
  73. u32 start_index,
  74. u32 pitch_index)
  75. {
  76. /* Draw the penguin */
  77. u32 __iomem *dst, *dst2;
  78. u32 color = 0, val, shift;
  79. int i, n, bpp = p->var.bits_per_pixel;
  80. u32 null_bits = 32 - bpp;
  81. u32 *palette = (u32 *) p->pseudo_palette;
  82. const u8 *src = image->data;
  83. u32 bswapmask = fb_compute_bswapmask(p);
  84. dst2 = (u32 __iomem *) dst1;
  85. for (i = image->height; i--; ) {
  86. n = image->width;
  87. dst = (u32 __iomem *) dst1;
  88. shift = 0;
  89. val = 0;
  90. if (start_index) {
  91. u32 start_mask = ~fb_shifted_pixels_mask_u32(p,
  92. start_index, bswapmask);
  93. val = FB_READL(dst) & start_mask;
  94. shift = start_index;
  95. }
  96. while (n--) {
  97. if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
  98. p->fix.visual == FB_VISUAL_DIRECTCOLOR )
  99. color = palette[*src];
  100. else
  101. color = *src;
  102. color <<= FB_LEFT_POS(p, bpp);
  103. val |= FB_SHIFT_HIGH(p, color, shift ^ bswapmask);
  104. if (shift >= null_bits) {
  105. FB_WRITEL(val, dst++);
  106. val = (shift == null_bits) ? 0 :
  107. FB_SHIFT_LOW(p, color, 32 - shift);
  108. }
  109. shift += bpp;
  110. shift &= (32 - 1);
  111. src++;
  112. }
  113. if (shift) {
  114. u32 end_mask = fb_shifted_pixels_mask_u32(p, shift,
  115. bswapmask);
  116. FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
  117. }
  118. dst1 += p->fix.line_length;
  119. if (pitch_index) {
  120. dst2 += p->fix.line_length;
  121. dst1 = (u8 __iomem *)((long __force)dst2 & ~(sizeof(u32) - 1));
  122. start_index += pitch_index;
  123. start_index &= 32 - 1;
  124. }
  125. }
  126. }
  127. static inline void slow_imageblit(const struct fb_image *image, struct fb_info *p,
  128. u8 __iomem *dst1, u32 fgcolor,
  129. u32 bgcolor,
  130. u32 start_index,
  131. u32 pitch_index)
  132. {
  133. u32 shift, color = 0, bpp = p->var.bits_per_pixel;
  134. u32 __iomem *dst, *dst2;
  135. u32 val, pitch = p->fix.line_length;
  136. u32 null_bits = 32 - bpp;
  137. u32 spitch = (image->width+7)/8;
  138. const u8 *src = image->data, *s;
  139. u32 i, j, l;
  140. u32 bswapmask = fb_compute_bswapmask(p);
  141. dst2 = (u32 __iomem *) dst1;
  142. fgcolor <<= FB_LEFT_POS(p, bpp);
  143. bgcolor <<= FB_LEFT_POS(p, bpp);
  144. for (i = image->height; i--; ) {
  145. shift = val = 0;
  146. l = 8;
  147. j = image->width;
  148. dst = (u32 __iomem *) dst1;
  149. s = src;
  150. /* write leading bits */
  151. if (start_index) {
  152. u32 start_mask = ~fb_shifted_pixels_mask_u32(p,
  153. start_index, bswapmask);
  154. val = FB_READL(dst) & start_mask;
  155. shift = start_index;
  156. }
  157. while (j--) {
  158. l--;
  159. color = (*s & (1 << l)) ? fgcolor : bgcolor;
  160. val |= FB_SHIFT_HIGH(p, color, shift ^ bswapmask);
  161. /* Did the bitshift spill bits to the next long? */
  162. if (shift >= null_bits) {
  163. FB_WRITEL(val, dst++);
  164. val = (shift == null_bits) ? 0 :
  165. FB_SHIFT_LOW(p, color, 32 - shift);
  166. }
  167. shift += bpp;
  168. shift &= (32 - 1);
  169. if (!l) { l = 8; s++; };
  170. }
  171. /* write trailing bits */
  172. if (shift) {
  173. u32 end_mask = fb_shifted_pixels_mask_u32(p, shift,
  174. bswapmask);
  175. FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
  176. }
  177. dst1 += pitch;
  178. src += spitch;
  179. if (pitch_index) {
  180. dst2 += pitch;
  181. dst1 = (u8 __iomem *)((long __force)dst2 & ~(sizeof(u32) - 1));
  182. start_index += pitch_index;
  183. start_index &= 32 - 1;
  184. }
  185. }
  186. }
  187. /*
  188. * fast_imageblit - optimized monochrome color expansion
  189. *
  190. * Only if: bits_per_pixel == 8, 16, or 32
  191. * image->width is divisible by pixel/dword (ppw);
  192. * fix->line_legth is divisible by 4;
  193. * beginning and end of a scanline is dword aligned
  194. */
  195. static inline void fast_imageblit(const struct fb_image *image, struct fb_info *p,
  196. u8 __iomem *dst1, u32 fgcolor,
  197. u32 bgcolor)
  198. {
  199. u32 fgx = fgcolor, bgx = bgcolor, bpp = p->var.bits_per_pixel;
  200. u32 ppw = 32/bpp, spitch = (image->width + 7)/8;
  201. u32 bit_mask, end_mask, eorx, shift;
  202. const char *s = image->data, *src;
  203. u32 __iomem *dst;
  204. const u32 *tab = NULL;
  205. int i, j, k;
  206. switch (bpp) {
  207. case 8:
  208. tab = fb_be_math(p) ? cfb_tab8_be : cfb_tab8_le;
  209. break;
  210. case 16:
  211. tab = fb_be_math(p) ? cfb_tab16_be : cfb_tab16_le;
  212. break;
  213. case 32:
  214. default:
  215. tab = cfb_tab32;
  216. break;
  217. }
  218. for (i = ppw-1; i--; ) {
  219. fgx <<= bpp;
  220. bgx <<= bpp;
  221. fgx |= fgcolor;
  222. bgx |= bgcolor;
  223. }
  224. bit_mask = (1 << ppw) - 1;
  225. eorx = fgx ^ bgx;
  226. k = image->width/ppw;
  227. for (i = image->height; i--; ) {
  228. dst = (u32 __iomem *) dst1, shift = 8; src = s;
  229. for (j = k; j--; ) {
  230. shift -= ppw;
  231. end_mask = tab[(*src >> shift) & bit_mask];
  232. FB_WRITEL((end_mask & eorx)^bgx, dst++);
  233. if (!shift) { shift = 8; src++; }
  234. }
  235. dst1 += p->fix.line_length;
  236. s += spitch;
  237. }
  238. }
  239. /*
  240. * Optimized fast_imageblit for bpp == 16. ppw = 2, bit_mask = 3 folded
  241. * into the code, main loop unrolled.
  242. */
  243. static inline void fast_imageblit16(const struct fb_image *image,
  244. struct fb_info *p, u8 __iomem * dst1,
  245. u32 fgcolor, u32 bgcolor)
  246. {
  247. u32 fgx = fgcolor, bgx = bgcolor;
  248. u32 spitch = (image->width + 7) / 8;
  249. u32 end_mask, eorx;
  250. const char *s = image->data, *src;
  251. u32 __iomem *dst;
  252. const u32 *tab = NULL;
  253. int i, j, k;
  254. tab = fb_be_math(p) ? cfb_tab16_be : cfb_tab16_le;
  255. fgx <<= 16;
  256. bgx <<= 16;
  257. fgx |= fgcolor;
  258. bgx |= bgcolor;
  259. eorx = fgx ^ bgx;
  260. k = image->width / 2;
  261. for (i = image->height; i--;) {
  262. dst = (u32 __iomem *) dst1;
  263. src = s;
  264. j = k;
  265. while (j >= 4) {
  266. u8 bits = *src;
  267. end_mask = tab[(bits >> 6) & 3];
  268. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  269. end_mask = tab[(bits >> 4) & 3];
  270. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  271. end_mask = tab[(bits >> 2) & 3];
  272. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  273. end_mask = tab[bits & 3];
  274. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  275. src++;
  276. j -= 4;
  277. }
  278. if (j != 0) {
  279. u8 bits = *src;
  280. end_mask = tab[(bits >> 6) & 3];
  281. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  282. if (j >= 2) {
  283. end_mask = tab[(bits >> 4) & 3];
  284. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  285. if (j == 3) {
  286. end_mask = tab[(bits >> 2) & 3];
  287. FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  288. }
  289. }
  290. }
  291. dst1 += p->fix.line_length;
  292. s += spitch;
  293. }
  294. }
  295. /*
  296. * Optimized fast_imageblit for bpp == 32. ppw = 1, bit_mask = 1 folded
  297. * into the code, main loop unrolled.
  298. */
  299. static inline void fast_imageblit32(const struct fb_image *image,
  300. struct fb_info *p, u8 __iomem * dst1,
  301. u32 fgcolor, u32 bgcolor)
  302. {
  303. u32 fgx = fgcolor, bgx = bgcolor;
  304. u32 spitch = (image->width + 7) / 8;
  305. u32 end_mask, eorx;
  306. const char *s = image->data, *src;
  307. u32 __iomem *dst;
  308. const u32 *tab = NULL;
  309. int i, j, k;
  310. tab = cfb_tab32;
  311. eorx = fgx ^ bgx;
  312. k = image->width;
  313. for (i = image->height; i--;) {
  314. dst = (u32 __iomem *) dst1;
  315. src = s;
  316. j = k;
  317. while (j >= 8) {
  318. u8 bits = *src;
  319. end_mask = tab[(bits >> 7) & 1];
  320. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  321. end_mask = tab[(bits >> 6) & 1];
  322. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  323. end_mask = tab[(bits >> 5) & 1];
  324. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  325. end_mask = tab[(bits >> 4) & 1];
  326. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  327. end_mask = tab[(bits >> 3) & 1];
  328. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  329. end_mask = tab[(bits >> 2) & 1];
  330. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  331. end_mask = tab[(bits >> 1) & 1];
  332. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  333. end_mask = tab[bits & 1];
  334. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  335. src++;
  336. j -= 8;
  337. }
  338. if (j != 0) {
  339. u32 bits = (u32) * src;
  340. while (j > 1) {
  341. end_mask = tab[(bits >> 7) & 1];
  342. FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  343. bits <<= 1;
  344. j--;
  345. }
  346. end_mask = tab[(bits >> 7) & 1];
  347. FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  348. }
  349. dst1 += p->fix.line_length;
  350. s += spitch;
  351. }
  352. }
  353. void cfb_imageblit(struct fb_info *p, const struct fb_image *image)
  354. {
  355. u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
  356. u32 bpl = sizeof(u32), bpp = p->var.bits_per_pixel;
  357. u32 width = image->width;
  358. u32 dx = image->dx, dy = image->dy;
  359. u8 __iomem *dst1;
  360. if (p->state != FBINFO_STATE_RUNNING)
  361. return;
  362. bitstart = (dy * p->fix.line_length * 8) + (dx * bpp);
  363. start_index = bitstart & (32 - 1);
  364. pitch_index = (p->fix.line_length & (bpl - 1)) * 8;
  365. bitstart /= 8;
  366. bitstart &= ~(bpl - 1);
  367. dst1 = p->screen_base + bitstart;
  368. if (p->fbops->fb_sync)
  369. p->fbops->fb_sync(p);
  370. if (image->depth == 1) {
  371. if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
  372. p->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  373. fgcolor = ((u32*)(p->pseudo_palette))[image->fg_color];
  374. bgcolor = ((u32*)(p->pseudo_palette))[image->bg_color];
  375. } else {
  376. fgcolor = image->fg_color;
  377. bgcolor = image->bg_color;
  378. }
  379. if (!start_index && !pitch_index) {
  380. if (bpp == 32)
  381. fast_imageblit32(image, p, dst1, fgcolor,
  382. bgcolor);
  383. else if (bpp == 16 && (width & 1) == 0)
  384. fast_imageblit16(image, p, dst1, fgcolor,
  385. bgcolor);
  386. else if (bpp == 8 && (width & 3) == 0)
  387. fast_imageblit(image, p, dst1, fgcolor,
  388. bgcolor);
  389. else
  390. slow_imageblit(image, p, dst1, fgcolor,
  391. bgcolor,
  392. start_index, pitch_index);
  393. } else
  394. slow_imageblit(image, p, dst1, fgcolor, bgcolor,
  395. start_index, pitch_index);
  396. } else
  397. color_imageblit(image, p, dst1, start_index, pitch_index);
  398. }
  399. EXPORT_SYMBOL(cfb_imageblit);
  400. MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
  401. MODULE_DESCRIPTION("Generic software accelerated imaging drawing");
  402. MODULE_LICENSE("GPL");