syscopyarea.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Generic Bit Block Transfer for frame buffers located in system RAM with
  3. * packed pixels of any depth.
  4. *
  5. * Based almost entirely from cfbcopyarea.c (which is based almost entirely
  6. * on Geert Uytterhoeven's copyarea 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. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/fb.h>
  19. #include <asm/types.h>
  20. #include <asm/io.h>
  21. #include "fb_draw.h"
  22. /*
  23. * Generic bitwise copy algorithm
  24. */
  25. static void
  26. bitcpy(struct fb_info *p, unsigned long *dst, unsigned dst_idx,
  27. const unsigned long *src, unsigned src_idx, int bits, unsigned n)
  28. {
  29. unsigned long first, last;
  30. int const shift = dst_idx-src_idx;
  31. int left, right;
  32. first = FB_SHIFT_HIGH(p, ~0UL, dst_idx);
  33. last = ~(FB_SHIFT_HIGH(p, ~0UL, (dst_idx+n) % bits));
  34. if (!shift) {
  35. /* Same alignment for source and dest */
  36. if (dst_idx+n <= bits) {
  37. /* Single word */
  38. if (last)
  39. first &= last;
  40. *dst = comp(*src, *dst, first);
  41. } else {
  42. /* Multiple destination words */
  43. /* Leading bits */
  44. if (first != ~0UL) {
  45. *dst = comp(*src, *dst, first);
  46. dst++;
  47. src++;
  48. n -= bits - dst_idx;
  49. }
  50. /* Main chunk */
  51. n /= bits;
  52. while (n >= 8) {
  53. *dst++ = *src++;
  54. *dst++ = *src++;
  55. *dst++ = *src++;
  56. *dst++ = *src++;
  57. *dst++ = *src++;
  58. *dst++ = *src++;
  59. *dst++ = *src++;
  60. *dst++ = *src++;
  61. n -= 8;
  62. }
  63. while (n--)
  64. *dst++ = *src++;
  65. /* Trailing bits */
  66. if (last)
  67. *dst = comp(*src, *dst, last);
  68. }
  69. } else {
  70. unsigned long d0, d1;
  71. int m;
  72. /* Different alignment for source and dest */
  73. right = shift & (bits - 1);
  74. left = -shift & (bits - 1);
  75. if (dst_idx+n <= bits) {
  76. /* Single destination word */
  77. if (last)
  78. first &= last;
  79. if (shift > 0) {
  80. /* Single source word */
  81. *dst = comp(*src << left, *dst, first);
  82. } else if (src_idx+n <= bits) {
  83. /* Single source word */
  84. *dst = comp(*src >> right, *dst, first);
  85. } else {
  86. /* 2 source words */
  87. d0 = *src++;
  88. d1 = *src;
  89. *dst = comp(d0 >> right | d1 << left, *dst,
  90. first);
  91. }
  92. } else {
  93. /* Multiple destination words */
  94. /** We must always remember the last value read,
  95. because in case SRC and DST overlap bitwise (e.g.
  96. when moving just one pixel in 1bpp), we always
  97. collect one full long for DST and that might
  98. overlap with the current long from SRC. We store
  99. this value in 'd0'. */
  100. d0 = *src++;
  101. /* Leading bits */
  102. if (shift > 0) {
  103. /* Single source word */
  104. *dst = comp(d0 << left, *dst, first);
  105. dst++;
  106. n -= bits - dst_idx;
  107. } else {
  108. /* 2 source words */
  109. d1 = *src++;
  110. *dst = comp(d0 >> right | d1 << left, *dst,
  111. first);
  112. d0 = d1;
  113. dst++;
  114. n -= bits - dst_idx;
  115. }
  116. /* Main chunk */
  117. m = n % bits;
  118. n /= bits;
  119. while (n >= 4) {
  120. d1 = *src++;
  121. *dst++ = d0 >> right | d1 << left;
  122. d0 = d1;
  123. d1 = *src++;
  124. *dst++ = d0 >> right | d1 << left;
  125. d0 = d1;
  126. d1 = *src++;
  127. *dst++ = d0 >> right | d1 << left;
  128. d0 = d1;
  129. d1 = *src++;
  130. *dst++ = d0 >> right | d1 << left;
  131. d0 = d1;
  132. n -= 4;
  133. }
  134. while (n--) {
  135. d1 = *src++;
  136. *dst++ = d0 >> right | d1 << left;
  137. d0 = d1;
  138. }
  139. /* Trailing bits */
  140. if (m) {
  141. if (m <= bits - right) {
  142. /* Single source word */
  143. d0 >>= right;
  144. } else {
  145. /* 2 source words */
  146. d1 = *src;
  147. d0 = d0 >> right | d1 << left;
  148. }
  149. *dst = comp(d0, *dst, last);
  150. }
  151. }
  152. }
  153. }
  154. /*
  155. * Generic bitwise copy algorithm, operating backward
  156. */
  157. static void
  158. bitcpy_rev(struct fb_info *p, unsigned long *dst, unsigned dst_idx,
  159. const unsigned long *src, unsigned src_idx, unsigned bits,
  160. unsigned n)
  161. {
  162. unsigned long first, last;
  163. int shift;
  164. dst += (dst_idx + n - 1) / bits;
  165. src += (src_idx + n - 1) / bits;
  166. dst_idx = (dst_idx + n - 1) % bits;
  167. src_idx = (src_idx + n - 1) % bits;
  168. shift = dst_idx-src_idx;
  169. first = ~FB_SHIFT_HIGH(p, ~0UL, (dst_idx + 1) % bits);
  170. last = FB_SHIFT_HIGH(p, ~0UL, (bits + dst_idx + 1 - n) % bits);
  171. if (!shift) {
  172. /* Same alignment for source and dest */
  173. if ((unsigned long)dst_idx+1 >= n) {
  174. /* Single word */
  175. if (first)
  176. last &= first;
  177. *dst = comp(*src, *dst, last);
  178. } else {
  179. /* Multiple destination words */
  180. /* Leading bits */
  181. if (first) {
  182. *dst = comp(*src, *dst, first);
  183. dst--;
  184. src--;
  185. n -= dst_idx+1;
  186. }
  187. /* Main chunk */
  188. n /= bits;
  189. while (n >= 8) {
  190. *dst-- = *src--;
  191. *dst-- = *src--;
  192. *dst-- = *src--;
  193. *dst-- = *src--;
  194. *dst-- = *src--;
  195. *dst-- = *src--;
  196. *dst-- = *src--;
  197. *dst-- = *src--;
  198. n -= 8;
  199. }
  200. while (n--)
  201. *dst-- = *src--;
  202. /* Trailing bits */
  203. if (last != -1UL)
  204. *dst = comp(*src, *dst, last);
  205. }
  206. } else {
  207. /* Different alignment for source and dest */
  208. int const left = shift & (bits-1);
  209. int const right = -shift & (bits-1);
  210. if ((unsigned long)dst_idx+1 >= n) {
  211. /* Single destination word */
  212. if (first)
  213. last &= first;
  214. if (shift < 0) {
  215. /* Single source word */
  216. *dst = comp(*src >> right, *dst, last);
  217. } else if (1+(unsigned long)src_idx >= n) {
  218. /* Single source word */
  219. *dst = comp(*src << left, *dst, last);
  220. } else {
  221. /* 2 source words */
  222. *dst = comp(*src << left | *(src-1) >> right,
  223. *dst, last);
  224. }
  225. } else {
  226. /* Multiple destination words */
  227. /** We must always remember the last value read,
  228. because in case SRC and DST overlap bitwise (e.g.
  229. when moving just one pixel in 1bpp), we always
  230. collect one full long for DST and that might
  231. overlap with the current long from SRC. We store
  232. this value in 'd0'. */
  233. unsigned long d0, d1;
  234. int m;
  235. d0 = *src--;
  236. /* Leading bits */
  237. if (shift < 0) {
  238. /* Single source word */
  239. d1 = d0;
  240. d0 >>= right;
  241. } else {
  242. /* 2 source words */
  243. d1 = *src--;
  244. d0 = d0 << left | d1 >> right;
  245. }
  246. if (!first)
  247. *dst = d0;
  248. else
  249. *dst = comp(d0, *dst, first);
  250. d0 = d1;
  251. dst--;
  252. n -= dst_idx+1;
  253. /* Main chunk */
  254. m = n % bits;
  255. n /= bits;
  256. while (n >= 4) {
  257. d1 = *src--;
  258. *dst-- = d0 << left | d1 >> right;
  259. d0 = d1;
  260. d1 = *src--;
  261. *dst-- = d0 << left | d1 >> right;
  262. d0 = d1;
  263. d1 = *src--;
  264. *dst-- = d0 << left | d1 >> right;
  265. d0 = d1;
  266. d1 = *src--;
  267. *dst-- = d0 << left | d1 >> right;
  268. d0 = d1;
  269. n -= 4;
  270. }
  271. while (n--) {
  272. d1 = *src--;
  273. *dst-- = d0 << left | d1 >> right;
  274. d0 = d1;
  275. }
  276. /* Trailing bits */
  277. if (m) {
  278. if (m <= bits - left) {
  279. /* Single source word */
  280. d0 <<= left;
  281. } else {
  282. /* 2 source words */
  283. d1 = *src;
  284. d0 = d0 << left | d1 >> right;
  285. }
  286. *dst = comp(d0, *dst, last);
  287. }
  288. }
  289. }
  290. }
  291. void sys_copyarea(struct fb_info *p, const struct fb_copyarea *area)
  292. {
  293. u32 dx = area->dx, dy = area->dy, sx = area->sx, sy = area->sy;
  294. u32 height = area->height, width = area->width;
  295. unsigned long const bits_per_line = p->fix.line_length*8u;
  296. unsigned long *base = NULL;
  297. int bits = BITS_PER_LONG, bytes = bits >> 3;
  298. unsigned dst_idx = 0, src_idx = 0, rev_copy = 0;
  299. if (p->state != FBINFO_STATE_RUNNING)
  300. return;
  301. /* if the beginning of the target area might overlap with the end of
  302. the source area, be have to copy the area reverse. */
  303. if ((dy == sy && dx > sx) || (dy > sy)) {
  304. dy += height;
  305. sy += height;
  306. rev_copy = 1;
  307. }
  308. /* split the base of the framebuffer into a long-aligned address and
  309. the index of the first bit */
  310. base = (unsigned long *)((unsigned long)p->screen_base & ~(bytes-1));
  311. dst_idx = src_idx = 8*((unsigned long)p->screen_base & (bytes-1));
  312. /* add offset of source and target area */
  313. dst_idx += dy*bits_per_line + dx*p->var.bits_per_pixel;
  314. src_idx += sy*bits_per_line + sx*p->var.bits_per_pixel;
  315. if (p->fbops->fb_sync)
  316. p->fbops->fb_sync(p);
  317. if (rev_copy) {
  318. while (height--) {
  319. dst_idx -= bits_per_line;
  320. src_idx -= bits_per_line;
  321. bitcpy_rev(p, base + (dst_idx / bits), dst_idx % bits,
  322. base + (src_idx / bits), src_idx % bits, bits,
  323. width*p->var.bits_per_pixel);
  324. }
  325. } else {
  326. while (height--) {
  327. bitcpy(p, base + (dst_idx / bits), dst_idx % bits,
  328. base + (src_idx / bits), src_idx % bits, bits,
  329. width*p->var.bits_per_pixel);
  330. dst_idx += bits_per_line;
  331. src_idx += bits_per_line;
  332. }
  333. }
  334. }
  335. EXPORT_SYMBOL(sys_copyarea);
  336. MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
  337. MODULE_DESCRIPTION("Generic copyarea (sys-to-sys)");
  338. MODULE_LICENSE("GPL");