cfbimgblt.c 8.0 KB

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