cfbimgblt.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. #if defined (__BIG_ENDIAN)
  73. #define LEFT_POS(bpp) (32 - bpp)
  74. #define SHIFT_HIGH(val, bits) ((val) >> (bits))
  75. #define SHIFT_LOW(val, bits) ((val) << (bits))
  76. #else
  77. #define LEFT_POS(bpp) (0)
  78. #define SHIFT_HIGH(val, bits) ((val) << (bits))
  79. #define SHIFT_LOW(val, bits) ((val) >> (bits))
  80. #endif
  81. static inline void color_imageblit(const struct fb_image *image,
  82. struct fb_info *p, u8 __iomem *dst1,
  83. u32 start_index,
  84. u32 pitch_index)
  85. {
  86. /* Draw the penguin */
  87. u32 __iomem *dst, *dst2;
  88. u32 color = 0, val, shift;
  89. int i, n, bpp = p->var.bits_per_pixel;
  90. u32 null_bits = 32 - bpp;
  91. u32 *palette = (u32 *) p->pseudo_palette;
  92. const u8 *src = image->data;
  93. dst2 = (u32 __iomem *) dst1;
  94. for (i = image->height; i--; ) {
  95. n = image->width;
  96. dst = (u32 __iomem *) dst1;
  97. shift = 0;
  98. val = 0;
  99. if (start_index) {
  100. u32 start_mask = ~(SHIFT_HIGH(~(u32)0, start_index));
  101. val = FB_READL(dst) & start_mask;
  102. shift = start_index;
  103. }
  104. while (n--) {
  105. if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
  106. p->fix.visual == FB_VISUAL_DIRECTCOLOR )
  107. color = palette[*src];
  108. else
  109. color = *src;
  110. color <<= LEFT_POS(bpp);
  111. val |= SHIFT_HIGH(color, shift);
  112. if (shift >= null_bits) {
  113. FB_WRITEL(val, dst++);
  114. val = (shift == null_bits) ? 0 :
  115. SHIFT_LOW(color, 32 - shift);
  116. }
  117. shift += bpp;
  118. shift &= (32 - 1);
  119. src++;
  120. }
  121. if (shift) {
  122. u32 end_mask = SHIFT_HIGH(~(u32)0, shift);
  123. FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
  124. }
  125. dst1 += p->fix.line_length;
  126. if (pitch_index) {
  127. dst2 += p->fix.line_length;
  128. dst1 = (u8 __iomem *)((long __force)dst2 & ~(sizeof(u32) - 1));
  129. start_index += pitch_index;
  130. start_index &= 32 - 1;
  131. }
  132. }
  133. }
  134. static inline void slow_imageblit(const struct fb_image *image, struct fb_info *p,
  135. u8 __iomem *dst1, u32 fgcolor,
  136. u32 bgcolor,
  137. u32 start_index,
  138. u32 pitch_index)
  139. {
  140. u32 shift, color = 0, bpp = p->var.bits_per_pixel;
  141. u32 __iomem *dst, *dst2;
  142. u32 val, pitch = p->fix.line_length;
  143. u32 null_bits = 32 - bpp;
  144. u32 spitch = (image->width+7)/8;
  145. const u8 *src = image->data, *s;
  146. u32 i, j, l;
  147. dst2 = (u32 __iomem *) dst1;
  148. for (i = image->height; i--; ) {
  149. shift = val = 0;
  150. l = 8;
  151. j = image->width;
  152. dst = (u32 __iomem *) dst1;
  153. s = src;
  154. /* write leading bits */
  155. if (start_index) {
  156. u32 start_mask = ~(SHIFT_HIGH(~(u32)0, start_index));
  157. val = FB_READL(dst) & start_mask;
  158. shift = start_index;
  159. }
  160. while (j--) {
  161. l--;
  162. color = (*s & (1 << l)) ? fgcolor : bgcolor;
  163. color <<= LEFT_POS(bpp);
  164. val |= SHIFT_HIGH(color, shift);
  165. /* Did the bitshift spill bits to the next long? */
  166. if (shift >= null_bits) {
  167. FB_WRITEL(val, dst++);
  168. val = (shift == null_bits) ? 0 :
  169. SHIFT_LOW(color,32 - shift);
  170. }
  171. shift += bpp;
  172. shift &= (32 - 1);
  173. if (!l) { l = 8; s++; };
  174. }
  175. /* write trailing bits */
  176. if (shift) {
  177. u32 end_mask = SHIFT_HIGH(~(u32)0, shift);
  178. FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
  179. }
  180. dst1 += pitch;
  181. src += spitch;
  182. if (pitch_index) {
  183. dst2 += pitch;
  184. dst1 = (u8 __iomem *)((long __force)dst2 & ~(sizeof(u32) - 1));
  185. start_index += pitch_index;
  186. start_index &= 32 - 1;
  187. }
  188. }
  189. }
  190. /*
  191. * fast_imageblit - optimized monochrome color expansion
  192. *
  193. * Only if: bits_per_pixel == 8, 16, or 32
  194. * image->width is divisible by pixel/dword (ppw);
  195. * fix->line_legth is divisible by 4;
  196. * beginning and end of a scanline is dword aligned
  197. */
  198. static inline void fast_imageblit(const struct fb_image *image, struct fb_info *p,
  199. u8 __iomem *dst1, u32 fgcolor,
  200. u32 bgcolor)
  201. {
  202. u32 fgx = fgcolor, bgx = bgcolor, bpp = p->var.bits_per_pixel;
  203. u32 ppw = 32/bpp, spitch = (image->width + 7)/8;
  204. u32 bit_mask, end_mask, eorx, shift;
  205. const char *s = image->data, *src;
  206. u32 __iomem *dst;
  207. u32 *tab = NULL;
  208. int i, j, k;
  209. switch (bpp) {
  210. case 8:
  211. tab = cfb_tab8;
  212. break;
  213. case 16:
  214. tab = cfb_tab16;
  215. break;
  216. case 32:
  217. tab = cfb_tab32;
  218. break;
  219. }
  220. for (i = ppw-1; i--; ) {
  221. fgx <<= bpp;
  222. bgx <<= bpp;
  223. fgx |= fgcolor;
  224. bgx |= bgcolor;
  225. }
  226. bit_mask = (1 << ppw) - 1;
  227. eorx = fgx ^ bgx;
  228. k = image->width/ppw;
  229. for (i = image->height; i--; ) {
  230. dst = (u32 __iomem *) dst1, shift = 8; src = s;
  231. for (j = k; j--; ) {
  232. shift -= ppw;
  233. end_mask = tab[(*src >> shift) & bit_mask];
  234. FB_WRITEL((end_mask & eorx)^bgx, dst++);
  235. if (!shift) { shift = 8; src++; }
  236. }
  237. dst1 += p->fix.line_length;
  238. s += spitch;
  239. }
  240. }
  241. void cfb_imageblit(struct fb_info *p, const struct fb_image *image)
  242. {
  243. u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
  244. u32 bpl = sizeof(u32), bpp = p->var.bits_per_pixel;
  245. u32 width = image->width, height = image->height;
  246. u32 dx = image->dx, dy = image->dy;
  247. int x2, y2, vxres, vyres;
  248. u8 __iomem *dst1;
  249. if (p->state != FBINFO_STATE_RUNNING)
  250. return;
  251. vxres = p->var.xres_virtual;
  252. vyres = p->var.yres_virtual;
  253. /*
  254. * We could use hardware clipping but on many cards you get around
  255. * hardware clipping by writing to framebuffer directly like we are
  256. * doing here.
  257. */
  258. if (image->dx > vxres || image->dy > vyres)
  259. return;
  260. x2 = image->dx + image->width;
  261. y2 = image->dy + image->height;
  262. dx = image->dx > 0 ? image->dx : 0;
  263. dy = image->dy > 0 ? image->dy : 0;
  264. x2 = x2 < vxres ? x2 : vxres;
  265. y2 = y2 < vyres ? y2 : vyres;
  266. width = x2 - dx;
  267. height = y2 - dy;
  268. bitstart = (dy * p->fix.line_length * 8) + (dx * bpp);
  269. start_index = bitstart & (32 - 1);
  270. pitch_index = (p->fix.line_length & (bpl - 1)) * 8;
  271. bitstart /= 8;
  272. bitstart &= ~(bpl - 1);
  273. dst1 = p->screen_base + bitstart;
  274. if (p->fbops->fb_sync)
  275. p->fbops->fb_sync(p);
  276. if (image->depth == 1) {
  277. if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
  278. p->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  279. fgcolor = ((u32*)(p->pseudo_palette))[image->fg_color];
  280. bgcolor = ((u32*)(p->pseudo_palette))[image->bg_color];
  281. } else {
  282. fgcolor = image->fg_color;
  283. bgcolor = image->bg_color;
  284. }
  285. if (32 % bpp == 0 && !start_index && !pitch_index &&
  286. ((width & (32/bpp-1)) == 0) &&
  287. bpp >= 8 && bpp <= 32)
  288. fast_imageblit(image, p, dst1, fgcolor, bgcolor);
  289. else
  290. slow_imageblit(image, p, dst1, fgcolor, bgcolor,
  291. start_index, pitch_index);
  292. } else
  293. color_imageblit(image, p, dst1, start_index, pitch_index);
  294. }
  295. EXPORT_SYMBOL(cfb_imageblit);
  296. MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
  297. MODULE_DESCRIPTION("Generic software accelerated imaging drawing");
  298. MODULE_LICENSE("GPL");