sysimgblt.c 6.8 KB

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