cfbfillrect.c 8.9 KB

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