sysimgblt.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Generic 1-bit or 8-bit source to 1-32 bit destination expansion
  3. * for frame buffer located in system RAM with packed pixels of any depth.
  4. *
  5. * Based almost entirely on cfbimgblt.c
  6. *
  7. * Copyright (C) April 2007 Antonino Daplas <adaplas@pol.net>
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file COPYING in the main directory of this archive for
  11. * more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/string.h>
  15. #include <linux/fb.h>
  16. #include <asm/types.h>
  17. #define DEBUG
  18. #ifdef DEBUG
  19. #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt,__FUNCTION__,## args)
  20. #else
  21. #define DPRINTK(fmt, args...)
  22. #endif
  23. static const u32 cfb_tab8[] = {
  24. #if defined(__BIG_ENDIAN)
  25. 0x00000000,0x000000ff,0x0000ff00,0x0000ffff,
  26. 0x00ff0000,0x00ff00ff,0x00ffff00,0x00ffffff,
  27. 0xff000000,0xff0000ff,0xff00ff00,0xff00ffff,
  28. 0xffff0000,0xffff00ff,0xffffff00,0xffffffff
  29. #elif defined(__LITTLE_ENDIAN)
  30. 0x00000000,0xff000000,0x00ff0000,0xffff0000,
  31. 0x0000ff00,0xff00ff00,0x00ffff00,0xffffff00,
  32. 0x000000ff,0xff0000ff,0x00ff00ff,0xffff00ff,
  33. 0x0000ffff,0xff00ffff,0x00ffffff,0xffffffff
  34. #else
  35. #error FIXME: No endianness??
  36. #endif
  37. };
  38. static const u32 cfb_tab16[] = {
  39. #if defined(__BIG_ENDIAN)
  40. 0x00000000, 0x0000ffff, 0xffff0000, 0xffffffff
  41. #elif defined(__LITTLE_ENDIAN)
  42. 0x00000000, 0xffff0000, 0x0000ffff, 0xffffffff
  43. #else
  44. #error FIXME: No endianness??
  45. #endif
  46. };
  47. static const u32 cfb_tab32[] = {
  48. 0x00000000, 0xffffffff
  49. };
  50. static void color_imageblit(const struct fb_image *image, struct fb_info *p,
  51. void *dst1, u32 start_index, u32 pitch_index)
  52. {
  53. /* Draw the penguin */
  54. u32 *dst, *dst2;
  55. u32 color = 0, val, shift;
  56. int i, n, bpp = p->var.bits_per_pixel;
  57. u32 null_bits = 32 - bpp;
  58. u32 *palette = (u32 *) p->pseudo_palette;
  59. const u8 *src = image->data;
  60. dst2 = dst1;
  61. for (i = image->height; i--; ) {
  62. n = image->width;
  63. dst = dst1;
  64. shift = 0;
  65. val = 0;
  66. if (start_index) {
  67. u32 start_mask = ~(FB_SHIFT_HIGH(~(u32)0,
  68. start_index));
  69. val = *dst & start_mask;
  70. shift = start_index;
  71. }
  72. while (n--) {
  73. if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
  74. p->fix.visual == FB_VISUAL_DIRECTCOLOR )
  75. color = palette[*src];
  76. else
  77. color = *src;
  78. color <<= FB_LEFT_POS(bpp);
  79. val |= FB_SHIFT_HIGH(color, shift);
  80. if (shift >= null_bits) {
  81. *dst++ = val;
  82. val = (shift == null_bits) ? 0 :
  83. FB_SHIFT_LOW(color, 32 - shift);
  84. }
  85. shift += bpp;
  86. shift &= (32 - 1);
  87. src++;
  88. }
  89. if (shift) {
  90. u32 end_mask = FB_SHIFT_HIGH(~(u32)0, shift);
  91. *dst &= end_mask;
  92. *dst |= val;
  93. }
  94. dst1 += p->fix.line_length;
  95. if (pitch_index) {
  96. dst2 += p->fix.line_length;
  97. dst1 = (u8 *)((long)dst2 & ~(sizeof(u32) - 1));
  98. start_index += pitch_index;
  99. start_index &= 32 - 1;
  100. }
  101. }
  102. }
  103. static void slow_imageblit(const struct fb_image *image, struct fb_info *p,
  104. void *dst1, u32 fgcolor, u32 bgcolor,
  105. u32 start_index, u32 pitch_index)
  106. {
  107. u32 shift, color = 0, bpp = p->var.bits_per_pixel;
  108. u32 *dst, *dst2;
  109. u32 val, pitch = p->fix.line_length;
  110. u32 null_bits = 32 - bpp;
  111. u32 spitch = (image->width+7)/8;
  112. const u8 *src = image->data, *s;
  113. u32 i, j, l;
  114. dst2 = dst1;
  115. fgcolor <<= FB_LEFT_POS(bpp);
  116. bgcolor <<= FB_LEFT_POS(bpp);
  117. for (i = image->height; i--; ) {
  118. shift = val = 0;
  119. l = 8;
  120. j = image->width;
  121. dst = dst1;
  122. s = src;
  123. /* write leading bits */
  124. if (start_index) {
  125. u32 start_mask = ~(FB_SHIFT_HIGH(~(u32)0,start_index));
  126. val = *dst & start_mask;
  127. shift = start_index;
  128. }
  129. while (j--) {
  130. l--;
  131. color = (*s & (1 << l)) ? fgcolor : bgcolor;
  132. val |= FB_SHIFT_HIGH(color, shift);
  133. /* Did the bitshift spill bits to the next long? */
  134. if (shift >= null_bits) {
  135. *dst++ = val;
  136. val = (shift == null_bits) ? 0 :
  137. FB_SHIFT_LOW(color,32 - shift);
  138. }
  139. shift += bpp;
  140. shift &= (32 - 1);
  141. if (!l) { l = 8; s++; };
  142. }
  143. /* write trailing bits */
  144. if (shift) {
  145. u32 end_mask = FB_SHIFT_HIGH(~(u32)0, shift);
  146. *dst &= end_mask;
  147. *dst |= val;
  148. }
  149. dst1 += pitch;
  150. src += spitch;
  151. if (pitch_index) {
  152. dst2 += pitch;
  153. dst1 = (u8 *)((long)dst2 & ~(sizeof(u32) - 1));
  154. start_index += pitch_index;
  155. start_index &= 32 - 1;
  156. }
  157. }
  158. }
  159. /*
  160. * fast_imageblit - optimized monochrome color expansion
  161. *
  162. * Only if: bits_per_pixel == 8, 16, or 32
  163. * image->width is divisible by pixel/dword (ppw);
  164. * fix->line_legth is divisible by 4;
  165. * beginning and end of a scanline is dword aligned
  166. */
  167. static void fast_imageblit(const struct fb_image *image, struct fb_info *p,
  168. void *dst1, u32 fgcolor, u32 bgcolor)
  169. {
  170. u32 fgx = fgcolor, bgx = bgcolor, bpp = p->var.bits_per_pixel;
  171. u32 ppw = 32/bpp, spitch = (image->width + 7)/8;
  172. u32 bit_mask, end_mask, eorx, shift;
  173. const char *s = image->data, *src;
  174. u32 *dst;
  175. const u32 *tab = NULL;
  176. int i, j, k;
  177. switch (bpp) {
  178. case 8:
  179. tab = cfb_tab8;
  180. break;
  181. case 16:
  182. tab = cfb_tab16;
  183. break;
  184. case 32:
  185. default:
  186. tab = cfb_tab32;
  187. break;
  188. }
  189. for (i = ppw-1; i--; ) {
  190. fgx <<= bpp;
  191. bgx <<= bpp;
  192. fgx |= fgcolor;
  193. bgx |= bgcolor;
  194. }
  195. bit_mask = (1 << ppw) - 1;
  196. eorx = fgx ^ bgx;
  197. k = image->width/ppw;
  198. for (i = image->height; i--; ) {
  199. dst = dst1;
  200. shift = 8;
  201. src = s;
  202. for (j = k; j--; ) {
  203. shift -= ppw;
  204. end_mask = tab[(*src >> shift) & bit_mask];
  205. *dst++ = (end_mask & eorx) ^ bgx;
  206. if (!shift) {
  207. shift = 8;
  208. src++;
  209. }
  210. }
  211. dst1 += p->fix.line_length;
  212. s += spitch;
  213. }
  214. }
  215. void sys_imageblit(struct fb_info *p, const struct fb_image *image)
  216. {
  217. u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
  218. u32 bpl = sizeof(u32), bpp = p->var.bits_per_pixel;
  219. u32 width = image->width;
  220. u32 dx = image->dx, dy = image->dy;
  221. void *dst1;
  222. if (p->state != FBINFO_STATE_RUNNING)
  223. return;
  224. bitstart = (dy * p->fix.line_length * 8) + (dx * bpp);
  225. start_index = bitstart & (32 - 1);
  226. pitch_index = (p->fix.line_length & (bpl - 1)) * 8;
  227. bitstart /= 8;
  228. bitstart &= ~(bpl - 1);
  229. dst1 = (void __force *)p->screen_base + bitstart;
  230. if (p->fbops->fb_sync)
  231. p->fbops->fb_sync(p);
  232. if (image->depth == 1) {
  233. if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
  234. p->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  235. fgcolor = ((u32*)(p->pseudo_palette))[image->fg_color];
  236. bgcolor = ((u32*)(p->pseudo_palette))[image->bg_color];
  237. } else {
  238. fgcolor = image->fg_color;
  239. bgcolor = image->bg_color;
  240. }
  241. if (32 % bpp == 0 && !start_index && !pitch_index &&
  242. ((width & (32/bpp-1)) == 0) &&
  243. bpp >= 8 && bpp <= 32)
  244. fast_imageblit(image, p, dst1, fgcolor, bgcolor);
  245. else
  246. slow_imageblit(image, p, dst1, fgcolor, bgcolor,
  247. start_index, pitch_index);
  248. } else
  249. color_imageblit(image, p, dst1, start_index, pitch_index);
  250. }
  251. EXPORT_SYMBOL(sys_imageblit);
  252. MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
  253. MODULE_DESCRIPTION("1-bit/8-bit to 1-32 bit color expansion (sys-to-sys)");
  254. MODULE_LICENSE("GPL");