fbseg.c 17 KB

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