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/config.h>
  33. #include <linux/module.h>
  34. #include <linux/string.h>
  35. #include <linux/fb.h>
  36. #include <asm/types.h>
  37. #define DEBUG
  38. #ifdef DEBUG
  39. #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt,__FUNCTION__,## args)
  40. #else
  41. #define DPRINTK(fmt, args...)
  42. #endif
  43. static u32 cfb_tab8[] = {
  44. #if defined(__BIG_ENDIAN)
  45. 0x00000000,0x000000ff,0x0000ff00,0x0000ffff,
  46. 0x00ff0000,0x00ff00ff,0x00ffff00,0x00ffffff,
  47. 0xff000000,0xff0000ff,0xff00ff00,0xff00ffff,
  48. 0xffff0000,0xffff00ff,0xffffff00,0xffffffff
  49. #elif defined(__LITTLE_ENDIAN)
  50. 0x00000000,0xff000000,0x00ff0000,0xffff0000,
  51. 0x0000ff00,0xff00ff00,0x00ffff00,0xffffff00,
  52. 0x000000ff,0xff0000ff,0x00ff00ff,0xffff00ff,
  53. 0x0000ffff,0xff00ffff,0x00ffffff,0xffffffff
  54. #else
  55. #error FIXME: No endianness??
  56. #endif
  57. };
  58. static u32 cfb_tab16[] = {
  59. #if defined(__BIG_ENDIAN)
  60. 0x00000000, 0x0000ffff, 0xffff0000, 0xffffffff
  61. #elif defined(__LITTLE_ENDIAN)
  62. 0x00000000, 0xffff0000, 0x0000ffff, 0xffffffff
  63. #else
  64. #error FIXME: No endianness??
  65. #endif
  66. };
  67. static u32 cfb_tab32[] = {
  68. 0x00000000, 0xffffffff
  69. };
  70. #define FB_WRITEL fb_writel
  71. #define FB_READL fb_readl
  72. static inline void color_imageblit(const struct fb_image *image,
  73. struct fb_info *p, u8 __iomem *dst1,
  74. u32 start_index,
  75. u32 pitch_index)
  76. {
  77. /* Draw the penguin */
  78. u32 __iomem *dst, *dst2;
  79. u32 color = 0, val, shift;
  80. int i, n, bpp = p->var.bits_per_pixel;
  81. u32 null_bits = 32 - bpp;
  82. u32 *palette = (u32 *) p->pseudo_palette;
  83. const u8 *src = image->data;
  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_SHIFT_HIGH(~(u32)0, start_index));
  92. val = FB_READL(dst) & start_mask;
  93. shift = start_index;
  94. }
  95. while (n--) {
  96. if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
  97. p->fix.visual == FB_VISUAL_DIRECTCOLOR )
  98. color = palette[*src];
  99. else
  100. color = *src;
  101. color <<= FB_LEFT_POS(bpp);
  102. val |= FB_SHIFT_HIGH(color, shift);
  103. if (shift >= null_bits) {
  104. FB_WRITEL(val, dst++);
  105. val = (shift == null_bits) ? 0 :
  106. FB_SHIFT_LOW(color, 32 - shift);
  107. }
  108. shift += bpp;
  109. shift &= (32 - 1);
  110. src++;
  111. }
  112. if (shift) {
  113. u32 end_mask = FB_SHIFT_HIGH(~(u32)0, shift);
  114. FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
  115. }
  116. dst1 += p->fix.line_length;
  117. if (pitch_index) {
  118. dst2 += p->fix.line_length;
  119. dst1 = (u8 __iomem *)((long __force)dst2 & ~(sizeof(u32) - 1));
  120. start_index += pitch_index;
  121. start_index &= 32 - 1;
  122. }
  123. }
  124. }
  125. static inline void slow_imageblit(const struct fb_image *image, struct fb_info *p,
  126. u8 __iomem *dst1, u32 fgcolor,
  127. u32 bgcolor,
  128. u32 start_index,
  129. u32 pitch_index)
  130. {
  131. u32 shift, color = 0, bpp = p->var.bits_per_pixel;
  132. u32 __iomem *dst, *dst2;
  133. u32 val, pitch = p->fix.line_length;
  134. u32 null_bits = 32 - bpp;
  135. u32 spitch = (image->width+7)/8;
  136. const u8 *src = image->data, *s;
  137. u32 i, j, l;
  138. dst2 = (u32 __iomem *) dst1;
  139. fgcolor <<= FB_LEFT_POS(bpp);
  140. bgcolor <<= FB_LEFT_POS(bpp);
  141. for (i = image->height; i--; ) {
  142. shift = val = 0;
  143. l = 8;
  144. j = image->width;
  145. dst = (u32 __iomem *) dst1;
  146. s = src;
  147. /* write leading bits */
  148. if (start_index) {
  149. u32 start_mask = ~(FB_SHIFT_HIGH(~(u32)0,start_index));
  150. val = FB_READL(dst) & start_mask;
  151. shift = start_index;
  152. }
  153. while (j--) {
  154. l--;
  155. color = (*s & (1 << l)) ? fgcolor : bgcolor;
  156. val |= FB_SHIFT_HIGH(color, shift);
  157. /* Did the bitshift spill bits to the next long? */
  158. if (shift >= null_bits) {
  159. FB_WRITEL(val, dst++);
  160. val = (shift == null_bits) ? 0 :
  161. FB_SHIFT_LOW(color,32 - shift);
  162. }
  163. shift += bpp;
  164. shift &= (32 - 1);
  165. if (!l) { l = 8; s++; };
  166. }
  167. /* write trailing bits */
  168. if (shift) {
  169. u32 end_mask = FB_SHIFT_HIGH(~(u32)0, shift);
  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. u32 *tab = NULL;
  200. int i, j, k;
  201. switch (bpp) {
  202. case 8:
  203. tab = cfb_tab8;
  204. break;
  205. case 16:
  206. tab = cfb_tab16;
  207. break;
  208. case 32:
  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");