fbcopy.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. *
  3. * Copyright © 1998 Keith Packard
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and its
  6. * documentation for any purpose is hereby granted without fee, provided that
  7. * the above copyright notice appear in all copies and that both that
  8. * copyright notice and this permission notice appear in supporting
  9. * documentation, and that the name of Keith Packard not be used in
  10. * advertising or publicity pertaining to distribution of the software without
  11. * specific, written prior permission. Keith Packard makes no
  12. * representations about the suitability of this software for any purpose. It
  13. * is provided "as is" without express or implied warranty.
  14. *
  15. * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17. * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  19. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  20. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21. * PERFORMANCE OF THIS SOFTWARE.
  22. */
  23. #ifdef HAVE_DIX_CONFIG_H
  24. #include <dix-config.h>
  25. #endif
  26. #include <stdlib.h>
  27. #include "fb.h"
  28. void
  29. fbCopyNtoN(DrawablePtr pSrcDrawable,
  30. DrawablePtr pDstDrawable,
  31. GCPtr pGC,
  32. BoxPtr pbox,
  33. int nbox,
  34. int dx,
  35. int dy, Bool reverse, Bool upsidedown, Pixel bitplane, void *closure)
  36. {
  37. CARD8 alu = pGC ? pGC->alu : GXcopy;
  38. FbBits pm = pGC ? fbGetGCPrivate(pGC)->pm : FB_ALLONES;
  39. FbBits *src;
  40. FbStride srcStride;
  41. int srcBpp;
  42. int srcXoff, srcYoff;
  43. FbBits *dst;
  44. FbStride dstStride;
  45. int dstBpp;
  46. int dstXoff, dstYoff;
  47. fbGetDrawable(pSrcDrawable, src, srcStride, srcBpp, srcXoff, srcYoff);
  48. fbGetDrawable(pDstDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);
  49. while (nbox--) {
  50. fbBlt(src + (pbox->y1 + dy + srcYoff) * srcStride,
  51. srcStride,
  52. (pbox->x1 + dx + srcXoff) * srcBpp,
  53. dst + (pbox->y1 + dstYoff) * dstStride,
  54. dstStride,
  55. (pbox->x1 + dstXoff) * dstBpp,
  56. (pbox->x2 - pbox->x1) * dstBpp,
  57. (pbox->y2 - pbox->y1), alu, pm, dstBpp, reverse, upsidedown);
  58. pbox++;
  59. }
  60. }
  61. void
  62. fbCopy1toN(DrawablePtr pSrcDrawable,
  63. DrawablePtr pDstDrawable,
  64. GCPtr pGC,
  65. BoxPtr pbox,
  66. int nbox,
  67. int dx,
  68. int dy, Bool reverse, Bool upsidedown, Pixel bitplane, void *closure)
  69. {
  70. FbGCPrivPtr pPriv = fbGetGCPrivate(pGC);
  71. FbBits *src;
  72. FbStride srcStride;
  73. int srcBpp;
  74. int srcXoff, srcYoff;
  75. FbBits *dst;
  76. FbStride dstStride;
  77. int dstBpp;
  78. int dstXoff, dstYoff;
  79. fbGetDrawable(pSrcDrawable, src, srcStride, srcBpp, srcXoff, srcYoff);
  80. fbGetDrawable(pDstDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);
  81. while (nbox--) {
  82. if (dstBpp == 1) {
  83. fbBlt(src + (pbox->y1 + dy + srcYoff) * srcStride,
  84. srcStride,
  85. (pbox->x1 + dx + srcXoff) * srcBpp,
  86. dst + (pbox->y1 + dstYoff) * dstStride,
  87. dstStride,
  88. (pbox->x1 + dstXoff) * dstBpp,
  89. (pbox->x2 - pbox->x1) * dstBpp,
  90. (pbox->y2 - pbox->y1),
  91. FbOpaqueStipple1Rop(pGC->alu,
  92. pGC->fgPixel, pGC->bgPixel),
  93. pPriv->pm, dstBpp, reverse, upsidedown);
  94. }
  95. else {
  96. fbBltOne((FbStip *) (src + (pbox->y1 + dy + srcYoff) * srcStride),
  97. srcStride * (FB_UNIT / FB_STIP_UNIT),
  98. (pbox->x1 + dx + srcXoff),
  99. dst + (pbox->y1 + dstYoff) * dstStride,
  100. dstStride,
  101. (pbox->x1 + dstXoff) * dstBpp,
  102. dstBpp,
  103. (pbox->x2 - pbox->x1) * dstBpp,
  104. (pbox->y2 - pbox->y1),
  105. pPriv->and, pPriv->xor, pPriv->bgand, pPriv->bgxor);
  106. }
  107. pbox++;
  108. }
  109. }
  110. void
  111. fbCopyNto1(DrawablePtr pSrcDrawable,
  112. DrawablePtr pDstDrawable,
  113. GCPtr pGC,
  114. BoxPtr pbox,
  115. int nbox,
  116. int dx,
  117. int dy, Bool reverse, Bool upsidedown, Pixel bitplane, void *closure)
  118. {
  119. FbGCPrivPtr pPriv = fbGetGCPrivate(pGC);
  120. while (nbox--) {
  121. if (pDstDrawable->bitsPerPixel == 1) {
  122. FbBits *src;
  123. FbStride srcStride;
  124. int srcBpp;
  125. int srcXoff, srcYoff;
  126. FbStip *dst;
  127. FbStride dstStride;
  128. int dstBpp;
  129. int dstXoff, dstYoff;
  130. fbGetDrawable(pSrcDrawable, src, srcStride, srcBpp, srcXoff,
  131. srcYoff);
  132. fbGetStipDrawable(pDstDrawable, dst, dstStride, dstBpp, dstXoff,
  133. dstYoff);
  134. fbBltPlane(src + (pbox->y1 + dy + srcYoff) * srcStride, srcStride,
  135. (pbox->x1 + dx + srcXoff) * srcBpp, srcBpp,
  136. dst + (pbox->y1 + dstYoff) * dstStride, dstStride,
  137. (pbox->x1 + dstXoff) * dstBpp,
  138. (pbox->x2 - pbox->x1) * srcBpp, (pbox->y2 - pbox->y1),
  139. (FbStip) pPriv->and, (FbStip) pPriv->xor,
  140. (FbStip) pPriv->bgand, (FbStip) pPriv->bgxor, bitplane);
  141. }
  142. else {
  143. FbBits *src;
  144. FbStride srcStride;
  145. int srcBpp;
  146. int srcXoff, srcYoff;
  147. FbBits *dst;
  148. FbStride dstStride;
  149. int dstBpp;
  150. int dstXoff, dstYoff;
  151. FbStip *tmp;
  152. FbStride tmpStride;
  153. int width, height;
  154. width = pbox->x2 - pbox->x1;
  155. height = pbox->y2 - pbox->y1;
  156. tmpStride = ((width + FB_STIP_MASK) >> FB_STIP_SHIFT);
  157. tmp = malloc(tmpStride * height * sizeof(FbStip));
  158. if (!tmp)
  159. return;
  160. fbGetDrawable(pSrcDrawable, src, srcStride, srcBpp, srcXoff,
  161. srcYoff);
  162. fbGetDrawable(pDstDrawable, dst, dstStride, dstBpp, dstXoff,
  163. dstYoff);
  164. fbBltPlane(src + (pbox->y1 + dy + srcYoff) * srcStride,
  165. srcStride,
  166. (pbox->x1 + dx + srcXoff) * srcBpp,
  167. srcBpp,
  168. tmp,
  169. tmpStride,
  170. 0,
  171. width * srcBpp,
  172. height,
  173. fbAndStip(GXcopy, FB_ALLONES, FB_ALLONES),
  174. fbXorStip(GXcopy, FB_ALLONES, FB_ALLONES),
  175. fbAndStip(GXcopy, 0, FB_ALLONES),
  176. fbXorStip(GXcopy, 0, FB_ALLONES), bitplane);
  177. fbBltOne(tmp,
  178. tmpStride,
  179. 0,
  180. dst + (pbox->y1 + dstYoff) * dstStride,
  181. dstStride,
  182. (pbox->x1 + dstXoff) * dstBpp,
  183. dstBpp,
  184. width * dstBpp,
  185. height,
  186. pPriv->and, pPriv->xor, pPriv->bgand, pPriv->bgxor);
  187. free(tmp);
  188. }
  189. pbox++;
  190. }
  191. }
  192. void
  193. fbCopyRegion(DrawablePtr pSrcDrawable,
  194. DrawablePtr pDstDrawable,
  195. GCPtr pGC,
  196. RegionPtr pDstRegion,
  197. int dx, int dy, fbCopyProc copyProc, Pixel bitPlane, void *closure)
  198. {
  199. int careful;
  200. Bool reverse;
  201. Bool upsidedown;
  202. BoxPtr pbox;
  203. int nbox;
  204. BoxPtr pboxNew1, pboxNew2, pboxBase, pboxNext, pboxTmp;
  205. pbox = REGION_RECTS(pDstRegion);
  206. nbox = REGION_NUM_RECTS(pDstRegion);
  207. /* XXX we have to err on the side of safety when both are windows,
  208. * because we don't know if IncludeInferiors is being used.
  209. */
  210. careful = ((pSrcDrawable == pDstDrawable) ||
  211. ((pSrcDrawable->type == DRAWABLE_WINDOW) &&
  212. (pDstDrawable->type == DRAWABLE_WINDOW)));
  213. pboxNew1 = NULL;
  214. pboxNew2 = NULL;
  215. if (careful && dy < 0) {
  216. upsidedown = TRUE;
  217. if (nbox > 1) {
  218. /* keep ordering in each band, reverse order of bands */
  219. pboxNew1 = (BoxPtr) ALLOCATE_LOCAL(sizeof(BoxRec) * nbox);
  220. if (!pboxNew1)
  221. return;
  222. pboxBase = pboxNext = pbox + nbox - 1;
  223. while (pboxBase >= pbox) {
  224. while ((pboxNext >= pbox) && (pboxBase->y1 == pboxNext->y1))
  225. pboxNext--;
  226. pboxTmp = pboxNext + 1;
  227. while (pboxTmp <= pboxBase) {
  228. *pboxNew1++ = *pboxTmp++;
  229. }
  230. pboxBase = pboxNext;
  231. }
  232. pboxNew1 -= nbox;
  233. pbox = pboxNew1;
  234. }
  235. }
  236. else {
  237. /* walk source top to bottom */
  238. upsidedown = FALSE;
  239. }
  240. if (careful && dx < 0) {
  241. /* walk source right to left */
  242. if (dy <= 0)
  243. reverse = TRUE;
  244. else
  245. reverse = FALSE;
  246. if (nbox > 1) {
  247. /* reverse order of rects in each band */
  248. pboxNew2 = (BoxPtr) ALLOCATE_LOCAL(sizeof(BoxRec) * nbox);
  249. if (!pboxNew2) {
  250. if (pboxNew1)
  251. DEALLOCATE_LOCAL(pboxNew1);
  252. return;
  253. }
  254. pboxBase = pboxNext = pbox;
  255. while (pboxBase < pbox + nbox) {
  256. while ((pboxNext < pbox + nbox) &&
  257. (pboxNext->y1 == pboxBase->y1))
  258. pboxNext++;
  259. pboxTmp = pboxNext;
  260. while (pboxTmp != pboxBase) {
  261. *pboxNew2++ = *--pboxTmp;
  262. }
  263. pboxBase = pboxNext;
  264. }
  265. pboxNew2 -= nbox;
  266. pbox = pboxNew2;
  267. }
  268. }
  269. else {
  270. /* walk source left to right */
  271. reverse = FALSE;
  272. }
  273. (*copyProc) (pSrcDrawable,
  274. pDstDrawable,
  275. pGC,
  276. pbox, nbox, dx, dy, reverse, upsidedown, bitPlane, closure);
  277. if (pboxNew1)
  278. DEALLOCATE_LOCAL(pboxNew1);
  279. if (pboxNew2)
  280. DEALLOCATE_LOCAL(pboxNew2);
  281. }
  282. RegionPtr
  283. fbDoCopy(DrawablePtr pSrcDrawable,
  284. DrawablePtr pDstDrawable,
  285. GCPtr pGC,
  286. int xIn,
  287. int yIn,
  288. int widthSrc,
  289. int heightSrc,
  290. int xOut, int yOut, fbCopyProc copyProc, Pixel bitPlane, void *closure)
  291. {
  292. RegionPtr prgnSrcClip = NULL; /* may be a new region, or just a copy */
  293. Bool freeSrcClip = FALSE;
  294. RegionPtr prgnExposed = NULL;
  295. RegionRec rgnDst;
  296. int dx;
  297. int dy;
  298. int numRects;
  299. int box_x1;
  300. int box_y1;
  301. int box_x2;
  302. int box_y2;
  303. Bool fastSrc = FALSE; /* for fast clipping with pixmap source */
  304. Bool fastDst = FALSE; /* for fast clipping with one rect dest */
  305. Bool fastExpose = FALSE; /* for fast exposures with pixmap source */
  306. /* Short cut for unmapped windows */
  307. if (pDstDrawable->type == DRAWABLE_WINDOW &&
  308. !((WindowPtr) pDstDrawable)->realized) {
  309. return NULL;
  310. }
  311. if ((pSrcDrawable != pDstDrawable) && pSrcDrawable->pScreen->SourceValidate) {
  312. (*pSrcDrawable->pScreen->SourceValidate) (pSrcDrawable, xIn, yIn,
  313. widthSrc, heightSrc);
  314. }
  315. /* Compute source clip region */
  316. if (pSrcDrawable->type == DRAWABLE_PIXMAP) {
  317. if ((pSrcDrawable == pDstDrawable) && (pGC->clientClipType == CT_NONE))
  318. prgnSrcClip = fbGetCompositeClip(pGC);
  319. else
  320. fastSrc = TRUE;
  321. }
  322. else {
  323. if (pGC->subWindowMode == IncludeInferiors) {
  324. /*
  325. * XFree86 DDX empties the border clip when the
  326. * VT is inactive, make sure the region isn't empty
  327. */
  328. if (!((WindowPtr) pSrcDrawable)->parent &&
  329. REGION_NOTEMPTY(
  330. &((WindowPtr) pSrcDrawable)->borderClip)) {
  331. /*
  332. * special case bitblt from root window in
  333. * IncludeInferiors mode; just like from a pixmap
  334. */
  335. fastSrc = TRUE;
  336. }
  337. else if ((pSrcDrawable == pDstDrawable) &&
  338. (pGC->clientClipType == CT_NONE)) {
  339. prgnSrcClip = fbGetCompositeClip(pGC);
  340. }
  341. else {
  342. prgnSrcClip = NotClippedByChildren((WindowPtr) pSrcDrawable);
  343. freeSrcClip = TRUE;
  344. }
  345. }
  346. else {
  347. prgnSrcClip = &((WindowPtr) pSrcDrawable)->clipList;
  348. }
  349. }
  350. xIn += pSrcDrawable->x;
  351. yIn += pSrcDrawable->y;
  352. xOut += pDstDrawable->x;
  353. yOut += pDstDrawable->y;
  354. box_x1 = xIn;
  355. box_y1 = yIn;
  356. box_x2 = xIn + widthSrc;
  357. box_y2 = yIn + heightSrc;
  358. dx = xIn - xOut;
  359. dy = yIn - yOut;
  360. /* Don't create a source region if we are doing a fast clip */
  361. if (fastSrc) {
  362. RegionPtr cclip;
  363. fastExpose = TRUE;
  364. /*
  365. * clip the source; if regions extend beyond the source size,
  366. * make sure exposure events get sent
  367. */
  368. if (box_x1 < pSrcDrawable->x) {
  369. box_x1 = pSrcDrawable->x;
  370. fastExpose = FALSE;
  371. }
  372. if (box_y1 < pSrcDrawable->y) {
  373. box_y1 = pSrcDrawable->y;
  374. fastExpose = FALSE;
  375. }
  376. if (box_x2 > pSrcDrawable->x + (int) pSrcDrawable->width) {
  377. box_x2 = pSrcDrawable->x + (int) pSrcDrawable->width;
  378. fastExpose = FALSE;
  379. }
  380. if (box_y2 > pSrcDrawable->y + (int) pSrcDrawable->height) {
  381. box_y2 = pSrcDrawable->y + (int) pSrcDrawable->height;
  382. fastExpose = FALSE;
  383. }
  384. /* Translate and clip the dst to the destination composite clip */
  385. box_x1 -= dx;
  386. box_x2 -= dx;
  387. box_y1 -= dy;
  388. box_y2 -= dy;
  389. /* If the destination composite clip is one rectangle we can
  390. do the clip directly. Otherwise we have to create a full
  391. blown region and call intersect */
  392. cclip = fbGetCompositeClip(pGC);
  393. if (REGION_NUM_RECTS(cclip) == 1) {
  394. BoxPtr pBox = REGION_RECTS(cclip);
  395. if (box_x1 < pBox->x1)
  396. box_x1 = pBox->x1;
  397. if (box_x2 > pBox->x2)
  398. box_x2 = pBox->x2;
  399. if (box_y1 < pBox->y1)
  400. box_y1 = pBox->y1;
  401. if (box_y2 > pBox->y2)
  402. box_y2 = pBox->y2;
  403. fastDst = TRUE;
  404. }
  405. }
  406. /* Check to see if the region is empty */
  407. if (box_x1 >= box_x2 || box_y1 >= box_y2) {
  408. REGION_NULL(&rgnDst);
  409. }
  410. else {
  411. BoxRec box;
  412. box.x1 = box_x1;
  413. box.y1 = box_y1;
  414. box.x2 = box_x2;
  415. box.y2 = box_y2;
  416. REGION_INIT(&rgnDst, &box, 1);
  417. }
  418. /* Clip against complex source if needed */
  419. if (!fastSrc) {
  420. REGION_INTERSECT(&rgnDst, &rgnDst, prgnSrcClip);
  421. REGION_TRANSLATE(&rgnDst, -dx, -dy);
  422. }
  423. /* Clip against complex dest if needed */
  424. if (!fastDst) {
  425. REGION_INTERSECT(&rgnDst, &rgnDst,
  426. fbGetCompositeClip(pGC));
  427. }
  428. /* Do bit blitting */
  429. numRects = REGION_NUM_RECTS(&rgnDst);
  430. if (numRects && widthSrc && heightSrc)
  431. fbCopyRegion(pSrcDrawable, pDstDrawable, pGC,
  432. &rgnDst, dx, dy, copyProc, bitPlane, closure);
  433. /* Pixmap sources generate a NoExposed (we return NULL to do this) */
  434. if (!fastExpose && pGC->fExpose)
  435. prgnExposed = miHandleExposures(pSrcDrawable, pDstDrawable, pGC,
  436. xIn - pSrcDrawable->x,
  437. yIn - pSrcDrawable->y,
  438. widthSrc, heightSrc,
  439. xOut - pDstDrawable->x,
  440. yOut - pDstDrawable->y,
  441. (unsigned long) bitPlane);
  442. REGION_UNINIT(&rgnDst);
  443. if (freeSrcClip)
  444. REGION_DESTROY(prgnSrcClip);
  445. fbValidateDrawable(pDstDrawable);
  446. return prgnExposed;
  447. }
  448. RegionPtr
  449. fbCopyArea(DrawablePtr pSrcDrawable,
  450. DrawablePtr pDstDrawable,
  451. GCPtr pGC,
  452. int xIn, int yIn, int widthSrc, int heightSrc, int xOut, int yOut)
  453. {
  454. fbCopyProc copy;
  455. if (pSrcDrawable->bitsPerPixel != pDstDrawable->bitsPerPixel)
  456. copy = fb24_32CopyMtoN;
  457. else
  458. copy = fbCopyNtoN;
  459. return fbDoCopy(pSrcDrawable, pDstDrawable, pGC, xIn, yIn,
  460. widthSrc, heightSrc, xOut, yOut, copy, 0, 0);
  461. }
  462. RegionPtr
  463. fbCopyPlane(DrawablePtr pSrcDrawable,
  464. DrawablePtr pDstDrawable,
  465. GCPtr pGC,
  466. int xIn,
  467. int yIn,
  468. int widthSrc,
  469. int heightSrc, int xOut, int yOut, unsigned long bitplane)
  470. {
  471. if (pSrcDrawable->bitsPerPixel > 1)
  472. return fbDoCopy(pSrcDrawable, pDstDrawable, pGC,
  473. xIn, yIn, widthSrc, heightSrc,
  474. xOut, yOut, fbCopyNto1, (Pixel) bitplane, 0);
  475. else if (bitplane & 1)
  476. return fbDoCopy(pSrcDrawable, pDstDrawable, pGC, xIn, yIn,
  477. widthSrc, heightSrc, xOut, yOut, fbCopy1toN,
  478. (Pixel) bitplane, 0);
  479. else
  480. return miHandleExposures(pSrcDrawable, pDstDrawable, pGC,
  481. xIn, yIn,
  482. widthSrc, heightSrc, xOut, yOut, bitplane);
  483. }