sysfillrect.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Generic fillrect for frame buffers in system RAM with packed pixels of
  3. * any depth.
  4. *
  5. * Based almost entirely from cfbfillrect.c (which is based almost entirely
  6. * on Geert Uytterhoeven's fillrect routine)
  7. *
  8. * Copyright (C) 2007 Antonino Daplas <adaplas@pol.net>
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file COPYING in the main directory of this archive for
  12. * more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/string.h>
  16. #include <linux/fb.h>
  17. #include <asm/types.h>
  18. #include "fb_draw.h"
  19. /*
  20. * Aligned pattern fill using 32/64-bit memory accesses
  21. */
  22. static void
  23. bitfill_aligned(unsigned long *dst, int dst_idx, unsigned long pat,
  24. unsigned n, int bits)
  25. {
  26. unsigned long first, last;
  27. if (!n)
  28. return;
  29. first = FB_SHIFT_HIGH(~0UL, dst_idx);
  30. last = ~(FB_SHIFT_HIGH(~0UL, (dst_idx+n) % bits));
  31. if (dst_idx+n <= bits) {
  32. /* Single word */
  33. if (last)
  34. first &= last;
  35. *dst = comp(pat, *dst, first);
  36. } else {
  37. /* Multiple destination words */
  38. /* Leading bits */
  39. if (first!= ~0UL) {
  40. *dst = comp(pat, *dst, first);
  41. dst++;
  42. n -= bits - dst_idx;
  43. }
  44. /* Main chunk */
  45. n /= bits;
  46. while (n >= 8) {
  47. *dst++ = pat;
  48. *dst++ = pat;
  49. *dst++ = pat;
  50. *dst++ = pat;
  51. *dst++ = pat;
  52. *dst++ = pat;
  53. *dst++ = pat;
  54. *dst++ = pat;
  55. n -= 8;
  56. }
  57. while (n--)
  58. *dst++ = pat;
  59. /* Trailing bits */
  60. if (last)
  61. *dst = comp(pat, *dst, last);
  62. }
  63. }
  64. /*
  65. * Unaligned generic pattern fill using 32/64-bit memory accesses
  66. * The pattern must have been expanded to a full 32/64-bit value
  67. * Left/right are the appropriate shifts to convert to the pattern to be
  68. * used for the next 32/64-bit word
  69. */
  70. static void
  71. bitfill_unaligned(unsigned long *dst, int dst_idx, unsigned long pat,
  72. int left, int right, unsigned n, int bits)
  73. {
  74. unsigned long first, last;
  75. if (!n)
  76. return;
  77. first = FB_SHIFT_HIGH(~0UL, dst_idx);
  78. last = ~(FB_SHIFT_HIGH(~0UL, (dst_idx+n) % bits));
  79. if (dst_idx+n <= bits) {
  80. /* Single word */
  81. if (last)
  82. first &= last;
  83. *dst = comp(pat, *dst, first);
  84. } else {
  85. /* Multiple destination words */
  86. /* Leading bits */
  87. if (first) {
  88. *dst = comp(pat, *dst, first);
  89. dst++;
  90. pat = pat << left | pat >> right;
  91. n -= bits - dst_idx;
  92. }
  93. /* Main chunk */
  94. n /= bits;
  95. while (n >= 4) {
  96. *dst++ = pat;
  97. pat = pat << left | pat >> right;
  98. *dst++ = pat;
  99. pat = pat << left | pat >> right;
  100. *dst++ = pat;
  101. pat = pat << left | pat >> right;
  102. *dst++ = pat;
  103. pat = pat << left | pat >> right;
  104. n -= 4;
  105. }
  106. while (n--) {
  107. *dst++ = pat;
  108. pat = pat << left | pat >> right;
  109. }
  110. /* Trailing bits */
  111. if (last)
  112. *dst = comp(pat, *dst, first);
  113. }
  114. }
  115. /*
  116. * Aligned pattern invert using 32/64-bit memory accesses
  117. */
  118. static void
  119. bitfill_aligned_rev(unsigned long *dst, int dst_idx, unsigned long pat,
  120. unsigned n, int bits)
  121. {
  122. unsigned long val = pat;
  123. unsigned long first, last;
  124. if (!n)
  125. return;
  126. first = FB_SHIFT_HIGH(~0UL, dst_idx);
  127. last = ~(FB_SHIFT_HIGH(~0UL, (dst_idx+n) % bits));
  128. if (dst_idx+n <= bits) {
  129. /* Single word */
  130. if (last)
  131. first &= last;
  132. *dst = comp(*dst ^ val, *dst, first);
  133. } else {
  134. /* Multiple destination words */
  135. /* Leading bits */
  136. if (first!=0UL) {
  137. *dst = comp(*dst ^ val, *dst, first);
  138. dst++;
  139. n -= bits - dst_idx;
  140. }
  141. /* Main chunk */
  142. n /= bits;
  143. while (n >= 8) {
  144. *dst++ ^= val;
  145. *dst++ ^= val;
  146. *dst++ ^= val;
  147. *dst++ ^= val;
  148. *dst++ ^= val;
  149. *dst++ ^= val;
  150. *dst++ ^= val;
  151. *dst++ ^= val;
  152. n -= 8;
  153. }
  154. while (n--)
  155. *dst++ ^= val;
  156. /* Trailing bits */
  157. if (last)
  158. *dst = comp(*dst ^ val, *dst, last);
  159. }
  160. }
  161. /*
  162. * Unaligned generic pattern invert using 32/64-bit memory accesses
  163. * The pattern must have been expanded to a full 32/64-bit value
  164. * Left/right are the appropriate shifts to convert to the pattern to be
  165. * used for the next 32/64-bit word
  166. */
  167. static void
  168. bitfill_unaligned_rev(unsigned long *dst, int dst_idx, unsigned long pat,
  169. int left, int right, unsigned n, int bits)
  170. {
  171. unsigned long first, last;
  172. if (!n)
  173. return;
  174. first = FB_SHIFT_HIGH(~0UL, dst_idx);
  175. last = ~(FB_SHIFT_HIGH(~0UL, (dst_idx+n) % bits));
  176. if (dst_idx+n <= bits) {
  177. /* Single word */
  178. if (last)
  179. first &= last;
  180. *dst = comp(*dst ^ pat, *dst, first);
  181. } else {
  182. /* Multiple destination words */
  183. /* Leading bits */
  184. if (first != 0UL) {
  185. *dst = comp(*dst ^ pat, *dst, first);
  186. dst++;
  187. pat = pat << left | pat >> right;
  188. n -= bits - dst_idx;
  189. }
  190. /* Main chunk */
  191. n /= bits;
  192. while (n >= 4) {
  193. *dst++ ^= pat;
  194. pat = pat << left | pat >> right;
  195. *dst++ ^= pat;
  196. pat = pat << left | pat >> right;
  197. *dst++ ^= pat;
  198. pat = pat << left | pat >> right;
  199. *dst++ ^= pat;
  200. pat = pat << left | pat >> right;
  201. n -= 4;
  202. }
  203. while (n--) {
  204. *dst ^= pat;
  205. pat = pat << left | pat >> right;
  206. }
  207. /* Trailing bits */
  208. if (last)
  209. *dst = comp(*dst ^ pat, *dst, last);
  210. }
  211. }
  212. void sys_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
  213. {
  214. unsigned long pat, fg;
  215. unsigned long width = rect->width, height = rect->height;
  216. int bits = BITS_PER_LONG, bytes = bits >> 3;
  217. u32 bpp = p->var.bits_per_pixel;
  218. unsigned long *dst;
  219. int dst_idx, left;
  220. if (p->state != FBINFO_STATE_RUNNING)
  221. return;
  222. if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
  223. p->fix.visual == FB_VISUAL_DIRECTCOLOR )
  224. fg = ((u32 *) (p->pseudo_palette))[rect->color];
  225. else
  226. fg = rect->color;
  227. pat = pixel_to_pat( bpp, fg);
  228. dst = (unsigned long *)((unsigned long)p->screen_base & ~(bytes-1));
  229. dst_idx = ((unsigned long)p->screen_base & (bytes - 1))*8;
  230. dst_idx += rect->dy*p->fix.line_length*8+rect->dx*bpp;
  231. /* FIXME For now we support 1-32 bpp only */
  232. left = bits % bpp;
  233. if (p->fbops->fb_sync)
  234. p->fbops->fb_sync(p);
  235. if (!left) {
  236. void (*fill_op32)(unsigned long *dst, int dst_idx,
  237. unsigned long pat, unsigned n, int bits) =
  238. NULL;
  239. switch (rect->rop) {
  240. case ROP_XOR:
  241. fill_op32 = bitfill_aligned_rev;
  242. break;
  243. case ROP_COPY:
  244. fill_op32 = bitfill_aligned;
  245. break;
  246. default:
  247. printk( KERN_ERR "cfb_fillrect(): unknown rop, "
  248. "defaulting to ROP_COPY\n");
  249. fill_op32 = bitfill_aligned;
  250. break;
  251. }
  252. while (height--) {
  253. dst += dst_idx >> (ffs(bits) - 1);
  254. dst_idx &= (bits - 1);
  255. fill_op32(dst, dst_idx, pat, width*bpp, bits);
  256. dst_idx += p->fix.line_length*8;
  257. }
  258. } else {
  259. int right;
  260. int r;
  261. int rot = (left-dst_idx) % bpp;
  262. void (*fill_op)(unsigned long *dst, int dst_idx,
  263. unsigned long pat, int left, int right,
  264. unsigned n, int bits) = NULL;
  265. /* rotate pattern to correct start position */
  266. pat = pat << rot | pat >> (bpp-rot);
  267. right = bpp-left;
  268. switch (rect->rop) {
  269. case ROP_XOR:
  270. fill_op = bitfill_unaligned_rev;
  271. break;
  272. case ROP_COPY:
  273. fill_op = bitfill_unaligned;
  274. break;
  275. default:
  276. printk(KERN_ERR "cfb_fillrect(): unknown rop, "
  277. "defaulting to ROP_COPY\n");
  278. fill_op = bitfill_unaligned;
  279. break;
  280. }
  281. while (height--) {
  282. dst += dst_idx >> (ffs(bits) - 1);
  283. dst_idx &= (bits - 1);
  284. fill_op(dst, dst_idx, pat, left, right,
  285. width*bpp, bits);
  286. r = (p->fix.line_length*8) % bpp;
  287. pat = pat << (bpp-r) | pat >> r;
  288. dst_idx += p->fix.line_length*8;
  289. }
  290. }
  291. }
  292. EXPORT_SYMBOL(sys_fillrect);
  293. MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
  294. MODULE_DESCRIPTION("Generic fill rectangle (sys-to-sys)");
  295. MODULE_LICENSE("GPL");