fbseg.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * Copyright © 1998 Keith Packard
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that
  7. * copyright notice and this permission notice appear in supporting
  8. * documentation, and that the name of Keith Packard not be used in
  9. * advertising or publicity pertaining to distribution of the software without
  10. * specific, written prior permission. Keith Packard makes no
  11. * representations about the suitability of this software for any purpose. It
  12. * is provided "as is" without express or implied warranty.
  13. *
  14. * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  20. * PERFORMANCE OF THIS SOFTWARE.
  21. */
  22. #ifdef HAVE_DIX_CONFIG_H
  23. #include <dix-config.h>
  24. #endif
  25. #include <stdlib.h>
  26. #include "fb.h"
  27. #include "miline.h"
  28. #define fbBresShiftMask(mask,dir,bpp) ((bpp == FB_STIP_UNIT) ? 0 : \
  29. ((dir < 0) ? FbStipLeft(mask,bpp) : \
  30. FbStipRight(mask,bpp)))
  31. void
  32. fbBresSolid(DrawablePtr pDrawable,
  33. GCPtr pGC,
  34. int dashOffset,
  35. int signdx,
  36. int signdy,
  37. int axis, int x1, int y1, int e, int e1, int e3, int len)
  38. {
  39. FbStip *dst;
  40. FbStride dstStride;
  41. int dstBpp;
  42. int dstXoff, dstYoff;
  43. FbGCPrivPtr pPriv = fbGetGCPrivate(pGC);
  44. FbStip and = (FbStip) pPriv->and;
  45. FbStip xor = (FbStip) pPriv->xor;
  46. FbStip mask, mask0;
  47. FbStip bits;
  48. fbGetStipDrawable(pDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);
  49. dst += ((y1 + dstYoff) * dstStride);
  50. x1 = (x1 + dstXoff) * dstBpp;
  51. dst += x1 >> FB_STIP_SHIFT;
  52. x1 &= FB_STIP_MASK;
  53. mask0 = FbStipMask(0, dstBpp);
  54. mask = FbStipRight(mask0, x1);
  55. if (signdx < 0)
  56. mask0 = FbStipRight(mask0, FB_STIP_UNIT - dstBpp);
  57. if (signdy < 0)
  58. dstStride = -dstStride;
  59. if (axis == X_AXIS) {
  60. bits = 0;
  61. while (len--) {
  62. bits |= mask;
  63. mask = fbBresShiftMask(mask, signdx, dstBpp);
  64. if (!mask) {
  65. WRITE(dst, FbDoMaskRRop(READ(dst), and, xor, bits));
  66. bits = 0;
  67. dst += signdx;
  68. mask = mask0;
  69. }
  70. e += e1;
  71. if (e >= 0) {
  72. if (bits) {
  73. WRITE(dst, FbDoMaskRRop (READ(dst), and, xor, bits));
  74. bits = 0;
  75. }
  76. dst += dstStride;
  77. e += e3;
  78. }
  79. }
  80. if (bits)
  81. WRITE(dst, FbDoMaskRRop(READ(dst), and, xor, bits));
  82. }
  83. else {
  84. while (len--) {
  85. WRITE(dst, FbDoMaskRRop(READ(dst), and, xor, mask));
  86. dst += dstStride;
  87. e += e1;
  88. if (e >= 0) {
  89. e += e3;
  90. mask = fbBresShiftMask(mask, signdx, dstBpp);
  91. if (!mask) {
  92. dst += signdx;
  93. mask = mask0;
  94. }
  95. }
  96. }
  97. }
  98. fbFinishAccess(pDrawable);
  99. }
  100. void
  101. fbBresDash(DrawablePtr pDrawable,
  102. GCPtr pGC,
  103. int dashOffset,
  104. int signdx,
  105. int signdy, int axis, int x1, int y1, int e, int e1, int e3, int len)
  106. {
  107. FbStip *dst;
  108. FbStride dstStride;
  109. int dstBpp;
  110. int dstXoff, dstYoff;
  111. FbGCPrivPtr pPriv = fbGetGCPrivate(pGC);
  112. FbStip and = (FbStip) pPriv->and;
  113. FbStip xor = (FbStip) pPriv->xor;
  114. FbStip bgand = (FbStip) pPriv->bgand;
  115. FbStip bgxor = (FbStip) pPriv->bgxor;
  116. FbStip mask, mask0;
  117. FbDashDeclare;
  118. int dashlen;
  119. Bool even;
  120. Bool doOdd;
  121. fbGetStipDrawable(pDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);
  122. doOdd = pGC->lineStyle == LineDoubleDash;
  123. FbDashInit(pGC, pPriv, dashOffset, dashlen, even);
  124. dst += ((y1 + dstYoff) * dstStride);
  125. x1 = (x1 + dstXoff) * dstBpp;
  126. dst += x1 >> FB_STIP_SHIFT;
  127. x1 &= FB_STIP_MASK;
  128. mask0 = FbStipMask(0, dstBpp);
  129. mask = FbStipRight(mask0, x1);
  130. if (signdx < 0)
  131. mask0 = FbStipRight(mask0, FB_STIP_UNIT - dstBpp);
  132. if (signdy < 0)
  133. dstStride = -dstStride;
  134. while (len--) {
  135. if (even)
  136. WRITE(dst, FbDoMaskRRop(READ(dst), and, xor, mask));
  137. else if (doOdd)
  138. WRITE(dst, FbDoMaskRRop(READ(dst), bgand, bgxor, mask));
  139. if (axis == X_AXIS) {
  140. mask = fbBresShiftMask(mask, signdx, dstBpp);
  141. if (!mask) {
  142. dst += signdx;
  143. mask = mask0;
  144. }
  145. e += e1;
  146. if (e >= 0) {
  147. dst += dstStride;
  148. e += e3;
  149. }
  150. }
  151. else {
  152. dst += dstStride;
  153. e += e1;
  154. if (e >= 0) {
  155. e += e3;
  156. mask = fbBresShiftMask(mask, signdx, dstBpp);
  157. if (!mask) {
  158. dst += signdx;
  159. mask = mask0;
  160. }
  161. }
  162. }
  163. FbDashStep(dashlen, even);
  164. }
  165. fbFinishAccess(pDrawable);
  166. }
  167. void
  168. fbBresFill(DrawablePtr pDrawable,
  169. GCPtr pGC,
  170. int dashOffset,
  171. int signdx,
  172. int signdy, int axis, int x1, int y1, int e, int e1, int e3, int len)
  173. {
  174. while (len--) {
  175. fbFill(pDrawable, pGC, x1, y1, 1, 1);
  176. if (axis == X_AXIS) {
  177. x1 += signdx;
  178. e += e1;
  179. if (e >= 0) {
  180. e += e3;
  181. y1 += signdy;
  182. }
  183. }
  184. else {
  185. y1 += signdy;
  186. e += e1;
  187. if (e >= 0) {
  188. e += e3;
  189. x1 += signdx;
  190. }
  191. }
  192. }
  193. }
  194. static void
  195. fbSetFg(DrawablePtr pDrawable, GCPtr pGC, Pixel fg)
  196. {
  197. if (fg != pGC->fgPixel) {
  198. ChangeGCVal val;
  199. val.val = fg;
  200. ChangeGC(NullClient, pGC, GCForeground, &val);
  201. ValidateGC(pDrawable, pGC);
  202. }
  203. }
  204. void
  205. fbBresFillDash(DrawablePtr pDrawable,
  206. GCPtr pGC,
  207. int dashOffset,
  208. int signdx,
  209. int signdy,
  210. int axis, int x1, int y1, int e, int e1, int e3, int len)
  211. {
  212. FbGCPrivPtr pPriv = fbGetGCPrivate(pGC);
  213. FbDashDeclare;
  214. int dashlen;
  215. Bool even;
  216. Bool doOdd;
  217. Bool doBg;
  218. Pixel fg, bg;
  219. fg = pGC->fgPixel;
  220. bg = pGC->bgPixel;
  221. /* whether to fill the odd dashes */
  222. doOdd = pGC->lineStyle == LineDoubleDash;
  223. /* whether to switch fg to bg when filling odd dashes */
  224. doBg = doOdd && (pGC->fillStyle == FillSolid ||
  225. pGC->fillStyle == FillStippled);
  226. /* compute current dash position */
  227. FbDashInit(pGC, pPriv, dashOffset, dashlen, even);
  228. while (len--) {
  229. if (even || doOdd) {
  230. if (doBg) {
  231. if (even)
  232. fbSetFg(pDrawable, pGC, fg);
  233. else
  234. fbSetFg(pDrawable, pGC, bg);
  235. }
  236. fbFill(pDrawable, pGC, x1, y1, 1, 1);
  237. }
  238. if (axis == X_AXIS) {
  239. x1 += signdx;
  240. e += e1;
  241. if (e >= 0) {
  242. e += e3;
  243. y1 += signdy;
  244. }
  245. }
  246. else {
  247. y1 += signdy;
  248. e += e1;
  249. if (e >= 0) {
  250. e += e3;
  251. x1 += signdx;
  252. }
  253. }
  254. FbDashStep(dashlen, even);
  255. }
  256. if (doBg)
  257. fbSetFg(pDrawable, pGC, fg);
  258. }
  259. static void
  260. fbBresSolid24RRop(DrawablePtr pDrawable,
  261. GCPtr pGC,
  262. int dashOffset,
  263. int signdx,
  264. int signdy,
  265. int axis, int x1, int y1, int e, int e1, int e3, int len)
  266. {
  267. FbStip *dst;
  268. FbStride dstStride;
  269. int dstBpp;
  270. int dstXoff, dstYoff;
  271. FbGCPrivPtr pPriv = fbGetGCPrivate(pGC);
  272. FbStip and = pPriv->and;
  273. FbStip xor = pPriv->xor;
  274. FbStip leftMask, rightMask;
  275. int nl;
  276. FbStip *d;
  277. int x;
  278. int rot;
  279. FbStip andT, xorT;
  280. fbGetStipDrawable(pDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);
  281. dst += ((y1 + dstYoff) * dstStride);
  282. x1 = (x1 + dstXoff) * 24;
  283. if (signdy < 0)
  284. dstStride = -dstStride;
  285. signdx *= 24;
  286. while (len--) {
  287. d = dst + (x1 >> FB_STIP_SHIFT);
  288. x = x1 & FB_STIP_MASK;
  289. rot = FbFirst24Rot(x);
  290. andT = FbRot24Stip(and, rot);
  291. xorT = FbRot24Stip(xor, rot);
  292. FbMaskStip(x, 24, leftMask, nl, rightMask);
  293. if (leftMask) {
  294. WRITE(d, FbDoMaskRRop(READ(d), andT, xorT, leftMask));
  295. d++;
  296. andT = FbNext24Stip(andT);
  297. xorT = FbNext24Stip(xorT);
  298. }
  299. if (rightMask)
  300. WRITE(d, FbDoMaskRRop(READ(d), andT, xorT, rightMask));
  301. if (axis == X_AXIS) {
  302. x1 += signdx;
  303. e += e1;
  304. if (e >= 0) {
  305. e += e3;
  306. dst += dstStride;
  307. }
  308. }
  309. else {
  310. dst += dstStride;
  311. e += e1;
  312. if (e >= 0) {
  313. e += e3;
  314. x1 += signdx;
  315. }
  316. }
  317. }
  318. fbFinishAccess(pDrawable);
  319. }
  320. static void
  321. fbBresDash24RRop(DrawablePtr pDrawable,
  322. GCPtr pGC,
  323. int dashOffset,
  324. int signdx,
  325. int signdy,
  326. int axis, int x1, int y1, int e, int e1, int e3, int len)
  327. {
  328. FbStip *dst;
  329. FbStride dstStride;
  330. int dstBpp;
  331. int dstXoff, dstYoff;
  332. FbGCPrivPtr pPriv = fbGetGCPrivate(pGC);
  333. FbStip andT, xorT;
  334. FbStip fgand = pPriv->and;
  335. FbStip fgxor = pPriv->xor;
  336. FbStip bgand = pPriv->bgand;
  337. FbStip bgxor = pPriv->bgxor;
  338. FbStip leftMask, rightMask;
  339. int nl;
  340. FbStip *d;
  341. int x;
  342. int rot;
  343. FbDashDeclare;
  344. int dashlen;
  345. Bool even;
  346. Bool doOdd;
  347. fbGetStipDrawable(pDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);
  348. doOdd = pGC->lineStyle == LineDoubleDash;
  349. /* compute current dash position */
  350. FbDashInit(pGC, pPriv, dashOffset, dashlen, even);
  351. dst += ((y1 + dstYoff) * dstStride);
  352. x1 = (x1 + dstXoff) * 24;
  353. if (signdy < 0)
  354. dstStride = -dstStride;
  355. signdx *= 24;
  356. while (len--) {
  357. if (even || doOdd) {
  358. if (even) {
  359. andT = fgand;
  360. xorT = fgxor;
  361. }
  362. else {
  363. andT = bgand;
  364. xorT = bgxor;
  365. }
  366. d = dst + (x1 >> FB_STIP_SHIFT);
  367. x = x1 & FB_STIP_MASK;
  368. rot = FbFirst24Rot(x);
  369. andT = FbRot24Stip(andT, rot);
  370. xorT = FbRot24Stip(xorT, rot);
  371. FbMaskStip(x, 24, leftMask, nl, rightMask);
  372. if (leftMask) {
  373. WRITE(d, FbDoMaskRRop(READ(d), andT, xorT, leftMask));
  374. d++;
  375. andT = FbNext24Stip(andT);
  376. xorT = FbNext24Stip(xorT);
  377. }
  378. if (rightMask)
  379. WRITE(d, FbDoMaskRRop(READ(d), andT, xorT, rightMask));
  380. }
  381. if (axis == X_AXIS) {
  382. x1 += signdx;
  383. e += e1;
  384. if (e >= 0) {
  385. e += e3;
  386. dst += dstStride;
  387. }
  388. }
  389. else {
  390. dst += dstStride;
  391. e += e1;
  392. if (e >= 0) {
  393. e += e3;
  394. x1 += signdx;
  395. }
  396. }
  397. FbDashStep(dashlen, even);
  398. }
  399. fbFinishAccess(pDrawable);
  400. }
  401. /*
  402. * For drivers that want to bail drawing some lines, this
  403. * function takes care of selecting the appropriate rasterizer
  404. * based on the contents of the specified GC.
  405. */
  406. FbBres *
  407. fbSelectBres(DrawablePtr pDrawable, GCPtr pGC)
  408. {
  409. FbGCPrivPtr pPriv = fbGetGCPrivate(pGC);
  410. int dstBpp = pDrawable->bitsPerPixel;
  411. FbBres *bres;
  412. if (pGC->lineStyle == LineSolid) {
  413. bres = fbBresFill;
  414. if (pGC->fillStyle == FillSolid) {
  415. bres = fbBresSolid;
  416. if (dstBpp == 24)
  417. bres = fbBresSolid24RRop;
  418. if (pPriv->and == 0) {
  419. switch (dstBpp) {
  420. case 8:
  421. bres = fbBresSolid8;
  422. break;
  423. case 16:
  424. bres = fbBresSolid16;
  425. break;
  426. case 24:
  427. bres = fbBresSolid24;
  428. break;
  429. case 32:
  430. bres = fbBresSolid32;
  431. break;
  432. }
  433. }
  434. }
  435. }
  436. else {
  437. bres = fbBresFillDash;
  438. if (pGC->fillStyle == FillSolid) {
  439. bres = fbBresDash;
  440. if (dstBpp == 24)
  441. bres = fbBresDash24RRop;
  442. if (pPriv->and == 0 &&
  443. (pGC->lineStyle == LineOnOffDash || pPriv->bgand == 0)) {
  444. switch (dstBpp) {
  445. case 8:
  446. bres = fbBresDash8;
  447. break;
  448. case 16:
  449. bres = fbBresDash16;
  450. break;
  451. case 24:
  452. bres = fbBresDash24;
  453. break;
  454. case 32:
  455. bres = fbBresDash32;
  456. break;
  457. }
  458. }
  459. }
  460. }
  461. return bres;
  462. }
  463. void
  464. fbBres(DrawablePtr pDrawable,
  465. GCPtr pGC,
  466. int dashOffset,
  467. int signdx,
  468. int signdy, int axis, int x1, int y1, int e, int e1, int e3, int len)
  469. {
  470. (*fbSelectBres(pDrawable, pGC)) (pDrawable, pGC, dashOffset,
  471. signdx, signdy, axis, x1, y1,
  472. e, e1, e3, len);
  473. }
  474. void
  475. fbSegment(DrawablePtr pDrawable,
  476. GCPtr pGC,
  477. int x1, int y1, int x2, int y2, Bool drawLast, int *dashOffset)
  478. {
  479. FbBres *bres;
  480. RegionPtr pClip = fbGetCompositeClip(pGC);
  481. BoxPtr pBox;
  482. int nBox;
  483. int adx; /* abs values of dx and dy */
  484. int ady;
  485. int signdx; /* sign of dx and dy */
  486. int signdy;
  487. int e, e1, e2, e3; /* bresenham error and increments */
  488. int len; /* length of segment */
  489. int axis; /* major axis */
  490. int octant;
  491. int dashoff;
  492. int doff;
  493. unsigned int bias = miGetZeroLineBias(pDrawable->pScreen);
  494. unsigned int oc1; /* outcode of point 1 */
  495. unsigned int oc2; /* outcode of point 2 */
  496. nBox = RegionNumRects(pClip);
  497. pBox = RegionRects(pClip);
  498. bres = fbSelectBres(pDrawable, pGC);
  499. CalcLineDeltas(x1, y1, x2, y2, adx, ady, signdx, signdy, 1, 1, octant);
  500. if (adx > ady) {
  501. axis = X_AXIS;
  502. e1 = ady << 1;
  503. e2 = e1 - (adx << 1);
  504. e = e1 - adx;
  505. len = adx;
  506. }
  507. else {
  508. axis = Y_AXIS;
  509. e1 = adx << 1;
  510. e2 = e1 - (ady << 1);
  511. e = e1 - ady;
  512. SetYMajorOctant(octant);
  513. len = ady;
  514. }
  515. FIXUP_ERROR(e, octant, bias);
  516. /*
  517. * Adjust error terms to compare against zero
  518. */
  519. e3 = e2 - e1;
  520. e = e - e1;
  521. /* we have bresenham parameters and two points.
  522. all we have to do now is clip and draw.
  523. */
  524. if (drawLast)
  525. len++;
  526. dashoff = *dashOffset;
  527. *dashOffset = dashoff + len;
  528. while (nBox--) {
  529. oc1 = 0;
  530. oc2 = 0;
  531. OUTCODES(oc1, x1, y1, pBox);
  532. OUTCODES(oc2, x2, y2, pBox);
  533. if ((oc1 | oc2) == 0) {
  534. (*bres) (pDrawable, pGC, dashoff,
  535. signdx, signdy, axis, x1, y1, e, e1, e3, len);
  536. break;
  537. }
  538. else if (oc1 & oc2) {
  539. pBox++;
  540. }
  541. else {
  542. int new_x1 = x1, new_y1 = y1, new_x2 = x2, new_y2 = y2;
  543. int clip1 = 0, clip2 = 0;
  544. int clipdx, clipdy;
  545. int err;
  546. if (miZeroClipLine(pBox->x1, pBox->y1, pBox->x2 - 1,
  547. pBox->y2 - 1,
  548. &new_x1, &new_y1, &new_x2, &new_y2,
  549. adx, ady, &clip1, &clip2,
  550. octant, bias, oc1, oc2) == -1) {
  551. pBox++;
  552. continue;
  553. }
  554. if (axis == X_AXIS)
  555. len = abs(new_x2 - new_x1);
  556. else
  557. len = abs(new_y2 - new_y1);
  558. if (clip2 != 0 || drawLast)
  559. len++;
  560. if (len) {
  561. /* unwind bresenham error term to first point */
  562. doff = dashoff;
  563. err = e;
  564. if (clip1) {
  565. clipdx = abs(new_x1 - x1);
  566. clipdy = abs(new_y1 - y1);
  567. if (axis == X_AXIS) {
  568. doff += clipdx;
  569. err += e3 * clipdy + e1 * clipdx;
  570. }
  571. else {
  572. doff += clipdy;
  573. err += e3 * clipdx + e1 * clipdy;
  574. }
  575. }
  576. (*bres) (pDrawable, pGC, doff,
  577. signdx, signdy, axis, new_x1, new_y1,
  578. err, e1, e3, len);
  579. }
  580. pBox++;
  581. }
  582. } /* while (nBox--) */
  583. }