cfbimgblt.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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,__FUNCTION__,## args)
  40. #else
  41. #define DPRINTK(fmt, args...)
  42. #endif
  43. static const 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 const 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 const 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. u32 bswapmask = fb_compute_bswapmask(p);
  85. dst2 = (u32 __iomem *) dst1;
  86. for (i = image->height; i--; ) {
  87. n = image->width;
  88. dst = (u32 __iomem *) dst1;
  89. shift = 0;
  90. val = 0;
  91. if (start_index) {
  92. u32 start_mask = ~fb_shifted_pixels_mask_u32(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(bpp);
  103. val |= FB_SHIFT_HIGH(color, shift ^ bswapmask);
  104. if (shift >= null_bits) {
  105. FB_WRITEL(val, dst++);
  106. val = (shift == null_bits) ? 0 :
  107. FB_SHIFT_LOW(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(shift, bswapmask);
  115. FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
  116. }
  117. dst1 += p->fix.line_length;
  118. if (pitch_index) {
  119. dst2 += p->fix.line_length;
  120. dst1 = (u8 __iomem *)((long __force)dst2 & ~(sizeof(u32) - 1));
  121. start_index += pitch_index;
  122. start_index &= 32 - 1;
  123. }
  124. }
  125. }
  126. static inline void slow_imageblit(const struct fb_image *image, struct fb_info *p,
  127. u8 __iomem *dst1, u32 fgcolor,
  128. u32 bgcolor,
  129. u32 start_index,
  130. u32 pitch_index)
  131. {
  132. u32 shift, color = 0, bpp = p->var.bits_per_pixel;
  133. u32 __iomem *dst, *dst2;
  134. u32 val, pitch = p->fix.line_length;
  135. u32 null_bits = 32 - bpp;
  136. u32 spitch = (image->width+7)/8;
  137. const u8 *src = image->data, *s;
  138. u32 i, j, l;
  139. u32 bswapmask = fb_compute_bswapmask(p);
  140. dst2 = (u32 __iomem *) dst1;
  141. fgcolor <<= FB_LEFT_POS(bpp);
  142. bgcolor <<= FB_LEFT_POS(bpp);
  143. for (i = image->height; i--; ) {
  144. shift = val = 0;
  145. l = 8;
  146. j = image->width;
  147. dst = (u32 __iomem *) dst1;
  148. s = src;
  149. /* write leading bits */
  150. if (start_index) {
  151. u32 start_mask = ~fb_shifted_pixels_mask_u32(start_index, bswapmask);
  152. val = FB_READL(dst) & start_mask;
  153. shift = start_index;
  154. }
  155. while (j--) {
  156. l--;
  157. color = (*s & (1 << l)) ? fgcolor : bgcolor;
  158. val |= FB_SHIFT_HIGH(color, shift ^ bswapmask);
  159. /* Did the bitshift spill bits to the next long? */
  160. if (shift >= null_bits) {
  161. FB_WRITEL(val, dst++);
  162. val = (shift == null_bits) ? 0 :
  163. FB_SHIFT_LOW(color,32 - shift);
  164. }
  165. shift += bpp;
  166. shift &= (32 - 1);
  167. if (!l) { l = 8; s++; };
  168. }
  169. /* write trailing bits */
  170. if (shift) {
  171. u32 end_mask = fb_shifted_pixels_mask_u32(shift, bswapmask);
  172. FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
  173. }
  174. dst1 += pitch;
  175. src += spitch;
  176. if (pitch_index) {
  177. dst2 += pitch;
  178. dst1 = (u8 __iomem *)((long __force)dst2 & ~(sizeof(u32) - 1));
  179. start_index += pitch_index;
  180. start_index &= 32 - 1;
  181. }
  182. }
  183. }
  184. /*
  185. * fast_imageblit - optimized monochrome color expansion
  186. *
  187. * Only if: bits_per_pixel == 8, 16, or 32
  188. * image->width is divisible by pixel/dword (ppw);
  189. * fix->line_legth is divisible by 4;
  190. * beginning and end of a scanline is dword aligned
  191. */
  192. static inline void fast_imageblit(const struct fb_image *image, struct fb_info *p,
  193. u8 __iomem *dst1, u32 fgcolor,
  194. u32 bgcolor)
  195. {
  196. u32 fgx = fgcolor, bgx = bgcolor, bpp = p->var.bits_per_pixel;
  197. u32 ppw = 32/bpp, spitch = (image->width + 7)/8;
  198. u32 bit_mask, end_mask, eorx, shift;
  199. const char *s = image->data, *src;
  200. u32 __iomem *dst;
  201. const u32 *tab = NULL;
  202. int i, j, k;
  203. switch (bpp) {
  204. case 8:
  205. tab = cfb_tab8;
  206. break;
  207. case 16:
  208. tab = cfb_tab16;
  209. break;
  210. case 32:
  211. default:
  212. tab = cfb_tab32;
  213. break;
  214. }
  215. for (i = ppw-1; i--; ) {
  216. fgx <<= bpp;
  217. bgx <<= bpp;
  218. fgx |= fgcolor;
  219. bgx |= bgcolor;
  220. }
  221. bit_mask = (1 << ppw) - 1;
  222. eorx = fgx ^ bgx;
  223. k = image->width/ppw;
  224. for (i = image->height; i--; ) {
  225. dst = (u32 __iomem *) dst1, shift = 8; src = s;
  226. for (j = k; j--; ) {
  227. shift -= ppw;
  228. end_mask = tab[(*src >> shift) & bit_mask];
  229. FB_WRITEL((end_mask & eorx)^bgx, dst++);
  230. if (!shift) { shift = 8; src++; }
  231. }
  232. dst1 += p->fix.line_length;
  233. s += spitch;
  234. }
  235. }
  236. void cfb_imageblit(struct fb_info *p, const struct fb_image *image)
  237. {
  238. u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
  239. u32 bpl = sizeof(u32), bpp = p->var.bits_per_pixel;
  240. u32 width = image->width;
  241. u32 dx = image->dx, dy = image->dy;
  242. u8 __iomem *dst1;
  243. if (p->state != FBINFO_STATE_RUNNING)
  244. return;
  245. bitstart = (dy * p->fix.line_length * 8) + (dx * bpp);
  246. start_index = bitstart & (32 - 1);
  247. pitch_index = (p->fix.line_length & (bpl - 1)) * 8;
  248. bitstart /= 8;
  249. bitstart &= ~(bpl - 1);
  250. dst1 = p->screen_base + bitstart;
  251. if (p->fbops->fb_sync)
  252. p->fbops->fb_sync(p);
  253. if (image->depth == 1) {
  254. if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
  255. p->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  256. fgcolor = ((u32*)(p->pseudo_palette))[image->fg_color];
  257. bgcolor = ((u32*)(p->pseudo_palette))[image->bg_color];
  258. } else {
  259. fgcolor = image->fg_color;
  260. bgcolor = image->bg_color;
  261. }
  262. if (32 % bpp == 0 && !start_index && !pitch_index &&
  263. ((width & (32/bpp-1)) == 0) &&
  264. bpp >= 8 && bpp <= 32)
  265. fast_imageblit(image, p, dst1, fgcolor, bgcolor);
  266. else
  267. slow_imageblit(image, p, dst1, fgcolor, bgcolor,
  268. start_index, pitch_index);
  269. } else
  270. color_imageblit(image, p, dst1, start_index, pitch_index);
  271. }
  272. EXPORT_SYMBOL(cfb_imageblit);
  273. MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
  274. MODULE_DESCRIPTION("Generic software accelerated imaging drawing");
  275. MODULE_LICENSE("GPL");