cfbfillrect.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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(unsigned long __iomem *dst, int dst_idx, unsigned long pat, unsigned n, int bits)
  37. {
  38. unsigned long first, last;
  39. if (!n)
  40. return;
  41. first = FB_SHIFT_HIGH(~0UL, dst_idx);
  42. last = ~(FB_SHIFT_HIGH(~0UL, (dst_idx+n) % bits));
  43. if (dst_idx+n <= bits) {
  44. // Single word
  45. if (last)
  46. first &= last;
  47. FB_WRITEL(comp(pat, FB_READL(dst), first), dst);
  48. } else {
  49. // Multiple destination words
  50. // Leading bits
  51. if (first!= ~0UL) {
  52. FB_WRITEL(comp(pat, FB_READL(dst), first), dst);
  53. dst++;
  54. n -= bits - dst_idx;
  55. }
  56. // Main chunk
  57. n /= bits;
  58. while (n >= 8) {
  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. FB_WRITEL(pat, dst++);
  65. FB_WRITEL(pat, dst++);
  66. FB_WRITEL(pat, dst++);
  67. n -= 8;
  68. }
  69. while (n--)
  70. FB_WRITEL(pat, dst++);
  71. // Trailing bits
  72. if (last)
  73. FB_WRITEL(comp(pat, FB_READL(dst), last), dst);
  74. }
  75. }
  76. /*
  77. * Unaligned generic pattern fill using 32/64-bit memory accesses
  78. * The pattern must have been expanded to a full 32/64-bit value
  79. * Left/right are the appropriate shifts to convert to the pattern to be
  80. * used for the next 32/64-bit word
  81. */
  82. static void
  83. bitfill_unaligned(unsigned long __iomem *dst, int dst_idx, unsigned long pat,
  84. int left, int right, unsigned n, int bits)
  85. {
  86. unsigned long first, last;
  87. if (!n)
  88. return;
  89. first = FB_SHIFT_HIGH(~0UL, dst_idx);
  90. last = ~(FB_SHIFT_HIGH(~0UL, (dst_idx+n) % bits));
  91. if (dst_idx+n <= bits) {
  92. // Single word
  93. if (last)
  94. first &= last;
  95. FB_WRITEL(comp(pat, FB_READL(dst), first), dst);
  96. } else {
  97. // Multiple destination words
  98. // Leading bits
  99. if (first) {
  100. FB_WRITEL(comp(pat, FB_READL(dst), first), dst);
  101. dst++;
  102. pat = pat << left | pat >> right;
  103. n -= bits - dst_idx;
  104. }
  105. // Main chunk
  106. n /= bits;
  107. while (n >= 4) {
  108. FB_WRITEL(pat, dst++);
  109. pat = pat << left | pat >> right;
  110. FB_WRITEL(pat, dst++);
  111. pat = pat << left | pat >> right;
  112. FB_WRITEL(pat, dst++);
  113. pat = pat << left | pat >> right;
  114. FB_WRITEL(pat, dst++);
  115. pat = pat << left | pat >> right;
  116. n -= 4;
  117. }
  118. while (n--) {
  119. FB_WRITEL(pat, dst++);
  120. pat = pat << left | pat >> right;
  121. }
  122. // Trailing bits
  123. if (last)
  124. FB_WRITEL(comp(pat, FB_READL(dst), first), dst);
  125. }
  126. }
  127. /*
  128. * Aligned pattern invert using 32/64-bit memory accesses
  129. */
  130. static void
  131. bitfill_aligned_rev(unsigned long __iomem *dst, int dst_idx, unsigned long pat, unsigned n, int bits)
  132. {
  133. unsigned long val = pat, dat;
  134. unsigned long first, last;
  135. if (!n)
  136. return;
  137. first = FB_SHIFT_HIGH(~0UL, dst_idx);
  138. last = ~(FB_SHIFT_HIGH(~0UL, (dst_idx+n) % bits));
  139. if (dst_idx+n <= bits) {
  140. // Single word
  141. if (last)
  142. first &= last;
  143. dat = FB_READL(dst);
  144. FB_WRITEL(comp(dat ^ val, dat, first), dst);
  145. } else {
  146. // Multiple destination words
  147. // Leading bits
  148. if (first!=0UL) {
  149. dat = FB_READL(dst);
  150. FB_WRITEL(comp(dat ^ val, dat, first), dst);
  151. dst++;
  152. n -= bits - dst_idx;
  153. }
  154. // Main chunk
  155. n /= bits;
  156. while (n >= 8) {
  157. FB_WRITEL(FB_READL(dst) ^ val, dst);
  158. dst++;
  159. FB_WRITEL(FB_READL(dst) ^ val, dst);
  160. dst++;
  161. FB_WRITEL(FB_READL(dst) ^ val, dst);
  162. dst++;
  163. FB_WRITEL(FB_READL(dst) ^ val, dst);
  164. dst++;
  165. FB_WRITEL(FB_READL(dst) ^ val, dst);
  166. dst++;
  167. FB_WRITEL(FB_READL(dst) ^ val, dst);
  168. dst++;
  169. FB_WRITEL(FB_READL(dst) ^ val, dst);
  170. dst++;
  171. FB_WRITEL(FB_READL(dst) ^ val, dst);
  172. dst++;
  173. n -= 8;
  174. }
  175. while (n--) {
  176. FB_WRITEL(FB_READL(dst) ^ val, dst);
  177. dst++;
  178. }
  179. // Trailing bits
  180. if (last) {
  181. dat = FB_READL(dst);
  182. FB_WRITEL(comp(dat ^ val, dat, last), dst);
  183. }
  184. }
  185. }
  186. /*
  187. * Unaligned generic pattern invert using 32/64-bit memory accesses
  188. * The pattern must have been expanded to a full 32/64-bit value
  189. * Left/right are the appropriate shifts to convert to the pattern to be
  190. * used for the next 32/64-bit word
  191. */
  192. static void
  193. bitfill_unaligned_rev(unsigned long __iomem *dst, int dst_idx, unsigned long pat,
  194. int left, int right, unsigned n, int bits)
  195. {
  196. unsigned long first, last, dat;
  197. if (!n)
  198. return;
  199. first = FB_SHIFT_HIGH(~0UL, dst_idx);
  200. last = ~(FB_SHIFT_HIGH(~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, 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. void (*fill_op32)(unsigned long __iomem *dst, int dst_idx,
  271. unsigned long pat, unsigned n, int bits) = NULL;
  272. switch (rect->rop) {
  273. case ROP_XOR:
  274. fill_op32 = bitfill_aligned_rev;
  275. break;
  276. case ROP_COPY:
  277. fill_op32 = bitfill_aligned;
  278. break;
  279. default:
  280. printk( KERN_ERR "cfb_fillrect(): unknown rop, defaulting to ROP_COPY\n");
  281. fill_op32 = bitfill_aligned;
  282. break;
  283. }
  284. while (height--) {
  285. dst += dst_idx >> (ffs(bits) - 1);
  286. dst_idx &= (bits - 1);
  287. fill_op32(dst, dst_idx, pat, width*bpp, bits);
  288. dst_idx += p->fix.line_length*8;
  289. }
  290. } else {
  291. int right;
  292. int r;
  293. int rot = (left-dst_idx) % bpp;
  294. void (*fill_op)(unsigned long __iomem *dst, int dst_idx,
  295. unsigned long pat, int left, int right,
  296. unsigned n, int bits) = NULL;
  297. /* rotate pattern to correct start position */
  298. pat = pat << rot | pat >> (bpp-rot);
  299. right = bpp-left;
  300. switch (rect->rop) {
  301. case ROP_XOR:
  302. fill_op = bitfill_unaligned_rev;
  303. break;
  304. case ROP_COPY:
  305. fill_op = bitfill_unaligned;
  306. break;
  307. default:
  308. printk( KERN_ERR "cfb_fillrect(): unknown rop, defaulting to ROP_COPY\n");
  309. fill_op = bitfill_unaligned;
  310. break;
  311. }
  312. while (height--) {
  313. dst += dst_idx >> (ffs(bits) - 1);
  314. dst_idx &= (bits - 1);
  315. fill_op(dst, dst_idx, pat, left, right,
  316. width*bpp, bits);
  317. r = (p->fix.line_length*8) % bpp;
  318. pat = pat << (bpp-r) | pat >> r;
  319. dst_idx += p->fix.line_length*8;
  320. }
  321. }
  322. }
  323. EXPORT_SYMBOL(cfb_fillrect);
  324. MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
  325. MODULE_DESCRIPTION("Generic software accelerated fill rectangle");
  326. MODULE_LICENSE("GPL");