sysfillrect.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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(struct fb_info *p, unsigned long *dst, int dst_idx,
  24. unsigned long pat, unsigned n, int bits)
  25. {
  26. unsigned long first, last;
  27. if (!n)
  28. return;
  29. first = FB_SHIFT_HIGH(p, ~0UL, dst_idx);
  30. last = ~(FB_SHIFT_HIGH(p, ~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(struct fb_info *p, unsigned long *dst, int dst_idx,
  72. unsigned long pat, int left, int right, unsigned n, int bits)
  73. {
  74. unsigned long first, last;
  75. if (!n)
  76. return;
  77. first = FB_SHIFT_HIGH(p, ~0UL, dst_idx);
  78. last = ~(FB_SHIFT_HIGH(p, ~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, last);
  113. }
  114. }
  115. /*
  116. * Aligned pattern invert using 32/64-bit memory accesses
  117. */
  118. static void
  119. bitfill_aligned_rev(struct fb_info *p, unsigned long *dst, int dst_idx,
  120. unsigned long pat, 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(p, ~0UL, dst_idx);
  127. last = ~(FB_SHIFT_HIGH(p, ~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(struct fb_info *p, unsigned long *dst, int dst_idx,
  169. unsigned long pat, int left, int right, unsigned n,
  170. int bits)
  171. {
  172. unsigned long first, last;
  173. if (!n)
  174. return;
  175. first = FB_SHIFT_HIGH(p, ~0UL, dst_idx);
  176. last = ~(FB_SHIFT_HIGH(p, ~0UL, (dst_idx+n) % bits));
  177. if (dst_idx+n <= bits) {
  178. /* Single word */
  179. if (last)
  180. first &= last;
  181. *dst = comp(*dst ^ pat, *dst, first);
  182. } else {
  183. /* Multiple destination words */
  184. /* Leading bits */
  185. if (first != 0UL) {
  186. *dst = comp(*dst ^ pat, *dst, first);
  187. dst++;
  188. pat = pat << left | pat >> right;
  189. n -= bits - dst_idx;
  190. }
  191. /* Main chunk */
  192. n /= bits;
  193. while (n >= 4) {
  194. *dst++ ^= pat;
  195. pat = pat << left | pat >> right;
  196. *dst++ ^= pat;
  197. pat = pat << left | pat >> right;
  198. *dst++ ^= pat;
  199. pat = pat << left | pat >> right;
  200. *dst++ ^= pat;
  201. pat = pat << left | pat >> right;
  202. n -= 4;
  203. }
  204. while (n--) {
  205. *dst ^= pat;
  206. pat = pat << left | pat >> right;
  207. }
  208. /* Trailing bits */
  209. if (last)
  210. *dst = comp(*dst ^ pat, *dst, last);
  211. }
  212. }
  213. void sys_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
  214. {
  215. unsigned long pat, pat2, fg;
  216. unsigned long width = rect->width, height = rect->height;
  217. int bits = BITS_PER_LONG, bytes = bits >> 3;
  218. u32 bpp = p->var.bits_per_pixel;
  219. unsigned long *dst;
  220. int dst_idx, left;
  221. if (p->state != FBINFO_STATE_RUNNING)
  222. return;
  223. if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
  224. p->fix.visual == FB_VISUAL_DIRECTCOLOR )
  225. fg = ((u32 *) (p->pseudo_palette))[rect->color];
  226. else
  227. fg = rect->color;
  228. pat = pixel_to_pat( bpp, fg);
  229. dst = (unsigned long *)((unsigned long)p->screen_base & ~(bytes-1));
  230. dst_idx = ((unsigned long)p->screen_base & (bytes - 1))*8;
  231. dst_idx += rect->dy*p->fix.line_length*8+rect->dx*bpp;
  232. /* FIXME For now we support 1-32 bpp only */
  233. left = bits % bpp;
  234. if (p->fbops->fb_sync)
  235. p->fbops->fb_sync(p);
  236. if (!left) {
  237. void (*fill_op32)(struct fb_info *p, unsigned long *dst,
  238. int dst_idx, unsigned long pat, unsigned n,
  239. int bits) = NULL;
  240. switch (rect->rop) {
  241. case ROP_XOR:
  242. fill_op32 = bitfill_aligned_rev;
  243. break;
  244. case ROP_COPY:
  245. fill_op32 = bitfill_aligned;
  246. break;
  247. default:
  248. printk( KERN_ERR "cfb_fillrect(): unknown rop, "
  249. "defaulting to ROP_COPY\n");
  250. fill_op32 = bitfill_aligned;
  251. break;
  252. }
  253. while (height--) {
  254. dst += dst_idx >> (ffs(bits) - 1);
  255. dst_idx &= (bits - 1);
  256. fill_op32(p, dst, dst_idx, pat, width*bpp, bits);
  257. dst_idx += p->fix.line_length*8;
  258. }
  259. } else {
  260. int right, r;
  261. void (*fill_op)(struct fb_info *p, unsigned long *dst,
  262. int dst_idx, unsigned long pat, int left,
  263. int right, unsigned n, int bits) = NULL;
  264. #ifdef __LITTLE_ENDIAN
  265. right = left;
  266. left = bpp - right;
  267. #else
  268. right = bpp - left;
  269. #endif
  270. switch (rect->rop) {
  271. case ROP_XOR:
  272. fill_op = bitfill_unaligned_rev;
  273. break;
  274. case ROP_COPY:
  275. fill_op = bitfill_unaligned;
  276. break;
  277. default:
  278. printk(KERN_ERR "sys_fillrect(): unknown rop, "
  279. "defaulting to ROP_COPY\n");
  280. fill_op = bitfill_unaligned;
  281. break;
  282. }
  283. while (height--) {
  284. dst += dst_idx / bits;
  285. dst_idx &= (bits - 1);
  286. r = dst_idx % bpp;
  287. /* rotate pattern to the correct start position */
  288. pat2 = le_long_to_cpu(rolx(cpu_to_le_long(pat), r, bpp));
  289. fill_op(p, dst, dst_idx, pat2, left, right,
  290. width*bpp, bits);
  291. dst_idx += p->fix.line_length*8;
  292. }
  293. }
  294. }
  295. EXPORT_SYMBOL(sys_fillrect);
  296. MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
  297. MODULE_DESCRIPTION("Generic fill rectangle (sys-to-sys)");
  298. MODULE_LICENSE("GPL");