cfbimgblt.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. */
  32. #include <linux/module.h>
  33. #include <linux/string.h>
  34. #include <linux/fb.h>
  35. #include <asm/types.h>
  36. #include "fb_draw.h"
  37. #define DEBUG
  38. #ifdef DEBUG
  39. #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt,__func__,## args)
  40. #else
  41. #define DPRINTK(fmt, args...)
  42. #endif
  43. static const u32 cfb_tab8_be[] = {
  44. 0x00000000,0x000000ff,0x0000ff00,0x0000ffff,
  45. 0x00ff0000,0x00ff00ff,0x00ffff00,0x00ffffff,
  46. 0xff000000,0xff0000ff,0xff00ff00,0xff00ffff,
  47. 0xffff0000,0xffff00ff,0xffffff00,0xffffffff
  48. };
  49. static const u32 cfb_tab8_le[] = {
  50. 0x00000000,0xff000000,0x00ff0000,0xffff0000,
  51. 0x0000ff00,0xff00ff00,0x00ffff00,0xffffff00,
  52. 0x000000ff,0xff0000ff,0x00ff00ff,0xffff00ff,
  53. 0x0000ffff,0xff00ffff,0x00ffffff,0xffffffff
  54. };
  55. static const u32 cfb_tab16_be[] = {
  56. 0x00000000, 0x0000ffff, 0xffff0000, 0xffffffff
  57. };
  58. static const u32 cfb_tab16_le[] = {
  59. 0x00000000, 0xffff0000, 0x0000ffff, 0xffffffff
  60. };
  61. static const u32 cfb_tab32[] = {
  62. 0x00000000, 0xffffffff
  63. };
  64. #define FB_WRITEL fb_writel
  65. #define FB_READL fb_readl
  66. static inline void color_imageblit(const struct fb_image *image,
  67. struct fb_info *p, u8 __iomem *dst1,
  68. u32 start_index,
  69. u32 pitch_index)
  70. {
  71. /* Draw the penguin */
  72. u32 __iomem *dst, *dst2;
  73. u32 color = 0, val, shift;
  74. int i, n, bpp = p->var.bits_per_pixel;
  75. u32 null_bits = 32 - bpp;
  76. u32 *palette = (u32 *) p->pseudo_palette;
  77. const u8 *src = image->data;
  78. u32 bswapmask = fb_compute_bswapmask(p);
  79. dst2 = (u32 __iomem *) dst1;
  80. for (i = image->height; i--; ) {
  81. n = image->width;
  82. dst = (u32 __iomem *) dst1;
  83. shift = 0;
  84. val = 0;
  85. if (start_index) {
  86. u32 start_mask = ~fb_shifted_pixels_mask_u32(p,
  87. start_index, bswapmask);
  88. val = FB_READL(dst) & start_mask;
  89. shift = start_index;
  90. }
  91. while (n--) {
  92. if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
  93. p->fix.visual == FB_VISUAL_DIRECTCOLOR )
  94. color = palette[*src];
  95. else
  96. color = *src;
  97. color <<= FB_LEFT_POS(p, bpp);
  98. val |= FB_SHIFT_HIGH(p, color, shift ^ bswapmask);
  99. if (shift >= null_bits) {
  100. FB_WRITEL(val, dst++);
  101. val = (shift == null_bits) ? 0 :
  102. FB_SHIFT_LOW(p, color, 32 - shift);
  103. }
  104. shift += bpp;
  105. shift &= (32 - 1);
  106. src++;
  107. }
  108. if (shift) {
  109. u32 end_mask = fb_shifted_pixels_mask_u32(p, shift,
  110. bswapmask);
  111. FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
  112. }
  113. dst1 += p->fix.line_length;
  114. if (pitch_index) {
  115. dst2 += p->fix.line_length;
  116. dst1 = (u8 __iomem *)((long __force)dst2 & ~(sizeof(u32) - 1));
  117. start_index += pitch_index;
  118. start_index &= 32 - 1;
  119. }
  120. }
  121. }
  122. static inline void slow_imageblit(const struct fb_image *image, struct fb_info *p,
  123. u8 __iomem *dst1, u32 fgcolor,
  124. u32 bgcolor,
  125. u32 start_index,
  126. u32 pitch_index)
  127. {
  128. u32 shift, color = 0, bpp = p->var.bits_per_pixel;
  129. u32 __iomem *dst, *dst2;
  130. u32 val, pitch = p->fix.line_length;
  131. u32 null_bits = 32 - bpp;
  132. u32 spitch = (image->width+7)/8;
  133. const u8 *src = image->data, *s;
  134. u32 i, j, l;
  135. u32 bswapmask = fb_compute_bswapmask(p);
  136. dst2 = (u32 __iomem *) dst1;
  137. fgcolor <<= FB_LEFT_POS(p, bpp);
  138. bgcolor <<= FB_LEFT_POS(p, bpp);
  139. for (i = image->height; i--; ) {
  140. shift = val = 0;
  141. l = 8;
  142. j = image->width;
  143. dst = (u32 __iomem *) dst1;
  144. s = src;
  145. /* write leading bits */
  146. if (start_index) {
  147. u32 start_mask = ~fb_shifted_pixels_mask_u32(p,
  148. start_index, bswapmask);
  149. val = FB_READL(dst) & start_mask;
  150. shift = start_index;
  151. }
  152. while (j--) {
  153. l--;
  154. color = (*s & (1 << l)) ? fgcolor : bgcolor;
  155. val |= FB_SHIFT_HIGH(p, color, shift ^ bswapmask);
  156. /* Did the bitshift spill bits to the next long? */
  157. if (shift >= null_bits) {
  158. FB_WRITEL(val, dst++);
  159. val = (shift == null_bits) ? 0 :
  160. FB_SHIFT_LOW(p, color, 32 - shift);
  161. }
  162. shift += bpp;
  163. shift &= (32 - 1);
  164. if (!l) { l = 8; s++; };
  165. }
  166. /* write trailing bits */
  167. if (shift) {
  168. u32 end_mask = fb_shifted_pixels_mask_u32(p, shift,
  169. bswapmask);
  170. FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
  171. }
  172. dst1 += pitch;
  173. src += spitch;
  174. if (pitch_index) {
  175. dst2 += pitch;
  176. dst1 = (u8 __iomem *)((long __force)dst2 & ~(sizeof(u32) - 1));
  177. start_index += pitch_index;
  178. start_index &= 32 - 1;
  179. }
  180. }
  181. }
  182. /*
  183. * fast_imageblit - optimized monochrome color expansion
  184. *
  185. * Only if: bits_per_pixel == 8, 16, or 32
  186. * image->width is divisible by pixel/dword (ppw);
  187. * fix->line_legth is divisible by 4;
  188. * beginning and end of a scanline is dword aligned
  189. */
  190. static inline void fast_imageblit(const struct fb_image *image, struct fb_info *p,
  191. u8 __iomem *dst1, u32 fgcolor,
  192. u32 bgcolor)
  193. {
  194. u32 fgx = fgcolor, bgx = bgcolor, bpp = p->var.bits_per_pixel;
  195. u32 ppw = 32/bpp, spitch = (image->width + 7)/8;
  196. u32 bit_mask, end_mask, eorx, shift;
  197. const char *s = image->data, *src;
  198. u32 __iomem *dst;
  199. const u32 *tab = NULL;
  200. int i, j, k;
  201. switch (bpp) {
  202. case 8:
  203. tab = fb_be_math(p) ? cfb_tab8_be : cfb_tab8_le;
  204. break;
  205. case 16:
  206. tab = fb_be_math(p) ? cfb_tab16_be : cfb_tab16_le;
  207. break;
  208. case 32:
  209. default:
  210. tab = cfb_tab32;
  211. break;
  212. }
  213. for (i = ppw-1; i--; ) {
  214. fgx <<= bpp;
  215. bgx <<= bpp;
  216. fgx |= fgcolor;
  217. bgx |= bgcolor;
  218. }
  219. bit_mask = (1 << ppw) - 1;
  220. eorx = fgx ^ bgx;
  221. k = image->width/ppw;
  222. for (i = image->height; i--; ) {
  223. dst = (u32 __iomem *) dst1, shift = 8; src = s;
  224. for (j = k; j--; ) {
  225. shift -= ppw;
  226. end_mask = tab[(*src >> shift) & bit_mask];
  227. FB_WRITEL((end_mask & eorx)^bgx, dst++);
  228. if (!shift) { shift = 8; src++; }
  229. }
  230. dst1 += p->fix.line_length;
  231. s += spitch;
  232. }
  233. }
  234. void cfb_imageblit(struct fb_info *p, const struct fb_image *image)
  235. {
  236. u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
  237. u32 bpl = sizeof(u32), bpp = p->var.bits_per_pixel;
  238. u32 width = image->width;
  239. u32 dx = image->dx, dy = image->dy;
  240. u8 __iomem *dst1;
  241. if (p->state != FBINFO_STATE_RUNNING)
  242. return;
  243. bitstart = (dy * p->fix.line_length * 8) + (dx * bpp);
  244. start_index = bitstart & (32 - 1);
  245. pitch_index = (p->fix.line_length & (bpl - 1)) * 8;
  246. bitstart /= 8;
  247. bitstart &= ~(bpl - 1);
  248. dst1 = p->screen_base + bitstart;
  249. if (p->fbops->fb_sync)
  250. p->fbops->fb_sync(p);
  251. if (image->depth == 1) {
  252. if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
  253. p->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  254. fgcolor = ((u32*)(p->pseudo_palette))[image->fg_color];
  255. bgcolor = ((u32*)(p->pseudo_palette))[image->bg_color];
  256. } else {
  257. fgcolor = image->fg_color;
  258. bgcolor = image->bg_color;
  259. }
  260. if (32 % bpp == 0 && !start_index && !pitch_index &&
  261. ((width & (32/bpp-1)) == 0) &&
  262. bpp >= 8 && bpp <= 32)
  263. fast_imageblit(image, p, dst1, fgcolor, bgcolor);
  264. else
  265. slow_imageblit(image, p, dst1, fgcolor, bgcolor,
  266. start_index, pitch_index);
  267. } else
  268. color_imageblit(image, p, dst1, start_index, pitch_index);
  269. }
  270. EXPORT_SYMBOL(cfb_imageblit);
  271. MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
  272. MODULE_DESCRIPTION("Generic software accelerated imaging drawing");
  273. MODULE_LICENSE("GPL");