cfbfillrect.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Generic fillrect for frame buffers with packed pixels of any depth.
  3. *
  4. * Copyright (C) 2000 James Simmons (jsimmons@linux-fbdev.org)
  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. * Also need to add code to deal with cards endians that are different than
  13. * the native cpu endians. I also need to deal with MSB position in the word.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/string.h>
  18. #include <linux/fb.h>
  19. #include <asm/types.h>
  20. #include "fb_draw.h"
  21. #if BITS_PER_LONG == 32
  22. # define FB_WRITEL fb_writel
  23. # define FB_READL fb_readl
  24. #else
  25. # define FB_WRITEL fb_writeq
  26. # define FB_READL fb_readq
  27. #endif
  28. /*
  29. * Aligned pattern fill using 32/64-bit memory accesses
  30. */
  31. static void
  32. bitfill_aligned(struct fb_info *p, unsigned long __iomem *dst, int dst_idx,
  33. unsigned long pat, unsigned n, int bits, u32 bswapmask)
  34. {
  35. unsigned long first, last;
  36. if (!n)
  37. return;
  38. first = fb_shifted_pixels_mask_long(p, dst_idx, bswapmask);
  39. last = ~fb_shifted_pixels_mask_long(p, (dst_idx+n) % bits, bswapmask);
  40. if (dst_idx+n <= bits) {
  41. // Single word
  42. if (last)
  43. first &= last;
  44. FB_WRITEL(comp(pat, FB_READL(dst), first), dst);
  45. } else {
  46. // Multiple destination words
  47. // Leading bits
  48. if (first!= ~0UL) {
  49. FB_WRITEL(comp(pat, FB_READL(dst), first), dst);
  50. dst++;
  51. n -= bits - dst_idx;
  52. }
  53. // Main chunk
  54. n /= bits;
  55. while (n >= 8) {
  56. FB_WRITEL(pat, dst++);
  57. FB_WRITEL(pat, dst++);
  58. FB_WRITEL(pat, dst++);
  59. FB_WRITEL(pat, dst++);
  60. FB_WRITEL(pat, dst++);
  61. FB_WRITEL(pat, dst++);
  62. FB_WRITEL(pat, dst++);
  63. FB_WRITEL(pat, dst++);
  64. n -= 8;
  65. }
  66. while (n--)
  67. FB_WRITEL(pat, dst++);
  68. // Trailing bits
  69. if (last)
  70. FB_WRITEL(comp(pat, FB_READL(dst), last), dst);
  71. }
  72. }
  73. /*
  74. * Unaligned generic pattern fill using 32/64-bit memory accesses
  75. * The pattern must have been expanded to a full 32/64-bit value
  76. * Left/right are the appropriate shifts to convert to the pattern to be
  77. * used for the next 32/64-bit word
  78. */
  79. static void
  80. bitfill_unaligned(struct fb_info *p, unsigned long __iomem *dst, int dst_idx,
  81. unsigned long pat, int left, int right, unsigned n, int bits)
  82. {
  83. unsigned long first, last;
  84. if (!n)
  85. return;
  86. first = FB_SHIFT_HIGH(p, ~0UL, dst_idx);
  87. last = ~(FB_SHIFT_HIGH(p, ~0UL, (dst_idx+n) % bits));
  88. if (dst_idx+n <= bits) {
  89. // Single word
  90. if (last)
  91. first &= last;
  92. FB_WRITEL(comp(pat, FB_READL(dst), first), dst);
  93. } else {
  94. // Multiple destination words
  95. // Leading bits
  96. if (first) {
  97. FB_WRITEL(comp(pat, FB_READL(dst), first), dst);
  98. dst++;
  99. pat = pat << left | pat >> right;
  100. n -= bits - dst_idx;
  101. }
  102. // Main chunk
  103. n /= bits;
  104. while (n >= 4) {
  105. FB_WRITEL(pat, dst++);
  106. pat = pat << left | pat >> right;
  107. FB_WRITEL(pat, dst++);
  108. pat = pat << left | pat >> right;
  109. FB_WRITEL(pat, dst++);
  110. pat = pat << left | pat >> right;
  111. FB_WRITEL(pat, dst++);
  112. pat = pat << left | pat >> right;
  113. n -= 4;
  114. }
  115. while (n--) {
  116. FB_WRITEL(pat, dst++);
  117. pat = pat << left | pat >> right;
  118. }
  119. // Trailing bits
  120. if (last)
  121. FB_WRITEL(comp(pat, FB_READL(dst), last), dst);
  122. }
  123. }
  124. /*
  125. * Aligned pattern invert using 32/64-bit memory accesses
  126. */
  127. static void
  128. bitfill_aligned_rev(struct fb_info *p, unsigned long __iomem *dst,
  129. int dst_idx, unsigned long pat, unsigned n, int bits,
  130. u32 bswapmask)
  131. {
  132. unsigned long val = pat, dat;
  133. unsigned long first, last;
  134. if (!n)
  135. return;
  136. first = fb_shifted_pixels_mask_long(p, dst_idx, bswapmask);
  137. last = ~fb_shifted_pixels_mask_long(p, (dst_idx+n) % bits, bswapmask);
  138. if (dst_idx+n <= bits) {
  139. // Single word
  140. if (last)
  141. first &= last;
  142. dat = FB_READL(dst);
  143. FB_WRITEL(comp(dat ^ val, dat, first), dst);
  144. } else {
  145. // Multiple destination words
  146. // Leading bits
  147. if (first!=0UL) {
  148. dat = FB_READL(dst);
  149. FB_WRITEL(comp(dat ^ val, dat, first), dst);
  150. dst++;
  151. n -= bits - dst_idx;
  152. }
  153. // Main chunk
  154. n /= bits;
  155. while (n >= 8) {
  156. FB_WRITEL(FB_READL(dst) ^ val, dst);
  157. dst++;
  158. FB_WRITEL(FB_READL(dst) ^ val, dst);
  159. dst++;
  160. FB_WRITEL(FB_READL(dst) ^ val, dst);
  161. dst++;
  162. FB_WRITEL(FB_READL(dst) ^ val, dst);
  163. dst++;
  164. FB_WRITEL(FB_READL(dst) ^ val, dst);
  165. dst++;
  166. FB_WRITEL(FB_READL(dst) ^ val, dst);
  167. dst++;
  168. FB_WRITEL(FB_READL(dst) ^ val, dst);
  169. dst++;
  170. FB_WRITEL(FB_READL(dst) ^ val, dst);
  171. dst++;
  172. n -= 8;
  173. }
  174. while (n--) {
  175. FB_WRITEL(FB_READL(dst) ^ val, dst);
  176. dst++;
  177. }
  178. // Trailing bits
  179. if (last) {
  180. dat = FB_READL(dst);
  181. FB_WRITEL(comp(dat ^ val, dat, last), dst);
  182. }
  183. }
  184. }
  185. /*
  186. * Unaligned generic pattern invert using 32/64-bit memory accesses
  187. * The pattern must have been expanded to a full 32/64-bit value
  188. * Left/right are the appropriate shifts to convert to the pattern to be
  189. * used for the next 32/64-bit word
  190. */
  191. static void
  192. bitfill_unaligned_rev(struct fb_info *p, unsigned long __iomem *dst,
  193. int dst_idx, unsigned long pat, int left, int right,
  194. unsigned n, int bits)
  195. {
  196. unsigned long first, last, dat;
  197. if (!n)
  198. return;
  199. first = FB_SHIFT_HIGH(p, ~0UL, dst_idx);
  200. last = ~(FB_SHIFT_HIGH(p, ~0UL, (dst_idx+n) % bits));
  201. if (dst_idx+n <= bits) {
  202. // Single word
  203. if (last)
  204. first &= last;
  205. dat = FB_READL(dst);
  206. FB_WRITEL(comp(dat ^ pat, dat, first), dst);
  207. } else {
  208. // Multiple destination words
  209. // Leading bits
  210. if (first != 0UL) {
  211. dat = FB_READL(dst);
  212. FB_WRITEL(comp(dat ^ pat, dat, first), dst);
  213. dst++;
  214. pat = pat << left | pat >> right;
  215. n -= bits - dst_idx;
  216. }
  217. // Main chunk
  218. n /= bits;
  219. while (n >= 4) {
  220. FB_WRITEL(FB_READL(dst) ^ pat, dst);
  221. dst++;
  222. pat = pat << left | pat >> right;
  223. FB_WRITEL(FB_READL(dst) ^ pat, dst);
  224. dst++;
  225. pat = pat << left | pat >> right;
  226. FB_WRITEL(FB_READL(dst) ^ pat, dst);
  227. dst++;
  228. pat = pat << left | pat >> right;
  229. FB_WRITEL(FB_READL(dst) ^ pat, dst);
  230. dst++;
  231. pat = pat << left | pat >> right;
  232. n -= 4;
  233. }
  234. while (n--) {
  235. FB_WRITEL(FB_READL(dst) ^ pat, dst);
  236. dst++;
  237. pat = pat << left | pat >> right;
  238. }
  239. // Trailing bits
  240. if (last) {
  241. dat = FB_READL(dst);
  242. FB_WRITEL(comp(dat ^ pat, dat, last), dst);
  243. }
  244. }
  245. }
  246. void cfb_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
  247. {
  248. unsigned long pat, pat2, fg;
  249. unsigned long width = rect->width, height = rect->height;
  250. int bits = BITS_PER_LONG, bytes = bits >> 3;
  251. u32 bpp = p->var.bits_per_pixel;
  252. unsigned long __iomem *dst;
  253. int dst_idx, left;
  254. if (p->state != FBINFO_STATE_RUNNING)
  255. return;
  256. if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
  257. p->fix.visual == FB_VISUAL_DIRECTCOLOR )
  258. fg = ((u32 *) (p->pseudo_palette))[rect->color];
  259. else
  260. fg = rect->color;
  261. pat = pixel_to_pat(bpp, fg);
  262. dst = (unsigned long __iomem *)((unsigned long)p->screen_base & ~(bytes-1));
  263. dst_idx = ((unsigned long)p->screen_base & (bytes - 1))*8;
  264. dst_idx += rect->dy*p->fix.line_length*8+rect->dx*bpp;
  265. /* FIXME For now we support 1-32 bpp only */
  266. left = bits % bpp;
  267. if (p->fbops->fb_sync)
  268. p->fbops->fb_sync(p);
  269. if (!left) {
  270. u32 bswapmask = fb_compute_bswapmask(p);
  271. void (*fill_op32)(struct fb_info *p,
  272. unsigned long __iomem *dst, int dst_idx,
  273. unsigned long pat, unsigned n, int bits,
  274. u32 bswapmask) = NULL;
  275. switch (rect->rop) {
  276. case ROP_XOR:
  277. fill_op32 = bitfill_aligned_rev;
  278. break;
  279. case ROP_COPY:
  280. fill_op32 = bitfill_aligned;
  281. break;
  282. default:
  283. printk( KERN_ERR "cfb_fillrect(): unknown rop, defaulting to ROP_COPY\n");
  284. fill_op32 = bitfill_aligned;
  285. break;
  286. }
  287. while (height--) {
  288. dst += dst_idx >> (ffs(bits) - 1);
  289. dst_idx &= (bits - 1);
  290. fill_op32(p, dst, dst_idx, pat, width*bpp, bits,
  291. bswapmask);
  292. dst_idx += p->fix.line_length*8;
  293. }
  294. } else {
  295. int right, r;
  296. void (*fill_op)(struct fb_info *p, unsigned long __iomem *dst,
  297. int dst_idx, unsigned long pat, int left,
  298. int right, unsigned n, int bits) = NULL;
  299. #ifdef __LITTLE_ENDIAN
  300. right = left;
  301. left = bpp - right;
  302. #else
  303. right = bpp - left;
  304. #endif
  305. switch (rect->rop) {
  306. case ROP_XOR:
  307. fill_op = bitfill_unaligned_rev;
  308. break;
  309. case ROP_COPY:
  310. fill_op = bitfill_unaligned;
  311. break;
  312. default:
  313. printk(KERN_ERR "cfb_fillrect(): unknown rop, defaulting to ROP_COPY\n");
  314. fill_op = bitfill_unaligned;
  315. break;
  316. }
  317. while (height--) {
  318. dst += dst_idx / bits;
  319. dst_idx &= (bits - 1);
  320. r = dst_idx % bpp;
  321. /* rotate pattern to the correct start position */
  322. pat2 = le_long_to_cpu(rolx(cpu_to_le_long(pat), r, bpp));
  323. fill_op(p, dst, dst_idx, pat2, left, right,
  324. width*bpp, bits);
  325. dst_idx += p->fix.line_length*8;
  326. }
  327. }
  328. }
  329. EXPORT_SYMBOL(cfb_fillrect);
  330. MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
  331. MODULE_DESCRIPTION("Generic software accelerated fill rectangle");
  332. MODULE_LICENSE("GPL");